In [70]:
from glob import glob
import seaborn as sns
import nibabel as nib
import numpy as np
from nilearn import image, plotting
import json
import os.path as osp
import pandas as pd
from matplotlib import pyplot as plt
import statsmodels
import statsmodels.formula.api as smf
%matplotlib inline
sns.set(rc={'axes.facecolor':'lightgray', 'figure.facecolor':'white'})
In [258]:
mds = glob('/home/grg/tbss_subjects/*L2.nii.gz')
skel = np.array(nib.load('/home/grg/tbss_fmodel/stats/mean_FA_skeleton.nii.gz').dataobj)
ages = json.load(open('/home/grg/spm/data/age.json'))
apoe = json.load(open('/home/grg/spm/data/apoe_groups.json'))
gender = json.load(open('/home/grg/spm/data/genders.json'))
educ = json.load(open('/home/grg/spm/data/educyears.json'))
data = []
for each in mds:
md = np.array(nib.load(each).dataobj)
mean = md[skel>0.01].mean()
subject = osp.basename(each).split('_')[1]
print subject
data.append((subject, mean, ages[subject[:5]]/365.25, apoe[subject], educ[subject], gender[subject]))
In [274]:
df
Out[274]:
In [283]:
def load_md(fp):
df = pd.read_excel(fp)
subjects = df['subject'].tolist()
df = df.set_index('subject')
#d = {'apoe23':0, 'apoe24':1, 'apoe33':0, 'apoe34':1, 'apoe44':2}
d = {0:0, 1:1, 2:0, 3:1, 4:2}
ht = {0:1, 1:2, 2:1, 3:2, 4:1}
groups = [d[e] for e in df['apo'].values]
df['group'] = pd.Series(groups, index=subjects)
return df
In [313]:
df_dartel = load_md('/tmp/mean_MD_dartel.xls').dropna()
df_tbss = load_md('/tmp/mean_MD.xls').dropna()
%run /home/grg/git/alfa/nilearn-helper.py
%run /home/grg/git/alfa/roicollect.py
#sns.boxplot(x='groups', y='mean_MD', data=df, showfliers=False)
#groups = get_groups(df, )
df_dartel['roi'] = correct(df_dartel)
df_tbss['roi'] = correct(df_tbss)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
set_figaxes(df_tbss, ylim=[df_dartel['roi'].min(), df_tbss['roi'].max()])
plot_region(df_dartel, '', ['All'], order=1, ax=ax)
plot_region(df_tbss, '', ['All'], order=1, ax=ax, c='toto')
#sns.boxplot(x='group', y='roi', data=df, showfliers=False)#, ax=ax)
#df = df.dropna()
#boxplot_region(df, ['HO','HT','NC'])
In [301]:
In [264]:
mds = sorted(glob('/home/grg/dartel_csf.5/rswr*_MD_t1space_wo_csf_nohdr.nii'))
#skel = sorted(glob('/home/grg/data/ALFA_DWI/*/T1/*_mabonlm_nobias_spm_c2.nii'))
ages = json.load(open('/home/grg/spm/data/age.json'))
apoe = json.load(open('/home/grg/spm/data/apoe_groups.json'))
educ = json.load(open('/home/grg/spm/data/educyears.json'))
gender = json.load(open('/home/grg/spm/data/genders.json'))
data = []
for each in mds:
#md = np.array(nib.load(each).dataobj)
subject = osp.basename(each).split('_')[0][4:]
print subject
skelfp = '/home/grg/data/templates/MNI_AAL/MNI_T1_seg_2.nii.gz'
# glob('/home/grg/data/ALFA_DWI/%s*/T1/%s*_mabonlm_nobias_spm_c2.nii'%(subject, subject))[0]
#image.resample_to_img(each, skelfp).to_filename('/tmp/teeesst.nii.gz')
md = np.array(nib.load(each).dataobj)
skel = np.array(nib.load(skelfp).dataobj)
mean = md[skel>0.01].mean()
print mean
data.append((subject, mean, ages[subject[:5]]/365.25, apoe[subject], educ[subject], gender[subject]))
df = pd.DataFrame(data, columns=['subject', 'roi', 'age', 'apo', 'educyears', 'gender'])
df.to_excel('/tmp/mean_MD_dartel.xls')
df.head()
Out[264]:
In [268]:
mod = smf.ols(formula='roi ~ 1 + age + I(age**2)', data=df_dartel)
res = mod.fit()
print 'dartel quadratic', 'aic=', res.aic, 'bic=', res.bic
mod = smf.ols(formula='roi ~ 1 + age', data=df_dartel)
res = mod.fit()
print 'dartel linear', 'aic=', res.aic, 'bic=', res.bic
mod = smf.ols(formula='roi ~ 1 + age + I(age**2)', data=df_tbss)
res = mod.fit()
print 'tbss quadratic', 'aic=', res.aic, 'bic=', res.bic
mod = smf.ols(formula='roi ~ 1 + age', data=df_tbss)
res = mod.fit()
print 'tbss linear', 'aic=', res.aic, 'bic=', res.bic
Out[268]:
In [307]:
mod = smf.ols(formula='roi ~ 1 + I(age**2)', data=df_tbss)
res = mod.fit()
res.summary()
Out[307]:
In [ ]: