Advanced example
Mode understanding
Modes can be set by providing mode=.. keyword or explicitly by specifying read_only and overwrite options.
'w' mode is the same as
>>> sd = DH5('somedata.h5', read_only=False)
'a' mode is the same as
>>> sd = DH5('somedata.h5', read_only=False, overwrite=False)
'r' mode is the same as
>>> sd = DH5('somedata.h5', read_only=True)
DH5.open_overwrite method is the same as
>>> sd = DH5('somedata.h5', read_only=False, overwrite=True)
or
>>> sd = DH5('somedata.h5', mode='w', overwrite=True)
Mirroring acquisitions with a custom backend
AcquisitionManager accepts an optional AcquisitionBackend
implementation. Backends run in the background every time an acquisition is
saved and can be used to duplicate the generated HDF5 file (and the companion
Python/configuration files that Labmate writes next to it) to another location.
The example below mirrors every saved acquisition into a second directory. It
does so by copying the HDF5 file and any file that shares the same stem (e.g.
*_CELL.py, copied configuration files, …) to the mirror folder.
from pathlib import Path
import shutil
from labmate.acquisition import AcquisitionBackend
from labmate.acquisition_notebook import AcquisitionAnalysisManager
class MirrorDirectoryBackend(AcquisitionBackend):
def __init__(self, mirror_root: str):
self._mirror_root = Path(mirror_root)
def save_snapshot(self, acquisition):
source_prefix = Path(acquisition.filepath)
destination_dir = self._mirror_root / source_prefix.parent.name
destination_dir.mkdir(parents=True, exist_ok=True)
# Copy the main HDF5 file and any auxiliary files generated by Labmate.
for source_path in source_prefix.parent.glob(f"{source_prefix.name}*"):
shutil.copy2(source_path, destination_dir / source_path.name)
backend = MirrorDirectoryBackend("/mnt/labmate-mirror")
aqm = AcquisitionAnalysisManager("/data/labmate", backend=backend)
When save_acquisition (or save_acquisition on AcquisitionAnalysisManager)
is called, Labmate will asynchronously copy the freshly written files to the
mirror directory. Should you need to hydrate new local acquisitions from an
existing mirror, implement load_snapshot in the same backend and perform the
copy in the opposite direction.