Skip to content

Commit 1ca0655

Browse files
committed
simplify norm calculation
1 parent e5c7af8 commit 1ca0655

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

src/spikeinterface/comparison/comparisontools.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ def compute_dot_product(
218218
):
219219
"""
220220
Computes the dot product between two spike trains.
221-
The dot product in this case is the dot product of the spikes viewed as box-care functions in
221+
The dot product in this case is the dot product of the spikes viewed as box-car functions in
222222
the Hilbert space L2.
223+
223224
The dot product gives a measure of the similarity between two spike trains. Each match is weighted by the
224225
delta_frames - abs(frame1 - frame2) where frame1 and frame2 are the frames of the matching spikes.
225-
When the spike trains are identical, the dot product returns all the matches within the same spike train.
226-
The sum of this dot product is the squared norm of the spike train in the Hilbert space L2.
226+
227227
Parameters
228228
----------
229229
spike_frames_train1 : ndarray
@@ -277,8 +277,8 @@ def compute_dot_product(
277277
# match
278278
unit_index1, unit_index2 = unit_indices1[index1], unit_indices2[index2]
279279

280-
match_weight = delta_frames - abs(frame1 - frame2)
281-
dot_product[unit_index1, unit_index2] += match_weight
280+
weighted_match = delta_frames - abs(frame1 - frame2)
281+
dot_product[unit_index1, unit_index2] += weighted_match
282282

283283
return dot_product
284284

@@ -298,27 +298,30 @@ def get_optimized_compute_norm_function():
298298
def compute_norm(sample_frames, unit_indices, num_units_sorting, delta_frames):
299299
norm_vector = np.zeros(num_units_sorting, dtype=np.uint32)
300300

301-
minimal_search = 0
302301
num_samples = len(sample_frames)
303302
for index1 in range(num_samples):
304303
frame1 = sample_frames[index1]
305304
unit_index1 = unit_indices[index1]
306305

307-
for index2 in range(minimal_search, num_samples):
306+
# Perfect match with itself
307+
# norm_vector[unit_index1] += delta_frames
308+
309+
# Only look ahead
310+
for index2 in range(index1 + 1, num_samples):
308311
frame2 = sample_frames[index2]
309312
unit_index2 = unit_indices[index2]
310313

311314
# Only compare spikes from the same unit
312315
if unit_index1 != unit_index2:
313316
continue
314317

315-
if frame2 < frame1 - delta_frames:
316-
minimal_search += 1
317-
continue
318-
elif frame2 > frame1 + delta_frames:
319-
break
318+
distance = frame2 - frame1 # Only positive here because of looking ahead
319+
if distance <= delta_frames:
320+
weighted_match = delta_frames - distance
321+
# Count one match front and one back
322+
norm_vector[unit_index1] += 2 * weighted_match
320323
else:
321-
norm_vector[unit_index1] += delta_frames - abs(frame1 - frame2)
324+
break
322325

323326
return norm_vector
324327

@@ -379,10 +382,7 @@ def compute_distance_matrix(sorting1, sorting2, delta_frames):
379382
delta_frames,
380383
)
381384

382-
# Now perform the addition
383-
norm_matrix = norm1[:, np.newaxis] + norm2[np.newaxis, :]
384-
385-
distance_matrix += norm_matrix - 2 * dot_product_matrix
385+
distance_matrix += norm1[:, np.newaxis] + norm2[np.newaxis, :] - 2 * dot_product_matrix
386386

387387
return np.sqrt(distance_matrix), dot_product_matrix
388388

0 commit comments

Comments
 (0)