diff --git a/src/spikeinterface/core/base.py b/src/spikeinterface/core/base.py index abaa753c3d..ee7671c6ed 100644 --- a/src/spikeinterface/core/base.py +++ b/src/spikeinterface/core/base.py @@ -15,7 +15,14 @@ import numpy as np from .globals import get_global_tmp_folder, is_set_global_tmp_folder -from .core_tools import check_json, is_dict_extractor, SIJsonEncoder, make_paths_relative, make_paths_absolute, check_paths_relative +from .core_tools import ( + check_json, + is_dict_extractor, + SIJsonEncoder, + make_paths_relative, + make_paths_absolute, + check_paths_relative, +) from .job_tools import _shared_job_kwargs_doc @@ -446,10 +453,9 @@ def to_dict( else: # A warning will be very annoying for end user. # So let's switch back to absolute path, but silently! - # warnings.warn("Try to BaseExtractor.to_dict() using relative_to but there is no common folder") + # warnings.warn("Try to BaseExtractor.to_dict() using relative_to but there is no common folder") dump_dict["relative_paths"] = False - if folder_metadata is not None: if relative_to is not None: folder_metadata = Path(folder_metadata).resolve().absolute().relative_to(relative_to) @@ -1022,8 +1028,6 @@ def save_to_zarr( return cached - - def _load_extractor_from_dict(dic) -> BaseExtractor: """ Convert a dictionary into an instance of BaseExtractor or its subclass. diff --git a/src/spikeinterface/core/core_tools.py b/src/spikeinterface/core/core_tools.py index 294c064cc3..442516dd75 100644 --- a/src/spikeinterface/core/core_tools.py +++ b/src/spikeinterface/core/core_tools.py @@ -851,13 +851,14 @@ def recursive_path_modifier(d, func, target="path", copy=True) -> dict: raise ValueError(f"{k} key for path must be str or list[str]") - def _get_paths_list(d): # this explore a dict and get all paths flatten in a list # the trick is to use a closure func called by recursive_path_modifier() path_list = [] + def append_to_path(p): path_list.append(p) + recursive_path_modifier(d, append_to_path, target="path", copy=True) return path_list @@ -867,10 +868,11 @@ def _relative_to(p, relative_folder): relative_folder = Path(relative_folder).resolve() p = Path(p).resolve() - # the as_posix transform \\ to / on window which make better json files + # the as_posix transform \\ to / on window which make better json files rel_to = os.path.relpath(p.as_posix(), start=relative_folder.as_posix()) return Path(rel_to).as_posix() + def check_paths_relative(input_dict, relative_folder) -> bool: """ Check if relative path is possible to be applied on a dict describing an BaseExtractor. @@ -883,7 +885,7 @@ def check_paths_relative(input_dict, relative_folder) -> bool: A dict describing an extactor obtained by BaseExtractor.to_dict() relative_folder: str or Path The folder to be relative to. - + Returns ------- relative_possible: bool @@ -894,7 +896,7 @@ def check_paths_relative(input_dict, relative_folder) -> bool: for p in path_list: p = Path(p) # check path is not an URL - if 'http' in str(p): + if "http" in str(p): not_possible.append(p) continue @@ -905,14 +907,14 @@ def check_paths_relative(input_dict, relative_folder) -> bool: if p.resolve().absolute().drive != relative_folder.drive: not_possible.append(p) continue - + # check relative is possible try: p2 = _relative_to(p, relative_folder) except ValueError: not_possible.append(p) continue - + return len(not_possible) == 0 diff --git a/src/spikeinterface/core/tests/test_core_tools.py b/src/spikeinterface/core/tests/test_core_tools.py index 7566863c8f..006fae5fb8 100644 --- a/src/spikeinterface/core/tests/test_core_tools.py +++ b/src/spikeinterface/core/tests/test_core_tools.py @@ -187,13 +187,12 @@ def test_path_utils_functions(): d3 = make_paths_relative(d, Path("/yep")) assert d3["kwargs"]["path"] == "sub/path1" - assert d3["kwargs"]["recording"]["kwargs"]["path"] == "sub/path2" + assert d3["kwargs"]["recording"]["kwargs"]["path"] == "sub/path2" d4 = make_paths_absolute(d3, "/yop") assert d4["kwargs"]["path"].startswith("/yop") assert d4["kwargs"]["recording"]["kwargs"]["path"].startswith("/yop") - if platform.system() == "Windows": # test for windows Path d = { @@ -212,7 +211,7 @@ def test_path_utils_functions(): d2 = make_paths_relative(d, "c:\\yep") # the str be must unix like path even on windows for more portability assert d2["kwargs"]["path"] == "sub/path1" - assert d2["kwargs"]["recording"]["kwargs"]["path"] == "sub/path2" + assert d2["kwargs"]["recording"]["kwargs"]["path"] == "sub/path2" # same drive assert check_paths_relative(d, r"c:\yep") @@ -231,7 +230,6 @@ def test_path_utils_functions(): assert check_paths_relative(d, r"\\host\share") - if __name__ == "__main__": # Create a temporary folder using the standard library # import tempfile @@ -239,6 +237,6 @@ def test_path_utils_functions(): # with tempfile.TemporaryDirectory() as tmpdirname: # tmp_path = Path(tmpdirname) # test_write_binary_recording(tmp_path) - # test_write_memory_recording() - + # test_write_memory_recording() + test_path_utils_functions()