from sklearn.feature_extraction.image import grid_to_graph
from sklearn.cluster import WardAgglomeration
from parietal.time_series import preprocessing

BASE_DIR = "/volatile/bernardng/data/imagen/"
TR = 2.2
subList = np.loadtxt(os.path.join(BASE_DIR, "subjectLists/subjectList.txt"), dtype='str')

# Concatenating PCA-ed voxel timecourses across subjects
for sub in subList:
    tc = io.loadmat(os.path.join(BASE_DIR, sub, "restfMRI/tc_rest_vox.mat"))
    tc = tc["tc_vox"]
    pca = PCA(n_components=10)    
    pca.fit(tc.T)
    if sub == subList[0]:
        tc_group = preprocessing.standardize(pca.transform(tc.T))
    else:
        tc_group = np.hstack((tc_group, preprocessing.standardize(pca.transform(tc.T))))
    print("Concatenating subject" + sub + "'s timecourses")
#io.savemat(os.path.join(BASE_DIR, "group/tc_rest_pca_vox.mat"), {"tc_group": tc_group})

# Perform parcellation on PCA-ed timecourses
brain_img = as_volume_img("/volatile/bernardng/templates/spm8/rgrey.nii")
brain = brain_img.get_data()
dim = np.shape(brain)
brain = brain > 0.2 # Generate brain mask
brain = mask_utils.largest_cc(brain)
mem = Memory(cachedir='.', verbose=1)
# Define connectivity based on brain mask
A = grid_to_graph(n_x=brain.shape[0], n_y=brain.shape[1], n_z=brain.shape[2], mask=brain)
# Create ward object
Beispiel #2
0
    print "Extracting high variance voxel confounds"
    # High variance voxels
    std = np.std(tc, axis=0)
    ind = std.argsort()
    tc_highvar = tc[:, ind[::-1][:10]]       
    confounds = np.hstack((confounds, tc_highvar))
    
    # Adding shifted versions of wm, csf, and high variance voxels confounds    
    for i in np.array([-1, 0, 1]):
        regressors = np.hstack((regressors, np.roll(confounds, i, axis=0)))       

    # Temporal detrending
    if data_type: # Resting state
        print "Removing confounds..."            
        # Standardize all regressors
        regressors = preprocessing.standardize(regressors.T).T    
        regressors = np.hstack((regressors, np.ones((n_tpts, 1))))
        beta, _, _, _ = linalg.lstsq(regressors, tc)
        tc -= np.dot(regressors, beta)      
        
        # Bandpass filter to remove DC offset and low frequency drifts
        f_cut = np.array([0.01, 0.1])
        samp_freq = 1 / TR 
        w_cut = f_cut * 2 / samp_freq
        b, a = signal.butter(5, w_cut, btype='bandpass', analog=0, output='ba')
        for temp in tc.T: # Modify tc in place
            temp[:] = signal.filtfilt(b, a, temp)
    else: # Task
        T = TR * n_tpts
        T_cut = 128 # Default for SPM is 128 sec
        t = np.arange(0, TR * n_tpts, TR)
Beispiel #3
0
    print "Extracting high variance voxel confounds"
    # High variance voxels
    std = np.std(tc, axis=0)
    ind = std.argsort()
    tc_highvar = tc[:, ind[::-1][:10]]
    confounds = np.hstack((confounds, tc_highvar))

    # Adding shifted versions of wm, csf, and high variance voxels confounds
    for i in np.array([-1, 0, 1]):
        regressors = np.hstack((regressors, np.roll(confounds, i, axis=0)))

    # Temporal detrending
    if data_type:  # Resting state
        print "Removing confounds..."
        # Standardize all regressors
        regressors = preprocessing.standardize(regressors.T).T
        regressors = np.hstack((regressors, np.ones((n_tpts, 1))))
        beta, _, _, _ = linalg.lstsq(regressors, tc)
        tc -= np.dot(regressors, beta)

        # Bandpass filter to remove DC offset and low frequency drifts
        f_cut = np.array([0.01, 0.1])
        samp_freq = 1 / TR
        w_cut = f_cut * 2 / samp_freq
        b, a = signal.butter(5, w_cut, btype='bandpass', analog=0, output='ba')
        for temp in tc.T:  # Modify tc in place
            temp[:] = signal.filtfilt(b, a, temp)
    else:  # Task
        T = TR * n_tpts
        T_cut = 128  # Default for SPM is 128 sec
        t = np.arange(0, TR * n_tpts, TR)
Beispiel #4
0
epi_ref_img = as_volume_img(epi_ref_file)
# Resample tissue mask to grid of epi
gm_img = gm_img.resampled_to_img(epi_ref_img)
# Extract tissue mask
gm = gm_img.get_data()
# Normalize the mask to [0,1]
gm -= gm.min()
gm /= gm.max()
# Threshold tissue mask
gm_mask = (gm > .5)
# Find largest connected component
gm_mask = mask_utils.largest_cc(gm_mask)

# Extract graymatter voxel timecourses
time_series_gm, header_gm = mask_utils.series_from_mask(epi_files, gm_mask)
time_series_gm = preprocessing.standardize(time_series_gm).T
n_tpts = time_series_gm.shape[0]
# Load motion regressors
motion_regressor = np.loadtxt(os.path.join(BASE_DIR, "fmri", "rp_fga070108233-0004-00002-000002-01.txt"))
motion_regressor = preprocessing.standardize(motion_regressor)
# Bandpass filter to remove DC offset and low frequency drifts
beta, _, _, _ = linalg.lstsq(motion_regressor,time_series_gm)
time_series_gm -= np.dot(motion_regressor,beta)
f_cut = np.array([0.01, 0.1])
tr = 2.4
samp_freq = 1 / tr 
w_cut = f_cut * 2 / samp_freq
b, a = signal.butter(5, w_cut, btype='bandpass', analog=0, output='ba')
for tc in time_series_gm.T:
    tc[:] = signal.filtfilt(b, a, tc) # Modify timer_series_gm in place
Beispiel #5
0
from sklearn.cluster import WardAgglomeration
from parietal.time_series import preprocessing

BASE_DIR = "/volatile/bernardng/data/imagen/"
TR = 2.2
subList = np.loadtxt(os.path.join(BASE_DIR, "subjectLists/subjectList.txt"),
                     dtype='str')

# Concatenating PCA-ed voxel timecourses across subjects
for sub in subList:
    tc = io.loadmat(os.path.join(BASE_DIR, sub, "restfMRI/tc_rest_vox.mat"))
    tc = tc["tc_vox"]
    pca = PCA(n_components=10)
    pca.fit(tc.T)
    if sub == subList[0]:
        tc_group = preprocessing.standardize(pca.transform(tc.T))
    else:
        tc_group = np.hstack(
            (tc_group, preprocessing.standardize(pca.transform(tc.T))))
    print("Concatenating subject" + sub + "'s timecourses")
#io.savemat(os.path.join(BASE_DIR, "group/tc_rest_pca_vox.mat"), {"tc_group": tc_group})

# Perform parcellation on PCA-ed timecourses
brain_img = as_volume_img("/volatile/bernardng/templates/spm8/rgrey.nii")
brain = brain_img.get_data()
dim = np.shape(brain)
brain = brain > 0.2  # Generate brain mask
brain = mask_utils.largest_cc(brain)
mem = Memory(cachedir='.', verbose=1)
# Define connectivity based on brain mask
A = grid_to_graph(n_x=brain.shape[0],