from sklearn.manifold import MDS, Isomap
import matplotlib.pyplot as plt

msdl = False
dataset = load_dynacomp()
roi_names, roi_coords = load_roi_names_and_coords(dataset.subjects[0])
if msdl:
    roi_names, roi_coords = load_msdl_names_and_coords()

ind = np.tril_indices(len(roi_names), k=-1)

x = []
for subject_id in dataset.subjects:
    c = load_dynacomp_fc(subject_id=subject_id,
                         session='func2',
                         metric='gl',
                         msdl=msdl,
                         preprocessing_folder='pipeline_2')
    x.append(c[ind])
x = np.array(x)

# Label vector y
groups = ['avn', 'v', 'av']
y = np.zeros(len(dataset.subjects))
for i, group in enumerate(['v', 'av']):
    y[dataset.group_indices[group]] = i + 1

pca = PCA(n_components=2)
pcx = pca.fit_transform(x)

mds = MDS(n_jobs=2, random_state=42)
 y = np.zeros(len(dataset.subjects))
 yn = np.zeros(len(dataset.subjects))
 yv = np.ones(len(dataset.subjects))
 yv[dataset.group_indices['v']] = 0
 for i, group in enumerate(['v', 'av']):
     y[dataset.group_indices[group]] = i + 1
     yn[dataset.group_indices[group]] = 1
 
 # Do classification for each metric
 for metric in ['gl','gsc', 'pc']:
     
     # 3 groups classification
     X = []
     for i, subject_id in enumerate(dataset.subjects):
         X.append(load_dynacomp_fc(subject_id, session=session,
                                   metric=metric, msdl=msdl,
                                   preprocessing_folder=preprocessing_folder)[ind])
     X = np.array(X)
 #    plt.figure()
 #    classification_learning_curves(X, y, title='_'.join([metric,
 #                                                         session, msdl_str]))
     
     # pairwise classification
     for i in range(2):
         for j in range(i+1, 3):
             gr_i = dataset.group_indices[groups[i]]
             gr_j = dataset.group_indices[groups[j]]
             Xp = np.vstack((X[gr_i, :], X[gr_j, :]))
             yp = np.array([0] * len(gr_i) + [1] * len(gr_j))
             output = '_'.join([groups[i], groups[j], metric, session,
                                msdl_str, preprocessing_folder])
from nilearn.plotting import plot_roi, plot_stat_map, plot_img
from sklearn.decomposition import PCA
from sklearn.manifold import MDS, Isomap
import matplotlib.pyplot as plt

msdl = False
dataset = load_dynacomp()
roi_names, roi_coords  = load_roi_names_and_coords(dataset.subjects[0])
if msdl:
    roi_names, roi_coords  = load_msdl_names_and_coords()

ind = np.tril_indices(len(roi_names), k=-1)

x = []
for subject_id in dataset.subjects:
    c = load_dynacomp_fc(subject_id=subject_id, session='func2', metric='gl',
                         msdl=msdl, preprocessing_folder='pipeline_2')
    x.append(c[ind])
x = np.array(x)

# Label vector y
groups = ['avn', 'v', 'av']
y = np.zeros(len(dataset.subjects))
for i, group in enumerate(['v', 'av']):
    y[dataset.group_indices[group]] = i + 1
    

pca = PCA(n_components=2)
pcx = pca.fit_transform(x)

mds = MDS(n_jobs=2, random_state=42)
px = mds.fit_transform(x)
Exemplo n.º 4
0
##############################################################################
# Dynacomp rs-fMRI
##############################################################################
dyn_dataset = loader.load_dynacomp()
roi_imgs = loader.dict_to_list(loader.load_dynacomp_rois()[0])
roi_names, roi_coords = loader.load_roi_names_and_coords(
    dyn_dataset.subjects[0])
ind = np.tril_indices(len(roi_names), k=-1)

dyn_fc = []
for subject in dyn_dataset.subjects:
    dyn_fc.append(
        loader.load_dynacomp_fc(subject_id=subject,
                                session='func1',
                                metric='pc',
                                msdl=False,
                                preprocessing_folder='pipeline_1')[ind])
dyn_fc = np.asarray(dyn_fc)

##############################################################################
# NYU rs-fMRI
##############################################################################
nyu_func = fetch_nyu_rest()['func']
masker = NiftiMapsMasker(maps_img=roi_imgs,
                         low_pass=.1,
                         high_pass=.01,
                         t_r=2.,
                         smoothing_fwhm=6.,
                         detrend=True,
                         standardize=False,
for i in range(len(behav_data)):
    for key in ['Thresh', 'RT', 'HIT_RT', 'Perf', 'Conf_mean']:
        behav_data[i]['delta' + key] = behav_data[i]['post' + key] - \
                                       behav_data[i]['pre' + key]



# Roi names
roi_names = sorted(dataset.rois[0].keys())
if msdl:
    roi_names, roi_coords = load_msdl_names_and_coords()

# Take only the lower diagonal values
ind = np.tril_indices(len(roi_names), k=-1)

# Do regression for each metric and each behav score 
for metric in ['pc', 'gl', 'gsc']:
    for key in behav_data[0].keys():
        # Construct feature matrix and output measure
        X = []
        y = []
        for i, subject_id in enumerate(dataset.subjects):
            X.append(load_dynacomp_fc(subject_id, metric=metric, msdl=msdl)[ind])
            y.append(behav_data[i][key])
        X, y = np.array(X), np.array(y)
        
        behav_name = '_'.join([key, metric])
        if msdl:
            behav_name += '_msdl'
        plt.figure()
        regression_learning_curves(X, y, behav_name)
roi_names, roi_coords = load_msdl_names_and_coords()
stat_av = read_test('pc', 'av', 'avg')
stat_v = read_test('pc', 'v', 'avg')
stat2 = read_test2('pc', ['av', 'v'], 'avg')


i, j = np.unravel_index(stat2.argmax(), stat2.shape)
print 'av 1sample pval :', stat_av[i, j]
print 'v 1sample pval :', stat_v[i, j]
print roi_names[i], roi_names[j]
print i, j

m = np.eye(2)
m[1,0] = stat2[i, j]
m[0,1] = m[1,0] 
plot_connectome(m, [roi_coords[i], roi_coords[j]])


conn = []
behav = []
for i in range(len(dataset.subjects)):
    c = load_dynacomp_fc(dataset.subjects[i], session='func1', metric='pc', msdl=True)
    conn.append(c[i, j])
    b = behav_data[i]['postRT'] - behav_data[i]['preRT']
    behav.append(b)

sns.jointplot(np.array(conn), np.array(behav), kind='kde')
sns.axlabel('Connectivity', 'Behavior')

from nilearn.input_data import NiftiMapsMasker
from sklearn.covariance import GraphLassoCV


##############################################################################
# Dynacomp rs-fMRI
##############################################################################
dyn_dataset = loader.load_dynacomp()
roi_imgs = loader.dict_to_list(loader.load_dynacomp_rois()[0])
roi_names, roi_coords = loader.load_roi_names_and_coords(dyn_dataset.subjects[0])
ind = np.tril_indices(len(roi_names), k=-1)

dyn_fc = []
for subject in dyn_dataset.subjects:
    dyn_fc.append(loader.load_dynacomp_fc(subject_id=subject, session='func1',
                                          metric='pc', msdl=False,
                                          preprocessing_folder='pipeline_1')[ind])
dyn_fc = np.asarray(dyn_fc)


##############################################################################
# NYU rs-fMRI
##############################################################################
nyu_func = fetch_nyu_rest()['func']
masker = NiftiMapsMasker(maps_img=roi_imgs, 
                         low_pass=.1,
                         high_pass=.01,
                         t_r=2.,
                         smoothing_fwhm=6., detrend=True, standardize=False,
                         resampling_target='maps', memory_level=0,
                         verbose=5)