From f4c53e0be8c6aa6a15ab2aa40d7352bfbb5a58b8 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 24 Sep 2015 15:34:27 +0000 Subject: [PATCH] removed warnings by pyflakes, implemented new croak functionality --- processing/pre/geom_fromAng.py | 14 +- processing/pre/geom_fromEuclideanDistance.py | 10 +- processing/pre/geom_fromMinimalSurface.py | 6 +- processing/pre/geom_fromTable.py | 16 +-- processing/pre/geom_fromVPSC.py | 124 ++++++++---------- .../pre/geom_fromVoronoiTessellation.py | 21 +-- processing/pre/geom_pack.py | 8 +- processing/pre/geom_unpack.py | 6 +- processing/pre/seeds_fromRandom.py | 4 +- 9 files changed, 100 insertions(+), 109 deletions(-) diff --git a/processing/pre/geom_fromAng.py b/processing/pre/geom_fromAng.py index 04d5499b4..57486d06f 100755 --- a/processing/pre/geom_fromAng.py +++ b/processing/pre/geom_fromAng.py @@ -63,7 +63,7 @@ for name in filenames: table = damask.ASCIItable(name = name, outname = os.path.splitext(name)[0] +'%s'%('_material.config' if options.config else '.geom'), buffered = False, labeled = False, readonly=True) except: continue - table.croak(damask.util.emph(scriptName)+(': '+name if name else '')) + damask.util.report(scriptName,name) info = { 'grid': np.ones(3,'i'), @@ -86,7 +86,7 @@ for name in filenames: if words[0] == '#': # process initial comments/header block if len(words) > 2: if words[2].lower() == 'hexgrid': - table.croak('The file has HexGrid format. Please first convert to SquareGrid...\n') + damask.util.croak('The file has HexGrid format. Please first convert to SquareGrid...\n') break else: currPos = words[3:5] @@ -110,10 +110,10 @@ for name in filenames: limits = [360,180,360] if any([np.any(eulerangles[:,i]>=limits[i]) for i in [0,1,2]]): - table.croak('Error: euler angles out of bound. Ang file might contain unidexed poins.\n') + damask.util.croak('Error: euler angles out of bound. Ang file might contain unidexed poins.\n') for i,angle in enumerate(['phi1','PHI','phi2']): for n in np.nditer(np.where(eulerangles[:,i]>=limits[i]),['zerosize_ok']): - table.croak('%s in line %i (%4.2f %4.2f %4.2f)\n' + damask.util.croak('%s in line %i (%4.2f %4.2f %4.2f)\n' %(angle,n,eulerangles[n,0],eulerangles[n,1],eulerangles[n,2])) continue eulerangles=np.around(eulerangles,int(options.precision)) # round to desired precision @@ -154,17 +154,17 @@ for name in filenames: info['microstructures'] = len(microstructure) #--- report --------------------------------------------------------------------------------------- - table.croak('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + + damask.util.croak('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + 'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + 'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + 'homogenization: %i\n'%info['homogenization'] + 'microstructures: %i\n\n'%info['microstructures']) if np.any(info['grid'] < 1): - table.croak('invalid grid a b c.\n') + damask.util.croak('invalid grid a b c.\n') continue if np.any(info['size'] <= 0.0): - table.croak('invalid size x y z.\n') + damask.util.croak('invalid size x y z.\n') continue diff --git a/processing/pre/geom_fromEuclideanDistance.py b/processing/pre/geom_fromEuclideanDistance.py index e0652c3a0..928990621 100755 --- a/processing/pre/geom_fromEuclideanDistance.py +++ b/processing/pre/geom_fromEuclideanDistance.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: UTF-8 no BOM -*- -import os,sys,string,re,math,itertools +import os,sys,string,math,itertools import numpy as np from scipy import ndimage from optparse import OptionParser @@ -132,14 +132,14 @@ for name in filenames: table = damask.ASCIItable(name = name, buffered = False, labeled = False, readonly = True) except: continue - table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name else '')) + damask.util.report(scriptName,name) # --- interpret header ---------------------------------------------------------------------------- table.head_read() info,extra_header = table.head_getGeom() - table.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), + damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), 'size x y z: %s'%(' x '.join(map(str,info['size']))), 'origin x y z: %s'%(' : '.join(map(str,info['origin']))), 'homogenization: %i'%info['homogenization'], @@ -150,7 +150,7 @@ for name in filenames: if np.any(info['grid'] < 1): errors.append('invalid grid a b c.') if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.') if errors != []: - table.croak(errors) + damask.util.croak(errors) table.close(dismiss = True) continue @@ -187,7 +187,7 @@ for name in filenames: buffered = False, labeled = False) except: continue - table.croak(features[feature]['alias'][0]) + damask.util.croak(features[feature]['alias'][0]) distance = np.where(uniques >= features[feature]['aliens'],0.0,1.0) # seed with 0.0 when enough unique neighbor IDs are present distance = ndimage.morphology.distance_transform_edt(distance)*[options.scale]*3 diff --git a/processing/pre/geom_fromMinimalSurface.py b/processing/pre/geom_fromMinimalSurface.py index a35aa4961..6f35033b0 100755 --- a/processing/pre/geom_fromMinimalSurface.py +++ b/processing/pre/geom_fromMinimalSurface.py @@ -80,7 +80,7 @@ for name in filenames: table = damask.ASCIItable(outname = name, buffered = False, labeled = False) except: continue - table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name else '')) + damask.util.report(scriptName,name) # ------------------------------------------ make grid ------------------------------------- @@ -95,7 +95,7 @@ for name in filenames: #--- report --------------------------------------------------------------------------------------- - table.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), + damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), 'size x y z: %s'%(' x '.join(map(str,info['size']))), 'origin x y z: %s'%(' : '.join(map(str,info['origin']))), 'homogenization: %i'%info['homogenization'], @@ -106,7 +106,7 @@ for name in filenames: if np.any(info['grid'] < 1): errors.append('invalid grid a b c.') if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.') if errors != []: - table.croak(errors) + damask.util.croak(errors) table.close(dismiss = True) continue diff --git a/processing/pre/geom_fromTable.py b/processing/pre/geom_fromTable.py index 0898f66d5..614554ea3 100755 --- a/processing/pre/geom_fromTable.py +++ b/processing/pre/geom_fromTable.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: UTF-8 no BOM -*- -import os,sys,re,string,math +import os,sys,string,math import scipy.spatial, numpy as np from optparse import OptionParser import damask @@ -119,10 +119,10 @@ if filenames == []: filenames = [None] for name in filenames: try: table = damask.ASCIItable(name = name, - outname = os.path.splitext(name)[0]+'.geom' if name else name, + outname = os.path.splitext(name)[-2]+'.geom' if name else name, buffered = False) except: continue - table.croak(damask.util.emph(scriptName)+(': '+name if name else '')) + damask.util.report(scriptName,name) # ------------------------------------------ read head --------------------------------------- @@ -141,7 +141,7 @@ for name in filenames: errors.append('phase column {} is not scalar.'.format(options.phase)) if errors != []: - table.croak(errors) + damask.util.croak(errors) table.close(dismiss = True) continue @@ -173,7 +173,7 @@ for name in filenames: errors.append('regular grid spacing {} violated.'.format(' x '.join(map(repr,delta)))) if errors != []: - table.croak(errors) + damask.util.croak(errors) table.close(dismiss = True) continue @@ -198,7 +198,7 @@ for name in filenames: for z in xrange(grid[2]): for y in xrange(grid[1]): for x in xrange(grid[0]): - if (myRank+1)%(N/100.) < 1: table.croak('.',False) + if (myRank+1)%(N/100.) < 1: damask.util.croak('.',False) myData = table.data[index[myRank]] mySym = options.symmetry[min(int(myData[colPhase]),len(options.symmetry))-1] # select symmetry from option (take last specified option for all with higher index) if inputtype == 'eulers': @@ -236,7 +236,7 @@ for name in filenames: myRank += 1 - table.croak('') + damask.util.croak('') # --- generate header ---------------------------------------------------------------------------- @@ -248,7 +248,7 @@ for name in filenames: 'homogenization': options.homogenization, } - table.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), + damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), 'size x y z: %s'%(' x '.join(map(str,info['size']))), 'origin x y z: %s'%(' : '.join(map(str,info['origin']))), 'homogenization: %i'%info['homogenization'], diff --git a/processing/pre/geom_fromVPSC.py b/processing/pre/geom_fromVPSC.py index 0261f94f1..a4d5a68f7 100755 --- a/processing/pre/geom_fromVPSC.py +++ b/processing/pre/geom_fromVPSC.py @@ -27,8 +27,6 @@ parser.add_option('--phase', dest='phase', type='int', nargs = 2, metavar = 'int help='phase indices for configuration %default') parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'int', help='crystallite index for configuration [%default]') -parser.add_option('-c', '--configuration', dest='config', action='store_true', - help='output material configuration [%default]') parser.add_option('--compress', dest='compress', action='store_true', help='lump identical microstructure and texture information [%default]') parser.add_option('-p', '--precision', dest='precision', choices=['0','1','2','3'], metavar = 'int', @@ -45,27 +43,16 @@ parser.set_defaults(precision = '2') (options,filenames) = parser.parse_args() -#--- setup file handles --------------------------------------------------------------------------- -files = [] -if filenames == []: - files.append({'name':'STDIN', - 'input':sys.stdin, - 'output':sys.stdout, - 'croak':sys.stderr, - }) -else: - for name in filenames: - if os.path.exists(name): - files.append({'name':name, - 'input':open(name), - 'output':open(name+'_tmp','w'), - 'croak':sys.stdout, - }) +if filenames == []: filenames = [None] - -#--- loop over input files ------------------------------------------------------------------------ -for file in files: - file['croak'].write('\033[1m' + scriptName + '\033[0m: ' + (file['name'] if file['name'] != 'STDIN' else '') + '\n') +for name in filenames: + try: + table = damask.ASCIItable(name = name, + outname = os.path.splitext(name)[-2]+'.geom' if name else name, + buffered = False, + labeled = False) + except: continue + damask.util.report(scriptName,name) info = { 'grid': np.zeros(3,'i'), @@ -81,19 +68,20 @@ for file in files: phase = [] eulerangles = [] -# --------------- read data ----------------------------------------------------------------------- - for line in file['input']: - if line.strip(): - words = line.split() - currPos = words[3:6] + outputAlive = True + +# ------------------------------------------ process data ------------------------------------------ + while outputAlive and table.data_read(): + if table.data != []: + currPos = table.data[3:6] for i in xrange(3): coords[i][currPos[i]] = True currPos = map(float,currPos) for i in xrange(3): pos['min'][i] = min(pos['min'][i],currPos[i]) pos['max'][i] = max(pos['max'][i],currPos[i]) - eulerangles.append(map(math.degrees,map(float,words[:3]))) - phase.append(options.phase[int(float(words[options.column-1]) > options.threshold)]) + eulerangles.append(map(math.degrees,map(float,table.data[:3]))) + phase.append(options.phase[int(float(table.data[options.column-1]) > options.threshold)]) # --------------- determine size and grid --------------------------------------------------------- info['grid'] = np.array(map(len,coords),'i') @@ -130,60 +118,62 @@ for file in files: texture = np.arange(info['grid'].prod()) microstructure = np.hstack( zip(texture,phase) ).reshape(info['grid'].prod(),2) # create texture/phase pairs formatOut = 1+int(math.log10(len(texture))) - textureOut =['\n\n'] + + config_header = [] + + formatwidth = 1+int(math.log10(len(microstructure))) + config_header += [''] + for i in xrange(len(microstructure)): + config_header += ['[Grain%s]'%str(i+1).zfill(formatwidth), + 'crystallite\t%i'%options.crystallite, + '(constituent)\tphase %i\ttexture %i\tfraction 1.0'%(microstructure[i,1],microstructure[i,0]+1) + ] + config_header += [''] eulerFormatOut='%%%i.%if'%(int(options.precision)+4,int(options.precision)) - outStringAngles='(gauss) phi1 '+eulerFormatOut+' Phi '+eulerFormatOut+' phi2 '+eulerFormatOut+' scatter 0.0 fraction 1.0\n' + outStringAngles='(gauss) phi1 '+eulerFormatOut+' Phi '+eulerFormatOut+' phi2 '+eulerFormatOut+' scatter 0.0 fraction 1.0' for i in xrange(len(texture)): - textureOut += ['[Texture%s]\n'%str(i+1).zfill(formatOut) + + config_header += ['[Texture%s]'%str(i+1).zfill(formatOut), outStringAngles%tuple(eulerangles[texture[i],...]) - ] - formatOut = 1+int(math.log10(len(microstructure))) - microstructureOut =[''] - for i in xrange(len(microstructure)): - microstructureOut += ['[Grain%s]\n'%str(i+1).zfill(formatOut) + - 'crystallite\t%i\n'%options.crystallite + - '(constituent)\tphase %i\ttexture %i\tfraction 1.0\n'%(microstructure[i,1],microstructure[i,0]+1) - ] + ] + + table.labels_clear() + table.info_clear() info['microstructures'] = len(microstructure) #--- report --------------------------------------------------------------------------------------- - 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']))) + - 'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + - 'homogenization: %i\n'%info['homogenization'] + - 'microstructures: %i\n\n'%info['microstructures']) + damask.util.croak('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + + 'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + + 'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + + 'homogenization: %i\n'%info['homogenization'] + + 'microstructures: %i\n\n'%info['microstructures']) if np.any(info['grid'] < 1): - file['croak'].write('invalid grid a b c.\n') + damask.util.croak('invalid grid a b c.\n') continue if np.any(info['size'] <= 0.0): - file['croak'].write('invalid size x y z.\n') + damask.util.croak('invalid size x y z.\n') continue #--- write data ----------------------------------------------------------------------------------- - if options.config: - file['output'].write('\n'.join(microstructureOut+ textureOut) + '\n') + table.info_append([' '.join([scriptID] + sys.argv[1:]), + "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],), + "microstructures\t%i"%info['microstructures'], + "homogenization\t%i"%info['homogenization'], + config_header + ]) + table.head_write() + if options.compress: + table.data = matPoints.reshape(info['grid'][1]*info['grid'][2],info['grid'][0]) + table.data_writeArray('%%%ii'%(formatwidth),delimiter=' ') else: - header = [' '.join([scriptID] + sys.argv[1:]), - "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],), - "microstructures\t%i"%info['microstructures'], - "homogenization\t%i"%info['homogenization'], - ] - file['output'].write('\n'.join(['%i\theader'%(len(header))] + header) + '\n') - if options.compress: - matPoints = matPoints.reshape([info['grid'][1]*info['grid'][2],info['grid'][0]]) - np.savetxt(file['output'],matPoints,fmt='%0'+str(1+int(math.log10(np.amax(matPoints))))+'d') - else: - file['output'].write("1 to %i\n"%(info['microstructures'])) + table.data = ["1 to %i\n"%(info['microstructures'])] -#--- output finalization -------------------------------------------------------------------------- - if file['name'] != 'STDIN': - file['output'].close() - os.rename(file['name']+'_tmp', - os.path.splitext(file['name'])[0] +'%s'%('_material.config' if options.config else '.geom')) +# ------------------------------------------ output finalization ----------------------------------- + + table.close() diff --git a/processing/pre/geom_fromVoronoiTessellation.py b/processing/pre/geom_fromVoronoiTessellation.py index 5a02e4940..7e51f3965 100755 --- a/processing/pre/geom_fromVoronoiTessellation.py +++ b/processing/pre/geom_fromVoronoiTessellation.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: UTF-8 no BOM -*- -import os,re,sys,math,string +import os,sys,math,string import numpy as np import multiprocessing from optparse import OptionParser @@ -75,9 +75,10 @@ def laguerreTessellation(undeformed, coords, weights, grains, nonperiodic = Fals ]).astype(float) squaredweights = np.power(np.tile(weights,len(copies)),2) # Laguerre weights (squared, size N*n) - + for i,vec in enumerate(copies): # periodic copies of seed points (size N*n) - seeds = np.append(seeds, coords+vec, axis=0) if i > 0 else coords+vec + try: seeds = np.append(seeds, coords+vec, axis=0) + except NameError: seeds = coords+vec arguments = [[arg] + [seeds,squaredweights] for arg in list(undeformed)] @@ -196,10 +197,10 @@ if filenames == []: filenames = [None] for name in filenames: try: table = damask.ASCIItable(name = name, - outname = os.path.splitext(name)[0]+'.geom' if name else name, + outname = os.path.splitext(name)[-2]+'.geom' if name else name, buffered = False) except: continue - table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name else '')) + damask.util.report(scriptName,name) # --- read header ---------------------------------------------------------------------------- @@ -242,9 +243,9 @@ for name in filenames: if options.laguerre and not hasWeights: remarks.append('missing seed weights...') else: labels += [options.weight] - if remarks != []: table.croak(remarks) + if remarks != []: damask.util.croak(remarks) if errors != []: - table.croak(errors) + damask.util.croak(errors) table.close(dismiss=True) continue @@ -264,10 +265,10 @@ for name in filenames: y = (np.arange(info['grid'][1])+0.5)*info['size'][1]/info['grid'][1] z = (np.arange(info['grid'][2])+0.5)*info['size'][2]/info['grid'][2] - table.croak('tessellating...') + damask.util.croak('tessellating...') - table.croak('...using {} cpu{}'.format(options.cpus, 's' if options.cpus > 1 else '')) + damask.util.croak('...using {} cpu{}'.format(options.cpus, 's' if options.cpus > 1 else '')) grid = np.vstack(meshgrid2(x, y, z)).reshape(3,-1).T indices = laguerreTessellation(grid, coords, weights, grains, options.nonperiodic, options.cpus) @@ -278,7 +279,7 @@ for name in filenames: if info['homogenization'] == 0: info['homogenization'] = options.homogenization - table.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), + damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), 'size x y z: %s'%(' x '.join(map(str,info['size']))), 'origin x y z: %s'%(' : '.join(map(str,info['origin']))), 'homogenization: %i'%info['homogenization'], diff --git a/processing/pre/geom_pack.py b/processing/pre/geom_pack.py index c9be3c72f..ac8c0edc7 100755 --- a/processing/pre/geom_pack.py +++ b/processing/pre/geom_pack.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: UTF-8 no BOM -*- -import os,sys,string,math +import os,sys,string import numpy as np from optparse import OptionParser import damask @@ -29,14 +29,14 @@ for name in filenames: table = damask.ASCIItable(name = name, buffered = False, labeled = False) except: continue - table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name else '')) + damask.util.report(scriptName,name) # --- interpret header ---------------------------------------------------------------------------- table.head_read() info,extra_header = table.head_getGeom() - table.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), + damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), 'size x y z: %s'%(' x '.join(map(str,info['size']))), 'origin x y z: %s'%(' : '.join(map(str,info['origin']))), 'homogenization: %i'%info['homogenization'], @@ -47,7 +47,7 @@ for name in filenames: if np.any(info['grid'] < 1): errors.append('invalid grid a b c.') if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.') if errors != []: - table.croak(errors) + damask.util.croak(errors) table.close(dismiss = True) continue diff --git a/processing/pre/geom_unpack.py b/processing/pre/geom_unpack.py index 671d7b5ff..224adbb39 100755 --- a/processing/pre/geom_unpack.py +++ b/processing/pre/geom_unpack.py @@ -36,14 +36,14 @@ for name in filenames: table = damask.ASCIItable(name = name, buffered = False, labeled = False) except: continue - table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name else '')) + damask.util.report(scriptName,name) # --- interpret header ---------------------------------------------------------------------------- table.head_read() info,extra_header = table.head_getGeom() - table.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), + damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), 'size x y z: %s'%(' x '.join(map(str,info['size']))), 'origin x y z: %s'%(' : '.join(map(str,info['origin']))), 'homogenization: %i'%info['homogenization'], @@ -54,7 +54,7 @@ for name in filenames: if np.any(info['grid'] < 1): errors.append('invalid grid a b c.') if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.') if errors != []: - table.croak(errors) + damask.util.croak(errors) table.close(dismiss = True) continue diff --git a/processing/pre/seeds_fromRandom.py b/processing/pre/seeds_fromRandom.py index 12a62966a..9d1754d27 100755 --- a/processing/pre/seeds_fromRandom.py +++ b/processing/pre/seeds_fromRandom.py @@ -205,8 +205,8 @@ for name in filenames: "randomSeed\t{}".format(options.randomSeed), ]) table.labels_clear() - table.labels_append( ['{dim}_{label}'.format(dim = 1+i,label = 'pos') for i in xrange(3)] + - ['{dim}_{label}'.format(dim = 1+i,label = 'eulerangles') for i in xrange(3)] + + table.labels_append( ['{dim}_{label}'.format(dim = 1+k,label = 'pos') for k in xrange(3)] + + ['{dim}_{label}'.format(dim = 1+k,label = 'eulerangles') for k in xrange(3)] + ['microstructure'] + (['weight'] if options.weights else [])) table.head_write()