Skip to content

Storage

storage

Custom storage interface for MCMC sampling.

The custom interface is built in a way to enable storage of samples directly to the disk.

Classes:

Name Description
MCMCStorage

Abstract base class for storages

NumpyStorage

Simple in memory storage

ZarrStorage

Storage with a Zarr backend that saves samples to disk automatically

ls_mcmc.storage.MCMCStorage

Bases: ABC

Abstract base class for MCMC sample storage.

Methods:

Name Description
store

Store a new sample

values

return all stored samples

flush

flush samples to disk

values abstractmethod property

values: Iterable

Return all stored samples.

__init__

__init__() -> None

Initialize storage.

store abstractmethod

store(sample: np.ndarray[tuple[int], np.dtype[np.floating]]) -> None

Store a single sample.

flush abstractmethod

flush() -> None

Flushes samples to disk.

ls_mcmc.storage.NumpyStorage

Bases: MCMCStorage

In-memory storage using numpy arrays.

Methods:

Name Description
store

Store a new sample

values

return all stored samples

flush

Not intended to be used

values property

values: np.ndarray[tuple[int, int], np.dtype[np.floating]]

Return all stored samples as numpy array.

store

store(sample: np.ndarray[tuple[int], np.dtype[np.floating]]) -> None

Stores sample.

flush

flush() -> None

Does nothing except log an error, since NumpyStorage does not support storage to disk.

Use ZarrStorage if you want storage to disk.

ls_mcmc.storage.ZarrStorage

Bases: MCMCStorage

Disk-based storage using Zarr with chunking.

Methods:

Name Description
store

Store a new sample

values

return all stored samples

flush

flush samples to disk

initialize_from_disk

Initialize Zarr Storage from existing storage on the disk

values property

values: zarr.Array

Returns ZarrArray containing all the samples.

__init__

__init__(save_directory: pathlib.Path, chunk_size: int, zarr_storage_group: zarr.Group | None = None, overwrite: bool = False) -> None

Initialize ZarrStorage with save directory and chunk size.

Parameters:

Name Type Description Default
save_directory pathlib.Path

Path where the Zarr store will be created.

required
chunk_size int

Number of samples to accumulate before writing to disk. Must be greater than zero.

required
zarr_storage_group zarr.Group | None

If specified this zarr_storage_group is used instead of creating a new one. In this case save_directory can be set to None.

None
overwrite bool

Whether to overwrite the data at save_directory or not. Defaults to False.

False

initialize_from_disk classmethod

initialize_from_disk(save_directory: pathlib.Path, chunk_size: int = 1) -> ZarrStorage

Initializes the Zarr storage from an already existing Zarr storage on disk.

This can be used to either read the samples from the disk after a MCMC run or to restart a run and append the new samples.

Parameters:

Name Type Description Default
save_directory pathlib.Path

Path where the Zarr store will be created.

required
chunk_size int

Number of samples to accumulate before writing to disk. Must be greater than zero.

1

store

store(sample: np.ndarray[tuple[int], np.dtype[np.floating]]) -> None

Add sample to ZarrStorage.

_save_to_disk

_save_to_disk() -> None

Save accumulated samples to Zarr storage.

flush

flush() -> None

Save all values to disk.