2018-11-15 18:26:48 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-07-30 22:22:49 +05:30
|
|
|
import os,re,sys,collections
|
2018-02-27 04:20:44 +05:30
|
|
|
import math,scipy,scipy.linalg # 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
|
|
|
|
2016-07-30 06:11:15 +05:30
|
|
|
def listify(x):
|
2016-07-30 22:16:00 +05:30
|
|
|
return x if isinstance(x, collections.Iterable) else [x]
|
2016-07-30 06:11:15 +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.
|
2016-07-30 22:16:00 +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
|
2016-07-30 22:16:00 +05:30
|
|
|
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',
|
2017-08-11 21:26:27 +05:30
|
|
|
help = 'condition to alter existing column data')
|
2016-05-17 05:36:13 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
2016-10-25 00:46:29 +05:30
|
|
|
for i in range(len(options.formulas)):
|
2015-08-08 00:33:26 +05:30
|
|
|
options.formulas[i] = options.formulas[i].replace(';',',')
|
|
|
|
|
2016-07-30 22:16:00 +05:30
|
|
|
# ------------------------------------- loop over input files --------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-13 02:29:10 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2016-07-30 06:11:15 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
|
|
|
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-07-30 22:16:00 +05:30
|
|
|
# --------------------------------------------------------------------------------------------------
|
2012-02-16 17:26:16 +05:30
|
|
|
specials = { \
|
|
|
|
'_row_': 0,
|
|
|
|
}
|
|
|
|
|
2016-07-30 22:16:00 +05:30
|
|
|
# --------------------------------------- evaluate condition ---------------------------------------
|
2016-06-29 14:32:49 +05:30
|
|
|
if options.condition is not None:
|
|
|
|
condition = options.condition # copy per file, since might be altered inline
|
2016-05-17 05:36:13 +05:30
|
|
|
breaker = False
|
|
|
|
|
2017-08-11 21:26:27 +05:30
|
|
|
for position,(all,marker,column) in enumerate(set(re.findall(r'#(([s]#)?(.+?))#',condition))): # find three groups
|
|
|
|
idx = table.label_index(column)
|
|
|
|
dim = table.label_dimension(column)
|
|
|
|
if idx < 0 and column not in specials:
|
|
|
|
damask.util.croak('column "{}" not found.'.format(column))
|
|
|
|
breaker = True
|
2016-05-17 05:36:13 +05:30
|
|
|
else:
|
2017-08-11 21:26:27 +05:30
|
|
|
if column in specials:
|
|
|
|
replacement = 'specials["{}"]'.format(column)
|
|
|
|
elif dim == 1: # scalar input
|
|
|
|
replacement = '{}(table.data[{}])'.format({ '':'float',
|
|
|
|
's#':'str'}[marker],idx) # take float or string 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
|
|
|
|
|
|
|
|
condition = condition.replace('#'+all+'#',replacement)
|
|
|
|
|
|
|
|
if breaker: continue # found mistake in condition evaluation --> next file
|
2016-05-17 05:36:13 +05:30
|
|
|
|
2016-07-30 22:16:00 +05:30
|
|
|
# ------------------------------------------ build formulas ----------------------------------------
|
2016-05-17 05:36:13 +05:30
|
|
|
|
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
|
|
|
|
|
2016-07-30 22:16:00 +05:30
|
|
|
# ---------------------------- separate requested labels into old and new --------------------------
|
|
|
|
|
|
|
|
veterans = list(set(options.labels)&set(table.labels(raw=False)+table.labels(raw=True)) ) # intersection of requested and existing
|
|
|
|
newbies = list(set(options.labels)-set(table.labels(raw=False)+table.labels(raw=True)) ) # requested but not existing
|
2016-05-17 05:36:13 +05:30
|
|
|
|
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
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
if firstLine:
|
2015-08-08 00:33:26 +05:30
|
|
|
firstLine = False
|
2016-07-30 22:16:00 +05:30
|
|
|
|
|
|
|
# ---------------------------- line 1: determine dimension of formulas -----------------------------
|
|
|
|
|
2016-07-30 06:11:15 +05:30
|
|
|
resultDim = {}
|
|
|
|
for label in list(options.labels): # iterate over stable copy
|
|
|
|
resultDim[label] = np.size(eval(evaluator[label])) # get dimension of formula[label]
|
|
|
|
if resultDim[label] == 0: options.labels.remove(label) # remove label if invalid result
|
|
|
|
|
|
|
|
for veteran in list(veterans):
|
|
|
|
if resultDim[veteran] != table.label_dimension(veteran):
|
2016-07-30 22:16:00 +05:30
|
|
|
damask.util.croak('skipping {} due to inconsistent dimension...'.format(veteran))
|
|
|
|
veterans.remove(veteran) # discard culprit
|
|
|
|
|
|
|
|
# ----------------------------------- line 1: assemble header --------------------------------------
|
|
|
|
|
2016-07-30 06:11:15 +05:30
|
|
|
for newby in newbies:
|
2016-10-25 00:46:29 +05:30
|
|
|
table.labels_append(['{}_{}'.format(i+1,newby) for i in range(resultDim[newby])]
|
2016-07-30 06:11:15 +05:30
|
|
|
if resultDim[newby] > 1 else newby)
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-07-30 06:11:15 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
|
|
|
table.head_write()
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-07-30 22:16:00 +05:30
|
|
|
# -------------------------------------- evaluate formulas -----------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2017-08-12 00:49:47 +05:30
|
|
|
if options.condition is None or eval(condition): # condition for veteran replacement fulfilled
|
2016-07-30 22:16:00 +05:30
|
|
|
for veteran in veterans: # evaluate formulas that overwrite
|
2016-07-30 06:11:15 +05:30
|
|
|
table.data[table.label_index(veteran):
|
|
|
|
table.label_index(veteran)+table.label_dimension(veteran)] = \
|
|
|
|
listify(eval(evaluator[veteran]))
|
|
|
|
|
2016-07-30 22:16:00 +05:30
|
|
|
for newby in newbies: # evaluate formulas that append
|
2016-07-30 06:11:15 +05:30
|
|
|
table.data_append(listify(eval(evaluator[newby])))
|
2015-08-13 02:29:10 +05:30
|
|
|
|
2016-07-30 06:11:15 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-07-30 22:16:00 +05:30
|
|
|
# ------------------------------------- output finalization ----------------------------------------
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-07-30 06:11:15 +05:30
|
|
|
table.close() # close ASCII table
|
|
|
|
|