Creating HDF files

Creating HDF files#

To work with HDF5 files, the “wrapper”-class File is used (equivalent to h5py.File).

import h5rdmtoolbox as h5tbx
h5tbx.use(None)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import h5rdmtoolbox as h5tbx
      2 h5tbx.use(None)

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/checkouts/v1.7.0/h5rdmtoolbox/__init__.py:129
    125     with File(src) as h5:
    126         return h5.dumps()
--> 129 from h5rdmtoolbox.wrapper.ld.hdf.file import get_ld as hdf_get_ld
    130 from h5rdmtoolbox.wrapper.ld.user.file import get_ld as user_get_ld
    133 def get_ld(
    134         hdf_filename: Union[str, pathlib.Path],
    135         structural: bool = True,
    136         semantic: bool = True,
    137         blank_node_iri_base: Optional[str] = None,
    138         **kwargs) -> rdflib.Graph:

File ~/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/checkouts/v1.7.0/h5rdmtoolbox/wrapper/ld/__init__.py:1
----> 1 import ssnolib.ssno.standard_name
      2 from ontolutils.namespacelib import M4I
      3 from ontolutils.namespacelib import SCHEMA

ModuleNotFoundError: No module named 'ssnolib'

Creating a file does not require filename. If not specified, a temporary file is created. In this case the file-mode is r+, otherwise and if not specified, the default opening mode is r (read-only):

# init file object with a temporary file:
h5 = h5tbx.File()

# create a dataset
h5.create_dataset('velocity', data=1.3)

# close the file object
h5.close()

# print the filename
print(h5.hdf_filename.name)
tmp0.hdf

Note, that the h5rdmtoolbox introduces hdf_filename to the File-object. This allows you to access the filename even if the file has been closed already. This is not possible with h5py:

try:
    print(h5.filename)
except ValueError as e:
    print(e)
print(h5.hdf_filename.name)
Not a file or file object (not a file or file object)
tmp0.hdf

A safer way to work with files is to use python’s context manager. This is highly recommended and used throughout the hole documentation and package.

Thus, the above cell changes to:

with h5tbx.File('test.hdf', 'w') as h5:
    print(h5.hdf_filename.name)
test.hdf

Note: To inspect the file content, please visit the file content visualization notebook

As stated above, the hdf_filename is a pathlib.Path object, which gives much more possibilities to work with the file, among many, deleting the file is very simple:

h5.hdf_filename.unlink()