From 2a148377f1e99cdc94260fba90d1c36f05ea6140 Mon Sep 17 00:00:00 2001 From: fdeguire03 <150461249+fdeguire03@users.noreply.github.com> Date: Wed, 8 Jan 2025 18:10:34 -0800 Subject: [PATCH 1/2] Add npy support to load_memmap --- caiman/mmapping.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/caiman/mmapping.py b/caiman/mmapping.py index 1f15f7988..601f3a4b4 100644 --- a/caiman/mmapping.py +++ b/caiman/mmapping.py @@ -46,9 +46,12 @@ def load_memmap(filename: str, mode: str = 'r') -> tuple[Any, tuple, int]: """ logger = logging.getLogger("caiman") - if pathlib.Path(filename).suffix != '.mmap': + allowed_extensions = {'.mmap', '.npy'} + + extension = pathlib.Path(filename).suffix + if extension not in allowed_extensions: logger.error(f"Unknown extension for file {filename}") - raise ValueError(f'Unknown file extension for file {filename} (should be .mmap)') + raise ValueError(f'Unknown file extension for file {filename} (should be .mmap or .npy)') # Strip path components and use CAIMAN_DATA/example_movies # TODO: Eventually get the code to save these in a different dir #fn_without_path = os.path.split(filename)[-1] @@ -63,7 +66,11 @@ def load_memmap(filename: str, mode: str = 'r') -> tuple[Any, tuple, int]: #d1, d2, d3, T, order = int(fpart[-9]), int(fpart[-7]), int(fpart[-5]), int(fpart[-1]), fpart[-3] filename = caiman.paths.fn_relocated(filename) - Yr = np.memmap(filename, mode=mode, shape=prepare_shape((d1 * d2 * d3, T)), dtype=np.float32, order=order) + if extension == '.mmap': + Yr = np.memmap(filename, mode=mode, shape=prepare_shape((d1 * d2 * d3, T)), dtype=np.float32, order=order) + elif extension == '.npy': + Yr = np.load(filename, mmap_mode=mode) + if d3 == 1: return (Yr, (d1, d2), T) else: From 67787fec46df03fd2a9dcaf76264ca3988d56e49 Mon Sep 17 00:00:00 2001 From: fdeguire03 <150461249+fdeguire03@users.noreply.github.com> Date: Fri, 10 Jan 2025 12:42:44 -0800 Subject: [PATCH 2/2] Add data consistency checks to mmapping.py --- caiman/mmapping.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/caiman/mmapping.py b/caiman/mmapping.py index 601f3a4b4..a69558169 100644 --- a/caiman/mmapping.py +++ b/caiman/mmapping.py @@ -66,10 +66,21 @@ def load_memmap(filename: str, mode: str = 'r') -> tuple[Any, tuple, int]: #d1, d2, d3, T, order = int(fpart[-9]), int(fpart[-7]), int(fpart[-5]), int(fpart[-1]), fpart[-3] filename = caiman.paths.fn_relocated(filename) + shape = prepare_shape((d1 * d2 * d3, T)) if extension == '.mmap': - Yr = np.memmap(filename, mode=mode, shape=prepare_shape((d1 * d2 * d3, T)), dtype=np.float32, order=order) + Yr = np.memmap(filename, mode=mode, shape=shape, dtype=np.float32, order=order) elif extension == '.npy': Yr = np.load(filename, mmap_mode=mode) + if Yr.shape != shape: + raise ValueError(f"Data in npy file was an unexpected shape: {Yr.shape}, expected: {shape}") + if Yr.dtype != np.float32: + raise ValueError(f"Data in npy file was an unexpected dtype: {Yr.dtype}, expected: np.float32") + if order == 'C' and not Yr.flags['C_CONTIGUOUS']: + raise ValueError("Data in npy file is not in C-contiguous order as expected.") + elif order == 'F' and not Yr.flags['F_CONTIGUOUS']: + raise ValueError("Data in npy file is not in Fortran-contiguous order as expected.") + + if d3 == 1: return (Yr, (d1, d2), T)