55
66
77def setup_subtraction_grid (fig , nsweeps ):
8- # Use 5 x 2 grid when there are 2 sweeps
9- gs = GridSpec (6 , nsweeps , figure = fig )
10-
8+ # Use 6 x 2 grid when there are 2 sweeps
9+ gs = GridSpec (6 , nsweeps , figure = fig , height_ratios = [ 0.7 , 2 , 2 , 2 , 0.7 , 3 ] )
10+ # gs.subplots(sharey='row')
1111 # plot protocol at the top
1212 protocol_axs = [fig .add_subplot (gs [0 , i ]) for i in range (nsweeps )]
1313
@@ -20,18 +20,20 @@ def setup_subtraction_grid(fig, nsweeps):
2020 # Leak corrected traces
2121 corrected_axs = [fig .add_subplot (gs [3 , i ]) for i in range (nsweeps )]
2222
23- # Subtracted traces on one axis
24- subtracted_ax = fig .add_subplot (gs [4 , :])
25-
2623 # Long axis for protocol on the bottom (full width)
27- long_protocol_ax = fig .add_subplot (gs [5 , :])
24+ long_protocol_ax = fig .add_subplot (gs [4 , :])
25+
26+ # Subtracted traces on one axis
27+ subtracted_ax = fig .add_subplot (gs [5 , :])
2828
2929 for ax , cap in zip (list (protocol_axs ) + list (before_axs )
3030 + list (after_axs ) + list (corrected_axs )
31- + [subtracted_ax ] + [long_protocol_ax ],
31+ + [long_protocol_ax ] + [subtracted_ax ],
3232 'abcdefghijklm' ):
3333 ax .spines [['top' , 'right' ]].set_visible (False )
3434 ax .set_title (cap , loc = 'left' , fontweight = 'bold' )
35+ if cap != 'a' :
36+ ax .sharex (protocol_axs [0 ])
3537
3638 return protocol_axs , before_axs , after_axs , corrected_axs , subtracted_ax , long_protocol_ax
3739
@@ -46,100 +48,91 @@ def do_subtraction_plot(fig, times, sweeps, before_currents, after_currents,
4648 protocol_axs , before_axs , after_axs , corrected_axs , \
4749 subtracted_ax , long_protocol_ax = axs
4850
49- for ax in protocol_axs :
51+ for i , ax in enumerate ( protocol_axs ) :
5052 ax .plot (times * 1e-3 , voltages , color = 'black' )
51- ax .set_xlabel ('time (s)' )
52- ax .set_ylabel (r'$V_\mathrm{cmd}$ (mV)' )
53+ ax .set_title (f'Well { well } , sweep { sweeps [i ]} ' , fontweight = 'bold' )
54+ ax .tick_params (axis = 'x' , labelbottom = False )
55+
56+ protocol_axs [0 ].set_ylabel (r'$V_\mathrm{cmd}$ (mV)' , fontsize = 16 )
5357
5458 all_leak_params_before = []
5559 all_leak_params_after = []
56- for i in range (len (sweeps )):
57- before_params , _ = fit_linear_leak (before_currents , voltages , times ,
58- * ramp_bounds )
59- all_leak_params_before .append (before_params )
6060
61- after_params , _ = fit_linear_leak ( after_currents , voltages , times ,
62- * ramp_bounds )
63- all_leak_params_after . append ( after_params )
61+ alpha_of_zero = 0.2
62+ style_of_zero = '-'
63+ range_of_zero = [ times [ 0 ] * 1e-3 , times [ - 1 ] * 1e-3 ]
6464
6565 # Compute and store leak currents
6666 before_leak_currents = np .full ((nsweeps , voltages .shape [0 ]),
6767 np .nan )
6868 after_leak_currents = np .full ((nsweeps , voltages .shape [0 ]),
6969 np .nan )
70- for i , sweep in enumerate (sweeps ):
71-
72- b0 , b1 = all_leak_params_before [i ]
73- gleak = b1
74- Eleak = - b1 / b0
75- before_leak_currents [i , :] = gleak * (voltages - Eleak )
7670
77- b0 , b1 = all_leak_params_after [i ]
78- gleak = b1
79- Eleak = - b1 / b0
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 )
8076
81- 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
8281
8382 for i , (sweep , ax ) in enumerate (zip (sweeps , before_axs )):
84- gleak , Eleak = all_leak_params_before [i ]
85- ax .plot (times * 1e-3 , before_currents [i , :], label = f"pre-drug raw, sweep { sweep } " )
83+ b0 , b1 = all_leak_params_before [i ]
84+ gleak = b1
85+ Eleak = - b0 / b1
86+
87+ ax .plot (times * 1e-3 , before_currents [i , :], label = "Pre-drug raw" )
8688 ax .plot (times * 1e-3 , before_leak_currents [i , :],
87- label = r'$I_\mathrm{L}$.' f"g={ gleak :1E} , E={ Eleak :.1e} " )
88- # ax.legend()
89+ label = f"Fitted leak g={ gleak :7.5g} , E={ Eleak :7.4g} mV" )
90+ ax .plot (range_of_zero , [0 , 0 ], color = 'black' , linestyle = style_of_zero , alpha = alpha_of_zero )
91+ ax .legend ()
92+ ax .tick_params (axis = 'x' , labelbottom = False )
8993
90- if ax .get_legend ():
91- ax .get_legend ().remove ()
92- ax .set_xlabel ('time (s)' )
93- ax .set_ylabel (r'pre-drug trace' )
94- # ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
95- # ax.tick_params(axis='y', rotation=90)
94+ before_axs [0 ].set_ylabel (r'Pre-drug trace' , fontsize = 16 )
9695
9796 for i , (sweep , ax ) in enumerate (zip (sweeps , after_axs )):
98- gleak , Eleak = all_leak_params_before [i ]
99- ax .plot (times * 1e-3 , after_currents [i , :], label = f"post-drug raw, sweep { sweep } " )
97+ b0 , b1 = all_leak_params_after [i ]
98+ gleak = b1
99+ Eleak = - b0 / b1
100+
101+ ax .plot (times * 1e-3 , after_currents [i , :], label = "Post-drug raw" )
100102 ax .plot (times * 1e-3 , after_leak_currents [i , :],
101- label = r"$I_\mathrm{L}$." f"g={ gleak :1E} , E={ Eleak :.1e} " )
102- # ax.legend()
103- if ax .get_legend ():
104- ax .get_legend ().remove ()
105- ax .set_xlabel ('$t$ (s)' )
106- ax .set_ylabel (r'post-drug trace' )
107- # ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e'))
108- # ax.tick_params(axis='y', rotation=90)
103+ label = f"Fitted leak g={ gleak :7.5g} , E={ Eleak :7.4g} mV" )
104+ ax .plot (range_of_zero , [0 , 0 ], color = 'black' , linestyle = style_of_zero , alpha = alpha_of_zero )
105+ ax .legend ()
106+ ax .tick_params (axis = 'x' , labelbottom = False )
107+ after_axs [0 ].set_ylabel (r'Post-drug trace' , fontsize = 16 )
109108
110109 for i , (sweep , ax ) in enumerate (zip (sweeps , corrected_axs )):
111110 corrected_before_currents = before_currents [i , :] - before_leak_currents [i , :]
112111 corrected_after_currents = after_currents [i , :] - after_leak_currents [i , :]
113112 ax .plot (times * 1e-3 , corrected_before_currents ,
114- label = f"leak -corrected pre-drug trace, sweep { sweep } " )
113+ label = "Leak -corrected pre-drug trace" )
115114 ax .plot (times * 1e-3 , corrected_after_currents ,
116- label = f"leak -corrected post-drug trace, sweep { sweep } " )
117- ax .set_xlabel ( r'$t$ (s)' )
118- ax .set_ylabel (r'leak-corrected traces ' )
119- # ax.tick_params(axis='y', rotation=90 )
120- # ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e') )
115+ label = "Leak -corrected post-drug trace" )
116+ ax .plot ( range_of_zero , [ 0 , 0 ], color = 'black' , linestyle = style_of_zero , alpha = alpha_of_zero )
117+ ax .set_xlabel (r'Time (s) ' )
118+ ax .legend ( )
119+ corrected_axs [ 0 ]. set_ylabel ( r'Leak-corrected traces' , fontsize = 16 )
121120
122- ax = subtracted_ax
123121 for i , sweep in enumerate (sweeps ):
124- before_trace = before_currents [i , :].flatten ()
125- after_trace = after_currents [i , :].flatten ()
126- before_params , before_leak = fit_linear_leak (before_trace , voltages , times ,
127- * ramp_bounds )
128- after_params , after_leak = fit_linear_leak (after_trace , voltages , times ,
129- * ramp_bounds )
130-
131122 subtracted_currents = before_currents [i , :] - before_leak_currents [i , :] - \
132123 (after_currents [i , :] - after_leak_currents [i , :])
133- ax .plot (times * 1e-3 , subtracted_currents , label = f"sweep { sweep } " , alpha = .5 )
134124
135- # Cycle to next colour
136- ax .plot ([np .nan ], [np .nan ], label = f"sweep { sweep } " , alpha = .5 )
125+ subtracted_ax .plot (times * 1e-3 , subtracted_currents , label = f"sweep { sweep } " , alpha = .5 )
137126
138- ax .set_ylabel (r'$I_\mathrm{obs} - I_\mathrm{L}$ (mV)' )
139- ax .set_xlabel ('$t$ (s)' )
127+ subtracted_ax .legend ()
128+ subtracted_ax .plot (range_of_zero , [0 , 0 ], color = 'black' ,
129+ linestyle = style_of_zero , alpha = alpha_of_zero )
130+ subtracted_ax .set_ylabel ('Final subtracted traces' , fontsize = 16 )
131+ subtracted_ax .set_xlabel ('Time (s)' , fontsize = 16 )
140132
141133 long_protocol_ax .plot (times * 1e-3 , voltages , color = 'black' )
142- long_protocol_ax .set_xlabel ('time (s)' )
143- long_protocol_ax .set_ylabel (r'$V_\mathrm{cmd}$ (mV)' )
144- long_protocol_ax .tick_params (axis = 'y' , rotation = 90 )
134+ long_protocol_ax .set_ylabel (r'$V_\mathrm{cmd}$ (mV)' , fontsize = 16 )
135+ long_protocol_ax .tick_params (axis = 'x' , labelbottom = False )
136+
137+ fig .align_ylabels ()
145138
0 commit comments