def make_masker(scheme): ''' Parameters ---------- scheme : String The type of parcellation wanted. Returns ------- masker: nilearn.input_data.NiftiLabelsMasker Masker of the chosen scheme. labels: list Labels of all the regions in parcellation. ''' if scheme.lower() == "harvox": # 48 regions dataset = datasets.fetch_atlas_harvard_oxford('cort-maxprob-thr25-2mm') atlas_filename = dataset.maps labels = dataset.labels[1:] # trim off "background" label masker = NiftiLabelsMasker(labels_img=atlas_filename, standardize=True, high_variance_confounds=True, verbose=1) elif scheme.lower() == "yeo": # 17 regions dataset = datasets.fetch_atlas_yeo_2011() masker = NiftiLabelsMasker(labels_img=dataset['thick_17'], standardize=True, high_variance_confounds=True, verbose=1) labels = [ "Visual A", "Visual B", "Somatomotor A", "Somatomotor B", "Dorsal Attention A", "Dorsal Attention B", "Salience/Ventral Attention A", "Salience/Ventral Attention B", "Limbic A", "Limbic B", "Control C", "Control A", "Control B", "Temporal Parietal", "Default C", "Default A", "Default B" ] # list from valerie-jzr elif scheme.lower() == "aal": # 116 regions dataset = datasets.fetch_atlas_aal(version='SPM12') labels = dataset['labels'] masker = NiftiLabelsMasker(labels_img=dataset['maps'], standardize=True, high_variance_confounds=True, verbose=1) elif scheme.lower() == "schaefer": dataset = datasets.fetch_atlas_schaefer_2018(n_rois=100, yeo_networks=17) labels = dataset['labels'] masker = NiftiLabelsMasker(labels_img=dataset['maps'], standardize=True, high_variance_confounds=True, verbose=1) return masker, labels
def prep_data(n, mat_fname): # which Schaefer parcellation fetch and save to in_data atlas_info = datasets.fetch_atlas_schaefer_2018(n_rois=n, yeo_networks=7, resolution_mm=2, data_dir=os.getcwd() + '/in_data/', base_url=None, resume=True, verbose=1) # load the parcellation parc = nib.load(atlas_info['maps']) globe.PARCELLATION = parc.get_fdata() globe.ATLAS = parc.get_fdata() globe.AFFINE = parc.affine globe.HEADER = parc.header globe.LABELS = atlas_info['labels'] # create a dictionary from labels provided by Schaefer atlas globe.LABEL_DICT = {} # key: region index, value: dictionary of label info for id_, label in enumerate(globe.LABELS): label_parts = str(label).strip("b'").split('_') if len(label_parts) == 4: globe.LABEL_DICT[id_] = { 'Hemisphere': label_parts[1], 'Network': label_parts[2], 'Partition': label_parts[3] } elif len(label_parts) == 5: globe.LABEL_DICT[id_] = { 'Hemisphere': label_parts[1], 'Network': label_parts[2], 'Partition': label_parts[4] } else: print('Error in FC function.') # full functional conn data mat = hdf5storage.loadmat(mat_fname, variable_names=['Vp_clean']) N = int((n / 100) + 2) conn = mat['Vp_clean'][0, N] del mat # normalize by row conn_mean = np.mean(conn, axis=1) conn_mean = np.reshape(conn_mean, (conn_mean.shape[0], 1)) globe.CONN_NORM = (conn - conn_mean) / conn_mean
def main(argv): ## atlas 3 atlas = "Schaefer200Yeo17Pauli" # msdl or haox or mmp schaefer_atlas = datasets.fetch_atlas_schaefer_2018(n_rois=200, yeo_networks=17, resolution_mm=1, data_dir=None, base_url=None, resume=True, verbose=1) #atlas_filename = "MMP1_rois.nii" #Glasser et al., 2016 schaefer_filename = schaefer_atlas.maps schaefer_labels = schaefer_atlas.labels schaefer_masker = NiftiLabelsMasker(labels_img=schaefer_filename, standardize=True, memory='nilearn_cache', verbose=5) pauli_atlas = datasets.fetch_atlas_pauli_2017() pauli_filename = pauli_atlas.maps pauli_labels = pauli_atlas.labels pauli_masker = NiftiMapsMasker(maps_img=pauli_filename, standardize=True, verbose=5) all_labels = np.hstack([schaefer_labels, pauli_labels]) #print(all_labels) correlation_measure = ConnectivityMeasure(kind='correlation')
def fetch_resample_schaeffer(mask, scale="444"): from nilearn.datasets import fetch_atlas_schaefer_2018 from nilearn.image import resample_to_img atlas = fetch_atlas_schaefer_2018(n_rois=scale, resolution_mm=2)["maps"] resampled_atlas = resample_to_img(atlas, mask, interpolation='nearest') return resampled_atlas
def get_labelled_atlas(query, data_dir=None, return_labels=True): """Parses input query to determine which atlas to fetch and what version of the atlas to use (if applicable). Parameters ---------- query : str Input string in the following format: nilearn:{atlas_name}:{atlas_parameters}. The following can be for `atlas_name`: 'destrieux', 'yeo', 'aal', 'talairach', and 'schaefer'. `atlas_parameters` is not available for the `destrieux` atlas. data_dir : str, optional Directory in which to save atlas data. By default None, which creates a ~/nilearn_data/ directory as per nilearn. return_labels : bool, optional Whether to return atlas labels. Default is True. Not available for the 'basc' atlas. Returns ------- str, list or None The atlas image and the accompanying labels (if provided) Raises ------ ValueError Raised when the query does is not formatted correctly or if the no match found. """ # extract parameters params = query.split(':') if len(params) == 3: _, atlas_name, sub_param = params elif len(params) == 2: _, atlas_name = params sub_param = None else: raise ValueError('Incorrect atlas query string provided') # get atlas if atlas_name == 'destrieux': atlas = fetch_atlas_destrieux_2009(lateralized=True, data_dir=data_dir) img = atlas['maps'] labels = atlas['labels'] elif atlas_name == 'yeo': atlas = fetch_atlas_yeo_2011(data_dir=data_dir) img = atlas[sub_param] if '17' in sub_param: labels = pd.read_csv(atlas['colors_17'], sep=r'\s+')['NONE'].tolist() elif atlas_name == 'aal': version = 'SPM12' if sub_param is None else sub_param atlas = fetch_atlas_aal(version=version, data_dir=data_dir) img = atlas['maps'] labels = atlas['labels'] elif atlas_name == 'basc': version, scale = sub_param.split('-') atlas = fetch_atlas_basc_multiscale_2015(version=version, data_dir=data_dir) img = atlas['scale{}'.format(scale.zfill(3))] labels = None elif atlas_name == 'talairach': atlas = fetch_atlas_talairach(level_name=sub_param, data_dir=data_dir) img = atlas['maps'] labels = atlas['labels'] elif atlas_name == 'schaefer': n_rois, networks, resolution = sub_param.split('-') # corrected version of schaefer labels until fixed in nilearn correct_url = ('https://raw.githubusercontent.com/ThomasYeoLab/CBIG/' 'v0.14.3-Update_Yeo2011_Schaefer2018_labelname/' 'stable_projects/brain_parcellation/' 'Schaefer2018_LocalGlobal/Parcellations/MNI/') atlas = fetch_atlas_schaefer_2018(n_rois=int(n_rois), yeo_networks=int(networks), resolution_mm=int(resolution), data_dir=data_dir, base_url=correct_url) img = atlas['maps'] labels = atlas['labels'] else: raise ValueError('No atlas detected. Check query string') if not return_labels: labels = None else: labels = labels.astype(str).tolist() return img, labels
import numpy as np import pandas as pd from nilearn import image as nlimg from nilearn import datasets as nldatasets import nilearn.plotting as nlplt from sys import argv import os if (len(argv) != 3): print('Enter Subject ID') os._exit(0) from nilearn import datasets dataset = datasets.fetch_atlas_schaefer_2018() # dataset = datasets.fetch_atlas_harvard_oxford('sub-maxprob-thr50-1mm') atlas_filename = dataset.maps labels = dataset.labels print(len(labels)) from nilearn.input_data import NiftiLabelsMasker masker = NiftiLabelsMasker(labels_img=atlas_filename, standardize=True) suffix = "" outputsuffix = "" if argv[2] == '1': suffix = "/rfMRI_REST1_LR/rfMRI_REST1_LR_hp2000_clean.nii.gz" outputsuffix = "/rfMRI_REST1_LR/SCHAEFER" else: suffix = "/rfMRI_REST2_LR/rfMRI_REST2_LR_hp2000_clean.nii.gz"
import nibabel as nib from nilearn import datasets from nilearn import plotting import matplotlib.pyplot as plt import pandas as pd import sys from nilearn.connectome import ConnectivityMeasure from srm import SRM_V2, SRM_V3 idx = int(sys.argv[1]) n_rois = 200 net = 17 vox_size = 2 atlas = datasets.fetch_atlas_schaefer_2018(n_rois, net, vox_size) atlas_filename = atlas.maps atlas_pd = pd.DataFrame(atlas) hrf = 4 # run 1 times song_bounds1 = np.array([ 0, 225, 314, 494, 628, 718, 898, 1032, 1122, 1301, 1436, 1660, 1749, 1973, 2198, 2377, 2511 ]) songs1 = [ 'Finlandia', 'Blue_Monk', 'I_Love_Music', 'Waltz_of_Flowers', 'Capriccio_Espagnole', 'Island', 'All_Blues', 'St_Pauls_Suite',
confounds_directory = "/mnt/chrastil/lab/users/rob/projects/MachineLearning/confounds/" timeseries_directory = "/mnt/chrastil/lab/users/rob/projects/DynConn/timeseries" array_directory = "/mnt/chrastil/lab/users/rob/projects/DynConn/3DArrays" pattern = "Ex" scan_num = 2 threshold = 0.8 window_step = 130 window_size = 150 kind = "coherence" # HO + Schaeffer Atases output_label = "HOsubcort+Schaefer400Coh_2mm" ts_pattern = "*Ex*HOsubcort+Schaefer400Coh_2mm*" schaefer = datasets.fetch_atlas_schaefer_2018(resolution_mm=2, verbose=0) schaefer_atlas = schaefer.maps schaefer_labels = schaefer.labels ho = datasets.fetch_atlas_harvard_oxford("sub-maxprob-thr25-2mm", verbose=0) ho_atlas = ho.maps ho_labels = ho.labels atlas_dict = {schaefer_atlas: "Labels", ho_atlas: "Labels"} # Load in the roi labels f = open("/mnt/chrastil/lab/users/rob/projects/DynConn/bc_labels.txt", 'rb') roi_names = pickle.load(f) f.close() # Code to execute w/ above parameters
##### Remove the warnings to keep notebook cleaner ##### import warnings warnings.filterwarnings('ignore') ##### Fetching Brain Parcellations ##### #Fetching Schaefer 400-Parcellations from nilearn import datasets work_dir = '/Volumes/Harddrive_CTS/MRI/Placebo_1_Analysis/' schaefer_parcellations = datasets.fetch_atlas_schaefer_2018(n_rois = 400, yeo_networks = 7, data_dir = work_dir) labels_schaefer = schaefer_parcellations.labels atlas_schaefer = schaefer_parcellations.maps print('Schaefer Atlas ROIs are located in nifti image (4D) at: %s' % atlas_schaefer) #Fetching Yeo 7-Network yeo_parcellations = datasets.fetch_atlas_yeo_2011(data_dir = work_dir, verbose = 1) atlas_yeo = yeo_parcellations.thin_7 print('Yeo Atlas ROIs are located in nifti image (4D) at: %s' % atlas_yeo) #Fetching Power 264-parcellations power = nilearn.datasets.fetch_coords_power_2011() print('Power atlas comes with {0}.'.format(power.keys())) #Compute spheres of fixed radius of sequence (x,y,z) import numpy as np power_coords = np.vstack((power.rois['x'], power.rois['y'], power.rois['z'])).T print('Stacked power coordinates in array of shape {0}.'.format(power_coords.shape)) #Plot Atlases %matplotlib gtk import matplotlib.pyplot as plt
def main(argv): ## this is input parsing try: opts, args = getopt.getopt(argv, "hi:", ["ifile="]) except getopt.GetoptError: print('single_subject_hitting_time3.py -i <sub_data>') sys.exit(2) for opt, arg in opts: if opt == '-h': print('single_subject_hitting_time3.py -i <sub_data>') sys.exit() elif opt in ("-i", "--ifile"): sub_data = arg print('sub_data is "', sub_data) ## atlas 3 atlas = "Schaefer200Yeo17Pauli" # msdl or haox or mmp schaefer_atlas = datasets.fetch_atlas_schaefer_2018( n_rois=200, yeo_networks=17, resolution_mm=1, data_dir=None, base_url=None, resume=True, verbose=1) #atlas_filename = "MMP1_rois.nii" #Glasser et al., 2016 schaefer_filename = schaefer_atlas.maps schaefer_labels = schaefer_atlas.labels schaefer_masker = NiftiLabelsMasker(labels_img=schaefer_filename, standardize=True, memory='nilearn_cache', verbose=5) pauli_atlas = datasets.fetch_atlas_pauli_2017() pauli_filename = pauli_atlas.maps pauli_labels = pauli_atlas.labels pauli_masker = NiftiMapsMasker(maps_img=pauli_filename, standardize=True, verbose=5) all_labels = np.hstack([schaefer_labels, pauli_labels]) print(all_labels) correlation_measure = ConnectivityMeasure(kind='correlation') #n_rois=len(schaefer_labels) + len(pauli_labels) #p_corr_all = np.zeros([n_rois, n_rois,len(sub_data)]) #H_all = np.zeros([n_rois, n_rois,len(sub_data)]) # generate subject number and position from sub_data subnum = sub_data.split(os.sep)[-4] #subnum_fmt = "{:06}".format(int(subnum)) out_base = os.sep.join(sub_data.split(os.sep)[:-3]) out_dir = out_base + os.sep + "deriv" + os.sep + "snag" if not os.path.exists(out_dir): os.makedirs(out_dir) #extract time series from a atlas(es) file_base = "_".join(sub_data.split(os.sep)[-1].split("_")[:-2]) adj_out_file = out_dir + os.sep + file_base + "_timeseries-corr_" + atlas + "_data_filt" if not (pathlib.Path(adj_out_file).exists()): #func_dir_in = sub_data #+ os.sep + 'restEPI' #directory with func images #funcs_filenames = glob.glob(func_dir_in + os.sep + '*.nii') #find all funcs in this directory confounds = high_variance_confounds(sub_data) #schafer cortical atlas schaefer_time_series = schaefer_masker.fit_transform( sub_data, confounds=confounds) #cortical segments print("schaefer ts shape: ") print(schaefer_time_series.shape) #subcortical atlas pauli_time_series = pauli_masker.fit_transform( sub_data, confounds=confounds) #subccortical segments print("pauli ts shape: ") print(pauli_time_series.shape) #stack time series and determine adjacency matrix from the resulting set of time series full_ts_set = np.hstack((schaefer_time_series, pauli_time_series)) print("concatenated ts shape: ") print(full_ts_set.shape) correlation_matrix = correlation_measure.fit_transform([full_ts_set ])[0] np.savetxt(adj_out_file, correlation_matrix, delimiter=",") print(correlation_matrix.shape[0], correlation_matrix.shape[1]) else: correlation_matrix = genfromtxt( adj_out_file, delimiter=',' ) #load the file if the correlation matrix was pre-computed correlation_matrix = abs( correlation_matrix ) #absolute value to make all transition probabilities positive np.fill_diagonal(correlation_matrix, 0) #set self connections to zero #p_corr_all[:,:,sub_ind] = correlation_matrix.copy() #stack correlation matrices for later analysis #build hitting time matrix H_out_file = out_dir + os.sep + file_base + "_normedH_" + atlas + "_corr" #file where hitting-time matrix will be saved print(H_out_file) if not (pathlib.Path(H_out_file).exists() ): #compute hitting time matrix if it isn't already saved H = hitting_matrix(correlation_matrix) #H_all[:,:,sub_ind] = H np.savetxt(H_out_file, H, delimiter=",") print("saved " + H_out_file)
datum = np.concatenate((data, data_squared), axis=1) # shape (121, 24) df_mp1 = pd.DataFrame(columns=np.arange(24), data=datum) # df shape (121, 24) # df_mp1.to_excel("C:\\sexualorientproject\\MC_Parameter\\second\\{}".format(MP[42:])) df_mp1.to_excel("{}".format(MP[42:])) ########################### ### Preparing masking ##### ########################### print("Masking") # define masker atlas = ds.fetch_atlas_schaefer_2018(n_rois=100, yeo_networks=7, resolution_mm=2, data_dir=None, base_url=None, resume=True, verbose=1) first_nii = nib.load(df.fMRI_path.values[0]) cur_ratlas_nii = resample_img( atlas.maps, target_affine=first_nii.affine, interpolation='nearest') masker = NiftiLabelsMasker(labels_img=cur_ratlas_nii, standardize=False) # standardization done later in the loop masker.fit() ################################### ### Preprocessing per subject ##### ################################### # extract TS per ROI, preprocess and compute correlation matrix # loop through 86 subjects print("Starting the ROI time series extraction")
from nilearn.input_data import NiftiLabelsMasker #To mask the data from nilearn.connectome import ConnectivityMeasure #To compute the connectivity matrices import pandas as pd #For dataframe manipulation (e.g. confound file) from matplotlib import pyplot as plt #Used to bypass a bug where the figures wouldn't close in Nilearn import numpy as np #To convert our confounds to numpy array import os #To create directories in the loop ### #Import the necessary atlases for the current processing. Chose the atlases you need ## An overview of available atlases is available here: https://nilearn.github.io/modules/reference.html#module-nilearn.datasets ## For the purpose of the present analyses, we chose the Schaefer Atlas with 200 brain regions. #This downloads the atlas #From the download, we first extract the 'maps' (i.e. the .nii image representing the atlas regions) and the we extract the labels (i.e. what each region is) atlas_schaefer = datasets.fetch_atlas_schaefer_2018(n_rois=200) atlas_filename_schaefer = atlas_schaefer.maps labels_schaefer = atlas_schaefer.labels print(f'The atlas is located at {atlas_filename_schaefer}') #print(labels_schaefer) #Prints the array of the labels #print(len(labels_schaefer)) #Prints the length of the labels #Should you need, you can plot the atlas representation here: #plotting.plot_roi(atlas_filename_schaefer) #Plotting the regions included in the atlas ### # Prepare to extract the subjects. To facilitate loops, we first: # 1) Set subjects location # 2) Set root location for derivatives (or creates it if the script is run for the first time) # 3) Set the variables for the iterations and confounds # 4) Start the loop