From 0d93a47c71be883c27afbeb518fd84f2a2d24d00 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Sun, 27 Mar 2016 13:32:08 -0400 Subject: [PATCH] Generate histogram of N bins in given data range. --- processing/post/histogram.py | 128 +++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100755 processing/post/histogram.py diff --git a/processing/post/histogram.py b/processing/post/histogram.py new file mode 100755 index 000000000..102170c86 --- /dev/null +++ b/processing/post/histogram.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 no BOM -*- + +import os,sys +import numpy as np +from optparse import OptionParser +import damask + +scriptName = os.path.splitext(os.path.basename(__file__))[0] +scriptID = ' '.join([scriptName,damask.version]) + +# -------------------------------------------------------------------- +# MAIN +# -------------------------------------------------------------------- + +parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """ +Generate histogram of N bins in given data range. + +""", version = scriptID) + +parser.add_option('-d','--data', + dest = 'data', + type = 'string', metavar = 'string', + help = 'column heading for data') +parser.add_option('--range', + dest = 'range', + type = 'float', nargs = 2, metavar = 'float float', + help = 'data range of histogram [min - max]') +parser.add_option('-N', + dest = 'N', + type = 'int', metavar = 'int', + help = 'number of bins') +parser.add_option('-l', '--logarithmic', + dest = 'log', + action = 'store_true', + help = 'logarithmically spaced bins') +parser.set_defaults(data = None, + range = None, + N = None, + log = False, + ) + +(options,filenames) = parser.parse_args() + +if not options.data: parser.error('no data specified.') +if not options.N: parser.error('no bin number specified.') + +if options.log: + def forward(x): + return np.log(x) + def reverse(x): + return np.exp(x) +else: + def forward(x): + return x + def reverse(x): + return x + + +# --- loop over input files ------------------------------------------------------------------------ + +if filenames == []: filenames = [None] + +for name in filenames: + try: table = damask.ASCIItable(name = name, + buffered = False, + readonly = True) + except: continue + damask.util.report(scriptName,name) + +# ------------------------------------------ read header ------------------------------------------ + + table.head_read() + +# ------------------------------------------ sanity checks ---------------------------------------- + + errors = [] + remarks = [] + + if table.label_dimension(options.data) != 1: errors.append('data {} is not a scalar.'.format(options.data)) + + if remarks != []: damask.util.croak(remarks) + if errors != []: + damask.util.croak(errors) + table.close(dismiss = True) + continue + +# --------------- read data ---------------------------------------------------------------- + + table.data_readArray(options.data) + bincenter = np.zeros(options.N,'f') + +# --------------- auto range --------------------------------------------------------------- + + if options.range is None: + rangeMin,rangeMax = min(table.data[:,0]),max(table.data[:,0]) + else: + rangeMin,rangeMax = min(options.range),max(options.range) + +# --------------- bin data ---------------------------------------------------------------- + + count,edges = np.histogram(table.data[:,0], + bins = reverse(forward(rangeMin) + np.arange(options.N+1) * + (forward(rangeMax)-forward(rangeMin))/options.N), + range = (rangeMin,rangeMax)) + bincenter = reverse(forward(rangeMin) + (0.5+np.arange(options.N)) * + (forward(rangeMax)-forward(rangeMin))/options.N) # determine center of bins + +# ------------------------------------------ assemble header --------------------------------------- + + table.info_clear() + table.info_append([scriptID + '\t' + ' '.join(sys.argv[1:]), + scriptID + ':\t' + + 'data range {} -- {}'.format(rangeMin,rangeMax) + + (' (log)' if options.log else ''), + ]) + table.labels_clear() + table.labels_append(['bincenter','count']) + table.head_write() + +# ------------------------------------------ output result ----------------------------------------- + + table.data = np.squeeze(np.dstack((bincenter,count))) + table.data_writeArray() + +# ------------------------------------------ output finalization ----------------------------------- + + table.close() # close ASCII tables