IO API
read_nwb(path, mode='a', return_io=False)
Read an NWB file.
Parameters:
-
path(str) –Path to the NWB file.
-
mode(str, default:'a') –File read mode (i.e. read/write/append). Defaults to "a".
-
return_io(bool, default:False) –Return IO object alongside the NWB file object. Defaults to False.
Returns:
-
NWBFile(NWBFile | tuple[NWBFile, NWBHDF5IO]) –NWB file object.
-
NWBHDF5IO(NWBFile | tuple[NWBFile, NWBHDF5IO]) –IO object (if return_io=True).
Source code in src/mesoscopy/io.py
write_nwb(path, nwbfile, mode='w', io=None, **kwargs)
Write an NWB file.
Parameters:
-
path(str) –Path to the NWB file.
-
nwbfile(NWBFile) –NWB file object.
-
mode(str, default:'w') –File write mode (i.e. write/append). Defaults to "w".
-
io(NWBHDF5IO, default:None) –An already open IO object to write through. When given, the file is not reopened and
pathandmodeare ignored. Defaults to None. -
**kwargs(Any, default:{}) –Parameters passed to NWBHDF5IO.write.
Source code in src/mesoscopy/io.py
read_h5(path)
Read an HDF5 file.
Parameters:
-
path(str) –Path to the HDF5 file.
Returns:
-
File–h5py.File: HDF5 file object.
write_h5(path, data, compression='lzf', attributes={})
Write a dictionary to an HDF5 file.
Parameters:
-
path(str) –Path to the HDF5 file.
-
data(dict) –Dictionary containing datasets to write in {'dataset_name': data_array} format.
-
compression(str, default:'lzf') –Compression method for the datasets. Defaults to "lzf".
-
attributes(dict, default:{}) –Attributes to write to the HDF5 file. Defaults to {}.
Returns:
-
str(str) –Path to the written HDF5 file.
Example
data = {"dataset1": np.array([1, 2, 3]), "dataset2": np.array([[1, 2], [3, 4]])} write_h5("output.h5", data)
Source code in src/mesoscopy/io.py
write_npz(path, data)
Write a dictionary to an NPZ file.
Parameters:
-
path(str) –Path to the NPZ file.
-
data(dict) –Dictionary containing arrays to write in {'array_name': array} format.
Returns:
-
str(str) –Path to the written NPZ file.
Example
data = {"array1": np.array([1, 2, 3]), "array2": np.array([[1, 2], [3, 4]])} write_npz("output.npz", data)
Source code in src/mesoscopy/io.py
store_interim(array, interim_path, compute=True, chunks=500)
Store an array in an interim Zarr file.
Parameters:
-
array(Dask or Numpy Array) –Dask or Numpy array to persist on disk.
-
interim_path(str) –Path to the interim Zarr file. The .zarr extension is added automatically.
-
compute(bool, default:True) –Whether to compute the array before storing, applies only to Dask arrays. Defaults to True.
-
chunks(int, default:500) –Chunk size for the Zarr file. Defaults to 500.
Returns:
-
Array–Zarr Array: Persistent Zarr array object
Source code in src/mesoscopy/io.py
load_deltaf(path, nwb=False)
Load preprocessed dF/F from an HDF5 or NWB file.
Parameters:
-
path(str) –Path to the preprocessed file.
-
nwb(bool, default:False) –Whether the file is an NWB file. Defaults to False.
Returns:
-
tuple[str, ndarray, ndarray]–tuple[str, np.ndarray, np.ndarray]: Session identifier, dF/F series, and timestamps.
Source code in src/mesoscopy/io.py
read_timestamps_aligned(path)
Read behaviour-aligned timestamps from an HDF5 file.
Parameters:
-
path(str) –Path to the HDF5 file.
Returns:
-
tuple[ndarray, dict[str, Any]] | None–tuple[np.ndarray, dict[str, typing.Any]] | None: The
/timestamps_aligneddataset and its attributes -
tuple[ndarray, dict[str, Any]] | None–(
session_start_time,behaviour_session,offset_s), or None if the file has no such dataset.
Source code in src/mesoscopy/io.py
write_timestamps_aligned(path, timestamps, attrs)
Write behaviour-aligned timestamps to an existing HDF5 file, replacing any existing dataset.
Parameters:
-
path(str) –Path to the HDF5 file.
-
timestamps(ArrayLike) –Seconds from behaviour session start, one value per frame.
-
attrs(dict[str, Any]) –Attributes to set on the dataset (
session_start_time,behaviour_session,offset_s).
Returns:
-
str(str) –Path to the HDF5 file.
Source code in src/mesoscopy/io.py
read_start_time(path)
Read a recording's start time, as stored in the file.
For HDF5 files this is the first /timestamps entry; for NWB files it is /session_start_time.
Parameters:
-
path(str) –Path to the HDF5 or NWB file.
Returns:
-
str(str) –The start time.
Raises:
-
ValueError–If the file has no start time.
Source code in src/mesoscopy/io.py
read_points(path)
Read a landmark points file.
Coordinates are read as (x, y) tuples, i.e. (column, row).
Parameters:
-
path(str) –Path to the points file.
Returns:
-
dict[str, tuple[float, float]]–dict[str, tuple[float, float]]: Dictionary with the landmark names as keys and their (x, y) coordinates
Raises:
-
ValueError–If the file format is unsupported.
Source code in src/mesoscopy/io.py
read_regressors(path)
Read a regressor file in NPZ or HDF5 format.
Parameters:
-
path(str) –Path to the regressor file.
Returns:
-
ndarray–tuple[np.ndarray, list[str], np.ndarray, dict[str, typing.Any] | None]: Regressor matrix, list of regressor
-
list[str]–labels, trial indexes, and behaviour alignment (
session_start_time,behaviour_session,timestamps) or -
ndarray–None if the file has no
session_start_time.
Raises:
-
ValueError–If the file format is unsupported.
Source code in src/mesoscopy/io.py
read_nuisance_regressors(path)
Read an external nuisance regressor file in NPZ or HDF5 format.
Parameters:
-
path(str) –Path to the nuisance regressor file.
Returns:
-
ndarray–tuple[np.ndarray, list[str], np.ndarray]: Nuisance regressor matrix of shape (n_samples, n_regressors),
-
list[str]–list of regressor labels, and the timestamps (n_samples,) the regressors were recorded at.
Raises:
-
ValueError–If the file format is unsupported.
Source code in src/mesoscopy/io.py
write_points(path, points)
Write a dictionary of landmark points to a CSV file.
Coordinates are written as (x, y) tuples, i.e. (column, row).
Parameters:
-
path(str) –Path to output CSV file.
-
points(dict[str, tuple[float, float]]) –Dictionary with the landmark names as keys and their (x, y) coordinates
Source code in src/mesoscopy/io.py
read_mask(path)
Read one or more custom region masks from an NPY, NPZ or TIFF file.
Masks are 2D arrays matching the frame shape of the recording they are applied to. A boolean or integer mask is read
as a single region, named after the NPZ key or, for NPY and TIFF files, the file stem. An integer mask holding
distinct non-zero values is read as one region per label, named <name>_<label>.
Parameters:
-
path(str) –Path to the mask file. NPZ files may hold several masks, one per key.
Returns:
-
dict[str, NDArray[bool_]]–dict[str, npt.NDArray[np.bool_]]: Dictionary with the region names as keys and their boolean masks as values.
Raises:
-
ValueError–If the file format is unsupported, a mask is not 2D, a non-integer mask holds values other than 0 and 1, or two masks in the file resolve to the same name.
Example
read_mask("barrel_cortex.npy") {'barrel_cortex': array([[False, ...]])}