Skip to content

Commit e044e19

Browse files
committed
Skip saving empty recording files/fields and improve warnings and assertions
1 parent 211d68e commit e044e19

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

src/spikeinterface/core/sortinganalyzer.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ def create(
255255
sparsity=None,
256256
return_scaled=True,
257257
):
258+
assert recording is not None, "To create a SortingAnalyzer you need to specify the recording"
258259
# some checks
259260
if sorting.sampling_frequency != recording.sampling_frequency:
260261
if math.isclose(sorting.sampling_frequency, recording.sampling_frequency, abs_tol=1e-2, rel_tol=1e-5):
@@ -368,7 +369,6 @@ def create_binary_folder(cls, folder, sorting, recording, sparsity, return_scale
368369
json.dump(check_json(info), f, indent=4)
369370

370371
# save a copy of the sorting
371-
# NumpyFolderSorting.write_sorting(sorting, folder / "sorting")
372372
sorting.save(folder=folder / "sorting")
373373

374374
if recording is not None:
@@ -377,16 +377,20 @@ def create_binary_folder(cls, folder, sorting, recording, sparsity, return_scale
377377
recording.dump(folder / "recording.json", relative_to=folder)
378378
elif recording.check_serializability("pickle"):
379379
recording.dump(folder / "recording.pickle", relative_to=folder)
380+
else:
381+
warnings.warn("The Recording is not serializable! The recording link will be lost for future load")
380382
else:
381383
assert rec_attributes is not None, "recording or rec_attributes must be provided"
382-
# write an empty recording.json
383-
with open(folder / "recording.json", mode="w") as f:
384-
json.dump({}, f, indent=4)
384+
warnings.warn("Recording not provided, instntiating SortingAnalyzer in recordingless mode.")
385385

386386
if sorting.check_serializability("json"):
387387
sorting.dump(folder / "sorting_provenance.json", relative_to=folder)
388388
elif sorting.check_serializability("pickle"):
389389
sorting.dump(folder / "sorting_provenance.pickle", relative_to=folder)
390+
else:
391+
warnings.warn(
392+
"The sorting provenance is not serializable! The sorting provenance link will be lost for future load"
393+
)
390394

391395
# dump recording attributes
392396
probegroup = None
@@ -535,13 +539,10 @@ def create_zarr(cls, folder, sorting, recording, sparsity, return_scaled, rec_at
535539
zarr_rec = np.array([rec_dict], dtype=object)
536540
zarr_root.create_dataset("recording", data=zarr_rec, object_codec=numcodecs.Pickle())
537541
else:
538-
warnings.warn(
539-
"SortingAnalyzer with zarr : the Recording is not json serializable, the recording link will be lost for future load"
540-
)
542+
warnings.warn("The Recording is not serializable! The recording link will be lost for future load")
541543
else:
542544
assert rec_attributes is not None, "recording or rec_attributes must be provided"
543-
zarr_rec = np.array([{}], dtype=object)
544-
zarr_root.create_dataset("recording", data=zarr_rec, object_codec=numcodecs.JSON())
545+
warnings.warn("Recording not provided, instntiating SortingAnalyzer in recordingless mode.")
545546

546547
# sorting provenance
547548
sort_dict = sorting.to_dict(relative_to=folder, recursive=True)
@@ -551,9 +552,10 @@ def create_zarr(cls, folder, sorting, recording, sparsity, return_scaled, rec_at
551552
elif sorting.check_serializability("pickle"):
552553
zarr_sort = np.array([sort_dict], dtype=object)
553554
zarr_root.create_dataset("sorting_provenance", data=zarr_sort, object_codec=numcodecs.Pickle())
554-
555-
# else:
556-
# warnings.warn("SortingAnalyzer with zarr : the sorting provenance is not json serializable, the sorting provenance link will be lost for futur load")
555+
else:
556+
warnings.warn(
557+
"The sorting provenance is not serializable! The sorting provenance link will be lost for future load"
558+
)
557559

558560
recording_info = zarr_root.create_group("recording_info")
559561

@@ -614,11 +616,13 @@ def load_from_zarr(cls, folder, recording=None, storage_options=None):
614616

615617
# load recording if possible
616618
if recording is None:
617-
rec_dict = zarr_root["recording"][0]
618-
try:
619-
recording = load_extractor(rec_dict, base_folder=folder)
620-
except:
621-
recording = None
619+
rec_field = zarr_root.get("recording")
620+
if rec_field is not None:
621+
rec_dict = rec_field[0]
622+
try:
623+
recording = load_extractor(rec_dict, base_folder=folder)
624+
except:
625+
recording = None
622626
else:
623627
# TODO maybe maybe not??? : do we need to check attributes match internal rec_attributes
624628
# Note this will make the loading too slow

0 commit comments

Comments
 (0)