Skip to content

Commit e07bd51

Browse files
committed
dev: fix tests
1 parent 58dfd9f commit e07bd51

6 files changed

Lines changed: 21 additions & 18 deletions

File tree

src/dartsort/clustering/cluster_util.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ def reorder_by_depth(
332332
geom: np.ndarray | None = None,
333333
centroids: np.ndarray | None = None,
334334
in_place: bool = False,
335-
is_flat: bool = False,
336335
) -> tuple[DARTsortSorting, np.ndarray]:
337336
"""Reorder cluster labels so that centroid depth is increasing
338337
@@ -350,9 +349,7 @@ def reorder_by_depth(
350349
reorder: np.ndarray
351350
reorder[j] is the new label of original unit j.
352351
"""
353-
354-
if not is_flat:
355-
sorting = sorting.flatten(include_gmm_properties=True, in_place=in_place)
352+
sorting = sorting.flatten(include_gmm_properties=True, in_place=in_place)
356353
assert sorting.labels is not None
357354

358355
if geom is None and motion is not None:
@@ -540,9 +537,11 @@ def decrumb_labels(labels: np.ndarray, min_size: int = 5, in_place=False, flatte
540537
The (flattened) decrumbed labels.
541538
"""
542539
units, counts, _ = pos_int_unique_and_counts(labels)
540+
if not units.size:
541+
return labels
543542
all_big = counts.min() >= min_size
544543
flat_ok = (not flatten) or np.array_equal(units, np.arange(len(units)))
545-
if (not units.size) or (all_big and flat_ok):
544+
if all_big and flat_ok:
546545
return labels
547546
remapping = np.full((units.max() + 1,), -1, dtype=labels.dtype)
548547
kept_units = units[counts >= min_size]
@@ -561,7 +560,11 @@ def decrumb(
561560
) -> DARTsortSorting:
562561
assert sorting.labels is not None
563562
units, counts, _ = pos_int_unique_and_counts(sorting.labels)
564-
if (not units.size) or (counts.min() >= min_size):
563+
if not units.size:
564+
return sorting
565+
all_big = counts.min() >= min_size
566+
flat_ok = (not flatten) or np.array_equal(units, np.arange(len(units)))
567+
if all_big and flat_ok:
565568
return sorting
566569

567570
remapping = np.full((units.max() + 1,), -1, dtype=sorting.labels.dtype)

src/dartsort/templates/postprocess_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def estimate_template_library(
178178

179179
# re-order along probe length
180180
if depth_order:
181-
sorting, templates = reorder_by_depth(sorting, templates)
181+
sorting, templates = reorder_templates_by_depth(sorting, templates)
182182

183183
return sorting, ensure_save(templates, template_npz_path)
184184

@@ -336,7 +336,7 @@ def snr_mask(template_data, min_n_spikes=50, min_template_snr=15.0):
336336
return good_templates
337337

338338

339-
def reorder_by_depth(sorting, template_data):
339+
def reorder_templates_by_depth(sorting, template_data):
340340
assert template_data.registered_geom is not None
341341
w = template_data.snrs_by_channel()
342342
w /= w.sum(axis=1, keepdims=True)

src/dartsort/util/data_util.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -936,11 +936,12 @@ def flatten(
936936
old_unique, _, _ = pos_int_unique_and_counts(self.labels)
937937
old_K = old_unique.max() + 1 if old_unique.shape[0] > 0 else 0
938938
if np.array_equal(old_unique, np.arange(old_K)):
939-
return self
940-
941-
new_labels = self.labels if in_place else self.labels.copy()
942-
remap = flatten_remapping(old_unique)
943-
apply_label_remapping_in_place(new_labels, remap)
939+
new_labels = self.labels
940+
remap = np.arange(old_K)
941+
else:
942+
new_labels = self.labels if in_place else self.labels.copy()
943+
remap = flatten_remapping(old_unique)
944+
apply_label_remapping_in_place(new_labels, remap)
944945

945946
keys = ("merged_candidates", "gmm_candidates")
946947
if not include_gmm_properties or not any(hasattr(self, k) for k in keys):
@@ -1038,7 +1039,6 @@ def has_dataset(self, dataset_name: str) -> bool:
10381039
return dataset_name in h5
10391040

10401041
def load_dataset(self, dataset_name: str, sl=()) -> np.ndarray:
1041-
10421042
if dataset_name in self._ephemeral_features:
10431043
return self._ephemeral_features[dataset_name][sl]
10441044
if dataset_name in self._persistent_features:

tests/test_alignment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_denoiser_alignment(align_sim, align_templates):
115115
ci = dartsort.waveform_util.full_channel_index(1, to_torch=True)
116116
rec = align_sim["recording"]
117117
gt_st = align_sim["sorting"]
118-
noise_wfs = gt_st._load_dataset("noise_waveforms")
118+
noise_wfs = gt_st.load_dataset("noise_waveforms")
119119

120120
# an optimal linear denoiser
121121
rolls = (-1, 0, 1)

tests/test_clustering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
refinement_strategies,
1111
)
1212
from dartsort.main import cluster
13-
from dartsort.templates.postprocess_util import reorder_by_depth
13+
from dartsort.templates.postprocess_util import reorder_templates_by_depth
1414
from dartsort.util.internal_config import (
1515
ClusteringConfig,
1616
ClusteringFeaturesConfig,
@@ -208,7 +208,7 @@ def test_reorder_by_depth(simulations, sim_name):
208208
sorting = sim["sorting"]
209209
template_data = sim["templates"]
210210

211-
sorting1, template_data1 = reorder_by_depth(sorting, template_data)
211+
sorting1, template_data1 = reorder_templates_by_depth(sorting, template_data)
212212

213213
assert np.array_equal(sorting.times_samples, sorting1.times_samples)
214214
assert np.array_equal(sorting.channels, sorting1.channels)

tests/test_matching.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def test_no_crumbs(subtests, refractory_sim, method, cd_iter, channel_selection_
185185
np.testing.assert_allclose(gt_up_templates, match_up_templates, atol=2.5e-3)
186186

187187
# difference between upsampling before going to multichan or after...
188-
true_temps_up = gt_sorting._load_dataset("templates_up")
188+
true_temps_up = gt_sorting.load_dataset("templates_up")
189189
true_temps_up = true_temps_up.astype(np.float32)
190190
np.testing.assert_allclose(gt_up_templates, true_temps_up, atol=1e-4)
191191
up_err = np.abs(gt_up_templates - true_temps_up).max() * (1 + 1e-5 + scaling)

0 commit comments

Comments
 (0)