Skip to content

Commit 451571d

Browse files
committed
feat(spike_sorting): update downstream ephys tables ingestion when NO UNITs found
1 parent 463ec52 commit 451571d

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

element_array_ephys/ephys_no_curation.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,16 @@ def make(self, key):
10381038

10391039
if si_sorting_analyzer_dir.exists(): # Read from spikeinterface outputs
10401040
import spikeinterface as si
1041+
from spikeinterface import sorters
1042+
1043+
sorting_file = output_dir / sorter_name / "spike_sorting" / "si_sorting.pkl"
1044+
si_sorting_: si.sorters.BaseSorter = si.load_extractor(
1045+
sorting_file, base_folder=output_dir
1046+
)
1047+
if si_sorting_.unit_ids.size == 0:
1048+
logger.info(f"No units found in {sorting_file}. Skipping Unit ingestion...")
1049+
self.insert1(key)
1050+
return
10411051

10421052
sorting_analyzer = si.load_sorting_analyzer(folder=si_sorting_analyzer_dir)
10431053
si_sorting = sorting_analyzer.sorting
@@ -1241,6 +1251,11 @@ def make(self, key):
12411251
output_dir = find_full_path(get_ephys_root_data_dir(), output_dir)
12421252
sorter_name = clustering_method.replace(".", "_")
12431253

1254+
self.insert1(key)
1255+
if not len(CuratedClustering.Unit & key):
1256+
logger.info(f"No CuratedClustering.Unit found for {key}, skipping Waveform ingestion.")
1257+
return
1258+
12441259
# Get channel and electrode-site mapping
12451260
electrode_query = (EphysRecording.Channel & key).proj(..., "-channel_name")
12461261
channel2electrode_map: dict[int, dict] = {
@@ -1294,7 +1309,6 @@ def yield_unit_waveforms():
12941309
]
12951310

12961311
yield unit_peak_waveform, unit_electrode_waveforms
1297-
12981312
else: # read from kilosort outputs (ecephys pipeline)
12991313
kilosort_dataset = kilosort.Kilosort(output_dir)
13001314

@@ -1394,7 +1408,6 @@ def yield_unit_waveforms():
13941408
yield unit_peak_waveform, unit_electrode_waveforms
13951409

13961410
# insert waveform on a per-unit basis to mitigate potential memory issue
1397-
self.insert1(key)
13981411
for unit_peak_waveform, unit_electrode_waveforms in yield_unit_waveforms():
13991412
if unit_peak_waveform:
14001413
self.PeakWaveform.insert1(unit_peak_waveform, ignore_extra_fields=True)
@@ -1501,6 +1514,11 @@ def make(self, key):
15011514
output_dir = find_full_path(get_ephys_root_data_dir(), output_dir)
15021515
sorter_name = clustering_method.replace(".", "_")
15031516

1517+
self.insert1(key)
1518+
if not len(CuratedClustering.Unit & key):
1519+
logger.info(f"No CuratedClustering.Unit found for {key}, skipping QualityMetrics ingestion.")
1520+
return
1521+
15041522
si_sorting_analyzer_dir = output_dir / sorter_name / "sorting_analyzer"
15051523
if si_sorting_analyzer_dir.exists(): # read from spikeinterface outputs
15061524
import spikeinterface as si
@@ -1556,7 +1574,6 @@ def make(self, key):
15561574
for unit_key in (CuratedClustering.Unit & key).fetch("KEY")
15571575
]
15581576

1559-
self.insert1(key)
15601577
self.Cluster.insert(metrics_list, ignore_extra_fields=True)
15611578
self.Waveform.insert(metrics_list, ignore_extra_fields=True)
15621579

0 commit comments

Comments
 (0)