def post_lesion_silent_comparison2(): fig = plt.figure() for subjID in subjIDs: d = pd.HDFStore(os.path.join(studydir, 'Combined', '%s_silent.h5' % subjID)) df = d['df'] d.close() df = df[df.good] epochgp = df.groupby(['epoch', 'hemi']) df['FFTabs'] = np.abs(df.FFT) epochmean = epochgp.FFTabs.apply(np.mean) epocherr = epochgp.FFTabs.apply(pd_std) colors = dict(Pre = 'b', Post = 'r') axs = dict(l = 1, r = 2) for (k, v) in epochmean.iterkv(): ax = fig.add_subplot(1, 2, axs[k[1]]) ax.set_title('%s hemisphere' % k[1]) misc.errorfill(fft_freq, np.log10(v), epocherr[k], color = colors[k[0]], ax = ax) fig.suptitle('%s (%s ear lesion)' % (subjID, lesionear[subjID])) fig.savefig(os.path.join(studydir, 'Analysis', '%s_silent_pre_post.png' % subjID)) fig.suptitle('') fig.clf()
def plot_group_results(df): df = df[df.cued==1] # group by session to get session mean gap detection gp_sess = df.groupby(['sess', 'freq']) gapratio_sess = gp_sess.agg(dict(gapratio = np.mean, freq = get_first, gen = get_first, exp = get_first, animalID = get_first)) # combine sessions for each animal to give animal performance by condition gp_animal = gapratio_sess.groupby(['animalID', 'exp', 'freq']) gapratio_animal = gp_animal.agg(dict(gapratio = np.mean, freq = get_first, gen = get_first, exp = get_first)) gp_exp = gapratio_animal.groupby(['freq', 'exp']) gapratio_mean = gp_exp.gapratio.apply(np.mean).unstack() gapratio_sem = gp_exp.gapratio.apply(apply_sem).unstack() fig = plt.figure(); ax = fig.add_subplot(111); for (k, v), (kerr, verr) in zip(gapratio_mean.iteritems(), gapratio_sem.iteritems()): misc.errorfill(np.arange(freqs.size), v, verr, ax = ax, color = colors[k], marker = '.', ls = '-', label = k) ax.legend() maxy = ax.get_ylim()[1] ax.set_ylim([0, maxy]) ax.set_ylabel('Gap ratio') ax.set_xlabel('Frequency (kHz)') ax.set_xticks(range(freqs.size)) ax.set_xticklabels(freqs) return ax
def single_subject_compare_conditions(self): ''' For each subject, plot the condition means for each condition on the same plot for comparison. ''' fig, ax = plt.subplots(figsize = (10, 8)) for animalID in self.animalIDs: animaldf = self.df[self.df.animalID==animalID] condgp = animaldf.groupby(('condition', 'freq')) ufreqs = np.unique(animaldf.freq) x = np.arange(ufreqs.size) y = condgp.agg(dict(gapratio = np.mean)) yerr = condgp.agg(dict(gapratio = st.sem)) for ((i, y_), (i, yerr_)) in zip(y.unstack().iterrows(), yerr.unstack().iterrows()): # ax.errorbar(x, y_, yerr_, label = i, marker = 'o', color = colors[i], ls = lss[i]) misc.errorfill(x, y_, yerr_, label = i, ax = ax, marker = 'o', color = colors[i], ls = lss[i]) ax.set_xticks(x) ax.set_xticklabels(np.int32(ufreqs/1000)) self._format_axis(ax) ax.set_title('%s %s' % (self.studyID, os.path.split(self.studydir)[1])) ax.legend() figpath = os.path.join(self.figdir, 'single_subject_compareconditions_%s.%s' % (animalID, self.figformat)) fig.savefig(figpath) ax.cla(); plt.close(fig)
def single_subject_compare_conditions_by_day(self): ''' For each subject, plot the mean gap ratio for each condition binned by date. (same as single_subject_compare_conditions, but uses binned date as an additional condition) ''' fig, ax = plt.subplots(figsize = (10, 8)) df = self.df for animalID in self.animalIDs: animaldf = df[df.animalID==animalID] condgp = animaldf.groupby(('condition_postdate', 'freq')) ufreqs = np.unique(animaldf.freq) x = np.arange(ufreqs.size) y = condgp.agg(dict(gapratio = np.mean)) yerr = condgp.agg(dict(gapratio = st.sem)) for ((i, y_), (i, yerr_)) in zip(y.unstack().iterrows(), yerr.unstack().iterrows()): misc.errorfill(x, y_, yerr_, label = i, ax = ax, marker = 'o') ax.set_xticks(x) ax.set_xticklabels(np.int32(ufreqs/1000)) self._format_axis(ax) ax.set_title('%s %s' % (self.studyID, os.path.split(self.studydir)[1])) ax.legend() figpath = os.path.join(self.figdir, 'single_subject_compare_conditions_by_day_%s.%s' % (animalID, self.figformat)) fig.savefig(figpath) ax.cla(); plt.close(fig)
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 pairwise_compare_conditions_by_freq(self, control = 'prenihl', conditions = ['tnfa', 'vehicle']): df = self.df ucond = np.unique(df.condition) ncond = len(ucond) # make animal/condition/freq groups animalgp = df.groupby(('animalID', 'condition', 'freq')) # take means of gap performance for those groups animalmeans = animalgp.agg(dict(gapratio=np.mean)) # put the control and manipulation columns side-by-side condstack = animalmeans.unstack('condition') # add a column indicating which condition this animal is animalcondition = [] for key, value in condstack.iterrows(): if not pd.isnull(value['gapratio']['tnfa']): animalcondition.append('tnfa') else: animalcondition.append('vehicle') condstack['animalcondition'] = animalcondition # make a column that is the result after manipulation # (combines multiple columns for each manipulation) # subtract the control from the manipulation values (for each frequency) manip = condstack.gapratio.vehicle.copy() manip.update(condstack.gapratio.tnfa) condstack['manip'] = manip condstack['manipdiff'] = condstack.manip - condstack.gapratio[control] # add a frequency column (turns frequency index into column) condstack['freq'] = condstack.index.get_level_values('freq') # group changes in gap detection by condition and frequency condgp = condstack.groupby(('animalcondition', 'freq')) condmeans = condgp.manipdiff.apply(np.mean).unstack('animalcondition') condsems = condgp.manipdiff.apply(st.sem).unstack('animalcondition') # line plot (change in gap detection VS frequency) ufreqs = np.unique(condstack.freq) x = range(len(ufreqs)) fig, ax = plt.subplots() styles = dict(tnfa=dict(color='r', hatch='///'), vehicle=dict(color='b', hatch=None)) for (key, y), (_, yerr) in zip(condmeans.iteritems(), condsems.iteritems()): misc.errorfill(x, y, yerr, ax=ax, label=key, color=styles[key]['color']) ax.set_xticks(x) ax.set_xticklabels((ufreqs/1000.).astype(int)) ax.legend() fig.savefig(os.path.join(self.figdir, 'pairwise_compare_conditions_by_freq.%s' % self.figformat)) 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 make_contactsheets(): subjIDs = ['Red102', 'Green102', 'Blue102', 'Red101'] epochs = ['Pre', 'Post'] for subjID in subjIDs: for epoch in epochs: sesspaths = glob.glob(os.path.join(studydir, 'Sessions', epoch, '*')) for sesspath in sesspaths: # fpaths = for fpath in fpaths: absol, relat = os.path.split(fpath) fname, _ = os.path.splitext(relat) print fname fin = pd.HDFStore(fpath, 'r') df = fin['df'] fin.close() ntrials = 100 duration = 0.333 nattens = np.unique(df.atten).size gp = df.groupby(('atten', 'hemi', 'relhemi')) means = gp.lfp.apply(np.mean) errs = gp.lfp.apply(np.std) / float(np.sqrt(ntrials)) t = np.linspace(0, duration, df.lfp[0].size) fig = plt.figure() ax = [] for i, ((k, m), (k, er)) in enumerate(zip(means.iterkv(), errs.iterkv())): ax.append(fig.add_subplot(nattens, 4, i+1)) misc.errorfill(t[:-1], m[:-1], er[:-1], ax = ax[-1]) ax[-1].set_title(k) ax[-1].set_xlim([0, duration]) misc.sameyaxis(ax) [a.set_xticklabels('') for a in ax[:-1]] [a.set_yticklabels('') for a in ax[1:]] fig.tight_layout() fig.savefig(os.path.join(studydir, 'Sheets', '%s_sheet.png' % fname)) plt.close(fig)