2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,re,sys
|
2016-03-02 00:07:31 +05:30
|
|
|
import math # noqa
|
2014-06-17 12:40:10 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-12-09 21:24:47 +05:30
|
|
|
|
2011-12-02 20:45:36 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-06-17 12:40:10 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2016-05-17 05:36:13 +05:30
|
|
|
Add or alter column(s) with derived values according to user-defined arithmetic operation between column(s).
|
2015-08-08 00:33:26 +05:30
|
|
|
Column labels are tagged by '#label#' in formulas. Use ';' for ',' in functions.
|
2014-08-06 18:57:09 +05:30
|
|
|
Numpy is available as np.
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
Special variables: #_row_# -- row index
|
|
|
|
Examples: (1) magnitude of vector -- "np.linalg.norm(#vec#)" (2) rounded root of row number -- "round(math.sqrt(#_row_#);3)"
|
2014-08-06 18:57:09 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'labels',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = '(list of) new column labels')
|
|
|
|
parser.add_option('-f','--formula',
|
|
|
|
dest = 'formulas',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = '(list of) formulas corresponding to labels')
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-05-17 05:36:13 +05:30
|
|
|
parser.add_option('-c','--condition',
|
|
|
|
dest = 'condition', metavar='string',
|
|
|
|
help = 'condition to filter rows')
|
|
|
|
|
|
|
|
parser.set_defaults(condition = None,
|
|
|
|
)
|
|
|
|
|
2011-12-02 20:45:36 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2016-03-02 01:14:43 +05:30
|
|
|
if options.labels is None or options.formulas is None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no formulas and/or labels specified.')
|
|
|
|
if len(options.labels) != len(options.formulas):
|
|
|
|
parser.error('number of labels ({}) and formulas ({}) do not match.'.format(len(options.labels),len(options.formulas)))
|
2015-05-09 18:15:30 +05:30
|
|
|
|
2012-08-22 23:17:34 +05:30
|
|
|
for i in xrange(len(options.formulas)):
|
2015-08-08 00:33:26 +05:30
|
|
|
options.formulas[i] = options.formulas[i].replace(';',',')
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-13 02:29:10 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-13 02:29:10 +05:30
|
|
|
try:
|
2016-05-17 05:36:13 +05:30
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
|
|
|
output = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
2015-08-13 02:29:10 +05:30
|
|
|
except:
|
|
|
|
continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header -------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
2016-05-17 05:36:13 +05:30
|
|
|
# -----------------------------------------------------------------------------------------------------
|
2012-02-16 17:26:16 +05:30
|
|
|
specials = { \
|
|
|
|
'_row_': 0,
|
|
|
|
}
|
|
|
|
|
2016-05-17 05:36:13 +05:30
|
|
|
# ------------------------------------------ Evaluate condition ---------------------------------------
|
2016-06-29 14:32:49 +05:30
|
|
|
if options.condition is not None:
|
2016-05-17 05:36:13 +05:30
|
|
|
interpolator = []
|
2016-06-29 14:32:49 +05:30
|
|
|
condition = options.condition # copy per file, since might be altered inline
|
2016-05-17 05:36:13 +05:30
|
|
|
breaker = False
|
|
|
|
|
2016-06-29 14:32:49 +05:30
|
|
|
for position,operand in enumerate(set(re.findall(r'#(([s]#)?(.+?))#',condition))): # find three groups
|
2016-05-17 05:36:13 +05:30
|
|
|
condition = condition.replace('#'+operand[0]+'#',
|
|
|
|
{ '': '{%i}'%position,
|
|
|
|
's#':'"{%i}"'%position}[operand[1]])
|
2016-06-29 14:32:49 +05:30
|
|
|
if operand[2] in specials: # special label
|
2016-05-17 05:36:13 +05:30
|
|
|
interpolator += ['specials["%s"]'%operand[2]]
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
interpolator += ['%s(table.data[%i])'%({ '':'float',
|
|
|
|
's#':'str'}[operand[1]],
|
2016-06-29 14:32:49 +05:30
|
|
|
table.label_index(operand[2]))] # could be generalized to indexrange as array lookup
|
2016-05-17 05:36:13 +05:30
|
|
|
except:
|
|
|
|
damask.util.croak('column "{}" not found.'.format(operand[2]))
|
|
|
|
breaker = True
|
|
|
|
|
2016-06-29 14:32:49 +05:30
|
|
|
if breaker: continue # found mistake in condition evaluation --> next file
|
2016-05-17 05:36:13 +05:30
|
|
|
|
|
|
|
evaluator_condition = "'" + condition + "'.format(" + ','.join(interpolator) + ")"
|
|
|
|
|
|
|
|
# ------------------------------------------ build formulae ----------------------------------------
|
|
|
|
|
2011-12-02 20:45:36 +05:30
|
|
|
evaluator = {}
|
2013-12-09 21:24:47 +05:30
|
|
|
|
2011-12-02 20:45:36 +05:30
|
|
|
for label,formula in zip(options.labels,options.formulas):
|
2014-07-10 14:57:51 +05:30
|
|
|
for column in re.findall(r'#(.+?)#',formula): # loop over column labels in formula
|
2015-08-08 00:33:26 +05:30
|
|
|
idx = table.label_index(column)
|
|
|
|
dim = table.label_dimension(column)
|
2013-12-09 21:24:47 +05:30
|
|
|
if column in specials:
|
2015-08-08 00:33:26 +05:30
|
|
|
replacement = 'specials["{}"]'.format(column)
|
|
|
|
elif dim == 1: # scalar input
|
|
|
|
replacement = 'float(table.data[{}])'.format(idx) # take float value of data column
|
|
|
|
elif dim > 1: # multidimensional input (vector, tensor, etc.)
|
|
|
|
replacement = 'np.array(table.data[{}:{}],dtype=float)'.format(idx,idx+dim) # use (flat) array representation
|
2012-02-16 17:26:16 +05:30
|
|
|
else:
|
2016-05-17 05:36:13 +05:30
|
|
|
damask.util.croak('column {} not found, skipping {}...'.format(column,label))
|
|
|
|
options.labels.remove(label)
|
2015-08-08 00:33:26 +05:30
|
|
|
break
|
|
|
|
|
|
|
|
formula = formula.replace('#'+column+'#',replacement)
|
2013-07-17 03:18:23 +05:30
|
|
|
|
2016-05-17 05:36:13 +05:30
|
|
|
evaluator[label] = formula
|
|
|
|
|
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
firstLine = True
|
2014-07-22 01:25:05 +05:30
|
|
|
outputAlive = True
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
specials['_row_'] += 1 # count row
|
2016-05-17 05:36:13 +05:30
|
|
|
output.data_clear()
|
|
|
|
|
2014-07-10 14:57:51 +05:30
|
|
|
# ------------------------------------------ calculate one result to get length of labels ---------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
if firstLine:
|
2015-08-08 00:33:26 +05:30
|
|
|
firstLine = False
|
|
|
|
labelDim = {}
|
2016-05-17 05:36:13 +05:30
|
|
|
for label in [x for x in options.labels]:
|
2015-08-08 00:33:26 +05:30
|
|
|
labelDim[label] = np.size(eval(evaluator[label]))
|
2016-05-17 05:36:13 +05:30
|
|
|
if labelDim[label] == 0: options.labels.remove(label)
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-05-17 05:36:13 +05:30
|
|
|
output.labels_clear()
|
|
|
|
tabLabels = table.labels()
|
|
|
|
for label in tabLabels:
|
|
|
|
dim = labelDim[label] if label in options.labels \
|
|
|
|
else table.label_dimension(label)
|
|
|
|
output.labels_append(['{}_{}'.format(i+1,label) for i in xrange(dim)] if dim > 1 else label)
|
|
|
|
|
|
|
|
for label in options.labels:
|
|
|
|
if label in tabLabels: continue
|
|
|
|
output.labels_append(['{}_{}'.format(i+1,label) for i in xrange(labelDim[label])]
|
|
|
|
if labelDim[label] > 1
|
|
|
|
else label)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-05-17 05:36:13 +05:30
|
|
|
output.info = table.info
|
|
|
|
output.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
|
|
|
output.head_write()
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
|
|
|
|
2016-05-17 05:36:13 +05:30
|
|
|
for label in output.labels():
|
|
|
|
oldIndices = table.label_indexrange(label)
|
2016-06-29 14:32:49 +05:30
|
|
|
Nold = max(1,len(oldIndices)) # Nold could be zero for new columns
|
2016-05-17 05:36:13 +05:30
|
|
|
Nnew = len(output.label_indexrange(label))
|
2016-05-17 05:39:00 +05:30
|
|
|
output.data_append(eval(evaluator[label]) if label in options.labels and
|
2016-06-29 14:32:49 +05:30
|
|
|
(options.condition is None or eval(eval(evaluator_condition)))
|
|
|
|
else np.tile([table.data[i] for i in oldIndices]
|
|
|
|
if label in tabLabels
|
|
|
|
else np.nan,
|
|
|
|
np.ceil(float(Nnew)/Nold))[:Nnew]) # spread formula result into given number of columns
|
2015-08-13 02:29:10 +05:30
|
|
|
|
2016-06-29 14:32:49 +05:30
|
|
|
outputAlive = output.data_write() # output processed line
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-05-17 05:36:13 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-06-29 14:32:49 +05:30
|
|
|
table.input_close() # close ASCII tables
|
|
|
|
output.close() # close ASCII tables
|
2016-05-17 20:11:38 +05:30
|
|
|
|