def test_fetch_haxby(): for i in range(1, 6): haxby = datasets.fetch_haxby(data_dir=tmpdir, n_subjects=i, verbose=0) assert_equal(len(url_request.urls), 1 + (i == 1)) # subject_data + md5 assert_equal(len(haxby.func), i) assert_equal(len(haxby.anat), i) assert_equal(len(haxby.session_target), i) assert_equal(len(haxby.mask_vt), i) assert_equal(len(haxby.mask_face), i) assert_equal(len(haxby.mask_house), i) assert_equal(len(haxby.mask_face_little), i) assert_equal(len(haxby.mask_house_little), i) url_request.reset()
def read_data_haxby(subject, tr=2.5, masker=False): haxby_dataset = fetch_haxby(subjects=[subject]) # Load fmri data fmri_filename = haxby_dataset.func[0] fmri = load_img(fmri_filename) # mask = haxby_dataset.mask_vt[0] masker = NiftiMasker(mask_strategy='epi', standardize=True, detrend=True, high_pass=0.01, t_r=tr, smoothing_fwhm=5) fmri = masker.fit_transform(fmri) fmri = fmri.reshape(12, -1, fmri.shape[-1]) # Load stimuli data classes = np.array(['rest', 'face', 'house', 'bottle', 'cat', 'chair', 'scissors', 'shoe', 'scrambledpix']) labels = np.recfromcsv( haxby_dataset.session_target[0], delimiter=" ")['labels'].reshape( 12, -1) stimuli, onsets, conditions = (np.zeros(( 12, len(labels[0]), len(classes))), [], []) stimuli[:, 0, 0] = 1 for session in range(12): onsets.append([]) conditions.append([]) for scan in range(1, len(fmri[session])): if (labels[session][scan - 1] == 'rest' and labels[session][scan] != 'rest'): label = labels[session][scan] stimuli[session, scan, np.where(classes == label)[0][0]] = 1 conditions[session].append(label) onsets[session].append(scan * tr) else: stimuli[session, scan, 0] = 1 if subject == 5: fmri = np.vstack((fmri[:8], fmri[9:])) stimuli = np.vstack((stimuli[:8], stimuli[9:])) onsets = np.vstack((onsets[:8], onsets[9:])) conditions = np.vstack((conditions[:8], conditions[9:])) if masker: return fmri, stimuli, onsets, conditions, masker return fmri, stimuli, onsets, conditions
return _check_vars(data) ''' subject_niimg = load_subject(ccc, mask_niimg) subject_niimg = load_subject(subject_filename, mask_niimg) print("Image shape is %s" % (str(subject_niimg.shape))) num_components = subject_niimg.shape[-1] print("Detected {num_components} spatial maps".format(num_components=num_components)) from nilearn import datasets haxby_dataset = datasets.fetch_haxby() # load dataset # print basic information on the dataset print('First subject anatomical nifti image (3D) is at: %s' % haxby_dataset.anat[0]) print('First subject functional nifti image (4D) is at: %s' % haxby_dataset.func[0]) # 4D data #%% # Build the mean image because we have no anatomic data from nilearn import image func_filename = haxby_dataset.func[0] mean_img = image.mean_img(func_filename) mean_img = image.mean_img(subject_niimg) z_slice = -14
This is a block-design fMRI dataset from a study on face and object\ representation in human ventral temporal cortex. It consists of 6 subjects\ with 12 runs per subject. In each run, the subjects passively viewed \ greyscale images of eight object categories, grouped in 24s blocks separated\ by rest periods. Each image was shown for 500ms and was followed by a 1500ms\ inter-stimulus interval. Full-brain fMRI data were recorded with a volume \ repetition time of 2.5s, thus, a stimulus block was covered by roughly 9 \ volumes. Get full description <a href="http://dev.pymvpa.org/datadb/haxby2001.html">\ here</a>.\ """ # fetch HAXBY dataset N_SUBJECTS = 2 haxby_data = fetch_haxby(n_subjects=N_SUBJECTS) # set output dir OUTPUT_DIR = os.path.join(os.path.dirname(haxby_data.mask), "haxby_runs") if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) # get subject data subjects = [] for subject_id in set( [os.path.basename(os.path.dirname(x)) for x in haxby_data.func]): # instantiate subject_data object subject_data = SubjectData() subject_data.subject_id = subject_id subject_data.session_id = "haxby2001"
Objects in Ventral Temporal Cortex". Specifically, we look at decoding accuracy for different objects in three different masks: the full ventral stream (mask_vt), the house selective areas (mask_house) and the face selective areas (mask_face), that have been defined via a standard GLM-based analysis. """ ########################################################################## # First we load and prepare the data # Fetch data using nilearn dataset fetcher from nilearn import datasets # by default we fetch 2nd subject data for analysis haxby_dataset = datasets.fetch_haxby() func_filename = haxby_dataset.func[0] # Print basic information on the dataset print('First subject anatomical nifti image (3D) located is at: %s' % haxby_dataset.anat[0]) print('First subject functional nifti image (4D) is located at: %s' % func_filename) # Load nilearn NiftiMasker, the practical masking and unmasking tool from nilearn.input_data import NiftiMasker # load labels import numpy as np labels = np.recfromcsv(haxby_dataset.session_target[0], delimiter=" ") stimuli = labels['labels']
""" NeuroImaging volumes visualization ==================================== Simple example to show Nifti data visualization. """ ### Fetch data ################################################################ from nilearn import datasets haxby_files = datasets.fetch_haxby(n_subjects=1) ### Load an fMRI file ######################################################### import nibabel fmri_img = nibabel.load(haxby_files.func[0]) fmri_data = fmri_img.get_data() fmri_affine = fmri_img.get_affine() ### Visualization ############################################################# import numpy as np import matplotlib.pyplot as plt # Compute the mean EPI: we do the mean along the axis 3, which is time mean_img = np.mean(fmri_data, axis=3) # Note that this can also be done on Nifti images using # nilearn.image.mean_img
""" Show stimuli of Haxby et al. dataset =============================================================================== In this script we plot an overview of the stimuli used in "Distributed and Overlapping Representations of Faces and Objects in Ventral Temporal Cortex" (Science 2001) """ from scipy.misc import imread import matplotlib.pyplot as plt from nilearn import datasets haxby_dataset = datasets.fetch_haxby(n_subjects=0, fetch_stimuli=True) stimulus_information = haxby_dataset.stimuli for stim_type in sorted(stimulus_information.keys()): if stim_type == "controls": # skip control images, there are too many continue file_names = stimulus_information[stim_type] plt.figure() for i in range(48): plt.subplot(6, 8, i + 1) try: plt.imshow(imread(file_names[i]), cmap=plt.cm.gray) except: # just go to the next one if the file is not present
def read_data_haxby(subject, tr=2.5): """ Applies anova feature selection to fmri data using classification accuracy on stimuli data as measure of performance Parameters ---------- subject: int from 1 to 6 subject from which to load haxby dataset data Returns ------- fmri: numpy array of shape [n_scans, n_voxels] feature-selected train data from the fmri sessions stimuli: numpy array of shape [n_scans, n_categories] time series of the stimuli with one-hot encoding onsets: array of shape [n_sessions, n_stimuli] onset times for stimuli conditions: array of shape [n_sessions, n_stimuli] labels for stimuli """ haxby_dataset = fetch_haxby(subjects=[subject]) # Load fmri data fmri_filename = haxby_dataset.func[0] fmri = load_img(fmri_filename) # mask = haxby_dataset.mask_vt[0] masker = NiftiMasker(mask_strategy='epi', standardize=True, detrend=True, high_pass=0.01, t_r=tr, smoothing_fwhm=5) fmri = masker.fit_transform(fmri) fmri = fmri.reshape(12, -1, fmri.shape[-1]) # Load stimuli data classes = np.array([ 'rest', 'face', 'house', 'bottle', 'cat', 'chair', 'scissors', 'shoe', 'scrambledpix' ]) labels = np.recfromcsv(haxby_dataset.session_target[0], delimiter=" ")['labels'].reshape(12, -1) stimuli, onsets, conditions = (np.zeros( (12, len(labels[0]), len(classes))), [], []) stimuli[0, 0] = 1 for session in range(12): onsets.append([]) conditions.append([]) for scan in range(1, len(fmri[session])): if (labels[session][scan - 1] == 'rest' and labels[session][scan] != 'rest'): label = labels[session][scan] stimuli[session, scan, np.where(classes == label)[0][0]] = 1 conditions[session].append(label) onsets[session].append(scan * tr) else: stimuli[session, scan, 0] = 1 # Remove ninth run for subject 5 (corrupted) if subject == 5: fmri = np.vstack((fmri[:8], fmri[9:])) stimuli = np.vstack((stimuli[:8], stimuli[9:])) onsets = np.vstack((onsets[:8], onsets[9:])) conditions = np.vstack((conditions[:8], conditions[9:])) onsets = np.asarray(onsets) conditions = np.asarray(conditions) return fmri, stimuli, onsets, conditions
""" Decoding with SpaceNet: face vs house object recognition ========================================================= Here is a simple example of decoding with a SpaceNet prior (i.e Graph-Net, TV-l1, etc.), reproducing the Haxby 2001 study on a face vs house discrimination task. See also the SpaceNet documentation: :ref:`space_net`. """ ############################################################################## # Load the Haxby dataset from nilearn.datasets import fetch_haxby data_files = fetch_haxby() # Load Target labels import numpy as np labels = np.recfromcsv(data_files.session_target[0], delimiter=" ") # Restrict to face and house conditions target = labels["labels"] condition_mask = np.logical_or(target == b"face", target == b"house") # Split data into train and test samples, using the chunks condition_mask_train = np.logical_and(condition_mask, labels["chunks"] <= 6) condition_mask_test = np.logical_and(condition_mask, labels["chunks"] > 6)
---------- [1] Winkler, A. M. et al. (2014). Permutation inference for the general linear model. Neuroimage. [2] Anderson, M. J. & Robinson, J. (2001). Permutation tests for linear models. Australian & New Zealand Journal of Statistics, 43(1), 75-88. (http://avesbiodiv.mncn.csic.es/estadistica/permut2.pdf) """ # Author: Virgile Fritsch, <*****@*****.**>, Feb. 2014 ############################################################################## # Load Haxby dataset from nilearn import datasets haxby_dataset = datasets.fetch_haxby(subjects=[2]) # print basic information on the dataset print('Mask nifti image (3D) is located at: %s' % haxby_dataset.mask) print('Functional nifti image (4D) is located at: %s' % haxby_dataset.func[0]) ############################################################################## # Mask data mask_filename = haxby_dataset.mask from nilearn.input_data import NiftiMasker nifti_masker = NiftiMasker( smoothing_fwhm=8, mask_img=mask_filename, memory='nilearn_cache', memory_level=1) # cache options func_filename = haxby_dataset.func[0] fmri_masked = nifti_masker.fit_transform(func_filename)
""" Plot Haxby masks ================= Small script to plot the masks of the Haxby dataset. """ from scipy import linalg import matplotlib.pyplot as plt from nilearn import datasets data = datasets.fetch_haxby() # Build the mean image because we have no anatomic data from nilearn import image mean_img = image.mean_img(data.func[0]) z_slice = -24 from nilearn.image.resampling import coord_transform affine = mean_img.get_affine() _, _, k_slice = coord_transform(0, 0, z_slice, linalg.inv(affine)) k_slice = round(k_slice) fig = plt.figure(figsize=(4, 5.4), facecolor='k') from nilearn.plotting import plot_anat display = plot_anat(mean_img, display_mode='z', cut_coords=[z_slice], figure=fig) display.add_contours(data.mask_vt[0], contours=1, antialiased=False, linewidths=4., levels=[0], colors=['red']) display.add_contours(data.mask_house[0], contours=1, antialiased=False,
fmri_compressed = np.array(fmri_reduced.T[labels]) return fmri_compressed import matplotlib.pyplot as plt from nilearn.plotting import plot_stat_map, plot_roi, plot_epi from nilearn import datasets from nilearn.input_data import NiftiMasker if __name__ == '__main__': plt.close('all') data_dir = '/volatile/andres/brain_codes/DATA/nilearn_data' data_dir = None dataset = datasets.fetch_haxby(data_dir=data_dir, n_subjects=1) masker = NiftiMasker(mask_strategy='epi', smoothing_fwhm=6, memory='cache') X = masker.fit_transform(dataset.func[0]) # cluster = KMeans(n_clusters=5) cluster = ReNN( masker=masker, scaling=False, n_clusters=2000, linkage='fast', # linkage='single', # random=True, ) # # Test random projection
def _crop_mask(mask): """Crops input mask to produce tighter (i.e smaller) bounding box with the same support (active voxels)""" idx = np.where(mask) i_min = max(idx[0].min() - 1, 0) i_max = idx[0].max() j_min = max(idx[1].min() - 1, 0) j_max = idx[1].max() k_min = max(idx[2].min() - 1, 0) k_max = idx[2].max() return mask[i_min:i_max + 1, j_min:j_max + 1, k_min:k_max + 1] ### Load haxby dataset ######################################################## from nilearn.datasets import fetch_haxby data_files = fetch_haxby('/home/eugene/Documents/') ### Load Target labels ######################################################## import numpy as np labels = np.recfromcsv(data_files.session_target[0], delimiter=" ") ### split data into train and test samples #################################### n_train = 6 target = labels['labels'] condition_mask = np.logical_or(target == "scissors", target == "scrambledpix") condition_mask_train = np.logical_and(condition_mask, labels['chunks'] <= n_train) condition_mask_test = np.logical_and(condition_mask, labels['chunks'] > n_train) # make X (design matrix) and y (response variable)
from nilearn.datasets import fetch_haxby haxby_data = fetch_haxby(n_subjects=1, fetch_stimuli=True)
32, kernel_size=3, strides=2, activation='relu', padding="same"), BatchNormalization(), Conv3D(16, kernel_size=3, activation='relu', padding="same"), BatchNormalization(), Conv3DTranspose( 16, kernel_size=3, strides=2, activation='relu', padding="same"), BatchNormalization(), Conv3D(1, kernel_size=3, activation=None, padding="same"), ], name="decoder") autoencoder = Sequential([encoder, decoder], name="autoencoder") return encoder, decoder, autoencoder if __name__ == "__main__": data = datasets.fetch_haxby(subjects=(2, )) fmri_filename = data.func[0] smoothed_img = image.smooth_img(fmri_filename, 2) smoothed_data = smoothed_img.get_data().transpose(3, 0, 1, 2) #mean = smoothed_data.mean(axis=0) #smoothed_data -= mean #scale = smoothed_data.std(axis=0) + 1e-6 scale = smoothed_data.std() # global scale smoothed_data /= scale smoothed_data = smoothed_data[:, :, :, :, None] input_shape = smoothed_data.shape[1:] smoothed_data_train = smoothed_data[:1200] smoothed_data_test = smoothed_data[1200:] encoder, decoder, autoencoder = make_models(input_shape=input_shape)
""" Show stimuli of Haxby et al. dataset =============================================================================== In this script we plot an overview of the stimuli used in "Distributed and Overlapping Representations of Faces and Objects in Ventral Temporal Cortex" (Science 2001) """ from scipy.misc import imread import matplotlib.pyplot as plt from nilearn.datasets import fetch_haxby stimulus_information = fetch_haxby(n_subjects=0, fetch_stimuli=True).stimuli for stim_type in sorted(stimulus_information.keys()): if stim_type == "controls": # skip control images, there are too many continue file_names = stimulus_information[stim_type] plt.figure() for i in range(48): plt.subplot(6, 8, i + 1) try: plt.imshow(imread(file_names[i])) plt.gray()
if axis is None: axis = 0 array = array.ravel() array = np.rollaxis(array, axis) remaining_shape = array.shape[1:] length = len(array) array = array.reshape(length, -1) x = np.arange(length) x -= x.mean() x /= x.max() regressors = np.array([x ** order for order in range(polyorder + 1)]).T coef, resids, rank, s = lstsq(regressors, array) return coef.reshape([polyorder + 1] + list(remaining_shape)) if __name__ == "__main__": import nibabel as nb from nilearn.datasets import fetch_haxby h = fetch_haxby(n_subjects=1) data = nb.load(h.func[0]).get_data().T skewmap = skewness(data, axis=0) kurtmap = kurtosis(data, axis=0) trendmap = trend_coef(data, polyorder=2, axis=0)
from sklearn.externals.joblib import Memory mem = Memory(cachedir='nilearn_cache') # Coordinates of the selected slice coronal = -24 sagittal = -33 axial = -17 cut_coords = (coronal, sagittal, axial) subject_idx = 1 # ## Load the data ################################################### print("Fetch the data files from Internet") from nilearn import datasets haxby_dataset = datasets.fetch_haxby(n_subjects=subject_idx + 1) print("Second, load the labels") import numpy as np haxby_labels = np.genfromtxt(haxby_dataset.session_target[0], skip_header=1, usecols=[0], dtype=basestring) print np.unique(haxby_labels) # ## Find voxels of interest ################################################# print("Load the data.") anat_filename = haxby_dataset.anat[subject_idx] anat_img = nibabel.load(anat_filename) fmri_filename = haxby_dataset.func[subject_idx]
padding="same"), BatchNormalization(), Conv3D(16, kernel_size=3, activation='relu', padding="same"), BatchNormalization(), Conv3DTranspose(16, kernel_size=3, strides=2, activation='relu', padding="same"), BatchNormalization(), Conv3D(1, kernel_size=3, activation=None, padding="same"), ], name="decoder") autoencoder = Sequential([encoder, decoder], name="autoencoder") return encoder, decoder, autoencoder if __name__ == "__main__": data = datasets.fetch_haxby(subjects=(2,)) fmri_filename = data.func[0] smoothed_img = image.smooth_img(fmri_filename, 2) smoothed_data = smoothed_img.get_data().transpose(3, 0, 1, 2) #mean = smoothed_data.mean(axis=0) #smoothed_data -= mean #scale = smoothed_data.std(axis=0) + 1e-6 scale = smoothed_data.std() # global scale smoothed_data /= scale smoothed_data = smoothed_data[:, :, :, :, None] input_shape = smoothed_data.shape[1:] smoothed_data_train = smoothed_data[:1200] smoothed_data_test = smoothed_data[1200:] encoder, decoder, autoencoder = make_models(input_shape=input_shape)
def boo(subject_idx=0, cut_coords=None, n_components=20, n_clusters=2000, memory='nilearn_cache'): mem = Memory(cachedir='nilearn_cache') # ## Load the data ################################################### print("Fetch the data files from Internet") haxby_dataset = datasets.fetch_haxby(n_subjects=subject_idx + 1) print("Second, load the labels") haxby_labels = np.genfromtxt(haxby_dataset.session_target[0], skip_header=1, usecols=[0], dtype=basestring) # ## Find voxels of interest ############################################## print("Load the data.") anat_filename = haxby_dataset.anat[subject_idx] anat_img = nibabel.load(anat_filename) fmri_filename = haxby_dataset.func[subject_idx] fmri_raw_img = nibabel.load(fmri_filename) print("Build a mask based on the activations.") epi_masker = NiftiMasker(mask_strategy='epi', detrend=True, standardize=True) epi_masker = mem.cache(epi_masker.fit)(fmri_raw_img) plot_roi(epi_masker.mask_img_, bg_img=anat_img, title='EPI mask', cut_coords=cut_coords) print("Normalize the (transformed) data") # zscore per pixel, over examples. fmri_masked_vectors = epi_masker.transform(fmri_raw_img) fmri_normed_vectors = mem.cache(stats.mstats.zscore)(fmri_masked_vectors, axis=0) fmri_normed_img = epi_masker.inverse_transform(fmri_normed_vectors) print("Smooth the (spatial) data.") fmri_smooth_img = mem.cache(image.smooth_img)(fmri_normed_img, fwhm=7) print("Mask the MRI data.") masked_fmri_vectors = mem.cache(epi_masker.transform)(fmri_smooth_img) # ## Compute mean values based on condition matrix ########################################## condition_names = list(np.unique(haxby_labels)) n_conditions = len(condition_names) n_good_voxels = masked_fmri_vectors.shape[1] mean_vectors = np.empty((n_conditions, n_good_voxels)) for ci, condition in enumerate(condition_names): condition_vectors = masked_fmri_vectors[haxby_labels == condition, :] mean_vectors[ci, :] = condition_vectors.mean(axis=0) # ## Use similarity across conditions as the 4th dimension ########################################## n_conds = len(condition_names) n_compares = n_conds * (n_conds - 1) / 2 p_vectors = np.zeros((n_compares, masked_fmri_vectors.shape[1])) comparison_text = [] comparison_img = [] idx = 0 for i, cond in enumerate(condition_names): for j, cond2 in enumerate(condition_names[i+1:]): print("Computing ttest for %s vs. %s." % (cond, cond2)) _, p_vector = stats.ttest_ind( masked_fmri_vectors[haxby_labels == cond, :], masked_fmri_vectors[haxby_labels == cond2, :], axis=0) p_vector /= p_vector.max() # normalize p_vector = -np.log10(p_vector) p_vector[np.isnan(p_vector)] = 0. p_vector[p_vector > 10.] = 10. p_img = epi_masker.inverse_transform(p_vector) comparison_img.append(p_img) comparison_text.append('%s vs. %s' % (cond, cond2)) p_vectors[idx, :] = p_vector idx += 1 #n_comparisons = n_conditions * (n_conditions-1) / 2 #similarity_vectors = np.empty((n_good_voxels, n_comparisons)) #for vi in np.arange(n_good_voxels): # similarity_vectors[vi, :] = pdist(mean_vectors[:, vi]) # Compute a connectivity matrix (for constraining the clustering) mask_data = epi_masker.mask_img_.get_data().astype(np.bool) connectivity = sk_image.grid_to_graph(n_x=mask_data.shape[0], n_y=mask_data.shape[1], n_z=mask_data.shape[2], mask=mask_data) # Cluster (#2) start = time.time() ward = WardAgglomeration(n_clusters=n_clusters, connectivity=connectivity, memory=memory) ward.fit(p_vectors) print("Ward agglomeration %d clusters: %.2fs" % ( n_clusters, time.time() - start)) # Compute an image with one ROI per label, and save to disk labels = ward.labels_ + 1 # Avoid 0 label - 0 means mask. labels_img = epi_masker.inverse_transform(labels) labels_img.to_filename('parcellation.nii') # Plot image with len(labels) ROIs, and store # the cut coordinates to reuse for all plots # and the figure for plotting all to a common axis first_plot = plot_roi(labels_img, title="Ward parcellation", bg_img=anat_img) plt.show()
from sklearn.svm import SVC from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.metrics import confusion_matrix, classification_report from nilearn.plotting import plot_stat_map ##### PARAMETERS TR = 2.5 ##### DIRECTORY BUSINESS ###### # original data directory where haxby2001 directory resides dataDir = '/tmp/Data' ##### DATA FILES # By default 2nd subject will be fetched haxby_dataset = datasets.fetch_haxby(data_dir=dataDir) imgfMRI = haxby_dataset.func[0] # fMRI data file imgAnat = haxby_dataset.anat[0] # structural data file imgMaskVT = haxby_dataset.mask_vt[0] # ventral-temporal streaming mask tableTarget = haxby_dataset.session_target[0] # session target table file ###### EXTRACTING DATA FROM ROI MASK # Masking the image data with the VT mask, extracting voxels masker = NiftiMasker(mask_img=imgMaskVT, standardize=True, detrend=True, high_pass=0.008, t_r=TR) # Extracting the voxel time series within the mask
""" Show stimuli of Haxby et al. dataset =============================================================================== In this script we plot an overview of the stimuli used in "Distributed and Overlapping Representations of Faces and Objects in Ventral Temporal Cortex" (Science 2001) """ from scipy.misc import imread import matplotlib.pyplot as plt from nilearn import datasets from nilearn.plotting import show haxby_dataset = datasets.fetch_haxby(subjects=[], fetch_stimuli=True) stimulus_information = haxby_dataset.stimuli for stim_type in sorted(stimulus_information.keys()): if stim_type == b'controls': # skip control images, there are too many continue file_names = stimulus_information[stim_type] plt.figure() for i in range(48): plt.subplot(6, 8, i + 1) try: plt.imshow(imread(file_names[i]), cmap=plt.cm.gray) except:
""" Decoding with SpaceNet: face vs house object recognition ========================================================= Here is a simple example of decoding with a SpaceNet prior (i.e Graph-Net, TV-l1, etc.), reproducing the Haxby 2001 study on a face vs house discrimination task. See also the SpaceNet documentation: :ref:`space_net`. """ ############################################################################## # Load the Haxby dataset from nilearn.datasets import fetch_haxby data_files = fetch_haxby() # Load Target labels import numpy as np labels = np.recfromcsv(data_files.session_target[0], delimiter=" ") # Restrict to face and house conditions target = labels['labels'] condition_mask = np.logical_or(target == b"face", target == b"house") # Split data into train and test samples, using the chunks condition_mask_train = np.logical_and(condition_mask, labels['chunks'] <= 6) condition_mask_test = np.logical_and(condition_mask, labels['chunks'] > 6) # Apply this sample mask to X (fMRI data) and y (behavioral labels)
'g_distgeo': { 'kind': 'geometric', 'method': 'distance', 'spars': 0.2 }, } verbose = 0 absolute = True geo_alpha = 0.0001 ##### Begin Analysis brainmask = load_mni152_brain_mask() template = load_mni152_template() basc = datasets.fetch_atlas_basc_multiscale_2015(version='sym')['scale444'] haxby = datasets.fetch_haxby(data_dir='D:/', subjects=6) mem = Memory('nilearn_cache') masker = NiftiLabelsMasker(labels_img=basc, mask_img=brainmask, memory=mem, memory_level=1, verbose=0, detrend=True, standardize=True, low_pass=0.1, high_pass=0.01, t_r=2.5, resampling_target='labels') masker.fit() for suj in range(6):
""" Simple example of decoding: the Haxby data ============================================== Here is a simple example of decoding, reproducing the Haxby 2001 study on a face vs house discrimination task in a mask of the ventral stream. """ ### Load haxby dataset ######################################################## from nilearn import datasets data = datasets.fetch_haxby() ### Load Target labels ######################################################## import numpy as np # Load target information as string and give a numerical identifier to each labels = np.recfromcsv(data.session_target[0], delimiter=" ") # scikit-learn >= 0.14 supports text labels. You can replace this line by: # target = labels['labels'] _, target = np.unique(labels['labels'], return_inverse=True) ### Keep only data corresponding to faces or cat ############################## condition_mask = np.logical_or(labels['labels'] == 'face', labels['labels'] == 'cat') target = target[condition_mask] ### Prepare the data: apply the mask ##########################################
import os # fetch haxby dataset from nilearn.datasets import fetch_haxby haxby = fetch_haxby() # print instructions print("To make a local copy of the data for subj2, run:") print("$ cp -a {} .".format(os.path.dirname(haxby.func[0])))
This is a block-design fMRI dataset from a study on face and object\ representation in human ventral temporal cortex. It consists of 6 subjects\ with 12 runs per subject. In each run, the subjects passively viewed \ greyscale images of eight object categories, grouped in 24s blocks separated\ by rest periods. Each image was shown for 500ms and was followed by a 1500ms\ inter-stimulus interval. Full-brain fMRI data were recorded with a volume \ repetition time of 2.5s, thus, a stimulus block was covered by roughly 9 \ volumes. Get full description <a href="http://dev.pymvpa.org/datadb/haxby2001.html">\ here</a>.\ """ # fetch HAXBY dataset N_SUBJECTS = 2 haxby_data = fetch_haxby(n_subjects=N_SUBJECTS) # set output dir OUTPUT_DIR = os.path.join(os.path.dirname(haxby_data.mask), "haxby_runs") if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) # get subject data subjects = [] for subject_id in set([os.path.basename(os.path.dirname(x)) for x in haxby_data.func]): # instantiate subject_data object subject_data = SubjectData() subject_data.subject_id = subject_id subject_data.session_id = "haxby2001"
""" Simple plotting in nilearn ========================== Nilearn comes with a set of plotting function for Nifti-like images, see :ref:`plotting` for more details. """ from nilearn import datasets from nilearn import plotting, image ############################################################################### # Retrieve the data: haxby dataset to have EPI images and masks, and # localizer dataset to have contrast maps haxby_dataset = datasets.fetch_haxby(n_subjects=1) haxby_anat_filename = haxby_dataset.anat[0] haxby_mask_filename = haxby_dataset.mask_vt[0] haxby_func_filename = haxby_dataset.func[0] localizer_dataset = datasets.fetch_localizer_contrasts( ["left vs right button press"], n_subjects=2, get_anats=True, get_tmaps=True) localizer_anat_filename = localizer_dataset.anats[1] localizer_cmap_filename = localizer_dataset.cmaps[1] localizer_tmap_filename = localizer_dataset.tmaps[1] ############################################################################### # demo the different plotting functions
""" Show stimuli of Haxby et al. dataset =============================================================================== In this script we plot an overview of the stimuli used in "Distributed and Overlapping Representations of Faces and Objects in Ventral Temporal Cortex" (Science 2001) """ from scipy.misc import imread import matplotlib.pyplot as plt from nilearn.datasets import fetch_haxby stimulus_information = fetch_haxby(n_subjects=0, fetch_stimuli=True).stimuli for stim_type in sorted(stimulus_information.keys()): if stim_type == "controls": # skip control images, there are too many continue file_names = stimulus_information[stim_type] plt.figure() for i in range(48): plt.subplot(6, 8, i + 1) try: plt.imshow(imread(file_names[i]), cmap=plt.cm.gray) except: # just go to the next one if the file is not present pass
Example showing how a T-test can be performed to compute an ROI mask, and how simple operations can improve the quality of the mask obtained. """ ### Coordinates of the selected slice ######################################### coronal = 27 sagittal = 21 axial = 26 ### Load the data ############################################################# # Fetch the data files from Internet from nilearn import datasets haxby_files = datasets.fetch_haxby(n_subjects=1) # First load the fMRI data import nibabel fmri_img = nibabel.load(haxby_files.func[0]) fmri_data = fmri_img.get_data() fmri_affine = fmri_img.get_affine() # Second, load the labels import numpy as np labels = np.genfromtxt(haxby_files.session_target[0], skip_header=1, usecols=[0], dtype=basestring) ### Visualization function ####################################################
import numpy as np import pandas as pd from nilearn.datasets import fetch_haxby from nilearn.input_data import NiftiMasker from kmapper import KeplerMapper, Cover from sklearn.manifold import TSNE from sklearn.cluster import DBSCAN from dyneusr import DyNeuGraph from dyneusr.tools import visualize_mapper_stages # Fetch dataset, extract time-series from ventral temporal (VT) mask dataset = fetch_haxby() masker = NiftiMasker( dataset.mask_vt[0], standardize=True, detrend=True, smoothing_fwhm=4.0, low_pass=0.09, high_pass=0.008, t_r=2.5, memory="nilearn_cache" ) X = masker.fit_transform(dataset.func[0]) # Encode labels as integers df = pd.read_csv(dataset.session_target[0], sep=" ") target, labels = pd.factorize(df.labels.values) y = pd.DataFrame({l:(target==i).astype(int) for i,l in enumerate(labels)}) # Generate shape graph using KeplerMapper mapper = KeplerMapper(verbose=1)
mask, and how simple operations can improve the quality of the mask obtained. """ ### Coordinates of the selected slice ######################################### coronal = -24 sagittal = -33 axial = -17 cut_coords = (coronal, sagittal, axial) ### Load the data ############################################################# # Fetch the data files from Internet from nilearn import datasets import nibabel haxby_dataset = datasets.fetch_haxby(n_subjects=1) # print basic information on the dataset print('First subject anatomical nifti image (3D) located is at: %s' % haxby_dataset.anat[0]) print('First subject functional nifti image (4D) is located at: %s' % haxby_dataset.func[0]) # Second, load the labels import numpy as np session_target = np.recfromcsv(haxby_dataset.session_target[0], delimiter=" ") haxby_labels = session_target['labels'] ### Visualization function ####################################################
""" Show stimuli of Haxby et al. dataset =============================================================================== In this script we plot an overview of the stimuli used in "Distributed and Overlapping Representations of Faces and Objects in Ventral Temporal Cortex" (Science 2001) """ import matplotlib.pyplot as plt from nilearn import datasets from nilearn.plotting import show haxby_dataset = datasets.fetch_haxby(subjects=[], fetch_stimuli=True) stimulus_information = haxby_dataset.stimuli for stim_type in stimulus_information: # skip control images, there are too many if stim_type != 'controls': file_names = stimulus_information[stim_type] fig, axes = plt.subplots(6, 8) fig.suptitle(stim_type) for img_path, ax in zip(file_names, axes.ravel()): ax.imshow(plt.imread(img_path), cmap=plt.cm.gray) for ax in axes.ravel(): ax.axis("off")
def _crop_mask(mask): """Crops input mask to produce tighter (i.e smaller) bounding box with the same support (active voxels)""" idx = np.where(mask) i_min = max(idx[0].min() - 1, 0) i_max = idx[0].max() j_min = max(idx[1].min() - 1, 0) j_max = idx[1].max() k_min = max(idx[2].min() - 1, 0) k_max = idx[2].max() return mask[i_min:i_max + 1, j_min:j_max + 1, k_min:k_max + 1] ### Load haxby dataset ######################################################## from nilearn.datasets import fetch_haxby data_files = fetch_haxby('/home/eugene/Documents/') ### Load Target labels ######################################################## import numpy as np labels = np.recfromcsv(data_files.session_target[0], delimiter=" ") ### split data into train and test samples #################################### n_train=6 target = labels['labels'] condition_mask = np.logical_or(target == "scissors", target == "scrambledpix") condition_mask_train = np.logical_and(condition_mask, labels['chunks'] <= n_train) condition_mask_test = np.logical_and(condition_mask, labels['chunks'] > n_train) # make X (design matrix) and y (response variable) import nibabel
""" NeuroImaging volumes visualization ================================== Simple example to show Nifti data visualization. """ ############################################################################## # Fetch data from nilearn import datasets # By default 2nd subject will be fetched haxby_dataset = datasets.fetch_haxby() # print basic information on the dataset print('First anatomical nifti image (3D) located is at: %s' % haxby_dataset.anat[0]) print('First functional nifti image (4D) is located at: %s' % haxby_dataset.func[0]) ############################################################################## # Visualization from nilearn.image.image import mean_img # Compute the mean EPI: we do the mean along the axis 3, which is time func_filename = haxby_dataset.func[0] mean_haxby = mean_img(func_filename) from nilearn.plotting import plot_epi, show plot_epi(mean_haxby)
def boo(subject_idx=0, cut_coords=None): mem = Memory(cachedir='nilearn_cache') # ## Load the data ################################################### print("Fetch the data files from Internet") haxby_dataset = datasets.fetch_haxby(n_subjects=subject_idx + 1) print("Second, load the labels") haxby_labels = np.genfromtxt(haxby_dataset.session_target[0], skip_header=1, usecols=[0], dtype=basestring) # ## Find voxels of interest ############################################## print("Load the data.") anat_filename = haxby_dataset.anat[subject_idx] anat_img = nibabel.load(anat_filename) fmri_filename = haxby_dataset.func[subject_idx] fmri_raw_img = nibabel.load(fmri_filename) shared_affine = fmri_raw_img.get_affine() print("Build a mask based on the activations.") epi_masker = NiftiMasker(mask_strategy='epi', detrend=True, standardize=True) epi_masker = mem.cache(epi_masker.fit)(fmri_raw_img) plot_roi(epi_masker.mask_img_, title='EPI mask', cut_coords=cut_coords) from nipy.labs.viz import plot_map # plot_map(epi_masker.mask_img_.get_data(), epi_masker.mask_img_.get_affine()) # plt.show() # exit() #print("Normalize the (transformed) data") # zscore per pixel, over examples. #fmri_masked_vectors = epi_masker.transform(fmri_raw_img) #fmri_normed_vectors = mem.cache(stats.mstats.zscore)(fmri_masked_vectors, axis=0) fmri_normed_img = fmri_raw_img #epi_masker.inverse_transform(fmri_normed_vectors) print("Smooth the (spatial) data.") fmri_smooth_img = mem.cache(image.smooth_img)(fmri_normed_img, fwhm=1) print("Mask the MRI data.") masked_fmri_vectors = mem.cache(epi_masker.transform)(fmri_smooth_img) fmri_masked_img = epi_masker.inverse_transform(masked_fmri_vectors) # ## Compute a similarity matrix ########################################## condition_names = list(np.unique(haxby_labels)) n_cond_img = (haxby_labels == condition_names[0]).sum() n_conds = len(condition_names) n_compares = n_conds * (n_conds - 1) / 2 p_vectors = np.zeros((n_compares, masked_fmri_vectors.shape[1])) idx = 0 for i, cond in enumerate(condition_names): for j, cond2 in enumerate(condition_names[i+1:]): print("Computing ttest for %s vs. %s." % (cond, cond2)) _, p_vectors[idx, :] = stats.ttest_ind( masked_fmri_vectors[haxby_labels == cond, :], masked_fmri_vectors[haxby_labels == cond2, :], axis=0) idx += 1 p_vectors_normd = p_vectors / p_vectors.max(axis=0) log_p_vectors = -np.log10(p_vectors) log_p_vectors[np.isnan(log_p_vectors)] = 0. log_p_vectors[log_p_vectors > 10.] = 10. #log_p_normd_vectors = log_p_vectors / log_p_vectors.sum(axis=0) plt.figure(); plt.hist(p_vectors_normd.max(axis=0), 100); plt.show() idx = 0 for i, cond in enumerate(condition_names): for j, cond2 in enumerate(condition_names[i+1:]): if cond != 'face' and cond2 != 'face': continue print("Plotting compares for %s vs. %s." % (cond, cond2)) log_p_img = epi_masker.inverse_transform(1/p_vectors[idx, :]) log_p_normd_img = epi_masker.inverse_transform(1. - p_vectors_normd[idx, :]) plot_two_maps(plot_stat_map, (log_p_img, "%s vs. %s." % (cond, cond2)), (log_p_normd_img, "%s vs. %s. (norm'd)" % (cond, cond2)), bg_img=anat_img) import pdb; pdb.set_trace() plt.show() idx += 1