Skip to content

Conversation

@chrishalcrow
Copy link
Member

@chrishalcrow chrishalcrow commented Apr 1, 2024

Fixes bug in get_traces for common reference recordings with a local reference, where you couldn't select a subset of channels. Noticed it when plotting traces.

Here's minimal code which reproduces the bug

from spikeinterface.core import generate_recording
from spikeinterface.preprocessing import common_reference

recording = generate_recording(durations=[5.0], num_channels=4)
recording_cmr_segment = common_reference(recording, reference="local")._recording_segments[0]

recording_cmr_segment.get_traces(start_frame=0, end_frame=10, channel_indices=[1,2])

This should raises an error.

Also added a test which selects a subset of channels ids, and makes sure it's selected the right one.

@chrishalcrow chrishalcrow added the bug Something isn't working label Apr 1, 2024
@alejoe91
Copy link
Member

alejoe91 commented Apr 1, 2024

Hi Chris, does it fail if you call the recording.get_traces() as well? Or only the segment?

@chrishalcrow
Copy link
Member Author

Hi Chris, does it fail if you call the recording.get_traces() as well? Or only the segment?

Morning! It works for get_traces on the recording. So it's only a problem if some functions (or users) use get_traces on the segments directly (e.g. in plot_traces).

Comment on lines 74 to 82
def test_common_reference_select_channels_local(recording):

recording_cmr = common_reference(recording, reference="local")
recording_segment = recording_cmr._recording_segments[0]

traces_all = recording_segment.get_traces(start_frame=0, end_frame=10, channel_indices=[0, 1, 2, 3])
traces_sub = recording_segment.get_traces(start_frame=0, end_frame=10, channel_indices=[1, 3])

assert np.all(traces_all[:, [1, 3]] == traces_sub)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrishalcrow instead of this "segment" test, I guess you can trigger the same issue by adding this line:

local_trace = recording_local_car.get_traces(channel_ids=["c", "d"])

after L71

I would actually prefer to extend that test instead of adding this new one, since it was designed (badly) to test the slicing behavior. Could you do that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have extended the test to include what you say, and also slicing the segments for each type of common reference. Also made the tests a little harder by choosing ["b", "d"] rather than ["a", "b"]. I also shorted the generated recording from 5s to 1s: I can't see any reason here not to do this and it doubles the speed of the test for me locally.

Copy link
Member

@alejoe91 alejoe91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thansk @chrishalcrow

Check comment!

Comment on lines 192 to 193
re_referenced_traces = np.zeros_like(traces[:, channel_indices])
channel_indices_array = np.arange(traces.shape[1])[channel_indices]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even theses 2 lines looks very very bad. no ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is because channel_indices can be a slice and we need an iterator

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe they look bad, but they're not breaking anything! Is this ultimately due to the difference between channel_ids and channel_indices?

@chrishalcrow chrishalcrow requested a review from alejoe91 April 2, 2024 15:28
Co-authored-by: Alessio Buccino <alejoe9187@gmail.com>
@alejoe91 alejoe91 merged commit 8e10eaf into SpikeInterface:main Apr 3, 2024
alejoe91 added a commit to alejoe91/spikeinterface that referenced this pull request May 10, 2024
@chrishalcrow chrishalcrow deleted the fix_plot_traces_ch_ids branch July 30, 2025 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants