@@ -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+
333350def 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
13061334def 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
0 commit comments