Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 5, 2023
1 parent ba0d29b commit 7db2cb7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
14 changes: 9 additions & 5 deletions src/spikeinterface/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 8 additions & 6 deletions src/spikeinterface/core/core_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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


Expand Down
10 changes: 4 additions & 6 deletions src/spikeinterface/core/tests/test_core_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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")
Expand All @@ -231,14 +230,13 @@ 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

# 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()

0 comments on commit 7db2cb7

Please sign in to comment.