sessions, fmri_series)): # create design matrices reg = all_reg[2 * sess: 2 * sess + 2] # fixme design_matrix = make_dmtx( frametimes, add_regs=reg_matrix, add_reg_names=reg, drift_model=drift_model, hfcut=hfcut) # plot the design matrix ax = design_matrix.show() ax.set_position([.05, .25, .9, .65]) ax.set_title('Design matrix') 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():
contrasts["V-H"] = contrasts["damier_V"] - contrasts["damier_H"] contrasts["left-right"] = contrasts["left"] - contrasts["right"] 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()
# Get multi-run fMRI data print ("Loading fmri data...") 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
design_matrix = dmtx.matrix # Plot the design matrix if plot_design_matrix: fig1 = mp.figure(figsize=(10, 6)) dmtx.show() mp.title(desc) fig1_file = os.path.join(out_path, label + 'design_matrix_test' + \ str(ntest) + '.png') 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
sessions, fmri_series)): # create design matrices reg = all_reg[4 * sess: 4 * sess + 4] # fixme design_matrix = make_dmtx( frametimes, add_regs=reg_matrix, add_reg_names=reg, 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') 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
# 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 mean_map = mask_array.astype(np.float)
reg = all_reg[2 * sess:2 * sess + 2] # fixme design_matrix = make_dmtx(frametimes, add_regs=reg_matrix, add_reg_names=reg, drift_model=drift_model, hfcut=hfcut) # plot the design matrix ax = design_matrix.show() ax.set_position([.05, .25, .9, .65]) ax.set_title('Design matrix') 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 (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():
# Get multi-run fMRI data print('Loading fmri data...') 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