-
Notifications
You must be signed in to change notification settings - Fork 267
Fix channel order bug and make select_channels private
#4712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chrishalcrow
wants to merge
8
commits into
SpikeInterface:main
Choose a base branch
from
chrishalcrow:select-channel-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+223
−7
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b84613b
Fix channel order bug
chrishalcrow f21e73f
add waveforms and pc slicing
chrishalcrow cacbb31
add and fix tests for selected analyzers
chrishalcrow 66396fb
respond to alessio
chrishalcrow c9e1caa
oups in tests
chrishalcrow 9502b82
Move PCA test to postprocessing
chrishalcrow 821aeb6
tweak loop when selecting
chrishalcrow 3132f3b
deal with zero channel case
chrishalcrow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -798,7 +798,7 @@ def test_select_channels(dataset): | |||||
| sorting_analyzer.compute(["random_spikes", "templates", "noise_levels"]) | ||||||
| # select channels | ||||||
| keep_channel_ids = recording.channel_ids[::2] | ||||||
| sorting_analyzer2 = sorting_analyzer.select_channels(channel_ids=keep_channel_ids) | ||||||
| sorting_analyzer2 = sorting_analyzer._select_channels(channel_ids=keep_channel_ids) | ||||||
|
|
||||||
| assert np.array_equal(sorting_analyzer2.channel_ids, keep_channel_ids) | ||||||
| assert np.array_equal(sorting_analyzer2.get_channel_locations(), recording.get_channel_locations(keep_channel_ids)) | ||||||
|
|
@@ -817,6 +817,92 @@ def test_select_channels(dataset): | |||||
| assert len(p) == len(keep_channel_ids) | ||||||
|
|
||||||
|
|
||||||
| def test_select_channels_sparse_waveforms_templates(dataset): | ||||||
| """ | ||||||
| Test that `_select_channels` selects the correct waveforms and templates when the analyzer | ||||||
| is sparse. | ||||||
|
|
||||||
| The actual code uses fancy indexing etc, so this test is designed to _not_ do this, and instead | ||||||
| just loop over all units and channels to check consistency. | ||||||
| """ | ||||||
|
|
||||||
| recording, sorting = dataset | ||||||
| # Make a sparse analyzer | ||||||
| sorting_analyzer = create_sorting_analyzer( | ||||||
| sorting, recording, format="memory", sparse=True, sparsity_kwargs={"method": "radius", "radius_um": 30} | ||||||
| ) | ||||||
| sorting_analyzer.compute(["random_spikes", "waveforms", "templates"]) | ||||||
|
|
||||||
| # Select channels, in a non-monotonic way | ||||||
| select_channel_ids = np.array(["3", "8", "7"]) | ||||||
| analyzer_seleted = sorting_analyzer._select_channels(channel_ids=select_channel_ids) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
:) |
||||||
|
|
||||||
| # Prepare the data | ||||||
| original_id_index_map = dict( | ||||||
| zip(sorting_analyzer.channel_ids, sorting_analyzer.channel_ids_to_indices(sorting_analyzer.channel_ids)) | ||||||
| ) | ||||||
| selected_id_index_map = dict( | ||||||
| zip(analyzer_seleted.channel_ids, analyzer_seleted.channel_ids_to_indices(analyzer_seleted.channel_ids)) | ||||||
| ) | ||||||
|
|
||||||
| original_templates = sorting_analyzer.get_extension("templates").get_data() | ||||||
| selected_templates = analyzer_seleted.get_extension("templates").get_data() | ||||||
|
|
||||||
| original_waveforms = sorting_analyzer.get_extension("waveforms") | ||||||
| selected_waveforms = analyzer_seleted.get_extension("waveforms") | ||||||
|
|
||||||
| for unit_index, unit_id in enumerate(sorting_analyzer.unit_ids): | ||||||
|
|
||||||
| original_units_to_channels = sorting_analyzer.sparsity.unit_id_to_channel_ids[unit_id] | ||||||
| selected_units_to_channels = analyzer_seleted.sparsity.unit_id_to_channel_ids[unit_id] | ||||||
|
|
||||||
| original_waveforms_one_unit = original_waveforms.get_waveforms_one_unit(unit_id) | ||||||
| selected_waveforms_one_unit = selected_waveforms.get_waveforms_one_unit(unit_id) | ||||||
|
|
||||||
| for channel_id in select_channel_ids: | ||||||
| if channel_id in original_units_to_channels: | ||||||
|
|
||||||
| # Check templates, which are dense | ||||||
| original_channel_index = original_id_index_map[channel_id] | ||||||
| selected_channel_index = selected_id_index_map[channel_id] | ||||||
|
|
||||||
| original_channel = original_templates[unit_index, :, original_channel_index] | ||||||
| selected_channel = selected_templates[unit_index, :, selected_channel_index] | ||||||
|
|
||||||
| assert np.all(original_channel == selected_channel) | ||||||
|
|
||||||
| # Now check waveforms and PCs, which are sparse | ||||||
| channel_index_in_original = np.where(original_units_to_channels == channel_id)[0][0] | ||||||
| original_unit_waveform = original_waveforms_one_unit[:, :, channel_index_in_original] | ||||||
|
|
||||||
| channel_index_in_selected = np.where(selected_units_to_channels == channel_id)[0][0] | ||||||
| selected_unit_waveform = selected_waveforms_one_unit[:, :, channel_index_in_selected] | ||||||
|
|
||||||
| assert np.all(original_unit_waveform == selected_unit_waveform) | ||||||
|
|
||||||
|
|
||||||
| def test_select_channels_independent(dataset): | ||||||
| """ | ||||||
| Test that `_select_channels` is independent of channel id order. | ||||||
| """ | ||||||
| recording, sorting = dataset | ||||||
| # Make a sparse analyzer | ||||||
| sorting_analyzer = create_sorting_analyzer( | ||||||
| sorting, recording, format="memory", sparse=True, sparsity_kwargs={"method": "radius", "radius_um": 30} | ||||||
| ) | ||||||
|
|
||||||
| select_channel_ids = np.array(["3", "7", "8"]) | ||||||
| sa_one = sorting_analyzer._select_channels(channel_ids=select_channel_ids) | ||||||
|
|
||||||
| # Make another analyzer with select_channel_ids ['7', '3', '8'] | ||||||
| shuffle_order = np.array([1, 0, 2]) | ||||||
| second_channel = select_channel_ids[shuffle_order] | ||||||
| sa_two = sorting_analyzer._select_channels(channel_ids=second_channel) | ||||||
|
|
||||||
| assert np.all(sa_one.sparsity.mask == sa_two.sparsity.mask[:, shuffle_order]) | ||||||
| assert np.all(sa_one.get_channel_locations() == sa_two.get_channel_locations()[shuffle_order]) | ||||||
|
|
||||||
|
|
||||||
| def test_main_channel_from_templates_dense_recordingless(tmp_path): | ||||||
| """When a dense analyzer has a `templates` extension but no attached recording, `get_main_channels` | ||||||
| recovers each unit's main channel from the templates (its peak channel) and reports it consistently | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not this?
selfis still the old analyzer and it keeps the order, right?