Functions
wirecell.validate.main Namespace Reference

Functions

def cli (ctx)
 
def diff_hists (ctx, name, out, file1, file2)
 
def magnify_diff (ctx, epsilon, out, file1, file2)
 
def magnify_jsonify (ctx, out, filename)
 
def magnify_dump (ctx, out, filename)
 
def npz_load (ctx, out, filename)
 
def magnify_plot_reduce (ctx, name, out, filename)
 
def magnify_plot (ctx, name, trebin, crebin, baseline, threshold, saturate, out, filename)
 
def main ()
 

Function Documentation

def wirecell.validate.main.cli (   ctx)
Wire Cell Validation

Definition at line 11 of file main.py.

11 def cli(ctx):
12  '''
13  Wire Cell Validation
14  '''
15  pass
16 
17 
18 @cli.command("diff-hists")
19 @click.option("-n", "--name", multiple=True,
20  help="Name of histogram in each file")
21 @click.option("-o", "--out", default=None,
22  help="Output file")
23 @click.argument("file1")
24 @click.argument("file2")
25 @click.pass_context
def cli(ctx)
Definition: main.py:11
def wirecell.validate.main.diff_hists (   ctx,
  name,
  out,
  file1,
  file2 
)
Produce an output ROOT file which  holds the difference of a histogram from two files.

Definition at line 26 of file main.py.

26 def diff_hists(ctx, name, out, file1, file2):
27  '''
28  Produce an output ROOT file which holds the difference of a histogram from two files.
29  '''
30 
31  import root
32  tfile1 = root.open_file(file1)
33  tfile2 = root.open_file(file2)
34  if out:
35  out = root.open_file(out, "recreate")
36 
37  tosave=list()
38  for one in name:
39  one = str(one) # unicode messes up ROOT
40  h1 = root.load_obj(tfile1, one)
41  h2 = root.load_obj(tfile2, one)
42  h1.Add(h2, -1.0)
43  if out:
44  h1.SetDirectory(out)
45  tosave.append(h1)
46  if not out:
47  return
48  out.Write()
49  out.Close()
50 
51 
52 @cli.command("magnify-diff")
53 @click.option("-e", "--epsilon", default=0.0,
54  help="The maximum delta for two histograms to be considered different")
55 @click.option("-o", "--out",
56  help="Output file")
57 
58 @click.argument("file1")
59 @click.argument("file2")
60 @click.pass_context
def diff_hists(ctx, name, out, file1, file2)
Definition: main.py:26
static QCString str
def wirecell.validate.main.magnify_diff (   ctx,
  epsilon,
  out,
  file1,
  file2 
)
Form a new Magnify file holds histograms which are the difference of those from the two inputs.

Definition at line 61 of file main.py.

61 def magnify_diff(ctx, epsilon, out, file1, file2):
62  '''
63  Form a new Magnify file holds histograms which are the difference of those from the two inputs.
64  '''
65  if not out:
66  sys.exit(1)
67 
68  from time import time
69  import root
70  tfile1 = root.open_file(file1)
71  tfile2 = root.open_file(file2)
72  out = root.open_file(out, "recreate")
73 
74  names1 = set([key.GetName() for key in tfile1.GetListOfKeys() if root.is_hist(key)])
75  names2 = set([key.GetName() for key in tfile2.GetListOfKeys() if root.is_hist(key)])
76 
77  loners = names1 ^ names2
78  for loner in loners:
79  print "loner: %s" % loner
80 
81  names = list(names1 & names2)
82  names.sort()
83  t1 = time()
84  hists1 = [tfile1.Get(name) for name in names]
85  hists2 = [tfile2.Get(name) for name in names]
86  t2 = time()
87  #print "load: ", t2-t1
88  for name,obj1,obj2 in zip(names, hists1, hists2):
89  obj1.Add(obj2, -1.0)
90  mi = obj1.GetMinimum()
91  ma = obj1.GetMaximum()
92  if abs(mi) > epsilon or abs(ma) > epsilon:
93  msg = "diff: %s %e %e" % (name, mi, ma)
94  print msg
95  obj1.SetDirectory(out)
96  t3 = time()
97  #print "subtract: ", t3-t2
98  out.Write()
99  out.Close()
100  t4 = time()
101  #print "done: ", t4-t3
102 
103 @cli.command("magnify-jsonify")
104 @click.option("-o", "--out",
105  help="Output file")
106 @click.argument("filename")
107 @click.pass_context
T abs(T value)
def magnify_diff(ctx, epsilon, out, file1, file2)
Definition: main.py:61
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
def wirecell.validate.main.magnify_dump (   ctx,
  out,
  filename 
)
Dump magnify histograms into Numpy .npz files.

The underlying histogram array is dumped into an array of the same
name.  For every dimension of histogram an array of bin edges is
dumped as <name>_<dim>edges where <dim> is "x", "y" or "z".

Definition at line 134 of file main.py.

134 def magnify_dump(ctx, out, filename):
135  '''Dump magnify histograms into Numpy .npz files.
136 
137  The underlying histogram array is dumped into an array of the same
138  name. For every dimension of histogram an array of bin edges is
139  dumped as <name>_<dim>edges where <dim> is "x", "y" or "z".
140  '''
141  import ROOT
142  import numpy
143  from root_numpy import hist2array
144  from time import time
145 
146  print "Reading ROOT file"
147  t1 = time()
148  arrs = dict()
149  tfile = ROOT.TFile.Open(filename)
150  for key in tfile.GetListOfKeys():
151  cname = key.GetClassName()
152  if cname[:3] not in ["TH1", "TH2", "TH3" ]:
153  continue
154 
155  th = key.ReadObj()
156  hname = th.GetName()
157  arr,edges = hist2array(th,return_edges=True)
158  arrs[hname] = arr
159  for dim,edge in enumerate(edges):
160  arrs["%s_%sedge"%(hname, "xyz"[dim])] = edge
161  t2 = time()
162  print t2-t1 # takes 5.7 seconds compared to 5.3 seconds loading npz
163  print "Writing NPZ file"
164  numpy.savez_compressed(out, **arrs)
165  t3 = time()
166  print t3-t2
167 
168 @cli.command("npz-load")
169 @click.option("-o", "--out",
170  help="Output file")
171 @click.argument("filename")
172 @click.pass_context
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def magnify_dump(ctx, out, filename)
Definition: main.py:134
def wirecell.validate.main.magnify_jsonify (   ctx,
  out,
  filename 
)
Jsonnify summary info about all histograms.

Definition at line 108 of file main.py.

108 def magnify_jsonify(ctx, out, filename):
109  '''
110  Jsonnify summary info about all histograms.
111  '''
112  import json
113  import root
114  tfile = root.open_file(filename)
115  names = list()
116 
117  dat = dict();
118  for key in tfile.GetListOfKeys():
119  if key.IsFolder():
120  continue
121  if not root.is_hist(key):
122  continue
123  d = root.hist_to_dict(key.ReadObj());
124  name = key.GetName()
125  dat[name] = d
126  #print name, d
127  open(out,"w").write(json.dumps(dat, indent=4))
128 
129 @cli.command("magnify-dump")
130 @click.option("-o", "--out",
131  help="Output file")
132 @click.argument("filename")
133 @click.pass_context
int open(const char *, int)
Opens a file descriptor.
size_t write(int, const char *, size_t)
Writes count bytes from buf to the filedescriptor fd.
def magnify_jsonify(ctx, out, filename)
Definition: main.py:108
def wirecell.validate.main.magnify_plot (   ctx,
  name,
  trebin,
  crebin,
  baseline,
  threshold,
  saturate,
  out,
  filename 
)
Plot magnify histograms.

Definition at line 255 of file main.py.

255 def magnify_plot(ctx, name, trebin, crebin, baseline, threshold, saturate, out, filename):
256  '''
257  Plot magnify histograms.
258  '''
259  import numpy
260  from root_numpy import hist2array as h2a
261  from wirecell.validate.arrays import rebin, bin_ndarray
262  from wirecell.validate.plots import three_horiz
263  import ROOT
264 
265  name = str(name)
266 
267  tfile = ROOT.TFile.Open(filename)
268  hists = [tfile.Get("h%s_%s"%(letter, name)) for letter in "uvw"]
269  if not all(hists):
270  raise ValueError('Could not get hists for "%s" from: %s' % (name, filename))
271 
272  # loading from ROOT takes the most time.
273 
274  aes = [h2a(h,return_edges=True) for h in hists]
275  arrs = list()
276  extents = list()
277  for h,(a,e) in zip(hists,aes):
278  xa,ya = [getattr(h,"Get"+l+"axis")() for l in "XY"]
279  nx,ny = [getattr(h,"GetNbins"+l)() for l in "XY"]
280 
281  # h2a returns tick-major shape (nchan,ntick).
282  ce,te = e
283  ext = ((ce[0],ce[-1]), (te[0],te[-1]))
284 
285  print "%s: X:%d in [%.0f %.0f] Y:%d in [%.0f %.0f]" % \
286  (h.GetName(),
287  nx, xa.GetBinLowEdge(1), xa.GetBinUpEdge(nx),
288  ny, ya.GetBinLowEdge(1), ya.GetBinUpEdge(ny),)
289  print "\t shape=%s ext=%s" % (a.shape, ext)
290 
291  arrs.append(numpy.fliplr(a))
292  extents.append(ext)
293 
294 
295  norm = 1.0/(crebin*trebin)
296 
297 
298  baselines = list()
299  if baseline:
300  newarrs = list()
301  for arr in arrs:
302  newarr = list()
303  bl = list() # across channels in one plane
304  for wav in arr:
305  digi = numpy.int32(wav)
306  #histo = numpy.bincount(digi)
307  #val = numpy.argmax(histo)
308  values, counts = numpy.unique(digi, return_counts=True)
309  ind = numpy.argmax(counts)
310  val = values[ind]
311  bl.append(val)
312  wav = wav - val
313  newarr.append(wav)
314  newarr = numpy.asarray(newarr)
315  bla = numpy.asarray(bl)
316  bla = rebin(bla, bla.size/crebin)
317  baselines.append(bla)
318  newarrs.append(newarr)
319  arrs = newarrs
320  #arrs = [norm*rebin(arr, arr.shape[0]/crebin, arr.shape[1]/trebin) for arr in arrs]
321  arrs = [bin_ndarray(arr, (arr.shape[0]/crebin, arr.shape[1]/trebin), "mean") for arr in arrs]
322 
323  tit = 'Type "%s" (rebin: ch=x%d tick=x%d), file:%s' % (name, crebin, trebin, os.path.basename(filename))
324 
325  if threshold > 0.0:
326  #print "thresholding at", threshold
327  tit += " [threshold at %d]" % threshold
328  arrs = [numpy.ma.masked_where(numpy.abs(arr)<=threshold, arr) for arr in arrs]
329 
330  if saturate > 0.0:
331  #print "saturating at", saturate
332  #tit += "saturate=%d" % saturate
333  newarrs = list()
334  for arr in arrs:
335  arr[arr > saturate] = saturate
336  arr[arr <-saturate] = -saturate
337  newarrs.append(arr)
338  arrs = newarrs
339 
340  # switch from tick-major (nchans, nticks) to chan-major (nticks, nchans)
341  arrs = [a.T for a in arrs]
342  extents = [(e[0][0], e[0][1], e[1][0], e[1][1]) for e in extents]
343 
344  for a,e in zip(arrs,extents):
345  print a.shape, e
346 
347 
348  fig = three_horiz(arrs, extents, name, baselines)
349  fig.suptitle(tit)
350  fig.savefig(out)
351 
352 
def magnify_plot(ctx, name, trebin, crebin, baseline, threshold, saturate, out, filename)
Definition: main.py:255
static QInternalList< QTextCodec > * all
Definition: qtextcodec.cpp:63
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
def bin_ndarray(ndarray, new_shape, operation='sum')
Definition: arrays.py:20
def rebin(a, args)
Definition: arrays.py:2
static QCString str
def wirecell.validate.main.magnify_plot_reduce (   ctx,
  name,
  out,
  filename 
)
Reduce a magnify 2D histogram along the time dim.

Definition at line 202 of file main.py.

202 def magnify_plot_reduce(ctx, name, out, filename):
203  '''
204  Reduce a magnify 2D histogram along the time dim.
205  '''
206  import ROOT
207  import numpy
208  from root_numpy import hist2array
209  from wirecell.validate.plots import channel_summaries as plotter
210 
211  methods = [
212  ("min", numpy.min),
213  ("max", numpy.max),
214  ("sum", numpy.sum),
215  ("rms", lambda a: numpy.sqrt(numpy.mean(numpy.square(a)))),
216  ("absmin", lambda a: numpy.min(numpy.abs(a))),
217  ("absmax", lambda a: numpy.max(numpy.abs(a))),
218  ("abssum", lambda a: numpy.sum(numpy.abs(a))),
219  ]
220  name = str(name)
221  tfile = ROOT.TFile.Open(filename)
222  hists = [tfile.Get("h%s_%s"%(letter, name)) for letter in "uvw"]
223  aes = [hist2array(hist, return_edges=True) for hist in hists]
224 
225  edges = [ae[1] for ae in aes]
226  extents = [(e[0][0],e[0][-1]) for e in edges]
227  #print extents
228 
229  arrs_by_name = dict()
230  for mname, meth in methods:
231  arrs_by_name[mname] = [numpy.apply_along_axis(meth, 1, ae[0]) for ae in aes]
232 
233  fig = plotter(name, arrs_by_name, extents)
234  #fig.suptitle('Channel Summaries for "%s"' % name)
235  fig.savefig(out)
236 
237 
238 @cli.command("magnify-plot")
239 @click.option("-n", "--name", default="orig",
240  help="The histogram type name")
241 @click.option("-t", "--trebin", type=int, default=1,
242  help="Set amount of rebinning in time domain (must be integral factor)")
243 @click.option("-c", "--crebin", type=int, default=1,
244  help="Set amount of rebinning in channel comain (must be integral factor)")
245 @click.option("--baseline/--no-baseline", default=False,
246  help="Calculate, subtract and display baselines.")
247 @click.option("--threshold", type=float, default=0.0,
248  help="Apply a threshold.")
249 @click.option("--saturate", type=float, default=0.0,
250  help="Saturate values.")
251 @click.option("-o", "--out",
252  help="Output file")
253 @click.argument("filename")
254 @click.pass_context
def magnify_plot_reduce(ctx, name, out, filename)
Definition: main.py:202
static QCString str
def wirecell.validate.main.main ( void  )

Definition at line 353 of file main.py.

353 def main():
354  cli(obj=dict())
355 
def cli(ctx)
Definition: main.py:11
def wirecell.validate.main.npz_load (   ctx,
  out,
  filename 
)

Definition at line 173 of file main.py.

173 def npz_load(ctx, out, filename):
174  import numpy
175  from time import time
176  t1 = time()
177  arrs1 = numpy.load(filename,'r+')
178  t2 = time()
179  print 'Memmap load: %f'%(t2-t1,)
180  arrs2 = numpy.load(filename,None)
181  t3 = time()
182  print 'Full load: %f'%(t3-t2)
183  outs=dict()
184  for name in arrs1:
185  arr1 = arrs1[name]
186  arr2 = arrs2[name]
187  arr = arr2-arr1
188  outs[name] = arr
189  t4 = time()
190  print 'Subtract: %f' % (t4-t3)
191  numpy.savez_compressed(out, **outs)
192  t5 = time()
193  print 'Done: %f' % (t5-t4)
194 
195 @cli.command("magnify-plot-reduce")
196 @click.option("-n", "--name", default="orig",
197  help="The histogram type name")
198 @click.option("-o", "--out",
199  help="Output file")
200 @click.argument("filename")
201 @click.pass_context
def npz_load(ctx, out, filename)
Definition: main.py:173