def corr_vs_incorr_targets(data): ''' shows the locations of targets that were correctly found versus targets that were missed ''' target_corr = np.array([dat for dat in data if dat['target'] == 1]) target_incorr = np.array([dat for dat in data if dat['target'] == 0]) target_loc = np.array([dat for dat in data]) fig = plt.figure(); # correct trial target locations ax1 = fig.add_subplot(131); plot_heatmap(target_corr, epoch = 'target', ax = ax1, gridsize = 20) # incorrect trial target locations ax2 = fig.add_subplot(132); plot_heatmap(target_incorr, epoch = 'target', ax = ax2, gridsize = 20) # all trial target locations ax3 = fig.add_subplot(133); plot_heatmap(target_loc, epoch = 'target', ax = ax3, gridsize = 20) misc.sameyaxis([ax1, ax2, ax3]) misc.samexaxis([ax1, ax2, ax3])
def plot_trials(df): plt.close('all'); fig1 = plt.figure(); fig2 = plt.figure(); ax1 = []; ax2 = [] hemis = ['r', 'l'] for j, hemi in enumerate(hemis[0]): df_ = df[df.hemi==hemi] ntrials = df_.lfp.size nsamp = df_.lfp[df_.index[0]].size for k, i in enumerate(range(ntrials)[::40]): ax1.append(fig1.add_subplot(5, 5, k+1)) ix = df_.index[i] ax1[-1].hist(df_.lfp[ix], bins = 100) ax1[-1].set_xticklabels('') ax1[-1].set_yticklabels('') ax1[-1].set_title(ix) ax2.append(fig2.add_subplot(5, 5, k+1)) ix = df_.index[i] ax2[-1].plot(df_.lfp[ix]) ax2[-1].set_xticklabels('') ax2[-1].set_yticklabels('') misc.samexaxis(ax1) misc.sameyaxis(ax2)
def make_noise_rr_sheets(): fpaths = glob.glob(os.path.join(studydir, 'Sessions', 'Pre', 'both', '*.h5')) for fpath in fpaths: absol, relat = os.path.split(fpath) fname, _ = os.path.splitext(relat) f = pd.HDFStore(fpath, 'r') df = f['df'] f.close() df = df[df.rr>0] # subset only non-silent trials df = df[df.good] # use only good trials urrs = np.unique(df.rr) nrrs = urrs.size gp = df.groupby(('rr', 'hemi')) nsamp = df.lfp.values[0].size Fs = nsamp / trial_duration lfp_mean = gp.lfp.apply(np.mean) lfp_err = gp.lfp.apply(np.std) fft_mean = gp.fft.apply(np.mean) fft_err = gp.fft.apply(np.std) t = np.linspace(0, trial_duration, nsamp) f = np.linspace(0, Fs/2., df.fft.values[0].size) fig = plt.figure(figsize = (14, 8.8)) ax_lfp = []; ax_fft = [] for i, ((k, lfp_mean_), (k, lfp_err_), (k, fft_mean_), (k, fft_err_)) in enumerate(zip(lfp_mean.iterkv(), lfp_err.iterkv(),\ fft_mean.iterkv(), fft_err.iterkv())): rr, hemi = k if hemi=='l': j=1 else: j=2 ax_lfp.append(fig.add_subplot(nrrs, 4, ((i/2)*4)+j)) misc.errorfill(t, lfp_mean_, lfp_err_, ax = ax_lfp[-1]) ax_lfp[-1].set_title(k) ax_fft.append(fig.add_subplot(nrrs, 4, ((i/2)*4)+2+j)) misc.errorfill(f, np.abs(fft_mean_), np.abs(fft_err_), ax = ax_fft[-1]) misc.sameyaxis(ax_lfp); misc.sameyaxis(ax_fft) misc.samexaxis(ax_lfp, [0, stim_duration]); misc.samexaxis(ax_fft, [0, 100.]) [a.set_yticklabels('') for a in ax_lfp[1::2]] fig.savefig(os.path.join(studydir, 'Sheets', '%s_noise.png'%fname)) plt.close(fig)
def make_silent_sheets(epoch = 'Pre'): sesspaths = glob.glob(os.path.join(studydir, 'Sessions', epoch, '*')) for sesspath in sesspaths: fpaths = glob.glob(os.path.join(sesspath, 'fileconversion', '*.h5')) for fpath in fpaths: absol, relat = os.path.split(fpath) fname, _ = os.path.splitext(relat) f = pd.HDFStore(fpath, 'r') df = f['df'] f.close() df = df[df.rr==-1] df = df[df.good] gp = df.groupby('hemi') nsamp = df.lfp.values[0].size Fs = nsamp / trial_duration lfp_mean = gp.lfp.apply(np.mean) lfp_err = gp.lfp.apply(np.std) fft_mean = gp.fft.apply(np.mean) fft_err = gp.fft.apply(np.std) t = np.linspace(0, trial_duration, nsamp) f = np.linspace(0, Fs/2., df.fft.values[0].size) fig = plt.figure() ax_lfp = []; ax_fft = [] for i, ((k, lfp_mean_), (k, lfp_err_), (k, fft_mean_), (k, fft_err_)) in enumerate(zip(lfp_mean.iterkv(), lfp_err.iterkv(),\ fft_mean.iterkv(), fft_err.iterkv())): # lfp_err_ = lfp_err[k] ax_lfp.append(fig.add_subplot(2, 2, i+1)); misc.errorfill(t, lfp_mean_, lfp_err_, ax = ax_lfp[-1]) ax_lfp[-1].set_title(k) ax_fft.append(fig.add_subplot(2, 2, i+3)); misc.errorfill(f, np.log(fft_mean_), np.log(fft_err_), ax = ax_fft[-1]) misc.sameyaxis(ax_lfp); misc.sameyaxis(ax_fft) misc.samexaxis(ax_lfp, [0, stim_duration]); misc.samexaxis(ax_fft, [f.min(), f.max()]) figpath = os.path.join(studydir, 'Sheets', '%s_silent.png' % fname) fig.savefig(figpath) plt.close(fig)
def make_noise_singleburst_sheets(): fpaths = glob.glob(os.path.join(studydir, 'Sessions', 'Pre', 'both', '*.h5')) fig = plt.figure(figsize = (14, 8.8)) ax_lfp = [] for j, fpath in enumerate(fpaths): absol, relat = os.path.split(fpath) fname, _ = os.path.splitext(relat) f = pd.HDFStore(fpath, 'r') df = f['df'] f.close() df = df[np.logical_and(df.rr>0, df.rr<16, df.good)] # subset only non-silent trials gp = df.groupby('hemi') nsamp = df.lfp.values[0].size Fs = nsamp / trial_duration lfp_mean = gp.lfp.apply(np.mean) lfp_err = gp.lfp.apply(np.std) fft_mean = gp.fft.apply(np.mean) fft_err = gp.fft.apply(np.std) t = np.linspace(0, trial_duration, nsamp) f = np.linspace(0, Fs/2., df.fft.values[0].size) for i, ((k, lfp_mean_), (k, lfp_err_), (k, fft_mean_), (k, fft_err_)) in enumerate(zip(lfp_mean.iterkv(), lfp_err.iterkv(),\ fft_mean.iterkv(), fft_err.iterkv())): ax_lfp.append(fig.add_subplot(len(fpaths), 2, (j*2)+i+1)) misc.errorfill(t, lfp_mean_, lfp_err_, ax = ax_lfp[-1]) if i==0: ax_lfp[-1].set_title('%s / %s' % (fname, k)) else: ax_lfp[-1].set_title(k) misc.sameyaxis(ax_lfp, [-0.0004, 0.0004]) misc.samexaxis(ax_lfp, [0.05, 0.175]) [a.set_yticklabels('') for a in ax_lfp[1::2]] fig.savefig(os.path.join(studydir, 'Sheets', 'noise_singleburst.png')) plt.close(fig)
def plot_peak_gen_vs_exp(self, x, measure = 'ampl'): ''' ''' gens = ['wt', 'ko'] exps = ['nai', 'exp'] freqs = [8000., 16000.] plt_opts = {'wt' : {'color' : 'b', 'x_offset' : 0}, 'ko' : {'color' : 'r', 'x_offset' : 1}} fig = plt.figure(figsize = (14, 8)) ax = [] for i in range(10): ax.append(fig.add_subplot(2, 5, i+1)) ax[-1].set_title('Peak %i' % ((i%5)+1)) a = np.arange(5) for f, freq in enumerate(freqs): x2 = x[x['freq']==freq] attens = np.unique(x2['atten']) for atten in attens[:1]: x3 = x2[x2['atten']==atten] for gen in gens: ampl = np.empty((len(exps), 5)) ampl_err = np.empty((len(exps), 5)) for j, exp in enumerate(exps): x4 = x3[np.vstack((x3['gen']==gen, x3['exp']==exp)).all(0)] ampl[j, :] = (x4[measure]*10).mean(0) ampl_err[j, :] = (x4[measure]*10).std(0) / np.sqrt(x4.size) for i in range(ampl.shape[1]): ampl_ = ampl[:, i] ampl_err_ = ampl_err[:, i] ax[(f*5)+i].errorbar(np.arange(2), ampl_, yerr = ampl_err_, color = plt_opts[gen]['color']) misc.sameyaxis(ax) misc.samexaxis(ax, [-1, 2]) for i in range(10): ax[i].set_xticks([0, 1]) ax[i].set_xticklabels(exps) for i in range(1, 10): ax[i].set_yticklabels([]) plt.show()
def dep_by_indep_for_2_groups(dep_key, indep_key, group1_key, group2_key, data, v = False): ''' takes 2-D data and splits it up by two groups it plots group1 groups on separate axes and group2 groups as different colors on the same axis ''' group1 = np.unique(data[group1_key]); ngroup1 = len(group1) group2 = np.unique(data[group2_key]); ngroup2 = len(group2) leg = [] line = [] for i in range(ngroup1): leg.append([]) line.append([]) colors = 'bgmyrc' fig = plt.figure() fig.tight_layout() for i, (g1, g2) in enumerate(itertools.product(range(ngroup1), range(ngroup2))): if v: print group1[g1], group2[g2] data_ = data[np.vstack((data[group1_key]==group1[g1], data[group2_key]==group2[g2])).all(0)] ax = fig.add_subplot(1, ngroup1, g1+1) ax.set_title(group1[g1]) if data_.size > 0: line_, ax_ = bar_by_indep_2d(dep_key, indep_key, data_, ax = ax, color = colors[g2]) line[g1].append(line_) leg[g1].append(group2[g2]) for g1 in range(ngroup1): ax = fig.add_subplot(1, ngroup1, g1+1) ax.legend(line[g1], leg[g1]) misc.samexaxis(fig.get_axes()) misc.sameyaxis(fig.get_axes()) plt.show() return fig
def plot_signal_quality(): ''' Plot "signal quality" measure for each animal. This is simply the histogram of maximum trial amplitudes. Movement artifacts are usually large amplitude (>0.002) spikes in the signal. ''' fpaths = glob.glob(os.path.join(studydir, 'Sessions', 'Pre', 'both', '*.h5')) axs = [] fig = plt.figure() npaths = len(fpaths) nrows = np.ceil(np.sqrt(npaths)) for i, fpath in enumerate(fpaths): d = pd.HDFStore(fpath, 'r') df = d['df'] d.close() axs.append(fig.add_subplot(nrows, nrows, i+1)) axs[-1].hist(df.lfp.apply(np.max), bins = np.arange(0, 0.01, 0.0005)) axs[-1].set_title(os.path.split(fpath)[-1]) misc.samexaxis(axs) plt.show() fig.savefig(os.path.join(studydir, 'Analysis', 'signal_quality.png'))
def plot_threshold_gen_vs_exp(self, x): ''' ''' gens = ['wt', 'ko'] exps = ['nai', 'exp'] freqs = np.unique(x['freq'])[:-1] plt_opts = {'wt' : {'color' : 'b', 'x_offset' : 0}, 'ko' : {'color' : 'r', 'x_offset' : 1}} fig = plt.figure(figsize = (14, 4)) ax = [] for i, freq in enumerate(freqs): ax.append(fig.add_subplot(1, 4, i+1)) ax[-1].set_title('%u kHz' % (freq/1000)) for j, gen in enumerate(gens): Y = np.empty(2) Y_err = np.empty(2) for k, exp in enumerate(exps): x_ = x[np.vstack((x['gen']==gen, x['exp']==exp, x['freq']==freq)).all(0)] y = x_['thresh_calib'].mean() y_err = x_['thresh_calib'].std() / np.sqrt(x_.size) Y[k] = y Y_err[k] = y_err ax[-1].errorbar([1, 2], Y, yerr = Y_err, color = plt_opts[gen]['color']) misc.sameyaxis(ax) misc.samexaxis(ax, [0, 3]) for i in range(4): ax[i].set_xticks([1, 2]) ax[i].set_xticklabels(exps) for i in range(1, 4): ax[i].set_yticklabels([]) plt.show()
def bar_by_indep_by_group_3d(dep_key, indep_key, data, visible = True, ax = None, group_keys = 'gen', bins = None, iscategorical = True, show_all = False, use_bar = False, fig = None, **kwargs): ''' plots a comparison over many groups attemps to make multiple line plots containing 2 lines per plot for ease-of-viewing gen exp sess ''' ngroups = len(group_keys) # number of groups # bin data for each group if bins is None: bins = [None] * ngroups groups = [] # actual entries for each group groups_bins = [] ugroups = [] nbins_per_group = [] for bin, group_key in zip(bins, group_keys): groups.append(data[group_key]) if bin is None: groups_bins.append(groups[-1]) # binned labels for each data point ugroups.append(np.unique(groups[-1])) # all unique binned labels else: groups_bins.append(misc.bin(groups[-1], bin)) ugroups.append(bin[:-1]) nbins_per_group.append(ugroups[-1].size) # how many unique labels in each group ''' ix is for all possible group combinations, a boolean vector indicating membership of each data point in the data set l is passed to itertools.product to iterate through all combinations of group values ''' ix, l = build_index(groups_bins, ugroups, nbins_per_group) if fig is None: fig = plt.figure(figsize = (11, 14)); naxs1 = nbins_per_group[0] # exp / row naxs2 = nbins_per_group[1] # cf / column cm = plt.cm.gist_ncar clrs = [cm(i) for i in np.linspace(0, 0.9, nbins_per_group[-1])] nlines = np.zeros((nbins_per_group[0], nbins_per_group[1])) for i in itertools.product(*l): print i axisnum = i[0]*naxs2 + i[1] + 1 ax = fig.add_subplot(naxs1, naxs2, axisnum) if ix[i].sum()>0: nlines[i[0], i[1]] += 1 bar_by_indep_2d(dep_key, indep_key, data[ix[i]], ax = ax, color = clrs[i[-1]]) if i[1] == 0: ax.set_ylabel('%s\n%s' % (str(ugroups[0][i[0]]), dep_key), multialignment = 'center') if i[0] == 0: ax.set_title('%s = %s' % (str(group_keys[1]), str(ugroups[1][i[1]]))) else: ax.set_title('') axs = fig.get_axes() misc.sameyaxis(axs) misc.samexaxis(axs) done = False i = 0 while not done: i += 1 if nlines[i % nbins_per_group[0], int(np.floor(i/nbins_per_group[0]))] == nbins_per_group[2]: axs[i].legend(ugroups[2]) done = True plt.show() return fig