`make_residual_recording` subtracts templates in uV from traces in raw units

Open Beginner friendly
#5,005 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
83/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
data

Research direction

Start with benchmark/residual_analysis.py and inspect make_residual_recording, then trace how analyzer templates and recording traces are represented when channel gains differ from 1. Reproduce the supplied gain cases and add a regression test showing the residual standard deviation in uV is consistent across gains. Done when the residual matches the expected 5.05 uV in all three cases.

Written by the indexing model from the issue text.

Description

I found this while reading benchmark/residual_analysis.py, not in my own analyses, so this is a "for the sake of improvement" report.

make_residual_recording takes the templates from the analyzer, in uV by default, and injects them with a negative sign into analyzer.recording, whose traces are in raw units. When the gain is not 1, the residual is wrong, without an error. In the example below the traces in uV are the same for the three gains.

from spikeinterface.core import generate_ground_truth_recording, create_sorting_analyzer
from spikeinterface.preprocessing import scale
from spikeinterface.benchmark import make_residual_recording

recording, sorting = generate_ground_truth_recording(durations=[10.0], num_channels=8, num_units=4, seed=0)
print(f"recording std {recording.get_traces(return_in_uV=True).std():.2f} uV")

for gain in (1.0, 0.195, 2.34):
    # same traces in uV, stored in raw units (uV / gain)
    raw = scale(recording, gain=1 / gain, dtype="float32")
    raw.set_channel_gains(gain)
    raw.set_channel_offsets(0.0)
    analyzer = create_sorting_analyzer(sorting, raw, sparse=False)
    analyzer.compute(["random_spikes", "templates", "noise_levels"])
    analyzer.compute("amplitude_scalings", handle_collisions=False)
    residual = make_residual_recording(analyzer)
    print(f"gain {gain}: residual std {residual.get_traces(return_in_uV=True).std():.2f} uV")

# Output on current main:
# recording std 7.17 uV
# gain 1.0: residual std 5.05 uV
# gain 0.195: residual std 6.48 uV
# gain 2.34: residual std 8.91 uV
# Expected: 5.05 uV for the three gains

With create_sorting_analyzer(..., return_in_uV=False), the residual std is 5.05 uV for the three gains.

Fix: divide the templates by the channel gains before injecting them when they are in uV, or inject them into scale_to_uV(analyzer.recording). The second option changes the dtype and units of the residual, so I leave the choice to you.

I can open a PR with a test if you agree.

Dominant language
Python
Stars
883
Forks
291
Avg merge
3d 17h
Merged PRs (30d)
40

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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.

More from SpikeInterface/spikeinterface

All issues in SpikeInterface/spikeinterface

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.