switched to damask.ASCIItable parsing, now aware of synonyms in geometry header, and speed-up by some orders of magnitude due to scipy.ndimage.filters use...

This commit is contained in:
Philip Eisenlohr 2013-11-04 14:12:10 +00:00
parent 2ab3e829a5
commit 463ecfd296
1 changed files with 62 additions and 75 deletions

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 no BOM -*- # -*- coding: UTF-8 no BOM -*-
import os,sys,string,re,math,numpy import os,sys,string,re,math,numpy, damask
from scipy import ndimage
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
scriptID = '$Id$' scriptID = '$Id$'
@ -29,6 +30,11 @@ class extendedOption(Option):
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
# MAIN # MAIN
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
synonyms = {
'grid': ['resolution'],
'size': ['dimension'],
}
identifiers = { identifiers = {
'grid': ['a','b','c'], 'grid': ['a','b','c'],
'size': ['x','y','z'], 'size': ['x','y','z'],
@ -53,12 +59,9 @@ parser.add_option('-v', '--vicinity', dest='vicinity', type='int', \
parser.add_option('-m', '--microstructureoffset', dest='offset', type='int', \ parser.add_option('-m', '--microstructureoffset', dest='offset', type='int', \
help='offset (positive or negative) for tagged microstructure. '+ help='offset (positive or negative) for tagged microstructure. '+
'"0" selects maximum microstructure index [%default]') '"0" selects maximum microstructure index [%default]')
parser.add_option('-2', '--twodimensional', dest='twoD', action='store_true', \
help='output geom file with two-dimensional data arrangement')
parser.set_defaults(vicinity = 1) parser.set_defaults(vicinity = 1)
parser.set_defaults(offset = 0) parser.set_defaults(offset = 0)
parser.set_defaults(twoD = False)
(options, filenames) = parser.parse_args() (options, filenames) = parser.parse_args()
@ -84,36 +87,27 @@ for file in files:
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n') if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n') else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
firstline = file['input'].readline() theTable = damask.ASCIItable(file['input'],file['output'],labels=False)
m = re.search('(\d+)\s*head', firstline.lower()) theTable.head_read()
if m:
headerlines = int(m.group(1))
headers = [file['input'].readline() for i in range(headerlines)]
else:
headerlines = 1
headers = firstline
content = file['input'].readlines() #--- interpret header ----------------------------------------------------------------------------
file['input'].close()
#--- interprete header ----------------------------------------------------------------------------
info = { info = {
'grid': numpy.zeros(3,'i'), 'grid': numpy.zeros(3,'i'),
'size': numpy.zeros(3,'d'), 'size': numpy.zeros(3,'d'),
'origin': numpy.zeros(3,'d'), 'origin': numpy.zeros(3,'d'),
'microstructures': 0, 'homogenization': 0,
'homogenization': 0 'microstructures': 0,
} }
newInfo = { newInfo = {
'microstructures': 0, 'microstructures': 0,
} }
extra_header = []
new_header = [] for header in theTable.info:
new_header.append(scriptID+'\n')
for header in headers:
headitems = map(str.lower,header.split()) headitems = map(str.lower,header.split())
if headitems[0] == 'resolution': headitems[0] = 'grid' if len(headitems) == 0: continue
if headitems[0] == 'dimension': headitems[0] = 'size' for synonym,alternatives in synonyms.iteritems():
if headitems[0] in alternatives: headitems[0] = synonym
if headitems[0] in mappings.keys(): if headitems[0] in mappings.keys():
if headitems[0] in identifiers.keys(): if headitems[0] in identifiers.keys():
for i in xrange(len(identifiers[headitems[0]])): for i in xrange(len(identifiers[headitems[0]])):
@ -122,7 +116,7 @@ for file in files:
else: else:
info[headitems[0]] = mappings[headitems[0]](headitems[1]) info[headitems[0]] = mappings[headitems[0]](headitems[1])
else: else:
new_header.append(header) extra_header.append(header)
file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \ file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \
'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \ 'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \
@ -132,69 +126,62 @@ for file in files:
if numpy.any(info['grid'] < 1): if numpy.any(info['grid'] < 1):
file['croak'].write('invalid grid a b c.\n') file['croak'].write('invalid grid a b c.\n')
sys.exit() continue
if numpy.any(info['size'] <= 0.0): if numpy.any(info['size'] <= 0.0):
file['croak'].write('invalid size x y z.\n') file['croak'].write('invalid size x y z.\n')
sys.exit() continue
#--- read data ------------------------------------------------------------------------------------ #--- read data ------------------------------------------------------------------------------------
microstructure = numpy.zeros(info['grid'],'i') microstructure = numpy.zeros(info['grid'].prod(),'i')
i = 0 i = 0
for line in content: theTable.data_rewind()
for item in map(int,line.split()): while theTable.data_read():
microstructure[i%info['grid'][0], items = theTable.data
(i/info['grid'][0])%info['grid'][1], if len(items) > 2:
i/info['grid'][0] /info['grid'][1]] = item if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
i += 1 elif items[1].lower() == 'to': items = xrange(int(items[0]),1+int(items[2]))
else: items = map(int,items)
else: items = map(int,items)
s = len(items)
microstructure[i:i+s] = items
i += s
#--- do work ------------------------------------------------------------------------------------
microstructure = microstructure.reshape(info['grid'],order='F')
if options.offset == 0: if options.offset == 0:
options.offset = microstructure.max() options.offset = microstructure.max()
formatwidth = 1+int(math.floor(math.log10(abs(microstructure.max()+options.offset))))
#--- search --------------------------------------------------------------------------------------- microstructure = numpy.where(ndimage.filters.maximum_filter(microstructure,size=1+2*options.vicinity,mode='wrap') ==
for x in xrange(info['grid'][0]): ndimage.filters.minimum_filter(microstructure,size=1+2*options.vicinity,mode='wrap'),
for y in xrange(info['grid'][1]): microstructure, microstructure + options.offset)
for z in xrange(info['grid'][2]):
me = microstructure[x,y,z]
breaker = False
for dx in xrange(-options.vicinity,options.vicinity+1):
for dy in xrange(-options.vicinity,options.vicinity+1):
for dz in xrange(-options.vicinity,options.vicinity+1):
they = microstructure[(x+dx)%info['grid'][0],(y+dy)%info['grid'][1],(z+dz)%info['grid'][2]]
if they != me and they != me+options.offset: # located alien microstructure in vicinity
microstructure[x,y,z] += options.offset # tag myself as close to aliens!
breaker = True
break
if breaker: break
if breaker: break
newInfo['microstructures'] = microstructure.max() newInfo['microstructures'] = microstructure.max()
if (newInfo['microstructures'] != info['microstructures']): if (newInfo['microstructures'] != info['microstructures']):
file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures']) file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures'])
# --- assemble header ----------------------------------------------------------------------------- #--- write header ---------------------------------------------------------------------------------
new_header.append("grid\ta %i\tb %i\tc %i\n"%(info['grid'][0],info['grid'][1],info['grid'][2])) theTable.labels_clear()
new_header.append("size\tx %f\ty %f\tz %f\n"%(info['size'][0],info['size'][1],info['size'][0])) theTable.info_clear()
new_header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2])) theTable.info_append(extra_header+[
new_header.append("microstructures\t%i\n"%newInfo['microstructures']) scriptID,
new_header.append("homogenization\t%i\n"%info['homogenization']) "grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],),
file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header)) "size\tx %f\ty %f\tz %f"%(info['size'][0],info['size'][1],info['size'][2],),
"origin\tx %f\ty %f\tz %f"%(info['origin'][0],info['origin'][1],info['origin'][2],),
"homogenization\t%i"%info['homogenization'],
"microstructures\t%i"%(newInfo['microstructures']),
])
theTable.head_write()
theTable.output_flush()
# --- write microstructure information ------------------------------------------------------------ # --- write microstructure information ------------------------------------------------------------
for z in xrange(info['grid'][2]): formatwidth = int(math.floor(math.log10(microstructure.max())+1))
for y in xrange(info['grid'][1]): theTable.data = microstructure.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
file['output'].write({True:' ',False:'\n'}[options.twoD]. theTable.data_writeArray('%%%ii'%(formatwidth))
join(map(lambda x: str(x).rjust(formatwidth), microstructure[:,y,z])) + '\n')
file['output'].write('\n')
#--- output finalization -------------------------------------------------------------------------- #--- output finalization --------------------------------------------------------------------------
if file['name'] != 'STDIN': if file['name'] != 'STDIN':
file['input'].close()
file['output'].close() file['output'].close()
os.rename(file['name']+'_tmp',file['name']) os.rename(file['name']+'_tmp',file['name'])