|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from spikeinterface.curation.auto_merge import auto_merge_units |
| 4 | +from spikeinterface.comparison import compare_sorter_to_ground_truth |
| 5 | +from spikeinterface.core.sortinganalyzer import create_sorting_analyzer |
| 6 | +from spikeinterface.widgets import ( |
| 7 | + plot_unit_templates, |
| 8 | + plot_amplitudes, |
| 9 | + plot_crosscorrelograms, |
| 10 | +) |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +from .benchmark_base import Benchmark, BenchmarkStudy |
| 14 | + |
| 15 | + |
| 16 | +class MergingBenchmark(Benchmark): |
| 17 | + |
| 18 | + def __init__(self, recording, splitted_sorting, params, gt_sorting, splitted_cells=None): |
| 19 | + self.recording = recording |
| 20 | + self.splitted_sorting = splitted_sorting |
| 21 | + self.gt_sorting = gt_sorting |
| 22 | + self.splitted_cells = splitted_cells |
| 23 | + self.method_kwargs = params["method_kwargs"] |
| 24 | + self.result = {} |
| 25 | + |
| 26 | + def run(self, **job_kwargs): |
| 27 | + sorting_analyzer = create_sorting_analyzer( |
| 28 | + self.splitted_sorting, self.recording, format="memory", sparse=True, **job_kwargs |
| 29 | + ) |
| 30 | + # sorting_analyzer.compute(['random_spikes', 'templates']) |
| 31 | + # sorting_analyzer.compute('template_similarity', max_lag_ms=0.1, method="l2", **job_kwargs) |
| 32 | + merged_analyzer, self.result["merged_pairs"], self.result["merges"], self.result["outs"] = auto_merge_units( |
| 33 | + sorting_analyzer, extra_outputs=True, **self.method_kwargs, **job_kwargs |
| 34 | + ) |
| 35 | + |
| 36 | + self.result["sorting"] = merged_analyzer.sorting |
| 37 | + |
| 38 | + def compute_result(self, **result_params): |
| 39 | + sorting = self.result["sorting"] |
| 40 | + comp = compare_sorter_to_ground_truth(self.gt_sorting, sorting, exhaustive_gt=True) |
| 41 | + self.result["gt_comparison"] = comp |
| 42 | + |
| 43 | + _run_key_saved = [("sorting", "sorting"), ("merges", "pickle"), ("merged_pairs", "pickle"), ("outs", "pickle")] |
| 44 | + _result_key_saved = [("gt_comparison", "pickle")] |
| 45 | + |
| 46 | + |
| 47 | +class MergingStudy(BenchmarkStudy): |
| 48 | + |
| 49 | + benchmark_class = MergingBenchmark |
| 50 | + |
| 51 | + def create_benchmark(self, key): |
| 52 | + dataset_key = self.cases[key]["dataset"] |
| 53 | + recording, gt_sorting = self.datasets[dataset_key] |
| 54 | + params = self.cases[key]["params"] |
| 55 | + init_kwargs = self.cases[key]["init_kwargs"] |
| 56 | + benchmark = MergingBenchmark(recording, gt_sorting, params, **init_kwargs) |
| 57 | + return benchmark |
| 58 | + |
| 59 | + def get_count_units(self, case_keys=None, well_detected_score=None, redundant_score=None, overmerged_score=None): |
| 60 | + import pandas as pd |
| 61 | + |
| 62 | + if case_keys is None: |
| 63 | + case_keys = list(self.cases.keys()) |
| 64 | + |
| 65 | + if isinstance(case_keys[0], str): |
| 66 | + index = pd.Index(case_keys, name=self.levels) |
| 67 | + else: |
| 68 | + index = pd.MultiIndex.from_tuples(case_keys, names=self.levels) |
| 69 | + |
| 70 | + columns = ["num_gt", "num_sorter", "num_well_detected"] |
| 71 | + comp = self.get_result(case_keys[0])["gt_comparison"] |
| 72 | + if comp.exhaustive_gt: |
| 73 | + columns.extend(["num_false_positive", "num_redundant", "num_overmerged", "num_bad"]) |
| 74 | + count_units = pd.DataFrame(index=index, columns=columns, dtype=int) |
| 75 | + |
| 76 | + for key in case_keys: |
| 77 | + comp = self.get_result(key)["gt_comparison"] |
| 78 | + assert comp is not None, "You need to do study.run_comparisons() first" |
| 79 | + |
| 80 | + gt_sorting = comp.sorting1 |
| 81 | + sorting = comp.sorting2 |
| 82 | + |
| 83 | + count_units.loc[key, "num_gt"] = len(gt_sorting.get_unit_ids()) |
| 84 | + count_units.loc[key, "num_sorter"] = len(sorting.get_unit_ids()) |
| 85 | + count_units.loc[key, "num_well_detected"] = comp.count_well_detected_units(well_detected_score) |
| 86 | + |
| 87 | + if comp.exhaustive_gt: |
| 88 | + count_units.loc[key, "num_redundant"] = comp.count_redundant_units(redundant_score) |
| 89 | + count_units.loc[key, "num_overmerged"] = comp.count_overmerged_units(overmerged_score) |
| 90 | + count_units.loc[key, "num_false_positive"] = comp.count_false_positive_units(redundant_score) |
| 91 | + count_units.loc[key, "num_bad"] = comp.count_bad_units() |
| 92 | + |
| 93 | + return count_units |
| 94 | + |
| 95 | + def plot_agreement_matrix(self, **kwargs): |
| 96 | + from .benchmark_plot_tools import plot_agreement_matrix |
| 97 | + |
| 98 | + return plot_agreement_matrix(self, **kwargs) |
| 99 | + |
| 100 | + def plot_unit_counts(self, case_keys=None, **kwargs): |
| 101 | + from .benchmark_plot_tools import plot_unit_counts |
| 102 | + |
| 103 | + return plot_unit_counts(self, case_keys, **kwargs) |
| 104 | + |
| 105 | + def get_splitted_pairs(self, case_key): |
| 106 | + return self.benchmarks[case_key].splitted_cells |
| 107 | + |
| 108 | + def get_splitted_pairs_index(self, case_key, pair): |
| 109 | + for count, i in enumerate(self.benchmarks[case_key].splitted_cells): |
| 110 | + if i == pair: |
| 111 | + return count |
| 112 | + |
| 113 | + def plot_splitted_amplitudes(self, case_key, pair_index=0, backend="ipywidgets"): |
| 114 | + analyzer = self.get_sorting_analyzer(case_key) |
| 115 | + if analyzer.get_extension("spike_amplitudes") is None: |
| 116 | + analyzer.compute(["spike_amplitudes"]) |
| 117 | + plot_amplitudes(analyzer, unit_ids=self.get_splitted_pairs(case_key)[pair_index], backend=backend) |
| 118 | + |
| 119 | + def plot_splitted_correlograms(self, case_key, pair_index=0, backend="ipywidgets"): |
| 120 | + analyzer = self.get_sorting_analyzer(case_key) |
| 121 | + if analyzer.get_extension("correlograms") is None: |
| 122 | + analyzer.compute(["correlograms"]) |
| 123 | + if analyzer.get_extension("template_similarity") is None: |
| 124 | + analyzer.compute(["template_similarity"]) |
| 125 | + plot_crosscorrelograms(analyzer, unit_ids=self.get_splitted_pairs(case_key)[pair_index]) |
| 126 | + |
| 127 | + def plot_splitted_templates(self, case_key, pair_index=0, backend="ipywidgets"): |
| 128 | + analyzer = self.get_sorting_analyzer(case_key) |
| 129 | + if analyzer.get_extension("spike_amplitudes") is None: |
| 130 | + analyzer.compute(["spike_amplitudes"]) |
| 131 | + plot_unit_templates(analyzer, unit_ids=self.get_splitted_pairs(case_key)[pair_index], backend=backend) |
| 132 | + |
| 133 | + def plot_potential_merges(self, case_key, min_snr=None, backend="ipywidgets"): |
| 134 | + analyzer = self.get_sorting_analyzer(case_key) |
| 135 | + mylist = self.get_splitted_pairs(case_key) |
| 136 | + |
| 137 | + if analyzer.get_extension("spike_amplitudes") is None: |
| 138 | + analyzer.compute(["spike_amplitudes"]) |
| 139 | + if analyzer.get_extension("correlograms") is None: |
| 140 | + analyzer.compute(["correlograms"]) |
| 141 | + |
| 142 | + if min_snr is not None: |
| 143 | + select_from = analyzer.sorting.unit_ids |
| 144 | + if analyzer.get_extension("noise_levels") is None: |
| 145 | + analyzer.compute("noise_levels") |
| 146 | + if analyzer.get_extension("quality_metrics") is None: |
| 147 | + analyzer.compute("quality_metrics", metric_names=["snr"]) |
| 148 | + |
| 149 | + snr = analyzer.get_extension("quality_metrics").get_data()["snr"].values |
| 150 | + select_from = select_from[snr > min_snr] |
| 151 | + mylist_selection = [] |
| 152 | + for i in mylist: |
| 153 | + if (i[0] in select_from) or (i[1] in select_from): |
| 154 | + mylist_selection += [i] |
| 155 | + mylist = mylist_selection |
| 156 | + |
| 157 | + from spikeinterface.widgets import plot_potential_merges |
| 158 | + |
| 159 | + plot_potential_merges(analyzer, mylist, backend=backend) |
| 160 | + |
| 161 | + def plot_performed_merges(self, case_key, backend="ipywidgets"): |
| 162 | + analyzer = self.get_sorting_analyzer(case_key) |
| 163 | + |
| 164 | + if analyzer.get_extension("spike_amplitudes") is None: |
| 165 | + analyzer.compute(["spike_amplitudes"]) |
| 166 | + if analyzer.get_extension("correlograms") is None: |
| 167 | + analyzer.compute(["correlograms"]) |
| 168 | + |
| 169 | + all_merges = list(self.benchmarks[case_key].result["merged_pairs"].values()) |
| 170 | + |
| 171 | + from spikeinterface.widgets import plot_potential_merges |
| 172 | + |
| 173 | + plot_potential_merges(analyzer, all_merges, backend=backend) |
0 commit comments