def test_unmask_list(random_state=42): rng = np.random.RandomState(random_state) shape = (3, 4, 5) affine = np.eye(4) mask_data = (rng.rand(*shape) < .5) mask_img = Nifti1Image(mask_data.astype(np.uint8), affine) a = unmask(mask_data[mask_data], mask_img) b = unmask(mask_data[mask_data].tolist(), mask_img) # shouldn't crash assert_array_equal(a.get_data(), b.get_data())
def _generate_report(self): """ Generates the visual report """ from niworkflows.viz.utils import plot_registration NIWORKFLOWS_LOG.info('Generating visual report') anat = load_img(self._anat_file) contour_nii = load_img(self._contour) if self._contour is not None else None if self._mask_file: anat = unmask(apply_mask(anat, self._mask_file), self._mask_file) mask_nii = load_img(self._mask_file) else: mask_nii = threshold_img(anat, 1e-3) n_cuts = 7 if not self._mask_file and contour_nii: cuts = cuts_from_bbox(contour_nii, cuts=n_cuts) else: cuts = cuts_from_bbox(mask_nii, cuts=n_cuts) # Call composer compose_view( plot_registration(anat, 'fixed-image', estimate_brightness=True, cuts=cuts, contour=contour_nii, compress=self.inputs.compress_report), [], out_file=self._out_report )
def _generate_report(self): """ Generates the visual report """ from niworkflows.viz.utils import plot_registration NIWORKFLOWS_LOG.info('Generating visual report') anat = load_img(self._anat_file) contour_nii = load_img( self._contour) if self._contour is not None else None if self._mask_file: anat = unmask(apply_mask(anat, self._mask_file), self._mask_file) mask_nii = load_img(self._mask_file) else: mask_nii = threshold_img(anat, 1e-3) n_cuts = 7 if not self._mask_file and contour_nii: cuts = cuts_from_bbox(contour_nii, cuts=n_cuts) else: cuts = cuts_from_bbox(mask_nii, cuts=n_cuts) # Call composer compose_view(plot_registration(anat, 'fixed-image', estimate_brightness=True, cuts=cuts, contour=contour_nii, compress=self.inputs.compress_report), [], out_file=self._out_report)
def _run_fwe_permutation(self, params): """ Run a single random permutation of a dataset. Does the shared work between vFWE and cFWE. """ iter_df, iter_ijk, conn, z_thresh = params iter_ijk = np.squeeze(iter_ijk) iter_df[['i', 'j', 'k']] = iter_ijk ale_values = self._compute_ale(iter_df) _, z_values = self._ale_to_p(ale_values) iter_max_value = np.max(ale_values) # Begin cluster-extent thresholding by thresholding matrix at cluster- # defining voxel-level threshold iter_z_map = unmask(z_values, self.mask) vthresh_iter_z_map = iter_z_map.get_data() vthresh_iter_z_map[vthresh_iter_z_map < z_thresh] = 0 labeled_matrix = ndimage.measurements.label(vthresh_iter_z_map, conn)[0] clust_sizes = [np.sum(labeled_matrix == val) for val in np.unique(labeled_matrix)] if len(clust_sizes) == 1: iter_max_cluster = 0 else: clust_sizes = clust_sizes[1:] # First cluster is zeros in matrix iter_max_cluster = np.max(clust_sizes) return iter_max_value, iter_max_cluster
def _perm(self, params): iter_ijk, iter_df, weight_vec, conn = params iter_ijk = np.squeeze(iter_ijk) iter_df[['i', 'j', 'k']] = iter_ijk k_est = self.kernel_estimator(iter_df, self.mask) iter_ma_maps = k_est.transform(self.ids, **self.kernel_arguments) iter_ma_maps = apply_mask(iter_ma_maps, self.mask) iter_ma_maps *= weight_vec iter_of_map = np.sum(iter_ma_maps, axis=0) iter_max_value = np.max(iter_of_map) iter_of_map = unmask(iter_of_map, self.mask) vthresh_iter_of_map = iter_of_map.get_data().copy() vthresh_iter_of_map[vthresh_iter_of_map < self.voxel_thresh] = 0 labeled_matrix = ndimage.measurements.label(vthresh_iter_of_map, conn)[0] clust_sizes = [ np.sum(labeled_matrix == val) for val in np.unique(labeled_matrix) ] clust_sizes = clust_sizes[1:] # First cluster is zeros in matrix if clust_sizes: iter_max_cluster = np.max(clust_sizes) else: iter_max_cluster = 0 return iter_max_value, iter_max_cluster
def _run_fwe_permutation(self, params): iter_ijk, iter_df, conn, voxel_thresh = params iter_ijk = np.squeeze(iter_ijk) iter_df[['i', 'j', 'k']] = iter_ijk iter_ma_maps = self.kernel_estimator.transform(iter_df, mask=self.mask, masked=True) iter_ma_maps *= self.weight_vec iter_of_map = np.sum(iter_ma_maps, axis=0) iter_max_value = np.max(iter_of_map) iter_of_map = unmask(iter_of_map, self.mask) vthresh_iter_of_map = iter_of_map.get_data().copy() vthresh_iter_of_map[vthresh_iter_of_map < voxel_thresh] = 0 labeled_matrix = ndimage.measurements.label(vthresh_iter_of_map, conn)[0] clust_sizes = [ np.sum(labeled_matrix == val) for val in np.unique(labeled_matrix) ] clust_sizes = clust_sizes[1:] # First cluster is zeros in matrix if clust_sizes: iter_max_cluster = np.max(clust_sizes) else: iter_max_cluster = 0 return iter_max_value, iter_max_cluster
def compute_tsnr(self, run=None, mean_only=False, std_only=False): """ Computes tsnr (or mean/std) image of the (mean) functional data. """ self.logger.info("Computing TSNR.") if mean_only and std_only: raise ValueError("Cannot return mean only and std only!") if run is None: n_runs = len(self.preproc['func_ts']) else: n_runs = 1 stat = np.zeros((n_runs, self.preproc['func_ts'][0].shape[-1])) for run in range(n_runs): func = self.preproc['func_ts'][run] if mean_only: this_stat = func.mean(axis=0) elif std_only: this_stat = func.std(axis=0) else: this_stat = func.mean(axis=0) / func.std(axis=0) this_stat[np.isnan(this_stat)] = 0 stat[run, :] = this_stat mean_stat = stat.mean(axis=0) if self.mask is not None: mean_stat = masking.unmask(mean_stat, self.mask) return mean_stat
def scramble_img(img, mask_img, random_state=None): '''Shuffles the voxel values within a Niimg-like object img: Niimg-like object Input image where the voxels should be randomly shuffled. mask_img: Niimg-like object A mask which masks the data to brain space random_state: int, RandomState instance or None, optional, default=None If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random. Used when shuffle == True. ''' rng = check_random_state(random_state) # mask image to MNI152 template img_data = apply_mask(img, mask_img).flatten() n_voxels = img_data.shape[0] # shuffle data img_data_scrambled = rng.choice(img_data, n_voxels, replace=False) # transform back to image img_scrambled = unmask(img_data_scrambled, mask_img) return img_scrambled
def array_to_mni_map(array, fname, data_path, mask, threshold=None, **kwargs): '''Transforms array to mni space and saves it as mni map IN: array - ndarray, array of statistics in group sapce to transform to mni space fname - string, name under which to save the mni map data_path - string, path to Forrest Gump directory mask - string, filename of the mask to unmask array. threshold - float, optional, threshold for mni map''' from nipype.interfaces import fsl import os from nilearn.masking import unmask if threshold is not None: array[np.abs(array) < threshold] = 0 unmasked = unmask(array, mask) unmasked.to_filename(fname) flirt = fsl.ApplyXFM() flirt.inputs.in_file = fname flirt.inputs.out_file = fname flirt.inputs.padding_size = 0 flirt.inputs.interp = 'nearestneighbour' flirt.inputs.reference = os.path.join(data_path, 'templates', 'grpbold7Tp1', 'in_mni', 'brain_12dof.nii.gz') flirt.inputs.in_matrix_file = os.path.join(data_path, 'templates', 'grpbold7Tp1', 'xfm', 'tmpl2mni_12dof.mat') flirt.run()
def fit(self, target_mask, method='min_distance', r=5, n_exps=50, n_parcels=2): """ Run MAPBOT parcellation. Parameters ---------- region_name : :obj:`str` Name of region for parcellation. n_parcels : :obj:`int`, optional Number of parcels to generate for ROI. If array_like, each parcel number will be evaluated and results for all will be returned. Default is 2. """ if not isinstance(n_parcels): n_parcels = [n_parcels] # Step 1: Build correlation matrix target_data = apply_mask(target_mask, self.mask) target_map = unmask(target_data, self.mask) target_data = target_map.get_data() mask_idx = np.vstack(np.where(target_data)) n_voxels = mask_idx.shape[1] voxel_arr = np.zeros((n_voxels, np.sum(self.mask))) del voxel_arr # currently unused ijk = self.coordinates[['i', 'j', 'k']].values temp_df = self.coordinates.copy() term_df = pd.DataFrame(columns=self.tfidf_df.columns, index=range(n_voxels)) for i_voxel in range(n_voxels): voxel = mask_idx[:, i_voxel] temp_df['distance'] = cdist(ijk, voxel) if method == 'min_studies': # number of studies temp_df2 = temp_df.groupby('id')[['distance']].min() temp_df2 = temp_df2.sort_values(by='distance') sel_ids = temp_df2.iloc[:n_exps].index.values elif method == 'min_distance': # minimum distance temp_df2 = temp_df.groupby('id')[['distance']].min() sel_ids = temp_df2.loc[temp_df2['distance'] < r].index.values # Build DT matrix voxel_df = self.tfidf_df.loc[self.tfidf_df.index.isin(sel_ids)] term_df.loc[i_voxel] = voxel_df.mean(axis=0) values = term_df.values d = np.dot(np.dot(values.T, values), np.ones((values.shape[0], 1))) values_prime = np.dot(values, d**-.5) for i_parc in n_parcels: model = NMF(n_components=i_parc, init='nndsvd', random_state=0) W = model.fit_transform(values_prime) H = model.components_ del W, H # not sure what's next
def _run_interface(self, runtime): ext = '.nii.gz' if self.inputs.compress else '.nii' flat_mask = apply_mask(self.inputs.image, self.inputs.mask) masked_image = unmask(flat_mask, self.inputs.mask) self._results['masked_t2s'] = fname_presuffix( self.inputs.image, suffix='_masked' + ext, newpath=runtime.cwd, use_ext=False) masked_image.to_filename(self._results['masked_t2s']) return runtime
def compute_TSNR(in_file, highpass=True, motion_corr=False): """ Computes temporal signal-to-noise ratio (TSNR) for a functional MRI file preprocessed with FMRIPREP. Parameters ---------- in_file : str Path to functional MRI file highpass : bool Whether to highpass the data (using the cosine basis set in the *confounds.tsv file) motion_corr : bool Whether to include the six motion parameters from motion correction in the confound regression step. Returns ------- out_file : str Path to TSNR file """ base_dir = op.dirname(in_file) base_file = op.basename(in_file) if 'preproc' not in base_file: raise ValueError("In_file should be a preprocessed file!") # Mask BOLD file mask = op.join(base_dir, base_file.split('preproc')[0] + 'brain_mask.nii.gz') bold_masked = masking.apply_mask(in_file, mask) # Get confounds and regress out of data if highpass or motion_corr: conf = op.join( base_dir, base_file.split('space')[0] + 'desc-confounds_regressors.tsv') conf_df = pd.read_csv(conf, sep='\t') conf_vars = [] if highpass: conf_vars.extend( [col for col in conf_df.columns if 'cosine' in col]) if motion_corr: conf_vars.extend( ['trans_x', 'trans_y', 'trans_z', 'rot_x', 'rot_y', 'rot_z']) conf_df = conf_df.loc[:, conf_vars] conf_values = np.c_[np.ones(conf_df.shape[0]), conf_df.values] conf_params = np.linalg.lstsq(conf_values, bold_masked, rcond=-1)[0] bold_masked = bold_masked - conf_values[:, 1:].dot(conf_params[1:, :]) # Compute tsnr tsnr = bold_masked.mean(axis=0) / bold_masked.std(axis=0) tsnr_img = masking.unmask(tsnr, mask) out_file = in_file.replace('.nii.gz', '_TSNR.nii.gz') tsnr_img.to_filename(out_file) return out_file
def _generate_report(self): """ Generates the visual report """ from niworkflows.viz.utils import plot_registration NIWORKFLOWS_LOG.info('Generating visual report') fixed_image_nii = load_img(self._fixed_image) moving_image_nii = load_img(self._moving_image) contour_nii = load_img( self._contour) if self._contour is not None else None if self._fixed_image_mask: fixed_image_nii = unmask( apply_mask(fixed_image_nii, self._fixed_image_mask), self._fixed_image_mask) # since the moving image is already in the fixed image space we # should apply the same mask moving_image_nii = unmask( apply_mask(moving_image_nii, self._fixed_image_mask), self._fixed_image_mask) mask_nii = load_img(self._fixed_image_mask) else: mask_nii = threshold_img(fixed_image_nii, 1e-3) n_cuts = 7 if not self._fixed_image_mask and contour_nii: cuts = cuts_from_bbox(contour_nii, cuts=n_cuts) else: cuts = cuts_from_bbox(mask_nii, cuts=n_cuts) # Call composer compose_view(plot_registration(fixed_image_nii, 'fixed-image', estimate_brightness=True, cuts=cuts, label=self._fixed_image_label, contour=contour_nii, compress=self.inputs.compress_report), plot_registration(moving_image_nii, 'moving-image', estimate_brightness=True, cuts=cuts, label=self._moving_image_label, contour=contour_nii, compress=self.inputs.compress_report), out_file=self._out_report)
def preprocess_and_tmp_save_fmri(data_path, task, subj, model, tmp_path, group_mask=None): ''' Generator for preprocessed fMRI runs from one subject of Forrest Gump aligns to group template run-wise linear de-trending and z-scoring IN: data_path - string, path pointing to the Forrest Gump directory task - string, which part of the Forrest Gump dataset to load subj - int, subject to pre-process tmp_path - string, path to save the dataset temporarily to OUT: preprocessed fMRI samples per run''' from nipype.interfaces import fsl dhandle = mvpa.OpenFMRIDataset(data_path) flavor = 'dico_bold7Tp1_to_subjbold7Tp1' if group_mask is None: group_mask = os.path.join(data_path, 'sub{0:03d}'.format(subj), 'templates', 'bold7Tp1', 'in_grpbold7Tp1', 'brain_mask.nii.gz') mask_fname = os.path.join(data_path, 'sub{0:03d}'.format(subj), 'templates', 'bold7Tp1', 'brain_mask.nii.gz') for run_id in dhandle.get_task_bold_run_ids(task)[subj]: run_ds = dhandle.get_bold_run_dataset(subj, task, run_id, chunks=run_id - 1, mask=mask_fname, flavor=flavor) filename = 'brain_subj_{}_run_{}.nii.gz'.format(subj, run_id) tmp_file = os.path.join(tmp_path, filename) save(unmask(run_ds.samples.astype('float32'), mask_fname), tmp_file) warp = fsl.ApplyWarp() warp.inputs.in_file = tmp_file warp.inputs.out_file = os.path.join(tmp_path, 'group_' + filename) warp.inputs.ref_file = os.path.join(data_path, 'templates', 'grpbold7Tp1', 'brain.nii.gz') warp.inputs.field_file = os.path.join(data_path, 'sub{0:03d}'.format(subj), 'templates', 'bold7Tp1', 'in_grpbold7Tp1', 'subj2tmpl_warp.nii.gz') warp.inputs.interp = 'nn' warp.run() os.remove(tmp_file) run_ds = mvpa.fmri_dataset(os.path.join(tmp_path, 'group_' + filename), mask=group_mask, chunks=run_id - 1) mvpa.poly_detrend(run_ds, polyord=1) mvpa.zscore(run_ds) os.remove(os.path.join(tmp_path, 'group_' + filename)) yield run_ds.samples.astype('float32')
def save_map_whole(subj, scores, threshold=None, model='logBSC_H200'): '''Saves brainmap as nifti file''' subj_mask = '/home/data/psyinf/forrest_gump/anondata/sub{0:03}'.format(subj) + '/templates/bold7Tp1/brain_mask.nii.gz' if threshold is not None: scores[scores<threshold] = 0 unmasked = unmask(scores, subj_mask) unmasked.to_filename( spenc_dir+'MaThe/maps/model_{}_subj_'.format(model)+\ '{}_map.nii.gz'.format(subj))
def save_map_avg_whole(scores, threshold=None, model='logBSC_H200'): '''Saves brainmap as nifti file''' mask = spenc_dir + 'brainmask_group_template.nii.gz' if threshold is not None: scores[scores<threshold] = 0 unmasked = unmask(scores, mask) unmasked.to_filename( spenc_dir+'MaThe/maps/model_{}_'.format(model)+\ 'whole_avg_map.nii.gz')
def save_map_avg(subj, scores, threshold=0.0, model='logBSC_H200'): '''Saves brainmap as nifti file''' mask = spenc_dir+'temporal_lobe_mask_grp_7T_test.nii.gz' if threshold is not None: scores[scores<threshold] = 0 unmasked = unmask(scores, mask) unmasked.to_filename( spenc_dir+'MaThe/maps/model_avg_{}_subj_'.format(model)+\ '{}_map.nii.gz'.format(subj))
def unmask_img(d, mask): """ Unmasks matrix d according to mask :param d: numpy array (2D) :param mask: mask :param logger: logger instance :return: image file """ return masking.unmask(d, mask)
def _generate_report(self): """ Generates the visual report """ from niworkflows.viz.utils import plot_registration NIWORKFLOWS_LOG.info('Generating visual report') fixed_image_nii = load_img(self._fixed_image) moving_image_nii = load_img(self._moving_image) contour_nii = load_img(self._contour) if self._contour is not None else None if self._fixed_image_mask: fixed_image_nii = unmask(apply_mask(fixed_image_nii, self._fixed_image_mask), self._fixed_image_mask) # since the moving image is already in the fixed image space we # should apply the same mask moving_image_nii = unmask(apply_mask(moving_image_nii, self._fixed_image_mask), self._fixed_image_mask) mask_nii = load_img(self._fixed_image_mask) else: mask_nii = threshold_img(fixed_image_nii, 1e-3) n_cuts = 7 if not self._fixed_image_mask and contour_nii: cuts = cuts_from_bbox(contour_nii, cuts=n_cuts) else: cuts = cuts_from_bbox(mask_nii, cuts=n_cuts) # Call composer compose_view( plot_registration(fixed_image_nii, 'fixed-image', estimate_brightness=True, cuts=cuts, label=self._fixed_image_label, contour=contour_nii, compress=self.inputs.compress_report), plot_registration(moving_image_nii, 'moving-image', estimate_brightness=True, cuts=cuts, label=self._moving_image_label, contour=contour_nii, compress=self.inputs.compress_report), out_file=self._out_report )
def main(file=None, mask=True, figure=False, out_dir=None): """ Computes the median temporal signal-to-noise ratio (TSNR) of a 4D fMRI file (similar to what MRIQC does). Parameters ---------- file : str or None If str, refers to the path of a 4D nifti file. If None, a default file is loaded. mask : bool Whether to mask the file (using Nilearn) figure : bool If False, only prints out the median TSNR value. If True, it will write out a figure with voxel-wise TSNR values. out_dir : str Directory to save TSNR figure to. If None, the same directory as `file` will be used. """ if file is None: print("WARNING: you did not specify a file! Exiting ...") sys.exit() print(f"INFO: computing TSNR for {file}") # Load in image and associated data img = image.load_img(file) # Apply mask if mask: print("INFO: computing EPI mask") mask = masking.compute_epi_mask(img) else: # If no mask, at least remove zeros nonzeros = (img.get_fdata().sum(axis=3) != 0).astype(int) mask = nib.Nifti1Image(nonzeros, affine=img.affine) img_2d = masking.apply_mask(img, mask) # Compute TSNR tsnr = np.mean(img_2d, axis=0) / np.std(img_2d, axis=0) print(f"INFO: median TSNR = {np.median(tsnr):.3f}") if figure: # Recreate 3D nifti + plot with Nilearn tsnr_img = masking.unmask(tsnr, mask_img=mask) plotting.plot_epi(tsnr_img) # Save to disk if out_dir is None: # set dir if necessary out_dir = os.path.dirname(file) f_name = os.path.basename(file).replace('.nii.gz', '_tsnr.png') f_out = os.path.join(out_dir, f_name) print(f"INFO: Saving figure to {f_out}") plt.savefig(f_out)
def save_map(subj, scores, threshold=0.0, model='logBSC_H200'): '''Saves brainmap as nifti file''' subj_mask = spenc_dir+'temporal_lobe_mask_brain'+\ '_subj{0:02}bold.nii.gz'.format(subj) if threshold is not None: scores[scores<threshold] = 0 unmasked = unmask(scores, subj_mask) unmasked.to_filename( spenc_dir+'MaThe/maps/model_{}_subj_'.format(model)+\ '{}_map.nii.gz'.format(subj))
def inverse_transform(self, X): self._check_fitted() img = masking.unmask(X, self.mask_img_) # Be robust again memmapping that will create read-only arrays in # internal structures of the header: remove the memmaped array try: img._header._structarr = np.array(img._header._structarr).copy() except: pass return img
def from_array_to_nifti(self, nii_output_filename, array, mask_img): """ Convert an array to a nitfi image and save it :param nii_output_filename: :param array: Dask array, shape(n_voxels) :param mask_img: :return: """ img_3D = unmask(array, mask_img, order='F') nb.save(img_3D, nii_output_filename)
def unmaskImg(d, mask, logger=None): """ Unmasks matrix d according to mask :param d: numpy array (2D) :param mask: mask :param logger: logger instance :return: image file """ from nilearn.masking import unmask write_to_logger("unmasking image...", logger) return unmask(d, mask)
def from_array_to_vol(self, array, nii_output_filename, mask_img): """ Convert an array to a volume :param array: :param nii_output_filename: :param mask_img: :return: """ img_3D = unmask(array, mask_img, order='F') nb.save(img_3D, nii_output_filename)
def save_stability_maps(self, darr_stab_maps_medoid, network_label, path_maps, mask_img, subject=''): if os.path.exists(path_maps) == False: os.makedirs(path_maps) for idx_state in range(darr_stab_maps_medoid.shape[0]): nii_output_filename = os.path.join(path_maps, 'map_' + subject + '_net' + str(network_label) + '_state' + str( idx_state)[0]) img_3D = unmask(darr_stab_maps_medoid[idx_state, :], mask_img, order='F') nb.save(img_3D, nii_output_filename)
def compute_TSNR(in_file, highpass=True, motion_corr=False): """ Computes temporal signal-to-noise ratio (TSNR) for a functional MRI file preprocessed with FMRIPREP. Parameters ---------- in_file : str Path to functional MRI file highpass : bool Whether to highpass the data (using the cosine basis set in the *confounds.tsv file) motion_corr : bool Whether to include the six motion parameters from motion correction in the confound regression step. Returns ------- out_file : str Path to TSNR file """ base_dir = op.dirname(in_file) base_file = op.basename(in_file) if 'preproc' not in base_file: raise ValueError("In_file should be a preprocessed file!") # Mask BOLD file mask = op.join(base_dir, base_file.split('preproc')[0] + 'brain_mask.nii.gz') bold_masked = masking.apply_mask(in_file, mask) # Get confounds and regress out of data if highpass or motion_corr: conf = op.join(base_dir, base_file.split('space')[0] + 'desc-confounds_regressors.tsv') conf_df = pd.read_csv(conf, sep='\t') conf_vars = [] if highpass: conf_vars.extend([col for col in conf_df.columns if 'cosine' in col]) if motion_corr: conf_vars.extend(['trans_x', 'trans_y', 'trans_z', 'rot_x', 'rot_y', 'rot_z']) conf_df = conf_df.loc[:, conf_vars] conf_values = np.c_[np.ones(conf_df.shape[0]), conf_df.values] conf_params = np.linalg.lstsq(conf_values, bold_masked, rcond=-1)[0] bold_masked = bold_masked - conf_values[:, 1:].dot(conf_params[1:, :]) # Compute tsnr tsnr = bold_masked.mean(axis=0) / bold_masked.std(axis=0) tsnr_img = masking.unmask(tsnr, mask) out_file = in_file.replace('.nii.gz', '_TSNR.nii.gz') tsnr_img.to_filename(out_file) return out_file
def plot_highest_score_bold_predicted_vs_actual(path_predicted, path_actual, arg_highscore, save_path, mask_path): print('Plotting', save_path) from scipy.stats import zscore x, y, z, fold = arg_highscore # bold = [] # for i in range(6): # # bold.append(joblib.load(path_actual.format(i))[:-1]) # bold.append(joblib.load(path_actual.format(i))) # bold = np.concatenate(bold) # (time, voxel) bold = joblib.load(path_actual.format(fold)) masks = joblib.load(mask_path) bold = unmask(bold, masks[fold]) # bold_predicted = nib.load(path_predicted) # bold_predicted = [] # for i in range(6): # # bold_actual.append(joblib.load(path_predicted.format(i))[:-1]) # bold_predicted.append(joblib.load(path_predicted.format(i))) # bold_predicted = np.concatenate(bold_predicted) # (time, voxel) bold_predicted = joblib.load(path_predicted.format(fold)) bold_predicted = unmask(bold_predicted, masks[fold]) # print(bold_predicted.shape) # print(bold.shape) bold_predicted_high = bold_predicted.dataobj[x, y, z] bold_predicted_high = zscore(bold_predicted_high) bold_high = bold.dataobj[x, y, z] bold_high = zscore(bold_high) plt.plot(bold_predicted_high, label='Predicted bold') plt.plot(bold_high, label='Actual bold') plt.legend() plt.xlabel('Volume') plt.ylabel('Zscore') r, prob = product_moment_corr(bold_predicted_high.reshape(-1, 1), bold_high.reshape(-1, 1)) plt.title('Predicted vs actual bold, voxel %s, r=%.4f, p=%.4f' % (str(arg_highscore), r, prob)) plt.savefig(save_path) # plt.show() plt.close()
def plot_subj_ko(subj, scores, threshold=0.01, coords=None): '''plots subject scoremap using nilearn and returns display object''' subj_mask = './masks/template_mask_thick.nii.gz' background_img = os.path.join(DATA_DIR, 'templates', 'grpbold7Tp1', 'brain.nii.gz') scores = scores.copy() scores[scores<threshold] = 0.0 unmasked = unmask(scores, subj_mask) unmasked = threshold_img(unmasked, 0.001) display = plot_stat_map( unmasked, cut_coords=coords, bg_img=background_img, symmetric_cbar=False, title='metric per voxel', dim=-1, aspect=1.25, threshold=0.001, draw_cross=False) fig = plt.gcf() fig.set_size_inches(12, 4) return display
def plot_diff_avg(scores, threshold=0.01, coords=None, cross=False, **kwargs): '''plots subject scoremap using nilearn and returns display object''' mask = spenc_dir+'temporal_lobe_mask_grp_7T_test.nii.gz' background_img = '/home/data/psyinf/forrest_gump/anondata/templates/' + \ 'grpbold7Tp1/brain.nii.gz' scores = scores.copy() scores[np.abs(scores)<threshold] = 0.0 unmasked = unmask(scores, mask) unmasked = threshold_img(unmasked, 0.001) display = plot_stat_map( unmasked, cut_coords=coords, bg_img=background_img, title='metric per voxel', dim=-1, aspect=1.25, threshold=0.001, draw_cross=cross, **kwargs) fig = plt.gcf() fig.set_size_inches(12, 4) return display
def plot_subj(subj, scores, threshold=0.01, coords=None): '''plots subject scoremap using nilearn and returns display object''' subj_mask = spenc_dir+'temporal_lobe_mask_brain_subj{0:02}bold.nii.gz'.format(subj) background_img = '/home/data/psyinf/forrest_gump/anondata/sub{0:03}/'.format(subj)+\ 'templates/bold7Tp1/brain.nii.gz' scores = scores.copy() scores[scores<threshold] = 0.0 unmasked = unmask(scores, subj_mask) unmasked = threshold_img(unmasked, 0.001) display = plot_stat_map( unmasked, cut_coords=coords, bg_img=background_img, symmetric_cbar=False, title='metric per voxel', dim=-1, aspect=1.25, threshold=0.001, draw_cross=False) fig = plt.gcf() fig.set_size_inches(12, 4) return display
def swap_img_data(img, mask_img, absolute_values=True): '''Swap data in a nifti image. User can choose to do swapping by taking the real or absolute values in the image into account''' img_data = apply_mask(img, mask_img) if absolute_values == True: map_dict = dict( zip(sorted(set(img_data), key=abs), sorted(set(img_data), key=abs, reverse=True))) else: map_dict = dict( zip(sorted(set(img_data)), sorted(set(img_data), reverse=True))) img_data_swapped = np.array([map_dict[x] for x in img_data]) img_swapped = unmask(img_data_swapped, mask_img) return img_swapped
def load_data_whole(subj, model='logBSC_H200', metric='r'): '''Loads data and returns them as an Encoding instance''' fmri_data = np.hstack( [joblib.load(('/home/data/scratch/mboos/prepro/' 'whole_fmri_subj_{}_split_{}.pkl').format(subj, i)) for i in xrange(10)]).astype('float32') subj_mask_lobe = spenc_dir+'temporal_lobe_mask_brain_subj{0:02}bold.nii.gz'.format(subj) subj_mask_brain = '/home/data/psyinf/forrest_gump/anondata/sub{0:03}'.format(subj) + '/templates/bold7Tp1/brain_mask.nii.gz' img = unmask(fmri_data, mask_img=subj_mask_brain) fmri_data = apply_mask(img, subj_mask_lobe) model_preds = joblib.load(spenc_dir + ('MaThe/predictions/' '{}_subj_{}_all.pkl').format(model, subj)) if metric in ('p', 'p_adj'): scores_model = metric_functions['r'](model_preds, fmri_data) else: scores_model = metric_functions[metric](model_preds, fmri_data) return Encoding(fmri_data, model_preds, scores_model, metric=metric)
def plot_max_coefs(ridges_path, mask_path, arg_highscore, save_path): mask = joblib.load(mask_path) for fold in range(len(mask)): filename = ridges_path.format(fold) if not os.path.exists(filename): continue print('Plotting', save_path.format(fold)) ridges = joblib.load(filename) # print(ridges.alpha_) coefs = unmask(ridges.coef_.T, mask[fold]) max_coefs = coefs.dataobj[arg_highscore[:3]] if use_mel_spec_features: max_coefs = max_coefs.reshape((-1, n_mel)) n_lag_bins = max_coefs.shape[0] # lagging_offset = 4.25 lagging_offset = 0.0 # lagging_offset = 4.0 x_ticks = (np.arange(n_lag_bins) * 0.025) + lagging_offset # x_stride = 20 x_stride = 80 if use_mel_spec_features: n_mel_stride = 4 plt.imshow(max_coefs.T, aspect='auto', origin='lower') plt.ylabel('Mel frequency (Hz)') plt.gca().set_yticks(np.arange(0, n_mel, n_mel_stride)) plt.gca().set_yticklabels(mel_frequencies[:n_mel:n_mel_stride]) cbar = plt.colorbar() cbar.set_label('Ridge coefficient', rotation=270) else: plt.plot(max_coefs) plt.ylabel('Ridge coefficient') plt.xlabel('Time lag (s)') plt.gca().set_xticks(np.arange(3, n_lag_bins, x_stride)) plt.gca().set_xticklabels(["%.1f" % tick for tick in x_ticks[3::x_stride]]) plt.tight_layout() plt.savefig(save_path.format(fold) + '.svg') plt.close()
def generate_nifti_4D(loc_volume, av_win, bm, smooth_f, fdir): fun_data = np.zeros((bm.shape[0], bm.shape[1], bm.shape[2], t_size)) for iv, (xv, yv, zv) in enumerate(loc_volume): # Put the time average of the functional data over a time window av_win long fun_data[xv, yv, zv, :] = np.average(fun[iv, :].reshape((-1, av_win)), axis=1) fun_img = nib.Nifti2Image(fun_data, affine=bm.affine) # Apply smoothing fun_img = smooth_img(fun_img, smooth_f) # Apply bm mask fun_img = unmask(apply_mask(fun_img, bm), bm) fout = fdir + 'T{:03d}_S{:02d}.nii.gz'.format(av_win, smooth_f) print('Nifti saved in {}'.format(fout)) nib.save(fun_img, fout) return fun_img
def compute_fxe_contrast(self, contrast_def, stat_type='t', run=None, output_type='z_score'): """ Computes a fixed effect across multiple runs. """ self.logger.info(f"Computing contrast: {contrast_def} for task {self.task} ...") if self.glm is None: raise ValueError("GLM has not been run yet!") if run is None: results = self.glm['results'] labels = self.glm['labels'] dms = self.glm['dms'] design_info = DesignInfo(dms[0].columns.tolist()) else: results = self.glm['results'][run] labels = self.glm['labels'][run] dms = self.glm['dms'][run] design_info = DesignInfo(dms.columns.tolist()) if isinstance(contrast_def, (np.ndarray, str)): con_vals = [contrast_def] elif isinstance(contrast_def, (list, tuple)): con_vals = contrast_def else: raise ValueError('contrast_def must be an array or str or list of' ' (array or str)') for cidx, con in enumerate(con_vals): if not isinstance(con, np.ndarray): con_vals[cidx] = design_info.linear_constraint(con).coefs if run is None: contrast = _fixed_effect_contrast(labels, results, con_vals, stat_type) else: contrast = compute_contrast(labels, results, con_vals, stat_type) values = getattr(contrast, output_type)() if self.mask is not None: return masking.unmask(values, self.mask) else: return values
def visualize(self, statmap, space='fsnative', threshold=0, hemi='R', mask=None, **plot_args): """ Visualizes a statmap on an image/surface background. """ if 'fs' in space and isinstance(statmap, nib.Nifti1Image): raise ValueError("Statmap is an image, but space is fs* something ...") bg = 'surface' if 'fs' in space else 'volume' self.logger.info(f"Visualizing statmap on {bg} ...") if 'fs' in space: fs_base = op.dirname(self.fs_dir) if space == 'fsnative': fs_id = f'sub-{self.sub}' else: fs_id = space return plotting.view_surf( surf_mesh=op.join(fs_base, fs_id, 'surf', f'{hemi.lower()}h.inflated'), surf_map=statmap, bg_map=op.join(fs_base, fs_id, 'surf', f'{hemi.lower()}h.sulc'), threshold=threshold, **plot_args ) else: if mask is not None: statmap = masking.unmask(statmap, mask) if space == 'T1w': bg = op.join(self.fp_dir, 'anat', f'sub-{self.sub}_desc-preproc_T1w.nii.gz') else: bg = 'MNI152' return plotting.view_img( stat_map_img=statmap, bg_img=bg, threshold=threshold, **plot_args )
def tmp_save_fmri(datapath, task, subj, model): dhandle = mvpa.OpenFMRIDataset(datapath) #mask_fname = os.path.join('/home','mboos','SpeechEncoding','temporal_lobe_mask_brain_subj' + str(subj) + 'bold.nii.gz') flavor = 'dico_bold7Tp1_to_subjbold7Tp1' group_brain_mask = '/home/mboos/SpeechEncoding/brainmask_group_template.nii.gz' mask_fname = os.path.join(datapath, 'sub{0:03d}'.format(subj), 'templates', 'bold7Tp1', 'brain_mask.nii.gz') #mask_fname = '/home/mboos/SpeechEncoding/masks/epi_subj_{}.nii.gz'.format(subj) scratch_path = '/home/data/scratch/mboos/prepro/tmp/' for run_id in dhandle.get_task_bold_run_ids(task)[subj]: run_ds = dhandle.get_bold_run_dataset(subj,task,run_id,chunks=run_id-1,mask=mask_fname,flavor=flavor) filename = 'whole_brain_subj_{}_run_{}.nii.gz'.format(subj, run_id) tmp_path = scratch_path + filename save(unmask(run_ds.samples.astype('float32'), mask_fname), tmp_path) os.system('applywarp -i {0} -o {1} -r /home/data/psyinf/forrest_gump/anondata/templates/grpbold7Tp1/brain.nii.gz -w /home/data/psyinf/forrest_gump/anondata/sub{2:03}/templates/bold7Tp1/in_grpbold7Tp1/subj2tmpl_warp.nii.gz --interp=nn'.format(tmp_path, scratch_path+'group_'+filename,subj)) os.remove(tmp_path) run_ds = mvpa.fmri_dataset(scratch_path+'group_'+filename, mask=group_brain_mask, chunks=run_id-1) mvpa.poly_detrend(run_ds, polyord=1) mvpa.zscore(run_ds) joblib.dump(run_ds.samples.astype('float32'), '/home/data/scratch/mboos/prepro/tmp/whole_brain_subj_{}_run_{}.pkl'.format(subj, run_id)) os.remove(scratch_path+'group_'+filename) return run_ds.samples.shape[1]
def main(posterior_dir, args): # Laod fixed effect posteriors fixef = pd.read_csv(join(posterior_dir, 'POP.txt'), sep='\t') # Load random effect posteriors ROI_Intercept = pd.read_csv(join(posterior_dir, 'ROI_Intercept.txt'), sep='\t') VOX_Intercept = pd.read_csv(join(posterior_dir, 'VOX_Intercept.txt'), sep='\t') posteriors = combinePos(ROI_Intercept, VOX_Intercept) posteriors = add_mean(posteriors, fixef, 'Intercept') # Load the insula mask mask = nil.image.load_img(args.mask) roi_indx = np.unique(mask.get_data())[1:] print('ROI indexes: ', roi_indx) # P+ of every voxel is computed and rendered on a brain template for visualization probMask = nil.image.new_img_like(mask, np.zeros_like(mask.get_data())) for ii, n in enumerate(roi_indx): n_vox = (mask.get_data() == n).sum() roi_mask = getSubMask(n, mask) print('ROI index:', n) print('Num of voxels in data table:', n_vox) print('Number of vox in the roi mask:', (roi_mask.get_data() == 1).sum()) p_plus = [] for jj, m in enumerate(range(n_vox)): vox = posteriors['roi{:02d}_{:03d}'.format(int(n), m)] p_plus.append(np.mean(vox > 0)) rendered = unmask(p_plus, roi_mask) probMask = nil.image.math_img("img1 + img2", img1=probMask, img2=rendered) return probMask
def gen_lag_map(epi_img, brain_mask_img, gm_mask_img, lags): print("...masking data...") epi_gm_masked_data = masking.apply_mask(epi_img, gm_mask_img) gm_mean_signal = np.mean(epi_gm_masked_data, axis=1) epi_brain_masked_data = masking.apply_mask(epi_img, brain_mask_img).T lag_index_correction = np.sum(np.array(lags) > 0) xcorr_array = [] print("...calculating lags...") for voxel in tqdm(epi_brain_masked_data, unit='voxel'): vox_signal = voxel vox_xcorr = xcorr_range(gm_mean_signal, vox_signal, lags) xcorr_maxima = sci_signal.argrelmax(np.array(vox_xcorr), order=1)[0] if len(xcorr_maxima) == 0: interp_max = np.argmax(vox_xcorr) - lag_index_correction elif len(xcorr_maxima) == 1: interp_max = parabolic(vox_xcorr, xcorr_maxima[0])[0] interp_max = interp_max - lag_index_correction elif len(xcorr_maxima) > 1: xpeak = xcorr_maxima[np.argmax(vox_xcorr[xcorr_maxima])] interp_max = parabolic(vox_xcorr, xpeak)[0] interp_max = interp_max - lag_index_correction xcorr_array.append(interp_max) return (masking.unmask(np.array(xcorr_array), brain_mask_img))
def generate_nifti_windows(fun, loc_volume, av_win, bm, smooth_f, fdir): fun_img = {} for e in ERPs: fun_data = np.zeros((bm.shape[0], bm.shape[1], bm.shape[2])) for iv, (xv, yv, zv) in enumerate(loc_volume): # Put the time average of the functional data over a time window av_win long fun_data[xv, yv, zv] = np.average(fun[iv, ERPs[e][0][0]:ERPs[e][0][1]]) fun_img[e] = nib.Nifti2Image(fun_data, affine=bm.affine) # Apply smoothing fun_img[e] = smooth_img(fun_img[e], smooth_f) # Apply bm mask fun_img[e] = unmask(apply_mask(fun_img[e], bm), bm) fout = out_dir + '{}_S{:02d}.nii.gz'.format(e, smooth_f) print('Nifti saved in {}'.format(fout)) nib.save(fun_img[e], fout) return fun_img
plt.ylabel('Explained variance') plt.savefig(spenc_dir+'MaThe/plots/'+\ '{}_r2_subj_{}.svg'.format('_'.join(model_scores.keys()), subj)) plt.close() # best voxels as a violin plot sns.violinplot(pd.DataFrame( {model:get_best_voxels(scores, voxel_limit) for model, scores in model_scores.iteritems()}), cut=0) plt.savefig(spenc_dir+'MaThe/plots/'+\ '{}_violin_subj_{}.svg'.format('_'.join(model_scores.keys()), subj)) plt.close() subj_mask = spenc_dir+\ 'temporal_lobe_mask_head_subj{0:02}bold.nii.gz'.format(subj) background_img = '/home/data/psyinf/forrest_gump/anondata/sub{0:03}/'.format(subj)+\ 'templates/bold7Tp1/head.nii.gz' # plotting the statistical map on the brain for model, scores in model_scores.iteritems(): unmasked = unmask(scores, subj_mask) # thresholding unmasked = threshold_img(unmasked, threshold) display = plot_stat_map( unmasked, bg_img=background_img, symmetric_cbar=False, title='Explained variance per voxel ({})'.format(model), vmax=max_r2, draw_cross=False) plt.savefig(spenc_dir+'MaThe/plots/'+\ '{}_r2_map_subj_{}.svg'.format(model, subj)) plt.close()
def test_unmask(): # A delta in 3D shape = (10, 20, 30, 40) generator = np.random.RandomState(42) data4D = generator.rand(*shape) data3D = data4D[..., 0] mask = generator.randint(2, size=shape[:3]) mask_img = Nifti1Image(mask, np.eye(4)) mask = mask.astype(bool) masked4D = data4D[mask, :].T unmasked4D = data4D.copy() unmasked4D[-mask, :] = 0 masked3D = data3D[mask] unmasked3D = data3D.copy() unmasked3D[-mask] = 0 # 4D Test, test value ordering at the same time. t = unmask(masked4D, mask_img, order="C").get_data() assert_equal(t.ndim, 4) assert_true(t.flags["C_CONTIGUOUS"]) assert_false(t.flags["F_CONTIGUOUS"]) assert_array_equal(t, unmasked4D) t = unmask([masked4D], mask_img, order="F") t = [t_.get_data() for t_ in t] assert_true(isinstance(t, list)) assert_equal(t[0].ndim, 4) assert_false(t[0].flags["C_CONTIGUOUS"]) assert_true(t[0].flags["F_CONTIGUOUS"]) assert_array_equal(t[0], unmasked4D) # 3D Test - check both with Nifti1Image and file for create_files in (False, True): with write_tmp_imgs(mask_img, create_files=create_files) as filename: t = unmask(masked3D, filename, order="C").get_data() assert_equal(t.ndim, 3) assert_true(t.flags["C_CONTIGUOUS"]) assert_false(t.flags["F_CONTIGUOUS"]) assert_array_equal(t, unmasked3D) t = unmask([masked3D], filename, order="F") t = [t_.get_data() for t_ in t] assert_true(isinstance(t, list)) assert_equal(t[0].ndim, 3) assert_false(t[0].flags["C_CONTIGUOUS"]) assert_true(t[0].flags["F_CONTIGUOUS"]) assert_array_equal(t[0], unmasked3D) # Error test: shape vec_1D = np.empty((500,), dtype=np.int) assert_raises(TypeError, unmask, vec_1D, mask_img) assert_raises(TypeError, unmask, [vec_1D], mask_img) vec_2D = np.empty((500, 500), dtype=np.float64) assert_raises(TypeError, unmask, vec_2D, mask_img) assert_raises(TypeError, unmask, [vec_2D], mask_img) # Error test: mask type assert_raises_regex(TypeError, 'mask must be a boolean array', _unmask_3d, vec_1D, mask.astype(np.int)) assert_raises_regex(TypeError, 'mask must be a boolean array', _unmask_4d, vec_2D, mask.astype(np.float64)) # Transposed vector transposed_vector = np.ones((np.sum(mask), 1), dtype=np.bool) assert_raises_regex(TypeError, 'X must be of shape', unmask, transposed_vector, mask_img)
def test_unmask(): # A delta in 3D shape = (10, 20, 30, 40) generator = np.random.RandomState(42) data4D = generator.rand(*shape) data3D = data4D[..., 0] mask = generator.randint(2, size=shape[:3]) mask_img = Nifti1Image(mask, np.eye(4)) mask = mask.astype(bool) masked4D = data4D[mask, :].T unmasked4D = data4D.copy() unmasked4D[np.logical_not(mask), :] = 0 masked3D = data3D[mask] unmasked3D = data3D.copy() unmasked3D[np.logical_not(mask)] = 0 # 4D Test, test value ordering at the same time. t = get_data(unmask(masked4D, mask_img, order="C")) assert_equal(t.ndim, 4) assert_true(t.flags["C_CONTIGUOUS"]) assert_false(t.flags["F_CONTIGUOUS"]) assert_array_equal(t, unmasked4D) t = unmask([masked4D], mask_img, order="F") t = [get_data(t_) for t_ in t] assert_true(isinstance(t, list)) assert_equal(t[0].ndim, 4) assert_false(t[0].flags["C_CONTIGUOUS"]) assert_true(t[0].flags["F_CONTIGUOUS"]) assert_array_equal(t[0], unmasked4D) # 3D Test - check both with Nifti1Image and file for create_files in (False, True): with write_tmp_imgs(mask_img, create_files=create_files) as filename: t = get_data(unmask(masked3D, filename, order="C")) assert_equal(t.ndim, 3) assert_true(t.flags["C_CONTIGUOUS"]) assert_false(t.flags["F_CONTIGUOUS"]) assert_array_equal(t, unmasked3D) t = unmask([masked3D], filename, order="F") t = [get_data(t_) for t_ in t] assert_true(isinstance(t, list)) assert_equal(t[0].ndim, 3) assert_false(t[0].flags["C_CONTIGUOUS"]) assert_true(t[0].flags["F_CONTIGUOUS"]) assert_array_equal(t[0], unmasked3D) # Error test: shape vec_1D = np.empty((500, ), dtype=np.int) assert_raises(TypeError, unmask, vec_1D, mask_img) assert_raises(TypeError, unmask, [vec_1D], mask_img) vec_2D = np.empty((500, 500), dtype=np.float64) assert_raises(TypeError, unmask, vec_2D, mask_img) assert_raises(TypeError, unmask, [vec_2D], mask_img) # Error test: mask type assert_raises_regex(TypeError, 'mask must be a boolean array', _unmask_3d, vec_1D, mask.astype(np.int)) assert_raises_regex(TypeError, 'mask must be a boolean array', _unmask_4d, vec_2D, mask.astype(np.float64)) # Transposed vector transposed_vector = np.ones((np.sum(mask), 1), dtype=np.bool) assert_raises_regex(TypeError, 'X must be of shape', unmask, transposed_vector, mask_img)
dset.coordinates, mask=dset.masker.mask_img, n_topics=10, n_regions=4, symmetric=True, ) model.fit(n_iters=100, loglikely_freq=20) model.save("gclda_model.pkl.gz") # Let's remove the model now that you know how to generate it. os.remove("gclda_model.pkl.gz") ############################################################################### # Look at topics # ----------------------------------------------------------------------------- topic_img_4d = masking.unmask(model.p_voxel_g_topic_.T, model.mask) for i_topic in range(5): topic_img_3d = image.index_img(topic_img_4d, i_topic) plotting.plot_stat_map( topic_img_3d, draw_cross=False, colorbar=False, annotate=False, title=f"Topic {i_topic + 1}", ) ############################################################################### # Generate a pseudo-statistic image from text # ----------------------------------------------------------------------------- text = "dorsal anterior cingulate cortex" encoded_img, _ = decode.encode.gclda_encode(model, text)
expl_var_pc[model] = pca.explained_variance_ratio_[:10] fig = plt.figure(figsize=(6, 6)) filtered = pca.transform(model_preds[:, voxels_to_use]) PC_rep[model] = filtered scatter = plt.scatter(filtered[:, 0], filtered[:, 1], cmap='YlGnBu', c=logenergy, marker='o', alpha=0.3) plt.xlabel('1st PC') plt.ylabel('2nd PC') plt.title('{0}, 1st PC r w/ log stim energy {1:.2f}'.format(model, corrcoef(filtered[:, 0], logenergy)[0,1])) plt.savefig(spenc_dir+'MaThe/plots/feature_space/two_PCs_{}_subj_{}.svg'.format(model, subj)) plt.close() subj_mask = spenc_dir + 'temporal_lobe_mask_head_subj{0:02}bold.nii.gz'.format(subj) background_img = '/home/data/psyinf/forrest_gump/anondata/sub{0:03}/'.format(subj) + 'templates/bold7Tp1/head.nii.gz' scores[scores<threshold] = 0 unmasked = unmask(scores, subj_mask) unmasked = threshold_img(unmasked, threshold) display = plot_stat_map( unmasked, bg_img=background_img, symmetric_cbar=False, aspect=1.25, vmax=max_r2, threshold=threshold, draw_cross=False, dim=-1.) fig = plt.gcf() fig.set_size_inches(12, 4) plt.savefig(spenc_dir+'MaThe/plots/feature_space/PC_model_{}_subj_{}_pcnr_0.svg'.format(model, subj)) plt.close() display = plot_stat_map( unmasked, bg_img=background_img, symmetric_cbar=False, aspect=1.25, vmax=max_r2, threshold=threshold, dim=-1.) plt.savefig(spenc_dir+'MaThe/plots/feature_space/PC_model_{}_subj_{}_pcnr_0_cross.svg'.format(model, subj)) plt.close()
brain_mask = sys.argv[2] gm_mask = sys.argv[3] max_lag = int(sys.argv[4]) out_dir = sys.argv[5] out_prefix = sys.argv[6] smooth_kernel = sys.argv[7] lags = range(-max_lag, max_lag+1) brain_mask_img = nib.load(brain_mask) brain_mask_data = brain_mask_img.get_data().astype(bool) gm_mask_img = nib.load(gm_mask) gm_mask_data = gm_mask_img.get_data().astype(bool) epi_img = nib.load(epi) epi_img_resampled = image.resample_img(epi_img, target_affine=brain_mask_img.get_affine(), target_shape=brain_mask_img.shape) epi_data_resampled = epi_img_resampled.get_data() lag_map_data = gen_lag_map(epi_data_resampled, brain_mask_data, gm_mask_data, lags) lag_map_image = masking.unmask(lag_map_data, brain_mask_img) lag_map_image_smoothed = image.smooth_img(lag_map_image, float(smooth_kernel)) nib.save(lag_map_image, os.path.join(out_dir, out_prefix+'_lag{}.nii.gz'.format(str(max_lag)))) nib.save(lag_map_image_smoothed, os.path.join(out_dir, out_prefix+'_lag{}_smoothed{}.nii.gz'.format(str(max_lag),smooth_kernel)))
def remove_mean(**kwargs): default_conf = { 'subject': ['110929angque','110929anngio','111004edolat'], 'experiment':['memory', 'decision'], 'level': [1,3,5], 'ds_num' : [1,2,3,4,5] , 'ds_type':['BETA'], 'file_pattern': \ "%s_%s_%s_MVPA_evidence_%s_balance_ds_%s_radius_3_searchlight_total_map.nii.gz", 'dir_pattern': "%s_%s_%s_MVPA_evidence_%s_balance_ds_%s" , "path": "/home/robbis/mount/fmri/memory/0_results/sl_k_3/", "mask_dir": "/home/robbis/mount/fmri/data7/Carlo_MDM/" } from nilearn.masking import apply_mask, unmask default_conf.update(kwargs) dir_pattern = default_conf.pop("dir_pattern") file_pattern = default_conf.pop("file_pattern") path = default_conf.pop("path") mask_dir = default_conf.pop("mask_dir") key_dict = dict(zip(default_conf.keys(), range(len(default_conf.keys())))) conf_product = itertools.product(*default_conf.values()) for elements in conf_product: subject = elements[key_dict['subject']] experiment = elements[key_dict['experiment']] level = elements[key_dict['level']] ds_num = elements[key_dict['ds_num']] ds_type = elements[key_dict['ds_type']] if experiment == 'memory' and ds_num > 1: continue file_path = os.path.join(path, dir_pattern % (subject, experiment, ds_type, level, str(ds_num)), file_pattern % (subject, experiment, ds_type, level, str(ds_num)), ) img = ni.load(file_path) mask_path = os.path.join(mask_dir, subject, "RESIDUALS_MVPA", "brain_mask.nii.gz", ) mask_img = ni.load(mask_path) masked_data = apply_mask(img, mask_img) mean_ = masked_data.mean() demeaned_data = masked_data - mean_ demeaned_img = unmask(demeaned_data, mask_img) save_pattern = file_pattern % (subject, experiment, ds_type, level, str(ds_num) ) save_pattern_ = save_pattern[:-7]+"_demeaned.nii.gz" save_path = os.path.join(path, dir_pattern % (subject, experiment, ds_type, level, str(ds_num)), save_pattern_ ) print subject, level, experiment, ds_num, mean_ ni.save(demeaned_img, save_path) mean_img = unmask(masked_data.mean(0), mask_img) save_pattern_ = save_pattern[:-7]+"_mean.nii.gz" save_path = os.path.join(path, dir_pattern % (subject, experiment, ds_type, level, str(ds_num)), save_pattern_ ) ni.save(mean_img, save_path) demeaned_mean = masked_data.mean(0) - masked_data.mean(0).mean() demeaned_mean_img = unmask(demeaned_mean, mask_img) save_pattern_ = save_pattern[:-7]+"_mean_demenead.nii.gz" save_path = os.path.join(path, dir_pattern % (subject, experiment, ds_type, level, str(ds_num)), save_pattern_ ) ni.save(demeaned_mean_img, save_path)
model_argmax = np.argmax([model_scores[model] for model in sorted(models)], axis=0) sorted_score_list = np.sort([model_scores[model] for model in sorted(models)], axis=0) model_diffs = sorted_score_list[-1] - sorted_score_list[-2] diff_max = np.max(model_diffs) for i, model in enumerate(sorted(models)): model_curr_diff = np.zeros_like(model_diffs) elements = np.logical_and(nonz_perf, model_argmax==i) model_curr_diff[elements] = model_diffs[elements] unmasked = unmask(model_curr_diff, subj_mask) unmasked = threshold_img(unmasked, threshold) if i == 0: display = plot_stat_map( unmasked, bg_img=background_img, symmetric_cbar=False, title=' '.join(['{}:{}'.format(m,n) for m,n in zip(models, cmap_names)]), threshold=threshold, cmap=cmap_cycle[i], vmax=diff_max, draw_cross=False, display_mode='z', cut_coords=6, colorbar=False, dim=-.5) else: display.add_overlay(unmasked, cmap=cmap_cycle[i], threshold=threshold) fig = plt.gcf() fig.set_size_inches(16, 12) plt.savefig(spenc_dir+'MaThe/plots/'+\ 'diff_comparison_subj_{}_models_{}.svg'.format(subj, '_'.join(models)))