plots.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 '''
3 Make some validation plots
4 '''
5 import numpy
6 import matplotlib.pyplot as plt
7 from matplotlib.colors import LinearSegmentedColormap
8 
9 
10 def make_cmaps():
11 
12  soft0 = [
13  (0.0, 1.0, 1.0),
14  (0.0, 0.0, 1.0),
15  (1.0, 0.0, 0.0),
16  (1.0, 1.0, 0.0),
17  ]
18 
19  # (relative point, low side color component, high side color component)
20 
21  hard0 = dict(
22  red = (
23  (0.0, 0.0, 0.0),
24  (0.5, 0.0, 1.0),
25  (1.0, 1.0, 1.0)
26  ),
27  green = (
28  (0.0, 0.0, 1.0),
29  (0.5, 0.0, 0.0),
30  (1.0, 1.0, 1.0)
31  ),
32  blue= (
33  (0.0, 0.0, 0.0),
34  (0.5, 1.0, 0.0),
35  (1.0, 0.0, 0.0)
36  )
37  )
38 
39  cm = LinearSegmentedColormap.from_list('wct_soft0', soft0, N=100)
40  cm.set_bad(color='white')
41  plt.register_cmap(cmap=cm)
42 
43  cm = LinearSegmentedColormap('wct_hard0', hard0, N=100)
44  cm.set_bad(color='white')
45  plt.register_cmap(cmap=cm)
46 make_cmaps()
47 
48 
49 def three_horiz(arrs, extents, name, baselines):
50  '''
51  Plot three arrays horizontally.
52  '''
53  #cmap = 'nipy_spectral'
54  #cmap = 'RdBu_r'
55 
56  cmap = "wct_soft0"
57  if name in "orig".split():
58  cmap = "wct_hard0"
59 
60  zlabel = dict(orig="ADC", raw="nfADC", gauss="electrons", wiener="electrons")[name]
61 
62  dpi = 100.0 # nothing to do with actual DPI
63 
64  hpix = sum([a.shape[1] for a in arrs])
65  vpix = a.shape[0]
66 
67  tpix = 60 # title pix
68  bpix = 60 # border pix
69  cpix = 100
70  cibpix = 75 # room for text for color bar
71 
72  blpix = 0
73  if baselines: # vertical room for baseline plots
74  blpix = 100
75 
76  width_pix = hpix + len(arrs)*bpix*2 + cpix
77  width_inch = width_pix/dpi
78  height_pix = vpix + 2*bpix + blpix + tpix
79  height_inch = height_pix/dpi
80 
81  bot_pix = blpix + bpix # bottom of main event plots
82 
83  figsize=(width_inch, height_inch)
84  #print "figsize=",figsize
85  fig = plt.figure(dpi=dpi, figsize = figsize)
86 
87  ims = list()
88  axes = list()
89 
90  left_pix = 0
91  vmin = min([numpy.min(arr) for arr in arrs])
92  vmax = max([numpy.max(arr) for arr in arrs])
93  #print "vmm=",vmin,vmax
94  vmm = max([abs(vmin), abs(vmax)])
95  vmin = -vmax
96  vmax = vmax
97 
98  x_axes = list()
99 
100  for letter, arr, ext in zip("UVW", arrs, extents):
101  tit = "%d ticks x %d ch grps" % (arr.shape[0], arr.shape[1])
102 
103  xa = (left_pix+bpix)/float(width_pix) # left
104  ya = bot_pix/float(height_pix) # bottom
105  dxa = arr.shape[1]/float(width_pix) # width
106  dya = arr.shape[0]/float(height_pix) # height
107  relaxis = (xa,ya,dxa,dya)
108  x_axes.append((xa,dxa))
109 
110  left_pix += 2*bpix + arr.shape[1] # move for next time
111 
112  #print "axis=",relaxis
113  ax = fig.add_axes(relaxis)
114  axes.append(ax)
115  ax.set_title(tit)
116  ax.set_xlabel("%s channels" % letter)
117  ax.set_ylabel("ticks")
118 
119  im = ax.imshow(arr, cmap=cmap, interpolation='none', extent=ext, aspect='auto', vmin=vmin, vmax=vmax)
120  ims.append(im)
121 
122 
123  relaxis = [(left_pix)/float(width_pix),
124  bot_pix/float(height_pix),
125  (cpix-cibpix)/float(width_pix),
126  (arrs[0].shape[0])/float(height_pix)]
127  cbar_ax = fig.add_axes(relaxis)
128  fig.colorbar(ims[0], ax=axes[0], cmap=cmap, cax=cbar_ax, label=zlabel)
129 
130 
131  if not baselines:
132  return fig
133 
134  blaxes = list()
135  left_pix = 0
136  for letter, blarr, ext, (xa,dxa) in zip("UVW", baselines, extents, x_axes):
137 
138  relaxis = (xa,
139  bpix/float(height_pix), # bottom
140  dxa,
141  blpix/float(height_pix)) # height
142 
143  left_pix += 2*bpix + blarr.size/float(width_pix) # move for next time
144 
145  ax = fig.add_axes(relaxis)
146  blaxes.append(ax)
147  #ax.set_title(tit)
148  ax.set_xlabel("%s channels" % letter)
149  ax.set_ylabel("baseline")
150 
151  #print ext[:2], blarr.size
152  ax.plot(numpy.linspace(ext[0], ext[1], num=blarr.size), blarr)
153  ax.set_xlim([ext[0],ext[1]]) # really do what I say
154 
155  return fig
156 
157 
158 
159 def one_plane(arr, extent, name):
160  cmap = 'nipy_spectral'
161 
162  dpi = 100 # nothing to do with actual DPI
163  size = (arr.shape[0]/dpi, arr.shape[1]/dpi)
164 
165  fig = plt.figure(dpi=dpi, figsize=size)
166  ax = fig.add_subplot(1,1,1)
167  ax.set_title(name)
168  im = ax.imshow(arr, cmap=cmap, interpolation='none', extent=extent, aspect='auto')
169  fig.colorbar(im, cmap=cmap)
170 
171  return fig
172 
173 
174 def channel_summaries(name, arrs, extents):
175  import matplotlib
176  matplotlib.rcParams.update({'font.size': 8})
177 
178  mnames = arrs.keys()
179  mnames.sort()
180 
181  fig, axes = plt.subplots(len(mnames), len(arrs[mnames[0]]), figsize=(8.5,11), dpi=100)
182  for irow, mname in enumerate(mnames):
183  for icol, (arr,letter) in enumerate(zip(arrs[mname],"UVW")):
184  ext = extents[icol]
185  ls = numpy.linspace(ext[0], ext[1], num=arr.size)
186  ax = axes[irow,icol]
187  ax.plot(ls, arr)
188  ax.set_xlim([ext[0],ext[1]]) # really do what I say
189  ax.set_title("%s-plane %s '%s'" % (letter, mname, name));
190  #ax.set_xlabel("%s chans" % (letter, ))
191  fig.tight_layout()
192 
193  return fig
194 
195 
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
T abs(T value)
static int max(int a, int b)
def channel_summaries(name, arrs, extents)
Definition: plots.py:174
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
def three_horiz(arrs, extents, name, baselines)
Definition: plots.py:49
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
void split(std::string const &s, char c, OutIter dest)
Definition: split.h:35
def one_plane(arr, extent, name)
Definition: plots.py:159