can deal with "veterans" and "newbies" meaning over ride existing with new

This commit is contained in:
Aritra Chakraborty 2016-07-29 20:41:15 -04:00
parent 920cf2c849
commit 304fdf1ebe
1 changed files with 37 additions and 43 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2.7 #!/usr/bin/env python2
# -*- coding: UTF-8 no BOM -*- # -*- coding: UTF-8 no BOM -*-
import os,re,sys import os,re,sys
@ -6,10 +6,15 @@ import math
import numpy as np import numpy as np
from optparse import OptionParser from optparse import OptionParser
import damask import damask
import collections
scriptName = os.path.splitext(os.path.basename(__file__))[0] scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version]) scriptID = ' '.join([scriptName,damask.version])
def listify(x):
return (x if isinstance(x, collections.Iterable) else [x])
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# MAIN # MAIN
# -------------------------------------------------------------------- # --------------------------------------------------------------------
@ -55,13 +60,9 @@ for i in xrange(len(options.formulas)):
if filenames == []: filenames = [None] if filenames == []: filenames = [None]
for name in filenames: for name in filenames:
try: try: table = damask.ASCIItable(name = name,
table = damask.ASCIItable(name = name, buffered = False)
buffered = False) except: continue
output = damask.ASCIItable(name = name,
buffered = False)
except:
continue
damask.util.report(scriptName,name) damask.util.report(scriptName,name)
# ------------------------------------------ read header ------------------------------------------- # ------------------------------------------ read header -------------------------------------------
@ -129,53 +130,46 @@ for name in filenames:
while outputAlive and table.data_read(): # read next data line of ASCII table while outputAlive and table.data_read(): # read next data line of ASCII table
specials['_row_'] += 1 # count row specials['_row_'] += 1 # count row
output.data_clear()
# ------------------------------------------ calculate one result to get length of labels --------- # ------------------------------------------ calculate one result to get length of labels ---------
if firstLine: if firstLine:
firstLine = False firstLine = False
labelDim = {} resultDim = {}
for label in [x for x in options.labels]: for label in list(options.labels): # iterate over stable copy
labelDim[label] = np.size(eval(evaluator[label])) resultDim[label] = np.size(eval(evaluator[label])) # get dimension of formula[label]
if labelDim[label] == 0: options.labels.remove(label) if resultDim[label] == 0: options.labels.remove(label) # remove label if invalid result
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
for veteran in list(veterans):
if resultDim[veteran] != table.label_dimension(veteran):
damask.util.croak('{} is ignored due to inconsistent dimension...'.format(veteran))
veterans.remove(veteran) # ignore culprit
for newby in newbies:
table.labels_append(['{}_{}'.format(i+1,newby) for i in xrange(resultDim[newby])]
if resultDim[newby] > 1 else newby)
# ------------------------------------------ assemble header --------------------------------------- # ------------------------------------------ assemble header ---------------------------------------
output.labels_clear() table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
tabLabels = table.labels() table.head_write()
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)
output.info = table.info
output.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
output.head_write()
# ------------------------------------------ process data ------------------------------------------ # ------------------------------------------ process data ------------------------------------------
for label in output.labels(): if options.condition is None or eval(eval(evaluator_condition)): # condition for veteran replacement fulfilled
oldIndices = table.label_indexrange(label) for veteran in veterans: # evaluate formulae that overwrite
Nold = max(1,len(oldIndices)) # Nold could be zero for new columns table.data[table.label_index(veteran):
Nnew = len(output.label_indexrange(label)) table.label_index(veteran)+table.label_dimension(veteran)] = \
output.data_append(eval(evaluator[label]) if label in options.labels and listify(eval(evaluator[veteran]))
(options.condition is None or eval(eval(evaluator_condition)))
else np.tile([table.data[i] for i in oldIndices] for newby in newbies: # evaluate formulae that append
if label in tabLabels table.data_append(listify(eval(evaluator[newby])))
else np.nan,
np.ceil(float(Nnew)/Nold))[:Nnew]) # spread formula result into given number of columns
outputAlive = output.data_write() # output processed line outputAlive = table.data_write() # output processed line
# ------------------------------------------ output finalization ----------------------------------- # ------------------------------------------ output finalization -----------------------------------
table.input_close() # close ASCII tables table.close() # close ASCII table
output.close() # close ASCII tables