From 389b0488c5fe4bbd0d806246991603a7ae4eb5bd Mon Sep 17 00:00:00 2001 From: fdeguire03 <150461249+fdeguire03@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:32:16 -0800 Subject: [PATCH 1/4] Improve get_file_size efficiency for .npy files --- caiman/base/movies.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/caiman/base/movies.py b/caiman/base/movies.py index 0e0805dc1..c5beafe59 100644 --- a/caiman/base/movies.py +++ b/caiman/base/movies.py @@ -2026,7 +2026,14 @@ def get_file_size(file_name, var_name_hdf5:str='mov') -> tuple[tuple, Union[int, raise Exception('Variable not found. Use one of the above') T, dims = siz[0], siz[1:] elif extension in ('.npy', ): - shape = np.load(file_name, allow_pickle=False).shape + with open(file_path, 'rb') as f: + version = np.lib.format.read_magic(f) + if version == (1, 0): + shape, _, _ = np.lib.format.read_array_header_1_0(f) + elif version == (2, 0): + shape, _, _ = np.lib.format.read_array_header_2_0(f) + else: + raise ValueError(f"Unsupported .npy file version: {version}. Update this code to handle it.") T = shape[0] dims = shape[1:] elif extension in ('.sbx'): From 67c59dd557f97d1f3da34959e337813661c7c91b Mon Sep 17 00:00:00 2001 From: fdeguire03 <150461249+fdeguire03@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:44:49 -0800 Subject: [PATCH 2/4] Fix file_name typo --- caiman/base/movies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caiman/base/movies.py b/caiman/base/movies.py index c5beafe59..e2f99bc48 100644 --- a/caiman/base/movies.py +++ b/caiman/base/movies.py @@ -2026,7 +2026,7 @@ def get_file_size(file_name, var_name_hdf5:str='mov') -> tuple[tuple, Union[int, raise Exception('Variable not found. Use one of the above') T, dims = siz[0], siz[1:] elif extension in ('.npy', ): - with open(file_path, 'rb') as f: + with open(file_name, 'rb') as f: version = np.lib.format.read_magic(f) if version == (1, 0): shape, _, _ = np.lib.format.read_array_header_1_0(f) From c4668e6ee7b559bce6764e80ff39f7dbfbceff8a Mon Sep 17 00:00:00 2001 From: fdeguire03 <150461249+fdeguire03@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:32:16 -0800 Subject: [PATCH 3/4] Improve get_file_size efficiency for .npy files --- caiman/base/movies.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/caiman/base/movies.py b/caiman/base/movies.py index 0e0805dc1..e2f99bc48 100644 --- a/caiman/base/movies.py +++ b/caiman/base/movies.py @@ -2026,7 +2026,14 @@ def get_file_size(file_name, var_name_hdf5:str='mov') -> tuple[tuple, Union[int, raise Exception('Variable not found. Use one of the above') T, dims = siz[0], siz[1:] elif extension in ('.npy', ): - shape = np.load(file_name, allow_pickle=False).shape + with open(file_name, 'rb') as f: + version = np.lib.format.read_magic(f) + if version == (1, 0): + shape, _, _ = np.lib.format.read_array_header_1_0(f) + elif version == (2, 0): + shape, _, _ = np.lib.format.read_array_header_2_0(f) + else: + raise ValueError(f"Unsupported .npy file version: {version}. Update this code to handle it.") T = shape[0] dims = shape[1:] elif extension in ('.sbx'): From 5ce0ce5b74f6c0d5ccb8ef80c5c0b3be1001520d Mon Sep 17 00:00:00 2001 From: fdeguire03 <150461249+fdeguire03@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:02:23 -0800 Subject: [PATCH 4/4] Clarify comment in exception --- caiman/base/movies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caiman/base/movies.py b/caiman/base/movies.py index e2f99bc48..f0a9a5796 100644 --- a/caiman/base/movies.py +++ b/caiman/base/movies.py @@ -2033,7 +2033,7 @@ def get_file_size(file_name, var_name_hdf5:str='mov') -> tuple[tuple, Union[int, elif version == (2, 0): shape, _, _ = np.lib.format.read_array_header_2_0(f) else: - raise ValueError(f"Unsupported .npy file version: {version}. Update this code to handle it.") + raise ValueError(f"Unsupported .npy file version: {version}. Update caiman.base.movies.get_file_size() to handle it.") T = shape[0] dims = shape[1:] elif extension in ('.sbx'):