def find_steadystate(ode, BCL, dt, num_of_beats=50, path="../ode/"): """ Find a quasi steady state by pacing the 0D cell model at a given cycle length for a given number of beats. """ # Compile the ODE solver module module, forward = create_module(ode, path=path) # Fetch model parameter list and init states model_params = module.init_parameter_values() states = module.init_state_values() # Find indices of various states/parameters V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") # Set the BCL model_params[BCL_index] = BCL # Start array for recording results V = [states[V_index]] # Solve ODE system over time t = 0. tstop = num_of_beats * BCL while t <= tstop: forward(states, t, dt, model_params) V.append(states[V_index]) t += dt # Save the final state as the steady state sspath = "../data/steadystates/" np.save(sspath + "steadystate_%s_BCL%d" % (ode, BCL), states) return states
def find_steadystates(ode, BCL_range, dt, num_of_beats=50, path="../ode/"): """ Find the quasi steady state for a range of cycle lengths by pacing the 0D cell model at a given cycle length for a given number of beats, the default being 50 beats. """ # Compile the ODE solver module module, forward = create_module(ode, path=path) # Fetch model parameter list and init states model_params = module.init_parameter_values() init_states = module.init_state_values() # Find indices of various states/parameters V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") for BCL in BCL_range: print "Pacing for BCL %d." % BCL # Load initial condition states = init_states # Set the BCL model_params[BCL_index] = BCL # Solve ODE system for given number of beats t = 0.; tstop = num_of_beats*BCL while t <= tstop: forward(states, t, dt, model_params) t += dt # Save the final state as the steady state np.save("../data/steadystates/steadystate_%s_BCL%d" % (ode, BCL), states)
def find_steadystate(ode, BCL, dt, num_of_beats=50, path="../ode/"): """ Find a quasi steady state by pacing the 0D cell model at a given cycle length for a given number of beats. """ # Compile the ODE solver module module, forward = create_module(ode, path=path) # Fetch model parameter list and init states model_params = module.init_parameter_values() states = module.init_state_values() # Find indices of various states/parameters V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") # Set the BCL model_params[BCL_index] = BCL # Start array for recording results V = [states[V_index]] # Solve ODE system over time t = 0. tstop = num_of_beats*BCL while t <= tstop: forward(states, t, dt, model_params) V.append(states[V_index]) t += dt # Save the final state as the steady state sspath = "../data/steadystates/" np.save(sspath+"steadystate_%s_BCL%d" % (ode, BCL), states) return states
def find_steadystates(ode, BCL_range, dt, num_of_beats=50, path="../ode/"): """ Find the quasi steady state for a range of cycle lengths by pacing the 0D cell model at a given cycle length for a given number of beats, the default being 50 beats. """ # Compile the ODE solver module module, forward = create_module(ode, path=path) # Fetch model parameter list and init states model_params = module.init_parameter_values() init_states = module.init_state_values() # Find indices of various states/parameters V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") for BCL in BCL_range: print "Pacing for BCL %d." % BCL # Load initial condition states = init_states # Set the BCL model_params[BCL_index] = BCL # Solve ODE system for given number of beats t = 0. tstop = num_of_beats * BCL while t <= tstop: forward(states, t, dt, model_params) t += dt # Save the final state as the steady state np.save("../data/steadystates/steadystate_%s_BCL%d" % (ode, BCL), states)
def erp(ode, BCL_range, dt, start, threshold=0.8): """ Calculate the effective refractatory period of an ode model for a range of BCLs. First the cell is paced for 50 cycles at the given S1 BCL, then a S2 pulse of a lower cycle length is issued. If the action potential from the S2 pulse has a peak value of less than the threshold ratio of the S1 peak, the cell is refractatory. The ERP is the smallest S2 cycle length where the cell is not refractatory. """ module, forward = create_module(ode) model_params = module.init_parameter_values() init_states = module.init_state_values() # Find indices of various states/parameters V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") results = np.zeros((2, len(BCL_range))) for i, BCL in enumerate(BCL_range): # First pace cell for 50 cycles at S1 try: sspath = "../data/steadystates/" S1 = np.load(sspath + "steadystate_%s_BCL%d.npy" % (ode, BCL)) except: print "Pacing for BCL %d." % BCL # Load initial condition states = init_states # Set the BCL model_params[BCL_index] = BCL # Solve ODE system for given number of beats t = 0. tstop = 50 * BCL while t <= tstop: forward(states, t, dt, model_params) t += dt # Save the final state as the steady state np.save("../data/steadystates/steadystate_%s_BCL%d" % (ode, BCL), states) S1 = states.copy() if i == 0: S2_range = np.arange(start, BCL, 0.1) else: S2_range = np.arange(results[1][i - 1], BCL, 0.1) for S2 in S2_range: print "Trying %g" % S2 states = S1.copy() model_params[BCL_index] = S2 V = [] t = 0 tstop = 2 * S2 while t <= S2: forward(states, t, dt, model_params) V.append(states[V_index]) t += dt Vpeak = max(V) V = [] while t <= tstop: forward(states, t, dt, model_params) V.append(states[V_index]) t += dt if max(V) > 0.8 * Vpeak: break print "BCL: %g ERP: %s" % (BCL, S2) results[0][i] = BCL results[1][i] = S2 np.save("../data/results/erp_%s" % ode, results)
plt.xlabel(r"Time [ms]", fontsize=20) plt.ylabel(r"V [mV]", fontsize=20) #plt.title(r'MAP', fontsize=20) plt.grid() plt.legend([u"Koivumäki nSR", u"FK nSR"], prop={'size':20}) plt.savefig('../fig/compare_MAPS_nSR.png') plt.show() plt.close() ''' # cAF odes = ['hAM_KSMT_cAF', 'FK_cAF'] for i, ode in enumerate(odes): module, forward = create_module(ode) model_params = module.init_parameter_values() states = module.init_state_values() V_index = module.state_indices("V") offset_index = module.parameter_indices("stim_offset") model_params[offset_index] = 10 # Start array for recording results V = [states[V_index]] t = 0; tstop = 400 while t <= tstop: forward(states, t, dt, model_params) V.append(states[V_index]) t += dt
def pacing_dynamic(ode, BCL_range, dt, threshold=-60): """ Calculate a restituion curve using dynamic pacing. The ODE model is paced for 50 beats at every BCL value, and APD and DI are measured for the final beat. """ # Create ODE solver module, forward = create_module(ode) # Get model parameters and initial conditions model_params = module.init_parameter_values() init_states = module.init_state_values() # Get parameter indices V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") # For storing the results results = np.zeros((3, len(BCL_range))) for i, BCL in enumerate(BCL_range): # Set BCL model_params[BCL_index] = BCL # Read in steady state from file try: sspath = "../data/steadystates/" states = np.load(sspath+"steadystate_%s_BCL%d.npy" % (ode, BCL)) except: print "Pacing for BCL %d." % BCL # Load initial condition states = init_states # Solve ODE system for given number of beats num_of_beats = 50 t = 0.; tstop = num_of_beats*BCL while t <= tstop: forward(states, t, dt, model_params) t += dt # Save the final state as the steady state np.save(sspath+"steadystate_%s_BCL%d" % (ode, BCL), states) # Used to measure when potential crosses threshold cross_threshold = [] Vp = states[V_index] t=0.; tstop = BCL; while t < tstop: forward(states, t, dt, model_params) V = states[V_index] if (V-threshold)*(Vp-threshold) < 0: cross_threshold.append(t) Vp = V t += dt try: APD = cross_threshold[-1] - cross_threshold[-2] DI = tstop - cross_threshold[-1] except: APD = 0 DI = 0 print "BCL: %g, APD: %g, DI: %g" % (BCL, APD, DI) results[0][i] = BCL results[1][i] = APD results[2][i] = DI np.save("../data/results/dynamic_%s" % ode, results)
def pacing_S1S2(ode, S1, S2_range, dt, threshold=-60): """ Calculate a restitution curve using S1-S2 pacing, take in the relevant ode model, the S1 length and the range of S2 values to calculate APD/DI. Note that the steady state of a BCL=S1 must be lying in /data/. If this does not exist for the given ODE file, you can make it using steadystate.py. """ module, forward = create_module(ode) model_params = module.init_parameter_values() # Pace 0D cell model and find quasi-steady state try: sspath = "../data/steadystates/" init_states = np.load(sspath+"steadystate_%s_BCL%d.npy" % (ode, S1)) except: print "Pacing 0D model for BCL %g..." % S1, init_states = find_steadystate(S1, 50, dt, ode, plot_results=False) print "done." # Find indices of various states/parameters V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") def pulse_S2(S2): """Pulse the steady state with a single S2 pulse and measure APD/DI.""" states = init_states model_params[BCL_index] = S2 t = 0.; tstop = 2*S2 while t <= S2: forward(states, t, dt, model_params) t += dt Vp = states[V_index] cross_threshold = [] while t <= tstop: forward(states, t, dt, model_params) V = states[V_index] if (Vp-threshold)*(V-threshold) < 0: cross_threshold.append(t) Vp = V t += dt try: APD = cross_threshold[-1] - cross_threshold[-2] DI = tstop - cross_threshold[-1] except: APD = 0 DI = 0 return APD, DI results = np.zeros((3, len(S2_range))) for S2 in S2_range: APD, DI = pulse_S2(S2) results[0][i] = S2 results[1][i] = APD results[2][i] = DI print "S2: %g, APD: %g, DI: %g" % (S2, APD, DI) np.save("../data/results/S1S2_%s" % ode, results)
def pacing_dynamic(ode, BCL_range, dt, threshold=-60): """ Calculate a restituion curve using dynamic pacing. The ODE model is paced for 50 beats at every BCL value, and APD and DI are measured for the final beat. """ # Create ODE solver module, forward = create_module(ode) # Get model parameters and initial conditions model_params = module.init_parameter_values() init_states = module.init_state_values() # Get parameter indices V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") # For storing the results results = np.zeros((3, len(BCL_range))) for i, BCL in enumerate(BCL_range): # Set BCL model_params[BCL_index] = BCL # Read in steady state from file try: sspath = "../data/steadystates/" states = np.load(sspath + "steadystate_%s_BCL%d.npy" % (ode, BCL)) except: print "Pacing for BCL %d." % BCL # Load initial condition states = init_states # Solve ODE system for given number of beats num_of_beats = 50 t = 0. tstop = num_of_beats * BCL while t <= tstop: forward(states, t, dt, model_params) t += dt # Save the final state as the steady state np.save(sspath + "steadystate_%s_BCL%d" % (ode, BCL), states) # Used to measure when potential crosses threshold cross_threshold = [] Vp = states[V_index] t = 0. tstop = BCL while t < tstop: forward(states, t, dt, model_params) V = states[V_index] if (V - threshold) * (Vp - threshold) < 0: cross_threshold.append(t) Vp = V t += dt try: APD = cross_threshold[-1] - cross_threshold[-2] DI = tstop - cross_threshold[-1] except: APD = 0 DI = 0 print "BCL: %g, APD: %g, DI: %g" % (BCL, APD, DI) results[0][i] = BCL results[1][i] = APD results[2][i] = DI np.save("../data/results/dynamic_%s" % ode, results)
plt.xlabel(r"Time [ms]", fontsize=20) plt.ylabel(r"V [mV]", fontsize=20) #plt.title(r'MAP', fontsize=20) plt.grid() plt.legend([u"Koivumäki nSR", u"FK nSR"], prop={'size':20}) plt.savefig('../fig/compare_MAPS_nSR.png') plt.show() plt.close() ''' # cAF odes = ['hAM_KSMT_cAF', 'FK_cAF'] for i, ode in enumerate(odes): module, forward = create_module(ode) model_params = module.init_parameter_values() states = module.init_state_values() V_index = module.state_indices("V") offset_index = module.parameter_indices("stim_offset") model_params[offset_index] = 10 # Start array for recording results V = [states[V_index]] t = 0 tstop = 400 while t <= tstop: forward(states, t, dt, model_params) V.append(states[V_index])
def erp(ode, BCL_range, dt, start, threshold=0.8): """ Calculate the effective refractatory period of an ode model for a range of BCLs. First the cell is paced for 50 cycles at the given S1 BCL, then a S2 pulse of a lower cycle length is issued. If the action potential from the S2 pulse has a peak value of less than the threshold ratio of the S1 peak, the cell is refractatory. The ERP is the smallest S2 cycle length where the cell is not refractatory. """ module, forward = create_module(ode) model_params = module.init_parameter_values() init_states = module.init_state_values() # Find indices of various states/parameters V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") results = np.zeros((2,len(BCL_range))) for i, BCL in enumerate(BCL_range): # First pace cell for 50 cycles at S1 try: sspath = "../data/steadystates/" S1 = np.load(sspath+"steadystate_%s_BCL%d.npy" % (ode, BCL)) except: print "Pacing for BCL %d." % BCL # Load initial condition states = init_states # Set the BCL model_params[BCL_index] = BCL # Solve ODE system for given number of beats t = 0.; tstop = 50*BCL while t <= tstop: forward(states, t, dt, model_params) t += dt # Save the final state as the steady state np.save("../data/steadystates/steadystate_%s_BCL%d" % (ode, BCL), states) S1 = states.copy() if i == 0: S2_range = np.arange(start, BCL, 0.1) else: S2_range = np.arange(results[1][i-1], BCL, 0.1) for S2 in S2_range: print "Trying %g" % S2 states = S1.copy() model_params[BCL_index] = S2 V = [] t = 0; tstop = 2*S2 while t <= S2: forward(states, t, dt, model_params) V.append(states[V_index]) t += dt Vpeak = max(V) V = [] while t <= tstop: forward(states, t, dt, model_params) V.append(states[V_index]) t += dt if max(V) > 0.8*Vpeak: break print "BCL: %g ERP: %s" % (BCL, S2) results[0][i] = BCL results[1][i] = S2 np.save("../data/results/erp_%s" % ode, results)
def pacing_dynamic(ode, BCL_range, dt, pulse_train_length=30e3, pacing_memory=True, threshold=-60): """ Calculate a restitution curve using dynamic pacing, take in the relevant ode model, the range of BCLs to be used. A pulse train of 30 seconds is the default. The regime steps straight from one BCL to the next, meaning it contains pacing memory. If pacing memory is turned of, we reload the inital states. """ module, forward = create_module(ode) model_params = module.init_parameter_values() states = module.init_state_values() # Find indices of various states/parameters V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") def pulse_train(BCL, init_states): """ Run a pulse train on the cell with given BCL and length, and measure the APD and DI from the last pulse. """ states = init_states model_params[BCL_index] = BCL t=0.; if pulse_train_length > BCL*50.: tstop = np.ceil(pulse_train_length/BCL)*BCL else: tstop = BCL*50. while t < tstop - BCL: forward(states, t, dt, model_params) t += dt cross_threshold = [] Vp = states[V_index] while t <= tstop: forward(states, t, dt, model_params) V = states[V_index] if (V-threshold)*(Vp-threshold) < 0: cross_threshold.append(t) Vp = V t += dt APD = cross_threshold[-1] - cross_threshold[-2] DI = tstop - cross_threshold[-1] return APD, DI, states results_BCL = [] results_APD = [] results_DI = [] prev_states = init_states for BCL in BCL_range: states = prev_states if pacing_memory else init_states APD, DI, prev_states = pulse_train(BCL, states) results_BCL.append(BCL) results_APD.append(APD) results_DI.append(DI) print "BCL: %g, APD: %g, DI: %g" % (BCL, APD, DI) np.save("../data/resultss_dynamic_%s" % ode, np.array((results_BCL, results_APD, results_DI)))
def pacing_dynamic(ode, BCL_range, dt, pulse_train_length=30e3, pacing_memory=True, threshold=-60): """ Calculate a restitution curve using dynamic pacing, take in the relevant ode model, the range of BCLs to be used. A pulse train of 30 seconds is the default. The regime steps straight from one BCL to the next, meaning it contains pacing memory. If pacing memory is turned of, we reload the inital states. """ module, forward = create_module(ode) model_params = module.init_parameter_values() states = module.init_state_values() # Find indices of various states/parameters V_index = module.state_indices("V") BCL_index = module.parameter_indices("stim_period") def pulse_train(BCL, init_states): """ Run a pulse train on the cell with given BCL and length, and measure the APD and DI from the last pulse. """ states = init_states model_params[BCL_index] = BCL t = 0. if pulse_train_length > BCL * 50.: tstop = np.ceil(pulse_train_length / BCL) * BCL else: tstop = BCL * 50. while t < tstop - BCL: forward(states, t, dt, model_params) t += dt cross_threshold = [] Vp = states[V_index] while t <= tstop: forward(states, t, dt, model_params) V = states[V_index] if (V - threshold) * (Vp - threshold) < 0: cross_threshold.append(t) Vp = V t += dt APD = cross_threshold[-1] - cross_threshold[-2] DI = tstop - cross_threshold[-1] return APD, DI, states results_BCL = [] results_APD = [] results_DI = [] prev_states = init_states for BCL in BCL_range: states = prev_states if pacing_memory else init_states APD, DI, prev_states = pulse_train(BCL, states) results_BCL.append(BCL) results_APD.append(APD) results_DI.append(DI) print "BCL: %g, APD: %g, DI: %g" % (BCL, APD, DI) np.save("../data/resultss_dynamic_%s" % ode, np.array((results_BCL, results_APD, results_DI)))