Groups — h5py 3.13.0 documentation (original) (raw)

Groups are the container mechanism by which HDF5 files are organized. From a Python perspective, they operate somewhat like dictionaries. In this case the “keys” are the names of group members, and the “values” are the members themselves (Group and Dataset) objects.

Group objects also contain most of the machinery which makes HDF5 useful. The File object does double duty as the HDF5 root group, and serves as your entry point into the file:

f = h5py.File('foo.hdf5','w') f.name '/' list(f.keys()) []

Names of all objects in the file are all text strings (str). These will be encoded with the HDF5-approved UTF-8 encoding before being passed to the HDF5 C library. Objects may also be retrieved using byte strings, which will be passed on to HDF5 as-is.

Creating groups

New groups are easy to create:

grp = f.create_group("bar") grp.name '/bar' subgrp = grp.create_group("baz") subgrp.name '/bar/baz'

Multiple intermediate groups can also be created implicitly:

grp2 = f.create_group("/some/long/path") grp2.name '/some/long/path' grp3 = f['/some/long'] grp3.name '/some/long'

Dict interface and links

Groups implement a subset of the Python dictionary convention. They have methods like keys(), values() and support iteration. Most importantly, they support the indexing syntax, and standard exceptions:

myds = subgrp["MyDS"] missing = subgrp["missing"] KeyError: "Name doesn't exist (Symbol table: Object not found)"

Objects can be deleted from the file using the standard syntax:

del subgroup["MyDataset"]

Note

When using h5py from Python 3, the keys(), values() and items() methods will return view-like objects instead of lists. These objects support membership testing and iteration, but can’t be sliced like lists.

By default, objects inside group are iterated in alphanumeric order. However, if group is created with track_order=True, the insertion order for the group is remembered (tracked) in HDF5 file, and group contents are iterated in that order. The latter is consistent with Python 3.7+ dictionaries.

The default track_order for all new groups can be specified globally with h5.get_config().track_order.

Hard links

What happens when assigning an object to a name in the group? It depends on the type of object being assigned. For NumPy arrays or other data, the default is to create an HDF5 datasets:

grp["name"] = 42 out = grp["name"] out <HDF5 dataset "name": shape (), type "<i8">

When the object being stored is an existing Group or Dataset, a new link is made to the object:

grp["other name"] = out grp["other name"] <HDF5 dataset "other name": shape (), type "<i8">

Note that this is not a copy of the dataset! Like hard links in a UNIX file system, objects in an HDF5 file can be stored in multiple groups:

grp["other name"] == grp["name"] True

Soft links

Also like a UNIX filesystem, HDF5 groups can contain “soft” or symbolic links, which contain a text path instead of a pointer to the object itself. You can easily create these in h5py by using h5py.SoftLink:

myfile = h5py.File('foo.hdf5','w') group = myfile.create_group("somegroup") myfile["alias"] = h5py.SoftLink('/somegroup')

If the target is removed, they will “dangle”:

del myfile['somegroup'] print(myfile['alias']) KeyError: 'Component not found (Symbol table: Object not found)'

External links

External links are “soft links plus”, which allow you to specify the name of the file as well as the path to the desired object. You can refer to objects in any file you wish. Use similar syntax as for soft links:

myfile = h5py.File('foo.hdf5','w') myfile['ext link'] = h5py.ExternalLink("otherfile.hdf5", "/path/to/resource")

When the link is accessed, the file “otherfile.hdf5” is opened, and object at “/path/to/resource” is returned.

Since the object retrieved is in a different file, its “.file” and “.parent” properties will refer to objects in that file, not the file in which the link resides.

Note

Currently, you can’t access an external link if the file it points to is already open. This is related to how HDF5 manages file permissions internally.

Note

The filename is stored in the file as bytes, normally UTF-8 encoded. In most cases, this should work reliably, but problems are possible if a file created on one platform is accessed on another. Older versions of HDF5 may have problems on Windows in particular. See Filenames on different systems for more details.

Reference

class h5py.Group(identifier)

Generally Group objects are created by opening objects in the file, or by the method Group.create_group(). Call the constructor with a GroupID instance to create a new Group bound to an existing low-level identifier.

__iter__()

Iterate over the names of objects directly attached to the group. Use Group.visit() or Group.visititems() for recursive access to group members.

__contains__(name)

Dict-like membership testing. name may be a relative or absolute path.

__getitem__(name)

Retrieve an object. name may be a relative or absolute path, or an object or region reference. See Dict interface and links.

__setitem__(name, value)

Create a new link, or automatically create a dataset. See Dict interface and links.

__bool__()

Check that the group is accessible. A group could be inaccessible for several reasons. For instance, the group, or the file it belongs to, may have been closed elsewhere.

f = h5py.open(filename) group = f["MyGroup"] f.close() if group: ... print("group is accessible") ... else: ... print("group is inaccessible") group is inaccessible

keys()

Get the names of directly attached group members. Use Group.visit() or Group.visititems() for recursive access to group members.

Returns:

set-like object.

values()

Get the objects contained in the group (Group and Dataset instances). Broken soft or external links show up as None.

Returns:

a collection or bag-like object.

items()

Get (name, value) pairs for object directly attached to this group. Values for broken soft or external links show up as None.

Returns:

a set-like object.

get(name, default=None, getclass=False, getlink=False)

Retrieve an item, or information about an item. name and defaultwork like the standard Python dict.get.

Parameters:

visit(callable)

Recursively visit all objects in this group and subgroups. You supply a callable with the signature:

callable(name) -> None or return value

name will be the name of the object relative to the current group. Return None to continue visiting until all objects are exhausted. Returning anything else will immediately stop visiting and return that value from visit:

def find_foo(name): ... """ Find first object with 'foo' anywhere in the name """ ... if 'foo' in name: ... return name group.visit(find_foo) 'some/subgroup/foo'

visititems(callable)

Recursively visit all objects in this group and subgroups. LikeGroup.visit(), except your callable should have the signature:

callable(name, object) -> None or return value

In this case object will be a Group or Datasetinstance.

visit_links(callable)

visititems_links(callable)

These methods are like visit() and visititems(), but work on the links in groups, rather than the objects those links point to. So if you have two links pointing to the same object, these will ‘see’ both. They also see soft & external links, which visit() andvisititems() ignore.

The second argument to the callback for visititems_links is an instance of one of the link classes.

New in version 3.11.

move(source, dest)

Move an object or link in the file. If source is a hard link, this effectively renames the object. If a soft or external link, the link itself is moved.

Parameters:

copy(source, dest, name=None, shallow=False, expand_soft=False, expand_external=False, expand_refs=False, without_attrs=False)

Copy an object or group. The source can be a path, Group, Dataset, or Datatype object. The destination can be either a path or a Group object. The source and destination need not be in the same file.

If the source is a Group object, by default all objects within that group will be copied recursively.

When the destination is a Group object, by default the target will be created in that group with its current name (basename of obj.name). You can override that by setting “name” to a string.

Parameters:

create_group(name, track_order=None)

Create and return a new group in the file.

Parameters:

Returns:

The new Group object.

require_group(name)

Open a group in the file, creating it if it doesn’t exist. TypeError is raised if a conflicting object already exists. Parameters as in Group.create_group().

create_dataset(name, shape=None, dtype=None, data=None, **kwds)

Create a new dataset. Options are explained in Creating datasets.

Parameters:

require_dataset(name, shape, dtype, exact=False, **kwds)

Open a dataset, creating it if it doesn’t exist.

If keyword “exact” is False (default), an existing dataset must have the same shape and a conversion-compatible dtype to be returned. If True, the shape and dtype must match exactly.

If keyword “maxshape” is given, the maxshape and dtype must match instead.

If any of the keywords “rdcc_nslots”, “rdcc_nbytes”, or “rdcc_w0” are given, they will be used to configure the dataset’s chunk cache.

Other dataset keywords (see create_dataset) may be provided, but are only used if a new dataset is to be created.

Raises TypeError if an incompatible object already exists, or if the shape, maxshape or dtype don’t match according to the above rules.

Parameters:

exact – Require shape and type to match exactly (T/F)

create_dataset_like(name, other, **kwds)

Create a dataset similar to other, much like numpy’s _like functions.

Parameters:

Any dataset keywords (see create_dataset) may be provided, including shape and dtype, in which case the provided values take precedence over those from other.

create_virtual_dataset(name, layout, fillvalue=None)

Create a new virtual dataset in this group. See Virtual Datasets (VDS) for more details.

Parameters:

build_virtual_dataset()

Assemble a virtual dataset in this group.

This is used as a context manager:

with f.build_virtual_dataset('virt', (10, 1000), np.uint32) as layout: layout[0] = h5py.VirtualSource('foo.h5', 'data', (1000,))

Inside the context, you populate a VirtualLayout object. The file is only modified when you leave the context, and if there’s no error.

Parameters:

attrs

Attributes for this group.

id

The groups’s low-level identifier; an instance ofGroupID.

ref

An HDF5 object reference pointing to this group. SeeUsing object references.

regionref

A proxy object allowing you to interrogate region references. See Using region references.

name

String giving the full path to this group.

file

File instance in which this group resides.

parent

Group instance containing this group.

class h5py.HardLink

Exists only to support Group.get(). Has no state and provides no properties or methods.

class h5py.SoftLink(path)

Exists to allow creation of soft links in the file. See Soft links. These only serve as containers for a path; they are not related in any way to a particular file.

Parameters:

path (String) – Value of the soft link.

path

Value of the soft link

class h5py.ExternalLink(filename, path)

Like SoftLink, only they specify a filename in addition to a path. See External links.

Parameters:

filename

Name of the external file as a Unicode string

path

Path to the object in the external file