Skip to content

Commit

Permalink
Merge pull request #99 from sophiamaedler/redefine_tempmmap_location
Browse files Browse the repository at this point in the history
add possibility to redefine path where tempmmap arrays are saved
  • Loading branch information
jalew188 authored May 11, 2023
2 parents 1afe5be + 8343cb1 commit 5835683
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion alphabase/io/tempmmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import mmap
import h5py
import tempfile

import shutil

_TEMP_DIR = tempfile.TemporaryDirectory(prefix="temp_mmap_")
TEMP_DIR_NAME = _TEMP_DIR.name
Expand All @@ -22,6 +22,33 @@
"and might need to be triggered manually!"
)

def redefine_temp_location(path):
"""
Redfine the location where the temp arrays are written to.
Parameters
----------
path : string
Returns
------
str
the location of the new temporary directory.
"""

global _TEMP_DIR, _TEMP_DIR, 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!''')

#cleaup old temporary directory
shutil.rmtree(TEMP_DIR_NAME, ignore_errors = True)
del _TEMP_DIR

#create new tempfile at desired location
_TEMP_DIR = tempfile.TemporaryDirectory(prefix = os.path.join(path, 'temp_mmap'))
TEMP_DIR_NAME = _TEMP_DIR.name
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

def array(shape: tuple, dtype: np.dtype) -> np.ndarray:
"""Create a writable temporary mmapped array.
Expand Down

0 comments on commit 5835683

Please sign in to comment.