def load_inference(loglikelihood, working_path, *param_data): """ Load previous simulation results. :param loglikelihood: loglikelihood data of inference :param working_path: Where to print the results :param param_data: List containing: [name, range_min, range_max, resolution, mean, sigma, value] :return: inference object ready to evaluate """ p = [] for item in param_data: p.append( RandomVariable(name=item[0], range_min=float(item[1]), range_max=float(item[2]), resolution=float(item[3]), mean=float(item[4]), sigma=float(item[5]), value=float(item[6]))) pset = ParameterSet(*p) res = Analyse(loglikelihood, pset, working_path) print("Previous inference data result loaded!") return res
def get_default_param(name): Ra = RandomVariable(name='Ra', range_min=50., range_max=150., resolution=10, mean=100., sigma=20.) gpas = RandomVariable(name='gpas', range_min=0.00005, range_max=0.00015, resolution=10, mean=0.0001, sigma=0.00002) cm = RandomVariable(name='cm', range_min=0.5, range_max=1.5, resolution=10, mean=1., sigma=0.2) dict = {'Ra': Ra, 'cm': cm, 'gpas': gpas} return dict[name]
def load_parameter_set(params_data): """ Create ParameterSet object from parameter data :param params_data: Data list of parameters (save_params output) [param1, param2, ...] param1=param_init... :return: ParameterSet object """ from module.probability import RandomVariable, ParameterSet p = [] for item in params_data: p.append( RandomVariable(name=item[0], range_min=float(item[1]), range_max=float(item[2]), resolution=float(item[3]), mean=float(item[4]), sigma=float(item[5]), value=float(item[6]))) p_set = ParameterSet(*p) return p_set
np.random.seed(42) print(t_vec.shape) covmat, invcovmat = inv_cov_mat_sd(aut_corr_func, t_vec) print("Inverse covariance matrix is loaded to memory!") print(invcovmat.shape) # END OF SETTING UP SIMULATION PARAMETERS ----------------------------------------------------------------------------- # Set up parameters using prior information about them (fix the range we are assuming the true parameter) prior_params = [] for idx, item in enumerate(p_names): prior_params.append( RandomVariable(name=item, range_min=p_range[idx][0], range_max=p_range[idx][1], resolution=p_res[idx], sigma=p_std[idx], mean=p_mean[idx])) prior_set = ParameterSet(*prior_params) prior_set.batch_len = batch_size if batch_size is not None: prior_set.isBatch = True else: prior_set.isBatch = False prior_set.create_batch() # Create fixed params sampled from prior fixed_params = sampling_from_prior(prior_set, fixed_param_num) # Save parameter informations
import time num_of_iter = 50 noise_sigma = 7. stim = np.loadtxt( "/Users/Dani/TDK/parameter_estim/stim_protocol2/ramp/stim.txt") Ra_stat = np.zeros((num_of_iter, 6), dtype=np.float) gpas_stat = np.zeros((num_of_iter, 6), dtype=np.float) cm_stat = np.zeros((num_of_iter, 6), dtype=np.float) # Only for plotting pRa = RandomVariable(name='Ra', range_min=50., range_max=150., resolution=40, mean=100., sigma=20) pgpas = RandomVariable(name='gpas', range_min=0.00005, range_max=0.00015, resolution=40, mean=0.0001, sigma=0.00002) pcm = RandomVariable(name='cm', range_min=0.5, range_max=1.5, resolution=40, mean=1., sigma=0.2)
from module.noise import colored from module.trace import stat from module.plot import plot_stat from functools import partial from matplotlib import pyplot as plt import time num_of_iter = 50 Ra_stat = np.zeros((num_of_iter, 5), dtype=np.float) gpas_stat = np.zeros((num_of_iter, 5), dtype=np.float) # Only for plotting pRa = RandomVariable(name='Ra', range_min=80, range_max=230, resolution=80, mean=155, sigma=20) pgpas = RandomVariable(name='gpas', range_min=0.00005, range_max=0.00015, resolution=80, mean=0.0001, sigma=0.00002) # Load inverse covariance matrix for the multivariate normal noise model print("Loading inverse covariance matrix...") inv_covmat = np.genfromtxt( '/Users/Dani/TDK/parameter_estim/stim_protocol/broad_invcovmat.csv') print("Done!")
:param var: Normal distribution variance :return: Lognormal distribution sigma """ return np.sqrt(np.log(1 + var / mean**2)) if __name__ == "__main__": from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm as CM from matplotlib.ticker import LinearLocator, FormatStrFormatter from module.probability import RandomVariable cm = RandomVariable("cm", range_min=0.99999, range_max=1.00001, resolution=10, mean=1., sigma=0.2) gpas = RandomVariable("gpas", range_min=0.00005, range_max=0.00015, resolution=1000, mean=0.0001, sigma=0.00002) d_step = cm.sigma * 1e-6 d_range = [ cm.mean, ] n = 2
for i in range(10): print("\n\n--- SIMULATION FOR %ith FIXED PARAMETER ---" % (i+1)) # Set up "true" value for this cycle current_value = {} for j in range(len(p_names)): val = np.random.normal(p_mean[j], p_std[j]) current_value[p_names[j]] = val # Set up parameters for one cycle current_params = [] for idx, item in enumerate(p_names): current_params.append(RandomVariable(name=item, range_min=p_range[idx][0], range_max=p_range[idx][1], resolution=p_res[idx], value=current_value[item], sigma=p_std[idx], mean=p_mean[idx])) for item in hz: print("\n\n---------------------------------------- Running %i Hz zap protocol" % item) # Stimulus path stim = np.loadtxt("/Users/Dani/TDK/parameter_estim/stim_protocol2/zap/%i/stim.txt" % item) working_path = "/Users/Dani/TDK/parameter_estim/stim_protocol2/zaps/%i(%i)" % (item,i) # Generate deterministic trace and create synthetic data with noise model _, v = model(stype='custom', custom_stim=stim, Ra=current_value['Ra'], gpas=current_value['gpas'], cm=current_value['cm']) # Generate noise_rep synthetic data (noise_rep portion noise realisation) data = more_w_trace(noise, v, noise_rep)
import numpy as np from module.simulation import stick_and_ball from module.probability import RandomVariable, IndependentInference, ParameterSet from module.noise import white from module.plot import fullplot, plot_joint from functools import partial noise_sigma = 7. stim = np.loadtxt("/Users/Dani/TDK/parameter_estim/stim_protocol2/ramp/stim.txt") Ra = RandomVariable(name='Ra', range_min=40., range_max=150., resolution=160, mean=100., sigma=20) gpas = RandomVariable(name='gpas', range_min=0.00005, range_max=0.00015, resolution=160, mean=0.0001, sigma=0.00002) cm = RandomVariable(name='cm', range_min=0.5, range_max=1.7, resolution=160, mean=1., sigma=0.2) # Generate deterministic trace and create synthetic data with noise model # t, v = stick_and_ball(stype='custom', custom_stim=stim) # data = white(noise_sigma, v) # np.savetxt("/Users/Dani/TDK/parameter_estim/stim_protocol2/ramp/single/data(9).txt", data) data = np.loadtxt("/Users/Dani/TDK/parameter_estim/stim_protocol2/ramp/single/data(9).txt") multi_comp = partial(stick_and_ball, stype='custom', custom_stim=stim) # fix chosen stimulus type for simulations Ra_cm_gpas = ParameterSet(Ra, cm, gpas) inference = IndependentInference(model=multi_comp, noise_std=noise_sigma, target_trace=data, parameter_set=Ra_cm_gpas, working_path="/Users/Dani/TDK/parameter_estim/stim_protocol2/ramp/single", speed="min") if __name__ == '__main__': inference.run_sim() # inference.save_result()
from module.noise import white from module.probability import RandomVariable, ParameterSet, IndependentInference from module.simulation import stick_and_ball from module.plot import plot_joint, fullplot noise = 7. # 1.) Parameters to infer cm = RandomVariable(name='cm', range_min=0.5, range_max=1.5, resolution=60, mean=1.2, sigma=0.2, value=1.) gpas = RandomVariable(name='gpas', range_min=0.00005, range_max=0.00015, resolution=60, mean=0.00008, sigma=0.00002, value=0.0001) # Ra = RandomVariable(name='Ra', range_min=50., range_max=150., resolution=60, mean=100., sigma=20.) # 2.) Set up parameter set cm_gpas = ParameterSet(cm, gpas) # 3.) Sythetic data t, v = stick_and_ball() exp_v = white(noise, v)
from module.simulation import one_compartment from module.probability import RandomVariable, DependentInference, ParameterSet from module.noise import colored from module.trace import stat from module.plot import plot_stat from functools import partial import time num_of_iter = 50 cm_stat = np.zeros((num_of_iter, 5), dtype=np.float) gpas_stat = np.zeros((num_of_iter, 5), dtype=np.float) # Only for plotting pcm = RandomVariable(name='cm', range_min=0.5, range_max=1.5, resolution=80, mean=1., sigma=0.2) pgpas = RandomVariable(name='gpas', range_min=0.00005, range_max=0.00015, resolution=100, mean=0.0001, sigma=0.00002) # Load inverse covariance matrix for inference print("Loading inverse covariance matrix...") inv_covmat = np.genfromtxt('/Users/Dani/TDK/parameter_estim/stim_protocol/broad_invcovmat.csv') print("Done!") startTime = time.time() for i in range(num_of_iter): print(str(i) + " is DONE out of " + str(num_of_iter)) # Sampling current parameter from normal distribution current_cm = np.random.normal(pcm.mean, pcm.sigma) current_gpas = np.random.normal(pgpas.mean, pgpas.sigma)
current_mean[item.name] = mean current_minrange.append(mean - item.offset) current_maxrange.append(mean + item.offset) # Biopysicsal parameters can't be negative for idx, item in enumerate(current_minrange): if item <= 0.: current_minrange[idx] = 0.000001 # Set up parameters for one cycle current_params = [] for idx, item in enumerate(p_names): current_params.append( RandomVariable(item, range_min=current_minrange[idx], range_max=current_maxrange[idx], resolution=res[idx], mean=current_mean[item], sigma=fixed_params[idx].sigma)) # Generate deterministic trace and create synthetic data with noise model t, v = model(stype='custom', custom_stim=stim, **current_mean) data = white(noise, v) pset = ParameterSet(*current_params) inf = IndependentInference(data, pset, working_path=working_path, speed=speed) modell = partial(model, stype='custom', custom_stim=stim) if __name__ == '__main__':
cm_mean = 1. cm_sig = 0.2 gpas_mean = 0.0001 gpas_sig = 0.00002 noise_sigma = 7. cm_start = 0.5 cm_end = 1.5 gpas_start = gpas_mean - 0.00005 gpas_end = gpas_mean + 0.00005 # Only for plotting pcm = RandomVariable(name='cm', range_min=cm_start, range_max=cm_end, resolution=100, mean=cm_mean, sigma=cm_sig) pgpas = RandomVariable(name='gpas', range_min=gpas_start, range_max=gpas_end, resolution=100, mean=gpas_mean, sigma=gpas_sig) startTime = time.time() for i in range(num_of_iter): print(str(i) + " is DONE out of " + str(num_of_iter)) # Sampling current parameter from normal distribution current_cm = np.random.normal(cm_mean, cm_sig)