LorenFrankLab / spyglass

Neuroscience data analysis framework for reproducible research built by Loren Frank Lab at UCSF
https://lorenfranklab.github.io/spyglass/
MIT License
94 stars 43 forks source link

index error during artifact detection #1176

Open MichaelCoulter opened 2 weeks ago

MichaelCoulter commented 2 weeks ago

one of the artifact intervals for our recording includes the last index of the recording and then get_artifact_times gives an IndexError when trying to process this artifact interval. thanks for your help!

we are running this code

nwb_file_name2 = RS2120241016_.nwb
tetrode = 1
interval = 02_r1
for interval in ['02_r1','04_r2','06_r3','08_r4','10_r5']:
    for tetrode in [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]:
        key = {
            "nwb_file_name": nwb_file_name2,
            "sort_group_id": tetrode,
            "interval_list_name": interval,
            "preproc_param_name": "franklab_tetrode_hippocampus",
            "team_name": "mcoulter section",
        }
        sgs.SpikeSortingRecordingSelection.insert_selection(key)
        key = (sgs.SpikeSortingRecordingSelection & key).proj().fetch('recording_id', as_dict=True)
        sgs.SpikeSortingRecording.populate(key)

        key = (
            sgs.SpikeSortingRecordingSelection & {"nwb_file_name": nwb_file_name2} 
            & {"interval_list_name": interval} & {"sort_group_id": tetrode}).fetch1()

        sgs.ArtifactDetectionSelection.insert_selection({"recording_id": key["recording_id"], "artifact_param_name": "100uV_frac_07"})
        sgs.ArtifactDetection.populate(key)

and we get this error

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[12], line 20
     15 key = (
     16     sgs.SpikeSortingRecordingSelection & {"nwb_file_name": nwb_file_name2} 
     17     & {"interval_list_name": interval} & {"sort_group_id": tetrode}).fetch1()
     19 sgs.ArtifactDetectionSelection.insert_selection({"recording_id": key["recording_id"], "artifact_param_name": "100uV_frac_07"})
---> 20 sgs.ArtifactDetection.populate(key)

File ~/spyglass/src/spyglass/utils/dj_mixin.py:612, in SpyglassMixin.populate(self, *restrictions, **kwargs)
    610 if use_transact:  # Pass single-process populate to super
    611     kwargs["processes"] = processes
--> 612     return super().populate(*restrictions, **kwargs)
    613 else:  # No transaction protection, use bare make
    614     for key in keys:

File ~/datajoint-python/datajoint/autopopulate.py:254, in AutoPopulate.populate(self, keys, suppress_errors, return_exception_objects, reserve_jobs, order, limit, max_calls, display_progress, processes, make_kwargs, *restrictions)
    248 if processes == 1:
    249     for key in (
    250         tqdm(keys, desc=self.__class__.__name__)
    251         if display_progress
    252         else keys
    253     ):
--> 254         status = self._populate1(key, jobs, **populate_kwargs)
    255         if status is True:
    256             success_list.append(1)

File ~/datajoint-python/datajoint/autopopulate.py:322, in AutoPopulate._populate1(self, key, jobs, suppress_errors, return_exception_objects, make_kwargs)
    320 self.__class__._allow_insert = True
    321 try:
--> 322     make(dict(key), **(make_kwargs or {}))
    323 except (KeyboardInterrupt, SystemExit, Exception) as error:
    324     try:

File ~/spyglass/src/spyglass/spikesorting/v1/artifact.py:161, in ArtifactDetection.make(self, key)
    156 recording = se.read_nwb_recording(
    157     recording_analysis_nwb_file_abs_path, load_time_vector=True
    158 )
    160 # - detect artifacts
--> 161 artifact_removed_valid_times, _ = _get_artifact_times(
    162     recording,
    163     sort_interval_valid_times,
    164     **artifact_params,
    165 )
    167 # INSERT
    168 # - into IntervalList
    169 IntervalList.insert1(
    170     dict(
    171         nwb_file_name=(
   (...)
    179     skip_duplicates=True,
    180 )

File ~/spyglass/src/spyglass/spikesorting/v1/artifact.py:316, in _get_artifact_times(recording, sort_interval_valid_times, zscore_thresh, amplitude_thresh_uV, proportion_above_thresh, removal_window_ms, verbose, **job_kwargs)
    303 for interval_idx, interval in enumerate(artifact_intervals):
    304     interv_ind = [
    305         np.searchsorted(
    306             valid_timestamps,
   (...)
    312         ),
    313     ]
    314     artifact_intervals_s[interval_idx] = [
    315         valid_timestamps[interv_ind[0]],
--> 316         valid_timestamps[interv_ind[1]],
    317     ]
    319 # make the artifact intervals disjoint
    320 if len(artifact_intervals_s) > 1:

IndexError: index 36926888 is out of bounds for axis 0 with size 36926888