Skip to content

Commit

Permalink
Update the h5io interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-janssen committed Jun 6, 2024
1 parent e256f1f commit 1647d60
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 199 deletions.
2 changes: 1 addition & 1 deletion .ci_support/environment-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies:
- myst-parser
- cloudpickle =3.0.0
- gitpython =3.1.43
- h5io_browser =0.0.12
- h5io_browser =0.0.14
- h5py =3.11.0
- jinja2 =3.1.4
- monty =2024.5.24
Expand Down
2 changes: 1 addition & 1 deletion .ci_support/environment-old.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ channels:
dependencies:
- cloudpickle =2.0.0
- gitpython =3.1.23
- h5io_browser =0.0.6
- h5io_browser =0.0.14
- h5py =3.6.0
- jinja2 =2.11.3
- monty =2021.3.3
Expand Down
2 changes: 1 addition & 1 deletion .ci_support/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies:
- conda_subprocess =0.0.1
- cloudpickle =3.0.0
- gitpython =3.1.43
- h5io_browser =0.0.12
- h5io_browser =0.0.14
- h5py =3.11.0
- jinja2 =3.1.4
- monty =2024.5.24
Expand Down
37 changes: 27 additions & 10 deletions pyiron_base/storage/hdfio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

import numbers
from h5io_browser import Pointer
from h5io_browser import Pointer, list_hdf, read_nested_dict_from_hdf
from h5io_browser.base import (
_open_hdf,
_is_ragged_in_1st_dim_only,
Expand All @@ -22,11 +22,6 @@
import sys
from typing import Union, Optional, Any, Tuple

from pyiron_base.storage.helper_functions import (
get_h5_path,
list_groups_and_nodes,
read_dict_from_hdf,
)
from pyiron_base.interfaces.has_groups import HasGroups
from pyiron_base.state import state
from pyiron_base.jobs.job.util import _get_safe_job_name
Expand Down Expand Up @@ -54,6 +49,26 @@ def _extract_module_class_name(type_field: str) -> Tuple[str, str]:
return fully_qualified_path.rsplit(".", maxsplit=1)


def _list_groups_and_nodes(file_name, h5_path):
"""
Get the list of groups and list of nodes from an open HDF5 file
Args:
file_name (str): file name of the HDF5 file
h5_path (str): path inside the HDF5 file
Returns:
list, list: list of groups and list of nodes
"""
if h5_path[0] != "/":
h5_path = "/" + h5_path
nodes, groups = list_hdf(file_name=file_name, h5_path=h5_path, recursive=False)
return (
[group[len(h5_path) + 1 :] for group in groups],
[node[len(h5_path) + 1 :] for node in nodes],
)


def _import_class(module_path, class_name):
"""
Import given class from fully qualified name and return class object.
Expand Down Expand Up @@ -519,8 +534,10 @@ def _list_all(self):
dict: {'groups': [list of groups], 'nodes': [list of nodes]}
"""
if self.file_exists:
with _open_hdf(self.file_name) as hdf:
groups, nodes = list_groups_and_nodes(hdf=hdf, h5_path=self.h5_path)
nodes, groups = _list_groups_and_nodes(
file_name=self.file_name,
h5_path=self.h5_path,
)
iopy_nodes = self._filter_io_objects(set(groups))
return {
"groups": sorted(list(set(groups) - iopy_nodes)),
Expand Down Expand Up @@ -748,7 +765,7 @@ def read_dict_from_hdf(self, group_paths=[], recursive=False):
Returns:
dict: The loaded data. Can be of any type supported by ``write_hdf5``.
"""
return read_dict_from_hdf(
return read_nested_dict_from_hdf(
file_name=self.file_name,
h5_path=self.h5_path,
group_paths=group_paths,
Expand Down Expand Up @@ -777,7 +794,7 @@ def _get_h5_path(self, name):
Returns:
str: combined path
"""
return get_h5_path(h5_path=self.h5_path, name=name)
return posixpath.join(self.h5_path, name)

def _get_h5io_type(self, name):
"""
Expand Down
172 changes: 0 additions & 172 deletions pyiron_base/storage/helper_functions.py

This file was deleted.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ classifiers = [
dependencies = [
"cloudpickle==3.0.0",
"gitpython==3.1.43",
"h5io_browser==0.0.12",
"h5io_browser==0.0.14",
"h5py==3.11.0",
"jinja2==3.1.4",
"numpy==1.26.4",
Expand Down
25 changes: 12 additions & 13 deletions tests/storage/test_helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
import posixpath
import h5py
from unittest import TestCase
from pyiron_base.storage.helper_functions import (
list_groups_and_nodes,
read_dict_from_hdf,
)
from pyiron_base.storage.hdfio import _list_groups_and_nodes
from h5io_browser import read_nested_dict_from_hdf
from h5io_browser.base import write_dict_to_hdf, _read_hdf, _write_hdf


Expand Down Expand Up @@ -44,11 +42,11 @@ def test_read_hierarchical(self):
def test_read_dict_hierarchical(self):
self.assertEqual(
{"key_b": 3},
read_dict_from_hdf(file_name=self.file_name, h5_path=self.h5_path),
read_nested_dict_from_hdf(file_name=self.file_name, h5_path=self.h5_path),
)
self.assertEqual(
{"key_a": {"idx_0": 1, "idx_1": 2}, "key_b": 3, "key_c": {"key_d": 4}},
read_dict_from_hdf(
read_nested_dict_from_hdf(
file_name=self.file_name,
h5_path=self.h5_path,
group_paths=["key_a", "key_c"],
Expand All @@ -60,7 +58,7 @@ def test_read_dict_hierarchical(self):
"key_b": 3,
"key_c": {"key_d": 4, "key_e": {"key_f": 5}},
},
read_dict_from_hdf(
read_nested_dict_from_hdf(
file_name=self.file_name,
h5_path=self.h5_path,
group_paths=["key_a", "key_c", "key_c/key_e"],
Expand All @@ -72,7 +70,7 @@ def test_read_dict_hierarchical(self):
"key_b": 3,
"key_c": {"key_d": 4, "key_e": {"key_f": 5}},
},
read_dict_from_hdf(
read_nested_dict_from_hdf(
file_name=self.file_name,
h5_path=self.h5_path,
recursive=True,
Expand Down Expand Up @@ -105,8 +103,7 @@ def test_hdf5_structure(self):
)

def test_list_groups(self):
with h5py.File(self.file_name, "r") as f:
groups, nodes = list_groups_and_nodes(hdf=f, h5_path="data_hierarchical")
groups, nodes = _list_groups_and_nodes(file_name=self.file_name, h5_path="data_hierarchical")
self.assertEqual(list(sorted(groups)), ["key_a", "key_c"])
self.assertEqual(nodes, ["key_b"])

Expand Down Expand Up @@ -134,7 +131,7 @@ def test_read_hierarchical(self):
def test_read_dict_hierarchical(self):
self.assertEqual(
self.data_hierarchical,
read_dict_from_hdf(file_name=self.file_name, h5_path=self.h5_path),
read_nested_dict_from_hdf(file_name=self.file_name, h5_path=self.h5_path),
)

def test_hdf5_structure(self):
Expand All @@ -149,7 +146,9 @@ def test_hdf5_structure(self):
)

def test_list_groups(self):
with h5py.File(self.file_name, "r") as f:
groups, nodes = list_groups_and_nodes(hdf=f, h5_path="data_hierarchical")
groups, nodes = _list_groups_and_nodes(
file_name=self.file_name,
h5_path="data_hierarchical",
)
self.assertEqual(groups, [])
self.assertEqual(list(sorted(nodes)), ["a", "b", "c"])

0 comments on commit 1647d60

Please sign in to comment.