From 1cf8696cbb2ec39911d7465e24b79534299917f6 Mon Sep 17 00:00:00 2001 From: Vitesh Shah Date: Mon, 27 Nov 2023 15:51:02 +0100 Subject: [PATCH] correct crystal structure array shape --- python/damask/_result.py | 43 +++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/python/damask/_result.py b/python/damask/_result.py index 5b3c9101b..f413e4ae2 100644 --- a/python/damask/_result.py +++ b/python/damask/_result.py @@ -27,10 +27,10 @@ from . import tensor from . import util from ._typehints import FloatSequence, IntSequence, DADF5Dataset -SPEC_H5PY = importlib.util.find_spec('h5py') -h5py_modified = importlib.util.module_from_spec(SPEC_H5PY) -SPEC_H5PY.loader.exec_module(h5py_modified) -sys.modules['h5py_modified'] = h5py_modified +#SPEC_H5PY = importlib.util.find_spec('h5py') +#h5py_modified = importlib.util.module_from_spec(SPEC_H5PY) +#SPEC_H5PY.loader.exec_module(h5py_modified) +#sys.modules['h5py_modified'] = h5py_modified h5py3 = h5py.__version__[0] == '3' @@ -69,7 +69,7 @@ def _empty_like(dataset: np.ma.core.MaskedArray, fill_value = fill_float if dataset.dtype in np.sctypes['float'] else fill_int, mask = True) -class AttributeManagerNullterm(h5py_modified.AttributeManager): +class AttributeManagerNullterm(h5py.AttributeManager): """ Attribute management for DREAM.3D hdf5 files. @@ -84,12 +84,26 @@ class AttributeManagerNullterm(h5py_modified.AttributeManager): def create(self, name, data, shape=None, dtype=None): if isinstance(data,str): - tid = h5py_modified.h5t.C_S1.copy() + tid = h5py.h5t.C_S1.copy() tid.set_size(len(data + ' ')) - super().create(name=name,data=data+' ',dtype = h5py_modified.Datatype(tid)) + super().create(name=name,data=data+' ',dtype = h5py.Datatype(tid)) else: super().create(name=name,data=data,shape=shape,dtype=dtype) +class ResetAttributeManager(h5py.AttributeManager): + """ + Reset the attribute management for DREAM.3D hdf5 files. + + References + ---------- + https://stackoverflow.com/questions/38267076 + https://stackoverflow.com/questions/52750232 + + """ + + def create(self, name, data, shape=None, dtype=None): + super().create(name=name,data=data,shape=shape,dtype=dtype) + class Result: """ @@ -1976,7 +1990,7 @@ class Result: Directory to save DREAM3D files. Will be created if non-existent. """ - h5py_modified._hl.attrs.AttributeManager = AttributeManagerNullterm # 'Monkey patch' + h5py._hl.attrs.AttributeManager = AttributeManagerNullterm # 'Monkey patch' if self.N_constituents != 1 or not self.structured: raise TypeError('DREAM3D output requires structured grid with single constituent.') @@ -2017,7 +2031,7 @@ class Result: phase_ID_array[at_cell_ph[c][label]] = count + 1 job_file_no_ext = self.fname.stem - o = h5py_modified.File(f'{dream_dir}/{job_file_no_ext}_inc{inc.split(prefix_inc)[-1].zfill(N_digits)}.dream3d','w') + o = h5py.File(f'{dream_dir}/{job_file_no_ext}_inc{inc.split(prefix_inc)[-1].zfill(N_digits)}.dream3d','w') o.attrs['DADF5toDREAM3D'] = '1.0' o.attrs['FileVersion'] = '7.0' @@ -2074,15 +2088,15 @@ class Result: elif lattice_dict[label] in ['tP','tI']: crystal_structure = 'tetragonal' crystal_structure_list.append(Crystal_structure_types[crystal_structure]) - o[ensemble_label + '/CrystalStructures'] = np.uint32(crystal_structure_list) + o[ensemble_label + '/CrystalStructures'] = np.uint32(np.array(crystal_structure_list)).reshape((len(self.phases)+1,1)) o[ensemble_label + '/PhaseTypes'] = np.uint32(np.array([999] + [Phase_types['Primary']]*len(self.phases)))\ .reshape((len(self.phases)+1,1)) phase_name_list = ['Unknown Phase Type'] phase_name_list.extend(i for i in self.visible['phases']) - tid = h5py_modified.h5t.C_S1.copy() - tid.set_size(h5py_modified.h5t.VARIABLE) - tid.set_cset(h5py_modified.h5t.CSET_ASCII) - o[ensemble_label].create_dataset(name='PhaseName',data = phase_name_list, dtype=h5py_modified.Datatype(tid)) + tid = h5py.h5t.C_S1.copy() + tid.set_size(h5py.h5t.VARIABLE) + tid.set_cset(h5py.h5t.CSET_ASCII) + o[ensemble_label].create_dataset(name='PhaseName',data = phase_name_list, dtype=h5py.Datatype(tid)) # Attributes Ensemble Matrix o[ensemble_label].attrs['AttributeMatrixType'] = np.array([11],np.uint32) @@ -2114,6 +2128,7 @@ class Result: o[geom_label].attrs['GeometryType'] = np.array([0],np.uint32) o[geom_label].attrs['SpatialDimensionality'] = np.array([3],np.uint32) o[geom_label].attrs['UnitDimensionality'] = np.array([3],np.uint32) + h5py._hl.attrs.AttributeManager = ResetAttributeManager # Reset the attribute manager to original: def export_DADF5(self,