def compute_graph_lasso_covariance(subject_id,
                                   group,
                                   session='func1',
                                   preprocessing_folder='pipeline_1',
                                   plot=True,
                                   save=True,
                                   save_file=True,
                                   msdl=False):
    """Returns graph lasso covariance for a subject_id
    """
    # load timeseries
    if msdl:
        ts = load_dynacomp_msdl_timeseries(
            subject_id,
            session=session,
            preprocessing_folder=preprocessing_folder)
        roi_names, roi_coords = load_msdl_names_and_coords()
    else:
        ts = load_dynacomp_roi_timeseries(
            subject_id,
            session=session,
            preprocessing_folder=preprocessing_folder)
        # load rois
        roi_names, roi_coords = load_roi_names_and_coords(subject_id)

    # compute covariance
    gl = covariance.GraphLassoCV(verbose=2)
    gl.fit(ts)
    if plot:
        plot_connectivity_matrix(subject_id, group, gl.covariance_, roi_names,
                                 'gl_covariance', session,
                                 preprocessing_folder, save, msdl)
        plot_connectivity_matrix(subject_id, group, gl.precision_, roi_names,
                                 'gl_precision', session, preprocessing_folder,
                                 save, msdl)
        sparsity = (gl.precision_ == 0)
        plot_connectivity_matrix(subject_id, group, sparsity, roi_names,
                                 'gl_sparsity', session, preprocessing_folder,
                                 save, msdl)

        plot_connectivity_glassbrain(subject_id, group, gl.covariance_,
                                     roi_coords, 'gl_covariance', session,
                                     preprocessing_folder, save, msdl)

    if save_file:
        CONN_DIR = set_data_base_dir('Dynacomp/connectivity')
        sparsity = (gl.precision_ == 0)
        if not os.path.isdir(os.path.join(CONN_DIR, subject_id)):
            os.mkdir(os.path.join(CONN_DIR, subject_id))
        output_file = os.path.join(
            CONN_DIR, subject_id, 'gl_' + session + '_' + preprocessing_folder)
        if msdl:
            output_file += '_msdl'
        np.savez(output_file,
                 covariance=gl.covariance_,
                 precision=gl.precision_,
                 sparsity=sparsity,
                 roi_names=roi_names,
                 roi_coords=roi_coords)
    return gl, roi_names, roi_coords
def compute_graph_lasso_covariance(subject_id, group, session='func1',
                                   preprocessing_folder='pipeline_1',
                                   plot=True, save=True, save_file=True,
                                   msdl=False):
    """Returns graph lasso covariance for a subject_id
    """
    # load timeseries
    if msdl:
        ts = load_dynacomp_msdl_timeseries(subject_id, session=session,
                                          preprocessing_folder=preprocessing_folder)
        roi_names, roi_coords = load_msdl_names_and_coords()
    else:
        ts = load_dynacomp_roi_timeseries(subject_id, session=session,
                                          preprocessing_folder=preprocessing_folder)
        # load rois
        roi_names, roi_coords = load_roi_names_and_coords(subject_id)

    # compute covariance
    gl = covariance.GraphLassoCV(verbose=2)
    gl.fit(ts)
    if plot:
        plot_connectivity_matrix(subject_id, group, gl.covariance_,
                                 roi_names, 'gl_covariance', session,
                                 preprocessing_folder, save, msdl)
        plot_connectivity_matrix(subject_id, group, gl.precision_,
                                 roi_names, 'gl_precision', session,
                                 preprocessing_folder, save, msdl)
        sparsity = (gl.precision_ == 0)
        plot_connectivity_matrix(subject_id, group, sparsity,
                                 roi_names, 'gl_sparsity', session,
                                 preprocessing_folder, save, msdl)

        plot_connectivity_glassbrain(subject_id, group, gl.covariance_,
                                     roi_coords, 'gl_covariance', session,
                                     preprocessing_folder, save, msdl)

    if save_file:
        CONN_DIR = set_data_base_dir('Dynacomp/connectivity')
        sparsity = (gl.precision_ == 0)
        if not os.path.isdir(os.path.join(CONN_DIR, subject_id)):
            os.mkdir(os.path.join(CONN_DIR, subject_id))
        output_file = os.path.join(CONN_DIR, subject_id,
                                   'gl_' + session + '_' + preprocessing_folder)
        if msdl:
            output_file += '_msdl'
        np.savez(output_file, covariance=gl.covariance_,
                 precision=gl.precision_, sparsity=sparsity,
                 roi_names=roi_names, roi_coords=roi_coords)
    return gl, roi_names, roi_coords
def compute_pearson_connectivity(subject_id,
                                 group,
                                 session='func1',
                                 preprocessing_folder='pipeline_1',
                                 plot=True,
                                 save=True,
                                 save_file=True,
                                 msdl=False):
    """Returns Pearson correlation coefficient for a subject_id
    """
    # load timeseries
    if msdl:
        ts = load_dynacomp_msdl_timeseries(
            subject_id,
            session=session,
            preprocessing_folder=preprocessing_folder)
        roi_names, roi_coords = load_msdl_names_and_coords()
    else:
        ts = load_dynacomp_roi_timeseries(
            subject_id,
            session=session,
            preprocessing_folder=preprocessing_folder)
        # load rois
        roi_names, roi_coords = load_roi_names_and_coords(subject_id)

    # pearson correlation
    pc = np.corrcoef(ts.T)

    if plot:
        print session
        plot_connectivity_matrix(subject_id, group, pc, roi_names, 'pc',
                                 session, preprocessing_folder, save, msdl)
        plot_connectivity_glassbrain(subject_id, group, pc, roi_coords, 'pc',
                                     session, preprocessing_folder, save, msdl)
    if save_file:
        CONN_DIR = set_data_base_dir('Dynacomp/connectivity')
        if not os.path.isdir(os.path.join(CONN_DIR, subject_id)):
            os.mkdir(os.path.join(CONN_DIR, subject_id))
        output_file = os.path.join(
            CONN_DIR, subject_id, 'pc_' + session + '_' + preprocessing_folder)
        if msdl:
            output_file += '_msdl'
        np.savez(output_file,
                 correlation=pc,
                 roi_names=roi_names,
                 roi_coords=roi_coords)
    return pc, roi_names, roi_coords
def compute_pearson_connectivity(subject_id, group, session='func1',
                                 preprocessing_folder='pipeline_1',
                                 plot=True, save=True, save_file=True,
                                 msdl=False):
    """Returns Pearson correlation coefficient for a subject_id
    """    
    # load timeseries
    if msdl:
        ts = load_dynacomp_msdl_timeseries(subject_id, session=session,
                                          preprocessing_folder=preprocessing_folder)
        roi_names, roi_coords = load_msdl_names_and_coords()
    else:
        ts = load_dynacomp_roi_timeseries(subject_id, session=session,
                                          preprocessing_folder=preprocessing_folder)
        # load rois
        roi_names, roi_coords = load_roi_names_and_coords(subject_id)

    # pearson correlation
    pc = np.corrcoef(ts.T)

    if plot:
        print session
        plot_connectivity_matrix(subject_id, group, pc,
                                 roi_names, 'pc',
                                 session,
                                 preprocessing_folder, save, msdl)
        plot_connectivity_glassbrain(subject_id, group, pc,
                                     roi_coords, 'pc', session,
                                     preprocessing_folder, save, msdl)
    if save_file:
        CONN_DIR = set_data_base_dir('Dynacomp/connectivity')
        if not os.path.isdir(os.path.join(CONN_DIR, subject_id)):
            os.mkdir(os.path.join(CONN_DIR, subject_id))
        output_file = os.path.join(CONN_DIR, subject_id,
                                   'pc_' + session + '_' + preprocessing_folder)
        if msdl:
            output_file += '_msdl'
        np.savez(output_file, correlation=pc,
                 roi_names=roi_names, roi_coords=roi_coords)
    return pc, roi_names, roi_coords
def compute_group_sparse_covariance(dataset, session='func1',
                                    preprocessing_folder='pipeline_1',
                                    plot=True, save=True, save_file=True,
                                    msdl=False):
    """Returns Group sparse covariance for all subjects
    """

    ts = []
    # load timeseries
    if msdl:
        for subject_id in dataset.subjects:
            ts.append(load_dynacomp_msdl_timeseries(subject_id,\
                      session=session, preprocessing_folder=preprocessing_folder))
        roi_names, roi_coords = load_msdl_names_and_coords()
    else:
        for subject_id in dataset.subjects:
            ts.append(load_dynacomp_roi_timeseries(subject_id, session=session,\
                      preprocessing_folder=preprocessing_folder))
        # load rois
        roi_names, roi_coords = load_roi_names_and_coords(subject_id)

    gsc = GroupSparseCovarianceCV(verbose=2)
    gsc.fit(ts)

    if plot:
        for i in range(len(dataset.subjects)):
            if not msdl:
                # load rois
                roi_names,\
                roi_coords = load_roi_names_and_coords(dataset.subjects[i])

            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     gsc.covariances_[..., i],
                                     roi_names, 'gsc_covariance', session,
                                     preprocessing_folder, save, msdl)

            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     gsc.precisions_[..., i],
                                     roi_names, 'gsc_precision', session,
                                     preprocessing_folder, save, msdl)

            sparsity = (gsc.precisions_[..., i] == 0)
            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     sparsity,
                                     roi_names, 'gsc_sparsity', session,
                                     preprocessing_folder, save, msdl)

            plot_connectivity_glassbrain(dataset.subjects[i], dataset.group[i],
                                         gsc.covariances_[..., i],
                                         roi_coords, 'gsc_covariance', session,
                                         preprocessing_folder, save, msdl)

    for i in range(len(dataset.subjects)):
        if not msdl:
            # load rois
            roi_names,\
            roi_coords = load_roi_names_and_coords(dataset.subjects[i])
        sparsity = (gsc.precisions_[..., i] == 0)            
        CONN_DIR = set_data_base_dir('Dynacomp/connectivity')
        subject_id = dataset.subjects[i]
        if not os.path.isdir(os.path.join(CONN_DIR, subject_id)):
            os.mkdir(os.path.join(CONN_DIR, subject_id))
        output_file = os.path.join(CONN_DIR, subject_id,
                                   'gsc_' + session + '_' + preprocessing_folder)
        if msdl:
            output_file += '_msdl'
        np.savez(output_file, covariance=gsc.covariances_[..., i],
                 precision=gsc.precisions_[..., i], sparsity=sparsity,
                 roi_names=roi_names, roi_coords=roi_coords)

    return gsc, roi_names, roi_coords
Ejemplo n.º 6
0
dataset = load_dynacomp()
func = dataset.func1

n_components = 20
canica = CanICA(n_components=n_components,
                mask=dataset.mask,
                smoothing_fwhm=None,
                do_cca=True,
                threshold=3.,
                n_init=10,
                standardize=True,
                random_state=42,
                n_jobs=2,
                verbose=2)

CANICA_PATH = set_data_base_dir('Dynacomp/canica')
output_file = os.path.join(CANICA_PATH,
                           'canica_' + str(n_components) + '.nii.gz')

if not os.path.isfile(output_file):
    canica.fit(func)
    components_img = canica.masker_.inverse_transform(canica.components_)
    components_img.to_filename(output_file)
else:
    components_img = output_file

FIG_PATH = set_figure_base_dir('canica')

for i, cur_img in enumerate(iter_img(components_img)):
    output_file = os.path.join(FIG_PATH,
                               'canica_' + str(n_components) + '_IC%d' % i)
Ejemplo n.º 7
0
from loader import load_dynacomp, set_data_base_dir, set_figure_base_dir
from nilearn.decomposition import CanICA
from nilearn.plotting import plot_stat_map
from nilearn.image import iter_img


dataset = load_dynacomp()
func = dataset.func1

n_components = 20
canica = CanICA(n_components=n_components, mask=dataset.mask,
                smoothing_fwhm=None, do_cca=True, threshold=3.,
                n_init=10, standardize=True, random_state=42,
                n_jobs=2, verbose=2)

CANICA_PATH = set_data_base_dir('Dynacomp/canica')
output_file = os.path.join(CANICA_PATH,
                           'canica_' + str(n_components) + '.nii.gz')

if not os.path.isfile(output_file):
    canica.fit(func)
    components_img = canica.masker_.inverse_transform(canica.components_)
    components_img.to_filename(output_file)
else:
    components_img = output_file

FIG_PATH = set_figure_base_dir('canica')

for i, cur_img in enumerate(iter_img(components_img)):
    output_file = os.path.join(FIG_PATH,
                               'canica_'+ str(n_components) +'_IC%d' % i )
# -*- coding: utf-8 -*-
"""
Created on Wed Apr  1 14:37:52 2015

@author: mr243268
"""

import os
import numpy as np
from loader import load_dynacomp, set_data_base_dir,\
                    load_msdl_names_and_coords, load_dynacomp_fc
from nilearn.plotting import plot_connectome
import seaborn as sns
import matplotlib.pyplot as plt

STAT_DIR = set_data_base_dir('Dynacomp/stat')

def read_test(metric, group, session):
    """Returns test data
    """
    filename = '_'.join(['ttest_connectome', session, group, metric, 'msdl' ])
    path = os.path.join(STAT_DIR, filename + '.npy')
    return np.load(path)


def read_test2(metric, groups, session):
    """Returns test data
    """
    filename = '_'.join(['ttest2_connectome', session, groups[0], groups[1], metric, 'msdl' ])
    path = os.path.join(STAT_DIR, filename + '.npy')
    return np.load(path)
def compute_group_sparse_covariance(dataset,
                                    session='func1',
                                    preprocessing_folder='pipeline_1',
                                    plot=True,
                                    save=True,
                                    save_file=True,
                                    msdl=False):
    """Returns Group sparse covariance for all subjects
    """

    ts = []
    # load timeseries
    if msdl:
        for subject_id in dataset.subjects:
            ts.append(load_dynacomp_msdl_timeseries(subject_id,\
                      session=session, preprocessing_folder=preprocessing_folder))
        roi_names, roi_coords = load_msdl_names_and_coords()
    else:
        for subject_id in dataset.subjects:
            ts.append(load_dynacomp_roi_timeseries(subject_id, session=session,\
                      preprocessing_folder=preprocessing_folder))
        # load rois
        roi_names, roi_coords = load_roi_names_and_coords(subject_id)

    gsc = GroupSparseCovarianceCV(verbose=2)
    gsc.fit(ts)

    if plot:
        for i in range(len(dataset.subjects)):
            if not msdl:
                # load rois
                roi_names,\
                roi_coords = load_roi_names_and_coords(dataset.subjects[i])

            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     gsc.covariances_[..., i], roi_names,
                                     'gsc_covariance', session,
                                     preprocessing_folder, save, msdl)

            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     gsc.precisions_[..., i], roi_names,
                                     'gsc_precision', session,
                                     preprocessing_folder, save, msdl)

            sparsity = (gsc.precisions_[..., i] == 0)
            plot_connectivity_matrix(dataset.subjects[i], dataset.group[i],
                                     sparsity, roi_names, 'gsc_sparsity',
                                     session, preprocessing_folder, save, msdl)

            plot_connectivity_glassbrain(dataset.subjects[i], dataset.group[i],
                                         gsc.covariances_[..., i], roi_coords,
                                         'gsc_covariance', session,
                                         preprocessing_folder, save, msdl)

    for i in range(len(dataset.subjects)):
        if not msdl:
            # load rois
            roi_names,\
            roi_coords = load_roi_names_and_coords(dataset.subjects[i])
        sparsity = (gsc.precisions_[..., i] == 0)
        CONN_DIR = set_data_base_dir('Dynacomp/connectivity')
        subject_id = dataset.subjects[i]
        if not os.path.isdir(os.path.join(CONN_DIR, subject_id)):
            os.mkdir(os.path.join(CONN_DIR, subject_id))
        output_file = os.path.join(
            CONN_DIR, subject_id,
            'gsc_' + session + '_' + preprocessing_folder)
        if msdl:
            output_file += '_msdl'
        np.savez(output_file,
                 covariance=gsc.covariances_[..., i],
                 precision=gsc.precisions_[..., i],
                 sparsity=sparsity,
                 roi_names=roi_names,
                 roi_coords=roi_coords)

    return gsc, roi_names, roi_coords