Skip to content

Commit ea1dd80

Browse files
authored
Merge branch 'main' into MichaelClerx-patch-1
2 parents b8ac498 + 46c738d commit ea1dd80

5 files changed

Lines changed: 80 additions & 13 deletions

File tree

pcpostprocess/detect_ramp_bounds.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import numpy as np
22

33

4-
def detect_ramp_bounds(times, voltage_sections, ramp_no=0):
4+
def detect_ramp_bounds(times, voltage_sections, ramp_index=0):
55
"""
6-
Extract the the times at the start and end of the nth ramp in the protocol.
6+
Extract the timepoint indices at the start and end of the nth ramp in the protocol.
77
88
@param times: np.array containing the time at which each sample was taken
99
@param voltage_sections 2d np.array where each row describes a segment of the protocol: (tstart, tend, vstart, end)
10-
@param ramp_no: the index of the ramp to select. Defaults to 0 - the first ramp
10+
@param ramp_index: the index of the ramp to select. Defaults to 0 - the first ramp
1111
12-
@returns tstart, tend: the start and end times for the ramp_no+1^nth ramp
12+
@returns istart, iend: the start and end timepoint indices for the specified ramp
1313
"""
1414

1515
ramps = [(tstart, tend, vstart, vend) for tstart, tend, vstart, vend
1616
in voltage_sections if vstart != vend]
1717
try:
18-
ramp = ramps[ramp_no]
18+
ramp = ramps[ramp_index]
1919
except IndexError:
20-
print(f"Requested {ramp_no+1}th ramp (ramp_no={ramp_no}),"
20+
print(f"Requested {ramp_index+1}th ramp (ramp_index={ramp_index}),"
2121
" but there are only {len(ramps)} ramps")
2222

2323
tstart, tend = ramp[:2]

pcpostprocess/infer_reversal.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ def infer_reversal_potential(current, times, voltage_segments, voltages,
3333
"""
3434

3535
# Get ramp bounds, assuming final ramp is the reversal ramp
36-
tstart, tend = detect_ramp_bounds(times, voltage_segments, -1)
37-
38-
istart = np.argmax(times > tstart)
39-
iend = np.argmax(times > tend)
36+
istart, iend = detect_ramp_bounds(times, voltage_segments, -1)
4037

4138
current = current[istart:iend]
4239
voltages = voltages[istart:iend]

pcpostprocess/scripts/run_herg_qc.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,15 @@ def main():
314314
passed_QC_Erev_spread = E_rev_spread <= args.reversal_spread_threshold
315315
logging.info(f"passed_QC_Erev_spread {passed_QC_Erev_spread}")
316316

317+
# R_leftover only considered for protocols used for QC (i.e. staircase protocols)
318+
passed_QC_R_leftover = np.all(sub_df[sub_df.protocol.isin(args.D2SQC.values())]
319+
["QC.R_leftover"].values
320+
)
321+
322+
logging.info(f"passed_QC_R_leftover {passed_QC_R_leftover}")
323+
324+
passed_QC_Erev_spread = E_rev_spread <= args.reversal_spread_threshold
325+
317326
qc_erev_spread[well] = passed_QC_Erev_spread
318327
erev_spreads[well] = E_rev_spread
319328

tests/test_infer_reversal.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import unittest
4+
5+
from syncropatch_export.trace import Trace
6+
7+
from pcpostprocess import infer_reversal, leak_correct
8+
from pcpostprocess.detect_ramp_bounds import detect_ramp_bounds
9+
10+
11+
class TestInferReversal(unittest.TestCase):
12+
def setUp(self):
13+
test_data_dir = os.path.join('tests', 'test_data', '13112023_MW2_FF',
14+
"staircaseramp (2)_2kHz_15.01.07")
15+
json_file = "staircaseramp (2)_2kHz_15.01.07.json"
16+
17+
self.output_dir = os.path.join('test_output', self.__class__.__name__)
18+
19+
if not os.path.exists(self.output_dir):
20+
os.makedirs(self.output_dir)
21+
22+
self.test_trace = Trace(test_data_dir, json_file)
23+
24+
# get currents and QC from trace object
25+
self.currents = self.test_trace.get_all_traces(leakcorrect=False)
26+
self.currents['times'] = self.test_trace.get_times()
27+
self.currents['voltages'] = self.test_trace.get_voltage()
28+
29+
self.protocol_desc = self.test_trace.get_voltage_protocol().get_all_sections()
30+
self.leak_ramp_bound_indices = detect_ramp_bounds(self.currents['times'],
31+
self.protocol_desc,
32+
ramp_index=0)
33+
34+
self.voltages = self.test_trace.get_voltage()
35+
self.correct_Erev = -89.57184330525791438049054704606533050537109375
36+
37+
def test_plot_leak_fit(self):
38+
well = "A03"
39+
sweep = 0
40+
41+
voltage = self.test_trace.get_voltage()
42+
times = self.test_trace.get_times()
43+
44+
current = self.test_trace.get_trace_sweeps(sweeps=[sweep])[well][0, :]
45+
params, Ileak = leak_correct.fit_linear_leak(current, voltage, times,
46+
*self.leak_ramp_bound_indices,
47+
output_dir=self.output_dir,
48+
save_fname=f"{well}_sweep{sweep}_leak_correction")
49+
50+
I_corrected = current - Ileak
51+
52+
E_rev = infer_reversal.infer_reversal_potential(
53+
I_corrected, times, self.protocol_desc,
54+
self.voltages,
55+
output_path=os.path.join(self.output_dir,
56+
f"{well}_staircase"),
57+
known_Erev=self.correct_Erev)
58+
self.assertLess(abs(E_rev - self.correct_Erev), 1e-5)
59+
60+
61+
if __name__ == "__main__":
62+
pass

tests/test_leak_correct.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ def setUp(self):
1414
"staircaseramp (2)_2kHz_15.01.07")
1515
json_file = "staircaseramp (2)_2kHz_15.01.07.json"
1616

17-
self.output_dir = os.path.join("test_output",
18-
self.__class__.__name__)
17+
self.output_dir = os.path.join('test_output', self.__class__.__name__)
1918

2019
os.makedirs(self.output_dir, exist_ok=True)
2120

@@ -31,7 +30,7 @@ def setUp(self):
3130
# Find first times ahead of these times
3231
voltage_protocol = self.test_trace.get_voltage_protocol().get_all_sections()
3332
times = self.currents['times'].flatten()
34-
self.ramp_bound_indices = detect_ramp_bounds(times, voltage_protocol, ramp_no=0)
33+
self.ramp_bound_indices = detect_ramp_bounds(times, voltage_protocol, ramp_index=0)
3534

3635
def test_plot_leak_fit(self):
3736
well = 'A01'

0 commit comments

Comments
 (0)