reworked to match former script layouts and logics. (not yet tested, use at your own risk)
This commit is contained in:
parent
282d2d7b97
commit
7aba05ed9f
|
@ -36,7 +36,7 @@ def index(location,res):
|
||||||
# MAIN
|
# MAIN
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
|
||||||
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """
|
parser = OptionParser(option_class=extendableOption, usage='%prog options file[s]', description = """
|
||||||
Add column containing debug information
|
Add column containing debug information
|
||||||
Operates on periodic ordered three-dimensional data sets.
|
Operates on periodic ordered three-dimensional data sets.
|
||||||
|
|
||||||
|
@ -45,113 +45,128 @@ Operates on periodic ordered three-dimensional data sets.
|
||||||
|
|
||||||
|
|
||||||
parser.add_option('--no-shape','-s', dest='shape', action='store_false', \
|
parser.add_option('--no-shape','-s', dest='shape', action='store_false', \
|
||||||
help='calcuate mismatch of shape [%default]')
|
help='do not calcuate shape mismatch [%default]')
|
||||||
parser.add_option('--no-volume','-v', dest='volume', action='store_false', \
|
parser.add_option('--no-volume','-v', dest='volume', action='store_false', \
|
||||||
help='calculate mismatch of volume [%default]')
|
help='do not calculate volume mismatch [%default]')
|
||||||
parser.add_option('-d','--dimension', dest='dim', type='float', nargs=3, \
|
parser.add_option('-d','--dimension', dest='dim', type='float', nargs=3, \
|
||||||
help='physical dimension of data set in x (fast) y z (slow) [%default]')
|
help='physical dimension of data set in x (fast) y z (slow) [%default]')
|
||||||
parser.add_option('-r','--resolution', dest='res', type='int', nargs=3, \
|
parser.add_option('-r','--resolution', dest='res', type='int', nargs=3, \
|
||||||
help='resolution of data set in x (fast) y z (slow)')
|
help='resolution of data set in x (fast) y z (slow)')
|
||||||
|
parser.add_option('-f','--deformation', dest='defgrad', action='extend', type='string', \
|
||||||
|
help='heading(s) of columns containing deformation tensor values %default')
|
||||||
|
|
||||||
parser.set_defaults(volume = True)
|
parser.set_defaults(volume = True)
|
||||||
parser.set_defaults(shape = True)
|
parser.set_defaults(shape = True)
|
||||||
|
parser.set_defaults(defgrad = ['f'])
|
||||||
|
|
||||||
(options,filenames) = parser.parse_args()
|
(options,filenames) = parser.parse_args()
|
||||||
|
|
||||||
if not options.res or len(options.res) < 3:
|
if not options.res or len(options.res) < 3:
|
||||||
parser.error('improper resolution specification...')
|
parser.error('improper resolution specification...')
|
||||||
if not options.dim or len(options.dim) < 3:
|
if not options.dim or len(options.dim) < 3:
|
||||||
parser.error('improper resolution specification...')
|
parser.error('improper dimension specification...')
|
||||||
|
|
||||||
|
defgrad = {}
|
||||||
|
defgrad_av = {}
|
||||||
|
centroids = {}
|
||||||
|
nodes = {}
|
||||||
|
|
||||||
datainfo = { # list of requested labels per datatype
|
datainfo = { # list of requested labels per datatype
|
||||||
'tensor': {'len':9,
|
'defgrad': {'len':9,
|
||||||
'label':[]},
|
'label':[]},
|
||||||
}
|
}
|
||||||
|
|
||||||
datainfo['tensor']['label'] += 'f'
|
if options.defgrad != None: datainfo['defgrad']['label'] += options.defgrad
|
||||||
|
|
||||||
# ------------------------------------------ setup file handles ---------------------------------------
|
# ------------------------------------------ setup file handles ---------------------------------------
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
if filenames == []:
|
if filenames == []:
|
||||||
files.append({'name':'STDIN', 'handle':sys.stdin})
|
parser.error('no data file specified')
|
||||||
else:
|
else:
|
||||||
for name in filenames:
|
for name in filenames:
|
||||||
if os.path.exists(name):
|
if os.path.exists(name):
|
||||||
files.append({'name':name, 'handle':open(name)})
|
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w')})
|
||||||
|
|
||||||
# ------------------------------------------ loop over input files ---------------------------------------
|
# ------------------------------------------ loop over input files ---------------------------------------
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
print file['name']
|
print file['name']
|
||||||
|
|
||||||
content = file['handle'].readlines()
|
|
||||||
file['handle'].close()
|
|
||||||
|
|
||||||
# get labels by either read the first row, or - if keyword header is present - the last line of the header
|
# get labels by either read the first row, or - if keyword header is present - the last line of the header
|
||||||
|
|
||||||
headerlines = 1
|
firstline = file['input'].readline()
|
||||||
m = re.search('(\d+)\s*head', content[0].lower())
|
m = re.search('(\d+)\s*head', firstline.lower())
|
||||||
if m:
|
if m:
|
||||||
headerlines = int(m.group(1))
|
headerlines = int(m.group(1))
|
||||||
passOn = content[1:headerlines]
|
passOn = [file['input'].readline() for i in range(1,headerlines)]
|
||||||
headers = content[headerlines].split()
|
headers = file['input'].readline().split()
|
||||||
data = content[headerlines+1:]
|
else:
|
||||||
|
headerlines = 1
|
||||||
|
passOn = []
|
||||||
|
headers = firstline.split()
|
||||||
|
|
||||||
|
data = file['input'].readlines()
|
||||||
|
|
||||||
regexp = re.compile('1_\d+_')
|
|
||||||
for i,l in enumerate(headers):
|
for i,l in enumerate(headers):
|
||||||
if regexp.match(l):
|
if l.startswith('1_'):
|
||||||
headers[i] = l[2:]
|
if re.match('\d+_',l[2:]) or i == len(headers)-1 or not headers[i+1].endswith(l[2:]):
|
||||||
|
headers[i] = l[2:]
|
||||||
|
|
||||||
active = {}
|
active = {}
|
||||||
column = {}
|
column = {}
|
||||||
values = {}
|
|
||||||
head = []
|
head = []
|
||||||
|
|
||||||
for datatype,info in datainfo.items():
|
for datatype,info in datainfo.items():
|
||||||
for label in info['label']:
|
for label in info['label']:
|
||||||
key = {True :'1_%s',
|
key = {True :'1_%s',
|
||||||
False:'%s' }[info['len']>1]%label
|
False:'%s' }[info['len']>1]%label
|
||||||
if key not in headers:
|
if key not in headers:
|
||||||
print 'column %s not found...'%key
|
sys.stderr.write('column %s not found...\n'%key)
|
||||||
else:
|
else:
|
||||||
if datatype not in active: active[datatype] = []
|
if datatype not in active: active[datatype] = []
|
||||||
if datatype not in column: column[datatype] = {}
|
if datatype not in column: column[datatype] = {}
|
||||||
active[datatype].append(label)
|
active[datatype].append(label)
|
||||||
column[datatype][label]=headers.index(key)
|
column[datatype][label] = headers.index(key)
|
||||||
|
if options.shape: head += 'mismatch_shape(%s)'%label
|
||||||
defgrad = numpy.array([0.0 for i in range(9*options.res[0]*options.res[1]*options.res[2])]).\
|
if options.volume: head += 'mismatch_volume(%s)'%label
|
||||||
reshape((options.res[0],options.res[1],options.res[2],3,3))
|
|
||||||
|
|
||||||
if options.shape: head += ['shape_mismatch']
|
|
||||||
if options.volume: head += ['volume_mismatch']
|
|
||||||
|
|
||||||
# ------------------------------------------ assemble header ---------------------------------------
|
# ------------------------------------------ assemble header ---------------------------------------
|
||||||
|
|
||||||
output = '%i\theader'%(headerlines+1) + '\n' + \
|
output = '%i\theader'%(headerlines+1) + '\n' + \
|
||||||
''.join(passOn) + \
|
''.join(passOn) + \
|
||||||
string.replace('$Id$','\n','\\n')+ '\t' + \
|
string.replace('$Id$','\n','\\n')+ '\t' + \
|
||||||
' '.join(sys.argv[1:]) + '\n' + \
|
' '.join(sys.argv[1:]) + '\n' + \
|
||||||
'\t'.join(headers + head) + '\n' # build extended header
|
'\t'.join(headers + head) + '\n' # build extended header
|
||||||
|
|
||||||
# ------------------------------------------ read value field ---------------------------------------
|
|
||||||
|
|
||||||
idx = 0
|
# ------------------------------------------ read deformation tensors ---------------------------------------
|
||||||
for line in data:
|
|
||||||
items = line.split()[:len(headers)]
|
for datatype,labels in active.items():
|
||||||
if len(items) < len(headers):
|
for label in labels:
|
||||||
continue
|
defgrad[label] = numpy.array([0.0 for i in xrange(9*options.res[0]*options.res[1]*options.res[2])],'d').\
|
||||||
for datatype,labels in active.items():
|
reshape((options.res[0],options.res[1],options.res[2],3,3))
|
||||||
for label in labels:
|
|
||||||
defgrad[location(idx,options.res)[0]][location(idx,options.res)[1]][location(idx,options.res)[2]]\
|
idx = 0
|
||||||
= numpy.reshape(items[column[datatype][label]:column[datatype][label]+9],(3,3))
|
for line in data:
|
||||||
idx += 1
|
items = line.split()[:len(headers)] # take only valid first items
|
||||||
defgrad_av = postprocessingMath.tensor_avg(options.res[0],options.res[1],options.res[2],defgrad)
|
if len(items) < len(headers): # too short lines get dropped
|
||||||
centroids = postprocessingMath.deformed_fft(options.res[0],options.res[1],options.res[2],options.dim,defgrad,defgrad_av,1.0)
|
continue
|
||||||
nodes = postprocessingMath.mesh(options.res[0],options.res[1],options.res[2],options.dim,defgrad_av,centroids)
|
|
||||||
|
defgrad[label][location(idx,options.res)[0]]\
|
||||||
|
[location(idx,options.res)[1]]\
|
||||||
|
[location(idx,options.res)[2]]\
|
||||||
|
= numpy.array(map(float,items[column[datatype][label]:
|
||||||
|
column[datatype][label]+datainfo[datatype]['len']]),'d').reshape(3,3)
|
||||||
|
defgrad_av[label] = postprocessingMath.tensor_avg(options.res[0],options.res[1],options.res[2],defgrad[label])
|
||||||
|
centroids[label] = postprocessingMath.deformed_fft(options.res[0],options.res[1],options.res[2],options.dim,defgrad[label],defgrad_av[label],1.0)
|
||||||
|
nodes[label] = postprocessingMath.mesh(options.res[0],options.res[1],options.res[2],options.dim,defgrad_av[label],centroids[label])
|
||||||
|
if options.shape: shape_mismatch[label] = postprocessingMath.shape_compare( options.res[0],options.res[1],options.res[2],options.dim,nodes[label],centroids[label],defgrad[label])
|
||||||
|
if options.volume: volume_mismatch[label] = postprocessingMath.volume_compare(options.res[0],options.res[1],options.res[2],options.dim,nodes[label], defgrad[label])
|
||||||
|
idx += 1
|
||||||
|
|
||||||
# ------------------------------------------ read file ---------------------------------------
|
# ------------------------------------------ read file ---------------------------------------
|
||||||
if options.shape:
|
|
||||||
shape_mismatch = postprocessingMath.shape_compare(options.res[0],options.res[1],options.res[2],options.dim,nodes,centroids,defgrad)
|
|
||||||
if options.volume:
|
|
||||||
volume_mismatch = postprocessingMath.volume_compare(options.res[0],options.res[1],options.res[2],options.dim,nodes,defgrad)
|
|
||||||
idx = 0
|
idx = 0
|
||||||
for line in data:
|
for line in data:
|
||||||
items = line.split()[:len(headers)]
|
items = line.split()[:len(headers)]
|
||||||
|
@ -161,21 +176,18 @@ for file in files:
|
||||||
output += '\t'.join(items)
|
output += '\t'.join(items)
|
||||||
|
|
||||||
for datatype,labels in active.items():
|
for datatype,labels in active.items():
|
||||||
if options.shape:
|
for label in labels:
|
||||||
output += '\t%f'%shape_mismatch[location(idx,options.res)[0]][location(idx,options.res)[1]][location(idx,options.res)[2]]
|
|
||||||
if options.volume:
|
if options.shape: output += '\t%f'%shape_mismatch[label][location(idx,options.res)[0]][location(idx,options.res)[1]][location(idx,options.res)[2]]
|
||||||
output += '\t%f'%volume_mismatch[location(idx,options.res)[0]][location(idx,options.res)[1]][location(idx,options.res)[2]]
|
if options.volume: output += '\t%f'%volume_mismatch[label][location(idx,options.res)[0]][location(idx,options.res)[1]][location(idx,options.res)[2]]
|
||||||
output += '\n'
|
|
||||||
idx += 1
|
output += '\n'
|
||||||
|
idx += 1
|
||||||
|
|
||||||
|
file['input'].close()
|
||||||
|
|
||||||
# ------------------------------------------ output result ---------------------------------------
|
# ------------------------------------------ output result ---------------------------------------
|
||||||
|
|
||||||
if file['name'] == 'STDIN':
|
file['output'].write(output)
|
||||||
print output
|
file['output'].close
|
||||||
else:
|
os.rename(file['name']+'_tmp',file['name'])
|
||||||
file['handle'] = open(file['name']+'_tmp','w')
|
|
||||||
try:
|
|
||||||
file['handle'].write(output)
|
|
||||||
file['handle'].close()
|
|
||||||
os.rename(file['name']+'_tmp',file['name'])
|
|
||||||
except:
|
|
||||||
print 'error during writing',file['name']+'_tmp'
|
|
||||||
|
|
Loading…
Reference in New Issue