def proc_subject(filelist): """Given an infile of raw pupil data, saves out: 1. Session level data with dilation data summarized for each trial 2. Dataframe of average peristumulus timecourse for each condition 3. Plot of average peristumulus timecourse for each condition 4. Percent of samples with blinks """ tpre = 0.5 tpost = 2.5 samp_rate = 30. for fname in filelist: print('Processing {}'.format(fname)) if (os.path.splitext(fname)[-1] == ".gazedata") | (os.path.splitext(fname)[-1] == ".csv"): df = pd.read_csv(fname, sep="\t") elif os.path.splitext(fname)[-1] == ".xlsx": df = pd.read_excel(fname, parse_dates=False) else: raise IOError('Could not open {}'.format(fname)) subid = pupil_utils.get_subid(df['Subject'], fname) timepoint = pupil_utils.get_timepoint(df['Session'], fname) oddball_sess = get_oddball_session(fname) df = pupil_utils.deblink(df) dfresamp = pupil_utils.resamp_filt_data(df) dfresamp['Condition'] = np.where(dfresamp.CRESP == 5, 'Standard', 'Target') pupil_utils.plot_qc(dfresamp, fname) sessdf = get_sessdf(dfresamp) sessdf['BlinkPct'] = get_blink_pct(dfresamp, fname) dfresamp['zDiameterPupilLRFilt'] = pupil_utils.zscore( dfresamp['DiameterPupilLRFilt']) targdf, standdf = proc_all_trials(sessdf, dfresamp['zDiameterPupilLRFilt'], tpre, tpost, samp_rate) targdf_long = reshape_df(targdf) standdf_long = reshape_df(standdf) glm_results = ts_glm( dfresamp.zDiameterPupilLRFilt, sessdf.loc[sessdf.Condition == 'Target', 'Timestamp'], sessdf.loc[sessdf.Condition == 'Standard', 'Timestamp'], dfresamp.BlinksLR) # Set subject ID and session as (as type string) glm_results['Subject'] = subid glm_results['Session'] = timepoint glm_results['OddballSession'] = oddball_sess save_glm_results(glm_results, fname) allconddf = standdf_long.append(targdf_long).reset_index(drop=True) # Set subject ID and session as (as type string) allconddf['Subject'] = subid allconddf['Session'] = timepoint allconddf['OddballSession'] = oddball_sess plot_pstc(allconddf, fname) save_pstc(allconddf, fname) # Set subject ID and session as (as type string) sessdf['Subject'] = subid sessdf['Session'] = timepoint sessdf['OddballSession'] = oddball_sess sessout = pupil_utils.get_outfile(fname, '_SessionData.csv') sessdf.to_csv(sessout, index=False)
def plot_pstc(allconddf, infile, trial_start=0.): """Plot peri-stimulus timecourse across all trials and split by condition""" outfile = pupil_utils.get_outfile(infile, '_PSTCplot.png') p = sns.lineplot(data=allconddf, x="Timepoint", y="Dilation", hue="Condition", legend="brief") plt.axvline(trial_start, color='k', linestyle='--') p.figure.savefig(outfile) plt.close()
def save_total_blink_pct(dfresamp, infile): """Calculate and save out percent of trials with blinks in session""" outfile = pupil_utils.get_outfile(infile, '_BlinkPct.json') blink_dict = {} blink_dict['TotalBlinkPct'] = float(dfresamp.BlinksLR.mean()) blink_dict['Subject'] = pupil_utils.get_subid(dfresamp['Subject'], infile) blink_dict['Session'] = pupil_utils.get_timepoint(dfresamp['Session'], infile) blink_dict['OddballSession'] = get_oddball_session(infile) blink_json = json.dumps(blink_dict) with open(outfile, 'w') as f: f.write(blink_json)
def plot_event(signal_filt, trg_ts, std_ts, kernel, infile): """Plot peri-stimulus timecourse of each event type as well as the canonical pupil response function""" outfile = pupil_utils.get_outfile(infile, '_PSTCplot.png') plt.ioff() all_events = std_ts.data + (trg_ts.data * 2) all_events_ts = ts.TimeSeries(all_events, sampling_rate=30., time_unit='s') all_era = nta.EventRelatedAnalyzer(signal_filt, all_events_ts, len_et=75, correct_baseline=True) fig, ax = plt.subplots() viz.plot_tseries(all_era.eta, yerror=all_era.ets, fig=fig) ax.plot((all_era.eta.time * (10**-12)), kernel) ax.legend(['Standard', 'Target', 'Pupil IRF']) fig.savefig(outfile) plt.close(fig)
def save_pstc(allconddf, infile, trial_start=0.): """Save out peristimulus timecourse plots""" outfile = pupil_utils.get_outfile(infile, '_PSTCdata.csv') pstcdf = allconddf.groupby(['Subject', 'Condition', 'Timepoint']).mean().reset_index() pstcdf.to_csv(outfile, index=False)
def save_glm_results(glm_results, infile): """Calculate and save out percent of trials with blinks in session""" glm_json = json.dumps(glm_results) outfile = pupil_utils.get_outfile(infile, '_GLMresults.json') with open(outfile, 'w') as f: f.write(glm_json)