def extract_data(images, masks): """ given a set of brain images and a set of masks, extract the average signal inside each mask for each brain image. Returns a dataframe with 3 columns: image, mask, value. """ masker = NiftiMapsMasker(masks) values = masker.fit_transform(images) nimgs, nmasks = values.shape cp = op.commonpath(images) labelsimages = [i.replace(cp, '') for i in images] print(cp) print(labelsimages) cpmask = op.commonprefix(masks) labelsrois = [i.replace(cpmask, '').replace('.nii.gz', '') for i in masks] print(cpmask) print(labelsrois) df = pd.DataFrame(columns=['image', 'mask', 'value']) row = 0 for iimg in range(nimgs): for iroi in range(nmasks): df.loc[row] = pd.Series({ 'image': labelsimages[iimg], 'mask': labelsrois[iroi], 'value': values[iimg, iroi] }) row = row + 1 return df
def extract_from_masker(dataset_like,dataset_mask, funci, timer =True): tic = time.clock() maps_img = dict_to_list(dataset_like) #add mask, smoothing, filter and detrending print 'Nifti' masker = NiftiMapsMasker(maps_img=maps_img, mask_img = dataset_mask, low_pass = .1, high_pass = .01, smoothing_fwhm =6., t_r = 1.05, detrend = True, standardize = False, resampling_target ='data', memory_level = 0, verbose=5) #extract signal to x print 'masker' x = masker.fit_transform(funci) if timer: print time.clock() - tic return x
def extract_rois_signals(preprocessing_folder ='pipeline_2', prefix= 'resampled_wr'): dataset = load_dynacomp(preprocessing_folder = preprocessing_folder,prefix = prefix) for idx, func in enumerate([dataset.func1, dataset.func2]): for i in range(len(dataset.subjects)): tic = time.clock() print func[i] output_path, _ = os.path.split(func[i]) print dataset.subjects[i] maps_img = dict_to_list(dataset.rois[i]) #add mask, smoothing, filter and detrending print 'Nifti' masker = NiftiMapsMasker(maps_img=maps_img, mask_img = dataset.mask, low_pass = .1, high_pass = .01, smoothing_fwhm =6., t_r = 1.05, detrend = True, standardize = False, resampling_target ='data', memory_level = 0, verbose=5) #extract signal to x print 'masker' x = masker.fit_transform(func[i]) print x np.save(os.path.join(PATH_TO_SAVE_DATA,'output' + str(i+1) +'_rois_filter'),x) print time.clock() - tic return x
def extract_one_signal(dataset): for idx, func in enumerate([dataset.func1, dataset.func2]): for i in range(len(dataset.subjects)): tic = time.clock() #maps_img = dict_to_list(func) #add mask, smoothing, filter and detrending maps_img = dict_to_list(dataset.rois[i]) masker = NiftiMapsMasker(maps_img=maps_img, mask_img = dataset.mask, low_pass = .1, high_pass = .01, smoothing_fwhm =6., t_r = 1.05, detrend = True, standardize = False, resampling_target ='data', memory_level = 0, verbose=5) #extract signal to x x = masker.fit_transform(func[i]) print "loading time : "+ str(time.clock() - tic) return x,maps_img
def get_data_in_roi(path_roi, data_file): """Using of the NiftiMapsMasker """ masker = NiftiMapsMasker([path_roi]) array_datas = np.zeros((len(data_file), 177)) for e, d in enumerate(data_file): nifti_obj = nibabel.load(d) data = masker.fit_transform(d) array_datas[e-1] = data.ravel() return array_datas
def get_data_in_roi(path_roi, data_file): """Using of the NiftiMapsMasker """ masker = NiftiMapsMasker([path_roi]) array_datas = np.zeros((len(data_file), 177)) for e, d in enumerate(data_file): nifti_obj = nibabel.load(d) data = masker.fit_transform(d) array_datas[e - 1] = data.ravel() return array_datas
def extract_parcellation_time_series(in_data, parcellation_name, parcellations_dict, bp_freqs, tr): ''' Depending on parcellation['is_probabilistic'] this function chooses either NiftiLabelsMasker or NiftiMapsMasker to extract the time series of each parcel if bp_freq: data is band passfiltered at (hp, lp), if (None,None): no filter, if (None, .1) only lp... tr in ms (e.g. from freesurfer ImageInfo()) returns np.array with parcellation time series and saves this array also to parcellation_time_series_file, and path to pickled masker object ''' from nilearn.input_data import NiftiLabelsMasker, NiftiMapsMasker, NiftiSpheresMasker import os, pickle import numpy as np if parcellations_dict[parcellation_name][ 'is_probabilistic'] == True: # use probab. nilearn masker = NiftiMapsMasker( maps_img=parcellations_dict[parcellation_name]['nii_path'], standardize=True) elif parcellations_dict[parcellation_name]['is_probabilistic'] == 'sphere': atlas = pickle.load( open(parcellations_dict[parcellation_name]['nii_path'])) coords = atlas.rois masker = NiftiSpheresMasker(coords, radius=5, allow_overlap=True, standardize=True) else: # 0/1 labels masker = NiftiLabelsMasker( labels_img=parcellations_dict[parcellation_name]['nii_path'], standardize=True) # add bandpass filter (only executes if freq not None hp, lp = bp_freqs masker.low_pass = lp masker.high_pass = hp if tr is not None: masker.t_r = tr else: masker.t_r = None masker.standardize = True masker_file = os.path.join(os.getcwd(), 'masker.pkl') with open(masker_file, 'w') as f: pickle.dump(masker, f) parcellation_time_series = masker.fit_transform(in_data) parcellation_time_series_file = os.path.join( os.getcwd(), 'parcellation_time_series.npy') np.save(parcellation_time_series_file, parcellation_time_series) return parcellation_time_series, parcellation_time_series_file, masker_file
def extract_timeseries_probabilistic(filename, maps, confounds=None): """Because the power parcellation is given in coordinates and not labels, we dedicate an exclusive function to deal with it. """ maps_masker = NiftiMapsMasker(maps, resampling_target="data", standardize=True) time_series = maps_masker.fit_transform(filename, confounds=confounds) return time_series
def test_nifti_maps_masker_report_image_in_fit(niftimapsmasker_inputs): """""" masker = NiftiMapsMasker(**niftimapsmasker_inputs) image, _ = generate_random_img((13, 11, 12), affine=np.eye(4), length=3) masker.fit(image) html = masker.generate_report(2) assert masker._report_content['report_id'] == 0 assert masker._report_content['number_of_maps'] == 9 assert masker._report_content['warning_message'] is None assert html.body.count("<img") == 2
def spatial_correlations(mm, atlas=None): if atlas is None: from nilearn.datasets import fetch_atlas_msdl atlas = fetch_atlas_msdl() from nilearn.input_data import NiftiMapsMasker masker = NiftiMapsMasker(maps_img=atlas['maps']) rsns_masked = masker.fit_transform(atlas['maps']) mm_masked = masker.fit_transform([mm]) cc = np.corrcoef(mm_masked, rsns_masked) return cc
def run_mini_pipeline(): atlas = datasets.fetch_atlas_msdl() atlas_img = atlas['maps'] labels = pd.read_csv(atlas['labels'])['name'] masker = NiftiMapsMasker(maps_img=atlas_img, standardize=True, memory='/tmp/nilearn', verbose=0) data = datasets.fetch_adhd(number_subjects) figures_folder = '../figures/' count=0 for func_file, confound_file in zip(data.func, data.confounds): # fit the data to the atlas mask, regress out confounds time_series = masker.fit_transform(func_file, confounds=confound_file) correlation = np.corrcoef(time_series.T) #plotting starts here plt.figure(figsize=(10, 10)) plt.imshow(correlation, interpolation="nearest") x_ticks = plt.xticks(range(len(labels)), labels, rotation=90) y_ticks = plt.yticks(range(len(labels)), labels) corr_file = figures_folder+'subject_number_' + str(count) + '_correlation.pdf' plt.savefig(corr_file) atlas_region_coords = [plotting.find_xyz_cut_coords(img) for img in image.iter_img(atlas_img)] threshold = 0.6 plotting.plot_connectome(correlation, atlas_region_coords, edge_threshold=threshold) connectome_file = figures_folder+'subject_number_' + str(count) + '_connectome.pdf' plt.savefig(connectome_file) #graph setup #binarize correlation matrix correlation[correlation<threshold] = 0 correlation[correlation != 0] = 1 graph = nx.from_numpy_matrix(correlation) partition=louvain.best_partition(graph) values = [partition.get(node) for node in graph.nodes()] plt.figure() nx.draw_spring(graph, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=True) graph_file = figures_folder+'subject_number_' + str(count) + '_community.pdf' plt.savefig(graph_file) count += 1 plt.close('all')
def get_data_in_rois_method2(ROIs, subjects, contrasts, condir, localizerf, threshold): """ returns, for individual subjects, the average contrasts values in ROIs masked by individual localizers, thresholded at a fixed theshold""" values = np.zeros((len(subjects), len(contrasts), len(ROIs))) for isub, sub in enumerate(subjects): conlist = [op.join(sub, condir, x) for x in contrasts] localizer_img = nibabel.load(op.join(sub, localizerf)) locmask = binarize_img(localizer_img, threshold) masker = NiftiMapsMasker(ROIs, locmask) values[isub, :] = masker.fit_transform(conlist) return values
def get_data_in_rois_method1(ROIs, subjects, contrasts, condir): """ returns the average contratst in each ROI and for each subject """ masker = NiftiMapsMasker(ROIs) print ROIs values = np.zeros((len(subjects), len(contrasts), len(ROIs))) for isub, sub in enumerate(subjects): conlist = [op.join(sub, condir, x) for x in contrasts] print conlist res = masker.fit_transform(conlist) values[isub, :] = masker.fit_transform(conlist) #print values return values
def test_nifti_maps_masker_report_list_and_arrays_maps_number( niftimapsmasker_inputs, displayed_maps): """Tests report generation for NiftiMapsMasker with displayed_maps passed as a list of a Numpy arrays. """ masker = NiftiMapsMasker(**niftimapsmasker_inputs) masker.fit() html = masker.generate_report(displayed_maps) assert masker._report_content['report_id'] == 0 assert masker._report_content['number_of_maps'] == 9 assert (masker._report_content['displayed_maps'] == list(displayed_maps)) msg = ("No image provided to fit in NiftiMapsMasker. " "Plotting only spatial maps for reporting.") assert masker._report_content['warning_message'] == msg assert html.body.count("<img") == len(displayed_maps)
def series_times_ROI(Maps, func, typeF): from nilearn.input_data import NiftiLabelsMasker, NiftiMapsMasker from nilearn import plotting import scipy.io as sio import numpy as np import os ################################## Resul = os.getcwd() #+'-Results' n_map = Maps[Maps.rfind('/') + 1:][:Maps[Maps.rfind('/') + 1:].find('.')] n_plot = 'empty_plot' #os.system('mkdir '+Resul) ################################## if typeF == 'Labels': masker = NiftiLabelsMasker(labels_img=Maps, standardize=True) plot_atlas = plotting.plot_roi(Maps) n_plot = Resul + '/Atlas_' + n_map + '_' + typeF + '.svg' plot_atlas.savefig(n_plot) if typeF == 'Maps': masker = NiftiMapsMasker(maps_img=Maps, standardize=True, memory='nilearn_cache', verbose=5) time_series = masker.fit_transform(func) print('Shape of serial times ', np.shape(time_series)) out_mat = Resul + '/Time_series_' + n_map + '_' + typeF + '.mat' sio.savemat(out_mat, {'time_series': time_series}) return out_mat, n_plot
def compute_connectivity_subjects(func_list, atlas, mask, conn, n_jobs=-1): """ Returns connectivities for all subjects tril matrix n_subjects * n_rois_tril """ if len(nib.load(atlas).shape) == 4: masker = NiftiMapsMasker(maps_img=atlas, mask_img=mask, detrend=True, low_pass=.1, high_pass=.01, t_r=3., resampling_target='data', smoothing_fwhm=6, memory=CACHE_DIR, memory_level=2) else: masker = NiftiLabelsMasker(labels_img=atlas, mask_img=mask, t_r=3., detrend=True, low_pass=.1, high_pass=.01, resampling_target='data', smoothing_fwhm=6, memory=CACHE_DIR, memory_level=2) p = Parallel(n_jobs=n_jobs, verbose=5)(delayed( compute_connectivity_subject)(conn, func, masker)\ for func in func_list) return np.asarray(p)
def getConnectome(imgPath=None, atlasPath=None, viewInBrowser=False, displayCovMatrix=False): """ Gets the connectome of a functional MRI scan imgPath -> absolute or relative path to the .nii file atlasPath -> download path for the reference MSDL atlas viewInBrowser (optional, default=False) -> if True, opens up an interactive viewer in the browser displayCovMatrix (optional, default=False) -> display the inverse covariance matrix Returns a tuple of shape (estimator, atlas) """ # Download the reference atlas atlas = datasets.fetch_atlas_msdl(data_dir=atlasPath) # Loading atlas image stored in 'maps' atlasFilename = atlas['maps'] # Get the time series for the fMRI scan masker = NiftiMapsMasker(maps_img=atlasFilename, standardize=True, memory='nilearn_cache', verbose=5) timeSeries = masker.fit_transform(imgPath) # Compute the connectome using sparse inverse covariance estimator = GraphicalLassoCV() estimator.fit(timeSeries) if (displayCovMatrix): labels = atlas['labels'] plotting.plot_matrix(estimator.covariance_, labels=labels, figure=(9, 7), vmax=1, vmin=-1, title='Covariance') plotting.plot_matrix(estimator.precision_, labels=labels, figure=(9, 7), vmax=1, vmin=-1, title='Inverse covariance (Precision)') #covPlot.get_figure().savefig('Covariance.png') # precPlot.get_figure().savefig('Inverse Covariance.png') if (viewInBrowser): coords = atlas.region_coords view = plotting.view_connectome(-estimator.precision_, coords, '60.0%') #view.save_as_html(file_name='Connectome Test.html') view.open_in_browser() return (estimator, atlas)
def get_data(subj, mask, sessions=None, dtype='jacobian'): """ Parameters ---------- subj : (N,) list of img_like mask : Niimg_like sessions : list of str Returns ------- data : (N x M) np.ndarray Data extracted from ``imgs``, where ``M`` is the number of parcels in ``mask`` """ # check mask is correct if not isinstance(mask, BaseMasker): if not isinstance(mask, str): raise ValueError('Mask must be a mask object or filepath.') if 'probabilistic' in mask: mask = NiftiMapsMasker(mask, resampling_target='maps') else: mask = NiftiLabelsMasker(mask, resampling_target='labels') # only fit mask if it hasn't been fitted to save time if not hasattr(mask, 'maps_img_'): mask = mask.fit() # get images for supplied sessions (or all images) subj_dir = pjoin(DERIV_DIR, subj) if sessions is not None: imgs = list( itertools.chain.from_iterable( [glob.glob(pjoin(subj_dir, f'*_ses-{ses}_*_{dtype}.nii.gz')) for ses in sorted(sessions)] ) ) else: imgs = sorted(glob.glob(pjoin(subj_dir, '*{dtype}.nii.gz'))) # extract subject / session information from data (BIDS format) demo = np.row_stack([REGEX.findall(i) for i in imgs]) # fit mask to data and stack across sessions data = np.row_stack([mask.transform(check_niimg(img, atleast_4d=True)) for img in imgs]) return data, demo
def _fmri_roi_extract_image(data, atlas_path, atlas_type, radius, overlap_ok,mask = None): if 'label' in atlas_type: logging.debug('Labels Extract') label_masker = NiftiLabelsMasker(atlas_path, mask_img=mask) timeseries = label_masker.fit_transform(data) if 'sphere' in atlas_type: atlas_path = np.loadtxt(atlas_path) logging.debug('Sphere Extract') spheres_masker = NiftiSpheresMasker(atlas_path, float(radius),mask_img=mask, allow_overlap = overlap_ok) timeseries = spheres_masker.fit_transform(data) if 'maps' in atlas_type: logging.debug('Maps Extract') maps_masker = NiftiMapsMasker(atlas_path,mask_img=mask, allow_overlap = overlap_ok) timeseries = maps_masker.fit_transform(data) timeseries[timeseries == 0.0] = np.nan return timeseries
def cal_connectome(fmri_ff, confound_ff, atlas_ff, outputjpg_ff, metric='correlation', labelrange=None, label_or_map=0): if label_or_map == 0: # “correlation”, “partial correlation”, “tangent”, “covariance”, “precision” masker = NiftiLabelsMasker(labels_img=atlas_ff, standardize=True, verbose=0) else: masker = NiftiMapsMasker(maps_img=atlas_ff, standardize=True, verbose=0) time_series_0 = masker.fit_transform(fmri_ff, confounds=confound_ff) if labelrange is None: labelrange = np.arange(time_series_0.shape[1]) time_series = time_series_0[:, labelrange] if metric == 'sparse inverse covariance': try: estimator = GraphLassoCV() estimator.fit(time_series) correlation_matrix = -estimator.precision_ except: correlation_matrix = np.zeros( (time_series.shape[1], time_series.shape[1])) else: correlation_measure = ConnectivityMeasure(kind=metric) correlation_matrix = correlation_measure.fit_transform([time_series ])[0] # Plot the correlation matrix fig = plt.figure(figsize=(6, 5), dpi=100) plt.clf() # Mask the main diagonal for visualization: np.fill_diagonal(correlation_matrix, 0) plt.imshow(correlation_matrix, interpolation="nearest", cmap="RdBu_r", vmax=0.8, vmin=-0.8) plt.gca().yaxis.tick_right() plt.axis('off') plt.colorbar() plt.title(metric.title(), fontsize=12) plt.tight_layout() fig.savefig(outputjpg_ff, bbox_inches='tight') plt.close() return correlation_matrix
def extract_parcellation_time_series(in_data, parcellation_name, parcellations_dict, bp_freqs, tr): ''' Depending on parcellation['is_probabilistic'] this function chooses either NiftiLabelsMasker or NiftiMapsMasker to extract the time series of each parcel if bp_freq: data is band passfiltered at (hp, lp), if (None,None): no filter, if (None, .1) only lp... tr in ms (e.g. from freesurfer ImageInfo()) returns np.array with parcellation time series and saves this array also to parcellation_time_series_file, and path to pickled masker object ''' from nilearn.input_data import NiftiLabelsMasker, NiftiMapsMasker, NiftiSpheresMasker import os, pickle import numpy as np if parcellations_dict[parcellation_name]['is_probabilistic'] == True: # use probab. nilearn masker = NiftiMapsMasker(maps_img=parcellations_dict[parcellation_name]['nii_path'], standardize=True) elif parcellations_dict[parcellation_name]['is_probabilistic'] == 'sphere': atlas = pickle.load(open(parcellations_dict[parcellation_name]['nii_path'])) coords = atlas.rois masker = NiftiSpheresMasker(coords, radius=5, allow_overlap=True, standardize=True) else: # 0/1 labels masker = NiftiLabelsMasker(labels_img=parcellations_dict[parcellation_name]['nii_path'], standardize=True) # add bandpass filter (only executes if freq not None hp, lp = bp_freqs masker.low_pass = lp masker.high_pass = hp if tr is not None: masker.t_r = tr else: masker.t_r = None masker.standardize = True masker_file = os.path.join(os.getcwd(), 'masker.pkl') with open(masker_file, 'w') as f: pickle.dump(masker, f) parcellation_time_series = masker.fit_transform(in_data) parcellation_time_series_file = os.path.join(os.getcwd(), 'parcellation_time_series.npy') np.save(parcellation_time_series_file, parcellation_time_series) return parcellation_time_series, parcellation_time_series_file, masker_file
def _set_volume_masker(roi_file, as_voxels=False, **kwargs): """Check and see if multiple ROIs exist in atlas file""" if not isinstance(roi_file, str): raise ValueError('roi_file must be a file name string') if roi_file.endswith('.csv') or roi_file.endswith('.tsv'): roi = _read_coords(roi_file) n_rois = len(roi) print(' {} region(s) detected from coordinates'.format(n_rois)) if kwargs.get('radius') is None: warnings.warn('No radius specified for coordinates; setting ' 'to nilearn.input_data.NiftiSphereMasker default ' 'of extracting from a single voxel') masker = NiftiSpheresMasker(roi, **kwargs) elif roi_file.endswith('.nii.gz'): # remove args for NiftiSpheresMasker if 'radius' in kwargs: kwargs.pop('radius') if 'allow_overlap' in kwargs: kwargs.pop('allow_overlap') roi_img = image.load_img(roi_file) if len(roi_img.shape) == 4: n_rois = roi_img.shape[-1] print(' {} region(s) detected from {}'.format(n_rois, roi_img.get_filename())) masker = NiftiMapsMasker(roi_img, allow_overlap=True,**kwargs) else: n_rois = len(np.unique(roi_img.get_fdata())) - 1 print(' {} region(s) detected from {}'.format(n_rois, roi_img.get_filename())) if n_rois > 1: masker = NiftiLabelsMasker(roi_img, **kwargs) elif n_rois == 1: # binary mask for single ROI if as_voxels: if 'mask_img' in kwargs: kwargs.pop('mask_img') masker = NiftiMasker(roi_img, **kwargs) else: # more computationally efficient if only wanting the mean masker = NiftiLabelsMasker(roi_img, **kwargs) else: raise ValueError('No ROI detected; check ROI file') else: raise ValueError('Invalid file type for roi_file. Must be one of: ' '.nii.gz, .csv, .tsv') return masker, n_rois
def __init__(self, atlas_name, metric, mask, detrend=True, low_pass=.1, high_pass=.01, t_r=3., resampling_target='data', smoothing_fwhm=6., memory='', memory_level=2, n_jobs=1): """ - Setting attributes - Preparing maskers """ self.atlas = fetch_atlas(atlas_name) self.metric = metric self.mask = mask self.n_jobs = n_jobs self.masker_vox = NiftiMasker(mask_img=self.mask, detrend=detrend, low_pass=low_pass, high_pass=high_pass, t_r=t_r, smoothing_fwhm=smoothing_fwhm, memory=memory, memory_level=memory_level) if len(nib.load(self.atlas).shape) == 4: self.masker_ROI = NiftiMapsMasker( maps_img=self.atlas, mask_img=self.mask, detrend=detrend, low_pass=low_pass, high_pass=high_pass, t_r=t_r, resampling_target=resampling_target, smoothing_fwhm=smoothing_fwhm, memory=memory, memory_level=memory_level) else: self.masker_ROI = NiftiLabelsMasker( labels_img=self.atlas, mask_img=self.mask, detrend=detrend, low_pass=low_pass, high_pass=high_pass, t_r=t_r, resampling_target=resampling_target, smoothing_fwhm=smoothing_fwhm, memory=memory, memory_level=memory_level)
def signal_extract(data,atlas,t_r=2.2,masker_type='Spheres',saveas='file'): """ Extracts BOLD time-series from regions of interest Parameters ---------- data: Filenames of subjects. atlas: regions or coordinates to extract signals from. masker_type : Type of masker used to extract BOLD signals . types are : 'Spheres','Maps','Labels' saveas : Destination to save and load output (.npz) Returns --------- subject_ts : array-like , 2-D (n_subjects,n_regions) Array of BOLD time-series """ subjects_ts=[] if os.path.exists(saveas): subjects_ts=np.load(saveas)['arr_0'] else: if masker_type== 'Spheres': masker = NiftiSpheresMasker( seeds=atlas, smoothing_fwhm=6, radius=4 ,mask_img=brainmask, detrend=False, standardize=True, low_pass=0.1, high_pass=0.01, t_r=t_r) elif masker_type == 'Maps': masker = NiftiMapsMasker(maps_img=atlas,mask_img=brainmask,standardize=True, high_pass=0.01,low_pass=0.1,detrend=False,t_r=t_r, memory_level=2,smoothing_fwhm=5,resampling_target='data', memory=mem,verbose=5) elif masker_type == 'Labels': masker = NiftiLabelsMasker(labels_img=atlas,mask_img=brainmask,standardize=True, high_pass=0.01,low_pass=0.1,detrend=False,t_r=t_r, memory_level=2,smoothing_fwhm=5,resampling_target='data', memory=mem,verbose=5) else: raise ValueError("Please provide masker type") for func_file in data: time_series = masker.fit_transform(func_file) subjects_ts.append(time_series) np.savez(saveas,subjects_ts) return subjects_ts
def test_nifti_maps_masker_report_displayed_maps_errors( niftimapsmasker_inputs, displayed_maps): """Tests that a TypeError is raised when the argument `displayed_maps` of `generate_report()` is not valid. """ masker = NiftiMapsMasker(**niftimapsmasker_inputs) masker.fit() with pytest.raises(TypeError, match=("Parameter ``displayed_maps``")): masker.generate_report(displayed_maps)
def test_nifti_maps_masker_report_maps_number_errors(niftimapsmasker_inputs, displayed_maps): """Tests that a ValueError is raised when the argument `displayed_maps` contains invalid map numbers. """ masker = NiftiMapsMasker(**niftimapsmasker_inputs) masker.fit() with pytest.raises(ValueError, match="Report cannot display the following maps"): masker.generate_report(displayed_maps)
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 __init__(self, atlas_name, metric, mask, rois=False, detrend=True, low_pass=.1, high_pass=.01, t_r=3., resampling_target='data', smoothing_fwhm=6., memory='', memory_level=2, n_jobs=1): self.fc_ = None self.atlas, self.rois = fetch_atlas(atlas_name, rois) self.metric = metric self.mask = mask self.n_jobs = n_jobs if len(nii_shape(self.atlas)) == 4: self.masker = NiftiMapsMasker(maps_img=self.atlas, mask_img=self.mask, detrend=detrend, low_pass=low_pass, high_pass=high_pass, t_r=t_r, resampling_target=resampling_target, smoothing_fwhm=smoothing_fwhm, memory=memory, memory_level=memory_level, verbose=5) else: self.masker = NiftiLabelsMasker( labels_img=self.atlas, mask_img=self.mask, detrend=detrend, low_pass=low_pass, high_pass=high_pass, t_r=t_r, resampling_target=resampling_target, smoothing_fwhm=smoothing_fwhm, memory=memory, memory_level=memory_level, verbose=5)
def __init__(self, atlas_name, rois, metric, mask, detrend=True, low_pass=.1, high_pass=.01, t_r=3., resampling_target='data', smoothing_fwhm=6., memory='', memory_level=2, n_jobs=1): self.atlas = fetch_atlas(atlas_name) self.rois = np.asarray(rois) self.metric = metric self.mask = mask self.n_jobs = n_jobs if len(nib.load(self.atlas).shape) == 4: self.masker = NiftiMapsMasker(maps_img=self.atlas, mask_img=self.mask, detrend=detrend, low_pass=low_pass, high_pass=high_pass, t_r=t_r, resampling_target=resampling_target, smoothing_fwhm=smoothing_fwhm, memory=memory, memory_level=memory_level) else: self.masker = NiftiLabelsMasker( labels_img=self.atlas, mask_img=self.mask, detrend=detrend, low_pass=low_pass, high_pass=high_pass, t_r=t_r, resampling_target=resampling_target, smoothing_fwhm=smoothing_fwhm, memory=memory, memory_level=memory_level)
def get_loader_pipe(parc, pipe='elastic_pipe', obj_params=None, **loader_params): '''TODO write doc - then add to docs.''' from ..main.funcs import pipeline_check from ..main.input import Loader if obj_params is None: obj_params = {} try: from neurotools.transform import SurfLabels, SurfMaps from neurotools.loading import load from nilearn.input_data import NiftiLabelsMasker, NiftiMapsMasker except ImportError: raise ImportError('neurotools must be installed!') # Apply pipeline check pipe = pipeline_check(pipe) # Get dimensionality of parcellation parc_dims = len(load(parc).shape) # Get correct object based off if parc_dims == 1: obj = SurfLabels(labels=parc, **obj_params) elif parc_dims == 2: obj = SurfMaps(maps=parc, **obj_params) elif parc_dims == 3: obj = NiftiLabelsMasker(labels_img=parc, **obj_params) elif parc_dims == 4: obj = NiftiMapsMasker(maps_img=parc, **obj_params) # Init loader from object loader = Loader(obj, **loader_params) # Add loader before rest of steps pipe.steps = [loader] + pipe.steps return pipe
def make_masker_from_atlas(atlas, memory=None, memory_level=1): """Construct a maker from a given atlas. Parameters ---------- atlas : str or 3D/4D Niimg-like object, The atlas to use to create the masker. If string, it should corresponds to the path of a Nifti image. memory : instance of joblib.Memory or string, (default=None) Used to cache the masking process. By default, no caching is done. If a string is given, it is the path to the caching directory. memory_level : integer, optional (default=1) Rough estimator of the amount of memory used by caching. Higher value means more memory for caching. Returns ------- masker : Nilearn Masker Nilearn Masker. """ atlas_ = check_niimg(atlas) atlas_dim = len(atlas_.shape) if atlas_dim == 3: masker = NiftiLabelsMasker(atlas_, memory=memory, memory_level=memory_level, smoothing_fwhm=6, detrend=True, verbose=1) elif atlas_dim == 4: masker = NiftiMapsMasker(atlas_, memory=memory, memory_level=memory_level, smoothing_fwhm=6, detrend=True, verbose=1) return masker
def extract_parcellation_time_series(in_data, parcellation_name, parcellations_dict, bp_freqs, tr): """ Depending on parcellation['is_probabilistic'] this function chooses either NiftiLabelsMasker or NiftiMapsMasker to extract the time series of each parcel if bp_freq: data is band passfiltered at (hp, lp), if (None,None): no filter, if (None, .1) only lp... tr in ms (e.g. from freesurfer ImageInfo()) returns np.array with parcellation time series and saves this array also to parcellation_time_series_file, and path to pickled masker object """ from nilearn.input_data import NiftiLabelsMasker, NiftiMapsMasker import os, pickle import numpy as np if parcellations_dict[parcellation_name]["is_probabilistic"]: # use probab. nilearn masker = NiftiMapsMasker(maps_img=parcellations_dict[parcellation_name]["nii_path"]) else: # 0/1 labels masker = NiftiLabelsMasker(labels_img=parcellations_dict[parcellation_name]["nii_path"]) # add bandpass filter (only executes if freq not None hp, lp = bp_freqs masker.low_pass = lp masker.high_pass = hp if tr is not None: masker.t_r = float(tr) / 1000.0 else: masker.t_r = None masker.standardize = True masker_file = os.path.join(os.getcwd(), "masker.pkl") with open(masker_file, "w") as f: pickle.dump(masker, f) parcellation_time_series = masker.fit_transform(in_data) parcellation_time_series_file = os.path.join(os.getcwd(), "parcellation_time_series.npy") np.save(parcellation_time_series_file, parcellation_time_series) return parcellation_time_series, parcellation_time_series_file, masker_file
def test_nifti_maps_masker_report_integer_and_all_displayed_maps( niftimapsmasker_inputs, displayed_maps): """Tests NiftiMapsMasker reporting with no image provided to fit and displayed_maps provided as an integer or as 'all'. """ masker = NiftiMapsMasker(**niftimapsmasker_inputs) masker.fit() expected_n_maps = 9 if displayed_maps == 'all' else min(9, displayed_maps) if displayed_maps != 'all' and displayed_maps > 9: with pytest.warns(UserWarning, match="masker only has 9 maps."): html = masker.generate_report(displayed_maps) else: html = masker.generate_report(displayed_maps) assert masker._report_content['report_id'] == 0 assert masker._report_content['number_of_maps'] == 9 assert (masker._report_content['displayed_maps'] == list( range(expected_n_maps))) msg = ("No image provided to fit in NiftiMapsMasker. " "Plotting only spatial maps for reporting.") assert masker._report_content['warning_message'] == msg assert html.body.count("<img") == expected_n_maps
atlas4d = nib.load(atlas['maps']) atlas4d_data = atlas4d.get_data() atlas3d_data = np.sum(atlas4d_data, axis=3) atlas3d = nib.Nifti1Image(atlas3d_data, atlas4d.get_affine()) n_subjects = len(func_files) subjects = [] cov_feat = [] for subject_n in range(n_subjects): filename = func_files[subject_n] print("Processing file %s" % filename) print("-- Computing region signals ...") masker = NiftiMapsMasker(atlas["maps"], resampling_target="maps", standardize=False, memory=CACHE_DIR, memory_level=1, verbose=0) region_ts = masker.fit_transform(filename) subjects.append(region_ts) print("-- Computing covariances") cov_matrix = np.cov(region_ts.T) cov_feat.append(cov_matrix[np.tril_indices(len(cov_matrix))]) from sklearn.svm import SVC from sklearn.cross_validation import StratifiedShuffleSplit cov_feat = np.array(cov_feat) nb_iter = 100 pg_counter = 0 groups = [['AD', 'Normal'], ['AD', 'EMCI'], ['AD', 'LMCI'],
atlas = datasets.fetch_atlas_msdl() # Loading atlas image stored in 'maps' atlas_filename = atlas['maps'] # Loading atlas data stored in 'labels' labels = atlas['labels'] # Load the functional datasets data = datasets.fetch_adhd(n_subjects=1) print('First subject resting-state nifti image (4D) is located at: %s' % data.func[0]) ############################################################################ # Extract the time series from nilearn.input_data import NiftiMapsMasker masker = NiftiMapsMasker(maps_img=atlas_filename, standardize=True, memory='nilearn_cache', verbose=5) time_series = masker.fit_transform(data.func[0], confounds=data.confounds) ############################################################################ # `time_series` is now a 2D matrix, of shape (number of time points x # number of regions) print(time_series.shape) ############################################################################ # Build and display a correlation matrix from nilearn.connectome import ConnectivityMeasure correlation_measure = ConnectivityMeasure(kind='correlation') correlation_matrix = correlation_measure.fit_transform([time_series])[0]
dyn_fc = [] for subject in dyn_dataset.subjects: dyn_fc.append(loader.load_dynacomp_fc(subject_id=subject, session='func1', metric='pc', msdl=False, preprocessing_folder='pipeline_1')[ind]) dyn_fc = np.asarray(dyn_fc) ############################################################################## # NYU rs-fMRI ############################################################################## nyu_func = fetch_nyu_rest()['func'] masker = NiftiMapsMasker(maps_img=roi_imgs, low_pass=.1, high_pass=.01, t_r=2., smoothing_fwhm=6., detrend=True, standardize=False, resampling_target='maps', memory_level=0, verbose=5) masker.fit() def mask_and_covariance(f): x = masker.transform(f) return np.corrcoef(x.T)[ind] # gl = GraphLassoCV(verbose=2) # gl.fit(x) # return gl.covariance_[ind] from joblib import delayed, Parallel nyu_fc = Parallel(n_jobs=20, verbose=5)(delayed(mask_and_covariance)(f)
import numpy as np from loader import load_dynacomp from nilearn.datasets import fetch_msdl_atlas from nilearn.input_data import NiftiMapsMasker dataset = load_dynacomp(preprocessing_folder='pipeline_2', prefix='resampled_wr') atlas = fetch_msdl_atlas() # add mask, smoothing, filtering and detrending masker = NiftiMapsMasker(maps_img=atlas['maps'], mask_img=dataset.mask, low_pass=.1, high_pass=.01, t_r=1.05, smoothing_fwhm=6., detrend=True, standardize=False, resampling_target='data', memory_level=0, verbose=5) for i in range(len(dataset.subjects)): tic = time.clock() output_path, _ = os.path.split(dataset.func1[i]) if not os.path.isfile(os.path.join(output_path, 'func1_msdl_filter.npy')): print i, dataset.subjects[i] output_path, _ = os.path.split(dataset.func1[i]) x = masker.fit_transform(dataset.func1[i]) np.save(os.path.join(output_path, 'func1_msdl_filter') , x) x = masker.fit_transform(dataset.func2[i])
from nilearn.input_data import NiftiMapsMasker from nilearn import datasets import pandas as pd import os prepdir = "/oak/stanford/groups/russpold/data/ds000030/1.0.3/derivatives/fmriprep_0.4.4" subs = [x for x in os.listdir(prepdir) if x.startswith('sub-') and not x.endswith(".html")] atlas = datasets.fetch_atlas_msdl() atlas_filename = atlas['maps'] outdir = os.path.join(os.environ.get("SCRATCH"),"CNP_ts") if not os.path.exists(outdir): os.mkdir(outdir) for sub in subs: subnum = float(sub.split("-")[1]) outfile = os.path.join(outdir,"%s_MSDL.csv"%sub) if os.path.exists(outfile): continue preprocd = os.path.join(prepdir,sub,'func','%s_task-rest_bold_space-MNI152NLin2009cAsym_preproc.nii.gz'%sub) if not os.path.exists(preprocd): continue masker = NiftiMapsMasker(maps_img=atlas_filename, standardize=True,memory='nilearn_cache', verbose=5) time_series = masker.fit_transform(preprocd) ts = pd.DataFrame(time_series) ts.to_csv(outfile,index=False,header=False)
size_in_GB = all_sub_rs_maps.get_data().nbytes / 1e9 print('% rs images: %.2f GB' % (cur_shape[-1], size_in_GB)) ############################################################################### # dump network projections ############################################################################### # retrieve network projections from nilearn import datasets as ds smith_pkg = ds.fetch_atlas_smith_2009() icas_path = smith_pkg['rsn20'] from nilearn.input_data import NiftiMapsMasker nmm = NiftiMapsMasker( mask_img=mask_file, maps_img=icas_path, resampling_target='mask', standardize=True, detrend=True) nmm.fit() nmm.maps_img_.to_filename('dbg_ica_maps.nii.gz') FS_netproj = nmm.transform(all_sub_rs_maps) np.save('%i_nets_timeseries' % sub_id, FS_netproj) # compute network sparse inverse covariance from sklearn.covariance import GraphLassoCV from nilearn.image import index_img from nilearn import plotting try: gsc_nets = GraphLassoCV(verbose=2, alphas=20) gsc_nets.fit(FS_netproj)
dataset = load_dynacomp(preprocessing_folder='pipeline_2', prefix='resampled_wr') # func1, func2 for idx, func in enumerate([dataset.func1, dataset.func2]): # all the subjects for i in range(len(dataset.subjects)): tic = time.clock() output_path, _ = os.path.split(func[i]) print dataset.subjects[i] maps_img = dict_to_list(dataset.rois[i]) # add mask, smoothing, filtering and detrending masker = NiftiMapsMasker(maps_img=maps_img, mask_img=dataset.mask, low_pass=.1, high_pass=.01, smoothing_fwhm=6., t_r=1.05, detrend=True, standardize=False, resampling_target='data', memory_level=0, verbose=5) output_path, _ = os.path.split(func[i]) # extract the signal to x x = masker.fit_transform(func[i]) np.save(os.path.join(output_path, 'func' + str(idx+1) + '_rois_filter'), x) toc = time.clock() print toc - tic
Script to test functions Created on Thu Mar 26 15:02:11 2015 @author: [email protected] """ from loader import load_dynacomp, list_of_dicts_to_key_list, dict_to_list from nilearn.input_data import NiftiMapsMasker import time # Load Dynacomp dataset dataset = load_dynacomp() # Dataset keys print 'keys\n', dataset.keys() # Dataset functional 1 print 'func1\n', dataset.func1 # Dataset behaviordata : prePerf print 'prePerf\n', list_of_dicts_to_key_list(dataset.behavior, 'prePerf') # Generate seed-masker for subject 0 maps_img = dict_to_list(dataset.rois[1]) tic = time.clock() masker = NiftiMapsMasker(maps_img, verbose=5) x = masker.fit_transform(dataset.func1[1]) toc = time.clock() print toc - tic
# print('Warning: several func reference files: '+func_ref_file[0]+' will be used') #func_ref = nb.load(func_ref_file[0]) #func_template = nibabel.load display_atlas= nilearn.plotting.plot_prob_atlas(visu, anat_img=anat_ref_file[0], title=atlas_name+'_anat', cut_coords = (5,0,0),threshold=0.) at_check =[plt.gcf()] #figure handle for atlas check #masker for raw time series masker_r = NiftiMapsMasker(atlas_filename, mask_img=mask, smoothing_fwhm=None, standardize=stdz,detrend=detr, low_pass=None, high_pass=None,t_r=TR, resampling_target='data', memory=mem_dir,memory_level=5, verbose=0) # prepare pairs for pairwise comparisons between groups comps = [] for func_index in range(len(func_type_list)-1) : if func_index != len(func_type_list)-func_index: for i in range(func_index,len(func_type_list)-func_index): if i+1<len(func_type_list): comps.append([func_type_list[func_index],func_type_list[i+1]]) else: comps.append([func_type_list[func_index],func_type_list[func_index+1]]) Bonf = len(comps) all_time_series_r = {} #regressed time series
def get_data_in_roi(path_roi, data_file): """Using of the NiftiMapsMasker """ masker = NiftiMapsMasker([path_roi]) nifti_obj = nibabel.load(data_file) data = masker.fit_transform(nifti_obj) return data