def __init__(self, AnimalName, FolderName, taskstoplot, controlflag=0): print('Loading Data') self.taskstoplot = taskstoplot self.colors = sns.color_palette('deep', len(self.taskstoplot)) self.animalname = AnimalName if controlflag: self.animalinfo = DataDetails.ControlAnimals(self.animalname) else: self.animalinfo = DataDetails.ExpAnimalDetails(self.animalname) self.FolderName = os.path.join(FolderName, self.animalname) self.Task_Numframes = self.animalinfo['task_numframes'] self.TaskDict = self.animalinfo['task_dict'] # Run functions self.get_data_folders() self.load_behaviordata() self.load_lapparams()
def combineanimaldataframes(self, csvfiles, tasklen): count = 0 for n, f in enumerate(csvfiles): animalname = f[:f.find('_')] if not self.controlflag: animalinfo = DataDetails.ExpAnimalDetails(animalname) else: animalinfo = DataDetails.ControlAnimals(animalname) if len(animalinfo['task_dict']) >= tasklen: print(f) df = pd.read_csv(os.path.join(self.CombinedDataFolder, f), index_col=0) if count == 0: combined_dataframe = df else: combined_dataframe = combined_dataframe.append( df, ignore_index=True) count += 1 return combined_dataframe
def __init__(self, AnimalName, FolderName, SaveFigureFolder, taskstoplot, controlflag=0): print('Loading Data') self.taskstoplot = taskstoplot self.SaveFigureFolder = SaveFigureFolder self.controlflag = controlflag if self.controlflag: self.colors = sns.color_palette(["#3498db", "#9b59b6"]) else: self.colors = sns.color_palette('deep') self.task2_colors = [self.colors[1], self.colors[3]] self.animalname = AnimalName if self.controlflag: self.animalinfo = DataDetails.ControlAnimals(self.animalname) else: self.animalinfo = DataDetails.ExpAnimalDetails(self.animalname) self.ParentFolderName = FolderName self.FolderName = os.path.join(FolderName, self.animalname) self.Task_Numframes = self.animalinfo['task_numframes'] self.removeframesforbayes = self.animalinfo['task_framestokeep'] self.TaskDict = self.animalinfo['task_dict'] self.framespersec = 30.98 self.trackbins = 5 # Run functions self.get_data_folders() if self.animalinfo['v73_flag']: self.load_v73_Data() else: self.load_fluorescentdata() self.load_Bayesfit() self.load_behaviordata() self.load_lapparams() if not self.controlflag: self.lickstoplap = np.int(self.lickstoplap['Task2'] - 1) self.lickstopframe = np.where(self.good_lapframes['Task2'] == self.lickstoplap)[0][0]
def plot_lapwiseerror_withlick(axis, SaveFolder, taskstoplot, trackbins=5, to_plot='R2', classifier_type='Bayes'): numlaps = {'Task1a': 5, 'Task1b': 11} l = Compile() files = [f for f in os.listdir(SaveFolder) if classifier_type in f] correlation_data = np.zeros((len(files), sum(numlaps.values()))) lick_data = np.zeros((len(files), sum(numlaps.values()))) count = 0 for n1, f in enumerate(files): animalname = f[: f.find('_')] animal_tasks = DataDetails.ControlAnimals(animalname)['task_dict'] data = np.load(os.path.join(SaveFolder, f), allow_pickle=True) lick_per_lap = data['alllicks'].item() print(f) count_lap = 0 for t in animal_tasks.keys(): if t in taskstoplot: lap_r2, lap_accuracy = l.calulate_lapwiseerror(y_actual=data['fit'].item()[t]['ytest'], y_predicted=data['fit'].item()[t]['yang_pred'], trackbins=trackbins, numlaps=data['numlaps'].item()[t], lapframes=data['lapframes'].item()[t]) if to_plot == 'R2': decodererror = np.asarray(lap_r2) else: decodererror = np.asarray(lap_accuracy) decodererror = decodererror[~np.isnan(decodererror)] if t == 'Task1a': this_task_data = decodererror[-numlaps[t]:] this_lick_data = lick_per_lap[t][-numlaps[t]:] else: this_task_data = decodererror[:numlaps[t]] this_lick_data = lick_per_lap[t][:numlaps[t]] correlation_data[count, count_lap:count_lap + numlaps[t]] = this_task_data lick_data[count, count_lap:count_lap + numlaps[t]] = this_lick_data count_lap += numlaps[t] count += 1 # Normalize and compare for p-value with Task1 corr_norm = correlation_data / np.max(correlation_data[:, :numlaps['Task1a']]) lick_norm = lick_data / np.max(lick_data[:, :numlaps['Task1a']]) # Plot_traces plot_axis = [axis, axis.twinx()] color_animal = sns.color_palette(["#3498db", "#9b59b6"]) color_data = sns.color_palette('dark', 2) if to_plot == 'R2': label = ['Mean R-squared', 'Mean Licks'] else: label = ['Mean Accuracy', 'Mean Licks'] for n, d in enumerate([corr_norm, lick_norm]): mean = np.mean(d, 0) sem = scipy.stats.sem(d, 0) count = 0 for n2, l1 in enumerate(taskstoplot): data_m = mean[count:count + numlaps[l1]] data_sem = sem[count:count + numlaps[l1]] if n == 0: plot_axis[n].errorbar(np.arange(count, count + numlaps[l1]), data_m, yerr=data_sem, color=color_animal[n2]) else: plot_axis[n].plot(np.arange(np.size(mean)), mean, '.-', color=color_data[n], zorder=n) plot_axis[n].set_ylabel(label[n], color=color_data[n]) count += numlaps[l1] plot_axis[0].set_ylim((0, 1)) plot_axis[1].set_ylim((0, 1)) # Get p-values for l in np.arange(np.size(correlation_data, 1)): d, p = scipy.stats.ranksums(correlation_data[:, l], correlation_data[:, 0]) if np.round(p, 3) < 0.01: if to_plot == 'R2': axis.plot(l, 1.0, '*', color='k') else: axis.plot(l, 1.5, '*', color='k') print(l, p) for a in plot_axis: pf.set_axes_style(axis) axis.set_xlabel('Lap Number') return correlation_data, lick_data
def plot_meancorrelation_withshuffle(axis, SaveFolder, trackbins, taskstoplot, classifier_type='Bayes', to_plot='R2'): # Choose last 10 laps in Task1, random 4 laps in Task2, and Task2b 100 times to calculate mean decoding error per animal num_iterations = 1000 l = Compile() colors = sns.color_palette(["#3498db", "#9b59b6"]) files = [f for f in os.listdir(SaveFolder) if classifier_type in f] shuffle_mean_corr = {k: np.zeros((num_iterations, len(files))) for k in taskstoplot} count = 0 for n, f in enumerate(files): animalname = f[:f.find('_')] animal_tasks = DataDetails.ControlAnimals(f[:f.find('_')])['task_dict'] data = np.load(os.path.join(SaveFolder, f), allow_pickle=True) print(animalname) for t in animal_tasks: if t in taskstoplot: lap_r2, lap_accuracy = l.calulate_lapwiseerror(y_actual=data['fit'].item()[t]['ytest'], y_predicted=data['fit'].item()[t]['yang_pred'], trackbins=trackbins, numlaps=data['numlaps'].item()[t], lapframes=data['lapframes'].item()[t]) if to_plot == 'R2': decodererror = np.asarray(lap_r2) else: decodererror = np.asarray(lap_accuracy) decodererror = decodererror[~np.isnan(decodererror)] tasklap = np.size(decodererror) print(tasklap) for i in np.arange(num_iterations): if t == 'Task1a': randlaps = np.random.choice(np.arange(tasklap - 5, tasklap), 4, replace=False) shuffle_mean_corr[t][i, count] = np.nanmean(decodererror[randlaps]) else: randlaps = np.random.choice(np.arange(0, tasklap), 4, replace=False) shuffle_mean_corr[t][i, count] = np.nanmean(decodererror[randlaps]) count += 1 # Get p-value p_value_task1 = [] for i in np.arange(num_iterations): t, p = scipy.stats.ttest_rel(shuffle_mean_corr['Task1a'][i, :], shuffle_mean_corr['Task1b'][i, :]) p_value_task1.append(p > 0.05) # Plot shuffle histogram # Remove zeros data = {k: [] for k in ['Task1a', 'Task1b']} for n, t in enumerate(['Task1a', 'Task1b']): temp = shuffle_mean_corr[t].flatten() data[t] = temp if to_plot == 'R2': sns.distplot(data[t], label=t, bins=np.linspace(0, 1, 50), ax=axis[1, 0], hist_kws={'color': colors[n]}, kde_kws={'color': colors[n]}) else: sns.distplot(data[t], label=t, bins=np.linspace(0, 50, 50), ax=axis[1, 0]) axis[1, 0].set_title('Shuffled laps P-value %0.3f' % ( np.size(np.where(p_value_task1)) / num_iterations)) axis[1, 0].legend(loc='center left', bbox_to_anchor=(1, 0.5)) axis[1, 0].set_xlabel('R-squared') axis[1, 0].set_xlim((-0.1, 1.0)) t, p1 = scipy.stats.ks_2samp(data['Task1a'], data['Task1b']) print('Flattened P-value %f' % p1) # Get mean_correlation mean_correlation = {k: [] for k in taskstoplot} sem_correlation = {k: [] for k in taskstoplot} for t in taskstoplot: mean_correlation[t] = np.mean(shuffle_mean_corr[t], 0) sem_correlation[t] = scipy.stats.sem(shuffle_mean_corr[t], 0, nan_policy='omit') df = pd.DataFrame.from_dict(mean_correlation) df = df.replace(0, np.nan) df = df.dropna(how='all') for p in np.arange(2): if p == 0: df_melt = df.melt(var_name='Task', value_name='Error') for index, row in df.iterrows(): axis[0, p].plot([row['Task1a'], row['Task1b']], 'k') print(df) else: df_div = df[df.columns].div(df['Task1a'].values, axis=0) print(df_div) df_melt = df_div.melt(var_name='Task', value_name='Error') for index, row in df_div.iterrows(): axis[0, p].plot([row['Task1a'], row['Task1b']], 'k') sns.boxplot(x='Task', y='Error', data=df_melt, palette=colors, order=[ 'Task1a', 'Task1b'], ax=axis[0, p]) sns.stripplot(x='Task', y='Error', data=df_melt, color='k', size=5, order=[ 'Task1a', 'Task1b'], ax=axis[0, p], dodge=False, jitter=False) axis[0, p].set_xlabel('') axis[0, p].set_ylim((0, 1.1)) t, p1 = scipy.stats.ttest_rel(df['Task1a'], df['Task1b']) print('Mean P-value with lick %f' % p1) axis[1, 1].axis('off') for a in axis.flatten(): pf.set_axes_style(a, numticks=4) return shuffle_mean_corr
def compile_meanerror_bytrack_incontrols(self, ControlFolder, ax): numbins = int(self.tracklength / self.trackbins) animals = [ f for f in os.listdir(ControlFolder) if f not in ['BayesResults_All', 'SaveAnalysed'] ] Y_diff_by_track = {k: [] for k in ['Task1a', 'Task1b']} for n, a in enumerate(animals): animalinfo = DataDetails.ControlAnimals(a) bayesmodel = np.load(os.path.join(animalinfo['saveresults'], 'modeloneachtask.npy'), allow_pickle=True).item() for t in animalinfo['task_dict']: kfold = np.size(bayesmodel[t]['K-foldDataframe']['CVIndex']) for k in np.arange(6): y_predict = np.asarray( bayesmodel[t]['K-foldDataframe']['y_predict_angle'][k]) y_test = np.asarray( bayesmodel[t]['K-foldDataframe']['y_test'][k]) y_diff = np.abs( np.nan_to_num(y_predict) - np.nan_to_num(y_test)) * self.trackbins y_diff_append = np.zeros(numbins) for i in np.arange(numbins): Y_indices = np.where(y_test == i)[0] y_diff_append[i] = np.nanmean(y_diff[Y_indices]) Y_diff_by_track[t].append(y_diff_append) for t in ['Task1a', 'Task1b']: Y_diff_by_track[t] = np.asarray(Y_diff_by_track[t]) Y_diff_by_animal = np.abs(Y_diff_by_track['Task1a'] - Y_diff_by_track['Task1b']) for t in ['Task1a', 'Task1b']: meandiff, semdiff = np.nanmean( Y_diff_by_track[t], 0), scipy.stats.sem(Y_diff_by_track[t], 0, nan_policy='omit') error1, error2 = meandiff - semdiff, meandiff + semdiff ax[0].plot(np.arange(numbins), meandiff) ax[0].fill_between(np.arange(numbins), error1, error2, alpha=0.5) ax[0].set_ylabel('BD error (cm)') meandiff, semdiff = np.nanmean( Y_diff_by_animal, 0), scipy.stats.sem(Y_diff_by_animal, 0, nan_policy='omit') ax[1].errorbar(np.arange(numbins), meandiff, yerr=semdiff, marker='o', markerfacecolor='none', color='k') for a in ax: pf.set_axes_style(a) a.set_xlabel('Track Length (cm)') a.set_xlim((1, numbins)) a.set_xticks((1, 20, 40)) a.set_xticklabels((0, 100, 200)) return Y_diff_by_animal