# 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, t_off=t_off,
                  n_summary=n_summary, summary_stats=1, data=x_o)


M = DAPSimulator(x_o['I'], x_o['dt'], -75)
s = DAPSummaryStatsStepMoments(t_on, t_off, n_summary=n_summary)
G = Default(model=M, prior=prior_unif, summary=s)  # Generator

# Runing the simulation
inf_snpe = SNPE(generator=G, n_components=n_components, n_hiddens=n_hiddens, obs=S,
                # reg_lambda=reg_lambda, pilot_samples=0, prior_norm=True)
                reg_lambda=reg_lambda, pilot_samples=0)

logs, tds, posteriors = inf_snpe.run(n_train=[n_samples], n_rounds=n_rounds,
                                     proposal=prior_unif)


# Analyse results
samples_prior = prior_unif.gen(n_samples=int(5e5))
samples_posterior = posteriors[-1].gen(n_samples=int(5e5))

print('posterior:', posteriors[-1].mean)


x_post = syn_obs_data(I, dt, posteriors[-1].mean)
idx = np.arange(0, len(x_o['data']))
Beispiel #2
0
    s = DAPSummaryStats(t_on, t_off, n_summary=n_summary)
elif summary_stats_type == 1:
    s = DAPSummaryStatsA(t_on, t_off, n_summary=n_summary)
elif summary_stats_type == 2:
    s = DAPSummaryStatsNoAP(t_on, t_off, n_summary=n_summary)
else:
    raise ValueError('Only 0, 1, 2 as an option for summary statistics.')

sum_stats = DAPSummaryStatsA(t_on, t_off, n_summary=8)

G = Default(model=M, prior=prior, summary=sum_stats)  # Generator

# Runing the simulation
inf_snpe = SNPE(generator=G,
                n_components=1,
                n_hiddens=[2],
                obs=S,
                pilot_samples=10)

logs, tds, posteriors = inf_snpe.run(n_train=[n_samples],
                                     n_rounds=n_rounds,
                                     monitor=observables,
                                     round_cl=1)

# Analyse results
print('expected mean, std', expected.mean, expected.std)
print('posterior mean, std', posteriors[-1].mean, posteriors[-1].std)

# Saving Data into pickle files
posterior_sampl = simulate_data_distr(posteriors[-1],
                                      M,
Beispiel #3
0
prior_unif = Uniform(lower=prior_min, upper=prior_max)

samples_prior = prior_unif.gen(n_samples=int(5e5))
idx = np.arange(0, len(x_o['data']))

# Summary Statistics
S = syn_obs_stats(x_o['I'], params=params, dt=x_o['dt'], t_on=t_on, t_off=t_off,
                  n_summary=n_summary, summary_stats=0, data=x_o)

M = DAPSimulator(x_o['I'], x_o['dt'], -75)

s = DAPSummaryStatsMoments(t_on, t_off, n_summary=n_summary)
G = Default(model=M, prior=prior_unif, summary=s)  # Generator

# Runing the simulation
inf_snpe = SNPE(generator=G, n_components=n_components, n_hiddens=n_hiddens, obs=S,
                reg_lambda=reg_lambda, prior_mixin=prior_mixin, pilot_samples=0)

inf_snpe.standardize_init()


#run
logs, tds, posteriors = inf_snpe.run(n_train=[n_samples], n_rounds=n_rounds,
                                     proposal=prior_unif, monitor=observables,
                                     epochs=epochs, round_cl=round_cl)

# Save parameters
posteriors_means, posteriors_std = [], []
for i, posterior in enumerate(posteriors):
    posteriors_means.append(posterior.mean)
    posteriors_std.append(posterior.std)
                  params=params,
                  dt=x_o['dt'],
                  t_on=t_on,
                  t_off=t_off,
                  n_summary=n_summary,
                  summary_stats=0,
                  data=x_o)

M = DAPSimulator(x_o['I'], x_o['dt'], -75)
s = DAPSummaryStats(t_on, t_off, n_summary=n_summary)
G = Default(model=M, prior=prior_unif, summary=s)  # Generator

# Runing the simulation
inf_snpe = SNPE(generator=G,
                n_components=n_components,
                n_hiddens=n_hiddens,
                obs=S,
                pilot_samples=10,
                prior_norm=True)

logs, tds, posteriors = inf_snpe.run(n_train=[n_samples],
                                     n_rounds=n_rounds,
                                     proposal=prior_unif,
                                     monitor=observables)

# Analyse results
samples_prior = prior_unif.gen(n_samples=int(5e5))
samples_posterior = posteriors[-1].gen(n_samples=int(5e5))

# Plots
x_post = syn_obs_data(i_inj[0], dt, posteriors[-1].mean)
idx = np.arange(0, len(x_o['data']))
Beispiel #5
0
print(prior_min, prior_max, labels)

# Summary Statistics
# calcualte summary statistics
sum_stats_step = DAPSummaryStatsStepMoments(t_on_step, t_off_step, n_summary=17)
sum_stats_mom = DAPSummaryStatsMoments(t_on, t_off, n_summary=17)

s_step = sum_stats_step.calc([x_step])
s_ramp = sum_stats_mom.calc([x_o])
dap1 = DAPSimulatorMultiProtocol(I_all, dt_all, -75)


G = Default(model=dap1, prior=prior_unif, summary=sum_stats_step)  # Generator

# Runing the simulation
inf_snpe = SNPE(generator=G, n_components=n_components, n_hiddens=n_hiddens, obs=s_step,
                reg_lambda=reg_lambda, pilot_samples=0)

logs, tds, posteriors = inf_snpe.run(n_train=[n_samples], n_rounds=n_rounds,
                                     proposal=prior_unif, monitor=observables)


# Analyse results
samples_prior = prior_unif.gen(n_samples=int(5e5))
samples_posterior = posteriors[-1].gen(n_samples=int(5e5))

print('posterior:', posteriors[-1].mean)

x_post = syn_obs_data(I, dt, posteriors[-1].mean)
x_post_step = syn_obs_data(I_step, dt, posteriors[-1].mean)

idx = np.arange(0, len(x_o['data']))
sum_stats = [sum_stats_step, sum_stats_mom]

s_step = sum_stats_step.calc([x_step])
s_ramp = sum_stats_mom.calc([x_ramp])
# S = [s_step, s_ramp]
S = s_step
print('summary starts observed:', S)

print('summary stats ramp:', sum_stats_mom.calc(data_list[0]))
print('summary stats step:', sum_stats_step.calc(data_list[1]))

G = DAPDefault(model=dap1, prior=prior_unif, summary=sum_stats)  # Generator

inf_snpe = SNPE(generator=G,
                n_components=1,
                n_hiddens=[2],
                obs=S,
                reg_lambda=reg_lambda,
                pilot_samples=0)

logs, tds, posteriors = inf_snpe.run(n_train=[10],
                                     n_rounds=1,
                                     proposal=prior_unif)

print('goal parameters:', params[:2])
print('posterior parameters:', posteriors[-1].mean)

# plot inputs
fig, ax = plt.subplots(ncols=1, nrows=2, figsize=(20, 10))
ax[0].plot(Ir)
ax[1].plot(Is)