def main(args): # Set analysis directories root = '/net/synapse/nt/users/bmacintosh_lab/nluciw/' # Root directory of data. data_dir = root + 'data/EnF/sourcedata/' # Output directory. output_dir = root + 'outputs/perf_covar/' + args.output_dir # Create output if does not exist. if not os.path.exists(os.path.dirname(output_dir)): os.makedirs(os.path.dirname(output_dir)) # Save command line with open(output_dir + 'commandline_args.txt', 'w') as f: f.write('\n'.join(sys.argv[1:])) # Load 4d nifti objects for both groups bd_data = fetch_data(data_dir, args.nifti_name, metadata=args.metadata, subject_group=('BD', )) hc_data = fetch_data(data_dir, args.nifti_name, metadata=args.metadata, subject_group=('HC', )) hc_vols = concat_imgs(hc_data.imgs) bd_vols = concat_imgs(bd_data.imgs) # Construct or load parcellator object using the atlas we specify on # the command line. parcellator = NiftiLabelsMasker(labels_img=args.atlas, mask_img=data_dir + 'masks/cbf_80p_aal_merge_mni.nii.gz', standardize=False, strategy='mean') parcellator.fit() # Do the parcellation and correlation for both groups. hc_covar, bd_covar =\ covariance.parcellate_and_correlate([hc_vols,bd_vols], output_dir, parcellator, prefix = args.output_prefix, detrend=False#, # pve_gm_imgs=[concat_imgs(hc_data.struc_imgs), # concat_imgs(bd_data.struc_imgs)] ) print(len(bd_data.imgs), len(hc_data.imgs)) difference = statistics.compute_difference(bd_covar[0], hc_covar[0], len(bd_data.imgs), len(hc_data.imgs)) cors = np.stack((bd_covar[0], hc_covar[0], difference)) np.save(output_dir + args.output_prefix + 'cors', cors)
def test_img_to_signals_labels_non_float_type(target_dtype): fake_fmri_data = np.random.RandomState(0).rand(10, 10, 10, 10) > 0.5 fake_affine = np.eye(4, 4).astype(np.float64) fake_fmri_img_orig = nibabel.Nifti1Image( fake_fmri_data.astype(np.float64), fake_affine, ) fake_fmri_img_target_dtype = new_img_like( fake_fmri_img_orig, fake_fmri_data.astype(target_dtype)) fake_mask_data = np.ones((10, 10, 10), dtype=np.uint8) fake_mask = nibabel.Nifti1Image(fake_mask_data, fake_affine) masker = NiftiLabelsMasker(fake_mask) masker.fit() timeseries_int = masker.transform(fake_fmri_img_target_dtype) timeseries_float = masker.transform(fake_fmri_img_orig) assert np.sum(timeseries_int) != 0 assert np.allclose(timeseries_int, timeseries_float)
# Before data extraction, we convert an array labels to Nifti like image. All # inputs to NiftiLabelsMasker must be Nifti-like images or filename to Nifti # images. We use the same reference image as used above in previous sections labels_img = new_img_like(fmri_img, labels) # First, initialize masker with parameters suited for data extraction using # labels as input image, resampling_target is None as affine, shape/size is same # for all the data used here, time series signal processing parameters # standardize and detrend are set to False masker = NiftiLabelsMasker(labels_img, resampling_target=None, standardize=False, detrend=False) # After initialization of masker object, we call fit() for preparing labels_img # data according to given parameters masker.fit() # Preparing for data extraction: setting number of conditions, size, etc from # haxby dataset condition_names = haxby_labels.unique() n_cond_img = fmri_data[..., haxby_labels == 'house'].shape[-1] n_conds = len(condition_names) X1, X2 = np.zeros((n_cond_img, n_conds)), np.zeros((n_cond_img, n_conds)) # Gathering data for each condition and then use transformer from masker # object transform() on each data. The transformer extracts data in condition # maps where the target regions are specified by labels images for i, cond in enumerate(condition_names): cond_maps = new_img_like( fmri_img, fmri_data[..., haxby_labels == cond][..., :n_cond_img]) mask_data = masker.transform(cond_maps) X1[:, i], X2[:, i] = mask_data[:, 0], mask_data[:, 1]
mean_img, title='Connected components: second ROI', axes=fig_id) plt.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0) plot_roi(nibabel.Nifti1Image(first_roi_data, fmri_img.get_affine()), mean_img, title='Connected components: first ROI_', output_file='snapshot_first_ROI.png') plot_roi(nibabel.Nifti1Image(second_roi_data, fmri_img.get_affine()), mean_img, title='Connected components: second ROI', output_file='snapshot_second_ROI.png') # use the new ROIs to extract data maps in both ROIs masker2 = NiftiLabelsMasker( labels_img=nibabel.Nifti1Image(labels, fmri_img.get_affine()), resampling_target=None, standardize=False, detrend=False) masker2.fit() condition_names = sorted(list(set(haxby_labels))) n_cond_img = fmri_data[..., haxby_labels == b'house'].shape[-1] n_conds = len(condition_names) X1, X2 = np.zeros((n_cond_img, n_conds)), np.zeros((n_cond_img, n_conds)) for i, cond in enumerate(condition_names): cond_maps = nibabel.Nifti1Image( masker.inverse_transform(fmri_data[..., haxby_labels == cond][..., :n_cond_img].T).get_data(), fmri_img.get_affine()) mask_data = masker2.transform(cond_maps) X1[:, i], X2[:, i] = mask_data[:, 0], mask_data[:, 1] condition_names[condition_names.index('scrambledpix')] = 'scrambled' plt.figure(figsize=(15, 7)) for i in np.arange(2):
# We extract data from ROIs using nilearn's NiftiLabelsMasker from nilearn.input_data import NiftiLabelsMasker # Before data extraction, we convert an array labels to Nifti like image. All # inputs to NiftiLabelsMasker must be Nifti-like images or filename to Nifti # images. We use the same reference image as used above in previous sections labels_img = new_img_like(fmri_img, labels) # First, initialize masker with parameters suited for data extraction using # labels as input image, resampling_target is None as affine, shape/size is same # for all the data used here, time series signal processing parameters # standardize and detrend are set to False masker = NiftiLabelsMasker(labels_img, resampling_target=None, standardize=False, detrend=False) # After initialization of masker object, we call fit() for preparing labels_img # data according to given parameters masker.fit() # Preparing for data extraction: setting number of conditions, size, etc from # haxby dataset condition_names = haxby_labels.unique() n_cond_img = fmri_data[..., haxby_labels == 'house'].shape[-1] n_conds = len(condition_names) X1, X2 = np.zeros((n_cond_img, n_conds)), np.zeros((n_cond_img, n_conds)) # Gathering data for each condition and then use transformer from masker # object transform() on each data. The transformer extracts data in condition # maps where the target regions are specified by labels images for i, cond in enumerate(condition_names): cond_maps = new_img_like( fmri_img, fmri_data[..., haxby_labels == cond][..., :n_cond_img]) mask_data = masker.transform(cond_maps) X1[:, i], X2[:, i] = mask_data[:, 0], mask_data[:, 1]
atlas_nii = index_img(crad['scorr_mean'], 9) # Craddock 100 region atlas r_atlas_nii = resample_img( img=atlas_nii, target_affine=mask_file.get_affine(), target_shape=mask_file.shape, interpolation='nearest' ) r_atlas_nii.to_filename('debug_ratlas.nii.gz') from nilearn.input_data import NiftiLabelsMasker nlm = NiftiLabelsMasker( labels_img=r_atlas_nii, mask_img=mask_file, standardize=True, detrend=True) nlm.fit() FS_regpool = nlm.transform(all_sub_rs_maps) np.save('%i_regs_timeseries' % sub_id, FS_regpool) # 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_regpool) np.save('%i_regs_cov' % sub_id, gsc_nets.covariance_) np.save('%i_regs_prec' % sub_id, gsc_nets.precision_) except:
class Connectivity(BaseEstimator, TransformerMixin): """ Connectivity Estimator computes the functional connectivity of a list of 4D niimgs, according to ROIs defined on an atlas. First, the timeseries on ROIs are extracted. Then, the connectivity is computed for each pair of ROIs. The result is a ravel of half symmetric matrix. Parameters ---------- atlas : atlas filepath metric : metric name (gl, lw, oas, scov, corr, pcorr) mask : mask filepath detrend : masker param low_pass: masker param high_pass : masker param t_r : masker param smoothing : masker param resampling_target : masker param memory : masker param memory_level : masker param n_jobs : masker param Attributes ---------- fc_ : functional connectivity (covariance and precision) """ 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 fit(self, imgs, confounds=None): """ compute connectivities """ self.masker.fit() if self.metric == 'correlation' or \ self.metric == 'partial correlation' or \ self.metric == 'tangent': if confounds is None: ts = Parallel(n_jobs=self.n_jobs, verbose=5)( delayed(do_mask_img)(self.masker, func) for func in imgs) else: ts = Parallel(n_jobs=self.n_jobs, verbose=5)( delayed(do_mask_img)(self.masker, func, confound) for func, confound in zip(imgs, confounds)) cov_embedding = CovEmbedding(kind=self.metric) p_ = np.asarray(vec_to_sym(cov_embedding.fit_transform(ts))) ind = np.tril_indices(p_.shape[1], k=-1) self.fc_ = np.asarray( [p_[i, ...][ind] for i in range(p_.shape[0])]) else: p_ = Parallel(n_jobs=self.n_jobs, verbose=5)( delayed(compute_connectivity_subject)(self.metric, self.masker, func, confound) for func, confound in zip(imgs, confounds)) self.fc_ = np.asarray(p_)[:, 0, :] return self.fc_
# The first label correspond to the background print('The atlas contains {} non-overlapping regions'.format( len(atlas.labels) - 1)) ########################################################################### # Instantiate the mask and visualize atlas # from nilearn.input_data import NiftiLabelsMasker # Instantiate the masker with label image and label values masker = NiftiLabelsMasker(atlas.maps, labels=atlas.labels, standardize=True) # Visualize the atlas # Note that we need to call fit prior to generating the mask masker.fit() # At this point, no functional image has been provided to the masker. # We can still generate a report which can be displayed in a Jupyter # Notebook, opened in a browser using the .open_in_browser() method, # or saved to a file using the .save_as_html(output_filepath) mathod. report = masker.generate_report() report ########################################################################## # Fitting the mask and generating a report masker.fit(func_filename) # We can again generate a report, but this time, the provided functional # image is displayed with the ROI of the atlas. # The report also contains a summary table giving the region sizes in mm3
def roi_average(roi_paths, rois_parent_folder, tasks, df, selected_contrasts=None, subject_specific_rois=False, roi_masks=None): """ Function to compute the average and std of z-scores for a set of voxels inside of subject-specific, functional Regions-of-Interest (ROIs) in a set of contrast z-maps. """ # For every ROI, ... roi_names = [] all_rois_contrast_avgs = [] all_contrast_names = [] for r, roi_path in enumerate(roi_paths, start=1): # ROI name roi_name = re.match('.*' + rois_parent_folder + '/(.*).nii.gz', roi_path).groups()[0] roi_names.append(roi_name) # For every task, ... roi_contrast_avgs = [] contrast_names = [] for t, task in enumerate(tasks): # Select the entries in the data frame only concerned to # the ffx z-maps task_df = df[df.task == task][df.acquisition == 'ffx'] contrasts = task_df.contrast.unique() if selected_contrasts is not None: contrasts = np.intersect1d(selected_contrasts, contrasts) # For every contrast, ... for contrast in contrasts: flatten_list = [] for s, subject in enumerate(SUBJECTS): img_paths = [] # Create the subject-specific ROI mask if subject_specific_rois: if roi_masks is None: raise ValueError('roi_masks not defined!') else: roi = math_img('img == %d' % r, img=roi_masks[s]) print('Extracting subject-specific ' + \ '"%s" as ROI for %s from the contrast "%s".' %(roi_name, subject, contrast)) else: roi = roi_path print('Extracting general ' + \ '"%s" as ROI for %s from the contrast "%s".' %(roi_name, subject, contrast)) masker = NiftiLabelsMasker(labels_img=roi) masker.fit() # Paths of the contrast z-maps of every participants img_path = task_df[task_df.contrast == contrast]\ [task_df.subject == subject]\ .path.values[-1] img_paths.append(img_path) print(img_paths) # For each participant, extract data from z-map # according to the subject-specific ROI mask and # average straight off the values of # the corresponding voxels mask_data = masker.transform(img_paths) flatten_list.extend(mask_data.tolist()[0]) roi_contrast_avgs.append(flatten_list) # Labels of the contrasts contrast_names.append(LABELS[contrast][1].values[0] + ' vs. ' + LABELS[contrast][0].values[0]) # Append for all ROIs all_rois_contrast_avgs.append(roi_contrast_avgs) all_contrast_names.append(contrast_names) return all_rois_contrast_avgs, all_contrast_names, roi_names
face_image = image.index_img(fmri_masked_img, haxby_labels == 'face') house_image = image.index_img(fmri_masked_img, haxby_labels == 'house') mean_face_img = mem.cache(image.mean_img)(face_image) mean_house_img = mem.cache(image.mean_img)(house_image) plot_two_maps(plot_stat_map, (mean_face_img, 'Mean face image'), (mean_house_img, 'Mean house image')) plt.show() print("Plot mean activation (per condition) for the first two ROIs.") print("Use the new ROIs to extract data maps in both ROIs.") first_roi_masker = NiftiLabelsMasker(labels_img=first_roi_img, resampling_target=None, standardize=False, detrend=False) first_roi_masker.fit() second_roi_masker = NiftiLabelsMasker(labels_img=second_roi_img, resampling_target=None, standardize=False, detrend=False) second_roi_masker.fit() condition_names = list(set(haxby_labels)) n_cond_img = masked_fmri_vectors[haxby_labels == 'house', :].shape[0] n_conds = len(condition_names) for i, cond in enumerate(condition_names): cond_mask = haxby_labels == cond cond_img = image.index_img(fmri_masked_img, cond_mask) X1_vectors = mem.cache(first_roi_masker.transform)(cond_img).T X2_vectors = mem.cache(second_roi_masker.transform)(cond_img).T X1_image = first_roi_masker.inverse_transform(X1_vectors) X2_image = second_roi_masker.inverse_transform(X2_vectors) X1_mean_img = mem.cache(image.mean_img)(X1_image)
def test_nifti_labels_masker_report(data_img_3d, mask): shape = (13, 11, 12) affine = np.diag([2, 2, 2, 1]) n_regions = 9 labels = ['background' ] + ['region_{}'.format(i) for i in range(1, n_regions + 1)] EXPECTED_COLUMNS = [ 'label value', 'region name', 'size (in mm^3)', 'relative size (in %)' ] labels_img = generate_labeled_regions(shape, affine=affine, n_regions=n_regions) labels_img_floats = new_img_like(labels_img, get_data(labels_img).astype(float)) masker = NiftiLabelsMasker(labels_img_floats, labels=labels) masker.fit() masker.generate_report() # Check that providing incorrect labels raises an error masker = NiftiLabelsMasker(labels_img, labels=labels[:-1]) masker.fit() with pytest.raises(ValueError, match="Mismatch between the number of provided labels"): masker.generate_report() masker = NiftiLabelsMasker(labels_img, labels=labels) masker.fit() # Check that a warning is given when generating the report # since no image was provided to fit with pytest.warns(UserWarning, match="No image provided to fit in NiftiLabelsMasker"): masker.generate_report() # No image was provided to fit, regions are plotted using # plot_roi such that no contour should be in the image display = masker._reporting() for d in ['x', 'y', 'z']: assert len(display[0].axes[d].ax.collections) == 0 masker = NiftiLabelsMasker(labels_img, labels=labels) masker.fit(data_img_3d) display = masker._reporting() for d in ['x', 'y', 'z']: assert len(display[0].axes[d].ax.collections) > 0 assert len(display[0].axes[d].ax.collections) <= n_regions masker = NiftiLabelsMasker(labels_img, labels=labels, mask_img=mask) masker.fit(data_img_3d) report = masker.generate_report() assert masker._reporting_data is not None # Check that background label was left as default assert masker.background_label == 0 assert masker._report_content['description'] == ( 'This reports shows the regions defined by the labels of the mask.') # Check that the number of regions is correct assert masker._report_content['number_of_regions'] == n_regions # Check that all expected columns are present with the right size for col in EXPECTED_COLUMNS: assert col in masker._report_content['summary'] assert len(masker._report_content['summary'][col]) == n_regions # Check that labels match assert masker._report_content['summary']['region name'] == labels[1:] # Relative sizes of regions should sum to 100% assert_almost_equal( sum(masker._report_content['summary']['relative size (in %)']), 100) _check_html(report) assert "Regions summary" in str(report) # Check region sizes calculations expected_region_sizes = Counter(get_data(labels_img).ravel()) for r in range(1, n_regions + 1): assert_almost_equal( masker._report_content['summary']['size (in mm^3)'][r - 1], expected_region_sizes[r] * np.abs(np.linalg.det(affine[:3, :3]))) # Check that region labels are no displayed in the report # when they were not provided by the user. masker = NiftiLabelsMasker(labels_img) masker.fit() report = masker.generate_report() for col in EXPECTED_COLUMNS: if col == "region name": assert col not in masker._report_content["summary"] else: assert col in masker._report_content["summary"] assert len(masker._report_content['summary'][col]) == n_regions
rroi_img = nib.Nifti1Image(rroi_img, affine=tmp_img.get_affine()) rroi_img.to_filename('dbg_roi_labelimg.nii.gz') n_rois = len(np.unique(rroi_img.get_data())) - 1 # !fslview dbg_roi_labelimg.nii.gz roi_masker = NiftiLabelsMasker(labels_img=rroi_img, background_label=0, standardize=True, smoothing_fwhm=0, detrend=True, low_pass=None, high_pass=None, memory='nilearn_cache', verbose=0) roi_masker.fit() # parse the RS images according to the ROIs sub_FS = roi_masker.transform(rs_4D) # assert sub_FS.shape == (len(cur_rs_imgs), len(roi_names)) # dump_path = op.join(rs_base, 'roi_dump_' + ROI_DIR) # np.save(dump_path, arr=sub_FS) # vectorize cross_corr = np.corrcoef(sub_FS.T) tril_inds = np.tril_indices_from(cross_corr, k=-1) cc_ravel = cross_corr[tril_inds] # cross_corrs.append(cc_ravel) cur_FS[i_rs] = cc_ravel
from pathlib import Path from scipy import io as sio from pygsp import graphs import shutil # In[2]: from nilearn.input_data import NiftiLabelsMasker from nilearn.datasets import fetch_icbm152_2009 mnitemp = fetch_icbm152_2009() glassermasker = NiftiLabelsMasker(labels_img='Glasser_masker.nii.gz', mask_img=mnitemp['mask']) glassermasker.fit() # In[3]: import numpy as np coords = np.load('coords_with_order.npz')['coordinates'] ind_reord = np.load('coords_with_order.npz')['order'] # In[4]: from nilearn.datasets import fetch_abide_pcp import pandas import os df = pandas.read_csv('Phenotypic_V1_0b_preprocessed1.csv', sep='\t') site_id = np.unique(df['SITE_ID'])