Skip to content

Commit efeaf92

Browse files
adding handler for posixpath on windows suggested by claude code
1 parent 7c0891d commit efeaf92

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

suite2p/io/nwb.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import gc
66
import logging
77
import os
8+
import pickle
89
import time
910
from pathlib import Path
1011

@@ -14,6 +15,26 @@
1415

1516
logger = logging.getLogger(__name__)
1617

18+
19+
class _CrossPlatformUnpickler(pickle.Unpickler):
20+
"""Unpickler that handles PosixPath/WindowsPath across platforms."""
21+
22+
def find_class(self, module, name):
23+
if name == "PosixPath" or name == "WindowsPath":
24+
return Path
25+
return super().find_class(module, name)
26+
27+
def _load_npy_cross_platform(path):
28+
"""Load a .npy file that may contain Path objects from a different OS."""
29+
with open(path, "rb") as f:
30+
major, _ = np.lib.format.read_magic(f)
31+
read_header = (np.lib.format.read_array_header_1_0 if major == 1
32+
else np.lib.format.read_array_header_2_0)
33+
shape, fortran, dtype = read_header(f)
34+
if dtype.hasobject:
35+
return _CrossPlatformUnpickler(f).load()
36+
return np.load(path, allow_pickle=False)
37+
1738
from ..detection.stats import roi_stats
1839
from . import utils
1940
from .. import default_settings
@@ -306,7 +327,7 @@ def save_nwb(save_folder):
306327
np.load(f.joinpath("settings.npy"), allow_pickle=True).item() for f in plane_folders
307328
]
308329
dbs = [
309-
np.load(f.joinpath("db.npy"), allow_pickle=True).item() for f in plane_folders
330+
_load_npy_cross_platform(f.joinpath("db.npy")).item() for f in plane_folders
310331
]
311332

312333
# Load reg_outputs and detect_outputs for background images

tests/test_io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ def test_nwb_round_trip(data_folder):
250250
("/etc/bla/kjkcc/jodendopn/", "", False),
251251
],
252252
)
253+
253254
def test_get_suite2p_path(input_path, expected_path, success):
254255
if success:
255256
res_path = get_suite2p_path(input_path)

0 commit comments

Comments
 (0)