PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lincs/stats/plots.py

https://bitbucket.org/kljensen/kmj_lincs
Python | 91 lines | 64 code | 19 blank | 8 comment | 5 complexity | fe2ea0e10fd8cc5db69b7c37c08072d3 MD5 | raw file
  1. import pandas
  2. from matplotlib import pyplot
  3. import numpy
  4. import os
  5. import logging
  6. def get_best_bins_for_signal(treatment_data, signal, num_bins=100):
  7. """ Find the bins for a signal across treatments.
  8. """
  9. stddevs = []
  10. maxes = []
  11. for treatment, data in treatment_data.iteritems():
  12. c0_wells = (data.cell_count == 0)
  13. c1_wells = (data.cell_count == 1)
  14. logging.info("Creating 0/1 histogram for {0}/{1}".format(
  15. treatment, signal
  16. ))
  17. stddevs.append(
  18. data[c0_wells][signal].max() \
  19. + 10 * data[c0_wells][signal].std()
  20. )
  21. maxes.append(
  22. data[c1_wells][signal].max()
  23. )
  24. maxes.append(max(stddevs))
  25. bins = numpy.linspace(0, min(maxes), num_bins)
  26. return bins
  27. def makeIntensityHistograms(treatment_data, signals, thresholds, output_dir):
  28. """ Make plots that show the overlap between the 0 and
  29. 1-cell wells for each cytokine.
  30. """
  31. basedir = os.path.join(output_dir, "images")
  32. if not os.path.exists(basedir):
  33. os.makedirs(basedir)
  34. for signal in signals:
  35. bins = get_best_bins_for_signal(treatment_data, signal)
  36. for treatment, data in treatment_data.iteritems():
  37. c0_wells = (data.cell_count == 0)
  38. c1_wells = (data.cell_count == 1)
  39. logging.info("Creating 0/1 histogram for {0}/{1}".format(
  40. treatment, signal
  41. ))
  42. c0s = data[c0_wells][signal].copy()
  43. c1s = data[c1_wells][signal].copy()
  44. threshold = thresholds[signal].ix[(treatment, 'mid98')]
  45. c0s_on_percent = c0s[c0s > threshold].count() / float(c0s.count())
  46. c1s_on_percent = c1s[c1s > threshold].count() / float(c1s.count())
  47. # Apply a threshold, so that anything greater than
  48. # the greatest bin is just put into the greatest bin.
  49. c0s[c0s > bins[-1]] = bins[-1]
  50. c1s[c1s > bins[-1]] = bins[-1]
  51. pyplot.hist(c0s, bins, alpha=0.5, normed=True)
  52. pyplot.hist(c1s, bins, alpha=0.5, normed=True)
  53. if threshold < bins[-1]:
  54. pyplot.axvline(x=threshold)
  55. pyplot.text(0.90, 0.90, "0: {0:.2f} %on\n1: {1:.2f} %on".format(
  56. c0s_on_percent*100,
  57. c1s_on_percent*100
  58. ))
  59. pyplot.title("{0} {1}\n(0={2:.2f}% on; 1={3:.2f}% on)".format(
  60. treatment,
  61. signal,
  62. c0s_on_percent * 100,
  63. c1s_on_percent * 100
  64. ))
  65. pyplot.text(0.5, 0.5,'matplotlib',
  66. horizontalalignment='center',
  67. verticalalignment='center',
  68. )
  69. output_file = os.path.join(basedir, "{0}-{1}.png".format(treatment, signal))
  70. pyplot.savefig(output_file)
  71. pyplot.close()
  72. # import IPython; IPython.embed()