`plot_isi_distribution` adds the per-segment densities with several segments

Open Beginner friendly
#5,008 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
matplotlib, numpy, python

Research direction

Start with widgets/isi_distribution.py and inspect the loop that computes and combines each segment’s histogram. The issue suggests adding counts with density=False and normalizing once after the loop; use the two supplied examples to check that the total area is 1 and that a one-spike segment leaves the bars finite. The reporter offers to add a test, but no test file is named.

Written by the indexing model from the issue text.

Description

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

With several segments, the histogram of each segment is computed with density=True and the densities are added (there is a TODO about this in the loop). The area is then the number of segments, and a segment with 10 spikes weighs as much as one with 5000, without an error. If one segment has a single spike, all the bars of the unit are NaN.

import matplotlib

matplotlib.use("Agg")
import numpy as np
from spikeinterface.core import NumpySorting
from spikeinterface.widgets import plot_isi_distribution

rng = np.random.default_rng(0)
seg0 = np.cumsum(rng.integers(150, 600, 5000))  # 5000 spikes, ISI 5-20 ms
seg1 = np.cumsum(rng.integers(2100, 2700, 10))  # 10 spikes, ISI 70-90 ms
sorting = NumpySorting.from_unit_dict([{0: seg0}, {0: seg1}], sampling_frequency=30000.0)

w = plot_isi_distribution(sorting, window_ms=100.0, bin_ms=1.0)
heights = np.array([bar.get_height() for bar in w.axes.flatten()[0].patches])
print("area:", heights.sum() * 1.0)
# area: 2.0  expected 1.0
print("fraction of ISI in 70-90 ms:", heights[70:90].sum() / heights.sum())
# fraction of ISI in 70-90 ms: 0.5  expected about 0.002 (9 of 5008 intervals)

sorting = NumpySorting.from_unit_dict([{0: seg0}, {0: seg1[:1]}], sampling_frequency=30000.0)
w = plot_isi_distribution(sorting, window_ms=100.0, bin_ms=1.0)
heights = np.array([bar.get_height() for bar in w.axes.flatten()[0].patches])
print("finite bars:", np.isfinite(heights).sum(), "of", heights.size)
# finite bars: 0 of 99  expected 99 of 99

Fix: add the counts of all segments with density=False and normalize once after the loop.

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.