Blackrock sorting: spike frames are truncated, so ~2% of spikes land one sample early

未关闭
#4,910 2 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 2 天内回复

@h-mayorquin 已经在做这个了。

开始于 2026年11月5日。

评估

这个 Issue 还没有评估数据。

描述

bug extractors

NeoSortingSegment.get_unit_spike_train turns Neo's spike times back into frames with

spike_frames = ((spike_times_seconds - t_start) * self._sampling_frequency).astype("int64")

(src/spikeinterface/extractors/neoextractors/neobaseextractor.py, line 647 on main). For Blackrock, Neo converts the integer .nev ticks to seconds, and this line converts them back. Float round-off leaves some products just under an integer, for example 30936 / 30000 * 30000 == 30935.999999999996, and astype truncates, so those spikes move one sample earlier.

Reproduce with blackrock/blackrock_2_1/l101210-001.nev from ephy_testing_data. Its timestamp resolution is 30 kHz, the same as sampling_frequency, so frames should equal ticks exactly:

import numpy as np
from spikeinterface.extractors import read_blackrock_sorting

sorting = read_blackrock_sorting(file_path="blackrock/blackrock_2_1/l101210-001.nev", sampling_frequency=30000.0)
neo = sorting.neo_reader

diffs, units_off = [], 0
for i, unit_id in enumerate(sorting.unit_ids):
    ticks = neo.get_spike_timestamps(block_index=0, seg_index=0, spike_channel_index=i).astype(np.int64)
    d = sorting.get_unit_spike_train(unit_id) - ticks
    diffs.append(d)
    units_off += bool(np.any(d))
d = np.concatenate(diffs)
values, counts = np.unique(d, return_counts=True)
print(f"spikes: {d.size}, frame - tick: { {int(v): int(c) for v, c in zip(values, counts)} }")
print(f"units with at least one shifted spike: {units_off} of {len(sorting.unit_ids)}")
spikes: 14210, frame - tick: {-1: 272, 0: 13938}
units with at least one shifted spike: 107 of 218

So 272 of 14,210 spikes (1.9%) come out one sample early, and none late. Over tick values 0 to 109,225, 2.3% truncate this way.

Affected readers: those with neo_returns_frames = False, which in 0.105.4 are Blackrock and MEArec. NeuroConv's BlackrockSortingInterface goes through this path, so NWB units tables written from .nev files carry the shift.

Suggested fix:

spike_frames = np.round((spike_times_seconds - t_start) * self._sampling_frequency).astype("int64")

With that change all 14,210 spikes match the .nev ticks exactly.

Environment: Windows 11, Python 3.12.13, spikeinterface 0.105.4 (the line is unchanged on main), neo 0.14.6, numpy 2.5.4.

I found this while checking a NeuroConv conversion against the raw files: https://github.com/JamesBelanger/blackrock-nwb-roundtrip (script: repro/si_spike_truncation.py).

主要语言
Python
星标
883
派生
291
平均合并
3 天 17 小时
30 天内合并 PR
40

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

SpikeInterface/spikeinterface 的其他 Issue

查看 SpikeInterface/spikeinterface 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。