Skip to content

Commit c397bff

Browse files
Merge pull request #91 from CardiacModelling/js_fix_subtraction_plots
Simplify subtraction plot code
2 parents af48221 + d0bc8c1 commit c397bff

1 file changed

Lines changed: 11 additions & 41 deletions

File tree

pcpostprocess/subtraction_plots.py

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def do_subtraction_plot(fig, times, sweeps, before_currents, after_currents,
5252
ax.plot(times*1e-3, voltages, color='black')
5353
ax.set_title(f'Well {well}, sweep {sweeps[i]}', fontweight='bold')
5454
ax.tick_params(axis='x', labelbottom=False)
55-
# ax.set_xlabel('time (s)')
55+
5656
protocol_axs[0].set_ylabel(r'$V_\mathrm{cmd}$ (mV)', fontsize=16)
5757

5858
all_leak_params_before = []
@@ -62,32 +62,22 @@ def do_subtraction_plot(fig, times, sweeps, before_currents, after_currents,
6262
style_of_zero = '-'
6363
range_of_zero = [times[0]*1e-3, times[-1]*1e-3]
6464

65-
for i in range(len(sweeps)):
66-
before_params, _ = fit_linear_leak(before_currents[i], voltages, times,
67-
*ramp_bounds)
68-
all_leak_params_before.append(before_params)
69-
70-
after_params, _ = fit_linear_leak(after_currents[i], voltages, times,
71-
*ramp_bounds)
72-
all_leak_params_after.append(after_params)
73-
7465
# Compute and store leak currents
7566
before_leak_currents = np.full((nsweeps, voltages.shape[0]),
7667
np.nan)
7768
after_leak_currents = np.full((nsweeps, voltages.shape[0]),
7869
np.nan)
79-
for i, sweep in enumerate(sweeps):
80-
81-
b0, b1 = all_leak_params_before[i]
82-
gleak = b1
83-
Eleak = -b0/b1
84-
before_leak_currents[i, :] = gleak * (voltages - Eleak)
8570

86-
b0, b1 = all_leak_params_after[i]
87-
gleak = b1
88-
Eleak = -b0/b1
71+
for i in range(len(sweeps)):
72+
before_params, before_leak_current = fit_linear_leak(before_currents[i], voltages, times,
73+
*ramp_bounds)
74+
before_leak_currents[i, :] = before_leak_current
75+
all_leak_params_before.append(before_params)
8976

90-
after_leak_currents[i, :] = gleak * (voltages - Eleak)
77+
after_params, after_leak_current = fit_linear_leak(after_currents[i], voltages, times,
78+
*ramp_bounds)
79+
all_leak_params_after.append(after_params)
80+
after_leak_currents[i, :] = after_leak_current
9181

9282
for i, (sweep, ax) in enumerate(zip(sweeps, before_axs)):
9383
b0, b1 = all_leak_params_before[i]
@@ -101,13 +91,10 @@ def do_subtraction_plot(fig, times, sweeps, before_currents, after_currents,
10191
ax.legend()
10292
ax.tick_params(axis='x', labelbottom=False)
10393

104-
# ax.set_xlabel('time (s)')
10594
before_axs[0].set_ylabel(r'Pre-drug trace', fontsize=16)
106-
# ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
107-
# ax.tick_params(axis='y', rotation=90)
10895

10996
for i, (sweep, ax) in enumerate(zip(sweeps, after_axs)):
110-
b0, b1 = all_leak_params_before[i]
97+
b0, b1 = all_leak_params_after[i]
11198
gleak = b1
11299
Eleak = -b0/b1
113100

@@ -117,11 +104,7 @@ def do_subtraction_plot(fig, times, sweeps, before_currents, after_currents,
117104
ax.plot(range_of_zero, [0, 0], color='black', linestyle=style_of_zero, alpha=alpha_of_zero)
118105
ax.legend()
119106
ax.tick_params(axis='x', labelbottom=False)
120-
121-
# ax.set_xlabel('$t$ (s)')
122107
after_axs[0].set_ylabel(r'Post-drug trace', fontsize=16)
123-
# ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
124-
# ax.tick_params(axis='y', rotation=90)
125108

126109
for i, (sweep, ax) in enumerate(zip(sweeps, corrected_axs)):
127110
corrected_before_currents = before_currents[i, :] - before_leak_currents[i, :]
@@ -134,33 +117,20 @@ def do_subtraction_plot(fig, times, sweeps, before_currents, after_currents,
134117
ax.set_xlabel(r'Time (s)')
135118
ax.legend()
136119
corrected_axs[0].set_ylabel(r'Leak-corrected traces', fontsize=16)
137-
# ax.tick_params(axis='y', rotation=90)
138-
# ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
139120

140121
for i, sweep in enumerate(sweeps):
141-
before_trace = before_currents[i, :].flatten()
142-
after_trace = after_currents[i, :].flatten()
143-
before_params, before_leak = fit_linear_leak(before_trace, voltages, times,
144-
*ramp_bounds)
145-
after_params, after_leak = fit_linear_leak(after_trace, voltages, times,
146-
*ramp_bounds)
147-
148122
subtracted_currents = before_currents[i, :] - before_leak_currents[i, :] - \
149123
(after_currents[i, :] - after_leak_currents[i, :])
150124

151125
subtracted_ax.plot(times*1e-3, subtracted_currents, label=f"sweep {sweep}", alpha=.5)
152126

153-
#  Cycle to next colour
154-
# subtracted_ax.plot([np.nan], [np.nan], label=f"sweep {sweep}", alpha=.5)
155-
156127
subtracted_ax.legend()
157128
subtracted_ax.plot(range_of_zero, [0, 0], color='black',
158129
linestyle=style_of_zero, alpha=alpha_of_zero)
159130
subtracted_ax.set_ylabel('Final subtracted traces', fontsize=16)
160131
subtracted_ax.set_xlabel('Time (s)', fontsize=16)
161132

162133
long_protocol_ax.plot(times*1e-3, voltages, color='black')
163-
# long_protocol_ax.set_xlabel('time (s)')
164134
long_protocol_ax.set_ylabel(r'$V_\mathrm{cmd}$ (mV)', fontsize=16)
165135
long_protocol_ax.tick_params(axis='x', labelbottom=False)
166136

0 commit comments

Comments
 (0)