Skip to content

Commit 72cb114

Browse files
committed
make sure only valid keys are used in the threshold
1 parent 260d67d commit 72cb114

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/spikeinterface/curation/tests/test_threshold_metrics_curation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,13 @@ def test_threshold_metrics_label_units_missing_metric_raises():
219219
thresholds = {"does_not_exist": {"greater": 0.0}}
220220
with pytest.raises(ValueError, match="specified in thresholds are not present"):
221221
threshold_metrics_label_units(metrics, thresholds)
222+
223+
224+
def test_threshold_metrics_label_units_invalid_threshold_keys_raises():
225+
import pandas as pd
226+
227+
metrics = pd.DataFrame({"m1": [1.0]}, index=[0])
228+
thresholds = {"m1": {"greater": 0.0, "invalid_key": 1.0}}
229+
with pytest.raises(ValueError, match="contains invalid keys"):
230+
threshold_metrics_label_units(metrics, thresholds)
231+
threshold_metrics_label_units(metrics, thresholds)

src/spikeinterface/curation/threshold_metrics_curation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ def threshold_metrics_label_units(
7575
f"Available metrics are: {metrics.columns.tolist()}"
7676
)
7777

78+
# Check that threshold dictionaries contain only valid keys
79+
valid_keys = {"greater", "less", "abs"}
80+
for metric_name, threshold in thresholds_dict.items():
81+
if not set(threshold).issubset(valid_keys):
82+
raise ValueError(
83+
f"Threshold for metric '{metric_name}' contains invalid keys {set(threshold) - valid_keys}."
84+
)
85+
7886
if operator not in ("and", "or"):
7987
raise ValueError("operator must be 'and' or 'or'")
8088

0 commit comments

Comments
 (0)