Skip to content

Commit fc70547

Browse files
committed
#21 Run qc3 on all test data
1 parent cf9aa1c commit fc70547

File tree

1 file changed

+114
-73
lines changed

1 file changed

+114
-73
lines changed

tests/test_herg_qc.py

+114-73
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,27 @@ def setUp(self):
5656
]
5757

5858
plot_dir = "test_output"
59-
if not os.path.exists(plot_dir):
60-
os.makedirs(plot_dir)
59+
os.makedirs(plot_dir, exist_ok=True)
6160

6261
self.hergqc = hERGQC(
6362
sampling_rate=sampling_rate,
6463
plot_dir=plot_dir,
6564
voltage=self.voltage,
6665
)
6766

67+
def clone_herg_qc(self, plot_dir):
68+
hergqc = copy.deepcopy(self.hergqc)
69+
plot_dir = os.path.join(hergqc.plot_dir, plot_dir)
70+
os.makedirs(plot_dir, exist_ok=True)
71+
hergqc.plot_dir = plot_dir
72+
return hergqc
73+
6874
def test_qc_inputs(self):
6975
self.assertTrue(np.all(np.isfinite(self.voltage)))
7076
self.assertTrue(np.all(np.isfinite(self.times)))
7177

7278
def test_qc1(self):
73-
74-
hergqc = copy.deepcopy(self.hergqc)
75-
plot_dir = os.path.join(hergqc.plot_dir, "test_qc1")
76-
if not os.path.exists(plot_dir):
77-
os.makedirs(plot_dir)
78-
hergqc.plot_dir = plot_dir
79+
hergqc = self.clone_herg_qc("test_qc1")
7980

8081
# qc1 checks that rseal, cm, rseries are within range
8182
rseal_lo, rseal_hi = 1e8, 1e12
@@ -183,27 +184,25 @@ def test_qc1(self):
183184
)
184185

185186
def test_qc2(self):
186-
hergqc = copy.deepcopy(self.hergqc)
187-
plot_dir = os.path.join(hergqc.plot_dir, "test_qc2")
188-
if not os.path.exists(plot_dir):
189-
os.makedirs(plot_dir)
190-
hergqc.plot_dir = plot_dir
187+
hergqc = self.clone_herg_qc("test_qc2")
191188

192189
# qc2 checks that raw and subtracted SNR are above a minimum threshold
193190
test_matrix = [
194-
(10, 8082.1, True),
195-
(1, 74.0, True),
196-
(0.601, 25.1, True),
197-
(0.6, 25.0, False),
198-
(0.5, 16.8, False),
199-
(0.1, 0.5, False),
191+
(10, True, 8082.1),
192+
(1, True, 74.0),
193+
(0.601, True, 25.1),
194+
(0.6, False, 25.0),
195+
(0.599, False, 24.9),
196+
(0.5, False, 16.8),
197+
(0.1, False, 0.5),
200198
]
201199

202-
for i, ex_snr, ex_pass in test_matrix:
200+
for i, ex_pass, ex_snr in test_matrix:
203201
recording = np.asarray([0, 0.1] * (NOISE_LEN // 2) + [i] * 500)
204202
pass_, snr = hergqc.qc2(recording)
205203
self.assertAlmostEqual(
206-
snr, ex_snr, 1, f"QC2: ({i}) {snr} != {ex_snr}")
204+
snr, ex_snr, 1, f"QC2: ({i}) {snr} != {ex_snr}"
205+
)
207206
self.assertEqual(pass_, ex_pass, f"QC2: ({i}) {pass_} != {ex_pass}")
208207

209208
# Test on data
@@ -237,56 +236,115 @@ def test_qc2(self):
237236
)
238237

239238
def test_qc3(self):
240-
hergqc = copy.deepcopy(self.hergqc)
241-
plot_dir = os.path.join(hergqc.plot_dir, "test_qc3")
242-
if not os.path.exists(plot_dir):
243-
os.makedirs(plot_dir)
244-
hergqc.plot_dir = plot_dir
239+
hergqc = self.clone_herg_qc("test_qc3")
245240

246241
# qc3 checks that rmsd of two sweeps are similar
247242

248243
# Test with same noise, different signal
249244
test_matrix = [
250-
(-10, False),
251-
(-9, False),
252-
(-8, False), # rmsdc - rmsd = -0.6761186497920804
253-
(-7, True), # rmsdc - rmsd = 0.25355095037585507
254-
(0, True), # rmsdc - rmsd = 6.761238263598085
255-
(8, True), # rmsdc - rmsd = 0.6761272774054383
256-
(9, False), # rmsdc - rmsd = -0.08451158778363332
257-
(10, False),
245+
(-10, False, -2.5),
246+
(-9, False, -1.6),
247+
(-8, False, -0.7),
248+
(-7, True, 0.3),
249+
(0, True, 6.8),
250+
(8, True, 0.68),
251+
(9, False, -0.08),
252+
(10, False, -0.8),
258253
]
259254

260255
recording1 = np.asarray([0, 0.1] * (NOISE_LEN // 2) + [40] * 500)
261-
for i, expected in test_matrix:
256+
for i, ex_pass, ex_rmsd_diff in test_matrix:
262257
recording2 = np.asarray(
263-
[0, 0.1] * (NOISE_LEN // 2) + [40 + i] * 500)
264-
result = hergqc.qc3(recording1, recording2)
265-
self.assertEqual(result[0], expected, f"({i}: {result[1]})")
258+
[0, 0.1] * (NOISE_LEN // 2) + [40 + i] * 500
259+
)
260+
pass_, rmsd_diff = hergqc.qc3(recording1, recording2)
261+
self.assertAlmostEqual(
262+
rmsd_diff,
263+
ex_rmsd_diff,
264+
1,
265+
f"QC3: ({i}) {rmsd_diff} != {ex_rmsd_diff}",
266+
)
267+
self.assertEqual(pass_, ex_pass, f"QC3: ({i}) {pass_} != {ex_pass}")
266268

267269
# Test with same signal, different noise
268-
# TODO: Find failing example
269270
test_matrix = [
270-
(10, True),
271-
(100, True),
272-
(1000, True),
271+
(-100, True, 11.3),
272+
(-10, True, 6.3),
273+
(-1, True, 6.7),
274+
(0, True, 6.7),
275+
(1, True, 6.8),
276+
(10, True, 6.4),
277+
(100, True, 11.4),
273278
]
274279

275280
recording1 = np.asarray([0, 0.1] * (NOISE_LEN // 2) + [40] * 500)
276-
for i, expected in test_matrix:
281+
for i, ex_pass, ex_rmsd_diff in test_matrix:
277282
recording2 = np.asarray(
278-
[0, 0.1 * i] * (NOISE_LEN // 2) + [40] * 500)
279-
result = hergqc.qc3(recording1, recording2)
280-
self.assertEqual(result[0], expected, f"({i}: {result[1]})")
283+
[0, 0.1 * i] * (NOISE_LEN // 2) + [40] * 500
284+
)
285+
pass_, rmsd_diff = hergqc.qc3(recording1, recording2)
286+
self.assertAlmostEqual(
287+
rmsd_diff,
288+
ex_rmsd_diff,
289+
1,
290+
f"QC3: ({i}) {rmsd_diff} != {ex_rmsd_diff}",
291+
)
292+
self.assertEqual(pass_, ex_pass, f"QC3: ({i}) {pass_} != {ex_pass}")
281293

282-
# TODO: Test on select data
294+
# Test on data
295+
failed_wells_raw = [
296+
'A21', 'B05', 'B10', 'C19', 'E09', 'E19', 'F22', 'F23', 'I06', 'K23',
297+
'L09', 'M05', 'M06', 'M10', 'N12', 'N17', 'O13', 'O15', 'P11'
298+
]
299+
300+
failed_wells_E4031 = ['A05', 'A07', 'C19', 'E19', 'J16']
301+
302+
failed_wells_subtracted = [
303+
'A05', 'A20', 'A21', 'A24', 'B05', 'B07', 'B10', 'B15', 'B21', 'B23',
304+
'C04', 'C12', 'C14', 'C17', 'C18', 'C19', 'C20', 'D21', 'E04', 'E09',
305+
'E10', 'E15', 'E16', 'E17', 'E18', 'E19', 'E23', 'F04', 'F06', 'F07',
306+
'F12', 'F20', 'F21', 'G08', 'G09', 'G10', 'G12', 'G13', 'G16', 'G20',
307+
'G23', 'G24', 'H03', 'H07', 'H15', 'H19', 'H21', 'H24', 'I04', 'I05',
308+
'I12', 'I17', 'I21', 'J07', 'J16', 'J19', 'K02', 'K06', 'K22', 'K23',
309+
'L01', 'L05', 'L08', 'L09', 'L10', 'L11', 'L13', 'L24', 'M01', 'M02',
310+
'M04', 'M05', 'M06', 'M10', 'M12', 'M21', 'N04', 'N06', 'N08', 'N11',
311+
'N12', 'N17', 'N20', 'N24', 'O01', 'O03', 'O07', 'O10', 'O13', 'O15',
312+
'O19', 'O22', 'P01', 'P03', 'P06', 'P08', 'P12', 'P15', 'P17', 'P18'
313+
]
314+
315+
for well in self.all_wells:
316+
before = np.array(self.trace_sweeps_before[well])
317+
after = np.array(self.trace_sweeps_after[well])
318+
319+
pass_raw, rmsd_diff_raw = hergqc.qc3(before[0, :], before[1, :])
320+
ex_pass_raw = well not in failed_wells_raw
321+
self.assertEqual(
322+
pass_raw,
323+
ex_pass_raw,
324+
f"QC3: {well} (raw) {rmsd_diff_raw}",
325+
)
326+
327+
pass_E4031, rmsd_diff_E4031 = hergqc.qc3(after[0, :], after[1, :])
328+
ex_pass_E4031 = well not in failed_wells_E4031
329+
self.assertEqual(
330+
pass_E4031,
331+
ex_pass_E4031,
332+
f"QC3: {well} (E4031) {rmsd_diff_E4031}",
333+
)
334+
335+
pass_subtracted, rmsd_diff_subtracted = hergqc.qc3(
336+
before[0, :] - after[0, :],
337+
before[1, :] - after[1, :],
338+
)
339+
ex_pass_subtracted = well not in failed_wells_subtracted
340+
self.assertEqual(
341+
pass_subtracted,
342+
ex_pass_subtracted,
343+
f"QC3: {well} (subtracted) {rmsd_diff_subtracted}",
344+
)
283345

284346
def test_qc4(self):
285-
hergqc = copy.deepcopy(self.hergqc)
286-
plot_dir = os.path.join(hergqc.plot_dir, "test_qc4")
287-
if not os.path.exists(plot_dir):
288-
os.makedirs(plot_dir)
289-
hergqc.plot_dir = plot_dir
347+
hergqc = self.clone_herg_qc("test_qc4")
290348

291349
# qc4 checks that rseal, cm, rseries are similar before/after E-4031 change
292350
r_lo, r_hi = 1e6, 3e7
@@ -373,11 +431,7 @@ def test_qc4(self):
373431
# TODO: Test on select data
374432

375433
def test_qc5(self):
376-
hergqc = copy.deepcopy(self.hergqc)
377-
plot_dir = os.path.join(hergqc.plot_dir, "test_qc5")
378-
if not os.path.exists(plot_dir):
379-
os.makedirs(plot_dir)
380-
hergqc.plot_dir = plot_dir
434+
hergqc = self.clone_herg_qc("test_qc5")
381435

382436
# qc5 checks that the maximum current during the second half of the
383437
# staircase changes by at least 75% of the raw trace after E-4031 addition
@@ -401,11 +455,7 @@ def test_qc5(self):
401455
# TODO: Test on select data
402456

403457
def test_qc5_1(self):
404-
hergqc = copy.deepcopy(self.hergqc)
405-
plot_dir = os.path.join(hergqc.plot_dir, "test_qc5_1")
406-
if not os.path.exists(plot_dir):
407-
os.makedirs(plot_dir)
408-
hergqc.plot_dir = plot_dir
458+
hergqc = self.clone_herg_qc("test_qc5_1")
409459

410460
# qc5_1 checks that the RMSD to zero of staircase protocol changes
411461
# by at least 50% of the raw trace after E-4031 addition.
@@ -430,11 +480,7 @@ def test_qc5_1(self):
430480
# TODO: Test on select data
431481

432482
def test_qc6(self):
433-
hergqc = copy.deepcopy(self.hergqc)
434-
plot_dir = os.path.join(hergqc.plot_dir, "test_qc6")
435-
if not os.path.exists(plot_dir):
436-
os.makedirs(plot_dir)
437-
hergqc.plot_dir = plot_dir
483+
hergqc = self.clone_herg_qc("test_qc6")
438484

439485
# qc6 checks that the first step up to +40 mV, before the staircase, in
440486
# the subtracted trace is bigger than -2 x estimated noise level.
@@ -458,12 +504,7 @@ def test_qc6(self):
458504

459505
def test_run_qc(self):
460506
# Spot check a few wells; could check all, but it's time consuming.
461-
462-
hergqc = copy.deepcopy(self.hergqc)
463-
plot_dir = os.path.join(hergqc.plot_dir, "test_run_qc")
464-
if not os.path.exists(plot_dir):
465-
os.makedirs(plot_dir)
466-
hergqc.plot_dir = plot_dir
507+
hergqc = self.clone_herg_qc("test_run_qc")
467508

468509
test_matrix = [
469510
("A01", True),

0 commit comments

Comments
 (0)