From f30cbde99e27ff2d8fce711599d824eb347dd0e8 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 08:04:40 +0100 Subject: [PATCH 1/9] use first constituent as default --- python/damask/dadf5.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/damask/dadf5.py b/python/damask/dadf5.py index 60f993bf9..84168cd94 100644 --- a/python/damask/dadf5.py +++ b/python/damask/dadf5.py @@ -369,7 +369,7 @@ class DADF5(): return f[self.get_dataset_location('orientation')[0]].attrs['Lattice'].astype('str') # np.bytes_ to string - def read_dataset(self,path,c): + def read_dataset(self,path,c=0): """ Dataset for all points/cells. From c9b19444930d7ea9a9f6db6cf1d15535f7c459d9 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 08:50:27 +0100 Subject: [PATCH 2/9] handling of derived datatypes/structs currently required for orientation --- processing/post/DADF5_postResults.py | 4 ++-- python/damask/dadf5.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/processing/post/DADF5_postResults.py b/processing/post/DADF5_postResults.py index 0db018173..a16ef147c 100755 --- a/processing/post/DADF5_postResults.py +++ b/processing/post/DADF5_postResults.py @@ -65,7 +65,7 @@ for filename in options.filenames: x = results.get_dataset_location(label) if len(x) == 0: continue - array = results.read_dataset(x,0) + array = results.read_dataset(x,0,plain=True) d = int(np.product(np.shape(array)[1:])) data = np.concatenate((data,np.reshape(array,[np.product(results.grid),d])),1) @@ -80,7 +80,7 @@ for filename in options.filenames: x = results.get_dataset_location(label) if len(x) == 0: continue - array = results.read_dataset(x,0) + array = results.read_dataset(x,0,plain=True) d = int(np.product(np.shape(array)[1:])) data = np.concatenate((data,np.reshape(array,[np.product(results.grid),d])),1) diff --git a/python/damask/dadf5.py b/python/damask/dadf5.py index 84168cd94..69b19fefd 100644 --- a/python/damask/dadf5.py +++ b/python/damask/dadf5.py @@ -369,7 +369,7 @@ class DADF5(): return f[self.get_dataset_location('orientation')[0]].attrs['Lattice'].astype('str') # np.bytes_ to string - def read_dataset(self,path,c=0): + def read_dataset(self,path,c=0,plain=False): """ Dataset for all points/cells. @@ -402,7 +402,7 @@ class DADF5(): a=a.reshape([a.shape[0],1]) dataset[p,:] = a[u,:] - return dataset + return dataset if not plain else dataset.view(('float64',len(dataset.dtype.names))) def cell_coordinates(self): From 6060abb3752b01fbf5d7efbe98ea0856ed4fc29b Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 08:52:46 +0100 Subject: [PATCH 3/9] cleaning --- processing/pre/geom_clean.py | 3 --- processing/pre/geom_mirror.py | 2 -- python/damask/geom.py | 8 ++++++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index 7ee87fe0a..50f3657b2 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -5,9 +5,6 @@ import sys from io import StringIO from optparse import OptionParser -from scipy import ndimage -import numpy as np - import damask diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index 3f9755818..77ec1f4d7 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -5,8 +5,6 @@ import sys from io import StringIO from optparse import OptionParser -import numpy as np - import damask diff --git a/python/damask/geom.py b/python/damask/geom.py index 6fb9af5a5..6dfcda013 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -311,8 +311,8 @@ class Geom(): """ header = self.get_header() grid = self.get_grid() - format_string = '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) if self.microstructure.dtype == int \ - else '%g' + format_string = '%g' if self.microstructure in np.sctypes['float'] else \ + '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) np.savetxt(fname, self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, header='\n'.join(header), fmt=format_string, comments='') @@ -390,12 +390,14 @@ class Geom(): def mirror(self,directions,reflect=False): """ Mirror microstructure along given directions. + Parameters ---------- directions : iterable containing str direction(s) along which the microstructure is mirrored. Valid entries are 'x', 'y', 'z'. reflect : bool, optional reflect (include) outermost layers. + """ valid = {'x','y','z'} if not all(isinstance(d, str) for d in directions): @@ -420,10 +422,12 @@ class Geom(): def clean(self,stencil=3): """ Smooth microstructure by selecting most frequent index within given stencil at each location. + Parameters ---------- stencil : int, optional size of smoothing stencil. + """ def mostFrequent(arr): unique, inverse = np.unique(arr, return_inverse=True) From 02df55b9bde2d94c2caacf4a4a9ecac8f5dd7e35 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 11:18:41 +0100 Subject: [PATCH 4/9] bugfix casting to plain array works only (and makes sense only) for a derived type --- python/damask/dadf5.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python/damask/dadf5.py b/python/damask/dadf5.py index 69b19fefd..d879946eb 100644 --- a/python/damask/dadf5.py +++ b/python/damask/dadf5.py @@ -401,8 +401,11 @@ class DADF5(): if len(a.shape) == 1: a=a.reshape([a.shape[0],1]) dataset[p,:] = a[u,:] - - return dataset if not plain else dataset.view(('float64',len(dataset.dtype.names))) + + if plain and dataset.dtype.names is not None: + return dataset.view(('float64',len(dataset.dtype.names))) + else: + return dataset def cell_coordinates(self): From 8186be6293de991047a9acfbbbc6ad20397f2d28 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 14:27:24 +0100 Subject: [PATCH 5/9] compress functionality should be part of the geom class automated decision is base on heuristic whether compression is memory efficient --- processing/pre/geom_clean.py | 5 +-- processing/pre/geom_mirror.py | 5 +-- processing/pre/geom_pack.py | 41 ++---------------------- processing/pre/geom_unpack.py | 5 +-- python/damask/geom.py | 60 +++++++++++++++++++++++++++++++---- 5 files changed, 59 insertions(+), 57 deletions(-) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index 50f3657b2..65700ab61 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -42,7 +42,4 @@ for name in filenames: geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index 77ec1f4d7..f27b7eb66 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -46,7 +46,4 @@ for name in filenames: damask.util.croak(geom.mirror(options.directions,options.reflect)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_pack.py b/processing/pre/geom_pack.py index 786a40b95..e927c006f 100755 --- a/processing/pre/geom_pack.py +++ b/processing/pre/geom_pack.py @@ -33,42 +33,5 @@ for name in filenames: damask.util.croak(geom) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - - compressType = None - former = start = -1 - reps = 0 - - if name is None: - f = sys.stdout - else: - f= open(name,'w') - - for current in geom.microstructure.flatten('F'): - if abs(current - former) == 1 and (start - current) == reps*(former - current): - compressType = 'to' - reps += 1 - elif current == former and start == former: - compressType = 'of' - reps += 1 - else: - if compressType is None: - f.write('\n'.join(geom.get_header())+'\n') - elif compressType == '.': - f.write('{}\n'.format(former)) - elif compressType == 'to': - f.write('{} to {}\n'.format(start,former)) - elif compressType == 'of': - f.write('{} of {}\n'.format(reps,former)) - - compressType = '.' - start = current - reps = 1 - - former = current - - if compressType == '.': - f.write('{}\n'.format(former)) - elif compressType == 'to': - f.write('{} to {}\n'.format(start,former)) - elif compressType == 'of': - f.write('{} of {}\n'.format(reps,former)) + + geom.to_file(sys.stdout if name is None else name,pack=True) diff --git a/processing/pre/geom_unpack.py b/processing/pre/geom_unpack.py index 2a2d54130..58bd5de87 100755 --- a/processing/pre/geom_unpack.py +++ b/processing/pre/geom_unpack.py @@ -34,7 +34,4 @@ for name in filenames: damask.util.croak(geom) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/python/damask/geom.py b/python/damask/geom.py index 6dfcda013..61d7c64a4 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -299,7 +299,7 @@ class Geom(): return cls(microstructure.reshape(grid),size,origin,homogenization,comments) - def to_file(self,fname): + def to_file(self,fname,pack=None): """ Writes a geom file. @@ -307,15 +307,63 @@ class Geom(): ---------- fname : str or file handle geometry file to write. + pack : bool, optional + compress geometry with 'x of y' and 'a to b'. """ header = self.get_header() grid = self.get_grid() - format_string = '%g' if self.microstructure in np.sctypes['float'] else \ - '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) - np.savetxt(fname, - self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, - header='\n'.join(header), fmt=format_string, comments='') + + if pack is None: + plain = grid.prod()/np.unique(self.microstructure).size < 250 + else: + plain = not pack + + if plain: + format_string = '%g' if self.microstructure in np.sctypes['float'] else \ + '%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.microstructure))))) + np.savetxt(fname, + self.microstructure.reshape([grid[0],np.prod(grid[1:])],order='F').T, + header='\n'.join(header), fmt=format_string, comments='') + else: + if isinstance(fname,str): + f = open(fname,'w') + else: + f = fname + + compressType = None + former = start = -1 + reps = 0 + for current in self.microstructure.flatten('F'): + if abs(current - former) == 1 and (start - current) == reps*(former - current): + compressType = 'to' + reps += 1 + elif current == former and start == former: + compressType = 'of' + reps += 1 + else: + if compressType is None: + f.write('\n'.join(self.get_header())+'\n') + elif compressType == '.': + f.write('{}\n'.format(former)) + elif compressType == 'to': + f.write('{} to {}\n'.format(start,former)) + elif compressType == 'of': + f.write('{} of {}\n'.format(reps,former)) + + compressType = '.' + start = current + reps = 1 + + former = current + + if compressType == '.': + f.write('{}\n'.format(former)) + elif compressType == 'to': + f.write('{} to {}\n'.format(start,former)) + elif compressType == 'of': + f.write('{} of {}\n'.format(reps,former)) + def to_vtk(self,fname=None): """ From 816e86ae5fa5240bfae20b75ce305aa19564bb11 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 15:13:26 +0100 Subject: [PATCH 6/9] phasing out python shell scripts --- processing/pre/geom_rescale.py | 19 ++++--------------- python/damask/geom.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/processing/pre/geom_rescale.py b/processing/pre/geom_rescale.py index e84c7597b..84ac9e793 100755 --- a/processing/pre/geom_rescale.py +++ b/processing/pre/geom_rescale.py @@ -55,20 +55,9 @@ for name in filenames: np.array([o*float(n.lower().replace('x','')) if n.lower().endswith('x') \ else float(n) for o,n in zip(size,options.size)],dtype=float) - damask.util.croak(geom.update(microstructure = - ndimage.interpolation.zoom( - geom.microstructure, - new_grid/grid, - output=geom.microstructure.dtype, - order=0, - mode='nearest', - prefilter=False, - ) if np.any(new_grid != grid) \ - else None, - size = new_size)) + geom.scale(new_grid) + damask.util.croak(geom.update(microstructure = None, + size = new_size)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/python/damask/geom.py b/python/damask/geom.py index 61d7c64a4..872445459 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -467,6 +467,29 @@ class Geom(): #self.add_comments('tbd') + def scale(self,grid): + """ + Scale microstructure to new grid + + Parameters + ---------- + grid : iterable of int + new grid dimension + + """ + return self.update( + ndimage.interpolation.zoom( + self.microstructure, + grid/self.get_grid(), + output=self.microstructure.dtype, + order=0, + mode='nearest', + prefilter=False + ) + ) + #self.add_comments('tbd') + + def clean(self,stencil=3): """ Smooth microstructure by selecting most frequent index within given stencil at each location. From b2cdabd0094c07e291a0b6f93437fa0a711a2de2 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 18:21:05 +0100 Subject: [PATCH 7/9] getting rid of shell scripts --- processing/pre/geom_canvas.py | 5 +---- processing/pre/geom_clean.py | 3 --- processing/pre/geom_mirror.py | 2 -- processing/pre/geom_renumber.py | 13 ++----------- processing/pre/geom_rescale.py | 4 +--- processing/pre/geom_rotate.py | 5 +---- processing/pre/geom_vicinityOffset.py | 5 +---- python/damask/geom.py | 17 +++++++++++++++-- 8 files changed, 21 insertions(+), 33 deletions(-) diff --git a/processing/pre/geom_canvas.py b/processing/pre/geom_canvas.py index a44065dd2..e1093d33b 100755 --- a/processing/pre/geom_canvas.py +++ b/processing/pre/geom_canvas.py @@ -71,7 +71,4 @@ for name in filenames: damask.util.croak(geom.update(canvas,origin=origin+offset*size/old,rescale=True)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index 65700ab61..153d1eebd 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -37,9 +37,6 @@ for name in filenames: damask.util.report(scriptName,name) geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - damask.util.croak(geom.clean(options.stencil)) - geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index f27b7eb66..ff47cb88b 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -42,8 +42,6 @@ for name in filenames: damask.util.report(scriptName,name) geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - damask.util.croak(geom.mirror(options.directions,options.reflect)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_renumber.py b/processing/pre/geom_renumber.py index 2eee189e1..df817b1fc 100755 --- a/processing/pre/geom_renumber.py +++ b/processing/pre/geom_renumber.py @@ -32,15 +32,6 @@ for name in filenames: damask.util.report(scriptName,name) geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - - renumbered = np.empty(geom.get_grid(),dtype=geom.microstructure.dtype) - for i, oldID in enumerate(np.unique(geom.microstructure)): - renumbered = np.where(geom.microstructure == oldID, i+1, renumbered) - - damask.util.croak(geom.update(renumbered)) + damask.util.croak(self.renumber) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_rescale.py b/processing/pre/geom_rescale.py index 84ac9e793..8f5276b9d 100755 --- a/processing/pre/geom_rescale.py +++ b/processing/pre/geom_rescale.py @@ -56,8 +56,6 @@ for name in filenames: else float(n) for o,n in zip(size,options.size)],dtype=float) geom.scale(new_grid) - damask.util.croak(geom.update(microstructure = None, - size = new_size)) + damask.util.croak(geom.update(microstructure = None,size = new_size)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_rotate.py b/processing/pre/geom_rotate.py index c2a4af04b..6cca99caf 100755 --- a/processing/pre/geom_rotate.py +++ b/processing/pre/geom_rotate.py @@ -95,7 +95,4 @@ for name in filenames: damask.util.croak(geom.update(microstructure,origin=origin-(np.asarray(microstructure.shape)-grid)/2*size/grid,rescale=True)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/processing/pre/geom_vicinityOffset.py b/processing/pre/geom_vicinityOffset.py index 3a4853121..0b86adfb9 100755 --- a/processing/pre/geom_vicinityOffset.py +++ b/processing/pre/geom_vicinityOffset.py @@ -82,7 +82,4 @@ for name in filenames: geom.microstructure + offset,geom.microstructure))) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name) diff --git a/python/damask/geom.py b/python/damask/geom.py index 872445459..e544765a4 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -504,7 +504,20 @@ class Geom(): unique, inverse = np.unique(arr, return_inverse=True) return unique[np.argmax(np.bincount(inverse))] - return self.update(ndimage.filters.generic_filter(self.microstructure, + return self.update(ndimage.filters.generic_filter( + self.microstructure, mostFrequent, - size=(stencil,)*3).astype(self.microstructure.dtype)) + size=(stencil,)*3 + ).astype(self.microstructure.dtype) + ) + #self.add_comments('tbd') + + + def renumber(self): + """Renumber sorted microstructure indices to 1,...,N.""" + renumbered = np.empty(self.get_grid(),dtype=self.microstructure.dtype) + for i, oldID in enumerate(np.unique(self.microstructure)): + renumbered = np.where(self.microstructure == oldID, i+1, renumbered) + + return self.update(renumbered)) #self.add_comments('tbd') From c9f9f7c681e6e6ae841c9956dd3cf1dc0b5d2874 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 19:02:19 +0100 Subject: [PATCH 8/9] pack causes trouble with tests .. --- processing/pre/geom_addPrimitive.py | 5 +---- processing/pre/geom_canvas.py | 4 ++-- processing/pre/geom_clean.py | 2 +- processing/pre/geom_fromDREAM3D.py | 2 +- processing/pre/geom_fromMinimalSurface.py | 5 +---- processing/pre/geom_fromOsteonGeometry.py | 5 +---- processing/pre/geom_fromScratch.py | 5 +---- processing/pre/geom_fromTable.py | 5 +---- processing/pre/geom_fromVoronoiTessellation.py | 5 +---- processing/pre/geom_grainGrowth.py | 5 +---- processing/pre/geom_mirror.py | 2 +- processing/pre/geom_renumber.py | 2 +- processing/pre/geom_rescale.py | 2 +- processing/pre/geom_rotate.py | 2 +- processing/pre/geom_translate.py | 5 +---- processing/pre/geom_vicinityOffset.py | 2 +- python/damask/geom.py | 2 +- 17 files changed, 18 insertions(+), 42 deletions(-) diff --git a/processing/pre/geom_addPrimitive.py b/processing/pre/geom_addPrimitive.py index 3e147e24d..f33ba27b1 100755 --- a/processing/pre/geom_addPrimitive.py +++ b/processing/pre/geom_addPrimitive.py @@ -132,7 +132,4 @@ for name in filenames: damask.util.croak(geom.update(np.where(mask,geom.microstructure,fill))) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_canvas.py b/processing/pre/geom_canvas.py index e1093d33b..edd5fe622 100755 --- a/processing/pre/geom_canvas.py +++ b/processing/pre/geom_canvas.py @@ -61,7 +61,7 @@ for name in filenames: canvas = np.full(new,options.fill if options.fill is not None else np.nanmax(geom.microstructure)+1,geom.microstructure.dtype) - l = np.clip( offset, 0,np.minimum(old +offset,new)) + l = np.clip( offset, 0,np.minimum(old +offset,new)) # noqa r = np.clip( offset+old,0,np.minimum(old*2+offset,new)) L = np.clip(-offset, 0,np.minimum(new -offset,old)) R = np.clip(-offset+new,0,np.minimum(new*2-offset,old)) @@ -71,4 +71,4 @@ for name in filenames: damask.util.croak(geom.update(canvas,origin=origin+offset*size/old,rescale=True)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_clean.py b/processing/pre/geom_clean.py index 153d1eebd..8883c1b2a 100755 --- a/processing/pre/geom_clean.py +++ b/processing/pre/geom_clean.py @@ -39,4 +39,4 @@ for name in filenames: geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) damask.util.croak(geom.clean(options.stencil)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_fromDREAM3D.py b/processing/pre/geom_fromDREAM3D.py index 5d41e05b9..b20749b45 100755 --- a/processing/pre/geom_fromDREAM3D.py +++ b/processing/pre/geom_fromDREAM3D.py @@ -155,4 +155,4 @@ for name in filenames: homogenization=options.homogenization,comments=header) damask.util.croak(geom) - geom.to_file(os.path.splitext(name)[0]+'.geom') + geom.to_file(os.path.splitext(name)[0]+'.geom',pack=False) diff --git a/processing/pre/geom_fromMinimalSurface.py b/processing/pre/geom_fromMinimalSurface.py index ab42ce5af..bb6859b54 100755 --- a/processing/pre/geom_fromMinimalSurface.py +++ b/processing/pre/geom_fromMinimalSurface.py @@ -89,7 +89,4 @@ geom=damask.Geom(microstructure,options.size, comments=[scriptID + ' ' + ' '.join(sys.argv[1:])]) damask.util.croak(geom) -if name is None: - sys.stdout.write(str(geom.show())) -else: - geom.to_file(name) +geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_fromOsteonGeometry.py b/processing/pre/geom_fromOsteonGeometry.py index 146bf216c..499a8867f 100755 --- a/processing/pre/geom_fromOsteonGeometry.py +++ b/processing/pre/geom_fromOsteonGeometry.py @@ -145,7 +145,4 @@ geom = damask.Geom(microstructure.reshape(grid), homogenization=options.homogenization,comments=header) damask.util.croak(geom) -if name is None: - sys.stdout.write(str(geom.show())) -else: - geom.to_file(name) +geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_fromScratch.py b/processing/pre/geom_fromScratch.py index bfb294080..89fd27be5 100755 --- a/processing/pre/geom_fromScratch.py +++ b/processing/pre/geom_fromScratch.py @@ -63,7 +63,4 @@ geom = damask.Geom(microstructure=np.full(options.grid,options.fill,dtype=dtype) comments=scriptID + ' ' + ' '.join(sys.argv[1:])) damask.util.croak(geom) -if name is None: - sys.stdout.write(str(geom.show())) -else: - geom.to_file(name) +geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_fromTable.py b/processing/pre/geom_fromTable.py index aa37451c7..f513c4834 100755 --- a/processing/pre/geom_fromTable.py +++ b/processing/pre/geom_fromTable.py @@ -152,7 +152,4 @@ for name in filenames: homogenization=options.homogenization,comments=header) damask.util.croak(geom) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(os.path.splitext(name)[0]+'.geom') + geom.to_file(sys.stdout if name is None else os.path.splitext(name)[0]+'.geom',pack=False) diff --git a/processing/pre/geom_fromVoronoiTessellation.py b/processing/pre/geom_fromVoronoiTessellation.py index 9d4573c2c..28e215f85 100755 --- a/processing/pre/geom_fromVoronoiTessellation.py +++ b/processing/pre/geom_fromVoronoiTessellation.py @@ -302,7 +302,4 @@ for name in filenames: homogenization=options.homogenization,comments=header) damask.util.croak(geom) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(os.path.splitext(name)[0]+'.geom') + geom.to_file(sys.stdout if name is None else os.path.splitext(name)[0]+'.geom',pack=False) diff --git a/processing/pre/geom_grainGrowth.py b/processing/pre/geom_grainGrowth.py index b31fc13f2..bdf8d8efe 100755 --- a/processing/pre/geom_grainGrowth.py +++ b/processing/pre/geom_grainGrowth.py @@ -172,7 +172,4 @@ for name in filenames: damask.util.croak(geom.update(microstructure[0:grid_original[0],0:grid_original[1],0:grid_original[2]])) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_mirror.py b/processing/pre/geom_mirror.py index ff47cb88b..cca0a4e10 100755 --- a/processing/pre/geom_mirror.py +++ b/processing/pre/geom_mirror.py @@ -44,4 +44,4 @@ for name in filenames: geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) damask.util.croak(geom.mirror(options.directions,options.reflect)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_renumber.py b/processing/pre/geom_renumber.py index df817b1fc..1e28ca921 100755 --- a/processing/pre/geom_renumber.py +++ b/processing/pre/geom_renumber.py @@ -34,4 +34,4 @@ for name in filenames: geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) damask.util.croak(self.renumber) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_rescale.py b/processing/pre/geom_rescale.py index 8f5276b9d..55c2e57a2 100755 --- a/processing/pre/geom_rescale.py +++ b/processing/pre/geom_rescale.py @@ -58,4 +58,4 @@ for name in filenames: geom.scale(new_grid) damask.util.croak(geom.update(microstructure = None,size = new_size)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_rotate.py b/processing/pre/geom_rotate.py index 6cca99caf..977e00b65 100755 --- a/processing/pre/geom_rotate.py +++ b/processing/pre/geom_rotate.py @@ -95,4 +95,4 @@ for name in filenames: damask.util.croak(geom.update(microstructure,origin=origin-(np.asarray(microstructure.shape)-grid)/2*size/grid,rescale=True)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_translate.py b/processing/pre/geom_translate.py index 4b91920ae..2d4279821 100755 --- a/processing/pre/geom_translate.py +++ b/processing/pre/geom_translate.py @@ -58,7 +58,4 @@ for name in filenames: damask.util.croak(geom.update(substituted,origin=geom.get_origin()+options.origin)) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - if name is None: - sys.stdout.write(str(geom.show())) - else: - geom.to_file(name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_vicinityOffset.py b/processing/pre/geom_vicinityOffset.py index 0b86adfb9..e30779d31 100755 --- a/processing/pre/geom_vicinityOffset.py +++ b/processing/pre/geom_vicinityOffset.py @@ -82,4 +82,4 @@ for name in filenames: geom.microstructure + offset,geom.microstructure))) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) - geom.to_file(sys.stdout if name is None else name) + geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/python/damask/geom.py b/python/damask/geom.py index e544765a4..e2dcd9bcb 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -519,5 +519,5 @@ class Geom(): for i, oldID in enumerate(np.unique(self.microstructure)): renumbered = np.where(self.microstructure == oldID, i+1, renumbered) - return self.update(renumbered)) + return self.update(renumbered) #self.add_comments('tbd') From 3e8518d861e68146f31fecc9c13c90634125858d Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 24 Nov 2019 19:25:01 +0100 Subject: [PATCH 9/9] following prospector guidelines --- processing/pre/geom_fromDREAM3D.py | 12 ++++++------ processing/pre/geom_renumber.py | 4 +--- processing/pre/geom_rescale.py | 5 ++--- python/damask/geom.py | 2 +- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/processing/pre/geom_fromDREAM3D.py b/processing/pre/geom_fromDREAM3D.py index b20749b45..159793cd8 100755 --- a/processing/pre/geom_fromDREAM3D.py +++ b/processing/pre/geom_fromDREAM3D.py @@ -86,7 +86,7 @@ for name in filenames: * inFile[os.path.join(group_geom,'SPACING')][...] grid = inFile[os.path.join(group_geom,'DIMENSIONS')][...] origin = inFile[os.path.join(group_geom,'ORIGIN')][...] - except: + except KeyError: errors.append('Geometry data ({}) not found'.format(group_geom)) @@ -98,13 +98,13 @@ for name in filenames: try: quats = np.reshape(inFile[dataset][...],(np.product(grid),4)) rot = [damask.Rotation.fromQuaternion(q,True,P=+1) for q in quats] - except: + except KeyError: errors.append('Pointwise orientation (quaternion) data ({}) not readable'.format(dataset)) dataset = os.path.join(group_pointwise,options.phase) try: phase = np.reshape(inFile[dataset][...],(np.product(grid))) - except: + except KeyError: errors.append('Pointwise phase data ({}) not readable'.format(dataset)) microstructure = np.arange(1,np.product(grid)+1,dtype=int).reshape(grid,order='F') @@ -116,7 +116,7 @@ for name in filenames: dataset = os.path.join(group_pointwise,options.microstructure) try: microstructure = np.transpose(inFile[dataset][...].reshape(grid[::-1]),(2,1,0)) # convert from C ordering - except: + except KeyError: errors.append('Link between pointwise and grain average data ({}) not readable'.format(dataset)) group_average = os.path.join(rootDir,options.basegroup,options.average) @@ -124,13 +124,13 @@ for name in filenames: dataset = os.path.join(group_average,options.quaternion) try: rot = [damask.Rotation.fromQuaternion(q,True,P=+1) for q in inFile[dataset][...][1:]] # skip first entry (unindexed) - except: + except KeyError: errors.append('Average orientation data ({}) not readable'.format(dataset)) dataset = os.path.join(group_average,options.phase) try: phase = [i[0] for i in inFile[dataset][...]][1:] # skip first entry (unindexed) - except: + except KeyError: errors.append('Average phase data ({}) not readable'.format(dataset)) if errors != []: diff --git a/processing/pre/geom_renumber.py b/processing/pre/geom_renumber.py index 1e28ca921..b1db6ed13 100755 --- a/processing/pre/geom_renumber.py +++ b/processing/pre/geom_renumber.py @@ -5,8 +5,6 @@ import sys from io import StringIO from optparse import OptionParser -import numpy as np - import damask @@ -32,6 +30,6 @@ for name in filenames: damask.util.report(scriptName,name) geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name) - damask.util.croak(self.renumber) + damask.util.croak(geom.renumber) geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:])) geom.to_file(sys.stdout if name is None else name,pack=False) diff --git a/processing/pre/geom_rescale.py b/processing/pre/geom_rescale.py index 55c2e57a2..b1a15593c 100755 --- a/processing/pre/geom_rescale.py +++ b/processing/pre/geom_rescale.py @@ -2,11 +2,10 @@ import os import sys -import numpy as np - from io import StringIO from optparse import OptionParser -from scipy import ndimage + +import numpy as np import damask diff --git a/python/damask/geom.py b/python/damask/geom.py index e2dcd9bcb..69dfa9ec3 100644 --- a/python/damask/geom.py +++ b/python/damask/geom.py @@ -469,7 +469,7 @@ class Geom(): def scale(self,grid): """ - Scale microstructure to new grid + Scale microstructure to new grid. Parameters ----------