-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_trace_class.py
More file actions
executable file
·119 lines (93 loc) · 4.48 KB
/
test_trace_class.py
File metadata and controls
executable file
·119 lines (93 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
import json
import os
import tempfile
import unittest
import numpy as np
from syncropatch_export.trace import Trace
from syncropatch_export.voltage_protocols import VoltageProtocol
class TestTraceClass(unittest.TestCase):
"""
Tests both the Trace and VoltageProtocol classes.
"""
def setUp(self):
f = 'staircaseramp (2)_2kHz_15.01.07'
self.trace = Trace(
os.path.join('tests', 'test_data', '13112023_MW2_FF', f), f)
def test_protocol_descriptions(self):
voltages = self.trace.get_voltage()
times = self.trace.get_times()
protocol_from_json = self.trace.get_voltage_protocol()
holding_potential = protocol_from_json.get_holding_potential()
protocol_desc = VoltageProtocol.from_voltage_trace(voltages, times,
holding_potential)
sections1 = protocol_from_json.get_all_sections()
sections2 = protocol_desc.get_all_sections()
v_error = np.max(np.abs((sections1 - sections2))[:, 2:])
# There may be some extra time on the end of the protocol in the json
t_error = np.max(np.abs((sections1 - sections2))[:-1, :2])
self.assertLess(t_error, 1e-2)
self.assertLess(v_error, 1e-4)
def test_get_protocol_description(self):
a = np.array(self.trace.get_protocol_description())
b = np.array(self.trace.get_voltage_protocol().get_all_sections())
self.assertEqual(a.shape, b.shape)
self.assertTrue(np.all(a == b))
def test_protocol_export(self):
with tempfile.TemporaryDirectory() as d:
protocol = self.trace.get_voltage_protocol()
protocol.export_txt(os.path.join(d, 'protocol.txt'))
json_protocol = self.trace.get_voltage_protocol_json()
with open(os.path.join(d, 'protocol.json'), 'w') as fin:
json.dump(json_protocol, fin)
def test_protocol_timeseries(self):
voltages = self.trace.get_voltage()
times = self.trace.get_times()
voltage_protocol = self.trace.get_voltage_protocol()
def voltage_func(t):
for tstart, tend, vstart, vend in voltage_protocol.get_all_sections():
if t >= tstart and t < tend:
if vstart != vend:
return vstart + (vend - vstart) * (t - tstart) / (tend - tstart)
else:
return vstart
return voltage_protocol.get_holding_potential() # pragma: no cover
for t, v in zip(times, voltages):
self.assertLess(voltage_func(t) - v, 1e-3)
def test_protocol_get_step_start_times(self):
a = list(self.trace.get_voltage_protocol().get_step_start_times())
b = [0, 250, 300, 696, 896, 1896, 2396, 3396, 3896, 4396, 4896, 5396,
5896, 6396, 6896, 7396, 7896, 8396, 8896, 9396, 9896, 10396,
10896, 11396, 11896, 12396, 12896, 13896, 14396, 14406, 14502,
14892]
self.assertEqual(a, b)
def test_protocol_get_ramps(self):
a = np.array(self.trace.get_voltage_protocol().get_ramps())
b = np.array([[300, 696, -120, -80], [14406, 14502, -70, -110]])
self.assertEqual(a.shape, b.shape)
self.assertTrue(np.all(a == b))
def test_get_QC(self):
QC_values = self.trace.get_onboard_QC_values()
self.assertGreater(len(QC_values), 0)
def test_get_traces(self):
v = self.trace.get_voltage()
ts = self.trace.get_times()
all_traces = self.trace.get_all_traces(leakcorrect=True)
all_traces = self.trace.get_all_traces()
# TODO: Check the output, numerically, by comparing a few points
self.assertTrue(np.all(np.isfinite(v)))
self.assertTrue(np.all(np.isfinite(ts)))
for well, trace in all_traces.items():
self.assertTrue(np.all(np.isfinite(trace)))
# Test complex sweep selection
a = self.trace.get_trace_sweeps([-1, -2])
b = self.trace.get_trace_sweeps([1, 0])
self.assertEqual(len(a), len(b))
self.assertTrue(np.all(a['A01'] == b['A01']))
# Test asking for non-existent sweeps
self.assertRaisesRegex(ValueError, 'Invalid sweep selection',
self.trace.get_trace_sweeps, [2])
self.assertRaisesRegex(ValueError, 'Invalid sweep selection',
self.trace.get_trace_sweeps, [-3])
if __name__ == '__main__':
unittest.main() # pragma: no cover