checked Unix command piping conformity -- seems to now work:

seeds_fromRandom |
geom_fromVoronoiTessellation |
geom_grainGrowth |
geom_unpack |
geom_pack

However, grainGrowth functionality seems broken...
This commit is contained in:
Philip Eisenlohr 2014-02-03 23:44:29 +00:00
parent f66c0727d0
commit ccb950f3d8
5 changed files with 98 additions and 62 deletions

View File

@ -15,14 +15,15 @@ class ASCIItable():
def __init__(self, def __init__(self,
fileIn = sys.stdin, fileIn = sys.stdin,
fileOut = sys.stdout, fileOut = sys.stdout,
buffered = True, buffered = False, # flush writes
labels = True): labels = True): # assume table has labels
self.__IO__ = {'in': fileIn, self.__IO__ = {'in': fileIn,
'out':fileOut, 'out':fileOut,
'output':[], 'output':[],
'buffered':buffered, 'buffered':buffered,
'labels':labels, 'labels':labels,
'validReadSize': 0, 'validReadSize': 0,
'readBuffer': [], # buffer to hold non-advancing reads
'dataStart': 0, 'dataStart': 0,
} }
self.info = [] self.info = []
@ -100,7 +101,8 @@ class ASCIItable():
pass pass
if self.__IO__['validReadSize'] == 0: # in case no valid data length is known if self.__IO__['validReadSize'] == 0: # in case no valid data length is known
self.__IO__['validReadSize'] = len(self.__IO__['in'].readline().split()) # assume constant data width from first line self.data_read(advance = False)
self.__IO__['validReadSize'] = len(self.data) # assume constant data width from first line
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def head_write(self): def head_write(self):
@ -123,7 +125,7 @@ class ASCIItable():
def labels_append(self, def labels_append(self,
what): what):
''' '''
add item or list to existing set of labels add item or list to existing set of labels (and switch on labeling)
''' '''
if not isinstance(what, (str, unicode)): if not isinstance(what, (str, unicode)):
try: try:
@ -137,6 +139,9 @@ class ASCIItable():
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def labels_clear(self): def labels_clear(self):
'''
delete existing labels and switch to no labeling
'''
self.labels = [] self.labels = []
self.__IO__['labels'] = False self.__IO__['labels'] = False
@ -183,6 +188,9 @@ class ASCIItable():
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def info_clear(self): def info_clear(self):
'''
delete any info block
'''
self.info = [] self.info = []
# ------------------------------------------------------------------ # ------------------------------------------------------------------
@ -190,13 +198,28 @@ class ASCIItable():
self.__IO__['in'].seek(self.__IO__['dataStart']) self.__IO__['in'].seek(self.__IO__['dataStart'])
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_skipLines(self,lines): def data_skipLines(self,count):
for i in range(lines): '''
self.__IO__['in'].readline() wind forward by count number of lines
'''
for i in xrange(count):
alive = self.data_read()
return alive
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_read(self): def data_read(self,advance = True):
line = self.__IO__['in'].readline() # get next data row '''
read next line (possibly buffered) and parse it into data array
'''
if len(self.__IO__['readBuffer']) > 0:
line = self.__IO__['readBuffer'].pop(0) # take buffered content
else:
line = self.__IO__['in'].readline() # get next data row from file
if not advance:
self.__IO__['readBuffer'].append(line) # keep line just read in buffer
if self.__IO__['labels']: if self.__IO__['labels']:
items = line.split()[:self.__IO__['validReadSize']] # use up to valid size (label count) items = line.split()[:self.__IO__['validReadSize']] # use up to valid size (label count)
self.data = items if len(items) == self.__IO__['validReadSize'] else [] # take if correct number of entries self.data = items if len(items) == self.__IO__['validReadSize'] else [] # take if correct number of entries
@ -207,8 +230,11 @@ class ASCIItable():
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_readLine(self,line): def data_readLine(self,line):
'''
seek beginning of data and wind forward to selected line
'''
self.__IO__['in'].seek(self.__IO__['dataStart']) self.__IO__['in'].seek(self.__IO__['dataStart'])
for i in range(line-1): for i in xrange(line-1):
self.__IO__['in'].readline() self.__IO__['in'].readline()
self.data_read() self.data_read()
@ -223,15 +249,21 @@ class ASCIItable():
if labels == []: indices = range(self.__IO__['validReadSize']) # use all columns if labels == []: indices = range(self.__IO__['validReadSize']) # use all columns
else: indices = self.labels_index(labels) # use specified columns else: indices = self.labels_index(labels) # use specified columns
self.data_rewind() try:
self.data_rewind() # try to wind back to start of data
except:
pass # assume/hope we are at data start already...
self.data = numpy.loadtxt(self.__IO__['in'], usecols=indices) self.data = numpy.loadtxt(self.__IO__['in'], usecols=indices)
if len(self.data.shape) < 2: # single column if len(self.data.shape) < 2: # single column
self.data = self.data.reshape(self.data.shape[0],1) self.data = self.data.reshape(self.data.shape[0],1)
return self.data.shape return self.data.shape
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_write(self, delimiter='\t'): def data_write(self,delimiter = '\t'):
if len(self.data) == 0: return '''
write current data array and report alive output back
'''
if len(self.data) == 0: return True
if isinstance(self.data[0],list): if isinstance(self.data[0],list):
return self.output_write([delimiter.join(map(str,items)) for items in self.data]) return self.output_write([delimiter.join(map(str,items)) for items in self.data])
@ -239,12 +271,12 @@ class ASCIItable():
return self.output_write(delimiter.join(map(str,self.data))) return self.output_write(delimiter.join(map(str,self.data)))
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_writeArray(self,format='%g',delimiter = '\t'): def data_writeArray(self,format = '%g',delimiter = '\t'):
import numpy import numpy
''' '''
write whole numpy array data write whole numpy array data
''' '''
return numpy.savetxt(self.__IO__['out'], self.data, fmt=format, delimiter=delimiter) return numpy.savetxt(self.__IO__['out'],self.data,fmt = format,delimiter = delimiter)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_append(self, def data_append(self,

View File

@ -119,25 +119,25 @@ 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')
theTable = damask.ASCIItable(file['input'],file['output']) theTable = damask.ASCIItable(file['input'],file['output'],buffered = False)
theTable.head_read() theTable.head_read()
labels = ['x','y','z'] labels = ['x','y','z']
index = 0 index = 0
if numpy.all(theTable.labels_index(['phi1','Phi','phi2'])) != -1:
hasEulers = True hasEulers = numpy.all(theTable.labels_index(['phi1','Phi','phi2'])) != -1
hasGrains = theTable.labels_index('microstructure') != -1
if hasEulers:
labels += ['phi1','Phi','phi2'] labels += ['phi1','Phi','phi2']
index += 3 index += 3
else:
hasEulers = False
eulerCol = index eulerCol = index
if theTable.labels_index('microstructure') != -1: if hasGrains:
hasGrains = True
labels += ['microstructure'] labels += ['microstructure']
index += 1 index += 1
else:
hasGrains = False
grainCol = index grainCol = index
theTable.data_readArray(labels) theTable.data_readArray(labels)
@ -257,7 +257,6 @@ for file in files:
"microstructures\t%i"%(newInfo['microstructures']), "microstructures\t%i"%(newInfo['microstructures']),
]) ])
theTable.head_write() theTable.head_write()
theTable.output_flush()
# --- write microstructure information ------------------------------------------------------------ # --- write microstructure information ------------------------------------------------------------
formatwidth = 1+int(math.log10(newInfo['microstructures'])) formatwidth = 1+int(math.log10(newInfo['microstructures']))

View File

@ -90,7 +90,7 @@ 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')
theTable = damask.ASCIItable(file['input'],file['output'],labels=False) theTable = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False)
theTable.head_read() theTable.head_read()
#--- interpret header ---------------------------------------------------------------------------- #--- interpret header ----------------------------------------------------------------------------
@ -137,8 +137,8 @@ for file in files:
#--- read data ------------------------------------------------------------------------------------ #--- read data ------------------------------------------------------------------------------------
microstructure = numpy.zeros(numpy.prod([2 if i == 1 else i for i in info['grid']]),'i') # 2D structures do not work microstructure = numpy.zeros(numpy.prod([2 if i == 1 else i for i in info['grid']]),'i') # 2D structures do not work
i = 0 i = 0
theTable.data_rewind()
while theTable.data_read(): while theTable.data_read(): # read next data line of ASCII table
items = theTable.data items = theTable.data
if len(items) > 2: if len(items) > 2:
if items[1].lower() == 'of': items = [int(items[2])]*int(items[0]) if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
@ -179,15 +179,15 @@ for file in files:
for i in range(3): for i in range(3):
for j in range(3): for j in range(3):
for k in range(3): for k in range(3):
boundary = numpy.maximum(boundary,\ boundary = numpy.maximum(boundary,
interfacialEnergy(microstructure,microExt[i:microstructure.shape[0]+i,\ interfacialEnergy(microstructure,microExt[i:microstructure.shape[0]+i,
j:microstructure.shape[1]+j,\ j:microstructure.shape[1]+j,
k:microstructure.shape[2]+k])) k:microstructure.shape[2]+k]))
index = ndimage.morphology.distance_transform_edt(boundary == 0.,return_distances=False,return_indices=True) index = ndimage.morphology.distance_transform_edt(boundary == 0.,return_distances = False,return_indices = True)
boundary = numpy.fft.irfftn(numpy.fft.rfftn(numpy.where(ndimage.morphology.binary_dilation(boundary!=0.,\ boundary = numpy.fft.irfftn(numpy.fft.rfftn(numpy.where(ndimage.morphology.binary_dilation(boundary != 0.,
structure=struc,\ structure = struc,
iterations=2*options.d-1),\ iterations = 2*options.d-1),
boundary[index[0].flatten(),index[1].flatten(),index[2].flatten()].reshape(microstructure.shape),\ boundary[index[0].flatten(),index[1].flatten(),index[2].flatten()].reshape(microstructure.shape),
0.))*gauss) 0.))*gauss)
index = ndimage.morphology.distance_transform_edt(boundary >= 0.5,return_distances=False,return_indices=True) index = ndimage.morphology.distance_transform_edt(boundary >= 0.5,return_distances=False,return_indices=True)
microstructure = microstructure[index[0].flatten(),index[1].flatten(),index[2].flatten()].reshape(microstructure.shape) microstructure = microstructure[index[0].flatten(),index[1].flatten(),index[2].flatten()].reshape(microstructure.shape)
@ -211,7 +211,6 @@ for file in files:
"microstructures\t%i"%(newInfo['microstructures']), "microstructures\t%i"%(newInfo['microstructures']),
]) ])
theTable.head_write() theTable.head_write()
theTable.output_flush()
# --- write microstructure information ------------------------------------------------------------ # --- write microstructure information ------------------------------------------------------------
formatwidth = int(math.floor(math.log10(microstructure.max())+1)) formatwidth = int(math.floor(math.log10(microstructure.max())+1))

View File

@ -76,7 +76,7 @@ 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')
theTable = damask.ASCIItable(file['input'],file['output'],labels=False) theTable = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False)
theTable.head_read() theTable.head_read()
@ -130,7 +130,6 @@ for file in files:
"microstructures\t%i"%(info['microstructures']), "microstructures\t%i"%(info['microstructures']),
]) ])
theTable.head_write() theTable.head_write()
theTable.output_flush()
# --- write packed microstructure information ----------------------------------------------------- # --- write packed microstructure information -----------------------------------------------------
type = '' type = ''
@ -138,8 +137,8 @@ for file in files:
start = -1 start = -1
reps = 0 reps = 0
theTable.data_rewind() outputAlive = True
while theTable.data_read(): while outputAlive and theTable.data_read(): # read next data line of ASCII table
items = theTable.data items = theTable.data
if len(items) > 2: if len(items) > 2:
if items[1].lower() == 'of': items = [int(items[2])]*int(items[0]) if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
@ -155,28 +154,36 @@ for file in files:
type = 'of' type = 'of'
reps += 1 reps += 1
else: else:
theTable.data = { if type == '':
'' : [], theTable.data = []
'.' : [str(former)], elif type == '.':
'to': ['%i to %i'%(former-reps+1,former)], theTable.data = [str(former)]
'of': ['%i of %i'%(reps,former)], elif type == 'to':
}[type] theTable.data = ['%i to %i'%(former-reps+1,former)]
theTable.data_write(delimiter=' ') elif type == 'of':
theTable.data = ['%i of %i'%(reps,former)]
outputAlive = theTable.data_write(delimiter = ' ') # output processed line
type = '.' type = '.'
start = current start = current
reps = 1 reps = 1
former = current former = current
theTable.data = { theTable.data = {
'.' : [str(former)], '.' : [str(former)],
'to': ['%i to %i'%(former-reps+1,former)], 'to': ['%i to %i'%(former-reps+1,former)],
'of': ['%i of %i'%(reps,former)], 'of': ['%i of %i'%(reps,former)],
}[type] }[type]
theTable.data_write(delimiter=' ') outputAlive = theTable.data_write(delimiter = ' ') # output processed line
theTable.output_flush()
# ------------------------------------------ output result ---------------------------------------
outputAlive and theTable.output_flush() # just in case of buffered ASCII table
#--- output finalization -------------------------------------------------------------------------- #--- output finalization --------------------------------------------------------------------------
if file['name'] != 'STDIN': if file['name'] != 'STDIN':
file['input'].close() file['input'].close() # close input ASCII table
file['output'].close() file['output'].close() # close input ASCII table
os.rename(file['name']+'_tmp',file['name']) os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new

View File

@ -81,7 +81,7 @@ 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')
theTable = damask.ASCIItable(file['input'],file['output'],labels=False) theTable = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False)
theTable.head_read() theTable.head_read()
@ -126,8 +126,8 @@ for file in files:
#--- read data ------------------------------------------------------------------------------------ #--- read data ------------------------------------------------------------------------------------
microstructure = numpy.zeros(info['grid'].prod(),'i') microstructure = numpy.zeros(info['grid'].prod(),'i')
i = 0 i = 0
theTable.data_rewind()
while theTable.data_read(): while theTable.data_read(): # read next data line of ASCII table
items = theTable.data items = theTable.data
if len(items) > 2: if len(items) > 2:
if items[1].lower() == 'of': items = [int(items[2])]*int(items[0]) if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
@ -151,7 +151,6 @@ for file in files:
"microstructures\t%i"%(info['microstructures']), "microstructures\t%i"%(info['microstructures']),
]) ])
theTable.head_write() theTable.head_write()
theTable.output_flush()
# --- write microstructure information ------------------------------------------------------------ # --- write microstructure information ------------------------------------------------------------
formatwidth = int(math.floor(math.log10(microstructure.max())+1)) formatwidth = int(math.floor(math.log10(microstructure.max())+1))
@ -163,6 +162,6 @@ for file in files:
#--- output finalization -------------------------------------------------------------------------- #--- output finalization --------------------------------------------------------------------------
if file['name'] != 'STDIN': if file['name'] != 'STDIN':
file['input'].close() file['input'].close() # close input ASCII table
file['output'].close() file['output'].close() # close input ASCII table
os.rename(file['name']+'_tmp',file['name']) os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new