2012-12-07 03:19:49 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
import os,sys,string
|
2014-07-22 19:51:49 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
2012-12-07 03:19:49 +05:30
|
|
|
from scipy import ndimage
|
2014-07-22 19:51:49 +05:30
|
|
|
import damask
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-08-07 00:36:33 +05:30
|
|
|
scriptName = scriptID.split()[1][:-3]
|
2012-12-07 03:19:49 +05:30
|
|
|
|
|
|
|
def periodic_3Dpad(array, rimdim=(1,1,1)):
|
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
rimdim = np.array(rimdim,'i')
|
|
|
|
size = np.array(array.shape,'i')
|
|
|
|
padded = np.empty(size+2*rimdim,array.dtype)
|
2012-12-07 03:19:49 +05:30
|
|
|
padded[rimdim[0]:rimdim[0]+size[0],
|
|
|
|
rimdim[1]:rimdim[1]+size[1],
|
|
|
|
rimdim[2]:rimdim[2]+size[2]] = array
|
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
p = np.zeros(3,'i')
|
2012-12-07 03:19:49 +05:30
|
|
|
for side in xrange(3):
|
|
|
|
for p[(side+2)%3] in xrange(padded.shape[(side+2)%3]):
|
|
|
|
for p[(side+1)%3] in xrange(padded.shape[(side+1)%3]):
|
|
|
|
for p[side%3] in xrange(rimdim[side%3]):
|
|
|
|
spot = (p-rimdim)%size
|
|
|
|
padded[p[0],p[1],p[2]] = array[spot[0],spot[1],spot[2]]
|
|
|
|
for p[side%3] in xrange(rimdim[side%3]+size[side%3],size[side%3]+2*rimdim[side%3]):
|
|
|
|
spot = (p-rimdim)%size
|
|
|
|
padded[p[0],p[1],p[2]] = array[spot[0],spot[1],spot[2]]
|
|
|
|
return padded
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
features = [ \
|
2014-07-25 00:17:09 +05:30
|
|
|
{'aliens': 1, 'name': 'biplane'},
|
|
|
|
{'aliens': 1, 'name': 'boundary'},
|
|
|
|
{'aliens': 2, 'name': 'tripleline'},
|
|
|
|
{'aliens': 3, 'name': 'quadruplepoint'}
|
2012-12-07 03:19:49 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
neighborhoods = {
|
2014-07-22 19:51:49 +05:30
|
|
|
'neumann':np.array([
|
2012-12-07 03:19:49 +05:30
|
|
|
[-1, 0, 0],
|
|
|
|
[ 1, 0, 0],
|
|
|
|
[ 0,-1, 0],
|
|
|
|
[ 0, 1, 0],
|
|
|
|
[ 0, 0,-1],
|
|
|
|
[ 0, 0, 1],
|
|
|
|
]),
|
2014-07-22 19:51:49 +05:30
|
|
|
'moore':np.array([
|
2012-12-07 03:19:49 +05:30
|
|
|
[-1,-1,-1],
|
|
|
|
[ 0,-1,-1],
|
|
|
|
[ 1,-1,-1],
|
|
|
|
[-1, 0,-1],
|
|
|
|
[ 0, 0,-1],
|
|
|
|
[ 1, 0,-1],
|
|
|
|
[-1, 1,-1],
|
|
|
|
[ 0, 1,-1],
|
|
|
|
[ 1, 1,-1],
|
|
|
|
[-1,-1, 0],
|
|
|
|
[ 0,-1, 0],
|
|
|
|
[ 1,-1, 0],
|
|
|
|
[-1, 0, 0],
|
|
|
|
#
|
|
|
|
[ 1, 0, 0],
|
|
|
|
[-1, 1, 0],
|
|
|
|
[ 0, 1, 0],
|
|
|
|
[ 1, 1, 0],
|
|
|
|
[-1,-1, 1],
|
|
|
|
[ 0,-1, 1],
|
|
|
|
[ 1,-1, 1],
|
|
|
|
[-1, 0, 1],
|
|
|
|
[ 0, 0, 1],
|
|
|
|
[ 1, 0, 1],
|
|
|
|
[-1, 1, 1],
|
|
|
|
[ 0, 1, 1],
|
|
|
|
[ 1, 1, 1],
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2014-08-06 18:57:09 +05:30
|
|
|
Add column(s) containing Euclidean distance to grain structural features: boundaries, triple lines, and quadruple points.
|
|
|
|
|
|
|
|
""", version = scriptID)
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-07-25 00:17:09 +05:30
|
|
|
parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string',
|
2014-07-22 19:51:49 +05:30
|
|
|
help='column heading for coordinates [%default]')
|
2014-07-25 00:17:09 +05:30
|
|
|
parser.add_option('-i','--identifier', dest='id', action='store', type='string', metavar = 'string',
|
2014-07-22 19:51:49 +05:30
|
|
|
help='heading of column containing grain identifier [%default]')
|
2014-07-25 00:17:09 +05:30
|
|
|
parser.add_option('-t','--type', dest='type', action='extend', type='string', metavar='<string LIST>',
|
|
|
|
help='feature type (%s)'%(', '.join(map(lambda x:', '.join([x['name']]),features))))
|
|
|
|
parser.add_option('-n','--neighborhood',dest='neigborhood', action='store', type='choice',
|
|
|
|
choices=neighborhoods.keys(), metavar='string',
|
|
|
|
help='type of neighborhood (%s) [neumann]'%(', '.join(neighborhoods.keys())))
|
2012-12-07 03:19:49 +05:30
|
|
|
parser.set_defaults(type = [])
|
2014-07-25 00:17:09 +05:30
|
|
|
parser.set_defaults(coords = 'ip')
|
2012-12-07 03:19:49 +05:30
|
|
|
parser.set_defaults(id = 'texture')
|
|
|
|
parser.set_defaults(neighborhood = 'neumann')
|
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2014-07-25 00:17:09 +05:30
|
|
|
if len(options.type) == 0: parser.error('please select a feature type')
|
|
|
|
if not set(options.type).issubset(set(map(lambda x: x['name'],features))):
|
|
|
|
parser.error('type must be chosen from (%s)...'%(', '.join(map(lambda x:', '.join([x['name']]),features))))
|
|
|
|
if 'biplane' in options.type and 'boundary' in options.type:
|
|
|
|
parser.error("please select only one alias for 'biplane' and 'boundary'")
|
|
|
|
|
2012-12-07 03:19:49 +05:30
|
|
|
feature_list = []
|
|
|
|
for i,feature in enumerate(features):
|
2014-07-25 00:17:09 +05:30
|
|
|
if feature['name'] in options.type: feature_list.append(i) # remember valid features
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ setup file handles ------------------------------------
|
2012-12-07 03:19:49 +05:30
|
|
|
|
|
|
|
files = []
|
2014-07-22 19:51:49 +05:30
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
|
|
|
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
# ------------------------------------------ loop over input files ---------------------------------
|
2012-12-07 03:19:49 +05:30
|
|
|
for file in files:
|
2014-07-22 19:51:49 +05:30
|
|
|
file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table
|
|
|
|
table.head_read() # read ASCII header info
|
2014-08-06 18:57:09 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
# --------------- figure out position of labels and coordinates ------------------------------------
|
|
|
|
try:
|
|
|
|
locationCol = table.labels.index('%s.x'%options.coords) # columns containing location data
|
|
|
|
except ValueError:
|
2014-08-06 18:57:09 +05:30
|
|
|
file['croak'].write('no coordinate data (%s.x) found...\n'%options.coords)
|
2014-07-22 19:51:49 +05:30
|
|
|
continue
|
2012-12-07 03:19:49 +05:30
|
|
|
|
|
|
|
if options.id not in table.labels:
|
|
|
|
file['croak'].write('column %s not found...\n'%options.id)
|
|
|
|
continue
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2012-12-07 03:19:49 +05:30
|
|
|
for feature in feature_list:
|
2014-07-25 00:17:09 +05:30
|
|
|
table.labels_append('ED_%s(%s)'%(features[feature]['name'],options.id)) # extend ASCII header with new labels
|
2012-12-07 03:19:49 +05:30
|
|
|
|
|
|
|
table.head_write()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
table.data_readArray([options.coords+'.x',options.coords+'.y',options.coords+'.z',options.id])
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
coords = [{},{},{}]
|
2013-12-14 09:21:22 +05:30
|
|
|
for i in xrange(len(table.data)):
|
2012-12-07 03:19:49 +05:30
|
|
|
for j in xrange(3):
|
2014-08-06 20:55:18 +05:30
|
|
|
coords[j][str(table.data[i,j])] = True
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
grid = np.array(map(len,coords),'i')
|
2012-12-07 03:19:49 +05:30
|
|
|
unitlength = 0.0
|
2014-08-06 20:55:18 +05:30
|
|
|
for i,r in enumerate(grid):
|
|
|
|
if r > 1: unitlength = max(unitlength,(max(map(float,coords[i].keys()))-min(map(float,coords[i].keys())))/(r-1.0))
|
2012-12-07 03:19:49 +05:30
|
|
|
|
|
|
|
neighborhood = neighborhoods[options.neighborhood]
|
2014-08-06 20:55:18 +05:30
|
|
|
convoluted = np.empty([len(neighborhood)]+list(grid+2),'i')
|
|
|
|
microstructure = periodic_3Dpad(np.array(table.data[:,3].reshape(grid),'i'))
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2012-12-07 03:19:49 +05:30
|
|
|
for i,p in enumerate(neighborhood):
|
2014-07-22 19:51:49 +05:30
|
|
|
stencil = np.zeros((3,3,3),'i')
|
2012-12-07 03:19:49 +05:30
|
|
|
stencil[1,1,1] = -1
|
|
|
|
stencil[p[0]+1,
|
|
|
|
p[1]+1,
|
|
|
|
p[2]+1] = 1
|
|
|
|
|
|
|
|
convoluted[i,:,:,:] = ndimage.convolve(microstructure,stencil)
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
distance = np.ones((len(feature_list),grid[0],grid[1],grid[2]),'d')
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2014-07-22 19:51:49 +05:30
|
|
|
convoluted = np.sort(convoluted,axis=0)
|
2014-08-06 20:55:18 +05:30
|
|
|
uniques = np.zeros(grid)
|
|
|
|
check = np.empty(grid)
|
2014-07-22 19:51:49 +05:30
|
|
|
check[:,:,:] = np.nan
|
2012-12-07 16:20:34 +05:30
|
|
|
for i in xrange(len(neighborhood)):
|
2014-07-22 19:51:49 +05:30
|
|
|
uniques += np.where(convoluted[i,1:-1,1:-1,1:-1] == check,0,1)
|
2012-12-07 23:16:28 +05:30
|
|
|
check = convoluted[i,1:-1,1:-1,1:-1]
|
2012-12-07 16:20:34 +05:30
|
|
|
for i,feature_id in enumerate(feature_list):
|
2014-07-22 19:51:49 +05:30
|
|
|
distance[i,:,:,:] = np.where(uniques > features[feature_id]['aliens'],0.0,1.0)
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2012-12-07 03:19:49 +05:30
|
|
|
for i in xrange(len(feature_list)):
|
2012-12-19 20:20:45 +05:30
|
|
|
distance[i,:,:,:] = ndimage.morphology.distance_transform_edt(distance[i,:,:,:])*[unitlength]*3
|
2014-08-06 20:55:18 +05:30
|
|
|
distance.shape = (len(feature_list),grid.prod())
|
2012-12-07 16:20:34 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2012-12-07 03:19:49 +05:30
|
|
|
table.data_rewind()
|
|
|
|
l = 0
|
|
|
|
while table.data_read():
|
|
|
|
for i in xrange(len(feature_list)):
|
2014-07-22 19:51:49 +05:30
|
|
|
table.data_append(distance[i,l]) # add all distance fields
|
2012-12-07 03:19:49 +05:30
|
|
|
l += 1
|
2014-08-04 23:23:41 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
2014-07-22 19:51:49 +05:30
|
|
|
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
2012-12-07 03:19:49 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
file['input'].close() # close input ASCII table
|
|
|
|
file['output'].close() # close output ASCII table
|
2014-07-22 19:51:49 +05:30
|
|
|
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|