def _plot_power_each_mouse(res, figure_path, excitatory, valence): res[ykey_b] /= 100 ax_lim = {'yticks': [0, .5, 1], 'ylim': [0, 1.05]} name_str = '_E' if excitatory else '_I' name_str += '_' + valence for mouse in np.unique(res['mouse']): path, name = plot.plot_results(res, x_key=xkey_b, y_key=ykey_b, loop_keys='mouse', select_dict={'mouse': mouse}, plot_args=trace_args_bhv, path=figure_path, save=False, colors=['k'], ax_args=ax_lim) plot.plot_results(res, x_key='trials', y_key='power', loop_keys='mouse', select_dict={'mouse': mouse}, plot_args=trace_args, path=figure_path, reuse=True, save=False, colors=['k'], ax_args=ax_lim) plt.gca().set_xlim(left=0) plt.gca().set_ylim(bottom=-0.05) plt.legend(['neural', 'behavior'], frameon=False, loc=0) plot._easy_save(path, name + name_str)
def plot_max_dff_valence(res, start_days, end_days, figure_path): res = copy.copy(res) # list_of_days = list(zip(start_days, end_days)) list_of_days = end_days start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) start_end_day_res = filter.filter(start_end_day_res, {'odor_valence': ['CS+', 'CS-']}) _max_dff(start_end_day_res) start_end_day_res = reduce.new_filter_reduce( start_end_day_res, filter_keys=['odor_valence', 'mouse'], reduce_key='max_dff') add_naive_learned(start_end_day_res, start_days, end_days) ax_args_copy = ax_args.copy() # ax_args_copy.update({'xticks':[res['DAQ_O_ON_F'][-1], res['DAQ_W_ON_F'][-1]], 'xticklabels':['ON', 'US'], # 'ylim':[0, .2]}) nMice = len(np.unique(res['mouse'])) # colors = ['Green'] * nMice + ['Red'] * nMice # trace_args_copy = trace_args.copy() # trace_args_copy.update({'linestyle':'--','alpha':.5, 'linewidth':.75}) plot.plot_results(start_end_day_res, loop_keys='mouse', x_key='odor_valence', y_key='max_dff', path=figure_path, colors=['gray'] * 10, legend=False, fig_size=(2, 1.5))
def plot_compare_responsive(res, figure_path): ax_args_copy = ax_args.copy() ax_args_copy.update({ 'ylim': [0, .65], 'yticks': [0, .2, .4, .6], 'xticks': list(range(20)) }) res = copy.copy(res) res = filter.filter(res, {'odor_valence': ['CS+', 'CS-']}) res_ = get_compare_responsive_sig(res) line_args_copy = line_args.copy() line_args_copy.update({ 'marker': '.', 'linestyle': '--', 'linewidth': .5, 'alpha': .75 }) plot.plot_results(res_, x_key='day', y_key='Fraction', loop_keys=['mouse', 'odor_valence'], colors=['green', 'red'] * 10, path=figure_path, plot_args=line_args_copy, ax_args=ax_args_copy, fig_size=(2, 1.5), legend=False)
def plot_data(appended_files_data, merged_results): try: choosen_plots = [] possible_plots = [] for strategie in merged_results: possible_plots.append(strategie) print("\nPossible plots:") for i, p_p in enumerate(possible_plots): print(i, "-", p_p) input_plots = input("\nPlots: ") if input_plots != "": input_plots_arr = input_plots.split(" ") for i in input_plots_arr: choosen_plots.append(possible_plots[int(i)]) for p in choosen_plots: plot_results(appended_files_data, merged_results[p]) print("\nPlots choosen:") print(choosen_plots) except: print("Plot error.") exit()
def plot_simulation_results(simulation_results, title=None, savefig=None): x = list(range(1, simulation_results['generation_number'] + 1)) datasets = [ { 'y': simulation_results['average_fitnesses'], 'x': x, 'label': 'Average fitness' }, { 'y': simulation_results['best_fitnesses'], 'x': x, 'label': 'Best fitness' } ] if simulation_results['simulation'].get('plot_sigmas', True): datasets.append({ 'y': simulation_results['sigmas'], 'x': x, 'label': r'$\sigma$' }) title = title or "{} {} generations".format( simulation_results['simulation']['problem'].NAME, simulation_results['generation_number'] ) plot_results(datasets, xlabel='generations', ylabel='fitness', title=title, savefig=savefig)
def _example_velocity(res, save_path): xkey = 'trial' ykey = 'velocity' line_args = {'alpha': .5, 'linewidth': .25, 'marker': 'o', 'markersize': 0} mouse = 0 odor = 'PT CS+' temp = filter.filter(res, {'odor_valence': odor, 'mouse': mouse}) start = temp['on'][0] off = temp['off'][0] end = temp['end'][0] ax_args = { 'xticks': [start, off, end], 'xticklabels': ['ON', 'OFF', 'US'], 'ylim': [-5, 100] } for i, v in enumerate(temp[ykey]): v_ = savgol_filter(v, window_length=41, polyorder=0) temp[ykey][i] = v_ plot.plot_results(temp, x_key=xkey, y_key=ykey, loop_keys=['day', 'ix'], select_dict={ 'odor_valence': odor, 'mouse': mouse }, colors=['black'] * 200, plot_args=line_args, ax_args=ax_args, legend=False, path=save_path)
def plot_overlap_water(res, start_days, end_days, figure_path): ax_args_copy = overlap_ax_args.copy() res = copy.copy(res) mice = np.unique(res['mouse']) res = filter.filter_days_per_mouse(res, days_per_mouse=end_days) add_naive_learned(res, start_days, end_days) ax_args_copy.update({'xlim': [-1, 2]}) y_keys = ['US/CS+', 'CS+/US'] summary_res = defaultdict(list) for arg in y_keys: _get_overlap_water(res, arg=arg) new_res = reduce.new_filter_reduce( res, filter_keys=['mouse', 'day', 'odor_valence'], reduce_key='Overlap') new_res['Type'] = np.array([arg] * len(new_res['training_day'])) reduce.chain_defaultdicts(summary_res, new_res) summary_res.pop('Overlap_sem') summary_res.pop('Overlap_std') summary_res = filter.filter(summary_res, {'odor_valence': 'CS+'}) mean_std_res = reduce.new_filter_reduce(summary_res, filter_keys='Type', reduce_key='Overlap') types = np.unique(summary_res['Type']) scatter_args_copy = scatter_args.copy() scatter_args_copy.update({'s': 2, 'alpha': .6}) for i, type in enumerate(types): reuse_arg = True if i == 0: reuse_arg = False temp = filter.filter(summary_res, {'Type': type}) plot.plot_results(temp, x_key='Type', y_key='Overlap', loop_keys='mouse', colors=['Black'] * len(mice), plot_function=plt.scatter, path=figure_path, plot_args=scatter_args_copy, ax_args=ax_args_copy, save=False, reuse=reuse_arg, fig_size=(1.5, 1.5), rect=(.25, .25, .6, .6), legend=False) plot.plot_results(mean_std_res, x_key='Type', y_key='Overlap', error_key='Overlap_sem', path=figure_path, plot_function=plt.errorbar, plot_args=error_args, ax_args=ax_args, save=True, reuse=True, fig_size=(1.5, 1.5), legend=False) print(mean_std_res['Overlap'])
def plot_vm(): plot.plot_results( 'visual-matching.json', "Visual matching: dependency of score on errors and reaction", 'visual-matching.png', delays=[ *range(20, 200, 20), *range(200, 1200 + 1, 40), ], max_errors=MAX_ERRORS, )
def plot_sr(): plot.plot_results( 'simple-reaction.json', "Simple reaction time: dependency of score on errors and reaction", 'simple-reaction-time.png', delays=[ *range(20, 200, 10), *range(200, 1010, 20), ], max_errors=MAX_ERRORS, )
def test_matching(self): n_categories = 10 confusion = torch.zeros(n_categories, n_categories).long().cpu() for i, (sample, labels) in enumerate(self.test_loader_target): sample_digit = Variable(sample) if self.cuda: sample_digit = sample_digit.cuda() sample_digit = self.model_target.encoder_only(sample_digit.view(-1, 784)) sample_digit = self.model_source.decode(sample_digit) results = self.classify_model.test(sample_digit).cpu() for index, label in enumerate(labels): confusion[label][results[index]] += 1 plot_results(confusion, self.graph) return calculate_accuracy(confusion)
def plot_bar(res, days_per_mouse, odor_valence, day_pad, save, reuse, figure_path, color='black', normalize=False): res = copy.copy(res) res['day_'] = np.zeros_like(res['day']) res_ = defaultdict(list) for i, days in enumerate(days_per_mouse): temp = filter.filter_days_per_mouse(res, days_per_mouse=days) temp = filter.filter(temp, {'odor_valence': odor_valence[i]}) temp['day_'] = np.array([i + day_pad] * len(temp['day_'])) reduce.chain_defaultdicts(res_, temp) _max_dff(res_) res_ = reduce.new_filter_reduce( res_, filter_keys=['odor_valence', 'mouse', 'day_'], reduce_key='max_dff') res_.pop('max_dff_sem') summary = reduce.new_filter_reduce(res_, filter_keys=['day_', 'odor_valence'], reduce_key='max_dff') if normalize: _normalize_across_days(summary) # plot.plot_results(summary, x_key='day_', y_key='max_dff', error_key='max_dff_sem', # path=figure_path, # colors='black', legend=False, plot_args=error_args, plot_function= plt.errorbar, # fig_size=(2, 1.5), save=False, reuse=reuse) line_args_copy = line_args.copy() line_args_copy.update({'alpha': .75, 'linewidth': 1}) plot.plot_results(summary, x_key='day_', y_key='max_dff', path=figure_path, colors=color, legend=False, plot_args=line_args_copy, fig_size=(2, 1.5), save=save, reuse=reuse, name_str=odor_valence[-1])
def _plot_power_statistics(res_, figure_path, excitatory, valence): name_str = '_E' if excitatory else '_I' name_str += '_' + valence _ = reduce.new_filter_reduce(res_, filter_keys=['mouse', 'odor_valence'], reduce_key='half_lick') res = reduce.new_filter_reduce(res_, filter_keys=['mouse', 'odor_valence'], reduce_key='half_power') res['half_lick'] = _['half_lick'] from sklearn import linear_model from sklearn.metrics import mean_squared_error, r2_score regr = linear_model.LinearRegression() regr.fit(res['half_lick'].reshape(-1, 1), res['half_power'].reshape(-1, 1)) y_pred = regr.predict(res['half_lick'].reshape(-1, 1)) score = regr.score(res['half_lick'].reshape(-1, 1), res['half_power'].reshape(-1, 1)) lim = [10, 50] ax_lim = {'xlim': lim, 'ylim': lim} a, b = plot.plot_results(res, x_key='half_lick', y_key='half_power', plot_args=scatter_args, plot_function=plt.scatter, path=figure_path, save=False, ax_args=ax_lim) plt.plot(lim, lim, '--', color='red', alpha=.5, linewidth=1) plt.text(25, lim[1], 'R = {:.2f}'.format(score), fontsize=5) plot._easy_save(a, b + name_str, pdf=True)
def result_log(): dm.reload_credentials() dm.load_results() team_id = int(request.args.get('tid')) check_id = int(request.args.get('cid')) results = sorted(dm.results[team_id][check_id], key= lambda r: r.time, reverse=True) fname = plot.plot_results(results) return render_template('result_log.html', results=results, fname=fname)
def start_evaluation(args): device = util.setup_torch() num_classes = 100 if args.dataset == "cifar100" else 10 train_loader, test_loader = get_cifar(num_classes, batch_size=args.batch_size) # for benchmarking, decided whether we want to use unique test folders if USE_ID: test_id = util.generate_id() else: test_id = "" results_dir = Path(args.results_dir).joinpath(test_id) results_dir = Path(results_dir).joinpath(args.dataset) util.check_dir(results_dir) # Parsing arguments and prepare settings for training params = { "epochs": args.epochs, "modes": args.modes, "t_checkpoint": args.t_checkpoint, "results_dir": results_dir, "train_loader": train_loader, "test_loader": test_loader, "batch_size": args.batch_size, # model configuration "device": device, "teacher_name": args.t_name, "student_name": args.s_name, "num_classes": num_classes, # hyperparameters "weight_decay": args.weight_decay, "learning_rate": args.learning_rate, "momentum": args.momentum, "sched": "multisteplr", "optim": "SGD", # fixed knowledge distillation parameters "lambda_student": 0.5, "T_student": 5, } test_conf_name = results_dir.joinpath("test_config.json") util.dump_json_config(test_conf_name, params) run_benchmarks(args.modes, params, args.s_name, args.t_name) plot_results(results_dir, test_id=test_id)
def main(energy, density, temperature=100): from math import exp from random import random from plot import plot_results if type(density) != list: raise TypeError("The density matrix is not in the proper form!") #Store the initial density. initial_density = list(density) #Calculate the current energy. current_energy = energy(density) current_ite = 0 while current_ite < max_iterations: #Calculate the new density matrix. updated_density = update_density(density) print(updated_density) #Calculate the new energy. updated_energy = energy(updated_density) #Decide if the new energy must be accepted or not. if updated_energy < current_energy: density = updated_density current_energy = updated_energy else: P0 = exp( -(updated_energy - current_energy) / temperature) > random() if P0: density = updated_density current_energy = updated_energy current_ite += 1 print("The simulation has finished!") plot_results('output', initial_density, density) return
def plot_summary_water(res, start_days, end_days, figure_path): ax_args_copy = ax_args.copy() res = copy.copy(res) get_responsive_cells(res) list_of_days = list(zip(start_days, end_days)) mice = np.unique(res['mouse']) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) add_naive_learned(start_end_day_res, start_days, end_days, 'a', 'b') odor_list = ['US'] colors = ['Turquoise'] ax_args_copy.update({'xlim': [-1, 2]}) for i, odor in enumerate(odor_list): plot.plot_results(start_end_day_res, select_dict={'odor_standard': odor}, x_key='training_day', y_key='Fraction Responsive', loop_keys='mouse', colors=[colors[i]] * len(mice), path=figure_path, plot_args=line_args, ax_args=ax_args_copy, fig_size=(1.6, 1.5), legend=False) before_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'a', 'odor_standard': 'US' }) after_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'b', 'odor_standard': 'US' }) from scipy.stats import ranksums, wilcoxon, kruskal print('Before PT CS+: {}'.format(np.mean( before_csm['Fraction Responsive']))) print('After PT CS+: {}'.format(np.mean(after_csm['Fraction Responsive']))) print('Wilcoxin:{}'.format( wilcoxon(before_csm['Fraction Responsive'], after_csm['Fraction Responsive'])))
def plot_individual(res, lick_res, figure_path): ax_args_copy = ax_args.copy() ax_args_copy.update({'ylim': [0, .65], 'yticks': [0, .3, .6]}) overlap_ax_args_copy = overlap_ax_args.copy() res = copy.copy(res) get_responsive_cells(res) summary_res = reduce.new_filter_reduce( res, reduce_key='Fraction Responsive', filter_keys=['odor_valence', 'mouse', 'day']) summary_res = filter.filter(summary_res, {'odor_valence': 'CS+'}) lick_res = filter.filter(lick_res, {'odor_valence': 'CS+'}) for mouse in np.unique(summary_res['mouse']): select_dict = {'mouse': mouse} plot.plot_results(summary_res, x_key='day', y_key='Fraction Responsive', loop_keys='odor_valence', colors=['green', 'red', 'turquoise'], select_dict=select_dict, path=figure_path, ax_args=ax_args_copy, plot_args=line_args, save=False, sort=True) plot.plot_results(lick_res, x_key='day', y_key='lick_boolean', loop_keys='odor_valence', select_dict={'mouse': mouse}, colors=['green', 'red'], ax_args=overlap_ax_args_copy, plot_args=behavior_line_args, path=figure_path, reuse=True, save=True, twinax=True)
def _plot_power_every_mouse(res, figure_path, excitatory, valence): ax_lim = {'yticks': [0, .5, 1], 'ylim': [0, 1.05]} name_str = '_E' if excitatory else '_I' name_str += '_' + valence # plot.plot_results(res, x_key=xkey_b, y_key=ykey_b, loop_keys='mouse', # plot_args=trace_args_bhv, path=figure_path, # save=False, reuse=False, # ax_args = ax_lim, # name_str=name_str) plt.gca().set_xlim(left=0) plt.gca().set_ylim(bottom=-0.05) plot.plot_results(res, x_key='trials', y_key='power', loop_keys='mouse', plot_args=trace_args, path=figure_path, save=True, reuse=False, ax_args=ax_lim, legend=False, name_str=name_str)
def _average_velocity(res, save_path): xkey = 'trial' ykey = 'velocity' error_key = ykey + '_sem' color_dict = {'CS+': 'green', 'CS-': 'red', 'PT CS+': 'C1'} valences = [['CS+', 'CS-', 'PT CS+'], ['CS+', 'CS-'], ['PT CS+']] for valence in valences: colors = [color_dict[x] for x in valence] temp = filter.filter(res, {'odor_valence': valence}) start = temp['on'][0] off = temp['off'][0] end = temp['end'][0] ax_args = { 'xticks': [start, off, end], 'xticklabels': ['ON', 'OFF', 'US'], 'ylim': [-5, 50] } mean_res = reduce.new_filter_reduce(temp, filter_keys=['odor_valence'], reduce_key=ykey) for i, v in enumerate(mean_res[ykey]): v_ = savgol_filter(v, window_length=21, polyorder=0) mean_res[ykey][i] = v_ for i, v in enumerate(mean_res[error_key]): v_ = savgol_filter(v, window_length=21, polyorder=0) mean_res[error_key][i] = v_ plot.plot_results(mean_res, x_key=xkey, y_key=ykey, loop_keys='odor_valence', select_dict={'odor_valence': valence}, error_key=error_key, plot_function=plt.fill_between, colors=colors, plot_args=fill_args, ax_args=ax_args, path=save_path)
def _helper(plot_res, color='green'): mean_std_res = reduce.new_filter_reduce( plot_res, reduce_key='PCA Distance', filter_keys='title') titles = np.unique(plot_res['title']) summary_ax_args_ = copy.copy(summary_ax_args) summary_ax_args_.update({'xlim': [-1, len(titles)]}) plot.plot_results(plot_res, x_key='title', y_key='PCA Distance', loop_keys='mouse', select_dict={'title': titles}, colors=[color] * mice.size, ax_args=summary_ax_args_, plot_function=plt.plot, plot_args=summary_line_args, path=figure_path, save=False) plot.plot_results(mean_std_res, x_key='title', y_key='PCA Distance', select_dict={'title': titles}, colors=color, plot_function=plt.plot, ax_args=ax_args, plot_args=line_args, path=figure_path, reuse=True, save=False) plot.plot_results(mean_std_res, x_key='title', y_key='PCA Distance', select_dict={'title': titles}, error_key='PCA Distance_sem', colors=color, plot_function=plt.errorbar, ax_args=summary_ax_args_, plot_args=error_args, legend=False, path=figure_path, reuse=True, save=True)
def plot_reversal(res, start_days, end_days, figure_path): ax_args_copy = ax_args.copy() ax_args_copy.update({'ylim': [0, .6]}) res = copy.copy(res) list_of_days = list(zip(start_days, end_days)) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) reversal_res, stats_res = get_reversal_sig(start_end_day_res) filter.assign_composite(reversal_res, loop_keys=['day', 'odor_valence']) import seaborn as sns swarm_args = { 'marker': '.', 'size': 8, 'facecolors': 'none', 'alpha': .5, 'palette': ['green', 'red'], 'jitter': .1 } mean_res = reduce.new_filter_reduce(reversal_res, filter_keys=['day', 'odor_valence'], reduce_key='Fraction') plot.plot_results(mean_res, x_key='day_odor_valence', y_key='Fraction', error_key='Fraction_sem', path=figure_path, plot_function=plt.errorbar, plot_args=error_args, ax_args=ax_args_copy, fig_size=(2, 1.5), save=False) plt.plot([1.5, 1.5], plt.ylim(), '--', color='gray', linewidth=2) plot.plot_results( reversal_res, x_key='day_odor_valence', y_key='Fraction', path=figure_path, colors=['Green', 'Red', 'Green', 'Red'], # plot_function=plt.scatter, plot_args=scatter_args, plot_function=sns.stripplot, plot_args=swarm_args, ax_args=ax_args_copy, fig_size=(2, 1.5), reuse=True, save=True, legend=False) print(mean_res['day_odor_valence']) print(mean_res['Fraction']) from scipy.stats import wilcoxon ix_before_p = reversal_res['day_odor_valence'] == 'Lrn_CS+' ix_after_p = reversal_res['day_odor_valence'] == 'Rev_CS+' ix_before_m = reversal_res['day_odor_valence'] == 'Lrn_CS-' ix_after_m = reversal_res['day_odor_valence'] == 'Rev_CS-' stat_csp = wilcoxon(reversal_res['Fraction'][ix_before_p], reversal_res['Fraction'][ix_after_p]) stat_csm = wilcoxon(reversal_res['Fraction'][ix_before_m], reversal_res['Fraction'][ix_after_m]) print('CS+ to CS-: {}'.format(stat_csp)) print('CS- to CS+: {}'.format(stat_csm)) titles = ['', 'CS+', 'CS-', 'None'] conditions = [['none-p', 'p-m', 'p-none', 'p-p'], ['p-m', 'p-none', 'p-p'], ['m-m', 'm-none', 'm-p'], ['none-m', 'none-none', 'none-p']] labels = [['Added', 'Reversed', 'Lost', 'Retained'], ['Reversed', 'Lost', 'Retained'], ['Retained', 'Lost', 'Reversed'], ['to CS-', 'Retained', 'to CS+']] for i, title in enumerate(titles): mean_stats = reduce.new_filter_reduce(stats_res, filter_keys='condition', reduce_key='Fraction') ax_args_copy.update({ 'ylim': [-.1, 1], 'yticks': [0, .5, 1], 'xticks': [0, 1, 2, 3], 'xticklabels': labels[i] }) plot.plot_results(mean_stats, select_dict={'condition': conditions[i]}, x_key='condition', y_key='Fraction', loop_keys='mouse', error_key='Fraction_sem', sort=True, path=figure_path, colors=['Black'] * 10, plot_function=plt.errorbar, plot_args=error_args, ax_args=ax_args_copy, fig_size=(2, 1.5), save=False) plt.title(title) plot.plot_results(stats_res, select_dict={'condition': conditions[i]}, x_key='condition', y_key='Fraction', loop_keys='mouse', sort=True, path=figure_path, colors=['Black'] * 10, plot_function=plt.scatter, plot_args=scatter_args, ax_args=ax_args_copy, fig_size=(2, 1.5), legend=False, save=True, reuse=True) print(mean_stats['Fraction']) print(mean_res['Fraction'])
} } G = 100 N = 100 X = list(range(1, G + 1)) results = [ { 'x': X, 'y': average_n_runs(simulation, N, G)['average_fitnesses'], 'label': "All 1s" } ] target = list(random.getrandbits(1) for _ in range(40)) simulation.update({'target_phenotype': target}) results.append({ 'x': X, 'y': average_n_runs(simulation, N, G)['average_fitnesses'], 'label': "Random string" }) plot_results( datasets=results, savefig="../report/img/{}.png".format(datetime.datetime.now()), xlabel="Generation number", ylabel="Fitness", title="Different target strings", ncol=2 )
def plot_summary_odor_pretraining(res, start_days, end_days, arg_naive, figure_path, save, excitatory=True): ax_args_copy = ax_args.copy() res = copy.copy(res) list_of_days = list(zip(start_days, end_days)) mice = np.unique(res['mouse']) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) get_responsive_cells(start_end_day_res) if arg_naive: day_start = filter.filter(start_end_day_res, {'odor_standard': 'PT Naive'}) day_start['odor_standard'] = np.array(['PT CS+'] * len(day_start['odor_standard'])) day_end = filter.filter_days_per_mouse(start_end_day_res, days_per_mouse=end_days) day_end = filter.filter(day_end, {'odor_standard': 'PT CS+'}) reduce.chain_defaultdicts(day_start, day_end) start_end_day_res = day_start else: start_end_day_res = filter.exclude(start_end_day_res, {'odor_standard': 'PT Naive'}) add_naive_learned(start_end_day_res, start_days, end_days, 'a', 'b') filter.assign_composite(start_end_day_res, loop_keys=['odor_standard', 'training_day']) odor_list = ['PT CS+'] colors = ['Orange'] ax_args_copy = ax_args_copy.copy() ax_args_copy.update({ 'xlim': [-1, 10], 'ylim': [0, .4], 'yticks': [0, .1, .2, .3, .4] }) for i, odor in enumerate(odor_list): save_arg = False reuse_arg = True if i == 0: reuse_arg = False if save and i == len(odor_list) - 1: save_arg = True plot.plot_results(start_end_day_res, select_dict={'odor_standard': odor}, x_key='odor_standard_training_day', y_key='Fraction Responsive', loop_keys='mouse', colors=[colors[i]] * len(mice), path=figure_path, plot_args=line_args, ax_args=ax_args_copy, save=save_arg, reuse=reuse_arg, fig_size=(2.5, 1.5), legend=False, name_str='_E' if excitatory else '_I') before_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'a', 'odor_standard': 'PT CS+' }) after_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'b', 'odor_standard': 'PT CS+' }) from scipy.stats import ranksums, wilcoxon, kruskal print('Before PT CS+: {}'.format(np.mean( before_csm['Fraction Responsive']))) print('After PT CS+: {}'.format(np.mean(after_csm['Fraction Responsive']))) from scipy.stats import sem print('After PT CS+ STD: {}'.format(sem(after_csm['Fraction Responsive']))) print('Wilcoxin:{}'.format( wilcoxon(before_csm['Fraction Responsive'], after_csm['Fraction Responsive'])))
def plot_summary_odor(res, start_days, end_days, use_colors=True, figure_path=None, reuse=False, excitatory=True): ax_args_copy = ax_args.copy() res = copy.copy(res) get_responsive_cells(res) list_of_days = list(zip(start_days, end_days)) mice = np.unique(res['mouse']) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) add_naive_learned(start_end_day_res, start_days, end_days, 'a', 'b') filter.assign_composite(start_end_day_res, loop_keys=['odor_valence', 'training_day']) start_end_day_res = reduce.new_filter_reduce( start_end_day_res, filter_keys=['training_day', 'mouse', 'odor_valence'], reduce_key='Fraction Responsive') odor_list = ['CS+', 'CS-'] if use_colors: colors = ['Green', 'Red'] else: colors = ['Black'] * 2 ax_args_copy = ax_args_copy.copy() ax_args_copy.update({ 'xlim': [-1, 8], 'ylim': [0, .4], 'yticks': [0, .1, .2, .3, .4] }) name_str = '_E' if excitatory else '_I' for i, odor in enumerate(odor_list): save_arg = False reuse_arg = True if i == 0 and not reuse: reuse_arg = False if i == len(odor_list) - 1: save_arg = True plot.plot_results(start_end_day_res, select_dict={'odor_valence': odor}, x_key='odor_valence_training_day', y_key='Fraction Responsive', loop_keys='mouse', colors=[colors[i]] * len(mice), path=figure_path, plot_args=line_args, ax_args=ax_args_copy, save=save_arg, reuse=reuse_arg, fig_size=(2.5, 1.5), legend=False, name_str=','.join([str(x) for x in start_days]) + name_str) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) add_naive_learned(start_end_day_res, start_days, end_days, 'a', 'b') filter.assign_composite(start_end_day_res, loop_keys=['odor_valence', 'training_day']) start_end_day_res = reduce.new_filter_reduce( start_end_day_res, filter_keys=['training_day', 'mouse', 'odor_standard'], reduce_key='Fraction Responsive') before_csp = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'a', 'odor_valence': 'CS+' }) after_csp = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'b', 'odor_valence': 'CS+' }) before_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'a', 'odor_valence': 'CS-' }) after_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'b', 'odor_valence': 'CS-' }) try: from scipy.stats import ranksums, wilcoxon, kruskal print('Before CS+: {}'.format( np.mean(before_csp['Fraction Responsive']))) print('After CS+: {}'.format(np.mean( after_csp['Fraction Responsive']))) print('Wilcoxin:{}'.format( wilcoxon(before_csp['Fraction Responsive'], after_csp['Fraction Responsive']))) print('Before CS-: {}'.format( np.mean(before_csm['Fraction Responsive']))) print('After CS-: {}'.format(np.mean( after_csm['Fraction Responsive']))) print('Wilcoxin:{}'.format( wilcoxon(before_csm['Fraction Responsive'], after_csm['Fraction Responsive']))) except: print('stats didnt work')
def plot_responsive_difference_odor_and_water(res, odor_start_days, water_start_days, end_days, use_colors=True, figure_path=None, include_water=True, normalize=False, pt_start=None, pt_learned=None, average=True, ylim=.22, reuse_arg=False, save_arg=True): key = 'Change in Fraction Responsive' if normalize: key = 'Norm. Fraction Responsive' def _helper(start_end_day_res): combs, list_of_ixs = filter.retrieve_unique_entries( start_end_day_res, ['mouse', 'odor_standard']) for i, comb in enumerate(combs): ixs = list_of_ixs[i] assert len(ixs) == 2 if start_end_day_res['training_day'][0] == 'Naive': ref = ixs[0] test = ixs[1] elif start_end_day_res['training_day'][0] == 'Learned': ref = ixs[1] test = ixs[0] else: raise ValueError('cannot find ref day') if normalize: start_end_day_res[key][test] = start_end_day_res['Fraction Responsive'][test] / \ start_end_day_res['Fraction Responsive'][ref] start_end_day_res[key][ref] = 1 else: start_end_day_res[key][test] = start_end_day_res['Fraction Responsive'][test] - \ start_end_day_res['Fraction Responsive'][ref] start_end_day_res[key][ref] = 0 ax_args_copy = ax_args.copy() res = copy.copy(res) get_responsive_cells(res) list_of_days = list(zip(odor_start_days, end_days)) mice = np.unique(res['mouse']) res[key] = np.zeros_like(res['Fraction Responsive']) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) start_end_day_res = filter.filter( start_end_day_res, {'odor_valence': ['CS+', 'CS-', 'Naive']}) add_naive_learned(start_end_day_res, odor_start_days, end_days) odors = ['CS+', 'CS-', 'Naive'] if 'PT CS+' in np.unique(res['odor_valence']): odors = ['PT CS+'] + odors list_of_days = list(zip(pt_start, pt_learned)) start_end_day_res_pt = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) start_end_day_res_pt = filter.filter(start_end_day_res_pt, {'odor_valence': 'PT CS+'}) add_naive_learned(start_end_day_res_pt, pt_start, pt_learned) reduce.chain_defaultdicts(start_end_day_res, start_end_day_res_pt) if include_water: odors += ['US'] list_of_days = list(zip(water_start_days, end_days)) start_end_day_res_water = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) start_end_day_res_water = filter.filter(start_end_day_res_water, {'odor_valence': 'US'}) add_naive_learned(start_end_day_res_water, water_start_days, end_days) reduce.chain_defaultdicts(start_end_day_res, start_end_day_res_water) filter.assign_composite(start_end_day_res, loop_keys=['odor_standard', 'training_day']) if average: start_end_day_res = reduce.new_filter_reduce( start_end_day_res, filter_keys=['odor_valence', 'mouse', 'training_day'], reduce_key=key) start_end_day_res.pop(key + '_sem') _helper(start_end_day_res) start_end_day_res = filter.filter(start_end_day_res, {'training_day': 'Learned'}) summary_res = reduce.new_filter_reduce(start_end_day_res, filter_keys='odor_valence', reduce_key=key) dict = { 'CS+': 'Green', 'CS-': 'Red', 'US': 'Turquoise', 'PT CS+': 'Orange', 'Naive': 'Gray' } if use_colors: colors = [ dict[key] for key in np.unique(start_end_day_res['odor_valence']) ] else: colors = ['Black'] * 6 ax_args_copy = ax_args_copy.copy() n_valence = len(np.unique(summary_res['odor_valence'])) ax_args_copy.update({ 'xlim': [-.5, 3.5], 'ylim': [-ylim, ylim], 'yticks': [-.3, -.2, -.1, 0, .1, .2, .3] }) if normalize: ax_args_copy.update({ 'xlim': [-.5, 3.5], 'ylim': [-.1, 1.5], 'yticks': [0, .5, 1, 1.5] }) error_args_ = { 'fmt': '.', 'capsize': 2, 'elinewidth': 1, 'markersize': 2, 'alpha': .75 } scatter_args_copy = scatter_args.copy() scatter_args_copy.update({'s': 3}) for i, odor in enumerate(odors): reuse = True if i == 0: reuse = reuse_arg plot.plot_results(start_end_day_res, loop_keys='odor_valence', select_dict={'odor_valence': odor}, x_key='odor_valence', y_key=key, colors=[dict[odor]] * len(mice), path=figure_path, plot_args=scatter_args_copy, plot_function=plt.scatter, ax_args=ax_args_copy, save=False, reuse=reuse, fig_size=(2, 1.5), rect=(.25, .2, .6, .6), legend=False, name_str=','.join([str(x) for x in odor_start_days])) if not normalize: plt.plot(plt.xlim(), [0, 0], '--', color='gray', linewidth=1, alpha=.5) plot.plot_results(summary_res, x_key='odor_valence', y_key=key, error_key=key + '_sem', colors='black', path=figure_path, plot_args=error_args_, plot_function=plt.errorbar, ax_args=ax_args_copy, save=save_arg, reuse=True, fig_size=(2, 1.5), legend=False)
def plot_overlap_odor(res, start_days, end_days, delete_non_selective=False, figure_path=None, excitatory=True): ax_args_copy = overlap_ax_args.copy() res = copy.copy(res) res = _get_overlap_odor(res, delete_non_selective) list_of_days = list(zip(start_days, end_days)) mice = np.unique(res['mouse']) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) start_end_day_res = reduce.new_filter_reduce( start_end_day_res, filter_keys=['mouse', 'day', 'condition'], reduce_key='Overlap') add_naive_learned(start_end_day_res, start_days, end_days) filter.assign_composite(start_end_day_res, loop_keys=['condition', 'training_day']) odor_list = ['+:+', '-:-', '+:-'] colors = ['Green', 'Red', 'Gray'] name_str = '_E' if excitatory else '_I' ax_args_copy.update({'xlim': [-1, 6]}) for i, odor in enumerate(odor_list): save_arg = False reuse_arg = True if i == 0: reuse_arg = False if i == len(odor_list) - 1: save_arg = True temp = filter.filter(start_end_day_res, {'condition': odor}) name = ','.join([str(x) for x in start_days]) + '_' + ','.join( [str(x) for x in end_days]) name += name_str plot.plot_results(temp, x_key='condition_training_day', y_key='Overlap', loop_keys='mouse', colors=[colors[i]] * len(mice), path=figure_path, plot_args=line_args, ax_args=ax_args_copy, save=save_arg, reuse=reuse_arg, name_str=name, fig_size=(2, 1.5), legend=False) b = filter.filter(temp, {'training_day': 'Learned'}) print(odor) print(np.mean(b['Overlap'])) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) add_naive_learned(start_end_day_res, start_days, end_days, str1='0', str2='1') start_end_day_res.pop('Overlap_sem', None) summary_res = reduce.new_filter_reduce(start_end_day_res, filter_keys='training_day', reduce_key='Overlap') ax_args_copy.update({ 'xlim': [-1, 2], 'ylim': [0, .5], 'yticks': [0, .1, .2, .3, .4, .5] }) plot.plot_results(summary_res, x_key='training_day', y_key='Overlap', path=figure_path, plot_args=bar_args, ax_args=ax_args_copy, plot_function=plt.bar, fig_size=(2, 1.5), legend=False, reuse=False, save=False) plot.plot_results(summary_res, x_key='training_day', y_key='Overlap', error_key='Overlap_sem', path=figure_path, plot_function=plt.errorbar, plot_args=error_args, ax_args=ax_args, save=True, reuse=True, name_str=name_str) before_odor = filter.filter(start_end_day_res, filter_dict={ 'training_day': '0', 'condition': '+:+' }) after_odor = filter.filter(start_end_day_res, filter_dict={ 'training_day': '1', 'condition': '+:+' }) before_csp = filter.filter(start_end_day_res, filter_dict={ 'training_day': '0', 'condition': '+:-' }) after_csp = filter.filter(start_end_day_res, filter_dict={ 'training_day': '1', 'condition': '+:-' }) before_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': '0', 'condition': '-:-' }) after_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': '1', 'condition': '-:-' }) from scipy.stats import ranksums, wilcoxon, kruskal print('Before ++: {}'.format(np.mean(before_odor['Overlap']))) print('After ++: {}'.format(np.mean(after_odor['Overlap']))) print('Wilcoxin:{}'.format( wilcoxon(before_odor['Overlap'], after_odor['Overlap']))) print('Before +-: {}'.format(np.mean(before_csp['Overlap']))) print('After +-: {}'.format(np.mean(after_csp['Overlap']))) print('Wilcoxin:{}'.format( wilcoxon(before_csp['Overlap'], after_csp['Overlap']))) print('Before --: {}'.format(np.mean(before_csm['Overlap']))) print('After --: {}'.format(np.mean(after_csm['Overlap']))) print('Wilcoxin:{}'.format( wilcoxon(before_csm['Overlap'], after_csm['Overlap'])))
line_args_copy.update({'marker': '.', 'linewidth': .5, 'markersize': .5}) valences = np.unique(full['odor_valence']) for valence in valences: color = [color_dict_valence[valence]] mean_std_res = reduce.new_filter_reduce( full, filter_keys=['odor_valence', 'start_time'], reduce_key='training_end_licks') plot.plot_results(full, x_key='start_time', y_key='training_end_licks', loop_keys=['mouse', 'condition'], colors=color * 50, select_dict={'odor_valence': valence}, plot_args=line_args_copy, legend=False, path=save_path) mean_std_res['rho_over_mu'] = mean_std_res[ 'training_end_licks_std'] / mean_std_res['training_end_licks'] plot.plot_results(mean_std_res, x_key='start_time', y_key='rho_over_mu', select_dict={'odor_valence': valence}, plot_function=plt.scatter, plot_args=scatter_args, fig_size=[2, 1.5],
def behavior_vs_neural_power(neural_res, behavior_res, start, end, figure_path, neural_arg ='power', behavior_arg ='onset'): neural_res = copy.copy(neural_res) neural_key = 'neural' behavior_key = 'behavior' if behavior_arg == 'onset': behavior_data_key = 'time_first_lick_raw' elif behavior_arg == 'magnitude': behavior_data_key = 'lick_5s' elif behavior_arg == 'com': behavior_data_key = 'lick_com_raw' else: raise ValueError('bad key') neural_data_key = 'max_power' _power(neural_res, excitatory=True) for i, p in enumerate(neural_res['Power']): s, e = neural_res['DAQ_O_ON_F'][i], neural_res['DAQ_W_ON_F'][i] mp = np.max(p[s:e]) - np.mean(p[:s]) neural_res[neural_data_key].append(mp) neural_res[neural_data_key] = np.array(neural_res[neural_data_key]) list_of_days = [np.arange(s, e+1) for s, e in zip(start,end)] print(list_of_days) neural_res_filtered = filter.filter_days_per_mouse(neural_res, days_per_mouse=list_of_days) neural_res_filtered = filter.filter(neural_res_filtered, filter_dict={'odor_valence':'CS+'}) behavior_res_filtered = filter.filter(behavior_res, filter_dict={'odor_valence':'CS+'}) names_neu, ixs_neu = filter.retrieve_unique_entries(neural_res_filtered, ['mouse','day','odor_standard']) out = defaultdict(list) for ix, names in zip(ixs_neu, names_neu): mouse = names[0] day = names[1] odor_standard = names[2] assert len(ix) == 1 neural = neural_res_filtered[neural_data_key][ix[0]] temp = filter.filter(behavior_res_filtered, {'mouse': mouse, 'odor_standard': odor_standard}) assert len(temp[behavior_data_key]) == 1 ix_day = [x == day for x in temp['day'][0]] lick = temp[behavior_data_key][0][ix_day] lick = lick[lick>-1] out['mouse'].append(mouse) out['day'].append(day) out['odor_standard'].append(odor_standard) out[neural_key + neural_arg].append(np.mean(neural)) out[behavior_key + behavior_arg].append(np.mean(lick)) out['neural_raw'].append(neural) out['lick_raw'].append(lick) for k, v in out.items(): out[k] = np.array(v) ## versus if behavior_arg in ['onset', 'com']: xlim = [0, 5] xticks = [0, 2, 5] xticklabels = ['ON', 'OFF', 'US'] else: xlim = [0, 35] xticks = [0, 10, 20, 30] xticklabels = [0, 10, 20, 30] ax_args = {'xlim': xlim, 'ylim': [0, .1], 'xticks': xticks, 'yticks': [0, .05, .1], 'xticklabels': xticklabels} path, name = plot.plot_results(out, x_key=behavior_key + behavior_arg, y_key=neural_key + neural_arg, loop_keys='mouse', plot_function=plt.scatter, plot_args=scatter_args, ax_args=ax_args, colormap='jet', path=figure_path, save=False) from sklearn import linear_model from sklearn.metrics import mean_squared_error, r2_score scores = [] for mouse in np.unique(out['mouse']): res = filter.filter(out, {'mouse':mouse}) regr = linear_model.LinearRegression() x = res[behavior_key + behavior_arg].reshape(-1,1) y = res[neural_key + neural_arg].reshape(-1,1) regr.fit(x, y) y_pred = regr.predict(x) score = regr.score(x, y) scores.append(score) print('regression: {}'.format(scores)) xlim = plt.xlim() ylim = plt.ylim() plt.text((xlim[1] - xlim[0])/2, ylim[1]-.01, 'Average R = {:.2f}'.format(np.mean(scores))) name += '_' + behavior_arg plot._easy_save(path, name)
'mutation_method': mutate_bit_genome, 'mutation_rate': 0.01, 'stop': { 'fitness': None, 'generation': 100 } } G = 100 N = 100 X = list(range(1, G+1)) results = [] for msm in MATE_SELECTION_METHODS: print(msm) simulation.update({'mate_selection_method': msm}) results.append({ 'x': X, 'y': average_n_runs(simulation, N, G)['average_fitnesses'], 'label': str(msm).split(' ')[1] }) plot_results( datasets=results, savefig="../report/img/{}.png".format(datetime.datetime.now()), xlabel="Generation number", ylabel="Fitness", title="Different parent selection functions", ncol=3 )
# model.training(configs['model']['epochs'], steps_per_epoch, data_gen_train, save=True) # Load a trained model model.load_model('epoch0_loss6.74e-01') ntest = nrows - ntrain steps_test = int(ntest / configs['data']['batch_size']) print('> Testing model on', ntest, 'data rows with', steps_test, 'steps') batch_size = configs['data']['batch_size'] n_batches = 100 # Test the model data_gen_test = dl.generate_clean_data(-ntrain, -1) true_values = [] predictions = model.predict(steps_test, data_gen_test, true_values) # Print stats s1 = stats.STATS(true_values[-ntrain:], predictions[-ntrain:], threshold=0.5) s1.print_stats() s1 = stats.STATS(true_values[-ntrain:], predictions[-ntrain:], threshold=0.6) s1.print_stats() s1 = stats.STATS(true_values[-ntrain:], predictions[-ntrain:], threshold=0.7) s1.print_stats() s1 = stats.STATS(true_values[-ntrain:], predictions[-ntrain:], threshold=0.75) s1.print_stats() # s1 = stats.STATS(true_values[-ntrain:], predictions[-ntrain:], threshold=0.8) # s1.print_stats() # plot the last batch of the data plot.plot_results(true_values[-ntrain:], predictions[-ntrain:])
def plot_responsive_difference_odor_and_water(res, odor_start_days, end_days, use_colors=True, figure_path=None, normalize=False, ylim=.6): key = 'Change in Fraction' if normalize: key = 'Norm. Fraction' def _helper(start_end_day_res): combs, list_of_ixs = filter.retrieve_unique_entries( start_end_day_res, ['mouse', 'odor_valence']) for i, comb in enumerate(combs): ixs = list_of_ixs[i] assert len(ixs) == 2 if start_end_day_res['training_day'][0] == 'Naive': ref = ixs[0] test = ixs[1] elif start_end_day_res['training_day'][0] == 'Learned': ref = ixs[1] test = ixs[0] else: raise ValueError('cannot find ref day') if normalize: start_end_day_res[key][test] = start_end_day_res['Fraction'][test] / \ start_end_day_res['Fraction'][ref] start_end_day_res[key][ref] = 1 else: start_end_day_res[key][test] = start_end_day_res['Fraction'][test] - \ start_end_day_res['Fraction'][ref] start_end_day_res[key][ref] = 0 ax_args_copy = ax_args.copy() res = get_compare_responsive_sig(res) list_of_days = list(zip(odor_start_days, end_days)) mice = np.unique(res['mouse']) res[key] = np.zeros_like(res['Fraction']) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) start_end_day_res = filter.filter(start_end_day_res, {'odor_valence': ['CS+', 'CS-']}) add_naive_learned(start_end_day_res, odor_start_days, end_days) _helper(start_end_day_res) start_end_day_res = filter.filter(start_end_day_res, {'training_day': 'Learned'}) summary_res = reduce.new_filter_reduce(start_end_day_res, filter_keys='odor_valence', reduce_key=key) dict = {'CS+': 'Green', 'CS-': 'Red'} if use_colors: colors = [ dict[key] for key in np.unique(start_end_day_res['odor_valence']) ] else: colors = ['Black'] * 6 ax_args_copy = ax_args_copy.copy() n_valence = len(np.unique(summary_res['odor_valence'])) ax_args_copy.update({ 'xlim': [-1, n_valence], 'ylim': [-ylim, ylim], 'yticks': np.arange(-1, 1, .2) }) if normalize: ax_args_copy.update({ 'xlim': [-1, n_valence], 'ylim': [-.1, 1.5], 'yticks': [0, .5, 1, 1.5] }) scatter_args_copy = scatter_args.copy() scatter_args_copy.update({'s': 8}) odors = ['CS+', 'CS-'] for i, odor in enumerate(odors): reuse = True if i == 0: reuse = False plot.plot_results(start_end_day_res, loop_keys='odor_valence', select_dict={'odor_valence': odor}, x_key='odor_valence', y_key=key, colors=[dict[odor]] * len(mice), path=figure_path, plot_args=scatter_args_copy, plot_function=plt.scatter, ax_args=ax_args_copy, save=False, reuse=reuse, fig_size=(2, 1.5), legend=False, name_str=','.join([str(x) for x in odor_start_days])) if not normalize: plt.plot(plt.xlim(), [0, 0], '--', color='gray', linewidth=1, alpha=.5) plot.plot_results(summary_res, x_key='odor_valence', y_key=key, error_key=key + '_sem', colors='black', path=figure_path, plot_args=error_args, plot_function=plt.errorbar, ax_args=ax_args_copy, save=True, reuse=True, fig_size=(2, 1.5), legend=False)
def plot_summary_odor_and_water(res, odor_start_days, water_start_days, end_days, use_colors=True, excitatory=True, arg='odor_valence', figure_path=None): include_water = True ax_args_copy = ax_args.copy() res = copy.copy(res) get_responsive_cells(res) mice = np.unique(res['mouse']) list_of_days = list(zip(odor_start_days, end_days)) start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) start_end_day_res = filter.exclude(start_end_day_res, {'odor_valence': 'US'}) add_naive_learned(start_end_day_res, odor_start_days, end_days, 'a', 'b') if include_water: list_of_days = list(zip(water_start_days, end_days)) start_end_day_res_water = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) start_end_day_res_water = filter.filter(start_end_day_res_water, {'odor_valence': 'US'}) add_naive_learned(start_end_day_res_water, water_start_days, end_days, 'a', 'b') reduce.chain_defaultdicts(start_end_day_res, start_end_day_res_water) ax_args_copy = ax_args_copy.copy() if arg == 'odor_valence': start_end_day_res = reduce.new_filter_reduce( start_end_day_res, filter_keys=['training_day', 'mouse', 'odor_valence'], reduce_key='Fraction Responsive') odor_list = ['CS+', 'CS-'] ax_args_copy.update({ 'xlim': [-1, 6], 'ylim': [0, .6], 'yticks': [0, .1, .2, .3, .4, .5] }) colors = ['Green', 'Red'] elif arg == 'naive': arg = 'odor_valence' start_end_day_res = reduce.new_filter_reduce( start_end_day_res, filter_keys=['training_day', 'mouse', 'odor_valence'], reduce_key='Fraction Responsive') odor_list = ['CS+'] ax_args_copy.update({ 'xlim': [-1, 4], 'ylim': [0, .6], 'yticks': [0, .1, .2, .3, .4, .5] }) colors = ['GoldenRod'] else: odor_list = ['CS+1', 'CS+2', 'CS-1', 'CS-2'] colors = ['Green', 'Green', 'Red', 'Red'] ax_args_copy.update({ 'xlim': [-1, 10], 'ylim': [0, .6], 'yticks': [0, .1, .2, .3, .4, .5] }) filter.assign_composite(start_end_day_res, loop_keys=[arg, 'training_day']) if not use_colors: colors = ['Black'] * 4 name_str = '_E' if excitatory else '_I' for i, odor in enumerate(odor_list): reuse_arg = True if i == 0: reuse_arg = False plot.plot_results(start_end_day_res, select_dict={arg: odor}, x_key=arg + '_training_day', y_key='Fraction Responsive', loop_keys='mouse', colors=[colors[i]] * len(mice), path=figure_path, plot_args=line_args, ax_args=ax_args_copy, save=False, reuse=reuse_arg, fig_size=(2.5, 1.5), legend=False, name_str=','.join([str(x) for x in odor_start_days])) plot.plot_results(start_end_day_res, select_dict={'odor_standard': 'US'}, x_key='training_day', y_key='Fraction Responsive', loop_keys='mouse', colors=['Turquoise'] * len(mice), path=figure_path, plot_args=line_args, ax_args=ax_args_copy, fig_size=(1.6, 1.5), legend=False, reuse=True, save=True, name_str=name_str) before_odor = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'a', 'odor_valence': ['CS+', 'CS-'] }) after_odor = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'b', 'odor_valence': ['CS+', 'CS-'] }) before_csp = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'a', 'odor_valence': 'CS+' }) after_csp = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'b', 'odor_valence': 'CS+' }) before_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'a', 'odor_valence': 'CS-' }) after_csm = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'b', 'odor_valence': 'CS-' }) before_water = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'a', 'odor_valence': 'US' }) after_water = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'b', 'odor_valence': 'US' }) try: from scipy.stats import ranksums, wilcoxon, kruskal print('Before Odor: {}'.format( np.mean(before_odor['Fraction Responsive']))) print('After Odor: {}'.format( np.mean(after_odor['Fraction Responsive']))) print('Wilcoxin:{}'.format( wilcoxon(before_odor['Fraction Responsive'], after_odor['Fraction Responsive']))) print('Before CS+: {}'.format( np.mean(before_csp['Fraction Responsive']))) print('After CS+: {}'.format(np.mean( after_csp['Fraction Responsive']))) print('Wilcoxin:{}'.format( wilcoxon(before_csp['Fraction Responsive'], after_csp['Fraction Responsive']))) print('Before CS-: {}'.format( np.mean(before_csm['Fraction Responsive']))) print('After CS-: {}'.format(np.mean( after_csm['Fraction Responsive']))) print('Wilcoxin:{}'.format( wilcoxon(before_csm['Fraction Responsive'], after_csm['Fraction Responsive']))) print('Before US: {}'.format( np.mean(before_water['Fraction Responsive']))) print('After US: {}'.format(np.mean( after_water['Fraction Responsive']))) print('Wilcoxin:{}'.format( wilcoxon(before_water['Fraction Responsive'], after_water['Fraction Responsive']))) except: print('stats didnt work')
from problems.simulations import ONE_MAX avgs = [] x = list(range(1, 101)) for s in [ { 'crossover_rate': c, 'mutation_rate': m } for c in (0.75, 1.0) for m in (0.01, 0.005) ]: simulation = ONE_MAX simulation.update(s) label = "{}, {}".format( simulation['crossover_rate'], simulation['mutation_rate'] ) avgs.append({ 'x': x, 'y': average_n_runs(simulation, n=10, generations=100)['average_fitnesses'], 'label': label }) plot_results( datasets=avgs, savefig="../report/img/{}_avg.png".format(datetime.now()), xlabel="Generation number", ylabel="Fitness", title="Average fitness with specified crossover rate, mutation rate", ncol=4 )