📄 histo.py
字号:
write('#' + comment) write('\n') write('# Lower bound ' + fs % (self.lower)) write('# Upper bound ' + fs % (self.upper)) write('# Bucket width ' + fs % (self.bucketsz)) write('# No. buckets ' + '%d' % (self.nbuckets)) write('# No. samples ' + '%d' % (self.N)) write('# Total samples ' + '%d' % (self.nsamples)) if self.N: write('# Min. value ' + fs % (self.min)) write('# Max. value ' + fs % (self.max)) write('# Min. sample value ' + fs % (self.samplemin)) write('# Max. sample value ' + fs % (self.samplemax)) if self.N: write('# Mean %f' % mean) write('# S.D. %f' % sd) write('# Sample mean %f' % samplemean) write('# Sample S.D. %f' % samplesd) if nlow: write('# %d samples below lower bound' % (nlow)) if nhigh: write('# %d samples above upper bound' % (nhigh)) if self.nlre: write('# %d low range exceptions raised' % (self.nlre)) if self.nhre: write('# %d high range exceptions raised' % (self.nhre)) write('') write('# First column is highest inclusive bucket value, second is bucket score') write('') h = self.ha l = self.lower bw = self.bucketsz for i in range(self.nbuckets): count = h[i] top = l + ((i+1)*bw) if count or zeros: write(fsw % (top) + '%10d' % (count)) hist_vals.append([top, count]) write('') mlist = [(count, top) for top, count in hist_vals] if mlist: mlist.sort() mlist.reverse() modes = [mlist[0]] for count, top in mlist[1:]: if count < modes[0][0]: break else: modes.append((count, top)) if len(modes) == 1: write('# Mode %s (%s)' % (bktrange(modes[0][1]), modes[0][0])) else: modes.reverse() ms = '%s (%s)' % (bktrange(modes[0][1]), modes[0][0]) for count, top in modes[1:]: ms += ', %s (%s)' % (bktrange(top), count) write('# Modes %s' % (ms)) else: modes = [] if draw: try: from np_data import DataSet, DATA_HIST from np_plot import np_Plot, STYLE_BARS, MODE_HIST, MODE_RANGES except ImportError, s: print 'ImportError \"%s\" - add a directory containing np_data.py and np_plot.py to your PYTHONPATH (try $NPROBE_REPOSITORY_ROOT/analysis/plotter) or cd to $NPROBE_REPOSITORY_ROOT and \'make paths_raw\'' % (str(s)) sys.exit(1) bstring = '' sepstr = '' if nlow: bstring += '%d samples below range' % nlow sepstr = ' ' if nhigh: bstring += '%s%d samples above range' % (sepstr, nhigh) if bstring: bstring = '\n(' + bstring + ' - not included)' draw_title = title + bstring + '\n\n' + comment ds = DataSet(hist_vals, DATA_HIST, draw_title, 0, style=STYLE_BARS) np_Plot([ds], '', style=STYLE_BARS, mode=MODE_HIST|MODE_RANGES, title=title) return {'title':title, 'comment':comment, 'lower':self.lower, 'upper':self.upper, 'bucket size':self.bucketsz, 'nbuckets':self.nbuckets, 'nsamples':self.nsamples, 'min':self.min, 'max':self.max, 'samplemin':self.samplemin, 'samplemax':self.samplemax, 'nlow':nlow, 'lows':self.lows, 'nhigh':nhigh, 'highs':self.highs, 'low_range_exceptions':self.nlre, 'high_range_exceptions':self.nhre, 'values':hist_vals, 'mean':mean, 'sample_mean':samplemean, 'sd':sd, 'sample_sd':samplesd, 'modes': modes} ############################################################################################################################################################# def main(): """Module histo.py - provides class Histogram with obvious uses. When run as a script generates histograms from the specified data file(s). Usage: histo.py [flags] [file list] Flags: The following define operational modes - -l<val> Specify lower bound val -u<val> Specify upper bound val -w<val> Specify bucket width val -n<N> Specify N buckets (default 100) -t<N> Specify auto-ranging based upon first N samples The following determine how out of bounds samples are handled - default Just count No. of out of range samples -o Save out of range samples -r Throw HistogramLowRangeError and HistogramHighRangeError exceptions -e Catch range error exceptions and extend the histogram accordingly The following determine presentation of results - -P Print histogram data at completion -D Fire up plotter showing histogram -F<file>Save histogram data to <file> -L Print a dictionary of all histogram data Sundry flags - -d Use hard-coded data set for testing -f<n> Take samples from field n of input data file (default 1) -v Verbose mode -h This help -H Help on the Histogram class itself Auto-ranging: If lower and upper histogram bounds are specified all samples are immediately added in, and out of range samples dealt with as determined by the -o, -r, and -e flags. If neither, or only one bound, are specified the sample values are stored until a trigger number of samples - specified using the -t flag - have been presented, at which point an upper/lower bound, or both, are automatically calculated based upon the current max/min stored sample values, and the stored values are added in. Bucket widths or nbuckets are calculated based upon the specified nbuckets or width - if neither are specified the default nbuckets = 100 is used. The default trigger value of 0 results in all samples being stored and auto range calculations being performed on the entire sample set at completion. """ def iof(s): if s.find('.') >= 0: return float(s) else: return int(s) import os from sys import argv import getopt try: from np_data import DataReader except ImportError, s: print 'ImportError \"%s\" - add a directory containing np_data.py to your PYTHONPATH (try $NPROBE_REPOSITORY_ROOT/analysis/plotter) or cd to $NPROBE_REPOSITORY_ROOT and \'make paths_raw\'' % (str(s)) sys.exit(1) data_def = [0, 1,2,2,3,4,5,6,7,8,9, 10, 9.5, 6.000001, 11] #data_def = [0,0, 1,2,3,4,4,5,6,7,8,9,10] scriptname = os.path.basename(argv[0]) try: optlist, args = getopt.getopt(sys.argv[1:], 'l:u:n:w:t:oPF:LDdervf:hH') except getopt.error, s: print '%s: %s' % (scriptname, s) sys.exit(1) lower = upper = nbuckets = bsz = ntrig = orange = None printit = list = draw = throw_e = extend = verbose = 0 get_data = field = 1 ofile = '' stuff = None for opt in optlist: if opt[0] == '-l': lower = iof(opt[1]) if opt[0] == '-u': upper = iof(opt[1]) if opt[0] == '-n': nbuckets = iof(opt[1]) if opt[0] == '-w': bsz = iof(opt[1]) if opt[0] == '-t': ntrig = iof(opt[1]) if opt[0] == '-o': orange = 1 if opt[0] == '-P': printit = 1 if opt[0] == '-L': list = 1 if opt[0] == '-F': ofile = opt[1] if opt[0] == '-D': draw = 1 if opt[0] == '-d': get_data = 0 if opt[0] == '-r': throw_e = 1 if opt[0] == '-e': extend = 1 if opt[0] == '-v': verbose = 1 if opt[0] == '-f': field = int(opt[1]) if opt[0] == '-H': h = Histogram() print print h.__doc__ sys.exit(0) if opt[0] == '-h': print print main.__doc__ sys.exit(0) histos = [] if get_data or args: dr = DataReader(args) stuff = dr.get_data() if not stuff: title = 'Test set' data = [data_def] else: title=stuff[1] data = [] for d in stuff[0]: data.append(d.data) #print data for d in data: try: h = Histogram(lower=lower, upper = upper, nbuckets= nbuckets, bucketsz=bsz, ntrigvals=ntrig, save_orange=orange, rangeexcept=throw_e, verbose=verbose) t = type(d[0]) if t is IntType or t is FloatType or t is ListType or t is TupleType: if t is ListType or t is TupleType: try: d = [t[field] for t in d] except IndexError: print 'Specified field %d not found in data set - quitting' % (field) sys.exit(1) for v in d: try: h.add(v) except HistogramHighRangeError, s: if verbose: print s if extend: h.extend(v) else: print 'Value', v, 'above bound %s - quitting' % (str(h.upper)) sys.exit(1) except HistogramLowRangeError, s: if verbose: print s if extend: h.extend(v) else: print 'Value', v, 'below or equals bound %s - quitting' % (str(h.lower)) sys.exit(1) else: print scriptname, 'ERROR: data points appear to be', t sys.exit(1) histos.append(h) except HistogramError, s: print s return for h in histos: d = h.results(printit=printit, file=ofile, draw=draw, title=title, comment='') if list: d = d.items() d.sort() for i in d: if type(i[1]) is ListType and i[1]: print '%-10s' % (i[0]) for v in i[1]: print '\t', v elif type(i[1]) is StringType: print '%-10s \'%s\'' % (i[0], i[1]) else: print '%-10s' % (i[0]), i[1] ############################################################################################################################################################## Call main when run as scriptif __name__ == '__main__': main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -