def plot_percentpfs_ofdroppedcells(self, tasks_to_plot, bins=10):
     fs, ax = plt.subplots(1,
                           figsize=(6, 3),
                           dpi=100,
                           sharey='all',
                           sharex='all')
     for n in np.unique(self.pfparam_combined.animalname):
         # Plot COM of place cells along track
         df = self.pfparam_combined[self.pfparam_combined.animalname == n]
         numpyfile = [i for i in self.npyfiles if n in i][0]
         droppedcells = np.load(
             os.path.join(self.CombinedDataFolder, numpyfile),
             allow_pickle=True)['dropped_cells'].item()[tasks_to_plot[1]]
         if n == 'CFC4':
             normfactor = 678
         else:
             normfactor = np.load(os.path.join(self.CombinedDataFolder,
                                               numpyfile),
                                  allow_pickle=True)['numcells']
         df_plot = df[df['Task'] == tasks_to_plot[0]]
         com = df_plot[df_plot['CellNumber'].isin(
             droppedcells)]['WeightedCOM'] * self.trackbins
         hist_com, bins_com, center, width = CommonFunctions.make_histogram(
             com, bins, len(df_plot), self.tracklength)
         ax.bar(center,
                hist_com,
                align='center',
                width=width,
                color=sns.color_palette('dark', 1),
                alpha=0.5)
         ax.set_title('Fields in %s but not in %s' %
                      (tasks_to_plot[0], tasks_to_plot[1]))
         pf.set_axes_style(ax, numticks=4)
def plot_compiled_errorcorrelation(axis, SaveFolder, TaskDict, trackbins, to_plot='R2', classifier_type='Bayes'):
    l = Compile()
    files = [f for f in os.listdir(SaveFolder) if classifier_type in f]
    compilemeanerr = {k: [] for k in TaskDict.keys()}
    compilelaptime = {k: [] for k in TaskDict.keys()}
    for f in files:
        print(f)
        animalname = f[:f.find('_')]
        animal_tasks = DataDetails.ExpAnimalDetails(animalname)['task_dict']
        data = np.load(os.path.join(SaveFolder, f), allow_pickle=True)
        for n, t in enumerate(animal_tasks):
            m, laptime = l.plotlaptime_withbayeserror(axis=axis[n],
                                                      task=t,
                                                      lapframes=data['lapframes'].item()[t],
                                                      y_actual=data['fit'].item()[t]['ytest'],
                                                      y_predicted=data['fit'].item()[t]['yang_pred'],
                                                      numlaps=data['numlaps'].item()[t],
                                                      laptime=data['laptime'].item()[t],
                                                      trackbins=trackbins, to_plot=to_plot)
            pf.set_axes_style(axis[n], numticks=4)
            compilemeanerr[t].extend(m)
            compilelaptime[t].extend(laptime)

    # # get and plot best fit line
    # for n, t in enumerate(TaskDict):
    #     regression_line = l.best_fit_slope_and_intercept(np.asarray(compilemeanerr[t]), np.asarray(compilelaptime[t]))
    #     corrcoef = np.corrcoef(np.asarray(compilemeanerr[t]), np.asarray(compilelaptime[t]))[0, 1]
    #     axis[n].plot(compilemeanerr[t], regression_line, color='k', linewidth=2)
    #     axis[n].set_title('%s r = %0.2f' % (t, corrcoef))

    return compilelaptime, compilemeanerr
Пример #3
0
    def plot_bayesR2(self, r2data, ax, traininglaps=20):
        count = 0
        for n, i in enumerate(self.taskstoplot):
            x = np.arange(count, count + np.size(self.numlicksperlap[i]))
            print(i)
            if i == 'Task2':
                print(self.lickstoplap)
                ax.plot(x[:self.lickstoplap], r2data[x[:self.lickstoplap]], '.-', color=self.task2_colors[0],
                        linewidth=0.5, markersize=1, zorder=2)
                ax.plot(x[self.lickstoplap:], r2data[x[self.lickstoplap:]], '.-', color=self.task2_colors[1],
                        linewidth=0.5, markersize=1, zorder=2)
            elif i in ['Task1', 'Task1a']:
                ax.plot(x[traininglaps:], r2data[x[traininglaps:]], '.-', color=self.colors[n],
                        linewidth=0.5, markersize=1, zorder=2)
            else:
                ax.plot(x, r2data[x], '.-', color=self.colors[n],
                        linewidth=0.5, markersize=1, zorder=2)

            for l in np.arange(np.size(self.numlicksperlap[i])):
                if i in ['Task1', 'Task1a'] and l < traininglaps:
                    continue

                if self.numlicksperlap[i][l] > 0:
                    ax.axvline(count + l, color='grey', linewidth=0.2, zorder=1)
            count += np.size(self.numlicksperlap[i])
        ax.set_xlabel('Lap #')
        ax.set_ylabel('R-squared')
        ax.set_ylim((-0.1, 1))
        pf.set_axes_style(ax)
Пример #4
0
    def plot_barplot_correlation(ax, c_cntrl, c_exp):
        df1 = pd.DataFrame.from_dict(c_exp)
        df1 = df1.dropna()
        df1 = df1.melt(var_name='Task', value_name='Correlation')

        df2 = pd.DataFrame.from_dict(c_cntrl)
        df2 = df2.dropna()
        df2 = df2.melt(var_name='Task', value_name='Correlation')
        df = pd.concat((df1, df2))

        sns.boxplot(x='Task',
                    y='Correlation',
                    data=df[~df['Task'].isin(['Task1', 'Task1a'])],
                    ax=ax,
                    order=['Task1b', 'Task2b', 'Task3', 'Task4'],
                    showfliers=False,
                    width=0.5)

        for t in ['Task2b', 'Task3', 'Task4']:
            t1, p = scipy.stats.ttest_ind(
                df[df.Task == t]['Correlation'],
                df[df.Task == 'Task1b']['Correlation'])
            print('Task1 with %s : %0.3f' % (t, p))

        pf.set_axes_style(ax)
Пример #5
0
    def plot_rastermap(self, ax, ylim=0):
        if ylim == 0:
            ylim = self.numcells

        ax[0].imshow(self.Sm,
                     vmin=0,
                     vmax=0.5,
                     cmap='jet',
                     aspect='auto',
                     extent=[
                         0, self.Sm.shape[1] / self.framespersec, 0,
                         self.Sm.shape[0]
                     ])
        ax[0].set_ylim((0, ylim))
        count = 0
        for n, i in enumerate(self.taskstoplot):
            x = np.linspace(
                count,
                count + np.size(self.good_running_data[i]) / self.framespersec,
                np.size(self.good_running_data[i]))
            ax[0].axvline(x[-1], color='k')
            ax[1].plot(x, self.good_running_data[i], color=self.colors[n])
            count += np.size(self.good_running_data[i]) / self.framespersec
            ax[1].plot(x,
                       self.lick_data[i] * 0.7,
                       '|',
                       color='grey',
                       linewidth=0.5,
                       markersize=10)
        ax[1].set_yticklabels([])

        for a in ax:
            pf.set_axes_style(a, numticks=2)
Пример #6
0
    def compiled_rastermap(self, fighandle, ax, taskstoplot, compiled_fdata, compiled_Smdata, compiled_runningdata,
                           compiled_lickdata):
        im = ax[0].imshow(compiled_Smdata, vmin=0, vmax=0.3, cmap='plasma', aspect='auto', interpolation='hanning',
                          extent=[0, compiled_Smdata.shape[1] / self.framespersec, 0, compiled_Smdata.shape[0]])
        CommonFunctions.plot_colorbar(fighandle, ax[0], im, title=f'\u0394F/F', ticks=[0, 0.3])
        ax[0].set_xlim((0, compiled_Smdata.shape[1] / self.framespersec))

        count = 0
        count_dff = 0
        for n, i in enumerate(taskstoplot):
            x = np.linspace(count, count + np.size(compiled_runningdata[i]) / self.framespersec,
                            np.size(compiled_runningdata[i]))
            ax[0].axvline(x[-1], color='k', linewidth=0.5)
            if i == 'Task2':
                ax[2].plot(x[:self.lickstopframe], compiled_runningdata[i][:self.lickstopframe],
                           color=self.task2_colors[0], linewidth=0.5)
                ax[2].plot(x[self.lickstopframe:], compiled_runningdata[i][self.lickstopframe:],
                           color=self.task2_colors[1], linewidth=0.5)
            else:
                ax[2].plot(x, compiled_runningdata[i], color=self.colors[n], linewidth=0.5)
            ax[2].plot(x, compiled_lickdata[i] * 0.75, '|', color='grey', markeredgewidth=0.05, markersize=5,
                       alpha=0.5)
            ax[1].plot(x, savgol_filter(
                np.nanmean(compiled_fdata[:, count_dff:count_dff + np.size(compiled_runningdata[i])], 0), 31, 2),
                       color=self.colors[n], linewidth=0.5)
            count += np.size(compiled_runningdata[i]) / self.framespersec
            count_dff += np.size(compiled_runningdata[i])

        ax[2].set_yticklabels([])
        ax[1].set_ylim((0, 0.15))

        pf.set_axes_style(ax[0], numticks=4)
        pf.set_axes_style(ax[1], numticks=1)
Пример #7
0
 def get_bayes_data(self, axis, accuracy_measure='R2'):
     decoderfit = self.BayesData['fit'].item()
     count = 0
     for n, t in enumerate(self.taskstoplot):
         y_actual = decoderfit[t]['ytest']
         y_predicted = decoderfit[t]['yang_pred']
         R2 = self.calulate_lapwiseerror(y_actual, y_predicted,
                                         self.good_lapframes[t],
                                         accuracy_measure)
         if t == 'Task1':
             R2 = R2[-5:]
         numlaps = np.size(R2)
         print(numlaps)
         x = np.arange(count, count + numlaps)
         axis.plot(x,
                   R2,
                   '.-',
                   color=self.colors[n],
                   linewidth=2,
                   markeredgecolor='black',
                   zorder=2)
         for nl in np.arange(numlaps):
             if self.numlicksperlap[t][nl] > 0:
                 axis.axvline(nl + count,
                              color='k',
                              alpha=0.5,
                              linewidth=1,
                              markersize=10,
                              zorder=1)
         count += numlaps
     if accuracy_measure == 'R2':
         axis.set_ylabel('Goodness of fit')
     else:
         axis.set_ylabel('Decoder Error (cm)')
     pf.set_axes_style(axis, numticks=3)
def plot_lapwiseaccuracy_concatenated(axis, animalinfo, taskdict, decoderfit, data, to_plot='R2'):
    l = LapWiseAnalysis()
    count = 0
    c = sns.color_palette('dark')
    for n, t in enumerate(taskdict):
        R2, accuracy = l.calulate_lapwiseerror(y_actual=decoderfit[t]['ytest'],
                                               y_predicted=decoderfit[t]['ypred'],
                                               trackbins=animalinfo['trackbins'],
                                               numlaps=data.numlaps[t],
                                               lapframes=data.good_lapframes[t])

        if to_plot == 'R2':
            axis.set_ylabel('Goodness of fit')
            if t == 'Task1':
                data_plot = R2[-5:]
            else:
                data_plot = R2
        else:
            axis.set_ylabel('Decoding Accuracy')
            if t == 'Task1':
                data_plot = accuracy[-5:]
            else:
                data_plot = accuracy
        numlaps = np.size(data_plot)
        x = np.arange(count, count + numlaps)
        axis.plot(x, data_plot, color=c[n], linewidth=2)
        licks = data.numlicksperlap[t]
        for nl in np.arange(numlaps):
            if licks[nl] > 0:
                axis.axvline(nl + count, color='grey', linewidth=1, markersize=10)

        count += numlaps
    pf.set_axes_style(axis)
Пример #9
0
 def plot_stability_precision_of_reward_cells(self, reward_data):
     for index, row in reward_data.iterrows():
         animalname = row['AnimalName']
         cellnumber = row['CellNumber']
         thiscell_data = self.rewardparam_combined[
             (self.rewardparam_combined['animalname'] == animalname)
             & (self.rewardparam_combined['CellNumber'] == cellnumber)]
         if index == 0:
             combinedf = thiscell_data
         else:
             combinedf = pd.concat((combinedf, thiscell_data))
     columns_to_plot = ['Precision', 'Stability']
     fs, ax = plt.subplots(1, len(columns_to_plot), figsize=(7, 3))
     for n, c in enumerate(columns_to_plot):
         sns.boxplot(x='Task',
                     y=c,
                     data=combinedf,
                     ax=ax[n],
                     order=self.TaskDict.keys(),
                     palette='Blues',
                     showfliers=False)
         # sns.stripplot(x='Task', y=c, data=combinedf, ax=ax[n], color='k', size=2)
         pf.set_axes_style(ax[n])
     fs.tight_layout()
     return combinedf
Пример #10
0
    def compile_mean_bderror(self, ax):
        mean_error = {k: [] for k in ['R2', 'Task', 'animalname', 'BD error (cm)', 'BD accuracy']}
        for a in self.animals:
            animalinfo = DataDetails.ExpAnimalDetails(a)
            if self.datatype == 'endzonerem':
                bayesmodel = np.load(
                    os.path.join(animalinfo['saveresults'], 'modeloneachtask_withendzonerem.npy'),
                    allow_pickle=True).item()
            else:
                bayesmodel = np.load(os.path.join(animalinfo['saveresults'], 'modeloneachtask.npy'),
                                     allow_pickle=True).item()
            for t in animalinfo['task_dict'].keys():
                kfold = np.max(bayesmodel[t]['K-foldDataframe']['CVIndex'])
                mean_error['R2'].extend(bayesmodel[t]['K-foldDataframe']['R2_angle'])
                mean_error['BD accuracy'].extend(bayesmodel[t]['K-foldDataframe']['ModelAccuracy'])
                for k in np.arange(kfold):
                    mean_error['BD error (cm)'].append(
                        np.mean(bayesmodel[t]['K-foldDataframe']['Y_ang_diff'][k] * self.trackbins))
                    mean_error['Task'].append(t)
                    mean_error['animalname'].append(a)

        mean_error_df = pd.DataFrame.from_dict(mean_error)
        for n, i in enumerate(['R2', 'BD accuracy', 'BD error (cm)']):
            sns.boxplot(x='Task', y=i, data=mean_error_df, palette='Blues', ax=ax[n], showfliers=False)
            for t in self.taskdict.keys():
                if t != 'Task1':
                    d, p = Stats.significance_test(mean_error_df[mean_error_df.Task == t][i],
                                                   mean_error_df[mean_error_df.Task == 'Task1'][i],
                                                   type_of_test='KStest')
                    print(f'%s: %s: KStest: p-value %0.4f' % (i, t, p))
            ax[n].set_xlabel('')
            pf.set_axes_style(ax[n], numticks=4)
Пример #11
0
    def compile_confusion_matrix(self, fs, ax):
        cm_all = {k: np.zeros((int(self.tracklength / self.trackbins), int(self.tracklength / self.trackbins))) for k in
                  self.taskdict.keys()}
        for a in self.animals:
            animalinfo = DataDetails.ExpAnimalDetails(a)
            if self.datatype == 'endzonerem':
                bayesmodel = np.load(
                    os.path.join(animalinfo['saveresults'], 'modeloneachtask_withendzonerem.npy'),
                    allow_pickle=True).item()
            else:
                bayesmodel = np.load(os.path.join(animalinfo['saveresults'], 'modeloneachtask.npy'),
                                     allow_pickle=True).item()

            for t in animalinfo['task_dict']:
                cm = bayesmodel[t]['cm']
                cm_all[t] += cm

        for n, t in enumerate(self.taskdict.keys()):
            cm_all[t] = cm_all[t].astype('float') / cm_all[t].sum(axis=1)[:, np.newaxis]
            img = ax[n].imshow(cm_all[t], cmap="Blues", vmin=0, vmax=0.4, interpolation='nearest')
            ax[n].plot(ax[n].get_xlim()[::-1], ax[n].get_ylim(), ls="--", c=".3", lw=1)
            pf.set_axes_style(ax[n], numticks=3, both=True)

            if n == len(self.taskdict) - 1:
                CommonFunctions.create_colorbar(fighandle=fs, axis=ax[n], imghandle=img, title='Probability',
                                                ticks=[0, 0.4])

        ax[0].set_ylabel('Actual')
        ax[0].set_xlabel('Predicted')
Пример #12
0
    def velocity_inspace_byattention(self, axis):
        plot_title = [
            'With Lick', 'Attention withoutlick', 'Without attention'
        ]
        for n, laptype in enumerate([
                'lapswithlicks', 'attentivelaps_withoutlicks',
                'notattentivelaps_withoutlicks'
        ]):
            vinspace = np.asarray([])
            for a in self.animalname:
                required_laps = np.load(os.path.join(self.FolderName, a,
                                                     'SaveAnalysed',
                                                     'attentionlaps.npz'),
                                        allow_pickle=True)

                laps = required_laps[laptype]
                if n == 1:
                    speed_ratio = np.asarray(self.speed_ratio[a]['Task2'])
                    weird = np.where(speed_ratio > 1.06)[0]
                    laps = np.intersect1d(weird, laps)

                vinspace = np.vstack(
                    (vinspace, self.velocity_slope[a]['Task2'][laps, :])) if vinspace.size else \
                    self.velocity_slope[a]['Task2'][laps, :]

            axis[n].plot(vinspace.T, color='lightgrey')
            axis[n].plot(np.mean(vinspace, 0), color='k')
            axis[n].set_title(plot_title[n])
            axis[n].set_xticks([0, 18, 36])
            axis[n].set_xticklabels([0, 100, 200])
            axis[n].set_xlabel('Track Length (cm)')
            pf.set_axes_style(axis[n])
        axis[0].set_ylabel('Normalized velocity')
    def plot_decoderaccuracy_with_tracklength(axis, modeldataframe, tracklength, trackbins, taskcolors,
                                              angleflag=False):
        # Plot accuracy with maze position
        # Only plot for reward vs no reward condition
        numbins = int(tracklength / trackbins)
        for t in ['Task1a', 'Task1b']:
            Y_diff_by_track_mean = np.zeros((kfold_splits, numbins))
            for k in np.arange(kfold_splits):
                if angleflag:
                    y_diff = np.asarray(modeldataframe[t]['K-foldDataframe']['Y_ang_diff'][k]) * trackbins
                else:
                    y_diff = np.asarray(modeldataframe[t]['K-foldDataframe']['Y_diff'][k]) * trackbins

                y_test = np.asarray(modeldataframe[t]['K-foldDataframe']['y_test'][k])
                for i in np.arange(numbins):
                    Y_indices = np.where(y_test == i)[0]
                    Y_diff_by_track_mean[k, i] = np.mean(y_diff[Y_indices])

            meandiff = np.nanmean(Y_diff_by_track_mean, 0)
            semdiff = scipy.stats.sem(Y_diff_by_track_mean, 0, nan_policy='omit')
            error1, error2 = meandiff - semdiff, meandiff + semdiff
            axis.plot(np.arange(numbins), meandiff, color=taskcolors[t])
            axis.fill_between(np.arange(numbins), error1, error2, alpha=0.5, color=taskcolors[t])
            axis.set_xlabel('Track Length (cm)')
            axis.set_ylabel('Difference in\ndecoder accuracy (cm)')
            CommonFunctions.convertaxis_to_tracklength(axis, tracklength, trackbins, convert_axis='x')
            axis.set_xlim((0, tracklength / trackbins))
        pf.set_axes_style(axis, numticks=5)
Пример #14
0
    def plot_bayeserror_with_slidingwindow(axis, colors, bayeserror, numlicks):
        zerolick = np.where(numlicks == 0)
        nonzerolick = np.where(numlicks > 0)
        print(np.shape(zerolick), np.shape(nonzerolick))
        label = ['With Licks', 'Without Licks']
        ax1 = axis[0].twinx()
        for n, l in enumerate([nonzerolick, zerolick]):
            weights = np.ones_like(bayeserror[l]) / float(len(bayeserror[l]))
            sns.distplot(bayeserror[l],
                         bins=np.arange(-1, 1, 50),
                         hist=False,
                         ax=ax1,
                         color=colors[n],
                         label=label[n])
            axis[0].hist(bayeserror[l],
                         bins=np.linspace(-0.5, 1, 20),
                         color=colors[n],
                         alpha=0.5,
                         weights=weights)
            axis[1].hist(bayeserror[l],
                         bins=np.linspace(-0.5, 1, 1000),
                         color=colors[n],
                         normed=True,
                         cumulative=True,
                         histtype='step')

        axis[1].set_ylim((0, 1))
        axis[0].set_ylabel('Normalized laps')
        for a in axis:
            pf.set_axes_style(a, numticks=4)
            a.set_xlabel('R-squared')
        t, p = scipy.stats.ks_2samp(bayeserror[nonzerolick],
                                    bayeserror[zerolick])
        axis[0].set_title(f'P-value : %f' % p)
    def plot_placecells_with_track_pertask(self,
                                           pc_activity,
                                           sorted_pcs,
                                           figsize=(10, 4)):
        taskaxis = {'Task1': 0, 'Task2': 1, 'Task3': 2, 'Task4': 3, 'Task5': 4}
        fs, ax1 = plt.subplots(1,
                               len(self.TaskDict),
                               figsize=figsize,
                               dpi=100,
                               sharex='all',
                               sharey='all')
        for taskname in self.TaskDict.keys():
            task_data = pc_activity[taskname][sorted_pcs[taskname], :]
            normalise_data = (task_data - np.min(task_data.flatten())) / (
                np.max(task_data.flatten()) - np.min(task_data.flatten()))
            ax1[taskaxis[taskname]].imshow(normalise_data,
                                           aspect='auto',
                                           cmap='jet',
                                           interpolation='nearest',
                                           vmin=0,
                                           vmax=0.5)

            ax1[taskaxis[taskname]].set_title(self.new_taskDict[taskname])
            ax1[taskaxis[taskname]].set_xticks([0, 20, 39])
            ax1[taskaxis[taskname]].set_xticklabels([0, 100, 200])
            ax1[taskaxis[taskname]].set_xlim((0, 39))
            pf.set_axes_style(ax1[taskaxis[taskname]], numticks=4)
        ax1[0].set_xlabel('Track Length (cm)')
        ax1[0].set_ylabel('Cell')

        ax1[0].locator_params(nbins=4)
        fs.tight_layout()
        plt.show()
    def plot_confusion_matrix_ofkfold(fighandle, axis, cv_dataframe, tracklength, trackbins):
        cm_all = np.zeros((int(tracklength / trackbins), int(tracklength / trackbins)))  # Bins by binss
        for i in np.arange(kfold_splits):
            y_actual = cv_dataframe['y_test'][i]
            y_predicted = cv_dataframe['y_predict_angle'][i]
            cm = confusion_matrix(y_actual, y_predicted)
            print(np.shape(cm))
            if np.size(cm, 0) != int(tracklength / trackbins):
                cm_temp = np.zeros((int(tracklength / trackbins), int(tracklength / trackbins)))
                cm_temp[:np.size(cm, 0), :np.size(cm, 1)] = cm
                print('Correcting', np.shape(cm_temp))
                cm_all += cm_temp
            else:
                cm_all += cm
        cm_all = cm_all.astype('float') / cm_all.sum(axis=1)[:, np.newaxis]
        img = axis.imshow(cm_all, cmap="Blues", vmin=0, vmax=0.5,
                          interpolation='bilinear')
        CommonFunctions.create_colorbar(fighandle=fighandle, axis=axis, imghandle=img, title='Probability')
        # convert axis ro track length
        # CommonFunctions.convertaxis_to_tracklength(axis, tracklength, trackbins, convert_axis='both')
        axis.plot(axis.get_xlim()[::-1], axis.get_ylim(), ls="--", c=".3", lw=1)

        axis.set_ylabel('Actual')
        axis.set_xlabel('Predicted')
        pf.set_axes_style(axis, numticks=4)

        return cm_all
Пример #17
0
    def plot_decoderaccuracy_with_numcells(axis,
                                           modeldataframe,
                                           taskdict,
                                           taskcolors,
                                           angleflag=False):
        for t in taskdict.keys():
            numcells_df = modeldataframe[t]['Numcells_Dataframe']
            if angleflag:
                sns.pointplot(x='SampleSize',
                              y='R2_angle',
                              data=numcells_df,
                              color=taskcolors[t],
                              ax=axis)
            else:
                sns.pointplot(x='SampleSize',
                              y='R2',
                              data=numcells_df,
                              color=taskcolors[t],
                              ax=axis)

        # axis.set_ylim((-1.6, 1))

        axis.legend(
            handles=axis.lines[::len(numcells_df['SampleSize'].unique()) + 1],
            labels=taskdict.keys(),
            loc='center left',
            bbox_to_anchor=(1, 0.5))
        axis.set_xlabel('Percentage of active cells used')
        axis.set_ylabel('Decoding accuracy')
        # axis.set_aspect(aspect=1.6)
        plt.close(2)
        pf.set_axes_style(axis, numticks=4)
Пример #18
0
 def plot_lick_withtime(axis,
                        numlicks,
                        axislabel,
                        axislim=(0, 1),
                        color='k',
                        errorbar=0,
                        dashed=1):
     # num = (numlicks - np.min(numlicks, 1)[:, np.newaxis])
     # denom = (np.max(numlicks, 1) - np.min(numlicks, 1))[:, np.newaxis]
     numlicks = numlicks / np.nanmax(numlicks, 1)[:, np.newaxis]
     mean_licks = np.nanmean(numlicks, 0)
     sem_licks = scipy.stats.sem(numlicks, 0, nan_policy='omit')
     if dashed:
         axis.plot(mean_licks, 'o-', color=color, markerfacecolor='none')
     else:
         axis.plot(mean_licks, 'o', color=color, markerfacecolor='none')
     if errorbar == 1:
         axis.errorbar(np.arange(np.size(mean_licks)),
                       mean_licks,
                       yerr=sem_licks,
                       ls='none',
                       color='lightgrey')
     elif errorbar == 0:
         axis.fill_between(np.arange(np.size(mean_licks)),
                           mean_licks - sem_licks,
                           mean_licks + sem_licks,
                           color='lightgrey')
     axis.set_ylabel(axislabel)
     axis.set_xlabel('Laps in time')
     axis.set_ylim(axislim)
     pf.set_axes_style(axis, numticks=4)
Пример #19
0
    def plot_rewardcoms_on_allcoms(self,
                                   combined_data,
                                   ax,
                                   taskA,
                                   taskB,
                                   color,
                                   reward_flag=0):
        if reward_flag:
            y = (combined_data[f'WeightedCOM_%s' % taskA] /
                 self.framerate) - self.nsecondsroundrew
            x = (combined_data[f'WeightedCOM_%s' % taskB] /
                 self.framerate) - self.nsecondsroundrew
            ax.plot([-self.nsecondsroundrew, self.nsecondsroundrew * 2],
                    [-self.nsecondsroundrew, self.nsecondsroundrew * 2],
                    linewidth=2,
                    color=".3")
        else:
            y, x = combined_data[f'WeightedCOM_%s' %
                                 taskA] * self.trackbins, combined_data[
                                     f'WeightedCOM_%s' %
                                     taskB] * self.trackbins
            ax.plot([0, self.tracklength], [0, self.tracklength],
                    linewidth=2,
                    color=".3")

        ax.scatter(y, x, color=color)
        ax.set_xlabel(taskB)
        ax.set_ylabel(taskA)
        ax.set_title('Selected Reward Cells')
        pf.set_axes_style(ax, numticks=3)
def plot_compiledconfusionmatrix(axis, SaveFolder, TaskDict, classifier_type='Bayes'):
    files = [f for f in os.listdir(SaveFolder) if classifier_type in f]
    all_ytest = {k: [] for k in TaskDict.keys()}
    all_ypred = {k: [] for k in TaskDict.keys()}
    for f in files:
        animal_tasks = DataDetails.ExpAnimalDetails(f[:f.find('_')])['task_dict']
        data = np.load(os.path.join(SaveFolder, f), allow_pickle=True)
        print(f)
        for n, t in enumerate(animal_tasks):
            if t == 'Task1a':
                all_ytest[t].extend(data['fit'].item()[t]['ytest'][-500:])
                all_ypred[t].extend(data['fit'].item()[t]['yang_pred'][-500:])
            else:
                all_ytest[t].extend(data['fit'].item()[t]['ytest'])
                all_ypred[t].extend(data['fit'].item()[t]['yang_pred'])

    for n, t in enumerate(['Task1', 'Task1a']):
        y_actual = all_ytest[t]
        y_predicted = all_ypred[t]
        cm = confusion_matrix(y_actual, y_predicted)
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        img = axis[n].imshow(cm, cmap="Blues", vmin=0, vmax=0.4, interpolation='nearest')
        axis[n].plot(axis[n].get_xlim()[::-1], axis[n].get_ylim(), ls="--", c=".3", lw=1)
        pf.set_axes_style(axis[n], numticks=3, both=True)
        axis[n].set_title(t)
    axis[0].set_ylabel('Actual')
    axis[0].set_xlabel('Predicted')
    def plot_bayeserror_with_slidingwindow(self, taskstoplot, bayeserror, numlicks):
        # Plot just No Reward data
        fs, ax = plt.subplots(2, figsize=(6, 8))
        colors = sns.color_palette('deep')
        # # Get mean of licks
        # norm_lick = numlicks['Task2'] / np.nanmax(numlicks['Task2'], 1)[:, np.newaxis]
        # mean_lick = np.mean(norm_lick, 0)
        # sem_lick = scipy.stats.sem(norm_lick, 0)
        # Flatten error and licks and clean up
        bayeserror_flat = {k: [] for k in taskstoplot}
        numlicks_flat = {k: [] for k in taskstoplot}
        norm_lick = {k: [] for k in taskstoplot}
        # Remove those with NAN
        for t in taskstoplot:
            bayeserror_flat[t] = bayeserror[t].flatten()
            numlicks_flat[t] = numlicks[t].flatten()
            notnan_index = np.where(~np.isnan(bayeserror_flat[t]))[0]
            print(np.shape(bayeserror_flat[t]), np.shape(numlicks_flat[t]))
            bayeserror_flat[t] = bayeserror_flat[t][notnan_index]
            numlicks_flat[t] = numlicks_flat[t][notnan_index]
            temp_lick = numlicks[t] / np.nanmax(numlicks[t], 1)[:, np.newaxis]
            norm_lick[t] = temp_lick.flatten()[notnan_index]
        zerolick = np.where(numlicks_flat['Task2'] == 0)
        nonzerolick = np.where(numlicks_flat['Task2'] > 0)
        # Percentage of laps with greater correlation
        print('Zero licks with high correlation %d%%' % (
                (np.size(np.where(bayeserror_flat['Task2'][zerolick] > 0.8)) / np.size(zerolick)) * 100))
        print('High licks with high correlation %d%%' % (
                (np.size(np.where(bayeserror_flat['Task2'][nonzerolick] > 0.8)) / np.size(nonzerolick)) * 100))
        # ax[0].scatter(bayeserror_flat['Task2'][nonzerolick], norm_lick['Task2'][nonzerolick], marker='+', s=100,
        #               alpha=0.5, color=colors[0])
        # ax[0].plot(bayeserror['Task2'].flatten()[zerolick], norm_lick['Task2'][zerolick], '|',
        #            markersize=12, alpha=0.5, color=colors[1], linewidth=0.5)
        # ax[0].set_ylabel('Normalized licks')
        # ax[0].set_xlabel('R-squared')

        # print(bayeserror['Task2'].flatten()[zerolick])
        # ax[1].hist(bayeserror_flat['Task2'][nonzerolick], cumulative=True, histtype='step', normed=True, bins=10, alpha=0.5)
        # ax[1].hist(bayeserror_flat['Task2'][zerolick], cumulative=True, histtype='step', normed=True, bins=10, alpha=0.5)

        y = [0.3, 0]
        label = ['With Licks', 'Without Licks']
        for n, l in enumerate([nonzerolick, zerolick]):
            sns.distplot(bayeserror_flat['Task2'][l], bins=np.arange(-1, 1, 50), hist=False,
                         ax=ax[1], kde_kws={'cut': 0}, color=colors[n], label=label[n])
            ax[1].hist(bayeserror_flat['Task2'][l], bins=np.linspace(-0.5, 1, 20), color=colors[n], alpha=0.5,
                       normed=True)
            # ax[1].plot(bayeserror_flat['Task2'][l], [y[n]] * len(bayeserror_flat['Task2'][l]), '|', markersize=12,
            #            alpha=0.5, linewidth=0.5, color=colors[n])
            ax[0].hist(bayeserror_flat['Task2'][l], bins=np.linspace(-0.5, 1, 100), color=colors[n], alpha=0.5,
                       normed=True, cumulative=True, histtype='step')

        ax[1].legend()
        ax[1].set_xlabel('R-squared')
        ax[1].set_ylabel('Normalised histrogram')

        for a in ax:
            pf.set_axes_style(a, numticks=4)
Пример #22
0
    def plot_com_scatter_heatmap(fig,
                                 axis,
                                 combined_dataset,
                                 taskA,
                                 taskB,
                                 tracklength,
                                 bins=10,
                                 vmax=0):
        # Scatter plots
        y = combined_dataset[0, :]
        x = combined_dataset[1, :]
        axis[0].scatter(y, x, color='k', s=3)
        axis[0].plot([0, tracklength], [0, tracklength],
                     linewidth=2,
                     color=".3")
        axis[0].set_xlabel(taskB)
        axis[0].set_ylabel(taskA)
        axis[0].set_title('Center of Mass')

        # Heatmap of scatter plot
        heatmap, xedges, yedges = np.histogram2d(y, x, bins=bins)
        heatmap = (heatmap / np.size(y)) * 100
        extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
        if vmax == 0:
            img = axis[1].imshow(heatmap.T,
                                 cmap='gray_r',
                                 extent=extent,
                                 interpolation='bilinear',
                                 origin='lower',
                                 vmin=0,
                                 vmax=np.max(heatmap))
        else:
            img = axis[1].imshow(heatmap.T,
                                 cmap='gray_r',
                                 extent=extent,
                                 interpolation='bilinear',
                                 origin='lower',
                                 vmin=0,
                                 vmax=vmax)

        axis[1].plot([0 + bins, tracklength - bins],
                     [0 + bins, tracklength - bins],
                     linewidth=2,
                     color=".3")
        axins = PlotPCs.add_colorbar_as_inset(axes=axis[1])
        if vmax == 0:
            cb = fig.colorbar(img,
                              cax=axins,
                              pad=0.2,
                              ticks=[0, np.int(np.max(heatmap))])
        else:
            cb = fig.colorbar(img, cax=axins, pad=0.2, ticks=[0, vmax])
        cb.set_label('% Field Density', rotation=270, labelpad=12)

        for a in axis:
            pf.set_axes_style(a, numticks=4)
            a.set_ylim((0, tracklength))
            a.set_xlim((0, tracklength))
 def plot_bayeserror_bytimebins(self, ax, bin_df, plottitle):
     sns.boxplot(x='Bin', y='Value', data=bin_df, order=[f'Bin%d' % i for i in np.arange(4)], palette='Reds',
                 ax=ax)
     sns.stripplot(x='Bin', y='Value', data=bin_df, order=[f'Bin%d' % i for i in np.arange(4)], palette='Reds',
                   ax=ax)
     ax.set_title(plottitle)
     ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
     ax.set_xlabel('')
     pf.set_axes_style(ax, numticks=4)
Пример #24
0
    def plot_com_scatter_heatmap(self,
                                 combined_dataset,
                                 taskA,
                                 taskB,
                                 bins=10,
                                 vmax=0,
                                 datatype='array'):
        # Scatter plots
        fs, ax = plt.subplots(1, 2, figsize=(8, 4))
        if datatype == 'array':
            y = combined_dataset[0, :]
            x = combined_dataset[1, :]
        ax[0].scatter(y, x, color='k', s=3)
        ax[0].plot([0, self.tracklength], [0, self.tracklength],
                   linewidth=1,
                   color=".3")
        ax[0].set_xlabel(taskB)
        ax[0].set_ylabel(taskA)
        ax[0].set_title('Center of Mass')

        # Heatmap of scatter plot
        heatmap, xedges, yedges = np.histogram2d(y, x, bins=bins)
        heatmap = (heatmap / np.size(y)) * 100
        extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
        if vmax == 0:
            img = ax[1].imshow(heatmap.T,
                               cmap='gray_r',
                               extent=extent,
                               interpolation='bilinear',
                               origin='lower',
                               vmin=0,
                               vmax=np.max(heatmap))
        else:
            img = ax[1].imshow(heatmap.T,
                               cmap='gray_r',
                               extent=extent,
                               interpolation='bilinear',
                               origin='lower',
                               vmin=0,
                               vmax=vmax)

        ax[1].plot([0 + bins, self.tracklength - bins],
                   [0 + bins, self.tracklength - bins],
                   linewidth=2,
                   color=".3")
        axins = CommonFunctions.add_colorbar_as_inset(axes=ax[1])
        if vmax == 0:
            cb = fs.colorbar(img,
                             cax=axins,
                             pad=0.2,
                             ticks=[0, np.int(np.max(heatmap))])
        else:
            cb = fs.colorbar(img, cax=axins, pad=0.2, ticks=[0, vmax])
        cb.set_label('% Field Density', rotation=270, labelpad=12)

        for a in ax:
            pf.set_axes_style(a, numticks=5)
Пример #25
0
 def plot_numcells(axis, numcells_df, taskstoplot):
     numcells_df = numcells_df[taskstoplot]
     df = numcells_df.melt(var_name='Task', value_name='numcells')
     sns.boxplot(x='Task', y='numcells', data=df, ax=axis, width=0.5)
     for n, row in numcells_df.iterrows():
         axis.plot(row, 'o-', color='k', markerfacecolor='none')
     axis.set_ylabel('Percentage of place cells')
     GetPValues().get_shuffle_pvalue(numcells_df,
                                     taskstocompare=taskstoplot)
     pf.set_axes_style(axis, numticks=4)
    def plot_pfparams(self, tasks_to_plot):
        # Plot a combined historgram and a boxplot of means
        columns_to_plot = [
            'Precision', 'Width', 'FiringRatio', 'Firingintensity', 'Stability'
        ]
        df_plot = self.pfparam_combined[self.pfparam_combined['Task'].isin(
            tasks_to_plot)]
        fs, ax = plt.subplots(len(columns_to_plot),
                              3,
                              figsize=(7, 8),
                              gridspec_kw={'width_ratios': [2, 2, 1]})
        for n1, c in enumerate(columns_to_plot):
            # Plot boxplot
            sns.boxplot(x='Task',
                        y=c,
                        data=df_plot,
                        ax=ax[n1, 2],
                        order=tasks_to_plot,
                        showfliers=False)
            ax[n1, 2].set_xlabel('')
            # For significance test
            x, y = df_plot[df_plot['Task'] == tasks_to_plot[0]][c], df_plot[
                df_plot['Task'] == tasks_to_plot[1]][c]
            d, p = CommonFunctions.significance_test(x,
                                                     y,
                                                     type_of_test='KStest')
            print(f'%s: KStest : p-value %0.4f' % (c, p))
            d, p = CommonFunctions.significance_test(x,
                                                     y,
                                                     type_of_test='Wilcoxon')
            print(f'%s: Wilcoxon : p-value %0.4f' % (c, p))
            for n2, t in enumerate(tasks_to_plot):
                # Plot histogram
                d = df_plot[df_plot['Task'] == t][c]
                d = d[~np.isnan(d)]
                sns.distplot(d, ax=ax[n1, 0], kde=False)
                ax[n1, 1].hist(d,
                               bins=1000,
                               normed=True,
                               cumulative=True,
                               label='CDF',
                               histtype='step',
                               linewidth=1,
                               alpha=0.8)
                # Does the sample come from a null distribution
                s, p = CommonFunctions.normalitytest(d)
                print(
                    '%s in Task %s against normal distribution p-value %0.4f' %
                    (c, t, p))

        for a in ax.flatten():
            pf.set_axes_style(a, numticks=3)
        fs.tight_layout()
        fs.savefig('Place Cell params.pdf', bbox_inches='tight')
Пример #27
0
def plot_compilederror_withlick(axis,
                                SaveFolder,
                                TaskDict,
                                trackbins,
                                separate_lickflag=0,
                                licktype='all',
                                to_plot='R2',
                                classifier_type='Bayes'):
    l = Compile()
    files = [f for f in os.listdir(SaveFolder) if classifier_type in f]
    compilelickdata = {k: [] for k in TaskDict.keys()}
    compileerror = {k: [] for k in TaskDict.keys()}
    lickstoplap = []
    animalname = []
    for f in files:
        # print(f)
        animalname.append(f[:f.find('_')])
        animal_tasks = DataDetails.ExpAnimalDetails(
            f[:f.find('_')])['task_dict']
        print(len(animal_tasks))
        data = np.load(os.path.join(SaveFolder, f), allow_pickle=True)
        if data['lickstoplap'].item()['Task2'] > 2:
            print(f)
            for n, t in enumerate(animal_tasks):
                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 licktype == 'all':
                    licks = data['alllicks'].item()
                    lickstop = data['lickstoplap'].item()['Task2']
                else:
                    licks = data['licks_befclick'].item()
                    lickstop = data['lickstoplap_befclick'].item()['Task2']

                if to_plot == 'R2':
                    data_compile = lap_r2
                else:
                    data_compile = lap_accuracy

                l.plot_bayeserror_with_lickrate(
                    data_compile, licks[t],
                    data['lickstoplap'].item()['Task2'], t, axis[n],
                    separate_lickflag)
                pf.set_axes_style(axis[n], numticks=4)
                compilelickdata[t].append(licks[t])
                compileerror[t].append(np.asarray(data_compile))
            lickstoplap.append(lickstop)

    return compileerror, compilelickdata, np.asarray(lickstoplap), animalname
Пример #28
0
    def plot_scatter(self, axis, animallist, thresh=1.0, notflag=0):
        speed_ratio = []
        bayes = []
        for a in self.animalname:
            if len(animallist) > 0:
                if notflag:
                    if a not in animallist:
                        bayes.extend(self.bayescompiled[a]['Task2'])
                        speed_ratio.extend(
                            self.speed_ratio[a]['Task2'] /
                            np.mean(self.speed_ratio[a]['Task1']))
                else:
                    if a in animallist:
                        bayes.extend(self.bayescompiled[a]['Task2'])
                        speed_ratio.extend(
                            self.speed_ratio[a]['Task2'] /
                            np.mean(self.speed_ratio[a]['Task1']))
            else:
                bayes.extend(self.bayescompiled[a]['Task2'])
                speed_ratio.extend(self.speed_ratio[a]['Task2'] /
                                   np.mean(self.speed_ratio[a]['Task1']))
        print(np.shape(bayes))
        # if notflag:
        #     bayes, speed_ratio = np.asarray(bayes), np.asarray(speed_ratio)
        #     bayes = bayes[speed_ratio > thresh]
        #     speed_ratio = speed_ratio[speed_ratio > thresh]

        axis.plot(bayes,
                  speed_ratio,
                  'o',
                  color='grey',
                  markerfacecolor='none')
        # regression_line = CommonFunctions.best_fit_slope_and_intercept(np.asarray(bayes),
        #                                                                np.asarray(speed_ratio))
        corrcoef = np.corrcoef(np.nan_to_num(bayes),
                               np.nan_to_num(speed_ratio))[0, 1]
        pearsonsr = scipy.stats.pearsonr(np.nan_to_num(bayes),
                                         np.nan_to_num(speed_ratio))

        y_pred_linearreg, rsquared = CommonFunctions.linear_regression(
            np.asarray(bayes), np.asarray(speed_ratio))
        axis.plot(bayes, y_pred_linearreg, color='k', linewidth=1)
        # speed_dark = self.get_dark_attentiondata()
        # axis.axhline(np.mean(speed_dark), color='r')
        # axis.axhline(np.mean(speed_dark) - scipy.stats.sem(speed_dark), linestyle='--', color='r')
        # axis.axhline(np.mean(speed_dark) + scipy.stats.sem(speed_dark), linestyle='--', color='r')
        pf.set_axes_style(axis)
        axis.set_title('r2=%0.3f, r=%0.3f, p=%0.3f' %
                       (rsquared, corrcoef, pearsonsr[1]))
        axis.set_xlabel('Decoder R2')
        axis.set_ylabel('Attention')
Пример #29
0
    def plot_histogram_speed_ratio(self, axis, taskstoplot, bins=20):
        # Plot taskdata
        speed_ratio_exp = []
        axis1 = axis.twinx()
        for n, t in enumerate(taskstoplot):
            for a in self.speed_ratio.keys():
                speed_ratio_exp.extend(self.speed_ratio[a][t])
            weights = np.ones_like(speed_ratio_exp) / float(
                len(speed_ratio_exp))
            sns.distplot(speed_ratio_exp,
                         bins=np.linspace(0, 2, bins),
                         color=self.colors[n],
                         hist=False,
                         kde=True,
                         ax=axis1)
            axis.hist(speed_ratio_exp,
                      bins=np.linspace(0, 2, bins),
                      color=self.colors[n],
                      linewidth=2,
                      weights=weights,
                      label=t,
                      alpha=0.5)
        # plot dark data
        speed_ratio_dark = []
        for a in self.speed_ratio_dark.keys():
            speed_ratio_dark.extend(self.speed_ratio_dark[a])
        weights = np.ones_like(speed_ratio_dark) / float(len(speed_ratio_dark))
        sns.distplot(speed_ratio_dark,
                     bins=np.linspace(0, 2, bins),
                     kde=True,
                     ax=axis1,
                     hist=False,
                     color='grey')
        axis.hist(speed_ratio_dark,
                  bins=np.linspace(0, 2, bins),
                  color='grey',
                  linewidth=2,
                  weights=weights,
                  label='Dark',
                  alpha=0.5)

        axis.legend(loc='center left', bbox_to_anchor=(1, 0.5))
        axis.set_xlim((0, 2))
        axis.set_xticks((0, 1.00, 2.00))
        axis.set_xticklabels((0, 1.00, 2.00))
        axis.axvline(1, linestyle='--', color='k')
        axis1.set_yticklabels('')
        axis.set_xlabel('Ratio of speed in middle by speed at end')
        pf.set_axes_style(axis, numticks=3)
        pf.set_axes_style(axis1)
        axis.set_ylabel('Lap bins')
Пример #30
0
 def plotcrossvalidationresult(axis, cv_dataframe, trackbins, numsplits):
     # Difference
     meandiff = np.mean(cv_dataframe['Y_diff'].tolist(), 1) * trackbins
     sem = scipy.stats.sem(cv_dataframe['Y_diff'].tolist(), 1) * trackbins
     error1, error2 = meandiff - sem, meandiff + sem
     axis.plot(np.arange(numsplits), meandiff)
     axis.fill_between(np.arange(numsplits), error1, error2, alpha=0.5)
     axis.set_xlabel('CrossValidation #')
     axis.set_ylabel('Difference (cm)')
     axis.set_title(f'Difference in predicted position %.2f +/- %2f' %
                    (np.mean(cv_dataframe['Y_diff'].tolist()) * trackbins,
                     np.std(cv_dataframe['Y_diff'].tolist()) * trackbins))
     axis.set_ylim((0, np.max(error2)))
     pf.set_axes_style(axis, numticks=4)