Skip to content

Commit

Permalink
Merge pull request #1469 from pyiron/h5fixes
Browse files Browse the repository at this point in the history
HDF5 interface: Convert h5io datatypes to nodes
  • Loading branch information
jan-janssen authored Jun 6, 2024
2 parents 0d0ad64 + df22fae commit 111dc7d
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions pyiron_base/storage/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
import posixpath


# DataTypes implemented by h5io which are not supported by h5py are stored as HDF5 groups rather than HDF5 nodes.
# We thread these special HDF5 groups as HDF5 nodes unless they are of type list, dict, tuple or custom classes
# stored with __set_state__()/__reduce__().
h5io_group_types = (
"csc_matrix",
"csr_matrix",
"csc_array",
"csr_array",
"multiarray",
)


def list_groups_and_nodes(hdf, h5_path):
"""
Get the list of groups and list of nodes from an open HDF5 file
Expand All @@ -20,7 +32,14 @@ def list_groups_and_nodes(hdf, h5_path):
h = hdf[h5_path]
for k in h.keys():
if isinstance(h[k], h5py.Group):
groups.add(k)
group_attrs_dict = h[k].attrs
if (
"TITLE" in group_attrs_dict.keys()
and group_attrs_dict["TITLE"] in h5io_group_types
):
nodes.add(k)
else:
groups.add(k)
else:
nodes.add(k)
except KeyError:
Expand Down Expand Up @@ -97,7 +116,14 @@ def get_groups_hdf(hdf, h5_path):
h = hdf[h5_path]
group_lst = []
for group in [h[k].name for k in h.keys() if isinstance(h[k], h5py.Group)]:
group_lst += [group] + get_groups_hdf(hdf=hdf, h5_path=group)
group_attrs_dict = h[group].attrs
if (
"TITLE" in group_attrs_dict.keys()
and group_attrs_dict["TITLE"] not in h5io_group_types
):
group_lst += [group] + get_groups_hdf(hdf=hdf, h5_path=group)
elif "TITLE" not in group_attrs_dict.keys():
group_lst += [group] + get_groups_hdf(hdf=hdf, h5_path=group)
return group_lst
except KeyError:
return []
Expand Down

0 comments on commit 111dc7d

Please sign in to comment.