import sys from PyQt4 import QtGui, QtCore from math import * from scipy.integrate import odeint # ODE integration from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure import Emodels class AP_PlotCanvas(FigureCanvas): """Derives from QWidget as well as a FigureCanvasAgg.""" def __init__(self, parent=None, x1=[], y1=[], x2=[], y2=[]): # these settings have a large effect on the appearance of the graph width = 30; height = 20; dpi = 72 self.fig = Figure(figsize=(width, height), dpi=dpi) self.fig.set_edgecolor("None") # gets rid of border around figure # set position of axes with [left, bottom, width, height], as fraction of 1 # use Excel sheet to calculate the y values left = 0.1; right = 0.88 self.ax1 = self.fig.add_axes([left, 0.63, right, 0.34]) self.ax2 = self.fig.add_axes([left, 0.39, right, 0.19]) self.ax3 = self.fig.add_axes([left, 0.18, right, 0.19]) self.ax4 = self.fig.add_axes([left, 0.07, right, 0.09]) # set axis labels ylabel_loc = -0.07 # this value sets offset for y axis label location self.ax1.set_ylabel("Membrane Voltage (mV)") self.ax1.yaxis.set_label_coords(ylabel_loc, 0.5) self.ax2.set_ylabel("Conductance (uS)") self.ax2.yaxis.set_label_coords(ylabel_loc, 0.5) self.ax3.set_ylabel("Current (nA)") self.ax3.yaxis.set_label_coords(ylabel_loc, 0.5) self.ax4.set_ylabel("Current (nA)") self.ax4.yaxis.set_label_coords(ylabel_loc, 0.5) self.ax4.set_xlabel("Time (ms)") # set x-axis limits xlim = (0, 40) self.ax1.set_xlim(xlim); self.ax2.set_xlim(xlim); self.ax3.set_xlim(xlim); self.ax4.set_xlim(xlim) ylim1 = (-100, 50) self.ax1.set_ylim(ylim1) self.ax1.set_yticks([-100, -50, 0, 50]) self.ax1.set_autoscale_on(False) ylim2 = (0, 60) self.ax2.set_ylim(ylim2) self.ax2.set_yticks([0, 20, 40, 60]) self.ax2.set_xticklabels([]) self.ax2.set_autoscale_on(False) ylim3 = (-1000, 1000) self.ax3.set_ylim(ylim3) self.ax3.set_yticks([-1000, -500, 0, 500, 1000]) self.ax3.set_xticklabels([]) self.ax3.set_autoscale_on(False) ylim4 = (0, 60) self.ax4.set_ylim(ylim4) self.ax4.set_yticks([0, 30, 60]) self.ax4.set_autoscale_on(False) # need to set some default lines to give update_Graph1 something to delete on the first # call and allow the creation of legends self.ax1.plot([0, 40], [-70, -70], 'r') self.ax2.plot([0, 40], [0, 0], 'r', label="gNa") self.ax2.plot([0, 40], [0, 0], 'b', label="gK") leg2 = self.ax2.legend(loc='best') leg2.draw_frame(False) self.ax3.plot([0, 40], [0, 0], 'r', label="INa") self.ax3.plot([0, 40], [0, 0], 'b', label="IK") leg3 = self.ax3.legend(loc='best') leg3.draw_frame(False) self.ax4.plot([0, 40], [0, 0], 'r') FigureCanvas.__init__(self, self.fig) # initialize the canvas self.fig.canvas.draw() # force a redraw of the Figure self.setParent(parent) # this is necessary to avoid garbage collection def update_Graph1(self, x, y): del self.ax1.lines[0] self.ax1.plot(x, y, 'r') self.fig.canvas.draw() def update_Graph2(self, x, y1, y2): del self.ax2.lines[1] del self.ax2.lines[0] self.ax2.plot(x, y1, 'r') self.ax2.plot(x, y2, 'b') self.fig.canvas.draw() def update_Graph3(self, x, y1, y2): del self.ax3.lines[1] del self.ax3.lines[0] self.ax3.plot(x, y1, 'r') self.ax3.plot(x, y2, 'b') self.fig.canvas.draw() def update_Graph4(self, x, y): del self.ax4.lines[0] self.ax4.plot(x, y, 'r') self.fig.canvas.draw() class Exp3(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.runButton = QtGui.QPushButton("Run") self.runButton.clicked.connect(self.updateSimulation) self.seriesButton = QtGui.QPushButton("Threshold Series") self.seriesButton.clicked.connect(self.thresholdSeries) self.series2Button = QtGui.QPushButton("Refractory Series") self.series2Button.clicked.connect(self.refractorySeries) stim1_label = QtGui.QLabel("Stimulus 1 :") current1_label = QtGui.QLabel("Current (nA)") delay1_label = QtGui.QLabel("Delay (ms)") duration1_label = QtGui.QLabel("Duration (mS)") stim2_label = QtGui.QLabel("Stimulus 2 :") current2_label = QtGui.QLabel("Current (nA)") delay2_label = QtGui.QLabel("Delay (ms)") duration2_label = QtGui.QLabel("Duration (mS)") self.current1_spinBox = QtGui.QSpinBox() self.current1_spinBox.setRange(0, 60) self.current1_spinBox.setSingleStep(1) self.current1_spinBox.setValue(0) self.current1_spinBox.valueChanged.connect(self.update_current1) self.delay1_spinBox = QtGui.QSpinBox() self.delay1_spinBox.setRange(0, 40) self.delay1_spinBox.setSingleStep(1) self.delay1_spinBox.setValue(0) self.delay1_spinBox.valueChanged.connect(self.update_delay1) self.duration1_spinBox = QtGui.QSpinBox() self.duration1_spinBox.setRange(0, 40) self.duration1_spinBox.setSingleStep(1) self.duration1_spinBox.setValue(0) self.duration1_spinBox.valueChanged.connect(self.update_duration1) self.current2_spinBox = QtGui.QSpinBox() self.current2_spinBox.setRange(0, 60) self.current2_spinBox.setSingleStep(1) self.current2_spinBox.setValue(0) self.current2_spinBox.valueChanged.connect(self.update_current2) self.delay2_spinBox = QtGui.QSpinBox() self.delay2_spinBox.setRange(0, 40) self.delay2_spinBox.setSingleStep(1) self.delay2_spinBox.setValue(0) self.delay2_spinBox.valueChanged.connect(self.update_delay2) self.duration2_spinBox = QtGui.QSpinBox() self.duration2_spinBox.setRange(0, 40) self.duration2_spinBox.setSingleStep(1) self.duration2_spinBox.setValue(0) self.duration2_spinBox.valueChanged.connect(self.update_duration2) vbox = QtGui.QVBoxLayout() vbox.addWidget(self.runButton) vbox.addWidget(stim1_label) vbox.addWidget(current1_label) vbox.addWidget(self.current1_spinBox) vbox.addWidget(delay1_label) vbox.addWidget(self.delay1_spinBox) vbox.addWidget(duration1_label) vbox.addWidget(self.duration1_spinBox) vbox.addWidget(stim2_label) vbox.addWidget(current2_label) vbox.addWidget(self.current2_spinBox) vbox.addWidget(delay2_label) vbox.addWidget(self.delay2_spinBox) vbox.addWidget(duration2_label) vbox.addWidget(self.duration2_spinBox) vbox.addWidget(self.seriesButton) vbox.addWidget(self.series2Button) vbox.addStretch(1) self.plotWindow = AP_PlotCanvas() hbox = QtGui.QHBoxLayout() hbox.addLayout(vbox) hbox.addWidget(self.plotWindow) hbox.addStretch(1) self.setLayout(hbox) self.initParams() # call 'initStim' after construction of spin boxes because this updates their values self.initStim() def initParams(self): E_Na = 50 # equilibrium potential for Na E_K = -77 # equilibrium potential for K E_L = -70 # equilibrium potential for leak current gbarNa = 140 # mS/cm^2 Na max conductance gbarK = 36 # mS/cm^2 K max conductance gL = 0.3 # mS/cm^2 Leak conductance Cm = 1 # uF/cm^2 membrane capacitance # Pack params vector self.params = [E_Na, E_K, E_L, gbarNa, gbarK, gL, Cm] def initStim(self): delay1 = 10 # first stimulus parameters duration1 = 1 current1 = 20 delay2 = 0 # second stimulus parameters duration2 = 0 current2 = 0 self.stim = [delay1, duration1, current1, delay2, duration2, current2] # update values in Spin Boxes self.delay1_spinBox.setValue(delay1) self.duration1_spinBox.setValue(duration1) self.current1_spinBox.setValue(current1) self.delay2_spinBox.setValue(delay2) self.duration2_spinBox.setValue(duration2) self.current2_spinBox.setValue(current2) def runSimulation(self): # ODE solver parameters abserr = 1.0e-8 relerr = 1.0e-6 maxstepsize = 0.1 # need to limit max step size to stop stepping over stimulus SimInterval = 40 # duration of simulation points_per_ms = 50 # 50 points per millisecond numpoints = SimInterval * points_per_ms # create stimulus vector delay1, duration1, current1, delay2, duration2, current2 = self.stim stimVector = [0] * (SimInterval * points_per_ms) for i in range(duration1 * points_per_ms): if i + (delay1 * points_per_ms) <= numpoints - 1: stimVector[i + (delay1 * points_per_ms)] = current1 for i in range(duration2 * points_per_ms): if i + (delay2 * points_per_ms) <= numpoints - 1: stimVector[i + (delay2 * points_per_ms)] = stimVector[i + (delay2 * points_per_ms)] + current2 # Pack initial values vector # Create vector of initial values Vo = -71.2046 # Membrane potential at time = 0 # K channel n activation opening rate n_alpha = 0.01 * (Vo + 55) / (1 - exp(-0.1 * (Vo + 55))) # K channel n activation closing rate n_beta = 0.125 * exp(-0.0125 * (Vo + 65)) # Na channel m activation opening rate m_alpha = .1 * (Vo + 40) / (1 - exp(-.1 * (Vo + 40))) # Na channel m activation closing rate m_beta = 4 * exp(-0.0556 * (Vo + 65)) # Na channel h inactivation opening rate h_alpha = 0.07 * exp(-0.05 * (Vo + 65)) # Na channel h inactivation closing rate h_beta = 1 / (1 + exp(-0.1 * (Vo + 35))) minf = m_alpha / (m_alpha + m_beta) ninf = n_alpha / (n_alpha + n_beta) hinf = h_alpha / (h_alpha + h_beta) y0 = [Vo, minf, ninf, hinf] # vector of initial values # Create the time samples for the output of the ODE solver. t = [SimInterval * float(i) / (numpoints - 1) for i in range(numpoints)] # Solve function Solution = odeint(Emodels.APfn, y0, t, args=(self.params, self.stim), atol=abserr, rtol=relerr, hmax=maxstepsize) v = Solution[:, 0] m = Solution[:, 1] n = Solution[:, 2] h = Solution[:, 3] # Calculate conductance and current vectors E_Na, E_K, E_L, gbarNa, gbarK, gL, Cm = self.params gNa = gbarNa * m * m * m * h gK = gbarK * n * n * n * n INa = gNa * (v - E_Na) IK = gK * (v - E_K) return t, v, gNa, gK, INa, IK, stimVector def updateSimulation(self): t, v, gNa, gK, INa, IK, stimVector = self.runSimulation() self.plotWindow.update_Graph1(t, v) self.plotWindow.update_Graph2(t, gNa, gK) self.plotWindow.update_Graph3(t, INa, IK) self.plotWindow.update_Graph4(t, stimVector) def thresholdSimulation(self): currentsteps = [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 30, 40, 50, 60] current = currentsteps[self.counter] self.counter = self.counter + 1 self.current1_spinBox.setValue(current) self.updateSimulation() # This terminates the loop by setting the Qtimer to a single shot at the second last value of 'currentsteps' if (self.counter >= (len(currentsteps) - 1)): self.timer.setSingleShot(True) localtimer = QtCore.QTimer() # need this second timer to delay activation of widgets until after final run is finished localtimer.singleShot(800, self.activateControlWidgets) def thresholdSeries(self): # Used this approach because I couldn't figure out how to time out within a for loop # terminate timer using a counter and then setSingleShot true self.counter = 0 self.inactivateControlWidgets() self.current1_spinBox.setValue(0) # initialize Stim 1 starting values self.delay1_spinBox.setValue(10) self.duration1_spinBox.setValue(1) self.current2_spinBox.setValue(0) # initialize Stim 2 starting values self.delay2_spinBox.setValue(0) self.duration2_spinBox.setValue(0) self.updateSimulation() # do first run to avoid initial delay self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.thresholdSimulation) self.timer.start(800) # wait 800 ms between function calls def refractorySimulation(self): delaysteps = [ 10, 12, 14, 16, 18, 20, 25, 30, 35] delay2 = delaysteps[self.counter] self.counter = self.counter + 1 self.delay2_spinBox.setValue(delay2) self.updateSimulation() # This terminates the loop by setting the Qtimer to a single shot at the second last value of 'currentsteps' if (self.counter >= (len(delaysteps) - 1)): self.timer.setSingleShot(True) localtimer = QtCore.QTimer() # need this second timer to delay activation of widgets until after final run is finished localtimer.singleShot(800, self.activateControlWidgets) def refractorySeries(self): # Used this approach because I couldn't figure out how to time out within a for loop # terminate timer using a counter and then setSingleShot true self.counter = 0 self.inactivateControlWidgets() self.current1_spinBox.setValue(20) # initialize Stim 1 starting values self.delay1_spinBox.setValue(5) self.duration1_spinBox.setValue(1) self.current2_spinBox.setValue(20) # initialize Stim 2 starting values self.delay2_spinBox.setValue(8) self.duration2_spinBox.setValue(1) self.updateSimulation() # do first run to avoid initial delay self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.refractorySimulation) self.timer.start(800) # wait 800 ms between function calls def inactivateControlWidgets(self): self.runButton.setEnabled(False) self.seriesButton.setEnabled(False) self.delay1_spinBox.setEnabled(False) self.duration1_spinBox.setEnabled(False) self.current1_spinBox.setEnabled(False) self.delay2_spinBox.setEnabled(False) self.duration2_spinBox.setEnabled(False) self.current2_spinBox.setEnabled(False) def activateControlWidgets(self): self.runButton.setEnabled(True) self.seriesButton.setEnabled(True) self.delay1_spinBox.setEnabled(True) self.duration1_spinBox.setEnabled(True) self.current1_spinBox.setEnabled(True) self.delay2_spinBox.setEnabled(True) self.duration2_spinBox.setEnabled(True) self.current2_spinBox.setEnabled(True) def update_delay1(self): self.stim[0] = self.delay1_spinBox.value() def update_duration1(self): self.stim[1] = self.duration1_spinBox.value() def update_current1(self): self.stim[2] = self.current1_spinBox.value() def update_delay2(self): self.stim[3] = self.delay2_spinBox.value() def update_duration2(self): self.stim[4] = self.duration2_spinBox.value() def update_current2(self): self.stim[5] = self.current2_spinBox.value() # This code is for testing this page. Comment out to run in main module. #app = QtGui.QApplication(sys.argv) #test = Exp3() #test.show() #sys.exit(app.exec_()) # This code is for testing this page. Comment out to run in main module.