diff --git a/Fitter.py b/Fitter.py index 168cee2..70248c6 100644 --- a/Fitter.py +++ b/Fitter.py @@ -179,7 +179,8 @@ class Fitter: def fit_routine_no_dend_tau(self, start_parameters, error_weights=None): self.counter = 0 - # fit only v_offset, mem_tau, input_scaling, dend_tau + # fit all except dend_tau + self.base_model.parameters["dend_tau"] = 0 x0 = np.array([start_parameters["mem_tau"], start_parameters["noise_strength"], start_parameters["input_scaling"], start_parameters["tau_a"], @@ -189,7 +190,7 @@ class Fitter: # error_list = [error_bf, error_vs, error_sc, error_cv, error_bursty, # error_f_inf, error_f_inf_slope, error_f_zero, error_f_zero_slope_at_straight, error_f0_curve] - fmin = minimize(fun=self.cost_function_all, + fmin = minimize(fun=self.cost_function_no_dend_tau, args=(error_weights,), x0=x0, method="Nelder-Mead", options={"initial_simplex": initial_simplex, "xatol": 0.001, "maxfev": 600, "maxiter": 400}) @@ -208,9 +209,9 @@ class Fitter: print("tried negative parameter value") return 1000 + abs(X[i]) * 10000 - if X[6] > 1.05/self.eod_freq: # refractory period shouldn't be larger than one eod period + if X[5] > 1.05/self.eod_freq: # refractory period shouldn't be larger than one eod period print("tried too large ref period") - return 1000 + abs(X[6]) * 10000 + return 1000 + abs(X[5]) * 10000 self.base_model.set_variable("mem_tau", X[0]) self.base_model.set_variable("noise_strength", X[1]) self.base_model.set_variable("input_scaling", X[2]) @@ -238,7 +239,7 @@ class Fitter: def fit_routine_no_ref_period(self, start_parameters, error_weights=None): self.counter = 0 - # fit only v_offset, mem_tau, input_scaling, dend_tau + # fit all except ref_period self.base_model.set_variable("refractory_period", 0) @@ -250,7 +251,7 @@ class Fitter: # error_list = [error_bf, error_vs, error_sc, error_cv, error_bursty, # error_f_inf, error_f_inf_slope, error_f_zero, error_f_zero_slope_at_straight, error_f0_curve] - fmin = minimize(fun=self.cost_function_all, + fmin = minimize(fun=self.cost_function_no_ref_period, args=(error_weights,), x0=x0, method="Nelder-Mead", options={"initial_simplex": initial_simplex, "xatol": 0.001, "maxfev": 600, "maxiter": 400}) @@ -269,9 +270,6 @@ class Fitter: print("tried negative parameter value") return 1000 + abs(X[i]) * 10000 - if X[6] > 1.05/self.eod_freq: # refractory period shouldn't be larger than one eod period - print("tried too large ref period") - return 1000 + abs(X[6]) * 10000 self.base_model.set_variable("mem_tau", X[0]) self.base_model.set_variable("noise_strength", X[1]) self.base_model.set_variable("input_scaling", X[2]) @@ -299,9 +297,10 @@ class Fitter: def fit_routine_no_dend_tau_and_no_ref_period(self, start_parameters, error_weights=None): self.counter = 0 - # fit only v_offset, mem_tau, input_scaling, dend_tau - self.base_model.set_variable("refractory_period", 0) + # fit all except dend_tau and ref_period + self.base_model.parameters["refractory_period"] = 0 self.base_model.parameters["dend_tau"] = 0 + x0 = np.array([start_parameters["mem_tau"], start_parameters["noise_strength"], start_parameters["input_scaling"], start_parameters["tau_a"], start_parameters["delta_a"]]) initial_simplex = create_init_simples(x0, search_scale=3) @@ -309,7 +308,7 @@ class Fitter: # error_list = [error_bf, error_vs, error_sc, error_cv, error_bursty, # error_f_inf, error_f_inf_slope, error_f_zero, error_f_zero_slope_at_straight, error_f0_curve] - fmin = minimize(fun=self.cost_function_all, + fmin = minimize(fun=self.cost_function_no_dend_tau_and_no_ref_period, args=(error_weights,), x0=x0, method="Nelder-Mead", options={"initial_simplex": initial_simplex, "xatol": 0.001, "maxfev": 600, "maxiter": 400}) @@ -328,9 +327,6 @@ class Fitter: print("tried negative parameter value") return 1000 + abs(X[i]) * 10000 - if X[6] > 1.05/self.eod_freq: # refractory period shouldn't be larger than one eod period - print("tried too large ref period") - return 1000 + abs(X[6]) * 10000 self.base_model.set_variable("mem_tau", X[0]) self.base_model.set_variable("noise_strength", X[1]) self.base_model.set_variable("input_scaling", X[2]) diff --git a/models/LIFACnoise.py b/models/LIFACnoise.py index 544fc2e..648d26a 100644 --- a/models/LIFACnoise.py +++ b/models/LIFACnoise.py @@ -229,7 +229,8 @@ def binary_search_base_freq(model: LifacNoiseModel, base_stimulus, goal_frequenc raise ValueError("binary_search_base_freq() - LifacNoiseModel: Goal frequency might be nan?") if abs(upper_bound - lower_bound) < 0.0001: - print("v_offset search stopped. bounds converged") + print("v_offset search stopped. bounds converged! freq: {:.2f}, lower {:.1f}, upper: {:.1f}" + .format(frequency, lower_bound, upper_bound)) warn("Search was stopped. Upper and lower bounds converged without finding a value closer than threshold!") return middle diff --git a/random_models.py b/random_models.py index 79fda9b..d02b870 100644 --- a/random_models.py +++ b/random_models.py @@ -13,23 +13,64 @@ def main(): param_values = get_parameter_distributions(folder) plot_distributions(param_values) + get_gauss_fits(param_values) pass +def get_gauss_fits(param_values): + gauss_fits = {} + num_of_bins = 30 + + # keys = ['delta_a', 'dend_tau', 'input_scaling', 'mem_tau', 'noise_strength', 'refractory_period', 'tau_a', 'v_offset'] + + key = 'v_offset' + + values = param_values[key] + + bins = calculate_bins(values, num_of_bins) + normal, n_bins, patches = plt.hist(values, bins=bins, density=True) + plt.close() + + # fit gauss: + bin_width = np.mean(np.diff(n_bins)) + middle_of_bins = n_bins + bin_width / 2 + + # try: + # n_gauss_pars = fit_gauss(middle_of_bins[:-1], normal) + # x = np.arange(min(param_values[key]), max(param_values[key]), + # (max(param_values[key]) - min(param_values[key])) / 100) + # axes[i, 0].plot(x, fu.gauss(x, n_gauss_pars[0], n_gauss_pars[1], n_gauss_pars[2])) + # pass + + +def calculate_bins(values, num_of_bins): + minimum = np.min(values) + maximum = np.max(values) + step = (maximum - minimum) / (num_of_bins-1) + + bins = np.arange(minimum-0.5*step, maximum + step, step) + print(minimum, maximum) + print(bins) + return bins + + def plot_distributions(param_values): bin_number = 30 fig, axes = plt.subplots(len(param_values.keys()), 2) - + print(sorted(param_values.keys())) for i, key in enumerate(sorted(param_values.keys())): # normal hist: values = param_values[key] - normal, n_bins, patches = axes[i, 0].hist(values, bins=bin_number, density=True) + bins = calculate_bins(values, bin_number) + print("bins:", len(bins)) + normal, n_bins, patches = axes[i, 0].hist(values, bins=calculate_bins(values, bin_number), density=True) axes[i, 0].set_title(key) # fit gauss: bin_width = np.mean(np.diff(n_bins)) middle_of_bins = n_bins + bin_width / 2 + print("mid_bins:", len(middle_of_bins)) axes[i, 0].plot(middle_of_bins[:-1], normal, 'o') try: n_gauss_pars = fit_gauss(middle_of_bins[:-1], normal) diff --git a/run_Fitter.py b/run_Fitter.py index 8667052..0bd85df 100644 --- a/run_Fitter.py +++ b/run_Fitter.py @@ -14,8 +14,8 @@ from helperFunctions import plot_errors import multiprocessing as mp -SAVE_DIRECTORY = "./results/final_3/" -SAVE_DIRECTORY_BEST = "./results/final_3_best/" +SAVE_DIRECTORY = "./results/ref_and_tau/no_ref_period" +SAVE_DIRECTORY_BEST = "./results/ref_and_tau/nrp_best" # [vs, sc, cv, isi_hist, bursty, f_inf, f_inf_slope, f_zero, f_zero_slope, f0_curve] ERROR_WEIGHTS = (1, 1, 1, 1, 1, 1, 1, 1, 0, 1) @@ -31,7 +31,7 @@ def main(): quit() # test_single_cell("data/invivo/2010-11-08-al-invivo-1/") - test_single_cell("data/invivo_bursty/2013-04-09-ac-invivo-1/") + test_single_cell("data/ref_and_dend_set/2012-12-21-am-invivo-1/") # start_parameters = [p for p in iget_start_parameters()] # cell_data = CellData("data/invivo_bursty/2014-03-19-ae-invivo-1/") @@ -60,7 +60,7 @@ def fit_cell_base(parameters): time1 = time.time() fitter = Fitter() fitter.set_data_reference_values(parameters[0]) - fmin, res_par = fitter.fit_routine(parameters[2], ERROR_WEIGHTS) + fmin, res_par = fitter.fit_routine_no_ref_period(parameters[2], ERROR_WEIGHTS) cell_data = parameters[0] cell_name = os.path.split(cell_data.get_data_path())[-1] diff --git a/run_cellwise_fitting.sh b/run_cellwise_fitting.sh index 66f9042..62115e9 100755 --- a/run_cellwise_fitting.sh +++ b/run_cellwise_fitting.sh @@ -1,5 +1,5 @@ -for file in data/final/*; do +for file in data/ref_and_dend_set/*; do if [ -d "$file" ]; then nice python3 run_Fitter.py --cell $file fi diff --git a/thesis/figures/behaviour_correlations.png b/thesis/figures/behaviour_correlations.png index b6b31b2..e93e395 100644 Binary files a/thesis/figures/behaviour_correlations.png and b/thesis/figures/behaviour_correlations.png differ diff --git a/thesis/figures/fit_adaption_comparison.png b/thesis/figures/fit_adaption_comparison.png index f7aa785..8d44de9 100644 Binary files a/thesis/figures/fit_adaption_comparison.png and b/thesis/figures/fit_adaption_comparison.png differ diff --git a/thesis/figures/fit_baseline_comparison.png b/thesis/figures/fit_baseline_comparison.png index 827b14b..edf3350 100644 Binary files a/thesis/figures/fit_baseline_comparison.png and b/thesis/figures/fit_baseline_comparison.png differ diff --git a/thesis/figures/fit_burstiness_comparison.png b/thesis/figures/fit_burstiness_comparison.png index 3a5004b..d498530 100644 Binary files a/thesis/figures/fit_burstiness_comparison.png and b/thesis/figures/fit_burstiness_comparison.png differ diff --git a/thesis/figures/parameter_correlations.png b/thesis/figures/parameter_correlations.png index 1d1ee2f..f763b2c 100644 Binary files a/thesis/figures/parameter_correlations.png and b/thesis/figures/parameter_correlations.png differ