Skip to content

Commit 1399d0c

Browse files
committed
Fix and make more explicit qc thresholding, tests
1 parent 775951e commit 1399d0c

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

ibllib/qc/task_metrics.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,14 @@ def _thresholding(qc_value, thresholds=None):
9696
elif (qc_value > MAX_BOUND) or (qc_value < MIN_BOUND):
9797
raise ValueError("Values out of bound")
9898
else:
99-
passed = qc_value >= np.fromiter(thresholds.values(), dtype=float)
100-
return int(np.argmax(passed))
99+
if 'PASS' in thresholds and qc_value >= thresholds['PASS']:
100+
return 0
101+
elif 'WARNING' in thresholds and qc_value >= thresholds['WARNING']:
102+
return 1
103+
elif 'FAIL' in thresholds and qc_value >= thresholds['FAIL']:
104+
return 2
105+
else:
106+
return -1
101107

102108
def __init__(self, session_path_or_eid, **kwargs):
103109
"""

ibllib/tests/qc/test_task_metrics.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,38 @@
1313

1414

1515
class TestAggregateOutcome(unittest.TestCase):
16-
def test_outcome_from_dict(self):
16+
def test_outcome_from_dict_default(self):
17+
# For a task that has no costume thresholds, default is 0.99 PASS and 0.9 WARNING and 0 FAIL,
18+
# np.nan and None return not set
19+
qc_dict = {'gnap': .99, 'gnop': np.nan, 'gnip': None, 'gnep': 0.9, 'gnup': 0.89}
20+
expect = {'gnap': 'PASS', 'gnop': 'NOT_SET', 'gnip': 'NOT_SET', 'gnep': 'WARNING', 'gnup': 'FAIL'}
21+
outcome, outcome_dict = qcmetrics.TaskQC.compute_session_status_from_dict(qc_dict)
22+
self.assertEqual(outcome, 'FAIL')
23+
self.assertEqual(expect, outcome_dict)
24+
25+
def test_outcome_from_dict_stimFreeze_delays(self):
26+
# For '_task_stimFreeze_delays' the threshold are 0.99 PASS and 0 WARNING
1727
qc_dict = {'gnap': .99, 'gnop': np.nan, '_task_stimFreeze_delays': .1}
18-
expect = {'gnap': 'PASS', 'gnop': 'NOT_SET', '_task_stimFreeze_delays': 'NOT_SET'}
28+
expect = {'gnap': 'PASS', 'gnop': 'NOT_SET', '_task_stimFreeze_delays': 'WARNING'}
29+
outcome, outcome_dict = qcmetrics.TaskQC.compute_session_status_from_dict(qc_dict)
30+
self.assertEqual(outcome, 'WARNING')
31+
self.assertEqual(expect, outcome_dict)
32+
33+
def test_outcome_from_dict_iti_delays(self):
34+
# For '_task_iti_delays' the threshold is 0 NOT_SET
35+
qc_dict = {'gnap': .99, 'gnop': np.nan, '_task_iti_delays': .1}
36+
expect = {'gnap': 'PASS', 'gnop': 'NOT_SET', '_task_iti_delays': 'NOT_SET'}
1937
outcome, outcome_dict = qcmetrics.TaskQC.compute_session_status_from_dict(qc_dict)
2038
self.assertEqual(outcome, 'PASS')
2139
self.assertEqual(expect, outcome_dict)
2240

41+
def test_out_of_bounds(self):
42+
# When qc values are below 0 or above 1, give error
43+
qc_dict = {'gnap': 1.01, 'gnop': 0, 'gnip': 0.99}
44+
with self.assertRaises(ValueError) as e:
45+
outcome, outcome_dict = qcmetrics.TaskQC.compute_session_status_from_dict(qc_dict)
46+
self.assertTrue(e.exception.args[0] == 'Values out of bound')
47+
2348

2449
class TestTaskMetrics(unittest.TestCase):
2550
def setUp(self):

0 commit comments

Comments
 (0)