Esempio 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
Esempio 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
Esempio n. 3
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
Esempio n. 4
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
Esempio n. 5
0
]
my_roi.set_feature('contrast', contrast_feature)
my_roi.set_roi_feature('contrast_avg',
                       my_roi.representative_feature('contrast'))

########################################
# GLM analysis on the ROI average time courses
########################################

n_reg = len(names)
roi_tc = my_roi.get_roi_feature('signal_avg')
glm.fit(roi_tc.T)

plt.figure()
plt.subplot(1, 2, 1)
betas = glm.get_beta()
b1 = plt.bar(np.arange(n_reg - 1),
             betas[:-1, 0],
             width=.4,
             color='blue',
             label='region 1')
b2 = plt.bar(np.arange(n_reg - 1) + 0.3,
             betas[:-1, 1],
             width=.4,
             color='red',
             label='region 2')
plt.xticks(np.arange(n_reg - 1), names[:-1], fontsize=10)
plt.legend()
plt.title('Parameter estimates \n for the roi time courses')

bx = plt.subplot(1, 2, 2)
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'))
Esempio n. 7
0
                    for id in my_roi.get_id()]
my_roi.set_feature('contrast', contrast_feature)
my_roi.set_roi_feature('contrast_avg',
                       my_roi.representative_feature('contrast'))

########################################
# GLM analysis on the ROI average time courses
########################################

n_reg = len(names)
roi_tc = my_roi.get_roi_feature('signal_avg')
glm.fit(roi_tc.T)

plt.figure()
plt.subplot(1, 2, 1)
betas = glm.get_beta()
b1 = plt.bar(np.arange(n_reg - 1), betas[:-1, 0], width=.4, color='blue',
            label='region 1')
b2 = plt.bar(np.arange(n_reg - 1) + 0.3, betas[:- 1, 1], width=.4,
            color='red', label='region 2')
plt.xticks(np.arange(n_reg - 1), names[:-1], fontsize=10)
plt.legend()
plt.title('Parameter estimates \n for the roi time courses')

bx = plt.subplot(1, 2, 2)
my_roi.plot_feature('contrast', bx)

########################################
# fitted and adjusted response
########################################
Esempio n. 8
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
Esempio n. 9
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