SpikeInterface / SpikeInterface/spikeinterface

Unexpected: AssertionError: 'inter_sample_shift' is not a property!

Open
#3,628 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
847
Forks
280
Avg merge
3d 9h
Merged PRs (30d)
29

Description

Hello!
Thank you for producing and maintaining such a fabulous piece of software!

Briefly, when I run Kilosort4 via runsorter.run_sorter_local, it complains that

AssertionError: 'inter_sample_shift' is not a property!

even though, on the line immediately preceding this run_sorter command, the following passes:

assert 'inter_sample_shift' in rec.get_property_keys().

I created a minimal example below to reproduce this behavior. The last section contains the error, and everything above is setup. For context, I originally encountered this assert error while preprocessing and motion correcting using spikeinterface, then passing this processed recording to Kilosort4 while skipping preprocessing and motion correction. In the example below, I run Kilosort4 on unprocessed data (which works) just to demonstrate the unexpected behavior.

Thanks for any advice!!

`

Load config file

config_file = '/mnt/lab/users/barnold/spike_sorting/package/spike_sort_config.yaml'
with open(config_file, 'r') as f:
config = yaml.safe_load(f)

Configure probe, specifying geometry of channels and other kwargs

positions = []
for i in range(config['probe']['n_row']):
# add channels by row, 2 channels per row
positions.append([0, i * config['probe']['pitch_row']])
positions.append([config['probe']['pitch_col'], i * config['probe']['pitch_row']])
positions = np.array(positions)

probe = Probe(ndim=config['probe']['n_dim'], si_units=config['probe']['units'])

probe.set_contacts(positions=positions,
shapes=config['probe']['shape'],
shape_params={'width': config['probe']['chan_width']})

probe.set_device_channel_indices(np.arange(config['probe']['n_chan']-1))

probe_kwargs = {'sampling_frequency': 30000.0561519867,
'num_channels': 385,
'dtype': 'int16',
'gain_to_uV': 2.3438,
'offset_to_uV': 0}

Load NP recording, assign probe

NP_file = '/mnt/mscratch1/Tarzan/2024-12-12_12-42-50/2024-12-12_12-43-01/NPElectrophysiology_2_2.bin'
rec = extractors.read_binary(file_paths = NP_file, **probe_kwargs)
rec = rec.select_channels([i for i in range(config['probe']['n_chan']-1)])
rec = rec.set_probe(probe)

set sample_shifts

sample_shifts = extractors.neuropixels_utils.get_neuropixels_sample_shifts(num_channels=rec.get_num_channels(), num_channels_per_adc=12)
rec.set_property('inter_sample_shift', sample_shifts)

take a small sample for testing

start_time_secs, duration_secs = 0,10
rec = rec.frame_slice(start_frame=int(start_time_secs * rec.sampling_frequency), end_frame=int((start_time_secs + duration_secs) * rec.sampling_frequency))

Unexpected AssertionError

this works like a charm

sorting = runsorter.run_sorter_local(sorter_name="kilosort4",
recording=rec,
folder=config['out_dir'],
**config['kilosort'])

do standard preprocessing using spikeinterface's preprocessing module

rec = preprocessing.bandpass_filter(recording=rec,
freq_min=config['ap_freqband']['lower'],
freq_max=config['ap_freqband']['upper'])

bad_channel_ids, channel_labels = preprocessing.detect_bad_channels(rec) # for this step, recording assumed to be filtered
rec = rec.remove_channels(remove_channel_ids=bad_channel_ids)
rec = preprocessing.phase_shift(recording=rec)
rec = preprocessing.common_reference(recording=rec, **config['common_reference'])

this passes

assert 'inter_sample_shift' in rec.get_property_keys()

this does NOT work and fails because of the assert error that just passed in previous line

sorting = runsorter.run_sorter_local(sorter_name="kilosort4",
recording=rec,
folder=config['out_dir'],
**config['kilosort']) `

This is the error message from the last command:

SpikeSortingError: Spike sorting error trace: ... File "/usr/local/lib/python3.9/dist-packages/spikeinterface/preprocessing/phase_shift.py", line 46, in __init__ assert "inter_sample_shift" in recording.get_property_keys(), "'inter_sample_shift' is not a property!" AssertionError: 'inter_sample_shift' is not a property!

Brian

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with preprocessing/phase_shift.py and trace how the recording passed to runsorter.run_sorter_local retains properties after filtering, channel removal, and other preprocessing steps. Reproduce the minimal example and verify that the Kilosort4 run no longer raises the inter_sample_shift assertion while the property remains available.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.