From c668f06cddcb4347de84c381eb41645c276a6f78 Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:57:48 +0100 Subject: [PATCH 01/11] renamings --- alphabase/io/tempmmap.py | 72 ++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index 80c2af30..f9cfb01f 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -25,14 +25,14 @@ def _init_temp_dir(prefix: str = "temp_mmap_") -> str: TEMP_DIR_NAME = _TEMP_DIR.name logging.info( - f"Temp mmap arrays are written to {TEMP_DIR_NAME}. " + f"Memory-mapped arrays are written to temporary directory {TEMP_DIR_NAME}. " "Cleanup of this folder is OS dependent and might need to be triggered manually!" ) return TEMP_DIR_NAME -def _change_temp_dir_location(abs_path: str) -> str: +def _change_temp_dir_location(abs_path: str) -> None: """ Check if the directory to which the temp arrays should be written exists, if so defines this as the new temp dir location. If not raise a value error. @@ -69,7 +69,7 @@ def _get_file_location(abs_file_path: str, overwrite=False) -> str: Parameters ---------- - abs_path : str + abs_file_path : str The absolute path to the new temporary file. Returns @@ -77,19 +77,16 @@ def _get_file_location(abs_file_path: str, overwrite=False) -> str: str The file path if it is valid. """ - # check overwrite status and existence of file if not overwrite and os.path.exists(abs_file_path): raise ValueError( - "The file already exists. Set overwrite to True to overwrite the file or choose a different name." + f"The file '{abs_file_path}' already exists. Set overwrite to True to overwrite the file or choose a different name." ) - # ensure that the filename conforms to the naming convention if not os.path.basename(abs_file_path).endswith(".hdf"): raise ValueError( f"The chosen file name '{os.path.basename(abs_file_path)}' needs to end with .hdf" ) - # ensure that the directory in which the file should be created exists if os.path.isdir(os.path.dirname(abs_file_path)): return abs_file_path else: @@ -98,9 +95,8 @@ def _get_file_location(abs_file_path: str, overwrite=False) -> str: ) -def redefine_temp_location(path): - """ - Redfine the location where the temp arrays are written to. +def redefine_temp_location(path: str) -> str: + """Redefine the location where the temp arrays are written to. Parameters ---------- @@ -113,24 +109,17 @@ def redefine_temp_location(path): """ - global _TEMP_DIR, TEMP_DIR_NAME + global TEMP_DIR_NAME - logging.warning( - f"""Folder {TEMP_DIR_NAME} with temp mmap arrays is being deleted. All existing temp mmapp arrays will be unusable!""" - ) + _clear() - # cleaup old temporary directory + # cleanup old temporary directory shutil.rmtree(TEMP_DIR_NAME, ignore_errors=True) # create new tempfile at desired location - _TEMP_DIR = tempfile.TemporaryDirectory(prefix=os.path.join(path, "temp_mmap_")) - TEMP_DIR_NAME = _TEMP_DIR.name + temp_dir_name = _init_temp_dir(prefix=os.path.join(path, "temp_mmap_")) - logging.warning( - f"""New temp folder location. Temp mmap arrays are written to {TEMP_DIR_NAME}. Cleanup of this folder is OS dependant, and might need to be triggered manually!""" - ) - - return TEMP_DIR_NAME + return temp_dir_name def array(shape: tuple, dtype: np.dtype, tmp_dir_abs_path: str = None) -> np.ndarray: @@ -165,12 +154,15 @@ def array(shape: tuple, dtype: np.dtype, tmp_dir_abs_path: str = None) -> np.nda ) with h5py.File(temp_file_name, "w") as hdf_file: - array = hdf_file.create_dataset("array", shape=shape, dtype=dtype) - array[0] = np.string_("") if isinstance(dtype, np.dtypes.StrDType) else 0 - offset = array.id.get_offset() + created_array = hdf_file.create_dataset("array", shape=shape, dtype=dtype) + created_array[0] = ( + np.string_("") if isinstance(dtype, np.dtypes.StrDType) else 0 + ) + offset = created_array.id.get_offset() with open(temp_file_name, "rb+") as raw_hdf_file: mmap_obj = mmap.mmap(raw_hdf_file.fileno(), 0, access=mmap.ACCESS_WRITE) + return np.frombuffer( mmap_obj, dtype=dtype, count=np.prod(shape), offset=offset ).reshape(shape) @@ -227,8 +219,10 @@ def create_empty_mmap( ) # TODO overwrite=overwrite with h5py.File(temp_file_name, "w") as hdf_file: - array = hdf_file.create_dataset("array", shape=shape, dtype=dtype) - array[0] = np.string_("") if isinstance(dtype, np.dtypes.StrDType) else 0 + created_array = hdf_file.create_dataset("array", shape=shape, dtype=dtype) + created_array[0] = ( + np.string_("") if isinstance(dtype, np.dtypes.StrDType) else 0 + ) return temp_file_name # TODO temp_file_path @@ -251,10 +245,10 @@ def mmap_array_from_path(hdf_file: str) -> np.ndarray: # read parameters required to reinitialize the mmap object with h5py.File(path, "r") as hdf_file: - array = hdf_file["array"] - offset = array.id.get_offset() - shape = array.shape - dtype = array.dtype + array_ = hdf_file["array"] + offset = array_.id.get_offset() + shape = array_.shape + dtype = array_.dtype # reinitialize the mmap object with open(path, "rb+") as raw_hdf_file: @@ -279,9 +273,9 @@ def zeros(shape: tuple, dtype: np.dtype) -> np.ndarray: type A writable temporary mmapped array filled with zeros. """ - _array = array(shape, dtype) - _array[:] = 0 - return _array + array_ = array(shape, dtype) + array_[:] = 0 + return array_ def ones(shape: tuple, dtype: np.dtype) -> np.ndarray: @@ -299,9 +293,9 @@ def ones(shape: tuple, dtype: np.dtype) -> np.ndarray: type A writable temporary mmapped array filled with ones. """ - _array = array(shape, dtype) - _array[:] = 1 - return _array + array_ = array(shape, dtype) + array_[:] = 1 + return array_ @atexit.register @@ -314,8 +308,8 @@ def _clear() -> None: if _TEMP_DIR is not None: logging.warning( - f"Folder {TEMP_DIR_NAME} with temp mmap arrays is being deleted. " - "All existing temp mmapp arrays will be unusable!" + f"Temporary folder {TEMP_DIR_NAME} with memory-mapped arrays is being deleted. " + "All existing memory-mapped arrays will be unusable!" ) del _TEMP_DIR From 7e7cf392a484371177bd15c1359b206c9388b945 Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:59:31 +0100 Subject: [PATCH 02/11] more tests --- alphabase/io/tempmmap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index f9cfb01f..bda58636 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -51,10 +51,10 @@ def _change_temp_dir_location(abs_path: str) -> None: if os.path.isdir(abs_path): TEMP_DIR_NAME = abs_path else: - raise ValueError(f"The path {abs_path} does not point to a directory.") + raise ValueError(f"The path '{abs_path} 'does not point to a directory.") else: raise ValueError( - f"The directory {abs_path} in which the file should be created does not exist." + f"The directory '{abs_path}' in which the file should be created does not exist." ) From 7c866b8ea936089a44b3a69af1b41587adc6511e Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:59:46 +0100 Subject: [PATCH 03/11] more tests --- tests/unit/io/test_tempmmap.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/unit/io/test_tempmmap.py b/tests/unit/io/test_tempmmap.py index d000759f..ef6ddeda 100644 --- a/tests/unit/io/test_tempmmap.py +++ b/tests/unit/io/test_tempmmap.py @@ -66,6 +66,18 @@ def test_create_array_with_custom_temp_dir(): assert temp_dir == tempmmap.TEMP_DIR_NAME +def test_create_array_with_custom_temp_dir_nonexisting(): + tempmmap = sys.modules["alphabase.io.tempmmap"] + + temp_dir = "nonexisting_dir" + # when + with pytest.raises( + ValueError, + match="The directory 'nonexisting_dir' in which the file should be created does not exist.", + ): + _ = tempmmap.array((5, 5), np.int32, tmp_dir_abs_path=temp_dir) + + def test_mmap_array_from_path(): """Test reconnecting to an existing array.""" tempmmap = sys.modules["alphabase.io.tempmmap"] From 10ebe8ce6da9a997b8db2cdbb931d90e98fac622 Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:13:01 +0100 Subject: [PATCH 04/11] fix passing overwrite --- alphabase/io/tempmmap.py | 6 ++---- tests/unit/io/test_tempmmap.py | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index bda58636..59ece46a 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -51,7 +51,7 @@ def _change_temp_dir_location(abs_path: str) -> None: if os.path.isdir(abs_path): TEMP_DIR_NAME = abs_path else: - raise ValueError(f"The path '{abs_path} 'does not point to a directory.") + raise ValueError(f"The path '{abs_path}' does not point to a directory.") else: raise ValueError( f"The directory '{abs_path}' in which the file should be created does not exist." @@ -214,9 +214,7 @@ def create_empty_mmap( temp_dir_name, f"temp_mmap_{np.random.randint(2**63, dtype=np.int64)}.hdf" ) else: - temp_file_name = _get_file_location( - file_path, overwrite=False - ) # TODO overwrite=overwrite + temp_file_name = _get_file_location(file_path, overwrite=overwrite) with h5py.File(temp_file_name, "w") as hdf_file: created_array = hdf_file.create_dataset("array", shape=shape, dtype=dtype) diff --git a/tests/unit/io/test_tempmmap.py b/tests/unit/io/test_tempmmap.py index ef6ddeda..1895648e 100644 --- a/tests/unit/io/test_tempmmap.py +++ b/tests/unit/io/test_tempmmap.py @@ -67,6 +67,7 @@ def test_create_array_with_custom_temp_dir(): def test_create_array_with_custom_temp_dir_nonexisting(): + """Test creating an array with custom temp dir: not existing.""" tempmmap = sys.modules["alphabase.io.tempmmap"] temp_dir = "nonexisting_dir" @@ -78,6 +79,20 @@ def test_create_array_with_custom_temp_dir_nonexisting(): _ = tempmmap.array((5, 5), np.int32, tmp_dir_abs_path=temp_dir) +def test_create_array_with_custom_temp_dir_not_a_dir(): + """Test creating an array with custom temp dir: not a directory.""" + tempmmap = sys.modules["alphabase.io.tempmmap"] + + with tempfile.TemporaryFile() as temp_file, pytest.raises( + ValueError, + match=f"The path '{temp_file.name}' does not point to a directory.", + ): + # when + _ = tempmmap.create_empty_mmap( + (5, 5), np.int32, tmp_dir_abs_path=temp_file.name + ) + + def test_mmap_array_from_path(): """Test reconnecting to an existing array.""" tempmmap = sys.modules["alphabase.io.tempmmap"] @@ -168,6 +183,27 @@ def test_create_empty_with_custom_file_path(): assert temp_dir != tempmmap.TEMP_DIR_NAME +def test_create_empty_with_custom_file_path_exists(): + """Test creating and accessing an empty array with custom file path that exists.""" + tempmmap = sys.modules["alphabase.io.tempmmap"] + + # when + with tempfile.TemporaryFile() as temp_file, pytest.raises( + ValueError, + match=f"The file '{temp_file.name}' already exists. Set overwrite to True to overwrite the file or choose a different name.", + ): + _ = tempmmap.create_empty_mmap((5, 5), np.float32, file_path=temp_file.name) + + # when 2 + with tempfile.TemporaryDirectory() as temp_dir, open( + f"{temp_dir}/temp_mmap.hdf", "w" + ) as temp_file: + _ = tempmmap.create_empty_mmap( + (5, 5), np.float32, file_path=temp_file.name, overwrite=True + ) + # did not raise -> OK + + def test_create_empty_with_custom_file_path_error_cases(): """Test creating and accessing an empty array: error cases.""" tempmmap = sys.modules["alphabase.io.tempmmap"] From eb411bd052ec87c645dde5f229493e0c26c2689a Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:14:54 +0100 Subject: [PATCH 05/11] refactoring --- alphabase/io/tempmmap.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index 59ece46a..52eb7582 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -58,7 +58,7 @@ def _change_temp_dir_location(abs_path: str) -> None: ) -def _get_file_location(abs_file_path: str, overwrite=False) -> str: +def _get_file_location(abs_file_path: str, overwrite: bool = False) -> str: """ Check if the path specified for the new temporary file is valid. If not raise a value error. @@ -87,13 +87,13 @@ def _get_file_location(abs_file_path: str, overwrite=False) -> str: f"The chosen file name '{os.path.basename(abs_file_path)}' needs to end with .hdf" ) - if os.path.isdir(os.path.dirname(abs_file_path)): - return abs_file_path - else: + if not os.path.isdir(os.path.dirname(abs_file_path)): raise ValueError( f"The directory '{os.path.dirname(abs_file_path)}' in which the file should be created does not exist." ) + return abs_file_path + def redefine_temp_location(path: str) -> str: """Redefine the location where the temp arrays are written to. @@ -210,19 +210,19 @@ def create_empty_mmap( # if path does not exist generate a random file name in the TEMP directory if file_path is None: - temp_file_name = os.path.join( + temp_file_path = os.path.join( temp_dir_name, f"temp_mmap_{np.random.randint(2**63, dtype=np.int64)}.hdf" ) else: - temp_file_name = _get_file_location(file_path, overwrite=overwrite) + temp_file_path = _get_file_location(file_path, overwrite=overwrite) - with h5py.File(temp_file_name, "w") as hdf_file: + with h5py.File(temp_file_path, "w") as hdf_file: created_array = hdf_file.create_dataset("array", shape=shape, dtype=dtype) created_array[0] = ( np.string_("") if isinstance(dtype, np.dtypes.StrDType) else 0 ) - return temp_file_name # TODO temp_file_path + return temp_file_path def mmap_array_from_path(hdf_file: str) -> np.ndarray: From bdcbf16ce6abc9edf5ffede7b5fad45599850b48 Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:21:58 +0100 Subject: [PATCH 06/11] docs --- alphabase/io/tempmmap.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index 52eb7582..2a51d802 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -123,7 +123,7 @@ def redefine_temp_location(path: str) -> str: def array(shape: tuple, dtype: np.dtype, tmp_dir_abs_path: str = None) -> np.ndarray: - """Create a writable temporary mmapped array. + """Create a writable temporary memory-mapped array. Parameters ---------- @@ -139,7 +139,7 @@ def array(shape: tuple, dtype: np.dtype, tmp_dir_abs_path: str = None) -> np.nda Returns ------- type - A writable temporary mmapped array. + A writable temporary memory-mapped array. """ temp_dir_name = _init_temp_dir() @@ -226,7 +226,7 @@ def create_empty_mmap( def mmap_array_from_path(hdf_file: str) -> np.ndarray: - """reconnect to an exisiting HDF5 file to generate a writable temporary mmapped array. + """reconnect to an exisiting HDF5 file to generate a writable temporary memory-mapped array. Parameters ---------- @@ -236,7 +236,7 @@ def mmap_array_from_path(hdf_file: str) -> np.ndarray: Returns ------- type - A writable temporary mmapped array. + A writable temporary memory-mapped array. """ path = os.path.join(hdf_file) @@ -257,7 +257,7 @@ def mmap_array_from_path(hdf_file: str) -> np.ndarray: def zeros(shape: tuple, dtype: np.dtype) -> np.ndarray: - """Create a writable temporary mmapped array filled with zeros. + """Create a writable temporary memory-mapped array filled with zeros. Parameters ---------- @@ -269,7 +269,7 @@ def zeros(shape: tuple, dtype: np.dtype) -> np.ndarray: Returns ------- type - A writable temporary mmapped array filled with zeros. + A writable temporary memory-mapped array filled with zeros. """ array_ = array(shape, dtype) array_[:] = 0 @@ -277,7 +277,7 @@ def zeros(shape: tuple, dtype: np.dtype) -> np.ndarray: def ones(shape: tuple, dtype: np.dtype) -> np.ndarray: - """Create a writable temporary mmapped array filled with ones. + """Create a writable temporary memory-mapped array filled with ones. Parameters ---------- @@ -289,7 +289,7 @@ def ones(shape: tuple, dtype: np.dtype) -> np.ndarray: Returns ------- type - A writable temporary mmapped array filled with ones. + A writable temporary memory-mapped array filled with ones. """ array_ = array(shape, dtype) array_[:] = 1 @@ -298,7 +298,7 @@ def ones(shape: tuple, dtype: np.dtype) -> np.ndarray: @atexit.register def _clear() -> None: - """Reset the temporary folder containing temp mmapped arrays. + """Reset the temporary folder containing temp memory-mapped arrays. WARNING: All existing temp mmapp arrays will be unusable! """ @@ -315,7 +315,7 @@ def _clear() -> None: def clear() -> str: - """Reset the temporary folder containing temp mmapped arrays and create a new one. + """Reset the temporary folder containing temp memory-mapped arrays and create a new one. WARNING: All existing temp mmapp arrays will be unusable! From 97b0ec7b575e7938c10783982a352cfa8f96cb9f Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:27:52 +0100 Subject: [PATCH 07/11] renaming --- alphabase/io/tempmmap.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index 2a51d802..ad38a169 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -149,18 +149,18 @@ def array(shape: tuple, dtype: np.dtype, tmp_dir_abs_path: str = None) -> np.nda _change_temp_dir_location(tmp_dir_abs_path) temp_dir_name = tmp_dir_abs_path - temp_file_name = os.path.join( + temp_file_path = os.path.join( temp_dir_name, f"temp_mmap_{np.random.randint(2**63, dtype=np.int64)}.hdf" ) - with h5py.File(temp_file_name, "w") as hdf_file: + with h5py.File(temp_file_path, "w") as hdf_file: created_array = hdf_file.create_dataset("array", shape=shape, dtype=dtype) created_array[0] = ( np.string_("") if isinstance(dtype, np.dtypes.StrDType) else 0 ) offset = created_array.id.get_offset() - with open(temp_file_name, "rb+") as raw_hdf_file: + with open(temp_file_path, "rb+") as raw_hdf_file: mmap_obj = mmap.mmap(raw_hdf_file.fileno(), 0, access=mmap.ACCESS_WRITE) return np.frombuffer( From ab4b4a625f391ea1c16314543d90af6939a80d7d Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:44:16 +0100 Subject: [PATCH 08/11] do not log on warning level --- alphabase/io/tempmmap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index ad38a169..337f200b 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -305,7 +305,7 @@ def _clear() -> None: global _TEMP_DIR, TEMP_DIR_NAME if _TEMP_DIR is not None: - logging.warning( + logging.info( f"Temporary folder {TEMP_DIR_NAME} with memory-mapped arrays is being deleted. " "All existing memory-mapped arrays will be unusable!" ) From b8fde42dc389e9a1d8cecf19e259e6597287434b Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:53:40 +0100 Subject: [PATCH 09/11] fix --- alphabase/io/tempmmap.py | 1 + tests/unit/io/test_tempmmap.py | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index 337f200b..6deaee63 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -311,6 +311,7 @@ def _clear() -> None: ) del _TEMP_DIR + _TEMP_DIR = None TEMP_DIR_NAME = None diff --git a/tests/unit/io/test_tempmmap.py b/tests/unit/io/test_tempmmap.py index 1895648e..2e9ab6cd 100644 --- a/tests/unit/io/test_tempmmap.py +++ b/tests/unit/io/test_tempmmap.py @@ -22,9 +22,8 @@ def teardown_function(function): tempmmap = sys.modules["alphabase.io.tempmmap"] tempmmap._clear() # simulating @atexit.register - # # later: - # assert tempmmap._TEMP_DIR is None - # assert tempmmap.TEMP_DIR_NAME is None + assert tempmmap._TEMP_DIR is None + assert tempmmap.TEMP_DIR_NAME is None del sys.modules["alphabase.io.tempmmap"] From 520ccfac5a24012c8d2635e05cacf7412c83afbf Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 21 Jan 2025 08:14:20 +0100 Subject: [PATCH 10/11] make automatic temp dir deletion more obvious (review comments) --- alphabase/io/tempmmap.py | 7 +++++-- tests/unit/io/test_tempmmap.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index 6deaee63..ab882c12 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -310,8 +310,11 @@ def _clear() -> None: "All existing memory-mapped arrays will be unusable!" ) - del _TEMP_DIR - _TEMP_DIR = None + _TEMP_DIR = None # TempDirectory will take care of the cleanup + if os.path.exists(TEMP_DIR_NAME): + logging.info( + f"Temporary folder {TEMP_DIR_NAME} still exists, manual removal necessary." + ) TEMP_DIR_NAME = None diff --git a/tests/unit/io/test_tempmmap.py b/tests/unit/io/test_tempmmap.py index 2e9ab6cd..047426fb 100644 --- a/tests/unit/io/test_tempmmap.py +++ b/tests/unit/io/test_tempmmap.py @@ -45,6 +45,22 @@ def test_create_array(): assert tempmmap._TEMP_DIR is not None +def test_check_temp_dir_deletion(): + """Test that tempdir is deleted at exit.""" + tempmmap = sys.modules["alphabase.io.tempmmap"] + + _ = tempmmap.array((5, 5), np.float32) + temp_dir_name = tempmmap._TEMP_DIR.name + + # check presence of temp dir first + assert os.path.exists(temp_dir_name) + + # when + tempmmap._clear() + + assert not os.path.exists(temp_dir_name) + + def test_create_array_with_custom_temp_dir(): """Test creating and accessing an array with custom temp dir.""" tempmmap = sys.modules["alphabase.io.tempmmap"] From 81a8ca90d49da91432230d53e6f1ecf326935907 Mon Sep 17 00:00:00 2001 From: mschwoerer <82171591+mschwoer@users.noreply.github.com> Date: Tue, 21 Jan 2025 08:15:01 +0100 Subject: [PATCH 11/11] make it a warning --- alphabase/io/tempmmap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alphabase/io/tempmmap.py b/alphabase/io/tempmmap.py index ab882c12..2e9db3a3 100644 --- a/alphabase/io/tempmmap.py +++ b/alphabase/io/tempmmap.py @@ -312,7 +312,7 @@ def _clear() -> None: _TEMP_DIR = None # TempDirectory will take care of the cleanup if os.path.exists(TEMP_DIR_NAME): - logging.info( + logging.warning( f"Temporary folder {TEMP_DIR_NAME} still exists, manual removal necessary." ) TEMP_DIR_NAME = None