From 463ecfd2961c3294cea89d26fbda7aebe9cc6fb2 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Mon, 4 Nov 2013 14:12:10 +0000 Subject: [PATCH] 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... --- processing/pre/geom_vicinityOffset.py | 137 ++++++++++++-------------- 1 file changed, 62 insertions(+), 75 deletions(-) diff --git a/processing/pre/geom_vicinityOffset.py b/processing/pre/geom_vicinityOffset.py index dcfa36737..bbf3da3af 100755 --- a/processing/pre/geom_vicinityOffset.py +++ b/processing/pre/geom_vicinityOffset.py @@ -1,7 +1,8 @@ #!/usr/bin/env python # -*- 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 scriptID = '$Id$' @@ -29,6 +30,11 @@ class extendedOption(Option): #-------------------------------------------------------------------------------------------------- # MAIN #-------------------------------------------------------------------------------------------------- + +synonyms = { + 'grid': ['resolution'], + 'size': ['dimension'], + } identifiers = { 'grid': ['a','b','c'], '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', \ help='offset (positive or negative) for tagged microstructure. '+ '"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(offset = 0) -parser.set_defaults(twoD = False) (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') else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n') - firstline = file['input'].readline() - m = re.search('(\d+)\s*head', firstline.lower()) - if m: - headerlines = int(m.group(1)) - headers = [file['input'].readline() for i in range(headerlines)] - else: - headerlines = 1 - headers = firstline + theTable = damask.ASCIItable(file['input'],file['output'],labels=False) + theTable.head_read() - content = file['input'].readlines() - file['input'].close() - -#--- interprete header ---------------------------------------------------------------------------- +#--- interpret header ---------------------------------------------------------------------------- info = { - 'grid': numpy.zeros(3,'i'), - 'size': numpy.zeros(3,'d'), - 'origin': numpy.zeros(3,'d'), - 'microstructures': 0, - 'homogenization': 0 - } + 'grid': numpy.zeros(3,'i'), + 'size': numpy.zeros(3,'d'), + 'origin': numpy.zeros(3,'d'), + 'homogenization': 0, + 'microstructures': 0, + } newInfo = { - 'microstructures': 0, - } + 'microstructures': 0, + } + extra_header = [] - new_header = [] - new_header.append(scriptID+'\n') - for header in headers: + for header in theTable.info: headitems = map(str.lower,header.split()) - if headitems[0] == 'resolution': headitems[0] = 'grid' - if headitems[0] == 'dimension': headitems[0] = 'size' + if len(headitems) == 0: continue + for synonym,alternatives in synonyms.iteritems(): + if headitems[0] in alternatives: headitems[0] = synonym if headitems[0] in mappings.keys(): if headitems[0] in identifiers.keys(): for i in xrange(len(identifiers[headitems[0]])): @@ -122,7 +116,7 @@ for file in files: else: info[headitems[0]] = mappings[headitems[0]](headitems[1]) else: - new_header.append(header) + extra_header.append(header) 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']))) + \ @@ -132,69 +126,62 @@ for file in files: if numpy.any(info['grid'] < 1): file['croak'].write('invalid grid a b c.\n') - sys.exit() + continue if numpy.any(info['size'] <= 0.0): file['croak'].write('invalid size x y z.\n') - sys.exit() + continue #--- read data ------------------------------------------------------------------------------------ - microstructure = numpy.zeros(info['grid'],'i') + microstructure = numpy.zeros(info['grid'].prod(),'i') i = 0 - for line in content: - for item in map(int,line.split()): - microstructure[i%info['grid'][0], - (i/info['grid'][0])%info['grid'][1], - i/info['grid'][0] /info['grid'][1]] = item - i += 1 + theTable.data_rewind() + while theTable.data_read(): + items = theTable.data + if len(items) > 2: + if items[1].lower() == 'of': items = [int(items[2])]*int(items[0]) + 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: options.offset = microstructure.max() - formatwidth = 1+int(math.floor(math.log10(abs(microstructure.max()+options.offset)))) -#--- search --------------------------------------------------------------------------------------- - for x in xrange(info['grid'][0]): - for y in xrange(info['grid'][1]): - 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 + microstructure = numpy.where(ndimage.filters.maximum_filter(microstructure,size=1+2*options.vicinity,mode='wrap') == + ndimage.filters.minimum_filter(microstructure,size=1+2*options.vicinity,mode='wrap'), + microstructure, microstructure + options.offset) newInfo['microstructures'] = microstructure.max() if (newInfo['microstructures'] != info['microstructures']): file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures']) -# --- assemble header ----------------------------------------------------------------------------- - new_header.append("grid\ta %i\tb %i\tc %i\n"%(info['grid'][0],info['grid'][1],info['grid'][2])) - new_header.append("size\tx %f\ty %f\tz %f\n"%(info['size'][0],info['size'][1],info['size'][0])) - new_header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2])) - new_header.append("microstructures\t%i\n"%newInfo['microstructures']) - new_header.append("homogenization\t%i\n"%info['homogenization']) - file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header)) - +#--- write header --------------------------------------------------------------------------------- + theTable.labels_clear() + theTable.info_clear() + theTable.info_append(extra_header+[ + scriptID, + "grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],), + "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 ------------------------------------------------------------ - for z in xrange(info['grid'][2]): - for y in xrange(info['grid'][1]): - file['output'].write({True:' ',False:'\n'}[options.twoD]. - join(map(lambda x: str(x).rjust(formatwidth), microstructure[:,y,z])) + '\n') + formatwidth = int(math.floor(math.log10(microstructure.max())+1)) + theTable.data = microstructure.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose() + theTable.data_writeArray('%%%ii'%(formatwidth)) - file['output'].write('\n') - #--- output finalization -------------------------------------------------------------------------- if file['name'] != 'STDIN': + file['input'].close() file['output'].close() os.rename(file['name']+'_tmp',file['name']) +