|
5 | 5 | import gc |
6 | 6 | import logging |
7 | 7 | import os |
| 8 | +import pickle |
8 | 9 | import time |
9 | 10 | from pathlib import Path |
10 | 11 |
|
|
14 | 15 |
|
15 | 16 | logger = logging.getLogger(__name__) |
16 | 17 |
|
| 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 | + |
17 | 38 | from ..detection.stats import roi_stats |
18 | 39 | from . import utils |
19 | 40 | from .. import default_settings |
@@ -306,7 +327,7 @@ def save_nwb(save_folder): |
306 | 327 | np.load(f.joinpath("settings.npy"), allow_pickle=True).item() for f in plane_folders |
307 | 328 | ] |
308 | 329 | 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 |
310 | 331 | ] |
311 | 332 |
|
312 | 333 | # Load reg_outputs and detect_outputs for background images |
|
0 commit comments