Exemplo n.º 1
0
def glm(image_ts, regressors):
    
    '''
    image_ts should be a matrix of the form (t x v) where t is the no. of timepoints
    and v is the no. of voxels
    
    regressors should be a matrix of the form (t x n) where t is the no. of timepoints
    and n is the number of regressors
    
    ------------------
    
    beta output is a serie of beta vector of the form (n x v).
    
    '''
    Y = image_ts
        
    if len(regressors.shape) == 1:
        X = np.expand_dims(regressors, axis=1)
    else:
        X = regressors
        

    glm_dist = GeneralLinearModel(X)
    glm_dist.transform(Y)
    
    beta = glm_dist.get_beta()
    
    return beta
Exemplo n.º 2
0
def glm(image_ts, regressors):
    
    '''
    image_ts should be a matrix of the form (t x v) where t is the no. of timepoints
    and v is the no. of voxels
    
    regressors should be a matrix of the form (t x n) where t is the no. of timepoints
    and n is the number of regressors
    
    ------------------
    
    beta output is a serie of beta vector of the form (n x v).
    
    '''
    Y = image_ts
        
    if len(regressors.shape) == 1:
        X = np.expand_dims(regressors, axis=1)
    else:
        X = regressors
        

    glm_dist = GeneralLinearModel(X)
    glm_dist.transform(Y)
    
    beta = glm_dist.get_beta()
    
    return beta
Exemplo n.º 3
0
def _fit_glm(X, Y, glm_model, model_tol=1e10):
    design_mask = ~np.all(X == 0, axis=0)
    design_mask = np.ones(X.shape[1], dtype=np.bool)
    sv = np.linalg.svd(X[:, design_mask])[1]
    sv = sv[0] / sv[-1]
    if sv < model_tol:
        glm = GeneralLinearModel(X[:, design_mask])
        glm.fit(Y, model=glm_model)
        return glm, design_mask
    else:
        return None, design_mask
Exemplo n.º 4
0
def resting_dmn(sub, ses, in_file=None,
                lh_ctx_file=None, rh_ctx_file=None, sc_file=None,
                schedule_file=None):
    from pipe_hbn_ssi import wb_to_tss
    import os, sys
    import numpy as np
    sys.path.append('/home/bpinsard/data/projects/CoRe')
    import core.mvpa.dataset as cds
    from nipy.modalities.fmri.glm import GeneralLinearModel
    import scipy.ndimage    
    from mvpa2.datasets import Dataset

    sched = np.loadtxt(
        schedule_file, 
        converters = {0:int,1:int,2:str,3:int,4:str,5:str},
        dtype=np.object,
        skiprows=1)
    idx = sched[:,1].tolist().index(ses)
    #scan_no = sched[idx,2].split('-').index('Rest')
    if in_file is None:
        scan_no = [i for i,n in enumerate(lh_ctx_file) if 'RESTING' in n]
    else:
        scan_no = [i for i,n in enumerate(in_file) if 'RESTING' in n]
    scan_no = scan_no[0]
    
    if in_file is None:
        inf = lh_ctx_file[scan_no]
        print(inf)
        ds = Dataset(wb_to_tss(lh_ctx_file[scan_no], rh_ctx_file[scan_no], sc_file[scan_no]))
    else:
        inf = in_file[scan_no]
        print(inf)
        ds = cds.ds_from_ts(in_file[scan_no])
    #cds.preproc_ds(ds, detrend=True)
    ds.samples -= scipy.ndimage.gaussian_filter1d(ds.samples,sigma=8,axis=0,truncate=2)
    
    seed_roi = 9
    cds.add_aparc_ba_fa(ds, sub, pproc_tpl=os.path.join(pipe_hbn_ssi.proc_dir,'moco_multiband','surface_32k','_sub_%d'))
    roi_mask = np.logical_or(ds.fa.aparc==seed_roi+11100, ds.fa.aparc==seed_roi+12100)
    
    mean_roi_ts = ds.samples[:,roi_mask].mean(1)
    mean_roi_ts -= mean_roi_ts.mean()
    
    mtx = np.asarray([mean_roi_ts, np.ones(ds.nsamples)]).T
        
    glm = GeneralLinearModel(mtx)
    glm.fit(ds.samples,model='ols')
    contrast = glm.contrast([1,0], contrast_type='t')
    
    out_file = os.path.abspath('sub%d_ses%d_connectivity_results.npz'%(sub,ses))
    np.savez_compressed(out_file, contrast=contrast, mean_roi_ts=mean_roi_ts)
    #return contrast
    return out_file, inf
Exemplo n.º 5
0
    def _do_nipy_glm_for_one_voxel(self, v_x, v_y, v_z):
        """
    Does a NIPY GLM analysis for one voxel with coordinate (v_x, v_y, v_z).

    Args:
      v_x (int): x coordinate of the voxel
      v_y (int): y coordinate of the voxel
      v_z (int): z coordinate of the voxel
    """
        voxel_time_course = np.atleast_2d(self._ni_data[v_x, v_y,
                                                        v_z, :]).T  # Y data.
        model = GeneralLinearModel(self._design_matrix)
        model.fit(voxel_time_course)
        z_val = model.contrast(self._contrast).z_score()
        return z_val
Exemplo n.º 6
0
    def _nipy_glm(x, y):
        """
        Parameters
        ----------
        x:
        y:

        Returns
        -------
        nipy.GeneralLinearModel

        """
        myglm = GeneralLinearModel(x)
        myglm.fit(y)
        return myglm
Exemplo n.º 7
0
def global_signal_regression(timeserie, regressor):
        
    #Get timeseries data
    Y = timeserie.data.T
        
        
    X = np.expand_dims(regressor, axis=1)
    glm_dist = GeneralLinearModel(X)
    glm_dist.transform(Y)
    beta_dist = glm_dist.get_beta()
        
    r_signal = np.dot(X, beta_dist)
        
    regressed_s = Y - r_signal
    
    return regressed_s
Exemplo n.º 8
0
def global_signal_regression(timeserie, regressor):
        
    #Get timeseries data
    Y = timeserie.data.T
        
        
    X = np.expand_dims(regressor, axis=1)
    glm_dist = GeneralLinearModel(X)
    glm_dist.transform(Y)
    beta_dist = glm_dist.get_beta()
        
    r_signal = np.dot(X, beta_dist)
        
    regressed_s = Y - r_signal
    
    return regressed_s
Exemplo n.º 9
0
            pylab.savefig(os.path.join(write_dir, 'design_matrix_%s.png') %\
                              session)
            # get the data
            if side == False:
                Y, _ = data_scaling(np.array([load(f).get_data()[mask_array]
                                              for f in fmri_data]))
                affine = load(fmri_data[0]).get_affine()
            else:
                Y, _ = data_scaling(np.array(
                        [load_texture(f) for f in fmri_data]))
                mask_array = np.var(Y, 0) > 0
                Y = Y[:, mask_array]

            # fit the glm
            print 'Fitting a GLM (this takes time)...'
            result = GeneralLinearModel(design_matrix.matrix)
            result.fit(Y, model='ar1', steps=100)

            for contrast_id, contrast_val in contrasts.iteritems():
                if (contrast_val[session] == 0).all():
                    continue

                if contrast_id in contrast_obj.keys():
                    contrast_obj[contrast_id] = contrast_obj[contrast_id] +\
                        result.contrast(contrast_val[session])
                else:
                    contrast_obj[contrast_id] =\
                        result.contrast(contrast_val[session])

        print 'Computing contrasts...'
        # In fact, this only writes the output images/textures
Exemplo n.º 10
0
X = [np.load(f)['X'] for f in design_files]

# Get multi-session fMRI data
print('Loading fmri data...')
Y = [load_image(f) for f in fmri_files]

# Get mask image
print('Loading mask...')
mask = load_image(mask_file)
mask_array, affine = mask.get_data() > 0, mask.get_affine()

# GLM fitting
print('Starting fit...')
glms = []
for x, y in zip(X, Y):
    glm = GeneralLinearModel(x)
    data, mean = data_scaling(y.get_data()[mask_array].T)
    glm.fit(data, 'ar1')
    glms.append(glm)

# Compute the required contrast
print('Computing test contrast image...')
nregressors = X[0].shape[1]
## should check that all design matrices have the same
c = np.zeros(nregressors)
c[0:4] = cvect
z_vals = (glms[0].contrast(c) + glms[1].contrast(c)).z_score()

# Show Zmap image
z_map = mask_array.astype(np.float)
z_map[mask_array] = z_vals
def snr_measures(premade_output = None):
	studies = {'Diffusion1':'dMRI_AP1.nii.gz','Diffusion2':'dMRI_PA1.nii.gz','Diffusion3':'dMRI_AP2.nii.gz','Diffusion4':'dMRI_PA2.nii.gz','Rest1':'fMRI_rest1_AP.nii.gz', 'Rest2':'fMRI_rest2_PA.nii.gz', 'Rest3':'fMRI_rest3_AP.nii.gz', 'Rest4':'fMRI_rest4_PA.nii.gz', 'Gambling1':'tfMRI_gambling1_AP.nii.gz', 'Gambling2':'tfMRI_gambling2_PA.nii.gz','FaceMatching1':'tfMRI_faceMatching1_AP.nii.gz', 'FaceMatching2':'tfMRI_faceMatching2_PA.nii.gz', 'Conflict1':'tfMRI_conflict1_AP.nii.gz', 'Conflict2':'tfMRI_conflict2_PA.nii.gz', 'Conflict3':'tfMRI_conflict3_AP.nii.gz', 'Conflict4':'tfMRI_conflict4_PA.nii.gz', 'T2':'T2.nii.gz','T1':'T1.nii.gz'}
	scans=['Diffusion1','Diffusion2','Diffusion3','Diffusion4','Rest1','Rest2','Rest3','Rest4','Gambling1','Gambling2','FaceMatching1','FaceMatching2','Conflict1','Conflict2','Conflict3','Conflict4','T1','T2']

	diffusion=[ [0,98], [98,196],[196,295],[295,394]]

	if premade_output == None:
		snr_output=csv.writer(open('/autofs/space/erebus_001/users/data/scores/new2/snr_avg_output_wComposites_rjj020419.csv','w+'))
		snr_output.writerow(['subject','avg_snr','questionnaire','questionnaire_score'])

	subjects, sex, scores = utils.loadBANDA140(outliers)
	#print(subjects)

	for key, score in scores.items():
		f, axarr = plt.subplots(1,1,figsize=(23,14))
		#f.tight_layout()
		f.subplots_adjust(left=.03, bottom=.06, right=.97, top=.95, wspace=.18, hspace=.30)

		axarr.set_title(str(key))
		#for calculating axis ranges
		ymin_value = 0
		ymax_value = 0
		xmin_value = 0
		xmax_value = 0
		x_values = []
		y_values = []

		#for s in subjects:
		for s_index,s in enumerate(subjects):
			print (s)
			if premade_output ==None:
				snr_accumulate = 0
				for scan in scans:
					roi="/space/erebus/1/users/data/preprocess/"+s+"/snr/WMROI5001_2"+studies[scan]
					image="/space/erebus/1/users/data/preprocess/"+s+"/"+studies[scan]
					mean2="/space/erebus/1/users/data/preprocess/"+s+"/snr/mean"+studies[scan]
					std2="/space/erebus/1/users/data/preprocess/"+s+"/snr/std"+studies[scan]
					snr2="/space/erebus/1/users/data/preprocess/"+s+"/snr/snr"+studies[scan]
					#ROI= nib.load("/space/erebus/1/users/data/preprocess/"+s+"/snr/WMROI4010_2"+studies[scan]).get_data()
					#image = nib.load("/space/erebus/1/users/data/preprocess/"+s+"/"+studies[scan]).get_data()
					snr=0
					if os.path.isfile(image) :
						#os.popen("mri_concat --i "+image+ " --mean --o "+mean2).read()
						#os.popen("mri_concat --i "+image+ " --std --o "+std2).read()
						if "T1" in scan or "T2" in scan:
							mean=os.popen("fslstats "+image+" -k "+roi+" -m").read()
							std=os.popen("fslstats "+image+" -k "+roi+" -s").read()

							snr = float(mean.split("\n")[0])/float(std.split("\n")[0])
							#print( snr)
						elif os.path.isfile(std2):
							#os.popen("fscalc "+mean2 +" div "+std2 +" --o " +snr2).read()
							res=os.popen("fslstats "+snr2+" -k "+roi+" -m").read()
							snr= float(res.split("\n")[0])
							snr_accumulate+= snr
				avg_snr = snr_accumulate/len(scans)
				x = float(score[s_index])
				y = avg_snr
				snr_output.writerow([s,y,key,x])

			else:
				snr_accumulate = 0
				for scan in scans:
					with open('/space/erebus/1/users/data/scores/snr_output_master.csv', 'r') as output:
						output_reader = csv.DictReader(output, delimiter = ",")
						for row in iter(output_reader):
							skip = 0
							if str(row['subject'])==s:
								print (row['subject'],s,key,scan)
								if str(row['questionnaire'])==key and str(row['scan'])==scan:
									print (key,scan)
									snr_accumulate += float(row['score'])
									if scans.index(scan) == 0: x = float(row['questionnaire_score'])
									break
								else:
									if skip > 0:skip+= 70
									else: skip+=69
									for i in range(skip):
										next(iter(output_reader))
				avg_snr = snr_accumulate/len(scans)
				y = avg_snr

			axarr.plot(x, y, "o",markersize=5, c='m')

			x_values.append(x)
			y_values.append(y)

			if s_index == 0: ymin_value = y
			ymin_value = min(ymin_value,y)
			ymax_value = max(ymax_value,y)

			if s_index == 0: xmin_value = x
			xmin_value = min(xmin_value,x)
			xmax_value = max(xmax_value,x)

		if key == 'Anhedonia':
			x_1 = [g for g in x_values if g>=3]
			x_2 = [g for g in x_values if g<3]
			#regression lines
			(sl,b) = polyfit(x_1,y_values[-len(x_1):], 1)
			yp = polyval([sl,b],x_1)
			axarr.plot(x_1,yp,'-',color='g')

			#regression lines
			(sl,b) = polyfit(x_2,y_values[:len(x_2)], 1)
			yp = polyval([sl,b],x_2)
			axarr.plot(x_2,yp,'-',color='b')

		else:
			#regression lines
			(sl,b) = polyfit(x_values,y_values, 1)
			yp = polyval([sl,b],x_values)
			axarr.plot(x_values,yp,'-',color='r')
		#GLM
		X=np.array([])
		if key != 'Anhedonia':
			X  = np.ones(len(x_values))
			X =np.stack((X, x_values), axis=-1)
			cval = np.hstack((0, -1))
		else:
			for x in x_values:
				if x>3:
					X=np.append(X,[[1,0]])
				else:
					X=np.append(X,[[0,1]])
			cval = np.hstack((1, -1))
		X= np.array(X).reshape(len(y_values),2)
		Y=np.array(y_values )

		model = GeneralLinearModel(X)
		model.fit(Y)
		z_vals = model.contrast(cval).p_value() # z-transformed statistics
		print(key, z_vals)

		axarr.set_ylabel("Average SNR")
		axarr.set_xlabel(str(key))

		axarr.set_xlim(xmin_value-(np.median(x_values)*.30),xmax_value+(np.median(x_values)*.30))
		axarr.set_ylim(ymin_value-(np.median(y_values)*.30),ymax_value+(np.median(y_values)*.30))

		f.savefig("/autofs/space/erebus_001/users/data/scores/new2/plots/snr_avg_"+str(key),dpi=199)
Exemplo n.º 12
0
# Get the FMRI data
#######################################

fmri_data = surrogate_4d_dataset(mask=mask, dmtx=X, seed=1)[0]

# if you want to save it as an image
# data_file = op.join(write_dir,'fmri_data.nii')
# save(fmri_data, data_file)

########################################
# Perform a GLM analysis
########################################

# GLM fit
Y = fmri_data.get_data()[mask_array]
glm = GeneralLinearModel(X)
glm.fit(Y.T)

# specifiy the contrast [1 -1 0 ..]
contrast = np.zeros(X.shape[1])
contrast[:2] = np.array([1, -1])

# compute the constrast image related to it
zvals = glm.contrast(contrast).z_score()
zmap = mask_array.astype(np.float)
zmap[mask_array] = zvals

########################################
# Create ROIs
########################################
Exemplo n.º 13
0
Y = [load(f) for f in fmri_files]

# Get mask image
print ("Loading mask...")
mask = load(mask_file)
mask_array = mask.get_data() > 0

# GLM fitting
print ("Starting fit...")
results = []
for x, y in zip(X, Y):
    # normalize the data to report effects in percent of the baseline
    data = y.get_data()[mask_array].T
    data, mean = data_scaling(data)
    # fit the glm
    model = GeneralLinearModel(x)
    model.fit(data, "ar1")
    results.append(model)

# make a mean volume for display
wmean = mask_array.astype(np.int16)
wmean[mask_array] = mean


def make_fiac_contrasts():
    """Specify some constrasts for the FIAC experiment"""
    con = {}
    # the design matrices of both runs comprise 13 columns
    # the first 5 columns of the design matrices correpond to the following
    # conditions: ["SSt-SSp", "SSt-DSp", "DSt-SSp", "DSt-DSp", "FirstSt"]
    p = 13
Exemplo n.º 14
0
            for i_delay, delay in enumerate(
                np.linspace(0, 30, n_models + 1)[:-1]):
                paradigm = make_paradigm(
                    delay, duration=duration, length=length, 
                    odd=(subject in ['sujet10', 'sujet12']))
                design_matrix = make_dmtx(
                    frametimes, paradigm, drift_model=drift_model, hfcut=hf_cut)
                
                # plot the design matrix
                ax = design_matrix.show()
                ax.set_position([.05, .25, .9, .65])
                ax.set_title('Design matrix')

                # fit the glm
                print 'Fitting a GLM (this takes time)...'
                result = GeneralLinearModel(design_matrix.matrix)
                result.fit(Y, model='ar1', steps=100)
                z_values = result.contrast([contrast_val]).z_score()
                best_d[z_values > best_z] = i_delay 
                best_z = np.maximum(z_values, best_z)

            write_array = mask_array.astype(np.float)
            write_array[mask_array] = best_z
            best_d = best_d * 2 * np.pi / n_models
            best_d[best_d > np.pi] -= 2 * np.pi
            if side == False:
                contrast_path = os.path.join(write_dir, '%s_z_map.nii' %
                                             session)
                save(Nifti1Image(write_array, affine), contrast_path)
                write_array[mask_array] = best_d
                contrast_path = os.path.join(write_dir, '%s_d_map.nii' %
Exemplo n.º 15
0
def _fit_glm(X, Y, glm_model):
    glm = GeneralLinearModel(X)
    glm.fit(Y, model=glm_model)
    return glm
Exemplo n.º 16
0
# Get the FMRI data
#######################################

fmri_data = surrogate_4d_dataset(shape=shape, n_scans=n_scans)[0]

# if you want to save it as an image
data_file = 'fmri_data.nii'
save(fmri_data, data_file)

########################################
# Perform a GLM analysis
########################################

# GLM fit
Y = fmri_data.get_data().reshape(np.prod(shape), n_scans)
glm = GeneralLinearModel(X)
glm.fit(Y.T)

# specify the contrast [1 -1 0 ..]
contrast = np.zeros(X.shape[1])
contrast[0] = 1
contrast[1] = - 1

# compute the constrast image related to it
zvals = glm.contrast(contrast).z_score()
contrast_image = Nifti1Image(np.reshape(zvals, shape), affine)

# if you want to save the contrast as an image
contrast_path = 'zmap.nii'
save(contrast_image, contrast_path)
Exemplo n.º 17
0
contrasts["video-audio"] = contrasts["video"] - contrasts["audio"]
contrasts["computation-sentences"] = contrasts["computation"] -  \
                                     contrasts["sentences"]
contrasts["reading-visual"] = contrasts["sentences"] * 2 - \
                              contrasts["damier_H"] - contrasts["damier_V"]
contrasts['effects_of_interest'] = np.eye(25)[:20:2]

########################################
# Perform a GLM analysis
########################################

print 'Fitting a GLM (this takes time)...'
fmri_image = load(data_path)
Y, _ = data_scaling(fmri_image.get_data()[mask_array])
X = design_matrix.matrix
results = GeneralLinearModel(X)
results.fit(Y.T, steps=100)
affine = fmri_image.get_affine()

#########################################
# Estimate the contrasts
#########################################

print 'Computing contrasts...'
for index, (contrast_id, contrast_val) in enumerate(contrasts.iteritems()):
    print '  Contrast % 2i out of %i: %s' % (index + 1, len(contrasts),
                                             contrast_id)
    contrast_path = path.join(write_dir, '%s_z_map.nii' % contrast_id)
    write_array = mask_array.astype(np.float)
    write_array[mask_array] = results.contrast(contrast_val).z_score()
    contrast_image = Nifti1Image(write_array, affine)
Exemplo n.º 18
0
# Get the FMRI data
#######################################
fmri_data = surrogate_4d_dataset(mask=mask, dmtx=X)[0]
Y = fmri_data.get_data()[mask_array]

# artificially added signal in ROIs to make the example more meaningful
activation = 30 * (X.T[1] + .5 * X.T[0])
for (position, radius) in zip(positions, radii):
    Y[((domain.coord - position) ** 2).sum(1) < radius ** 2 + 1] += activation

########################################
# Perform a GLM analysis
########################################

# GLM fit
glm = GeneralLinearModel(X)
glm.fit(Y.T)

# specifiy the contrast [1 -1 0 ..]
contrast = np.hstack((1, -1, np.zeros(X.shape[1] - 2)))

# compute the constrast image related to it
zvals = glm.contrast(contrast).z_score()

########################################
# ROI-based analysis
########################################

# exact the time courses with ROIs
signal_feature = [Y[my_roi.select_id(id, roi=False)] for id in my_roi.get_id()]
my_roi.set_feature('signal', signal_feature)
Exemplo n.º 19
0
 def _fit_model(self, ds, X, reg_names):
     glm = GeneralLinearModel(X)
     glm.fit(ds.samples, **self.glmfit_kwargs)
     out = Dataset(glm.get_beta(),
                   sa={self.get_space(): reg_names})
     return glm, out
Exemplo n.º 20
0
X = [np.load(f)['X'] for f in design_files]

# Get multi-session fMRI data
print('Loading fmri data...')
Y = [load_image(f) for f in fmri_files]

# Get mask image
print('Loading mask...')
mask = load_image(mask_file)
mask_array, affine = mask.get_data() > 0, mask.get_affine()

# GLM fitting
print('Starting fit...')
glms = []
for x, y in zip(X, Y):
    glm = GeneralLinearModel(x)
    data, mean = data_scaling(y.get_data()[mask_array].T)
    glm.fit(data, 'ar1')
    glms.append(glm)

# Compute the required contrast
print('Computing test contrast image...')
nregressors = X[0].shape[1]
## should check that all design matrices have the same
c = np.zeros(nregressors)
c[0:4] = cvect
z_vals = (glms[0].contrast(c) + glms[1].contrast(c)).z_score()

# Show Zmap image
z_map = mask_array.astype(np.float)
z_map[mask_array] = z_vals
Exemplo n.º 21
0
    # y = glob.glob(study_dir + '/sub???/model/model002/z_maps/*face_vs_house*.nii.gz*')
    if len(y) != 0:
        target.extend([(study_id, )] * len(y))
        niimgs.extend(y)
        studies.append(study_id)

lb = LabelBinarizer()
masker = NiftiMasker(mask='mask.nii.gz',
                     standardize=True,
                     memory=Memory(cache_dir))

X = lb.fit_transform(target)
X[:, -1] = 0

Y = masker.fit_transform(niimgs)

glm = GeneralLinearModel(X)
glm.fit(Y, model='ols')

affine = nb.load('mask.nii.gz').get_affine()

vs_baseline = []
for i, study_id in enumerate(studies[:-1]):
    con_val = np.zeros(len(studies))
    con_val[i] = 1
    contrast = glm.contrast(con_val, contrast_type='t')
    z_map = masker.inverse_transform(contrast.z_score())
    z_map.to_filename(result_dir + '/%s.nii.gz' % study_id)

    # vs_baseline.append()
Exemplo n.º 22
0
 def _fit_model(self, ds, X, reg_names):
     glm = GeneralLinearModel(X)
     glm.fit(ds.samples, **self.glmfit_kwargs)
     out = Dataset(glm.get_beta(), sa={self.get_space(): reg_names})
     return glm, out
Exemplo n.º 23
0
Y = [load(f) for f in fmri_files]

# Get mask image
print('Loading mask...')
mask = load(mask_file)
mask_array = mask.get_data() > 0

# GLM fitting
print('Starting fit...')
results = []
for x, y in zip(X, Y):
    # normalize the data to report effects in percent of the baseline
    data = y.get_data()[mask_array].T
    data, mean = data_scaling(data)
    # fit the glm 
    model = GeneralLinearModel(x)
    model.fit(data, 'ar1')
    results.append(model)

# make a mean volume for display
wmean = mask_array.astype(np.int16)
wmean[mask_array] = mean


def make_fiac_contrasts():
    """Specify some constrasts for the FIAC experiment"""
    con = {}
    # the design matrices of both runs comprise 13 columns
    # the first 5 columns of the design matrices correpond to the following
    # conditions: ["SSt-SSp", "SSt-DSp", "DSt-SSp", "DSt-DSp", "FirstSt"]
    p = 13
Exemplo n.º 24
0
from nipy.modalities.fmri.glm import GeneralLinearModel

dimt = 100
dimx = 10
dimy = 11
dimz = 12

# axis defines the "time direction"
y = np.random.randn(dimt, dimx * dimy * dimz)
axis = 0

X = np.array([np.ones(dimt), range(dimt)])
X = X.T ## the design matrix X must have dimt lines

mod = GeneralLinearModel(X)
mod.fit(y)

# Define a t contrast
tcon = mod.contrast([1, 0])

# Compute the t-stat
t = tcon.stat()
## t = tcon.stat(baseline=1) to test effects > 1

# Compute the p-value
p = tcon.p_value()

# Compute the z-score
z = tcon.z_score()
Exemplo n.º 25
0
# Get the FMRI data
#######################################

fmri_data = surrogate_4d_dataset(shape=shape, n_scans=n_scans)[0]

# if you want to save it as an image
data_file = 'fmri_data.nii'
save(fmri_data, data_file)

########################################
# Perform a GLM analysis
########################################

# GLM fit
Y = fmri_data.get_data().reshape(np.prod(shape), n_scans)
glm = GeneralLinearModel(X)
glm.fit(Y.T)

# specify the contrast [1 -1 0 ..]
contrast = np.zeros(X.shape[1])
contrast[0] = 1
contrast[1] = -1

# compute the constrast image related to it
zvals = glm.contrast(contrast).z_score()
contrast_image = Nifti1Image(np.reshape(zvals, shape), affine)

# if you want to save the contrast as an image
contrast_path = 'zmap.nii'
save(contrast_image, contrast_path)
Exemplo n.º 26
0
contrasts["right-left"] = contrasts["right"] - contrasts["left"]
contrasts["audio-video"] = contrasts["audio"] - contrasts["video"]
contrasts["video-audio"] = contrasts["video"] - contrasts["audio"]
contrasts["computation-sentences"] = contrasts["computation"] - contrasts["sentences"]
contrasts["reading-visual"] = contrasts["sentences"] * 2 - contrasts["damier_H"] - contrasts["damier_V"]
contrasts["effects_of_interest"] = np.eye(25)[::2]

########################################
# Perform a GLM analysis
########################################

print "Fitting a GLM (this takes time)..."
fmri_image = load(data_path)
Y, _ = data_scaling(fmri_image.get_data()[mask_array])
X = design_matrix.matrix
results = GeneralLinearModel(X)
results.fit(Y.T, steps=100)
affine = fmri_image.get_affine()

#########################################
# Estimate the contrasts
#########################################

print "Computing contrasts..."
for index, (contrast_id, contrast_val) in enumerate(contrasts.iteritems()):
    print "  Contrast % 2i out of %i: %s" % (index + 1, len(contrasts), contrast_id)
    contrast_path = op.join(write_dir, "%s_z_map.nii" % contrast_id)
    write_array = mask_array.astype(np.float)
    write_array[mask_array] = results.contrast(contrast_val).z_score()
    contrast_image = Nifti1Image(write_array, affine)
    save(contrast_image, contrast_path)
Exemplo n.º 27
0
            mp.savefig(fig1_file)

        #-----------------------------------------------------------------
        # Mean-scale, de-mean and multiply data by 100
        #-----------------------------------------------------------------
        mask = np.sum(img.get_data(), axis=-1) > 0
        data, mean = data_scaling(img.get_data()[mask].T)
        if np.size(data):
            mean.shape = mask.shape

            #-----------------------------------------------------------------
            # Apply a general linear model to all pixels
            #-----------------------------------------------------------------
            print('   Apply general linear model...')
            model = "ar1"
            glm = GeneralLinearModel(design_matrix)
            glm.fit(data, model=model)

            #-----------------------------------------------------------------
            # Create a contrast image
            #
            # Contrast condition 1 vs. condition 2, holding condition 3 constant
            # (sleep vs. awake holding concentration of odorant constant)
            #-----------------------------------------------------------------
            print('  Make contrast image...')

            # Specify the contrast [1 -1 0 ..]
            contrast = np.zeros(design_matrix.shape[1])
            if ntest < 5:
                contrast[0] = 1
            else:
Exemplo n.º 28
0
Y = [load(f) for f in fmri_files]

# Get mask image
print('Loading mask...')
mask = load(mask_file)
mask_array = mask.get_data() > 0

# GLM fitting
print('Starting fit...')
results = []
for x, y in zip(X, Y):
    # normalize the data to report effects in percent of the baseline
    data = y.get_data()[mask_array].T
    data, mean = data_scaling(data)
    # fit the glm
    model = GeneralLinearModel(x)
    model.fit(data, 'ar1')
    results.append(model)

# make a mean volume for display
wmean = mask_array.astype(np.int16)
wmean[mask_array] = mean


def make_fiac_contrasts():
    """Specify some contrasts for the FIAC experiment"""
    con = {}
    # the design matrices of both runs comprise 13 columns
    # the first 5 columns of the design matrices correspond to the following
    # conditions: ["SSt-SSp", "SSt-DSp", "DSt-SSp", "DSt-DSp", "FirstSt"]
    p = 13
Exemplo n.º 29
0
def _fit_glm(X, Y, glm_model):
    glm = GeneralLinearModel(X)
    glm.fit(Y, model=glm_model)
    return glm
Exemplo n.º 30
0
def KK_visit_Vermeer_GLM(stats_path):

	stimulus_txt = np.loadtxt('/Users/davidzipser/Google_Drive/Data/stimuli/2015/6/Vermeer_1024x768/natural_stimuli_10Hz_Fri_Jun_19_21-56-59_2015.medium_letters/stimulus.txt')
	n_Secs = len(stimulus_txt) # DANGER
	n_TRs_all = 300
	n_images = 24

	stimulus_REGRESSORS = np.zeros((n_Secs, n_images))
	for s in range(n_Secs):
	    if stimulus_txt[s] > 0:
	        stimulus_REGRESSORS[s,stimulus_txt[s]-1] = 1

	hrf = KK_getcanonicalhrf()
	hrf_1Hz = hrf[::5]
	#PP[FF]=3,3
	#plt.figure('hrf_1Hz')
	#plt.plot(hrf_1Hz,'.-')
	#mi(stimulus_REGRESSORS,'stimulus_REGRESSORS')

	HRF_REG = np.zeros((n_TRs_all,n_images))
	stimulus_times = np.arange(0,n_Secs)
	TR_times = 0.9 * np.arange(0,n_TRs_all)

	for i in range(n_images):
	    r = stimulus_REGRESSORS[:,i]
	    hp = np.convolve(r, hrf_1Hz)[:(np.shape(r)[0])]
	    hp_interp = np.interp(TR_times,stimulus_times,hp)
	    HRF_REG[:,i]=hp_interp
	#PP[FF] = 5,8
	#mi(HRF_REG,'HRF_REG')

	nii = nib.load(opj(stats_path,'all_average.nii.gz'))
	data = nii.get_data()
	header = nii.get_header()
	affine = nii.get_affine()
	
	Voxels,Vox_xyzs = vol_to_voxels(data)

	# - http://nipy.org/nipy/api/generated/nipy.modalities.fmri.glm.html

	from nipy.modalities.fmri.glm import GeneralLinearModel

	n_TRs = 294
	n_voxels = np.shape(Voxels)[1]
	n_images = 24

	DATA = Voxels

	REGRESSORS = HRF_REG.copy()[6:] # this deals with DELETE volumes

	model = GeneralLinearModel(REGRESSORS)
	model.fit(DATA)

	#mi(DATA,1,[1,1,1],'DATA ' + str(np.shape(DATA)))
	#mi(REGRESSORS,2,[1,1,1],'REGRESSORS ' + str(np.shape(REGRESSORS)))

	betas = model.get_beta()
	#mi(betas,3,[1,1,1],'betas ' + str(np.shape(betas)))

	mse = model.get_mse()
	#plt.figure('mse ' + str(np.shape(mse)))
	#plt.plot(mse)

	vol_betas = vox_list_to_vol(betas,Vox_xyzs,data[:,:,:,0].copy())

	betas_nii = nib.Nifti1Image(vol_betas, affine, header)
	nib.save(betas_nii, opj(stats_path,'betas.nipy_GLM.nii.gz'))
Exemplo n.º 31
0
                      add_regs=motion,
                      add_reg_names=add_reg_names)

#######################################
# Get the FMRI data
#######################################

fmri_data = surrogate_4d_dataset(mask=mask, dmtx=X, seed=1)[0]

########################################
# Perform a GLM analysis
########################################

# GLM fit
Y = fmri_data.get_data()[mask_array]
glm = GeneralLinearModel(X)
glm.fit(Y.T)

# specifiy the contrast [1 -1 0 ..]
contrast = np.zeros(X.shape[1])
contrast[:2] = np.array([1, -1])

# compute the constrast image related to it
zvals = glm.contrast(contrast).z_score()
zmap = mask_array.astype(np.float)
zmap[mask_array] = zvals

########################################
# Create ROIs
########################################
def motionWithin_measures():
	studies = {'Diffusion1': 'dMRI_topup_eddy.nii.gz.eddy_parameters','Diffusion2': 'dMRI_topup_eddy.nii.gz.eddy_parameters','Diffusion3': 'dMRI_topup_eddy.nii.gz.eddy_parameters','Diffusion4':
	'dMRI_topup_eddy.nii.gz.eddy_parameters','Rest1': 'fMRI_rest1_AP_motion.nii.gz.par','Rest2': 'fMRI_rest2_PA_motion.nii.gz.par','Rest3': 'fMRI_rest3_AP_motion.nii.gz.par','Rest4':
	'fMRI_rest4_PA_motion.nii.gz.par', 'Gambling1' : 'tfMRI_gambling1_AP_motion.nii.gz.par','Gambling2' : 'tfMRI_gambling2_PA_motion.nii.gz.par','FaceMatching1' :
	'tfMRI_faceMatching1_AP_motion.nii.gz.par','FaceMatching2' :
	'tfMRI_faceMatching2_PA_motion.nii.gz.par','Conflict1':'tfMRI_conflict1_AP_motion.nii.gz.par','Conflict2':'tfMRI_conflict2_PA_motion.nii.gz.par','Conflict3':'tfMRI_conflict3_AP_motion.nii.gz.par',
	'Conflict4':'tfMRI_conflict4_PA_motion.nii.gz.par', 'T1':'motion/T1_motion.nii.gz.par','T2':'motion/T2_motion.nii.gz.par'}

	scans=['T1'] #'Diffusion1','Diffusion2','Diffusion3','Diffusion4'] #,'Rest1','Rest2','Rest3','Rest4','Gambling1','Gambling2','FaceMatching1','FaceMatching2','Conflict1','Conflict2','Conflict3','Conflict4','T1','T2']
	#scans.append("T1 all vnavs - no reacq")
	#scans.append("T2 all vnavs - no reacq")
	#scans=['T2']
	diffusion=[ [0,98], [98,196],[196,295],[295,394]]

	metric = [getTranslation, getRotation] #, getTranslationAbsolute, getRotationAbsolute]
	metric_label=["Translation (mm/s)" , "Rotation ($\degree/s$)", "Absolute translation per second", "Absolute rotation per second"]

	indexPerDiffusion = {'Diffusion1':[0,98], 'Diffusion2':[98,196], 'Diffusion3':[196,295],'Diffusion4':[295,394]}
	fmri_order = {'Rest1':0,'Rest2':1,'Rest3':2,'Rest4':3,'Gambling1':4,'Gambling2':5, 'FaceMatching1':6,'FaceMatching2':7, 'Conflict1':8,'Conflict2':9,'Conflict3':10,'Conflict4':11}

	subjects, sex, scores = utils.loadBANDA140(outliers)

	motionWithin_output=csv.writer(open('/space/erebus/1/users/data/scores/new2/test_rjj091219.csv','w+'))
	motionWithin_output.writerow(['subject','rot_trans','avg_score','questionnaire','questionnaire_score'])

	for key, score in scores.items():

		print (scans)
		f, axarr = plt.subplots(1, 2,figsize=(23,14))
		f.subplots_adjust(left=.03, bottom=.06, right=.97, top=.95, wspace=.18, hspace=.28)

		trans_boxplot_data = [[],[]]
		rotat_boxplot_data = [[],[]]

		anhedonia = 0
		anhedonia_no = 0

		#box plot measures
		g, (boxes1,boxes2) = plt.subplots(nrows=1,ncols=2,figsize=(15,14))

		for i,met in enumerate(metric):
			axarr[i].set_title(str(met)[13:18])
			if str(met)[13:18]=='Trans': boxes1.set_title(str(met)[13:18])
			elif str(met)[13:18]=='Rotat': boxes2.set_title(str(met)[13:18])
			#for calculating axis ranges
			ymin_value = 0
			ymax_value = 0
			xmin_value = 0
			xmax_value = 0

			x_values = []
			y_values = []
			#for s in subjects:
			for s_index,s in enumerate(subjects):
				print(s)
				t_accumulate = 0

				for name in scans:
					fileN = studies[name.split()[0]]

					column,acq_time = getFileData(fileN)
					if "no vnavs" in name:
						if "T1" in name :
							index=[0,166]
						else:
							index=[0,111]
					else:
						index=[0,1000]
					if "dMRI" in fileN:
						index =diffusion[i]
					filePath = getFilePath(fileN,s)

						#print(i,j)
						#print(name)
					if filePath != None :
						#print (name)
						if "T1" == name:
							ind = readVNavsScoreFiles(s, "T1")
						elif "T2" == name:
							ind = readVNavsScoreFiles(s, "T2")
							#print(s, ind)
						else:
							ind = range(index[0], index[1])

						t=  met(filePath,column,np.sort(ind),acq_time)
						t_accumulate += t

				avg_t = t_accumulate/len(scans)

				if s_index == 0: ymin_value = avg_t
				ymin_value = min(ymin_value,avg_t)
				ymax_value = max(ymax_value,avg_t)

				if s_index == 0: xmin_value = float(score[s_index])
				xmin_value = min(xmin_value,float(score[s_index]))
				xmax_value = max(xmax_value,float(score[s_index]))

				if str(str(key))=='Anhedonia':
					if str(met)[13:18]=='Trans':
						if float(score[s_index])<3:
							trans_boxplot_data[0].append(avg_t)
							anhedonia_no += 1
						else:
							trans_boxplot_data[1].append(avg_t)
							anhedonia += 1
					if str(met)[13:18]=='Rotat':

						if float(score[s_index])<3: rotat_boxplot_data[0].append(avg_t)
						else: rotat_boxplot_data[1].append(avg_t)

				motionWithin_output.writerow([s,metric_label[metric.index(met)],avg_t,key,float(score[s_index])])
				axarr[i].plot(float(score[s_index]),avg_t,"o",markersize=5, color='m')

				x_values.append(float(score[s_index]))
				y_values.append(avg_t)

			#regression lines
			(sl,b) = polyfit(x_values,y_values,1)
			yp = polyval([sl,b],x_values)
			axarr[i].plot(x_values,yp,'-',color='r')
			#GLM
			X=np.array([])
			if key != 'Anhedonia':
				X  = np.ones(len(x_values))
				X =np.stack((X, x_values), axis=-1)
				cval = np.hstack((0, -1))
			else:
				for x in x_values:
					if x>=3:
						X=np.append(X,[[1,0]])
					else:
						X=np.append(X,[[0,1]])
				cval = np.hstack((1, -1))
			X= np.array(X).reshape(len(y_values),2)
			Y=np.array(y_values )

			model = GeneralLinearModel(X)
			model.fit(Y)
			z_vals = model.contrast(cval).p_value() # z-transformed statistics
			print(key, z_vals)

			axarr[i].set_ylabel("Average" + str(metric_label[metric.index(met)]))
			axarr[i].set_xlabel(str(key))

			axarr[i].set_xlim(xmin_value-(np.median(x_values)*.30),xmax_value+(np.median(x_values)*.30))
			axarr[i].set_ylim(ymin_value-(np.median(y_values)*.30),ymax_value+(np.median(y_values)*.30))

			if str(key)=='Anhedonia':
				if i==0:
					trans_box_max_list = [ max(a) for a in trans_boxplot_data]
					trans_box_min_list = [ min(a) for a in trans_boxplot_data]
					boxes1.set_ylabel("Average " + str(metric_label[metric.index(met)]))
					boxes1.set_xlabel(str(key))
				elif i==1:
					rotat_box_max_list = [ max(a) for a in rotat_boxplot_data]
					rotat_box_min_list = [ min(a) for a in rotat_boxplot_data]
					boxes2.set_ylabel("Average " + str(metric_label[metric.index(met)]))
					boxes2.set_xlabel(str(key))
		#f.savefig("/space/erebus/1/users/data/comparisonPlots_1_88/plots/motion_snr/motionWithin_avg_"+str(key),dpi=199)
		f.savefig("/autofs/space/erebus_001/users/data/scores/new2/plots/test_09122019_motion_Within_avg_"+str(key),dpi=199)

	if str(str(key))=='Anhedonia':
		boxes1.set_ylim(0,max(trans_box_max_list)+.25)
		boxes2.set_ylim(0,max(rotat_box_max_list)+.25)


		#print(trans_boxplot_data[0])
		#print(trans_boxplot_data[1])
		boxes1.boxplot(trans_boxplot_data,labels=['no anhedonia','anhedonia'])
		boxes2.boxplot(rotat_boxplot_data,labels=['no anhedonia','anhedonia'])
		g.savefig("/autofs/space/erebus_001/users/data/scores/new2/plots/test_09112019_motion_Within_avg_Anhedonia"+"_boxplot",dpi=199)
		#g.savefig("/space/erebus/1/users/vsiless/QA_plots//motion_Within_avg_Anhedonia"+"_boxplot",dpi=199)
		print(anhedonia)
		print(anhedonia_no)
Exemplo n.º 33
0
            pylab.savefig(os.path.join(write_dir, 'design_matrix_%s.png') %\
                              session)
            # get the data
            if side == False:
                Y, _ = data_scaling(load(fmri_data).get_data()[mask_array].T)
                affine = load(fmri_data).get_affine()
            else:
                Y = np.array([x.data for x in read(fmri_data).darrays])
                Y, _ = data_scaling(Y)
                if sess == 0: # do not redefine the mask later !
                    mask_array = np.var(Y, 0) > 0
                Y = Y[:, mask_array]

            # fit the glm
            print 'Fitting a GLM'
            result = GeneralLinearModel(design_matrix.matrix)
            result.fit(Y, model='ar1', steps=100)

            for contrast_id, contrast_val in contrasts.iteritems():
                if (contrast_val[session] == 0).all():
                    continue
                contrast_obj[contrast_id] =\
                    result.contrast(contrast_val[session])

        print 'Computing contrasts...'
        # In fact, this only writes the output images/textures
        # define the effects of interest
        F_ = np.zeros(mask_array.sum())
        for index, contrast_id in enumerate(contrasts):
            F_ += (contrast_obj[contrast_id].stat()) ** 2
            print '  Contrast % 2i out of %i: %s' % (
Exemplo n.º 34
0
design_matrix = np.vstack((np.ones(len(beh_data['sub_id_database_brain'])), beh_data['memory_acc'], beh_data['perception_acc'], beh_data['age'])).T
design_matrix[1,:] = stats.zscore(design_matrix[1,:])
design_matrix[2,:] = stats.zscore(design_matrix[2,:])
design_matrix[3,:] = stats.zscore(design_matrix[3,:])

contrasts = 1#, "memory>perception": [0,1,-1,0]}
contrasts_t_mmaps = {}
contrasts_p_mmaps = {}
contrasts_z_mmaps = {}
for contrast_name in contrasts.keys():
    contrasts_t_mmaps[contrast_name] =  np.memmap("/scr/adenauer1/tmp/%s_t_map.float64"%contrast_name, dtype='float64', mode='w+', shape=corr_mmaps[0].shape)
    contrasts_p_mmaps[contrast_name] =  np.memmap("/scr/adenauer1/tmp/%s_p_map.float64"%contrast_name, dtype='float64', mode='w+', shape=corr_mmaps[0].shape)
    contrasts_z_mmaps[contrast_name] =  np.memmap("/scr/adenauer1/tmp/%s_z_map.float64"%contrast_name, dtype='float64', mode='w+', shape=corr_mmaps[0].shape)

glm = GeneralLinearModel(design_matrix)

chunk_size = 40000
print len(corr_mmaps[0])

counter = 0

while counter < len(corr_mmaps[0]):
    data_chunk = np.vstack([(corr_mmaps[i][counter:counter+chunk_size]) for i in range(len(beh_data['sub_id_database_brain']))])
    data_chunk = np.arctanh(data_chunk/10000.0)
    
    glm.fit(data_chunk,model="ols")

    for contrast_name, contrast in contrasts.iteritems():
        c = glm.contrast(contrast)
        contrasts_t_mmaps[contrast_name][counter:counter+chunk_size] = c.stat()
Exemplo n.º 35
0
# Get the FMRI data
#######################################
fmri_data = surrogate_4d_dataset(mask=mask, dmtx=X)[0]
Y = fmri_data.get_data()[mask_array]

# artificially added signal in ROIs to make the example more meaningful
activation = 30 * (X.T[1] + .5 * X.T[0])
for (position, radius) in zip(positions, radii):
    Y[((domain.coord - position)**2).sum(1) < radius**2 + 1] += activation

########################################
# Perform a GLM analysis
########################################

# GLM fit
glm = GeneralLinearModel(X)
glm.fit(Y.T)

# specifiy the contrast [1 -1 0 ..]
contrast = np.hstack((1, -1, np.zeros(X.shape[1] - 2)))

# compute the constrast image related to it
zvals = glm.contrast(contrast).z_score()

########################################
# ROI-based analysis
########################################

# exact the time courses with ROIs
signal_feature = [Y[my_roi.select_id(id, roi=False)] for id in my_roi.get_id()]
my_roi.set_feature('signal', signal_feature)
Exemplo n.º 36
0
def run_glms(subject):
    # necessary paths
    analysis_dir = os.path.join(spm_dir, subject, 'analyses')
    subject_dir = os.path.join(work_dir, subject)
    if os.path.exists(subject_dir) == False:
        os.mkdir(subject_dir)
    fmri_dir = os.path.join(subject_dir, 'fmri')
    if os.path.exists(fmri_dir) == False:
        os.mkdir(fmri_dir)
    result_dir = os.path.join(fmri_dir, 'results')
    if os.path.exists(result_dir) == False:
        os.mkdir(result_dir)
    memory = Memory(cachedir=os.path.join(fmri_dir, 'cache_dir'), verbose=0)
    
    # audiosentence protocol
    # step 1: get the necessary files
    spm_fmri_dir = os.path.join(spm_dir, subject, 'fMRI/audiosentence')
    onset_dir = os.path.join(analysis_dir, 'audiosentence')
    onset_files = glob.glob(os.path.join(onset_dir, 'onsetfile*.mat'))
    motion_files = glob.glob(
        os.path.join(spm_fmri_dir, 'rp*.txt'))
    left_fmri_files = glob.glob(os.path.join(spm_fmri_dir, 'sraaudio*_lh.gii'))
    right_fmri_files = glob.glob(os.path.join(spm_fmri_dir, 'sraaudio*_rh.gii'))
    onset_files.sort()
    motion_files.sort()
    left_fmri_files.sort()
    right_fmri_files.sort()
    
    # get the ratings of the trials
    final_data = os.path.join(behavioral_dir, subject,
                               'finaldata_%s.mat' %subject)
    ratings = make_ratings(final_data)
    
    # scan times
    n_scans = 200
    lh_effects, lh_variances, rh_effects, rh_variances = {}, {}, {}, {}
    for i, (onset_file, motion_file, left_fmri_file, right_fmri_file) in\
            enumerate(zip(
            onset_files, motion_files, left_fmri_files, right_fmri_files)):
        # Create the design matrix
        dmtx = audiosentence_dmtx(final_data, motion_file, n_scans, tr, i)
        ax = dmtx.show()
        ax.set_position([.05, .25, .9, .65])
        ax.set_title('Design matrix')
        session_contrasts = audiosentence_contrasts(dmtx.names, final_data, i)
        fmri_glm = GeneralLinearModel(dmtx.matrix)

        # left hemisphere
        Y = np.array([darrays.data for darrays in read(left_fmri_file).darrays])
        # fit the GLM
        fmri_glm.fit(Y, model='ar1')
        # Estimate the contrasts
        print('Computing contrasts...')
        for index, contrast_id in enumerate(session_contrasts):
            print('  Contrast % i out of %i: %s' %
                  (index + 1, len(session_contrasts), contrast_id))
            # save the z_image
            contrast_ = fmri_glm.contrast(session_contrasts[contrast_id])
            if i == 0:
                lh_effects[contrast_id] = [contrast_.effect.ravel()]
                lh_variances[contrast_id] = [contrast_.variance.ravel()]
            else:
                lh_effects[contrast_id].append(contrast_.effect.ravel())
                lh_variances[contrast_id].append(contrast_.variance.ravel())
        
        # right hemisphere
        Y = np.array(
            [darrays.data for darrays in read(right_fmri_file).darrays])
        # fit the GLM
        fmri_glm.fit(Y, model='ar1')

        # Estimate the contrasts
        
        for index, contrast_id in enumerate(session_contrasts):
            # save the z_image
            contrast_ = fmri_glm.contrast(session_contrasts[contrast_id])
            if i == 0:
                rh_effects[contrast_id] = [contrast_.effect.ravel()]
                rh_variances[contrast_id] = [contrast_.variance.ravel()]
            else:
                rh_effects[contrast_id].append(contrast_.effect.ravel())
                rh_variances[contrast_id].append(contrast_.variance.ravel())
        
    
    for index, contrast_id in enumerate(session_contrasts):
        # left hemisphere
        _, _, z_map = fixed_effects(
            lh_effects[contrast_id], lh_variances[contrast_id])
        z_texture = GiftiImage(
            darrays=[GiftiDataArray().from_array(z_map, intent='t test')])
        z_map_path = os.path.join(result_dir, '%s_z_map_lh.gii' % contrast_id)
        write(z_texture, z_map_path)
        # right hemisphere
        _, _, z_map = fixed_effects(
            rh_effects[contrast_id], rh_variances[contrast_id])
        z_texture = GiftiImage(
            darrays=[GiftiDataArray().from_array(z_map, intent='t test')])
        z_map_path = os.path.join(result_dir, '%s_z_map_rh.gii' % contrast_id)
        write(z_texture, z_map_path)

    #########################################################################
    # localizer protocol
    # get the necessary files
    spm_fmri_dir = os.path.join(spm_dir, subject, 'fMRI/localizer')
    motion_file, = glob.glob(
        os.path.join(spm_dir, subject, 'fMRI/localizer/rp*.txt'))
    left_fmri_file = glob.glob(
        os.path.join(spm_fmri_dir, 'sralocalizer*_lh.gii'))[0]
    right_fmri_file = glob.glob(
        os.path.join(spm_fmri_dir, 'sralocalizer*_rh.gii'))[0]
    
    n_scans = 205

    # Create the design matrix
    dmtx = localizer_dmtx(motion_file, n_scans, tr)
    ax = dmtx.show()
    ax.set_position([.05, .25, .9, .65])
    ax.set_title('Design matrix')
    session_contrasts = localizer_contrasts(dmtx)
    fmri_glm = GeneralLinearModel(dmtx.matrix)
    
    # left hemisphere
    Y = np.array([darrays.data for darrays in read(left_fmri_file).darrays])
    # fit the GLM
    fmri_glm.fit(Y, model='ar1')
    # Estimate the contrasts
    print('Computing contrasts...')
    for index, contrast_id in enumerate(session_contrasts):
        print('  Contrast % i out of %i: %s' %
              (index + 1, len(session_contrasts), contrast_id))
        # save the z_image
        contrast_ = fmri_glm.contrast(session_contrasts[contrast_id])
        z_map = contrast_.z_score()
        z_texture = GiftiImage(
            darrays=[GiftiDataArray().from_array(z_map, intent='t test')])
        z_map_path = os.path.join(result_dir, '%s_z_map_lh.gii' % contrast_id)
        write(z_texture, z_map_path)

    # right hemisphere
    Y = np.array([darrays.data for darrays in read(right_fmri_file).darrays])
    # fit the GLM
    fmri_glm.fit(Y, model='ar1')
    # Estimate the contrasts
    print('Computing contrasts...')
    for index, contrast_id in enumerate(session_contrasts):
        print('  Contrast % i out of %i: %s' %
              (index + 1, len(session_contrasts), contrast_id))
        # save the z_image
        contrast_ = fmri_glm.contrast(session_contrasts[contrast_id])
        z_map = contrast_.z_score()
        z_texture = GiftiImage(
            darrays=[GiftiDataArray().from_array(z_map, intent='t test')])
        z_map_path = os.path.join(result_dir, '%s_z_map_rh.gii' % contrast_id)
        write(z_texture, z_map_path)
    
    #########################################################################
    # VisualCategs protocol
    # get the necessary files
    spm_fmri_dir = os.path.join(spm_dir, subject, 'fMRI/visualcategs')
    onset_dir = os.path.join(analysis_dir, 'visualcategs')
    onset_files = glob.glob(os.path.join(onset_dir, 'onsetfile*.mat'))
    motion_files = glob.glob(
        os.path.join(spm_dir, subject, 'fMRI/visualcategs/rp*.txt'))
    fmri_files = glob.glob(os.path.join(fmri_dir, 'crvisu*.nii.gz'))
    onset_files.sort()
    motion_files.sort()
    fmri_files.sort()

    left_fmri_files = glob.glob(
        os.path.join(spm_fmri_dir, 'sravisu*_lh.gii'))
    right_fmri_files = glob.glob(
        os.path.join(spm_fmri_dir, 'sravisu*_rh.gii'))
    n_scans = 185

    lh_effects, lh_variances, rh_effects, rh_variances = {}, {}, {}, {}
    
    for i, (onset_file, motion_file, left_fmri_file, right_fmri_file) in\
            enumerate(zip(
            onset_files, motion_files, left_fmri_files, right_fmri_files)):
        # Create the design matrix
        dmtx = visualcategs_dmtx(onset_file, motion_file, n_scans, tr)
        ax = dmtx.show()
        ax.set_position([.05, .25, .9, .65])
        ax.set_title('Design matrix')
        session_contrasts = visualcategs_contrasts(dmtx.names)
        fmri_glm = GeneralLinearModel(dmtx.matrix)
    
        # left hemisphere
        Y = np.array([darrays.data for darrays in read(left_fmri_file).darrays])
        # fit the GLM
        fmri_glm.fit(Y, model='ar1')
        # Estimate the contrasts
        print('Computing contrasts...')
        for index, contrast_id in enumerate(session_contrasts):
            print('  Contrast % i out of %i: %s' %
                  (index + 1, len(session_contrasts), contrast_id))
            # save the z_image
            contrast_ = fmri_glm.contrast(session_contrasts[contrast_id])
            if i == 0:
                lh_effects[contrast_id] = [contrast_.effect.ravel()]
                lh_variances[contrast_id] = [contrast_.variance.ravel()]
            else:
                lh_effects[contrast_id].append(contrast_.effect.ravel())
                lh_variances[contrast_id].append(contrast_.variance.ravel())

        # right hemisphere
        Y = np.array([
                darrays.data for darrays in read(right_fmri_file).darrays])
        # fit the GLM
        fmri_glm.fit(Y, model='ar1')
        # Estimate the contrasts
        print('Computing contrasts...')
        for index, contrast_id in enumerate(session_contrasts):
            print('  Contrast % i out of %i: %s' %
                  (index + 1, len(session_contrasts), contrast_id))
            # save the z_image
            contrast_ = fmri_glm.contrast(session_contrasts[contrast_id])
            if i == 0:
                rh_effects[contrast_id] = [contrast_.effect.ravel()]
                rh_variances[contrast_id] = [contrast_.variance.ravel()]
            else:
                rh_effects[contrast_id].append(contrast_.effect.ravel())
                rh_variances[contrast_id].append(contrast_.variance.ravel())

    for index, contrast_id in enumerate(session_contrasts):
        # left hemisphere
        _, _, z_map = fixed_effects(
            lh_effects[contrast_id], lh_variances[contrast_id])
        z_texture = GiftiImage(
            darrays=[GiftiDataArray().from_array(z_map, intent='t test')])
        z_map_path = os.path.join(result_dir, '%s_z_map_lh.gii' % contrast_id)
        write(z_texture, z_map_path)
        # right hemisphere
        _, _, z_map = fixed_effects(
            rh_effects[contrast_id], rh_variances[contrast_id])
        z_texture = GiftiImage(
            darrays=[GiftiDataArray().from_array(z_map, intent='t test')])
        z_map_path = os.path.join(result_dir, '%s_z_map_rh.gii' % contrast_id)
        write(z_texture, z_map_path)