def get_localizations (X, y, cv, maskf, presels, sv): mask, hdr, aff = au.get_nii_data(maskf) maskidx = np.array(np.where(mask > 0)) hdr.set_data_dtype(np.dtype(np.float)) my_presels = np.zeros_like(presels[0]) my_svs = np.zeros_like(mask) my_svs_done = False #unmasking method found in: #http://nisl.github.io/auto_examples/plot_ica_resting_state.html from nisl.io import NiftiMasker k = 0 cv.n = X.shape[0] for train,test in cv: X_train, y_train = X[train,:], y[train] preselsvol = np.zeros_like (mask, dtype=np.float) preselsvol[tuple(maskidx)] = presels[k] > 0 preselsnii = au.save_nibabel ('', preselsvol, aff, hdr) my_presels += presels[k] > 0 if len(sv) > 0: try: nifti_masker = NiftiMasker(mask=preselsnii) nifti_masker.fit (X_train[:,presels[k]>0], y_train) niimg = nifti_masker.inverse_transform(sv[k][0]) #X_masked = nifti_masker.fit_transform (X_train[:,presels[k]>0], y_train) #,y_train, target_affine=aff, target_shape=hdr.get_data_shape() #niimg = nifti_masker.inverse_transform(sv[0][0]) #act = np.ma.masked_array(niimg.get_data(), niimg.get_data() == 0) my_svs += niimg.get_data() my_svs_done = True except: pass k += 1 my_presels /= cv.n_folds my_svs /= cv.n_folds prelocs = np.zeros_like (mask, dtype=np.float) prelocs[tuple(maskidx)] = my_presels return prelocs, my_svs, my_svs_done
def get_nii_data_from_folder(folder, use_verbs=False): # inside the folder, search only for betasXX.nii files beta_map_regex = r'betas(\d{2}).nii$' beta_map_filter = re_filter(beta_map_regex) beta_files = filter(beta_map_filter, glob.glob( os.path.join(folder, "*"))) beta_files = sorted(beta_files) # get mask file. It must be in same folder mask_file = os.path.join(folder, "mask.nii") #new_folder = '/volatile/thirion/mygit/worddecoding/brain_reading/fmri/vl100318/fmri/' ## ugly #mask_file = os.path.join(new_folder, "HO_epi.nii") masker = NiftiMasker(mask_file, smooth=3.) masker.fit(beta_files[0]) masked_nii_data = [masker.transform(beta_file) for beta_file in beta_files] # this returns a 3D array with dimensions # (sessions, trials, voxels) masked_nii_data = np.array(masked_nii_data) # return only the useful values: The last 6 are # drift regressors and are thrown away masked_nii_data = masked_nii_data[:, :-6, :] if use_verbs: return masked_nii_data else: # In case we do not want the verbs (default case) # we need to remove the lines corresponding to verbs masked_nii_data = masked_nii_data.reshape(-1, masked_nii_data.shape[-1]) masked_nii_data = masked_nii_data[:, masked_nii_data.std(0) > 0] _, verbs, _, _, _ = parse_stimuli() return masked_nii_data[verbs == False]
def get_nii_data_from_folder(folder, use_verbs=False): # inside the folder, search only for betasXX.nii files beta_map_regex = r'betas(\d{2}).nii$' beta_map_filter = re_filter(beta_map_regex) beta_files = filter(beta_map_filter, glob.glob(os.path.join(folder, "*"))) beta_files = sorted(beta_files) # get mask file. It must be in same folder mask_file = os.path.join(folder, "mask.nii") #new_folder = '/volatile/thirion/mygit/worddecoding/brain_reading/fmri/vl100318/fmri/' ## ugly #mask_file = os.path.join(new_folder, "HO_epi.nii") masker = NiftiMasker(mask_file, smooth=3.) masker.fit(beta_files[0]) masked_nii_data = [masker.transform(beta_file) for beta_file in beta_files] # this returns a 3D array with dimensions # (sessions, trials, voxels) masked_nii_data = np.array(masked_nii_data) # return only the useful values: The last 6 are # drift regressors and are thrown away masked_nii_data = masked_nii_data[:, :-6, :] if use_verbs: return masked_nii_data else: # In case we do not want the verbs (default case) # we need to remove the lines corresponding to verbs masked_nii_data = masked_nii_data.reshape(-1, masked_nii_data.shape[-1]) masked_nii_data = masked_nii_data[:, masked_nii_data.std(0) > 0] _, verbs, _, _, _ = parse_stimuli() return masked_nii_data[verbs == False]
================================== Here is a simple example of automatic mask computation using the nifti masker. The mask is computed and visualized. """ ### Load nyu_rest dataset ##################################################### from nisl import datasets from nisl.io import NiftiMasker dataset = datasets.fetch_nyu_rest(n_subjects=1) ### Compute the mask ########################################################## nifti_masker = NiftiMasker(memory="nisl_cache", memory_level=2) nifti_masker.fit(dataset.func[0]) mask = nifti_masker.mask_img_.get_data() ### Visualize the mask ######################################################## import pylab as pl import numpy as np import nibabel pl.figure() pl.axis('off') pl.imshow(np.rot90(nibabel.load(dataset.func[0]).get_data()[..., 20, 0]), interpolation='nearest', cmap=pl.cm.gray) ma = np.ma.masked_equal(mask, False) pl.imshow(np.rot90(ma[..., 20]), interpolation='nearest', cmap=pl.cm.autumn, alpha=0.5) pl.title("Mask")
### Restrict to faces and houses ############################################## condition_mask = np.logical_or(conditions == 'face', conditions == 'house') X = fmri_data[..., condition_mask] y = y[condition_mask] session = session[condition_mask] conditions = conditions[condition_mask] ### Loading step ############################################################## from nisl.io import NiftiMasker from nibabel import Nifti1Image nifti_masker = NiftiMasker(mask=mask, sessions=session, memory='nisl_cache', memory_level=1) niimg = Nifti1Image(X, affine) X_masked = nifti_masker.fit(niimg).transform(niimg) X_preprocessed = nifti_masker.inverse_transform(X_masked).get_data() X_preprocessed = np.rollaxis(X_preprocessed, axis=-1) mask = nifti_masker.mask_img_.get_data().astype(np.bool) ### Prepare the masks ######################################################### # Here we will use several masks : # * mask is the originalmask # * process_mask is a subset of mask, it contains voxels that should be # processed (we only keep the slice z = 26 and the back of the brain to speed # up computation) process_mask = mask.copy() process_mask[..., 38:] = False process_mask[..., :36] = False process_mask[:, 30:] = False
condition_mask = np.logical_or(conditions == 'face', conditions == 'house') X = fmri_data[..., condition_mask] y = y[condition_mask] session = session[condition_mask] conditions = conditions[condition_mask] ### Loading step ############################################################## from nisl.io import NiftiMasker from nibabel import Nifti1Image # Detrending is disabled as we are not yet able to do it by session nifti_masker = NiftiMasker(mask=mask, detrend=True, copy=False, sessions=session) niimg = Nifti1Image(X, affine) X_masked = nifti_masker.fit(niimg).transform(niimg) X_detrended = nifti_masker.inverse_transform(X_masked).get_data() ### Prepare the masks ######################################################### # Here we will use several masks : # * mask is the originalmask # * process_mask is a subset of mask, it contains voxels that should be # processed (we only keep the slice z = 26 and the back of the brain to speed # up computation) process_mask = mask.copy() process_mask[..., 38:] = False process_mask[..., :36] = False process_mask[:, 30:] = False ### Searchlight ###############################################################