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