In [1]:
import pandas as pd
import moss
from scipy import stats
import scipy as sp
import seaborn as sns
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import os.path as op
import os
# for plotting
sns.set(style='ticks', context='poster', font_scale=1.3)
%matplotlib inline
# import rpy2.rinterface as ri
# ri.set_initoptions(('rpy2', '--quiet', '--vanilla', '--no-save'))
# ri.initr()
# # R for stats
# %load_ext rpy2.ipython
In [2]:
# %R require(lme4)
# %R require(lmerTest)
In [3]:
dirs = dict()
dirs['basedir'] = op.join(op.expanduser('~'), 'Experiments/localizer')
dirs['datadir'] = op.join(dirs['basedir'], 'data/')
dirs['analydir'] = op.join(dirs['basedir'], 'analysis')
dirs['subj_info_file'] = op.join(dirs['datadir'], 'subj_info.csv')
In [4]:
subj_info = pd.read_csv(dirs['subj_info_file'])
subj_info = subj_info[pd.isnull(subj_info.remove)]
subj_info
Out[4]:
In [5]:
dl = pd.DataFrame()
for subid in subj_info.subid:
print subid
# add study file
study_file = op.join(dirs['datadir'], subid, subid + '_behav_localizer.csv')
d = pd.read_csv(study_file)
d['subid'] = subid
dl = dl.append(d, ignore_index=True)
# Merge with subj_info
dl = dl.merge(subj_info, on='subid', how='outer')
In [6]:
dl.head()
Out[6]:
In [7]:
len(dl.subid.unique())
Out[7]:
In [8]:
dl.groupby(['subid', 'group']).mean().reset_index().groupby('group').count().subid
Out[8]:
In [9]:
dl.loc[dl.resp == 'NR', 'respRT'] = dl.loc[dl.resp == 'NR', 'ISIrespRT'] + .5 # adjust for stimTime
dl.loc[dl.resp == 'NR', 'acc'] = dl.loc[dl.resp == 'NR', 'ISIacc']
dl.loc[dl.resp == 'NR', 'resp'] = dl.loc[dl.resp == 'NR', 'ISIresp']
drop_cols = ['ISIrespRT', 'ISIresp', 'ISIacc', 'remove', 'index']
dl.drop(drop_cols, 1, inplace=True)
# Correct duration of image trials by subtracting ITI (1s fix)
dl['duration_adj'] = dl.duration
dl.ix[dl.cond != 'rest', 'duration_adj'] = dl.ix[dl.cond != 'rest', 'duration_adj'] - 1 # subtract ITI
In [10]:
dl.head()
Out[10]:
In [11]:
dl['onset_adj'] = dl.onset - 12
dl.head()
Out[11]:
In [12]:
durations = dl.onset - dl.onset.shift(1)
durations[durations > -5].hist(bins=20)
sns.distplot(durations[durations > -5])
Out[12]:
In [13]:
sns.distplot(dl[dl.duration_adj < 5].duration)
Out[13]:
In [ ]:
In [14]:
dl_switch = dl[dl.cond != 'rest']
dl_switch['switch'] = dl_switch.cond == dl_switch.cond.shift(1)
dl_switch.switch = dl_switch.switch.apply(lambda x: 0 if x == True else 1)
for increment in [1, 2, 3, 4, 5]:
dl_switch.switch = np.nansum([dl_switch.switch,
dl_switch.switch.apply(lambda x: 0 if x < increment else x+1).shift(1)], axis=0)
In [15]:
dl_resp = dl_switch[(dl_switch.resp != 'NR') & (dl_switch.switch > 0)]
means = dl_resp.groupby(['subid', 'switch', 'group', 'cond', 'subcond', 'acc']).mean().reset_index()
sns.factorplot(x='switch', hue='group', y='respRT', col='cond',
units='subid', ci=68, dodge=.1,palette=['dodgerblue', 'orange'],
data=means.query('acc == 1'))
Out[15]:
In [16]:
dl_resp = dl_switch[(dl_switch.resp != 'NR') & (dl_switch.switch > 0)]
means = dl_resp.groupby(['subid', 'switch', 'group', 'acc']).mean().reset_index()
means.switch = means.switch.astype(int)
g = sns.factorplot(x='switch', hue='group', y='respRT', aspect=1.5,
units='subid', ci=68, dodge=.1, palette=['dodgerblue', 'orange'],
data=means.query('acc == 1'))
g.set_xlabels('trial relative to switch')
g.set_ylabels('RT (s)')
plt.savefig('/Users/steph-backup/Dropbox/Stanford/Presentations/AP/behav_loc_RTs_switch_acc.png')
In [17]:
dl_resp = dl[(dl.resp != 'NR') & (dl.cond != 'rest')]
means = dl_resp.groupby(['subid', 'group', 'cond', 'subcond', 'acc']).mean().reset_index()
sns.factorplot(x='cond', hue='acc', y='respRT', col='group',
units='subid', ci=68, dodge=.1,
data=means)
Out[17]:
In [18]:
dl_resp = dl[(dl.resp != 'NR') & (dl.cond != 'rest')]
means = dl_resp.groupby(['subid', 'group', 'cond', 'subcond', 'acc']).mean().reset_index()
sns.factorplot(x='subcond', aspect=1.5,
order=['female', 'male', 'manmade', 'natural', 'indoor', 'outdoor'],
hue='acc', y='respRT', col='group',
units='subid', ci=68, dodge=.1,
data=means)
Out[18]:
In [19]:
dl_resp = dl[(dl.cond != 'rest')]
means = dl_resp.groupby(['subid', 'group', 'cond', 'subcond']).mean().reset_index()
sns.factorplot(x='cond', y='acc', hue='group',
units='subid', ci=68, dodge=.1, aspect=1.2,
data=means, palette=['dodgerblue', 'orange'])
sns.despine(trim=True)
plt.savefig('/Users/steph-backup/Dropbox/Stanford/Presentations/AP/behav_loc_acc.png')
In [20]:
dl_resp = dl[(dl.cond != 'rest')]
%R -i dl_resp
In [21]:
%%R
print(str(dl_resp))
contrasts(dl_resp$group) = c(1,-1); print(contrasts(dl_resp$group))
contrasts(dl_resp$cond) = cbind(placeVSother = c(1,1,-2), faceVSobj=c(1,-1,0)); print(contrasts(dl_resp$cond))
res = lmer(acc ~ group * cond + (1 | subid), data=dl_resp)
In [ ]:
In [22]:
dl_resp = dl[(dl.cond != 'rest')]
means = dl_resp.groupby(['subid', 'group', 'cond', 'subcond']).mean().reset_index()
sns.factorplot(x='subcond', aspect=1.5,
x_order=['female', 'male', 'manmade', 'natural', 'indoor', 'outdoor'],
y='acc', hue='group', palette=['dodgerblue', 'orange'],
units='subid', ci=68, dodge=.1,
data=means)
Out[22]:
In [ ]:
Each csv file must have a column for run
, condition
, onset
, duration
, and value
In [23]:
output_filename = 'localizer_cond.csv'
output_dir = '/Volumes/group/awagner/sgagnon/AP/data'
for subid in subj_info.subid:
print subid
dsub = dl[(dl.subid == subid) & (dl.cond != 'rest')]
ddesign = pd.DataFrame({'run': dsub.run,
'condition': dsub.cond,
'onset': dsub.onset_adj,
'duration': 0.5,
'value': 1})
sub_output_dir = op.join(output_dir, subid.replace('loc', 'ap'), 'design')
if not os.path.exists(sub_output_dir):
os.makedirs(sub_output_dir)
ddesign.to_csv(op.join(sub_output_dir, output_filename), index=False)
In [24]:
output_filename = 'localizer_cond_dur=RT.csv'
output_dir = '/Volumes/group/awagner/sgagnon/AP/data'
for subid in subj_info.subid:
print subid
dsub = dl[(dl.subid == subid) & (dl.cond != 'rest')]
ddesign = pd.DataFrame({'run': dsub.run,
'condition': dsub.cond,
'onset': dsub.onset_adj,
'duration': dsub.respRT,
'value': 1})
sub_output_dir = op.join(output_dir, subid.replace('loc', 'ap'), 'design')
if not os.path.exists(sub_output_dir):
os.makedirs(sub_output_dir)
ddesign.to_csv(op.join(sub_output_dir, output_filename), index=False)
In [25]:
output_filename = 'localizer_cond_mvpa.csv'
output_dir = '/Volumes/group/awagner/sgagnon/AP/data'
for subid in subj_info.subid:
print subid
dsub = dl[(dl.subid == subid)] # & (dl.cond != 'rest')
ddesign = pd.DataFrame({'run': dsub.run + 6,
'condition': dsub.cond,
'onset': dsub.onset_adj,
'duration': 0.5,
'value': 1})
ddesign.to_csv(op.join(output_dir, subid.replace('loc', 'ap'),
'design', output_filename), index=False)
In [ ]:
In [ ]:
In [ ]: