Skip to content

Commit 25d515b

Browse files
committed
small separation
1 parent 89eb899 commit 25d515b

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

src/spikeinterface/comparison/comparisontools.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,23 @@ def make_match_count_matrix(
330330
return match_event_counts_df
331331

332332

333+
def calculate_agreement_scores_with_distance(sorting1, sorting2, delta_frames):
334+
335+
distance_matrix, dot_product_matrix = compute_distance_matrix(
336+
sorting1,
337+
sorting2,
338+
delta_frames,
339+
return_dot_product=True,
340+
)
341+
342+
agreement_matrix = 1 / ((distance_matrix**2 / dot_product_matrix) + 1)
343+
import pandas as pd
344+
345+
agreement_matrix_df = pd.DataFrame(agreement_matrix, index=sorting1.get_unit_ids(), columns=sorting2.get_unit_ids())
346+
347+
return agreement_matrix_df
348+
349+
333350
def make_agreement_scores(
334351
sorting1: BaseSorting,
335352
sorting2: BaseSorting,
@@ -1245,7 +1262,12 @@ def _compute_spike_vector_dot_product(
12451262
return dot_product_matrix
12461263

12471264

1248-
def compute_distance_matrix(sorting1: BaseSorting, sorting2: BaseSorting, delta_frames: int) -> np.ndarray:
1265+
def compute_distance_matrix(
1266+
sorting1: BaseSorting,
1267+
sorting2: BaseSorting,
1268+
delta_frames: int,
1269+
return_dot_product: bool = False,
1270+
) -> np.ndarray:
12491271
"""
12501272
Computes a distance matrix between two sorting objects
12511273
@@ -1264,11 +1286,15 @@ def compute_distance_matrix(sorting1: BaseSorting, sorting2: BaseSorting, delta_
12641286
The second spike train set to compare.
12651287
delta_frames : int
12661288
The frame width to consider in distance calculations.
1267-
1289+
return_dot_product : bool, optional
1290+
If True, the function will return the dot product matrix in addition to the distance matrix. Default is False.
12681291
Returns
12691292
-------
12701293
distance_matrix : (num_units1, num_units2) ndarray (float)
12711294
A matrix representing the pairwise L2 distances between units of sorting objects.
1295+
dot_product_matrix : (num_units1, num_units2) ndarray (float)
1296+
Only returned if `return_dot_product` is True.
1297+
A matrix representing the dot product between units of sorting objects.
12721298
"""
12731299
num_units1 = sorting1.get_num_units()
12741300
num_units2 = sorting2.get_num_units()
@@ -1298,9 +1324,11 @@ def compute_distance_matrix(sorting1: BaseSorting, sorting2: BaseSorting, delta_
12981324
)
12991325

13001326
distance_matrix = np.sqrt(squared_distance_matrix)
1301-
agreement_matrix = 1 / ((distance_matrix**2 / dot_product_matrix) + 1)
13021327

1303-
return distance_matrix, agreement_matrix
1328+
if not return_dot_product:
1329+
return distance_matrix
1330+
else:
1331+
return distance_matrix, dot_product_matrix
13041332

13051333

13061334
def calculate_generalized_comparison_metrics(
@@ -1370,12 +1398,14 @@ def calculate_generalized_comparison_metrics(
13701398
generalized_recall = dot_product / squared_norm1 # Assumes sorting1 is the ground truth
13711399
generalized_precision = dot_product / squared_norm2 # Assumes sorting2 is the sorting that is being evaluated
13721400

1373-
# TODO: Maybe distance should be here? who wants a distance by itself?
1401+
distance = np.sqrt(squared_norm1[:, np.newaxis] + squared_norm2[np.newaxis, :] - 2 * dot_product)
13741402

13751403
metrics = dict(
13761404
accuracy=generalized_accuracy,
13771405
recall=generalized_recall,
13781406
precision=generalized_precision,
13791407
cosine_similarity=cosine_similarity,
1408+
distance=distance,
1409+
dot_product=dot_product,
13801410
)
13811411
return metrics

src/spikeinterface/comparison/paircomparisons.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
do_count_event,
1010
make_match_count_matrix,
1111
make_agreement_scores_from_count,
12+
calculate_agreement_scores_with_distance,
1213
do_score_labels,
1314
do_confusion_matrix,
1415
do_count_score,
1516
compute_performance,
16-
compute_distance_matrix,
1717
)
1818
from ..postprocessing import compute_template_similarity_by_pair
1919

@@ -103,12 +103,11 @@ def _do_agreement(self):
103103
self.match_event_count, self.event_counts1, self.event_counts2
104104
)
105105
elif self.agreement_method == "distance_function":
106-
import pandas as pd
107106

108-
_, agreement_matrix = compute_distance_matrix(self.sorting1, self.sorting2, self.delta_frames)
109-
110-
self.agreement_scores = pd.DataFrame(
111-
agreement_matrix, index=self.sorting1.unit_ids, columns=self.sorting2.unit_ids
107+
self.agreement_scores = calculate_agreement_scores_with_distance(
108+
self.sorting1,
109+
self.sorting2,
110+
self.delta_frames,
112111
)
113112

114113
else:

0 commit comments

Comments
 (0)