Team Ai
Apppublic

opensourcebrain/HodgkinHuxleyTutorial

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
ui_widget.py270 linesDownload Raw Back to Python_HH_version
1import ipywidgets2import numpy as np3import pylab as plt4from traitlets import link5 6#running current (default) or voltage clamp7runMode = 'iclamp'8 9#default input parameters10default_capacitance  = 111default_cond_Na      = 12012default_cond_K       = 3613default_cond_L       = 0.314default_E_Na         = 63.5415default_E_K          = -74.1616default_E_L          = -54.317default_t0           = 018default_tn           = 5019default_deltat       = 0.0120default_ic_amplitude = 2.021default_ic_duration  = 2522default_ic_delay     = 523 24#voltage clamp default values25default_vc_delay         = 1026default_vc_duration      = 3027default_condVoltage   = -6528default_testVoltage   = 1029default_returnVoltage = -6530default_tn_vclamp     = 5031default_deltat_vclamp = 0.000532 33#function to reset input values to default on button click34def resetTodefault(_):35    slider_capacitance.value = default_capacitance36    slider_cond_Na.value     = default_cond_Na37    slider_cond_K.value      = default_cond_K38    slider_cond_L.value      = default_cond_L39    slider_E_Na.value      = default_E_Na40    slider_E_K.value       = default_E_K41    slider_E_L.value       = default_E_L42    time_end.value           = default_tn43    time_step.value          = default_deltat44    slider_amplitude.value   = default_ic_amplitude45    slider_width.value       = default_ic_duration46    slider_translation.value = default_ic_delay47 48    #voltage clamp49    slider_delay.value         = default_vc_delay50    slider_duration.value      = default_vc_duration51    slider_condVoltage.value   = default_condVoltage52    slider_testVoltage.value   = default_testVoltage53    slider_returnVoltage.value = default_returnVoltage54 55    #default runMode56    runMode_togglebtns.value='Current Clamp'57 58def showDefault(response):59    if showValue_togglebtn.value:60        defalultValues.layout.display = ''61    else:62        defalultValues.layout.display = 'none'63 64def runModeChange(c):65 66    global runMode67    if runMode_togglebtns.value=='Current Clamp':68        runMode_iclamp.layout.display = ''69        runMode_vclamp.layout.display = 'none'70        runMode = 'iclamp'71        time_step.value = default_deltat72        time_end.value  = default_tn73 74    else:75        runMode_iclamp.layout.display = 'none'76        runMode_vclamp.layout.display = ''77        runMode = 'vclamp'78        time_end.value = default_tn_vclamp79        time_step.value = default_deltat_vclamp80 81#function to change slider handle colour when move from default82def highlight_slider():83    inputList    = [slider_capacitance, slider_cond_Na, slider_cond_K, slider_cond_L, slider_E_Na, slider_E_K, slider_E_L, slider_amplitude, slider_width, slider_translation,84                    slider_delay, slider_duration, slider_condVoltage, slider_testVoltage, slider_returnVoltage]85    inputDefault = [default_capacitance, default_cond_Na, default_cond_K, default_cond_L, default_E_Na, default_E_K, default_E_L, default_ic_amplitude, default_ic_duration, default_ic_delay,86                    default_vc_delay, default_vc_duration, default_condVoltage, default_testVoltage, default_returnVoltage]87    for l, d in zip(inputList,inputDefault):88        if l.value == d:89            l.style.handle_color = 'white'90        else:91            l.style.handle_color = 'orange'92 93#defining the widgets94#Header or texts as HTMLMath to include symbols95header_capacitance = ipywidgets.HTMLMath(value=r"<b> Membrane Capacitance, \(\mu{F}/cm^2\)</b>")96header_conductance = ipywidgets.HTMLMath(value=r"<b> Maximum Conductances, \(mS/cm^2\)</b>")97header_potential   = ipywidgets.HTMLMath(value=r"<b> Reversal Potentials, \(mV\)</b>")98header_simTime     = ipywidgets.HTMLMath(value=r"<b> Simulation Time, \(ms\)</b>")99header_injCurrent  = ipywidgets.HTMLMath(value=r"<b> Injection Current, \(\mu{A}/cm^2\) (or \(pA\) if cell has area 100 \(\mu{m}^2\))</b>")100header_runMode     = ipywidgets.HTML(value=r"<b>Select Run Mode</b>")101header_vclamp_time = ipywidgets.HTMLMath(value=r"<b> Time, \(ms\)</b>")102header_vclamp_volt = ipywidgets.HTMLMath(value=r"<b> Voltage, \(mV\)</b>")103 104#slider widgets105slider_capacitance = ipywidgets.FloatSlider(value=default_capacitance,min=0,max=3,step=0.1,description='Capacitance',readout=False,continuous_update=False)106slider_cond_Na     = ipywidgets.FloatSlider(value=default_cond_Na,min=0,max=160,step=0.1,description='Sodium',readout=False,continuous_update=False)107slider_cond_K      = ipywidgets.FloatSlider(value=default_cond_K,min=0,max=80,step=0.1,description='Potassium',readout=False,continuous_update=False)108slider_cond_L      = ipywidgets.FloatSlider(value=default_cond_L,min=0,max=1,step=0.1,description='Leak',readout=False,continuous_update=False)109slider_E_Na      = ipywidgets.FloatSlider(value=default_E_Na,min=-100,max=100,step=0.1,description='Sodium',readout=False,continuous_update=False)110slider_E_K       = ipywidgets.FloatSlider(value=default_E_K,min=-100,max=100,step=0.1,description='Potassium',readout=False,continuous_update=False)111slider_E_L       = ipywidgets.FloatSlider(value=default_E_L,min=-100,max=100,step=0.1,description='Leak',readout=False,continuous_update=False)112slider_amplitude   = ipywidgets.FloatSlider(value=default_ic_amplitude,min=-20.0,max=200.0,step=0.1,description='Amplitude',readout=False,continuous_update=False)113slider_width       = ipywidgets.FloatSlider(value=default_ic_duration,min=0,max=500,step=0.1,description='Duration',readout=False,continuous_update=False)114slider_translation = ipywidgets.FloatSlider(value=default_ic_delay,min=0,max=250,step=0.1,description='Time Delay',readout=False,continuous_update=False)115 116#vclamp sliders117slider_delay            = ipywidgets.FloatSlider(value=default_vc_delay         ,min=0,max=250,step=0.1,description='Time Delay',readout=False,continuous_update=False)118slider_duration         = ipywidgets.FloatSlider(value=default_vc_duration      ,min=0,max=500,step=0.1,description='Duration',readout=False,continuous_update=False)119slider_condVoltage      = ipywidgets.FloatSlider(value=default_condVoltage   ,min=-120,max=100,step=1,description='Conditioning',readout=False,continuous_update=False)120slider_testVoltage      = ipywidgets.FloatSlider(value=default_testVoltage   ,min=-120,max=100,step=1,description='Testing',readout=False,continuous_update=False)121slider_returnVoltage    = ipywidgets.FloatSlider(value=default_returnVoltage ,min=-120,max=100,step=1,description='Returning',readout=False,continuous_update=False)122 123#text box widgets124time_end           = ipywidgets.FloatText(value=default_tn,description='Total Time',disabled=False)125time_step          = ipywidgets.FloatText(value=default_deltat,description='Time Step',disabled=False)126 127#text box widgets to link with sliders (included to type in values for slider inputs also)128textBox_capacitance = ipywidgets.FloatText(value=default_capacitance,step=0.1,layout=ipywidgets.Layout(width='10%'))129textBox_cond_Na     = ipywidgets.FloatText(value=default_cond_Na,step=1,layout=ipywidgets.Layout(width='10%'))130textBox_cond_K      = ipywidgets.FloatText(value=default_cond_K,step=1,layout=ipywidgets.Layout(width='10%'))131textBox_cond_L      = ipywidgets.FloatText(value=default_cond_L,step=0.1,layout=ipywidgets.Layout(width='10%'))132textBox_E_Na      = ipywidgets.FloatText(value=default_E_Na,step=1,layout=ipywidgets.Layout(width='10%'))133textBox_E_K       = ipywidgets.FloatText(value=default_E_K,step=1,layout=ipywidgets.Layout(width='10%'))134textBox_E_L       = ipywidgets.FloatText(value=default_E_L,step=1,layout=ipywidgets.Layout(width='10%'))135textBox_amplitude   = ipywidgets.FloatText(value=default_ic_amplitude,step=1,layout=ipywidgets.Layout(width='10%'))136textBox_width       = ipywidgets.FloatText(value=default_ic_duration,step=1,layout=ipywidgets.Layout(width='10%'))137textBox_translation = ipywidgets.FloatText(value=default_ic_delay,step=1,layout=ipywidgets.Layout(width='10%'))138 139#voltage clamp textboxes to link with sliders140textBox_delay         = ipywidgets.FloatText(value=default_vc_delay,step=1,layout=ipywidgets.Layout(width='10%'))141textBox_duration      = ipywidgets.FloatText(value=default_vc_duration,step=1,layout=ipywidgets.Layout(width='10%'))142textBox_condVoltage   = ipywidgets.FloatText(value=default_condVoltage,step=1,layout=ipywidgets.Layout(width='10%'))143textBox_testVoltage   = ipywidgets.FloatText(value=default_testVoltage,step=1,layout=ipywidgets.Layout(width='10%'))144textBox_returnVoltage = ipywidgets.FloatText(value=default_returnVoltage,step=1,layout=ipywidgets.Layout(width='10%'))145 146#linking sliders and textbox for values147link_capacitance = link((slider_capacitance, 'value'), (textBox_capacitance, 'value'))148link_cond_Na     = link((slider_cond_Na, 'value'), (textBox_cond_Na, 'value'))149link_cond_K      = link((slider_cond_K, 'value'), (textBox_cond_K, 'value'))150link_cond_L      = link((slider_cond_L, 'value'), (textBox_cond_L, 'value'))151link_E_Na      = link((slider_E_Na, 'value'), (textBox_E_Na, 'value'))152link_E_K       = link((slider_E_K, 'value'), (textBox_E_K, 'value'))153link_E_L       = link((slider_E_L, 'value'), (textBox_E_L, 'value'))154link_amplitude   = link((slider_amplitude, 'value'), (textBox_amplitude, 'value'))155link_width       = link((slider_width, 'value'), (textBox_width, 'value'))156link_translation = link((slider_translation, 'value'), (textBox_translation, 'value'))157 158#voltage clamp slider and textbox link for values159link_delay          = link((slider_delay         , 'value'), (textBox_delay         , 'value'))160link_duration       = link((slider_duration      , 'value'), (textBox_duration      , 'value'))161link_condVoltage    = link((slider_condVoltage   , 'value'), (textBox_condVoltage   , 'value'))162link_testVoltage    = link((slider_testVoltage   , 'value'), (textBox_testVoltage   , 'value'))163link_returnVoltage  = link((slider_returnVoltage , 'value'), (textBox_returnVoltage , 'value'))164 165#define reset button and connect to fucntion call166reset_button = ipywidgets.Button(description="Reset All",button_style='warning',tooltip='Reset to default values for all user inputs')167reset_button.on_click(resetTodefault)168 169#define toggle button for default values and connect to fucntion call170showValue_togglebtn = ipywidgets.ToggleButton(value=False,description='Default Values',disabled=False,button_style='info',tooltip='Show/Hide default value below') # 'success', 'info', 'warning', 'danger' or ''171showValue_togglebtn.observe(showDefault)172defalultValues = ipywidgets.HTMLMath(value=r"\(C = %s\)<br>\(G_{Na} = %s, G_{K} = %s, G_{L} =  %s\)<br>\(E_{Na} =  %s, E_{K} = %s, E_{L} = %s\)" % \173                     (default_capacitance, default_cond_Na, default_cond_K, default_cond_L, default_E_Na, default_E_K, default_E_L))174defalultValues.layout.display = 'none'175 176#define toggle buttons for iclamp/vclamp run mode177runMode_togglebtns = ipywidgets.ToggleButtons(options=['Current Clamp', 'Voltage Clamp'],description='',button_style='',178                                             tooltips=['Simulate using an injected square current pulse', 'Simulate in voltage clamp mode - inject varying current to force specific voltage profile'])179runMode_togglebtns.observe(runModeChange,'value')180 181#layout widgets in column using HBox182h1=ipywidgets.HBox([header_capacitance])183h2=ipywidgets.HBox([slider_capacitance, textBox_capacitance])184h3=ipywidgets.HBox([header_conductance])185h4=ipywidgets.HBox([slider_cond_Na,textBox_cond_Na,slider_cond_K,textBox_cond_K,slider_cond_L,textBox_cond_L])186h5=ipywidgets.HBox([header_potential])187h6=ipywidgets.HBox([slider_E_Na,textBox_E_Na,slider_E_K,textBox_E_K,slider_E_L,textBox_E_L])188h7=ipywidgets.HBox([header_simTime])189h8=ipywidgets.HBox([time_end,time_step])190h9=ipywidgets.HBox([header_runMode])191h10=ipywidgets.HBox([runMode_togglebtns])192 193#widget for current clamp mode (default)194iclamp_sliders=ipywidgets.HBox([slider_amplitude,textBox_amplitude,slider_width,textBox_width,slider_translation,textBox_translation])195runMode_iclamp=ipywidgets.VBox([header_injCurrent,iclamp_sliders])196 197#widget for voltage clamp mode198vclamp_sliders_time    =ipywidgets.HBox([slider_delay,textBox_delay,slider_duration,textBox_duration])199vclamp_sliders_volt    =ipywidgets.HBox([slider_condVoltage,textBox_condVoltage,slider_testVoltage,textBox_testVoltage,slider_returnVoltage,textBox_returnVoltage])200runMode_vclamp         =ipywidgets.VBox([header_vclamp_time,vclamp_sliders_time,header_vclamp_volt,vclamp_sliders_volt])201runMode_vclamp.layout.display = 'none'202 203#reset and defalult value buttons in single row204button_row=ipywidgets.HBox([reset_button,showValue_togglebtn])205 206#plot selectors207header_plotting = ipywidgets.HTMLMath(value=r"<b> Select plots to show</b>")208injected_current_plot_value = ipywidgets.Checkbox(value=True, description="1) Current injection", disabled=False,)209gating_plot_value = ipywidgets.Checkbox(value=True, description="2) Gating variables", disabled=False,)210cond_scaling_value = ipywidgets.Checkbox(value=False, description="3) Conductance scaling", disabled=False,)211cond_dens_plot_value = ipywidgets.Checkbox(value=True, description="4) Conductance densities", disabled=False,)212driving_force_value = ipywidgets.Checkbox(value=False, description="5) Driving force", disabled=False,)213current_plot_value = ipywidgets.Checkbox(value=True, description="6) Current densities", disabled=False,)214memb_pot_plot_value = ipywidgets.Checkbox(value=True, description="7) Membrane potential", disabled=False,)215plot_selection_row=ipywidgets.VBox([header_plotting, ipywidgets.HBox([injected_current_plot_value, gating_plot_value, cond_scaling_value, cond_dens_plot_value]), ipywidgets.HBox([driving_force_value, current_plot_value, memb_pot_plot_value])])216 217#layout vertically all the widgets defined above218modelInputs=ipywidgets.VBox([h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,runMode_iclamp,runMode_vclamp,button_row,defalultValues,plot_selection_row])219 220# Main method to create interactive widget221def launch_interactive_widget():222 223    import ipywidgets224    from importlib.machinery import SourceFileLoader225 226    # imports the module from the given path227    HHmodel = SourceFileLoader("HodgkinHuxley.py","../../Tutorial/Source/HodgkinHuxley.py").load_module()228 229    #function to call python script as a module230    def runHH(C_m, g_Na, g_K, g_L, E_Na, E_K, E_L, t_n, delta_t,231              I_inj_max, I_inj_width, I_inj_trans, vc_delay, vc_duration,232              vc_condVoltage, vc_testVoltage, vc_returnVoltage, runMode,233              injected_current_plot, gating_plot, cond_scaling_plot, cond_dens_plot, driving_force_plot, current_plot, memb_pot_plot):234 235        highlight_slider()236        runner = HHmodel.HodgkinHuxley(C_m, g_Na, g_K, g_L, E_Na, E_K, E_L,237                                       t_n, delta_t, I_inj_max,238                                       I_inj_width, I_inj_trans, vc_delay,239                                       vc_duration, vc_condVoltage,240                                       vc_testVoltage, vc_returnVoltage,241                                       runMode,242                                       injected_current_plot=injected_current_plot,243                                       gating_plot=gating_plot,244                                       cond_scaling_plot=cond_scaling_plot,245                                       cond_dens_plot=cond_dens_plot,246                                       driving_force_plot=driving_force_plot,247                                       current_plot=current_plot,248                                       memb_pot_plot=memb_pot_plot)249        # init_values are the steady state values for v,m,h,n at zero current injection250        runner.simulate(init_values=[-63.8, 0.0609, 0.5538, 0.3361])251 252    #create plot area widget and interact with HHmodel253    wid_plotArea=ipywidgets.interactive_output(runHH,{'C_m':slider_capacitance,254                                            'g_Na':textBox_cond_Na, 'g_K':textBox_cond_K, 'g_L':textBox_cond_L,255                                            'E_Na':textBox_E_Na, 'E_K':textBox_E_K, 'E_L':textBox_E_L,256                                            't_n':time_end, 'delta_t':time_step,257                                            'I_inj_max':textBox_amplitude,'I_inj_width':textBox_width,'I_inj_trans':textBox_translation,258                                            'vc_delay':textBox_delay,'vc_duration':textBox_duration,'vc_condVoltage':textBox_condVoltage,259                                            'vc_testVoltage':textBox_testVoltage,'vc_returnVoltage':textBox_returnVoltage,260                                            'runMode':runMode_togglebtns, 'injected_current_plot': injected_current_plot_value,261                                                      'gating_plot':gating_plot_value,262                                                      'cond_scaling_plot':cond_scaling_value,263                                                      'cond_dens_plot':cond_dens_plot_value,264                                                      'driving_force_plot':driving_force_value,265                                                      'current_plot':current_plot_value,266                                                      'memb_pot_plot':memb_pot_plot_value})267 268    #display the widgets and plot area269    display(modelInputs,wid_plotArea)270