ditching the patched copy approach

Further details or things to look at:
2a48bf504e (note_12028)
This commit is contained in:
Vitesh Shah 2023-12-01 12:20:36 +01:00
parent 338fce8b5d
commit 9e51ab160c
1 changed files with 13 additions and 35 deletions

View File

@ -1,9 +1,6 @@
import re
import fnmatch
import os
import sys
import importlib
import importlib.util
import copy
import datetime
import xml.etree.ElementTree as ET # noqa
@ -28,27 +25,6 @@ from . import tensor
from . import util
from ._typehints import FloatSequence, IntSequence, DADF5Dataset
h5py_module_list = ['h5py','h5py._hl','h5py._hl.attrs']
module_dict = {}
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_HL = importlib.util.find_spec('h5py._hl')
h5py_hl_modified = importlib.util.module_from_spec(SPEC_H5PY_HL)
SPEC_H5PY_HL.loader.exec_module(h5py_hl_modified)
sys.modules['h5py_hl_modified'] = h5py_hl_modified
SPEC_H5PY_HL_attrs = importlib.util.find_spec('h5py._hl.attrs')
h5py_hl_attrs_modified = importlib.util.module_from_spec(SPEC_H5PY_HL_attrs)
SPEC_H5PY_HL_attrs.loader.exec_module(h5py_hl_attrs_modified)
sys.modules['h5py_hl_attrs_modified'] = h5py_hl_attrs_modified
h5py_hl_modified.__dict__['attrs'] = h5py_hl_attrs_modified
h5py_modified.__dict__['_hl'] = h5py_hl_modified
h5py3 = h5py.__version__[0] == '3'
chunk_size = 1024**2//8 # for compression in HDF5
@ -86,7 +62,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.
@ -107,7 +83,7 @@ class AttributeManagerNullterm(h5py_modified.AttributeManager):
else:
super().create(name=name,data=data,shape=shape,dtype=dtype)
class ResetAttributeManager(h5py_modified.AttributeManager):
class ResetAttributeManager(h5py.AttributeManager):
"""
Reset the attribute management for DREAM.3D hdf5 files.
@ -2007,13 +1983,14 @@ 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.')
N_digits = int(np.floor(np.log10(max(1,self.incs[-1]))))+1
Crystal_structure_types = {'Hexagonal': 0, 'Cubic': 1, 'Triclinic': 4, 'Monoclinic': 5, 'Orthorhombic': 6, 'Tetrogonal': 8}
Crystal_structure_types = {'Hexagonal': 0, 'Cubic': 1, 'Triclinic': 4, 'Monoclinic': 5, \
'Orthorhombic': 6, 'Tetrogonal': 8}
# crystal structure map according to Dream3D
Phase_types = {'Primary': 0}
@ -2048,7 +2025,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'
@ -2105,15 +2082,16 @@ 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(np.array(crystal_structure_list)).reshape((len(self.phases)+1,1))
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)
@ -2145,7 +2123,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_modified._hl.attrs.AttributeManager = ResetAttributeManager # Reset the attribute manager to original:
h5py._hl.attrs.AttributeManager = ResetAttributeManager # Reset the attribute manager to original:
def export_DADF5(self,