H5Table support customize data storage layout
This commit is contained in:
parent
753bdfd5a9
commit
912aa6989c
|
@ -34,6 +34,10 @@ def lables_to_path(label, dsXMLPath=None):
|
||||||
""" read the xml definition file and return the path."""
|
""" read the xml definition file and return the path."""
|
||||||
if dsXMLPath is None:
|
if dsXMLPath is None:
|
||||||
# use the default storage layout in DS_HDF5.xml
|
# use the default storage layout in DS_HDF5.xml
|
||||||
|
if "h5table.pyc" in __file__:
|
||||||
|
dsXMLPath = os.path.abspath(__file__).replace("h5table.pyc",
|
||||||
|
"DS_HDF5.xml")
|
||||||
|
else:
|
||||||
dsXMLPath = os.path.abspath(__file__).replace("h5table.py",
|
dsXMLPath = os.path.abspath(__file__).replace("h5table.py",
|
||||||
"DS_HDF5.xml")
|
"DS_HDF5.xml")
|
||||||
# This current implementation requires that all variables
|
# This current implementation requires that all variables
|
||||||
|
@ -77,53 +81,59 @@ class H5Table(object):
|
||||||
each dataset as dataset attribute.
|
each dataset as dataset attribute.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, h5f_path):
|
def __init__(self, h5f_path, new_file=False, dsXMLFile=None):
|
||||||
"""
|
|
||||||
"""
|
|
||||||
self.h5f_path = h5f_path
|
self.h5f_path = h5f_path
|
||||||
|
self.dsXMLFile = dsXMLFile
|
||||||
|
msg = 'Created by H5Talbe from DAMASK'
|
||||||
|
mode = 'w' if new_file else 'a'
|
||||||
|
with h5py.File(self.h5f_path, mode) as h5f:
|
||||||
|
h5f['/'].attrs['description'] = msg
|
||||||
|
|
||||||
def del_entry(self, feature_name):
|
def del_entry(self, feature_name):
|
||||||
""" delete entry in HDF5 table """
|
""" delete entry in HDF5 table """
|
||||||
# WARNING: this will PERMENANTLY delete attributes/dataset
|
dataType, h5f_path = lables_to_path(feature_name,
|
||||||
# use with caution
|
dsXMLPath=self.dsXMLFile)
|
||||||
dataType, h5f_path = lables_to_path(feature_name)
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
||||||
h5f = h5py.File(self.h5f_path, 'a')
|
|
||||||
del h5f[h5f_path]
|
del h5f[h5f_path]
|
||||||
|
|
||||||
def get_attr(self, attr_name):
|
def get_attr(self, attr_name):
|
||||||
h5f = h5py.File(self.h5f_path, 'r')
|
dataType, h5f_path = lables_to_path(attr_name,
|
||||||
dataType, h5f_path = lables_to_path(attr_name)
|
dsXMLPath=self.dsXMLFile)
|
||||||
return h5f[h5f_path].attrs[attr_name]
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
||||||
|
rst_attr = h5f[h5f_path].attrs[attr_name]
|
||||||
|
return rst_attr
|
||||||
|
|
||||||
def add_attr(self, attr_name, attr_data):
|
def add_attr(self, attr_name, attr_data):
|
||||||
h5f = h5py.File(self.h5f_path, 'a')
|
dataType, h5f_path = lables_to_path(attr_name,
|
||||||
dataType, h5f_path = lables_to_path(attr_name)
|
dsXMLPath=self.dsXMLFile)
|
||||||
if dataType == "attr":
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
||||||
h5f[h5f_path].attrs[attr_name] = attr_data
|
h5f[h5f_path].attrs[attr_name] = attr_data
|
||||||
else:
|
h5f.flush()
|
||||||
raise ValueError("Unspported attr: {}".format(attr_name))
|
|
||||||
|
|
||||||
def get_data(self, feature_name=None):
|
def get_data(self, feature_name=None):
|
||||||
""" extract dataset from HDF5 table and return it in a numpy array """
|
""" extract dataset from HDF5 table and return it in a numpy array """
|
||||||
dataType, h5f_path = lables_to_path(feature_name)
|
dataType, h5f_path = lables_to_path(feature_name,
|
||||||
h5f = h5py.File(self.h5f_path, 'r')
|
dsXMLPath=self.dsXMLFile)
|
||||||
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
||||||
h5f_dst = h5f[h5f_path] # get the handle for target dataset(table)
|
h5f_dst = h5f[h5f_path] # get the handle for target dataset(table)
|
||||||
return h5f_dst.read_direct(np.zeros(h5f_dst.shape))
|
rst_data = h5f_dst.read_direct(np.zeros(h5f_dst.shape))
|
||||||
|
return rst_data
|
||||||
|
|
||||||
def add_data(self, feature_name, dataset=None, cmd_log=None):
|
def add_data(self, feature_name, dataset, cmd_log=None):
|
||||||
""" adding new feature into existing HDF5 file """
|
""" adding new feature into existing HDF5 file """
|
||||||
dataType, h5f_path = lables_to_path(feature_name)
|
dataType, h5f_path = lables_to_path(feature_name,
|
||||||
if dataType is not "attr":
|
dsXMLPath=self.dsXMLFile)
|
||||||
h5f = h5py.File(self.h5f_path, 'a')
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
||||||
h5f.create_dataset(h5f_path, data=dataset)
|
h5f.create_dataset(h5f_path, data=dataset)
|
||||||
# store the cmd in log is possible
|
# store the cmd in log is possible
|
||||||
if cmd_log is not None:
|
if cmd_log is not None:
|
||||||
h5f[h5f_path].attrs['log'] = str(cmd_log)
|
h5f[h5f_path].attrs['log'] = str(cmd_log)
|
||||||
else:
|
h5f.flush()
|
||||||
raise ValueError("feature {} isn't valid".format(feature_name))
|
|
||||||
|
|
||||||
def get_cmdlog(self, feature_name):
|
def get_cmdlog(self, feature_name):
|
||||||
""" get cmd history used to generate the feature"""
|
""" get cmd history used to generate the feature"""
|
||||||
dataType, h5f_path = lables_to_path(feature_name)
|
dataType, h5f_path = lables_to_path(feature_name,
|
||||||
h5f = ht5py.File(self.h5f_path, 'r')
|
dsXMLPath=self.dsXMLFile)
|
||||||
return h5f[h5f_path].attrs['log']
|
with ht5py.File(self.h5f_path, 'a') as h5f:
|
||||||
|
cmd_logs = h5f[h5f_path].attrs['log']
|
||||||
|
return cmd_logs
|
||||||
|
|
Loading…
Reference in New Issue