-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3138 from chrishalcrow/fix-nn-calculations
Fix nn pca_metric computation and update tests
- Loading branch information
Showing
4 changed files
with
109 additions
and
142 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import pytest | ||
|
||
from spikeinterface.core import ( | ||
generate_ground_truth_recording, | ||
create_sorting_analyzer, | ||
) | ||
|
||
|
||
def _small_sorting_analyzer(): | ||
recording, sorting = generate_ground_truth_recording( | ||
durations=[2.0], | ||
num_units=10, | ||
seed=1205, | ||
) | ||
|
||
sorting = sorting.select_units([2, 7, 0], ["#3", "#9", "#4"]) | ||
|
||
sorting_analyzer = create_sorting_analyzer(recording=recording, sorting=sorting, format="memory") | ||
|
||
extensions_to_compute = { | ||
"random_spikes": {"seed": 1205}, | ||
"noise_levels": {"seed": 1205}, | ||
"waveforms": {}, | ||
"templates": {"operators": ["average", "median"]}, | ||
"spike_amplitudes": {}, | ||
"spike_locations": {}, | ||
"principal_components": {}, | ||
} | ||
|
||
sorting_analyzer.compute(extensions_to_compute) | ||
|
||
return sorting_analyzer | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def small_sorting_analyzer(): | ||
return _small_sorting_analyzer() |
This file contains 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
76 changes: 9 additions & 67 deletions
76
src/spikeinterface/qualitymetrics/tests/test_pca_metrics.py
This file contains 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 |
---|---|---|
@@ -1,82 +1,24 @@ | ||
import pytest | ||
from pathlib import Path | ||
import numpy as np | ||
from spikeinterface.core import ( | ||
generate_ground_truth_recording, | ||
create_sorting_analyzer, | ||
) | ||
|
||
|
||
from spikeinterface.qualitymetrics import ( | ||
compute_pc_metrics, | ||
nearest_neighbors_isolation, | ||
nearest_neighbors_noise_overlap, | ||
) | ||
|
||
|
||
job_kwargs = dict(n_jobs=2, progress_bar=True, chunk_duration="1s") | ||
|
||
|
||
def _sorting_analyzer_simple(): | ||
recording, sorting = generate_ground_truth_recording( | ||
durations=[ | ||
50.0, | ||
], | ||
sampling_frequency=30_000.0, | ||
num_channels=6, | ||
num_units=10, | ||
generate_sorting_kwargs=dict(firing_rates=6.0, refractory_period_ms=4.0), | ||
noise_kwargs=dict(noise_levels=5.0, strategy="tile_pregenerated"), | ||
seed=2205, | ||
) | ||
|
||
sorting_analyzer = create_sorting_analyzer(sorting, recording, format="memory", sparse=True) | ||
|
||
sorting_analyzer.compute("random_spikes", max_spikes_per_unit=300, seed=2205) | ||
sorting_analyzer.compute("noise_levels") | ||
sorting_analyzer.compute("waveforms", **job_kwargs) | ||
sorting_analyzer.compute("templates", operators=["average", "std", "median"]) | ||
sorting_analyzer.compute("principal_components", n_components=5, mode="by_channel_local", **job_kwargs) | ||
sorting_analyzer.compute("spike_amplitudes", **job_kwargs) | ||
|
||
return sorting_analyzer | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def sorting_analyzer_simple(): | ||
return _sorting_analyzer_simple() | ||
|
||
|
||
def test_calculate_pc_metrics(sorting_analyzer_simple): | ||
def test_calculate_pc_metrics(small_sorting_analyzer): | ||
import pandas as pd | ||
|
||
sorting_analyzer = sorting_analyzer_simple | ||
res1 = compute_pc_metrics(sorting_analyzer, n_jobs=1, progress_bar=True) | ||
sorting_analyzer = small_sorting_analyzer | ||
res1 = compute_pc_metrics(sorting_analyzer, n_jobs=1, progress_bar=True, seed=1205) | ||
res1 = pd.DataFrame(res1) | ||
|
||
res2 = compute_pc_metrics(sorting_analyzer, n_jobs=2, progress_bar=True) | ||
res2 = compute_pc_metrics(sorting_analyzer, n_jobs=2, progress_bar=True, seed=1205) | ||
res2 = pd.DataFrame(res2) | ||
|
||
for k in res1.columns: | ||
mask = ~np.isnan(res1[k].values) | ||
if np.any(mask): | ||
assert np.array_equal(res1[k].values[mask], res2[k].values[mask]) | ||
|
||
|
||
def test_nearest_neighbors_isolation(sorting_analyzer_simple): | ||
sorting_analyzer = sorting_analyzer_simple | ||
this_unit_id = sorting_analyzer.unit_ids[0] | ||
nearest_neighbors_isolation(sorting_analyzer, this_unit_id) | ||
|
||
|
||
def test_nearest_neighbors_noise_overlap(sorting_analyzer_simple): | ||
sorting_analyzer = sorting_analyzer_simple | ||
this_unit_id = sorting_analyzer.unit_ids[0] | ||
nearest_neighbors_noise_overlap(sorting_analyzer, this_unit_id) | ||
|
||
for metric_name in res1.columns: | ||
if metric_name != "nn_unit_id": | ||
assert not np.all(np.isnan(res1[metric_name].values)) | ||
assert not np.all(np.isnan(res2[metric_name].values)) | ||
|
||
if __name__ == "__main__": | ||
sorting_analyzer = _sorting_analyzer_simple() | ||
test_calculate_pc_metrics(sorting_analyzer) | ||
test_nearest_neighbors_isolation(sorting_analyzer) | ||
test_nearest_neighbors_noise_overlap(sorting_analyzer) | ||
assert np.array_equal(res1[metric_name].values, res2[metric_name].values) |