Manipulating Links In A HDF5 File Using Python Library H5py

Overview:

  • Any HDF5 Dataset or a group inside a HDF5 file can be made a link.
  • The link can be of the following types:
    • Hard link
    • Soft link
    • External link

Hard links in HDF5:

  • All datasets in a HDF5 file are objects. All the groups as well inside a HDF5 file are objects.
  • These objects are identified by the name given to them at the time of their creation and by more names given after their creation.

file[“/D1”] = 10;

file[“/D2”] = file[“/D1”];

  • These names referred using dictionary notation are links to the objects. Unless the links are symbolic links (soft link or external link), they are hard links. Hard links are reference counted. If the reference count is zero as in the case of objects with the name None, the objects are deleted from the file. i.e., as identity is lost for the object, it can never be referred and no point for it occupy disk space in the file.
  • Hard links are created for the objects as given in the name parameter of the methods create_group(), create_dataset(). The successful execution of File(), results in a link corresponding to the group /(root). The name of the /(root) cannot be None or anything else. But the name of the groups and datasets underneath /(root) can be user defined including None.
  • Hard links are also created using the dictionary notation.

Example:

# Example Python program that creates hard links to

# HDF5 groups and datasets

import h5py

 

# Create a HDF5 file

hdf5File    = h5py.File("HDFWithlinks.hdf5", "w");

 

# Create a group

hgrp = hdf5File.create_group("G1")

 

# Create a link to the group

hdf5File["GLink"] = hgrp;

 

# Create a dataset

dataShape = (5,5);

ds1 = hdf5File.create_dataset("/G1/D1", dataShape);

 

# Create links to the dataset

hdf5File["DLink1"]      = ds1;

hdf5File["DLink2"]      = ds1;

 

# Assign values to the HDF5 dataset using one link

hdf5File["DLink1"][0,0] = 10;

 

# Read from the HDF5 dataset through another link

print(hdf5File["DLink2"][0,0]);

 

hdf5File.close();

 

Output:

10.0

Soft links in HDF5:

  • Soft links contain just the text path rather than a pointer or the address to the object itself.

Example:

# Example Python program that creates a soft link in a HDF5 file

# using H5py

import h5py

 

# Create a HDF5 file

f   = h5py.File("Sample.HDF5", "w");

 

# Create a dataset at root level

d1  = f.create_dataset("/D1", (8, 8));

 

# Create a soft link for the dataset

sl = h5py.SoftLink("/D1");

 

# Print info on dataset and soft link

print(d1);

print(sl);

print("The hierarchical path to the HDF5 object:%s"%(sl.path));

f.close();

 

Output:

<HDF5 dataset "D1": shape (8, 8), type "<f4">

<SoftLink to "/D1">

The hierarchical path to the HDF5 object:/D1

External Links in HDF5:

  • An external link from a HDF5 file refers to a HDF5 Dataset or a HDF5 group, which resides in another HDF5 file.

Example:

# Example Python program that creates an external link

# to an another HDF5 file

import h5py

 

# Create a HDF5 file

file    = h5py.File("WithExternalLink.HDF5", "w");

 

# Create an external link

extLink = h5py.ExternalLink("Sample.hdf5", "/valli/pythonprogs/Sample.hdf5");

 

# Print external file properties

print("File name:%s"%extLink.filename);

print("Path:%s"%extLink.path);

 

# Close the file

file.close();

 

Output:

File name:Sample.hdf5

Path:/valli/pythonprogs/Sample.hdf5

 


Copyright 2023 © pythontic.com