correct crystal structure array shape

This commit is contained in:
Vitesh Shah 2023-11-27 15:51:02 +01:00
parent 986d6a5973
commit 1cf8696cbb
1 changed files with 29 additions and 14 deletions

View File

@ -27,10 +27,10 @@ from . import tensor
from . import util from . import util
from ._typehints import FloatSequence, IntSequence, DADF5Dataset from ._typehints import FloatSequence, IntSequence, DADF5Dataset
SPEC_H5PY = importlib.util.find_spec('h5py') #SPEC_H5PY = importlib.util.find_spec('h5py')
h5py_modified = importlib.util.module_from_spec(SPEC_H5PY) #h5py_modified = importlib.util.module_from_spec(SPEC_H5PY)
SPEC_H5PY.loader.exec_module(h5py_modified) #SPEC_H5PY.loader.exec_module(h5py_modified)
sys.modules['h5py_modified'] = h5py_modified #sys.modules['h5py_modified'] = h5py_modified
h5py3 = h5py.__version__[0] == '3' 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, fill_value = fill_float if dataset.dtype in np.sctypes['float'] else fill_int,
mask = True) mask = True)
class AttributeManagerNullterm(h5py_modified.AttributeManager): class AttributeManagerNullterm(h5py.AttributeManager):
""" """
Attribute management for DREAM.3D hdf5 files. 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): def create(self, name, data, shape=None, dtype=None):
if isinstance(data,str): if isinstance(data,str):
tid = h5py_modified.h5t.C_S1.copy() tid = h5py.h5t.C_S1.copy()
tid.set_size(len(data + ' ')) 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: else:
super().create(name=name,data=data,shape=shape,dtype=dtype) 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: class Result:
""" """
@ -1976,7 +1990,7 @@ class Result:
Directory to save DREAM3D files. Will be created if non-existent. 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: if self.N_constituents != 1 or not self.structured:
raise TypeError('DREAM3D output requires structured grid with single constituent.') 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 phase_ID_array[at_cell_ph[c][label]] = count + 1
job_file_no_ext = self.fname.stem 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['DADF5toDREAM3D'] = '1.0'
o.attrs['FileVersion'] = '7.0' o.attrs['FileVersion'] = '7.0'
@ -2074,15 +2088,15 @@ class Result:
elif lattice_dict[label] in ['tP','tI']: elif lattice_dict[label] in ['tP','tI']:
crystal_structure = 'tetragonal' crystal_structure = 'tetragonal'
crystal_structure_list.append(Crystal_structure_types[crystal_structure]) 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)))\ o[ensemble_label + '/PhaseTypes'] = np.uint32(np.array([999] + [Phase_types['Primary']]*len(self.phases)))\
.reshape((len(self.phases)+1,1)) .reshape((len(self.phases)+1,1))
phase_name_list = ['Unknown Phase Type'] phase_name_list = ['Unknown Phase Type']
phase_name_list.extend(i for i in self.visible['phases']) phase_name_list.extend(i for i in self.visible['phases'])
tid = h5py_modified.h5t.C_S1.copy() tid = h5py.h5t.C_S1.copy()
tid.set_size(h5py_modified.h5t.VARIABLE) tid.set_size(h5py.h5t.VARIABLE)
tid.set_cset(h5py_modified.h5t.CSET_ASCII) tid.set_cset(h5py.h5t.CSET_ASCII)
o[ensemble_label].create_dataset(name='PhaseName',data = phase_name_list, dtype=h5py_modified.Datatype(tid)) o[ensemble_label].create_dataset(name='PhaseName',data = phase_name_list, dtype=h5py.Datatype(tid))
# Attributes Ensemble Matrix # Attributes Ensemble Matrix
o[ensemble_label].attrs['AttributeMatrixType'] = np.array([11],np.uint32) 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['GeometryType'] = np.array([0],np.uint32)
o[geom_label].attrs['SpatialDimensionality'] = np.array([3],np.uint32) o[geom_label].attrs['SpatialDimensionality'] = np.array([3],np.uint32)
o[geom_label].attrs['UnitDimensionality'] = 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, def export_DADF5(self,