Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev memoized results #111

Merged
14 changes: 5 additions & 9 deletions element_interface/suite2p_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,6 @@ def __init__(self, suite2p_plane_dir: str):
)
self.creation_time = datetime.fromtimestamp(ops_fp.stat().st_ctime)

iscell_fp = self.fpath / "iscell.npy"
if not iscell_fp.exists():
raise FileNotFoundError(
'No "iscell.npy" found. Invalid suite2p plane folder: {}'.format(
self.fpath
)
)
self.curation_time = datetime.fromtimestamp(iscell_fp.stat().st_ctime)

# -- Initialize attributes --
for s2p_type in _suite2p_ftypes:
setattr(self, "_{}".format(s2p_type), None)
Expand All @@ -160,6 +151,11 @@ def __init__(self, suite2p_plane_dir: str):

# -- load core files --

@property
def curation_time(self):
print("DeprecationWarning: 'curation_time' is deprecated, set to be the same as 'creation time', no longer reliable.")
return self.creation_time

@property
def ops(self):
if self._ops is None:
Expand Down
14 changes: 8 additions & 6 deletions element_interface/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,27 @@ def __exit__(self, *args):
sys.stdout = self._original_stdout


def memoized_result(parameters: dict, output_directory: str):
def memoized_result(uniqueness_dict: dict, output_directory: str):
"""
This is a decorator factory designed to cache the results of a function based on its input parameters and the state of the output directory.
If the function is called with the same parameters and the output files in the directory remain unchanged,
it returns the cached results; otherwise, it executes the function and caches the new results along with metadata.
Conditions for robust usage:
- the "output_directory" is to store exclusively the resulting files generated by this function call only, not a shared space with other functions/processes
- the "parameters" passed to the decorator captures the true and uniqueness of the arguments to be used in the decorated function call

Args:
parameters: parameters that would identify a unique function call
uniqueness_dict: a dictionary that would identify a unique function call
output_directory: directory location for the output files

Returns: a decorator to enable a function call to memoize/cached the resulting files

Conditions for robust usage:
- the "output_directory" is to store exclusively the resulting files generated by this function call only, not a shared space with other functions/processes
- the "parameters" passed to the decorator captures the true and uniqueness of the arguments to be used in the decorated function call
"""

def decorator(func):
def wrapped(*args, **kwargs):
output_dir = _to_Path(output_directory)
input_hash = dict_to_uuid(parameters)
input_hash = dict_to_uuid(uniqueness_dict)
input_hash_fp = output_dir / f".{input_hash}.json"
# check if results already exist (from previous identical run)
output_dir_files_hash = dict_to_uuid(
Expand Down
Loading