swmr_tools package

Submodules

swmr_tools.DataSource module

class swmr_tools.DataSource.DataFollower(hdf5_file, keypaths, dataset_paths, timeout=1, as_dict=False)

Bases: object

Iterator for returning dataset frames for any number of datasets. This class acts as a wrapper for the KeyFollower.Follower and KeyFollower.FrameGrabber classes.

hdf5_file: h5py.File

Instance of h5py.File object. Choose the file containing data you wish to follow.

keypaths: list

A list of paths (as strings) to groups in hdf5_file containing unique key datasets. (Note: paths must be to the group containing the dataset

and not full paths to the dataset itself)

dataset_paths: list

A list of paths (as strings) to datasets in hdf5_file that you wish to return frames from (Note: paths must be to the dataset and not

to the group containing it)

timeout: int (optional)

The maximum time allowed for a dataset to update before the timeout termination condition is trigerred and iteration is halted. If a value is not set this will default to 10 seconds.

>>> with h5py.File("/home/documents/work/data/example.h5", "r", swmr = True) as f:
>>>     df = DataFollower(f, path_to_key_group, path_to_datasets, timeout = 1)
>>>     for list_of_frames in df:
>>>         print(list_of_frames)
reset()

Reset the iterator to start again from frame 0

swmr_tools.KeyFollower module

class swmr_tools.KeyFollower.Follower(hdf5_file, key_datasets, timeout=10, termination_conditions=['timeout'])

Bases: object

Iterator for following keys datasets in nexus files

hdf5_file: h5py.File

Instance of h5py.File object. Choose the file containing data you wish to follow.

key_datasets: list

A list of paths (as strings) to groups in hdf5_file containing unique key datasets.

timeout: int (optional)

The maximum time allowed for a dataset to update before the timeout termination condition is trigerred and iteration is halted. If a value is not set this will default to 10 seconds.

termination_conditions: list (optional)

A list of strings containing conditions for stopping iteration. Set as timeout by default.

>>> # open hdf5 file using context manager with swmr mode activated
>>> with h5py.File("/home/documents/work/data/example.h5", "r", swmr = True) as f:
>>> # create an instance of the Follower object to iterate through
>>>     kf = Follower(f, 
>>>                   ['path/to/key/group/one', 'path/to/key/group/two'], 
>>>                   timeout = 1, 
>>>                   termination_conditions = ['timeout'])
>>> # iterate through the iterator as with a standard iterator/generator object
>>>     for key in kf:
>>>         print(key)
is_finished()

Returns True if the KeyFollower instance has completed its iteration

reset()

Reset the iterator to start again from index 0

class swmr_tools.KeyFollower.FrameGrabber(dataset, hdf5_file)

Bases: object

Class for extracting frames from a dataset given an index generated by from an instance of the Follower class

hdf5_fileh5py.File

Instance of h5py.File object. Choose the file containing dataset you want to extract frames from.

datasetstr

The full path to the dataset to extract frames from in the hdf5_File

>>> #open and hdf5 file using context manager
>>> with h5py.File("path/to/file") as f:
>>>     fg = FrameGrabber(f, "path/to/dataset")
>>>     #call methods on fg to get the data that you want
Grabber(index)

Method for using an index from KeyFollower to extract that frame from the chosen hdf5 dataset.

indexint

Index for the correspondng non-zero unique key generated by an instance of KeyFollower.Follower

>>> #create a list of frames from all the keys returned by an instance
>>> #of KeyFollower.Follower
>>> frame_list = []
>>> with h5py.File("path/to/file") as f:
>>>     fg = KeyFollower.FrameGrabber(f, "path/to/dataset")
>>>     kf = KeyFollower.Follower(f, ["path/to/keys_1", 
>>>                          "path/to/keys_2"], 
>>>                      timeout = 10)
>>>     for key in kf:
>>>         frame = fg.Grabber(key)
>>>         frame_list.append(frame)

Module contents