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_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_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_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'])))
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_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')
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_consistency_within_day(res, start, end, shuffle, pretraining, figure_path): d = list(zip(start, end)) res_temp = filter.filter_days_per_mouse(res, d) if pretraining: res_temp = filter.filter(res_temp, {'odor_valence': ['PT CS+']}) else: res_temp = filter.filter(res_temp, {'odor_valence': ['CS+', 'CS-']}) corr_res = _correlation(res_temp) corr_res.pop('data') analysis.add_naive_learned(corr_res, start, end, '0', '1') res_ = reduce.new_filter_reduce( corr_res, filter_keys=['mouse', 'odor_standard', 'training_day'], reduce_key='consistency_corrcoef') res_.pop('consistency_corrcoef_sem') filter.assign_composite(res_, loop_keys=['training_day', 'odor_valence']) if shuffle: s = '_shuffled' else: s = '' ax_args_copy = ax_args.copy() ax_args_copy.update({ 'xlim': [-.5, 2.5], 'ylim': [0, .55], 'yticks': np.arange(0, 1.1, .1) }) swarm_args_copy = swarm_args.copy() if pretraining: swarm_args_copy.update({'palette': ['gray', 'orange', 'green', 'red']}) else: swarm_args_copy.update({'palette': ['gray', 'gray', 'green', 'red']}) ix = res_['training_day_odor_valence'] == '1_PT CS+' res_['training_day_odor_valence'][ix] = '1_APT CS+' plot.plot_results(res_, x_key='training_day_odor_valence', y_key='consistency_corrcoef', path=figure_path, plot_args=swarm_args_copy, plot_function=sns.stripplot, ax_args=ax_args_copy, reuse=False, save=False, sort=True, name_str=s) summary = reduce.new_filter_reduce(res_, filter_keys='training_day_odor_valence', reduce_key='consistency_corrcoef') plot.plot_results(summary, x_key='training_day_odor_valence', y_key='consistency_corrcoef', error_key='consistency_corrcoef_sem', colors='black', path=figure_path, plot_args=error_args, plot_function=plt.errorbar, save=True, reuse=True, legend=False, name_str=s) print(summary['consistency_corrcoef']) ix_a = res_['training_day_odor_valence'] == '0_CS+' ix_b = res_['training_day_odor_valence'] == '0_CS-' ix_c = res_['training_day_odor_valence'] == '1_CS+' ix_d = res_['training_day_odor_valence'] == '1_CS-' a = res_['consistency_corrcoef'][ix_a] b = res_['consistency_corrcoef'][ix_b] c = res_['consistency_corrcoef'][ix_c] d = res_['consistency_corrcoef'][ix_d] from scipy.stats import ranksums, wilcoxon, kruskal import scikit_posthocs print(kruskal(a, b, c)) x = scikit_posthocs.posthoc_dunn(a=[a, b, c, d], p_adjust=None) print(x)
def plot_power(res, start_days, end_days, figure_path, excitatory=True, odor_valence=('CS+'), naive=False, colors_before={ 'CS+': 'Green', 'CS-': 'Red' }, colors_after={ 'CS+': 'Green', 'CS-': 'Red' }, ylim=[0, .1], align=True, pad=True): res = copy.copy(res) _power(res, excitatory) if pad: right_on = np.median(res['DAQ_O_ON_F']) for i, odor_on in enumerate(res['DAQ_O_ON_F']): if np.abs(odor_on - right_on) > 2: diff = (right_on - odor_on).astype(int) if diff > 0: p = res['Power'][i] newp = np.zeros_like(p) newp[:diff] = p[0] newp[diff:] = p[:-diff] res['Power'][i] = newp print('early odor time. mouse: {}, day: {}'.format( res['mouse'][i], res['day'][i])) else: p = res['Power'][i] newp = np.zeros_like(p) newp[:diff] = p[-diff:] newp[diff:] = p[-1] res['Power'][i] = newp print('late odor time. mouse: {}, day: {}'.format( res['mouse'][i], res['day'][i])) if align: nF = [len(x) for x in res['Power']] max_frame = np.max(nF) for i, p in enumerate(res['Power']): if len(p) < max_frame: newp = np.zeros(max_frame) newp[:len(p)] = p newp[len(p):] = p[-1] res['Power'][i] = newp res['Time'][i] = np.arange(0, max_frame) print('pad frames. mouse: {}, day: {}'.format( res['mouse'][i], res['day'][i])) 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) add_naive_learned(start_end_day_res, start_days, end_days) if naive: start_end_day_res = filter.exclude(start_end_day_res, { 'odor_standard': 'PT CS+', 'training_day': 'Naive' }) ix = start_end_day_res['odor_valence'] == 'PT Naive' start_end_day_res['odor_valence'][ix] = 'PT CS+' start_end_day_sum_res = reduce.new_filter_reduce( start_end_day_res, filter_keys=['training_day', 'odor_valence'], reduce_key='Power') ax_args_copy = trace_ax_args.copy() if excitatory: yticks = np.arange(0, .2, .05) else: yticks = -1 * np.arange(0, .2, .025) ax_args_copy.update({ 'xticks': [res['DAQ_O_ON_F'][-1], res['DAQ_W_ON_F'][-1]], 'xticklabels': ['ON', 'US'], 'ylim': ylim, 'yticks': yticks }) colors_b = [colors_before[x] for x in odor_valence] colors = [colors_after[x] for x in odor_valence] strr = ','.join([str(x) for x in start_days]) + '_' + ','.join( [str(x) for x in end_days]) if excitatory: strr += '_E' else: strr += '_I' plot.plot_results(start_end_day_sum_res, select_dict={ 'odor_valence': odor_valence, 'training_day': 'Naive' }, x_key='Time', y_key='Power', loop_keys='odor_valence', error_key='Power_sem', path=figure_path, plot_function=plt.fill_between, plot_args=fill_args, ax_args=ax_args_copy, colors=colors_b, fig_size=(2, 1.5), rect=(.3, .2, .6, .6), save=False) plot.plot_results(start_end_day_sum_res, select_dict={ 'odor_valence': odor_valence, 'training_day': 'Naive' }, x_key='Time', y_key='Power', loop_keys='odor_valence', path=figure_path, plot_args=trace_args, ax_args=ax_args_copy, colors=colors_b, fig_size=(2, 1.5), reuse=True, save=False) plot.plot_results(start_end_day_sum_res, select_dict={ 'odor_valence': odor_valence, 'training_day': 'Learned' }, x_key='Time', y_key='Power', loop_keys='odor_valence', error_key='Power_sem', path=figure_path, plot_function=plt.fill_between, plot_args=fill_args, ax_args=ax_args_copy, colors=colors, fig_size=(2, 1.5), reuse=True, save=False) plot.plot_results(start_end_day_sum_res, select_dict={ 'odor_valence': odor_valence, 'training_day': 'Learned' }, x_key='Time', y_key='Power', loop_keys='odor_valence', path=figure_path, plot_args=trace_args, ax_args=ax_args_copy, colors=colors, fig_size=(2, 1.5), reuse=True, name_str=strr) for i, x in enumerate(start_end_day_res['Power']): on, off = [ start_end_day_res['DAQ_O_ON_F'][i], start_end_day_res['DAQ_W_ON_F'][i] ] y = np.max(x[on:off]) - np.min(x) start_end_day_res['stat'].append(y) start_end_day_res['stat'] = np.array(start_end_day_res['stat']) for valence in odor_valence: before_odor = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'Naive', 'odor_valence': valence }) after_odor = filter.filter(start_end_day_res, filter_dict={ 'training_day': 'Learned', 'odor_valence': valence }) try: from scipy.stats import ranksums, wilcoxon, kruskal print(before_odor['odor_valence']) # print('Before: {}'.format(before_odor['stat'])) print('Before: {}'.format(np.mean(before_odor['stat']))) # print('After: {}'.format(after_odor['stat'])) print('After: {}'.format(np.mean(after_odor['stat']))) print('Wilcoxin:{}'.format( wilcoxon(before_odor['stat'], after_odor['stat']))) except: print('stats didnt work') return before_odor['stat'], after_odor['stat']
} summary_res.pop('PCA Distance_std') summary_res.pop('PCA Distance_sem') summary_res = filter.filter(summary_res, filter_dict={'shuffle': 0}) if plot_cs: select_dict = {'odor_valence': 'CS+'} else: select_dict = None if condition.name == 'OFC_LONGTERM': dt_learned_day, dt_last_day = get_days_per_mouse( data_path, condition) dt_learned_day = [3, 2, 2, 3] filtered_res = filter.filter_days_per_mouse( summary_res, list(zip(dt_learned_day, dt_last_day))) analysis.add_naive_learned(filtered_res, dt_learned_day, dt_last_day, 'Learned', 'Over-trained') mean_std_res = reduce.new_filter_reduce( filtered_res, reduce_key='PCA Distance', filter_keys=['training_day', 'odor_valence']) plot.plot_results(filtered_res, x_key='training_day', y_key='PCA Distance', loop_keys=['odor_valence', 'mouse'], select_dict=select_dict, colors=['Green'] * mice.size + ['Red'] * mice.size, ax_args=summary_ax_args, plot_function=plt.plot, plot_args=summary_line_args, path=figure_path,
def magnitude_histogram(res, learned_days, end_days, use_colors=True, figure_path=None, reuse=False): def _helper(res): list_odor_on = res['DAQ_O_ON_F'] list_water_on = res['DAQ_W_ON_F'] list_of_dff = res['dff'] for i, dff in enumerate(list_of_dff): s = list_odor_on[i] e = list_water_on[i] max = np.max(dff[:, s:e], axis=1) res['max_dff'].append(max) res['max_dff'] = np.array(res['max_dff']) def _helper1(res): for data in res['amplitude']: res['max_dff'].append(data[data > 0]) res['max_dff'] = np.array(res['max_dff']) res = copy.copy(res) # list_of_days = list(zip(learned_days, end_days)) list_of_days = end_days start_end_day_res = filter.filter_days_per_mouse( res, days_per_mouse=list_of_days) _helper(start_end_day_res) add_naive_learned(start_end_day_res, learned_days, end_days) filter.assign_composite(start_end_day_res, loop_keys=['odor_standard', 'training_day']) csp = filter.filter(start_end_day_res, filter_dict={'odor_valence': 'CS+'}) csm = filter.filter(start_end_day_res, filter_dict={'odor_valence': 'CS-'}) csp_data = np.hstack(csp['max_dff'].flat) csm_data = np.hstack(csm['max_dff'].flat) fig = plt.figure(figsize=(3, 2)) ax = fig.add_axes([.2, .2, .7, .7]) x_upper = 1.01 y_upper = .5 bins = 20 xrange = [0, x_upper] yrange = [0, y_upper] xticks = np.arange(0, x_upper, .2) yticks = np.arange(0, y_upper, 3) legends = ['CS+', 'CS-'] colors = ['green', 'red'] for i, data in enumerate([csp_data, csm_data]): plt.hist(data, bins=bins, range=xrange, density=True, color=colors[i], alpha=.5) plt.legend(legends, loc=1, bbox_to_anchor=(1.05, .4), fontsize=5) ax.set_xlabel('DF/F') ax.set_ylabel('Fraction') ax.set_xticks(xticks) # ax.set_yticks(yticks) plt.xlim(xrange) # plt.ylim(yrange) plt.show()