IMP  2.3.1
The Integrative Modeling Platform
_histogram.py
1 def _show_histogram_1d(h, yscale, xscale, curves):
2  import numpy as np
3  import matplotlib.pyplot as plt
4  import matplotlib.mlab as mlab
5  fig = plt.figure()
6  ax = fig.add_subplot(111)
7  data = []
8  countsgrid = h.get_counts()
9  bins = [countsgrid.get_bounding_box(i).get_corner(0)[0]
10  for i in countsgrid.get_all_indexes()]
11  counts = h.get_counts().get_all_voxels()
12  gbb = h.get_bounding_box()
13  if yscale == 'linear':
14  ax.bar(bins, counts, align='edge', width=bins[1] - bins[0])
15  else:
16  ax.plot(bins, counts, "bx-", linewidth=2)
17  ax.set_xlim(gbb.get_corner(0)[0],
18  gbb.get_corner(1)[0])
19  ax.set_xscale(xscale)
20  ax.set_yscale(yscale)
21  # only scale based on histogram
22  ax.set_autoscaley_on(False)
23  for c in curves:
24  ax.plot(bins, [c(x) for x in bins], "go-", linewidth=1)
25  plt.show()
26 
27 
28 def _show_histogram_2d(h, yscale, xscale):
29  import numpy as np
30  import matplotlib.cm as cm
31  import matplotlib.mlab as mlab
32  import matplotlib.pyplot as plt
33  cg = h.get_counts()
34  steps = cg.get_unit_cell()
35  x = np.arange(cg.get_bounding_box().get_corner(0)[0] + .5 * steps[0],
36  cg.get_bounding_box().get_corner(1)[0] - .5 * steps[0])
37  y = np.arange(cg.get_bounding_box().get_corner(0)[1] + .5 * steps[1],
38  cg.get_bounding_box().get_corner(1)[1] - .5 * steps[1])
39  X, Y = np.meshgrid(x, y)
40  Z, junk = np.meshgrid(x, y)
41  for i, xi in enumerate(x):
42  for j, yj in enumerate(y):
43  Z[i][j] = cg[cg.get_nearest_index(IMP.algebra.Vector2D(xi, yj))]
44  im = plt.pcolor(X, Y, Z, cmap=cm.jet)
45  plt.colorbar(im)
46  plt.show()
47 
48 
49 def _get_min_dim(bb):
50  md = 100000
51  for i in range(0, bb.get_dimension()):
52  l = bb.get_corner(1)[i] - bb.get_corner(0)[i]
53  if l < md:
54  md = l
55  return md
56 
57 
58 def _show_histogram_3d(h, vmin, vmax):
59  import IMP.display
60  cg = h.get_counts()
61  if not vmin or not vmax:
62  minmax = h.get_minimum_and_maximum()
63  if not vmin:
64  vmin = minmax[0]
65  if not vmax:
66  vmax = minmax[1]
67  print vmin, vmax
69  import sys
70  print >> sys.stderr, "No pivy found"
71  return
73  for idx in cg.get_all_indexes():
74  v = cg[idx]
75  # print v, vmin
76  if v < vmin:
77  continue
78  coords = cg.get_center(idx)
79  r = .25 * _get_min_dim(cg.get_bounding_box(idx))
80  s = IMP.algebra.Sphere3D(coords, r)
81  g = IMP.display.SphereGeometry(s, "histogram")
82  scaled = (v - vmin) / (vmax - vmin)
83  if scaled > 1.0:
84  scaled = 1.0
85  color = IMP.display.get_hot_color(scaled)
86  g.set_color(color)
87  w.add_geometry(g)
88  w.show()
89 
90 
91 def show_histogram(h, yscale='linear', xscale='linear',
92  curves=[], vmin=None, vmax=None):
93  if h.get_dimension() == 1:
94  _show_histogram_1d(h, yscale, xscale, curves)
95  elif h.get_dimension() == 2:
96  _show_histogram_2d(h, yscale, xscale)
97  elif h.get_dimension() == 3:
98  _show_histogram_3d(h, vmin, vmax)
99  else:
100  raise ValueError(
101  "Dimension " + str(h.get_dimension()) + "not supported")
VectorD< 2 > Vector2D
Definition: VectorD.h:391
Color get_hot_color(double f)
Return the color for f from the hot color map.
void show_histogram(HistogramD h, std::string xscale="linear", std::string yscale="linear", Functions curves=Functions())
Output IMP model data in various file formats.