2016-10-07 19:17:15 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-10-10 02:43:40 +05:30
|
|
|
# ----------------------------------------------------------- #
|
|
|
|
# Ideally the h5py should be enough to serve as the data #
|
|
|
|
# interface for future DAMASK, but since we are still not #
|
|
|
|
# sure when this major shift will happen, it seems to be a #
|
|
|
|
# good idea to provide a interface class that help user ease #
|
|
|
|
# into using HDF5 as the new daily storage driver. #
|
|
|
|
# ----------------------------------------------------------- #
|
|
|
|
|
2016-10-07 19:17:15 +05:30
|
|
|
import os
|
|
|
|
import h5py
|
|
|
|
import numpy as np
|
2016-10-07 20:16:07 +05:30
|
|
|
import xml.etree.cElementTree as ET
|
2016-10-07 19:17:15 +05:30
|
|
|
|
|
|
|
# ---------------------------------------------------------------- #
|
|
|
|
# python 3 has no unicode object, this ensures that the code works #
|
|
|
|
# on Python 2&3 #
|
|
|
|
# ---------------------------------------------------------------- #
|
|
|
|
try:
|
2016-10-14 20:11:35 +05:30
|
|
|
test = isinstance('test', unicode)
|
2016-10-07 19:17:15 +05:30
|
|
|
except(NameError):
|
2016-10-14 20:11:35 +05:30
|
|
|
unicode = str
|
2016-10-07 19:17:15 +05:30
|
|
|
|
|
|
|
|
2016-10-08 01:03:43 +05:30
|
|
|
def lables_to_path(label, dsXMLPath=None):
|
2016-10-10 04:36:24 +05:30
|
|
|
"""read the xml definition file and return the path."""
|
2016-10-08 01:03:43 +05:30
|
|
|
if dsXMLPath is None:
|
2016-10-10 02:43:40 +05:30
|
|
|
# use the default storage layout in DS_HDF5.xml
|
2016-10-10 04:29:50 +05:30
|
|
|
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",
|
|
|
|
"DS_HDF5.xml")
|
2016-10-10 02:43:40 +05:30
|
|
|
# This current implementation requires that all variables
|
|
|
|
# stay under the root node, the nesting is defined through the
|
2016-10-13 05:50:15 +05:30
|
|
|
# h5path.
|
|
|
|
# Allow new derived data to be put under the root
|
2016-10-07 20:16:07 +05:30
|
|
|
tree = ET.parse(dsXMLPath)
|
2016-10-13 05:50:15 +05:30
|
|
|
try:
|
|
|
|
dataType = tree.find('{}/type'.format(label)).text
|
|
|
|
h5path = tree.find('{}/h5path'.format(label)).text
|
|
|
|
except:
|
|
|
|
dataType = "Scalar"
|
|
|
|
h5path = "/{}".format(label) # just put it under root
|
2016-10-08 01:03:43 +05:30
|
|
|
return (dataType, h5path)
|
2016-10-07 19:17:15 +05:30
|
|
|
|
|
|
|
|
|
|
|
class H5Table(object):
|
2016-10-13 05:50:15 +05:30
|
|
|
"""light weight interface class for h5py
|
2016-10-10 04:37:41 +05:30
|
|
|
|
2016-10-07 19:17:15 +05:30
|
|
|
DESCRIPTION
|
|
|
|
-----------
|
2016-10-10 02:49:49 +05:30
|
|
|
Interface/wrapper class for manipulating data in HDF5 with DAMASK
|
2016-10-08 01:03:43 +05:30
|
|
|
specialized data structure.
|
2016-10-13 05:50:15 +05:30
|
|
|
-->try to maintain a minimal API design.
|
2016-10-07 19:17:15 +05:30
|
|
|
PARAMETERS
|
|
|
|
----------
|
2016-10-08 01:03:43 +05:30
|
|
|
h5f_path: str
|
|
|
|
Absolute path the HDF5 file
|
2016-10-07 19:17:15 +05:30
|
|
|
METHOD
|
|
|
|
------
|
2016-10-10 02:49:49 +05:30
|
|
|
del_entry() -- Force delete attributes/group/datasets (Dangerous)
|
|
|
|
get_attr() -- Return attributes if possible
|
2016-10-14 21:35:36 +05:30
|
|
|
add_attr() -- Add NEW attributes to dataset/group (no force overwrite)
|
2016-10-10 02:49:49 +05:30
|
|
|
get_data() -- Retrieve data in numpy.ndarray
|
|
|
|
add_data() -- Add dataset to H5 file
|
|
|
|
get_cmdlog() -- Return the command used to generate the data if possible.
|
2016-10-07 19:17:15 +05:30
|
|
|
NOTE
|
|
|
|
----
|
|
|
|
1. As an interface class, it uses the lazy evaluation design
|
|
|
|
that read the data only when its absolutely necessary.
|
2016-10-08 03:13:52 +05:30
|
|
|
2. The command line used to generate new feature is stored with
|
|
|
|
each dataset as dataset attribute.
|
2016-10-10 04:39:45 +05:30
|
|
|
|
2016-10-07 19:17:15 +05:30
|
|
|
"""
|
|
|
|
|
2016-10-10 04:29:50 +05:30
|
|
|
def __init__(self, h5f_path, new_file=False, dsXMLFile=None):
|
2016-10-07 19:17:15 +05:30
|
|
|
self.h5f_path = h5f_path
|
2016-10-10 04:29:50 +05:30
|
|
|
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
|
2016-10-07 19:17:15 +05:30
|
|
|
|
2016-10-08 03:13:52 +05:30
|
|
|
def del_entry(self, feature_name):
|
2016-10-10 04:36:24 +05:30
|
|
|
"""delete entry in HDF5 table"""
|
2016-10-10 04:29:50 +05:30
|
|
|
dataType, h5f_path = lables_to_path(feature_name,
|
|
|
|
dsXMLPath=self.dsXMLFile)
|
|
|
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
|
|
|
del h5f[h5f_path]
|
2016-10-08 02:11:17 +05:30
|
|
|
|
2016-10-08 03:13:52 +05:30
|
|
|
def get_attr(self, attr_name):
|
2016-10-10 04:29:50 +05:30
|
|
|
dataType, h5f_path = lables_to_path(attr_name,
|
|
|
|
dsXMLPath=self.dsXMLFile)
|
|
|
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
|
|
|
rst_attr = h5f[h5f_path].attrs[attr_name]
|
|
|
|
return rst_attr
|
2016-10-07 19:17:15 +05:30
|
|
|
|
2016-10-08 03:13:52 +05:30
|
|
|
def add_attr(self, attr_name, attr_data):
|
2016-10-10 04:29:50 +05:30
|
|
|
dataType, h5f_path = lables_to_path(attr_name,
|
|
|
|
dsXMLPath=self.dsXMLFile)
|
|
|
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
2016-10-08 03:13:52 +05:30
|
|
|
h5f[h5f_path].attrs[attr_name] = attr_data
|
2016-10-10 04:29:50 +05:30
|
|
|
h5f.flush()
|
2016-10-07 19:17:15 +05:30
|
|
|
|
2016-10-08 01:03:43 +05:30
|
|
|
def get_data(self, feature_name=None):
|
2016-10-10 04:36:24 +05:30
|
|
|
"""extract dataset from HDF5 table and return it in a numpy array"""
|
2016-10-10 04:29:50 +05:30
|
|
|
dataType, h5f_path = lables_to_path(feature_name,
|
|
|
|
dsXMLPath=self.dsXMLFile)
|
|
|
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
|
|
|
h5f_dst = h5f[h5f_path] # get the handle for target dataset(table)
|
2016-10-13 05:50:15 +05:30
|
|
|
rst_data = np.zeros(h5f_dst.shape)
|
|
|
|
h5f_dst.read_direct(rst_data)
|
2016-10-10 04:29:50 +05:30
|
|
|
return rst_data
|
2016-10-07 19:17:15 +05:30
|
|
|
|
2016-10-10 04:29:50 +05:30
|
|
|
def add_data(self, feature_name, dataset, cmd_log=None):
|
2016-10-10 04:36:24 +05:30
|
|
|
"""adding new feature into existing HDF5 file"""
|
2016-10-10 04:29:50 +05:30
|
|
|
dataType, h5f_path = lables_to_path(feature_name,
|
|
|
|
dsXMLPath=self.dsXMLFile)
|
|
|
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
2016-10-15 00:00:04 +05:30
|
|
|
# NOTE:
|
|
|
|
# --> If dataset exists, delete the old one so as to write
|
|
|
|
# a new one. For brand new dataset. For brand new one,
|
|
|
|
# record its state as fresh in the cmd log.
|
|
|
|
try:
|
|
|
|
del h5f[h5f_path]
|
|
|
|
print "***deleting old {} from {}".format(feature_name,
|
|
|
|
self.h5f_path)
|
|
|
|
except:
|
2016-10-18 03:54:26 +05:30
|
|
|
# if no cmd log, None will used
|
|
|
|
cmd_log = str(cmd_log) + " [FRESH]"
|
2016-10-08 02:11:17 +05:30
|
|
|
h5f.create_dataset(h5f_path, data=dataset)
|
2016-10-10 02:43:40 +05:30
|
|
|
# store the cmd in log is possible
|
|
|
|
if cmd_log is not None:
|
|
|
|
h5f[h5f_path].attrs['log'] = str(cmd_log)
|
2016-10-10 04:29:50 +05:30
|
|
|
h5f.flush()
|
2016-10-08 03:13:52 +05:30
|
|
|
|
2016-10-10 02:43:40 +05:30
|
|
|
def get_cmdlog(self, feature_name):
|
2016-10-10 04:36:24 +05:30
|
|
|
"""get cmd history used to generate the feature"""
|
2016-10-10 04:29:50 +05:30
|
|
|
dataType, h5f_path = lables_to_path(feature_name,
|
|
|
|
dsXMLPath=self.dsXMLFile)
|
2016-10-10 04:37:41 +05:30
|
|
|
with h5py.File(self.h5f_path, 'a') as h5f:
|
2016-10-10 04:29:50 +05:30
|
|
|
cmd_logs = h5f[h5f_path].attrs['log']
|
|
|
|
return cmd_logs
|