Group
A Group
is analogous to a directory for an operating system. A Group
can contain any number of sub-Group
s (i.e., sub-directories) and it can contain any number
of Datasets. It uses a naming convention analogous to UNIX file systems where every sub-directory is
separated from its parent directory by the '/'
character.
From a Python perspective, a Group
operates like a dict
. The keys are
the names of Group
members, and the values are the members themselves
(Group
or Dataset
objects).
>>> print(root.tree())
<JSONWriter 'example.json' (3 groups, 1 datasets, 0 metadata)>
<Group '/a' (2 groups, 1 datasets, 0 metadata)>
<Group '/a/b' (1 groups, 1 datasets, 0 metadata)>
<Group '/a/b/c' (0 groups, 1 datasets, 0 metadata)>
<Dataset '/a/b/c/dset' shape=(100,) dtype='<f8' (0 metadata)>
A Group
can be in read-only mode, but can also be set to editable mode
>>> b.create_dataset('dset_b', data=[1, 2, 3, 4])
Traceback (most recent call last):
...
ValueError: Cannot modify <Group '/a/b' (1 groups, 1 datasets, 0 metadata)>. It is accessed in read-only mode.
>>> b.read_only = False
>>> b.create_dataset('dset_b', data=[1, 2, 3, 4])
<Dataset '/a/b/dset_b' shape=(4,) dtype='<f8' (0 metadata)>
The keys of a Group
can also be accessed as class attributes
>>> root['a']['b']['c']['dset']
<Dataset '/a/b/c/dset' shape=(100,) dtype='<f8' (0 metadata)>
>>> root.a.b.c.dset
<Dataset '/a/b/c/dset' shape=(100,) dtype='<f8' (0 metadata)>
See Accessing Keys as Class Attributes for more information.
You can navigate through the tree by considering a Group
to be an ancestor
or descendant of other Group
s
>>> for ancestor in c.ancestors():
... print(ancestor)
<Group '/a/b' (1 groups, 2 datasets, 0 metadata)>
<Group '/a' (2 groups, 2 datasets, 0 metadata)>
<JSONWriter 'example.json' (3 groups, 2 datasets, 0 metadata)>
>>> for descendant in b.descendants():
... print(descendant)
<Group '/a/b/c' (0 groups, 1 datasets, 0 metadata)>