Skip to content

Commit a890676

Browse files
authored
Merge pull request #3990 from chrishalcrow/propagate-relative-to
Propagate `relative_to` to `dump_to_pickle`
2 parents 2ed7897 + bc8f051 commit a890676

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/spikeinterface/core/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def dump(self, file_path: Union[str, Path], relative_to=None, folder_metadata=No
699699
if str(file_path).endswith(".json"):
700700
self.dump_to_json(file_path, relative_to=relative_to, folder_metadata=folder_metadata)
701701
elif str(file_path).endswith(".pkl") or str(file_path).endswith(".pickle"):
702-
self.dump_to_pickle(file_path, folder_metadata=folder_metadata)
702+
self.dump_to_pickle(file_path, relative_to=relative_to, folder_metadata=folder_metadata)
703703
else:
704704
raise ValueError("Dump: file must .json or .pkl")
705705

src/spikeinterface/core/tests/test_baserecording.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,38 @@ def test_BaseRecording(create_cache_folder):
346346
assert rec2.get_annotation(annotation_name) == rec_zarr2_loaded.get_annotation(annotation_name)
347347

348348

349+
def test_json_pickle_equivalence(create_cache_folder):
350+
"""
351+
For a json-ifyable recording, the json and pickle outputs created by `dump` should be the same
352+
(except for the probe information). We check this here for a saved-then-loaded recording,
353+
which tests if relative paths are dealt with in the same way.
354+
"""
355+
356+
rec = generate_recording(durations=[1])
357+
cache_folder = create_cache_folder
358+
359+
json_file_path = cache_folder / "recording.json"
360+
pkl_file_path = cache_folder / "recording.pkl"
361+
362+
rec.dump(json_file_path, relative_to=cache_folder)
363+
rec.dump(pkl_file_path, relative_to=cache_folder)
364+
365+
with open(json_file_path, "r") as f:
366+
data_json = json.load(f)
367+
368+
with open(pkl_file_path, "rb") as f:
369+
data_pickle = pickle.load(f)
370+
371+
for key, value in data_json.items():
372+
# skip probe info, since pickle keeps some additional information
373+
if key not in ["properties"]:
374+
if isinstance(value, dict):
375+
for sub_key, sub_value in value.items():
376+
assert np.all(sub_value == data_pickle[key][sub_key])
377+
else:
378+
assert np.all(value == data_pickle[key])
379+
380+
349381
def test_interleaved_probegroups():
350382
recording = generate_recording(durations=[1.0], num_channels=16)
351383

0 commit comments

Comments
 (0)