Skip to content

Commit 49c7a92

Browse files
committed
Use existing sparsity for unit location + add location with max channel
1 parent 0ae32e7 commit 49c7a92

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

src/spikeinterface/postprocessing/localization_tools.py

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ def compute_monopolar_triangulation(
7676
assert feature in ["ptp", "energy", "peak_voltage"], f"{feature} is not a valid feature"
7777

7878
contact_locations = sorting_analyzer_or_templates.get_channel_locations()
79+
80+
if sorting_analyzer_or_templates.sparsity is None:
81+
sparsity = compute_sparsity(sorting_analyzer_or_templates, method="radius", radius_um=radius_um)
82+
else:
83+
sparsity = sorting_analyzer_or_templates.sparsity
7984

80-
sparsity = compute_sparsity(sorting_analyzer_or_templates, method="radius", radius_um=radius_um)
8185
templates = get_dense_templates_array(
8286
sorting_analyzer_or_templates, return_scaled=get_return_scaled(sorting_analyzer_or_templates)
8387
)
@@ -157,9 +161,13 @@ def compute_center_of_mass(
157161

158162
assert feature in ["ptp", "mean", "energy", "peak_voltage"], f"{feature} is not a valid feature"
159163

160-
sparsity = compute_sparsity(
161-
sorting_analyzer_or_templates, peak_sign=peak_sign, method="radius", radius_um=radius_um
162-
)
164+
if sorting_analyzer_or_templates.sparsity is None:
165+
sparsity = compute_sparsity(
166+
sorting_analyzer_or_templates, peak_sign=peak_sign, method="radius", radius_um=radius_um
167+
)
168+
else:
169+
sparsity = sorting_analyzer_or_templates.sparsity
170+
163171
templates = get_dense_templates_array(
164172
sorting_analyzer_or_templates, return_scaled=get_return_scaled(sorting_analyzer_or_templates)
165173
)
@@ -650,8 +658,59 @@ def get_convolution_weights(
650658
enforce_decrease_shells = numba.jit(enforce_decrease_shells_data, nopython=True)
651659

652660

661+
662+
def compute_location_max_channel(
663+
templates_or_sorting_analyzer: SortingAnalyzer | Templates,
664+
unit_ids=None,
665+
peak_sign: "neg" | "pos" | "both" = "neg",
666+
mode: "extremum" | "at_index" | "peak_to_peak" = "extremum",
667+
) -> np.ndarray:
668+
"""
669+
Localize a unit using max channel.
670+
671+
This use inetrnally get_template_extremum_channel()
672+
673+
674+
Parameters
675+
----------
676+
templates_or_sorting_analyzer : SortingAnalyzer | Templates
677+
A SortingAnalyzer or Templates object
678+
unit_ids: str | int | None
679+
A list of unit_id to restrict the computation
680+
peak_sign : "neg" | "pos" | "both"
681+
Sign of the template to find extremum channels
682+
mode : "extremum" | "at_index" | "peak_to_peak", default: "at_index"
683+
Where the amplitude is computed
684+
* "extremum" : take the peak value (max or min depending on `peak_sign`)
685+
* "at_index" : take value at `nbefore` index
686+
* "peak_to_peak" : take the peak-to-peak amplitude
687+
688+
Returns
689+
-------
690+
unit_location: np.ndarray
691+
2d
692+
"""
693+
extremum_channels_index = get_template_extremum_channel(
694+
templates_or_sorting_analyzer,
695+
peak_sign=peak_sign,
696+
mode=mode,
697+
outputs="index"
698+
)
699+
contact_locations = templates_or_sorting_analyzer.get_channel_locations()
700+
if unit_ids is None:
701+
unit_ids = templates_or_sorting_analyzer.unit_ids
702+
else:
703+
unit_ids = np.asarray(unit_ids)
704+
unit_location = np.zeros((unit_ids.size, 2), dtype="float32")
705+
for i, unit_id in enumerate(unit_ids):
706+
unit_location[i, :] = contact_locations[extremum_channels_index[unit_id]]
707+
708+
return unit_location
709+
710+
653711
_unit_location_methods = {
654712
"center_of_mass": compute_center_of_mass,
655713
"grid_convolution": compute_grid_convolution,
656714
"monopolar_triangulation": compute_monopolar_triangulation,
715+
"max_channel": compute_location_max_channel,
657716
}

src/spikeinterface/postprocessing/tests/test_unit_locations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class TestUnitLocationsExtension(AnalyzerExtensionCommonTestSuite):
1313
dict(method="grid_convolution", radius_um=150, weight_method={"mode": "gaussian_2d"}),
1414
dict(method="monopolar_triangulation", radius_um=150),
1515
dict(method="monopolar_triangulation", radius_um=150, optimizer="minimize_with_log_penality"),
16+
dict(method="max_channel"),
1617
],
1718
)
1819
def test_extension(self, params):

0 commit comments

Comments
 (0)