`plot_unit_presence` shows bin indices on the `Time (s)` axis when `time_range` is not given

Open Beginner friendly
#5,006 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
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
matplotlib, python

Research direction

Start with widgets/unit_presence.py, where plot_unit_presence builds the bin counts and calls imshow. Use the two examples in the issue to check the axis limits when time_range is omitted and the IndexError at a 24414.0625 Hz sampling frequency. Done means the axis shows seconds and bin counting uses a consistent sample width without raising the indexing error.

Written by the indexing model from the issue text.

Description

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

Without time_range, no extent is passed to imshow, so the x axis shows bin indices while it is labelled "Time (s)". With the default 0.05 s bins, the times are 20 times too large, without an error. Also, the bin width in samples is truncated with int(bin_duration_s * fs) while the number of bins is computed in seconds, so some sampling frequencies raise an IndexError.

import matplotlib

matplotlib.use("Agg")
from spikeinterface.core import generate_sorting
from spikeinterface.widgets import plot_unit_presence

sorting = generate_sorting(num_units=4, durations=[10.0], sampling_frequency=30000.0, seed=0)
w = plot_unit_presence(sorting)
print(w.ax.get_xlabel(), [float(x) for x in w.ax.get_xlim()])
# Time (s) [-0.5, 198.5]  expected about [0, 10]

sorting = generate_sorting(num_units=4, durations=[60.0], sampling_frequency=24414.0625, seed=0)
plot_unit_presence(sorting)
# IndexError: index 1200 is out of bounds for axis 1 with size 1200

Fix: always pass an extent in seconds to imshow, and compute the number of bins and the bin indices with the same bin width in samples.

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.