simplified internal handling of conditions and added vector capability to it
This commit is contained in:
parent
c531dbc022
commit
b7d4b3d190
|
@ -41,7 +41,7 @@ parser.add_option('-f','--formula',
|
||||||
|
|
||||||
parser.add_option('-c','--condition',
|
parser.add_option('-c','--condition',
|
||||||
dest = 'condition', metavar='string',
|
dest = 'condition', metavar='string',
|
||||||
help = 'condition to filter rows')
|
help = 'condition to alter existing column data')
|
||||||
|
|
||||||
parser.set_defaults(condition = None,
|
parser.set_defaults(condition = None,
|
||||||
)
|
)
|
||||||
|
@ -81,24 +81,24 @@ for name in filenames:
|
||||||
condition = options.condition # copy per file, since might be altered inline
|
condition = options.condition # copy per file, since might be altered inline
|
||||||
breaker = False
|
breaker = False
|
||||||
|
|
||||||
for position,operand in enumerate(set(re.findall(r'#(([s]#)?(.+?))#',condition))): # find three groups
|
for position,(all,marker,column) in enumerate(set(re.findall(r'#(([s]#)?(.+?))#',condition))): # find three groups
|
||||||
condition = condition.replace('#'+operand[0]+'#',
|
idx = table.label_index(column)
|
||||||
{ '': '{%i}'%position,
|
dim = table.label_dimension(column)
|
||||||
's#':'"{%i}"'%position}[operand[1]])
|
if idx < 0 and column not in specials:
|
||||||
if operand[2] in specials: # special label
|
damask.util.croak('column "{}" not found.'.format(column))
|
||||||
interpolator += ['specials["%s"]'%operand[2]]
|
breaker = True
|
||||||
else:
|
else:
|
||||||
try:
|
if column in specials:
|
||||||
interpolator += ['%s(table.data[%i])'%({ '':'float',
|
replacement = 'specials["{}"]'.format(column)
|
||||||
's#':'str'}[operand[1]],
|
elif dim == 1: # scalar input
|
||||||
table.label_index(operand[2]))] # could be generalized to indexrange as array lookup
|
replacement = '{}(table.data[{}])'.format({ '':'float',
|
||||||
except:
|
's#':'str'}[marker],idx) # take float or string value of data column
|
||||||
damask.util.croak('column "{}" not found.'.format(operand[2]))
|
elif dim > 1: # multidimensional input (vector, tensor, etc.)
|
||||||
breaker = True
|
replacement = 'np.array(table.data[{}:{}],dtype=float)'.format(idx,idx+dim) # use (flat) array representation
|
||||||
|
|
||||||
if breaker: continue # found mistake in condition evaluation --> next file
|
condition = condition.replace('#'+all+'#',replacement)
|
||||||
|
|
||||||
evaluator_condition = "'" + condition + "'.format(" + ','.join(interpolator) + ")"
|
if breaker: continue # found mistake in condition evaluation --> next file
|
||||||
|
|
||||||
# ------------------------------------------ build formulas ----------------------------------------
|
# ------------------------------------------ build formulas ----------------------------------------
|
||||||
|
|
||||||
|
@ -162,7 +162,7 @@ for name in filenames:
|
||||||
|
|
||||||
# -------------------------------------- evaluate formulas -----------------------------------------
|
# -------------------------------------- evaluate formulas -----------------------------------------
|
||||||
|
|
||||||
if options.condition is None or eval(eval(evaluator_condition)): # condition for veteran replacement fulfilled
|
if options.condition is None or eval(condition): # condition for veteran replacement fulfilled
|
||||||
for veteran in veterans: # evaluate formulas that overwrite
|
for veteran in veterans: # evaluate formulas that overwrite
|
||||||
table.data[table.label_index(veteran):
|
table.data[table.label_index(veteran):
|
||||||
table.label_index(veteran)+table.label_dimension(veteran)] = \
|
table.label_index(veteran)+table.label_dimension(veteran)] = \
|
||||||
|
|
Loading…
Reference in New Issue