def gen_single(self, params): """Forward model for simulator for single parameter set Parameters ---------- params : list or np.array, 1d of length dim_param Parameter vector integration : string deciding on the type of integration used Returns ------- dict : dictionary with data The dictionary must contain a key data that contains the results of the forward run. Additional entries can be present. """ params = param_invtransform(self.prior_log,np.asarray(params)) assert params.ndim == 1, 'params.ndim must be 1' dap_seed = self.gen_newseed() dap = DAPcython(self.init, params, seed=dap_seed) states = dap.simulate(self.dt, self.t, self.I) return {'data': states.reshape(-1), 'time': self.t, 'dt': self.dt, 'I': self.I.reshape(-1)}
def syn_obs_stats(I, params, dt, t_on, t_off, data=None, V0=-75, summary_stats=0, n_xcorr=5, n_mom=5, n_summary=4, seed=None): """Summary stats for x_o of DAP""" if data is None: m = DAPcython(I=I, dt=dt, V0=V0, seed=seed) data = m.gen_single(params) if summary_stats == 0: s = DAPSummaryStatsMoments(t_on, t_off, n_summary=n_summary) elif summary_stats == 1: s = DAPSummaryStatsStepMoments(t_on, t_off, n_summary=n_summary) else: raise ValueError('Only 0, 1 as an option for summary statistics.') return s.calc([data])
from delfi.distribution import Uniform from dap.utils import obs_params_gbar, syn_current from dap.dap_sumstats_moments import DAPSummaryStatsMoments from dap import DAPcython from dap.dap_simulator import DAPSimulator # General Settings Pick n_samples = 100 n_summary = 13 dt = 0.01 percent_accept = 1 # Get current I, t, t_on, t_off = syn_current(duration=70, dt=dt, t_on=15, t_off=20, amp=3.1) params, labels = obs_params_gbar(reduced_model=True) dap = DAPcython(-75, params * 10) # Set up the model sim = DAPSimulator(I, dt, -75, dim_param=2) stats = DAPSummaryStatsMoments(t_on, t_off, n_summary=n_summary) # Setup Priors prior_min = np.array([0, 0]) prior_max = np.array([2, 2]) prior_unif = Uniform(lower=prior_min, upper=prior_max) # generate desired data U = dap.simulate(dt, t, I) y_o = {'data': U.reshape(-1), 'time': t, 'dt': dt, 'I': I} y = stats.calc([y_o])
data_dir, protocol='IV', ramp_amp=1) tstep = t_step[0:16200] Istep = I_step[2500:18700] vstep = v_step[2500:18700] I_all = [I, Istep] dt_all = [dt, dt_step] params, labels = obs_params(reduced_model=True) params = params[:n_params] print(params) print(labels) # Set up themodel dap = DAPcython(-75, params) U = dap.simulate(dt, t, I) U_step = dap.simulate(dt_step, tstep, Istep) # generate data format for SNPE / OBSERVABLE x_o = {'data': v.reshape(-1), 'time': t, 'dt': dt, 'I': I} # Setup Priors prior_min, prior_max, labels = load_prior_ranges(n_params) prior_unif = Uniform(lower=prior_min, upper=prior_max) # Summary Statistics S = syn_obs_stats(x_o['I'], params=params, dt=x_o['dt'], t_on=t_on,
from dap import DAP, DAPBe from dap import DAPcython from dap.utils import obs_params, obs_params_gbar, syn_current dt = 1e-2 params, labels = obs_params(reduced_model=True) params, labels = obs_params_gbar(reduced_model=False) I, t, t_on, t_off = syn_current(duration=120, dt=dt) print(params) # define models / check setters dap = DAP(-75, params) dap_back = DAPBe(-75, params) dap_cython = DAPcython(-75, params, solver=1) dap_cython_back = DAPcython(-75, params, solver=2) # run models U = dap.simulate(dt, t, I) U_cython = dap_cython.simulate(dt, t, I) U_back = dap_back.simulate(dt, t, I) U_cython_back = dap_cython_back.simulate(dt, t, I) # plot fig, ax = plt.subplots(5, 1, figsize=(20, 10)); ax[0].plot(t, U, label='forward') ax[1].plot(t, U_cython) ax[2].plot(t, U_back, label='backward') ax[3].plot(t, U_cython_back, label='backward_cython')
Ir, vr, tr, t_onr, t_offr, dtr = load_current(data_dir, protocol='rampIV', ramp_amp=3.1) Is, vs, ts, t_ons, t_offs, dts = load_current(data_dir, protocol='IV', ramp_amp=1) # get traces for both currents U_steps, U_ramps = [], [] for i, j in tqdm(df_param.iterrows()): # get parameters par_temp = j.values # define a model dap = DAPcython(-75, j) # run model U_step_x = dap.simulate(dts, ts, Is) U_ramp_x = dap.simulate(dtr, tr, Ir) # run model U_steps.append(U_step_x.transpose()[0]) U_ramps.append(U_ramp_x.transpose()[0]) # calculate distance for both currents dis_step = distance.euclidean(vs, U_step_x) dis_ramp = distance.euclidean(vr, U_ramp_x) # save into new columns df_param.loc[i, 'distance_ramp'] = dis_ramp
'precisions.mb1' } # Load the current data_dir = '/home/alteska/Desktop/LFI_DAP/data/rawData/2015_08_26b.dat' # best cell protocol = 'IV' # 'IV' # 'rampIV' # 'Zap20' ramp_amp = 1 I, v, t, t_on, t_off, dt = load_current(data_dir, protocol=protocol, ramp_amp=ramp_amp) I_ramp, v_ramp, t_ramp, t_on_ramp, t_off_ramp, dt_ramp = load_current( data_dir, protocol='rampIV', ramp_amp=3.1) # Set up themodel params, labels = obs_params(reduced_model=True) dap = DAPcython(-75, params) U_ramp = dap.simulate(dt_ramp, t_ramp, I_ramp) U = dap.simulate(dt, t, I) # generate data format for SNPE / OBSERVABLE x_o = {'data': v.reshape(-1), 'time': t, 'dt': dt, 'I': I} # Setup Priors prior_min, prior_max, labels = load_prior_ranges(n_params) prior_unif = Uniform(lower=prior_min, upper=prior_max) print(prior_min, prior_max, labels) # Summary Statistics S = syn_obs_stats(x_o['I'], params=params, dt=x_o['dt'],
from dap.utils import obs_params_gbar, load_current params, labels = obs_params_gbar() data_dir = '/home/ateska/Desktop/LFI_DAP/data/rawData/2015_08_26b.dat' # best cell # data_dir = '/home/ateska/Desktop/LFI_DAP/data/rawData/2015_08_11d.dat' # second best cell # load the data TODO: load actual Zap20 with CORRECT ramp_amp I, v, t, t_on, t_off, dt = load_current(data_dir, protocol='IV', ramp_amp=-0.15) time_start = time.clock() # define models dap = DAPcython(-75, params) # run model DAPdict = dap.simulate(dt, t, I, channels=True) time_end = time.clock() print('time elapsed:', time_end - time_start) # plot voltage trace fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(20, 10)) ax.plot(t, DAPdict['U'], label='model') ax.plot(t, v, label='data') ax.grid() ax.plot(t, I) ax.legend() plt.show()
import timeit import matplotlib.pyplot as plt from dap import DAP, DAPBe from dap import DAPcython from dap.utils import obs_params_gbar, obs_params, syn_current dt = 1e-2 # params, labels = obs_params_gbar(reduced_model=True) params, labels = obs_params(reduced_model=False) I, t, t_on, t_off = syn_current(duration=120, dt=dt) # define models dap = DAP(-75, params) dap_back = DAPBe(-75, params) dap_cython = DAPcython(-75, params, solver=1) dap_cython_back = DAPcython(-75, params, solver=2) # run models U = dap.simulate(dt, t, I) U_cython = dap_cython.simulate(dt, t, I) U_back = dap_back.simulate(dt, t, I) U_cython_back = dap_cython_back.simulate(dt, t, I) print('\n', 'forward:') print("cython:", timeit.timeit(lambda: dap_cython.simulate(dt, t, I), number=int(4))) print("python:", timeit.timeit(lambda: dap.simulate(dt, t, I), number=int(4))) print('\n', 'backward:') print("cython:",
import matplotlib.pyplot as plt from dap import DAPcython from dap.utils import load_current, obs_params_gbar params, labels = obs_params_gbar() data_dir = '/home/ateska/Desktop/LFI_DAP/data/rawData/2015_08_26b.dat' # best cell # data_dir = '/home/ateska/Desktop/LFI_DAP/data/rawData/2015_08_11d.dat' # second best cell # load the data I, v, t, t_on, t_off, dt = load_current(data_dir, protocol='rampIV', ramp_amp=3.1) I_iv, v_iv, t_iv, t_on_iv, t_off_iv, dt_iv = load_current(data_dir, protocol='IV', ramp_amp=1) # define and run the model dap = DAPcython(-75, params) U = dap.simulate(dt, t, I) U_iv = dap.simulate(dt_iv, t_iv, I_iv) print("cython:", timeit.timeit(lambda: dap.simulate(dt, t, I), number=int(1))) print("cython:", timeit.timeit(lambda: dap.simulate(dt_iv, t_iv, I_iv), number=int(1))) print(dt, dt_iv) fig, ax = plt.subplots(2, 1, figsize=(10,20)) ax[0].plot(t, I) ax[0].plot(t, v) ax[0].plot(t, U) ax[1].plot(t_iv, I_iv) ax[1].plot(t_iv, v_iv) ax[1].plot(t_iv, U_iv)