def p_roi_masking(substitution, ts_file_template, beta_file_template, p_file_template, design_file_template, event_file_template, p_level, brain_mask): """Apply a substitution pattern to timecourse, beta, and design file templates - and mask the data of the former two according to a roi. Subsequently scale the design by the mean beta. Parameters ---------- substitution : dict A dictionary containing the template replacement fields as keys and identifiers as values. ts_file_template : string Timecourse file template with replacement fields. The file should be in NIfTI format. beta_file_template : string Beta file template with replacement fields. The file should be in NIfTI format. design_file_template : string Design file template with replacement fields. The file should be in CSV format. roi_path : string Path to the region of interest file based on which to create a mask for the time course and beta files. The file should be in NIfTI format. brain_mask : string Path to the a mask file in the *exact same* coordinate space as the input image. This is very important, as the mask is needed to crop out artefactual p=0 values. These cannot just be filtered out nummerically, since it is possible that the GLM resturns p=0 for the most significant results. Returns ------- timecourse : array_like Numpy array containing the mean timecourse in the region of interest. design : array_like Numpy array containing the regressor scaled by the mean beta value of the region of interest.. mask_map : data Nibabel image of the mask subplot_title : string Title for the subplot, computed from the substitution fields. """ ts_file = path.abspath( path.expanduser(ts_file_template.format(**substitution))) beta_file = path.abspath( path.expanduser(beta_file_template.format(**substitution))) p_file = path.abspath( path.expanduser(p_file_template.format(**substitution))) design_file = path.abspath( path.expanduser(design_file_template.format(**substitution))) event_file = path.abspath( path.expanduser(event_file_template.format(**substitution))) brain_mask = path.abspath(path.expanduser(brain_mask)) try: img = nib.load(p_file) brain_mask = nib.load(brain_mask) except (FileNotFoundError, nib.py3k.FileNotFoundError): return None, None, None, None, None data = img.get_data() brain_mask = brain_mask.get_data() header = img.header affine = img.affine shape = data.shape data = data.flatten() brain_mask = brain_mask.flatten() brain_mask = brain_mask.astype(bool) brain_data = data[brain_mask] reject, nonzero_data, _, _ = multipletests(brain_data, p_level, method="fdr_bh") brain_mask[brain_mask] = reject brain_mask = brain_mask.astype(int) mask = brain_mask.reshape(shape) mask_map = nib.Nifti1Image(mask, affine, header) masker = NiftiMasker(mask_img=mask_map) try: timecourse = masker.fit_transform(ts_file).T betas = masker.fit_transform(beta_file).T except ValueError: return None, None, None, None, None subplot_title = "\n ".join( [str(substitution["subject"]), str(substitution["session"])]) timecourse = np.mean(timecourse, axis=0) design = pd.read_csv(design_file, skiprows=5, sep="\t", header=None, index_col=False) design = design * np.mean(betas) event_df = pd.read_csv(event_file, sep="\t") return timecourse, design, mask_map, event_df, subplot_title
def read_data_haxby(subject, tr=2.5): """ Applies anova feature selection to fmri data using classification accuracy on stimuli data as measure of performance Parameters ---------- subject: int from 1 to 6 subject from which to load haxby dataset data Returns ------- fmri: numpy array of shape [n_scans, n_voxels] feature-selected train data from the fmri sessions stimuli: numpy array of shape [n_scans, n_categories] time series of the stimuli with one-hot encoding onsets: array of shape [n_sessions, n_stimuli] onset times for stimuli conditions: array of shape [n_sessions, n_stimuli] labels for stimuli """ haxby_dataset = fetch_haxby(subjects=[subject]) # Load fmri data fmri_filename = haxby_dataset.func[0] fmri = load_img(fmri_filename) # mask = haxby_dataset.mask_vt[0] masker = NiftiMasker(mask_strategy='epi', standardize=True, detrend=True, high_pass=0.01, t_r=tr, smoothing_fwhm=5) fmri = masker.fit_transform(fmri) fmri = fmri.reshape(12, -1, fmri.shape[-1]) # Load stimuli data classes = np.array([ 'rest', 'face', 'house', 'bottle', 'cat', 'chair', 'scissors', 'shoe', 'scrambledpix' ]) labels = np.recfromcsv(haxby_dataset.session_target[0], delimiter=" ")['labels'].reshape(12, -1) stimuli, onsets, conditions = (np.zeros( (12, len(labels[0]), len(classes))), [], []) stimuli[0, 0] = 1 for session in range(12): onsets.append([]) conditions.append([]) for scan in range(1, len(fmri[session])): if (labels[session][scan - 1] == 'rest' and labels[session][scan] != 'rest'): label = labels[session][scan] stimuli[session, scan, np.where(classes == label)[0][0]] = 1 conditions[session].append(label) onsets[session].append(scan * tr) else: stimuli[session, scan, 0] = 1 # Remove ninth run for subject 5 (corrupted) if subject == 5: fmri = np.vstack((fmri[:8], fmri[9:])) stimuli = np.vstack((stimuli[:8], stimuli[9:])) onsets = np.vstack((onsets[:8], onsets[9:])) conditions = np.vstack((conditions[:8], conditions[9:])) onsets = np.asarray(onsets) conditions = np.asarray(conditions) return fmri, stimuli, onsets, conditions
############################################################################## # Load Haxby dataset from nilearn import datasets haxby_dataset = datasets.fetch_haxby(subjects=[2]) # print basic information on the dataset print('Mask nifti image (3D) is located at: %s' % haxby_dataset.mask) print('Functional nifti image (4D) is located at: %s' % haxby_dataset.func[0]) ############################################################################## # Mask data mask_filename = haxby_dataset.mask from nilearn.input_data import NiftiMasker nifti_masker = NiftiMasker(smoothing_fwhm=8, mask_img=mask_filename, memory='nilearn_cache', memory_level=1) # cache options func_filename = haxby_dataset.func[0] fmri_masked = nifti_masker.fit_transform(func_filename) ############################################################################## # Restrict to faces and houses import numpy as np import pandas as pd labels = pd.read_csv(haxby_dataset.session_target[0], sep=" ") conditions = labels['labels'] categories = conditions.unique() conditions_encoded = np.zeros_like(conditions) for c, category in enumerate(categories): conditions_encoded[conditions == category] = c sessions = labels['chunks']
def map_threshold(stat_img, mask_img=None, threshold=.001, height_control='fpr', cluster_threshold=0): """ Threshold the provided map Parameters ---------- stat_img : Niimg-like object, statistical image (presumably in z scale) mask_img : Niimg-like object, optional, mask image threshold: float, optional cluster forming threshold (either a p-value or z-scale value) height_control: string, optional false positive control meaning of cluster forming threshold: 'fpr'|'fdr'|'bonferroni'|'none' cluster_threshold : float, optional cluster size threshold Returns ------- thresholded_map : Nifti1Image, the stat_map theresholded at the prescribed voxel- and cluster-level threshold: float, the voxel-level threshold used actually """ # Masking if mask_img is None: masker = NiftiMasker(mask_strategy='background').fit(stat_img) else: masker = NiftiMasker(mask_img=mask_img).fit() stats = np.ravel(masker.transform(stat_img)) n_voxels = np.size(stats) # Thresholding if height_control == 'fpr': z_th = norm.isf(threshold) elif height_control == 'fdr': z_th = fdr_threshold(stats, threshold) elif height_control == 'bonferroni': z_th = norm.isf(threshold / n_voxels) else: # Brute-force thresholding z_th = threshold stats *= (stats > z_th) # embed it back to 3D grid stat_map = masker.inverse_transform(stats).get_data() # Extract connected components above threshold label_map, n_labels = label(stat_map > z_th) labels = label_map[masker.mask_img_.get_data() > 0] for label_ in range(1, n_labels + 1): if np.sum(labels == label_) < cluster_threshold: stats[labels == label_] = 0 return masker.inverse_transform(stats), z_th
### Restrict to faces and houses ############################################## condition_mask = np.logical_or(conditions == b'face', conditions == b'house') y = y[condition_mask] conditions = conditions[condition_mask] # We have 2 conditions n_conditions = np.size(np.unique(y)) ### Loading step ############################################################## from nilearn.input_data import NiftiMasker mask_filename = haxby_dataset.mask # For decoding, standardizing is often very important nifti_masker = NiftiMasker(mask_img=mask_filename, sessions=session, smoothing_fwhm=4, standardize=True, memory="nilearn_cache", memory_level=1) func_filename = haxby_dataset.func X = nifti_masker.fit_transform(func_filename) # Apply our condition_mask X = X[condition_mask] session = session[condition_mask] ### Prediction function ####################################################### ### Define the prediction function to be used. # Here we use a Support Vector Classification, with a linear kernel from sklearn.svm import SVC svc = SVC(kernel='linear')
import pandas as pd import json import os from json import JSONDecodeError from os.path import join import numpy as np from nilearn.input_data import NiftiMasker from cogspaces.datasets import fetch_mask from cogspaces.pipeline import get_output_dir basedir = join(get_output_dir(), 'multi_decompose', '3', 'run') mask = fetch_mask() masker = NiftiMasker(mask_img=mask).fit() res = [] for exp_dir in os.listdir(basedir): print(basedir) try: id_exp = int(exp_dir) except: continue exp_dir = join(basedir, exp_dir) # Loosy decompose.py artifact_dir = join(get_output_dir(), 'decompose', str(id_exp), 'artifacts') try: config = json.load(open(join(exp_dir, 'config.json'), 'r')) info = json.load(open(join(exp_dir, 'info.json'), 'r'))
def plot_melodic_components(melodic_dir, in_file, tr=None, out_file='melodic_reportlet.svg', compress='auto', report_mask=None, noise_components_file=None): from nilearn.image import index_img, iter_img import nibabel as nb import numpy as np import pylab as plt import seaborn as sns from matplotlib.gridspec import GridSpec import os import re from io import StringIO sns.set_style("white") current_palette = sns.color_palette() in_nii = nb.load(in_file) if not tr: tr = in_nii.header.get_zooms()[3] units = in_nii.header.get_xyzt_units() if units: if units[-1] == 'msec': tr = tr / 1000.0 elif units[-1] == 'usec': tr = tr / 1000000.0 elif units[-1] != 'sec': NIWORKFLOWS_LOG.warning('Unknown repetition time units ' 'specified - assuming seconds') else: NIWORKFLOWS_LOG.warning( 'Repetition time units not specified - assuming seconds') from nilearn.input_data import NiftiMasker from nilearn.plotting import cm if not report_mask: nifti_masker = NiftiMasker(mask_strategy='epi') nifti_masker.fit(index_img(in_nii, range(2))) mask_img = nifti_masker.mask_img_ else: mask_img = nb.load(report_mask) mask_sl = [] for j in range(3): mask_sl.append(transform_to_2d(mask_img.get_data(), j)) timeseries = np.loadtxt(os.path.join(melodic_dir, "melodic_mix")) power = np.loadtxt(os.path.join(melodic_dir, "melodic_FTmix")) stats = np.loadtxt(os.path.join(melodic_dir, "melodic_ICstats")) n_components = stats.shape[0] Fs = 1.0 / tr Ny = Fs / 2 f = Ny * (np.array(list(range(1, power.shape[0] + 1)))) / (power.shape[0]) n_rows = int((n_components + (n_components % 2)) / 2) fig = plt.figure(figsize=(6.5 * 1.5, n_rows * 0.85)) gs = GridSpec(n_rows * 2, 9, width_ratios=[1, 1, 1, 4, 0.001, 1, 1, 1, 4, ], height_ratios=[1.1, 1] * n_rows) noise_components = None if noise_components_file: noise_components = np.loadtxt(noise_components_file, dtype=int, delimiter=',', ndmin=1) for i, img in enumerate( iter_img(os.path.join(melodic_dir, "melodic_IC.nii.gz"))): col = i % 2 row = int(i / 2) l_row = row * 2 # Set default colors color_title = 'k' color_time = current_palette[0] color_power = current_palette[1] if noise_components is not None and noise_components.size > 0: # If a noise components list is provided, assign red/green color_title = color_time = color_power = ( 'r' if (i + 1) in noise_components else 'g') data = img.get_data() for j in range(3): ax1 = fig.add_subplot(gs[l_row:l_row + 2, j + col * 5]) sl = transform_to_2d(data, j) m = np.abs(sl).max() ax1.imshow(sl, vmin=-m, vmax=+m, cmap=cm.cold_white_hot, interpolation="nearest") ax1.contour(mask_sl[j], levels=[0.5], colors='k', linewidths=0.5) plt.axis("off") ax1.autoscale_view('tight') if j == 0: ax1.set_title( "C%d: Tot. var. expl. %.2g%%" % (i + 1, stats[i, 1]), x=0, y=1.18, fontsize=7, horizontalalignment='left', verticalalignment='top', color=color_title) ax2 = fig.add_subplot(gs[l_row, 3 + col * 5]) ax3 = fig.add_subplot(gs[l_row + 1, 3 + col * 5]) ax2.plot(np.arange(len(timeseries[:, i])) * tr, timeseries[:, i], linewidth=min(200 / len(timeseries[:, i]), 1.0), color=color_time) ax2.set_xlim([0, len(timeseries[:, i]) * tr]) ax2.axes.get_yaxis().set_visible(False) ax2.autoscale_view('tight') ax2.tick_params(axis='both', which='major', pad=0) sns.despine(left=True, bottom=True) for tick in ax2.xaxis.get_major_ticks(): tick.label.set_fontsize(6) tick.label.set_color(color_time) ax3.plot(f[0:], power[0:, i], color=color_power, linewidth=min(100 / len(power[0:, i]), 1.0)) ax3.set_xlim([f[0], f.max()]) ax3.axes.get_yaxis().set_visible(False) ax3.autoscale_view('tight') ax3.tick_params(axis='both', which='major', pad=0) for tick in ax3.xaxis.get_major_ticks(): tick.label.set_fontsize(6) tick.label.set_color(color_power) sns.despine(left=True, bottom=True) plt.subplots_adjust(hspace=0.5) image_buf = StringIO() fig.savefig(image_buf, dpi=300, format='svg', transparent=True, bbox_inches='tight', pad_inches=0.01) fig.clf() image_svg = image_buf.getvalue() if compress is True or compress == 'auto': image_svg = svg_compress(image_svg, compress) image_svg = re.sub(' height="[0-9]+[a-z]*"', '', image_svg, count=1) image_svg = re.sub(' width="[0-9]+[a-z]*"', '', image_svg, count=1) image_svg = re.sub(' viewBox', ' preseveAspectRation="xMidYMid meet" viewBox', image_svg, count=1) with open(out_file, 'w' if PY3 else 'wb') as f: f.write(image_svg)
os.mkdir(WRITE_DIR) ############################################################################## # load+preprocess data ############################################################################## print('Loading data...') # load the information + niftis from SPM analyses mat_paths = glob.glob( '/Volumes/TRESOR/houpand/1st_level_unnormalized/*/SPM.mat') DMP_FNAME = 'FS_labels_sublabels_dump__' if op.exists(DMP_FNAME): FS, labels, sub_labels = joblib.load(DMP_FNAME) masker = NiftiMasker(mask_img=('debug_mask.nii')) masker.fit() else: # load the data from scratch cond_labels = [] sub_labels = [] nii_paths = [] # 5 trials per block; cond_names = [ 'nehmen', 'fangen', 'rollen', 'Ring', 'Kugel', 'Zylinder', 'Ring_3D', 'Kugel_3D', 'Zylinder_3D', 'Ring_Hand', 'Kugel_Hand', 'Zylinder_Hand' ] for ipath, mpath in enumerate(mat_paths): m = loadmatnow(mpath)
MASK_DIR = 'masks' SUBJ_COUNT = 6 STIM_LENGTH = 9 masks_list = [] func_masked = [] for subj in range(1, SUBJ_COUNT + 1): print('Subject ' + str(subj)) # Retreive Masks for Subject mask_dir = os.path.join(MASK_DIR, 'subj' + str(subj), 'mask4_vt.nii.gz') masks_list.append(nib.load(mask_dir)) masker = NiftiMasker(mask_img=mask_dir, smoothing_fwhm=4, standardize=True, memory="nilearn_cache", memory_level=1) # Get FMRI and Label Directories for Subject func_dir = os.path.join(DATA_DIR, 'sub-' + str(subj), 'func\\*.gz') func_evt_dir = os.path.join(DATA_DIR, 'sub-' + str(subj), 'func\\*.tsv') func_files = glob(func_dir) func_evt_dir = glob(func_evt_dir) # Temp Lists for Subject func_masked_temp = [] run_cnt = 1 for file, evt in zip(func_files, func_evt_dir): print('Run ', run_cnt)
Returns: Sampled data through mask position, and normalized ''' q, w = maskpos.shape newdata = np.zeros(q) for i in range(q): x, y, z = maskpos[i, :] newdata[i] = data[int(x), int(y), int(z)] return newdata / (newdata.sum()) data = image.load_img("200.nii.gz") dataS = smooth_img(data, fwhm=None) masker = NiftiMasker(mask_img=mask_img, smoothing_fwhm=8.0, memory='nilearn_cache', memory_level=1) masker = masker.fit() data2d = masker.transform(dataS) data3d = masker.inverse_transform(data2d) odata = data3d.get_fdata() ''' Loading ADHD200 preprocessed data, manipulate with basic preprocessing method, include dismiss infinite points, Gaussin smooth with 8x8x8 mm kernel in MNI space Masking with MNI gray matter mask Converting back to 3-D array data, convert to numpy format ''' odata = odata[:, :, :, 9:172] for i in range(162): d1 = datamask(odata[:, :, :, i], spos)
process_mask_img=process_mask_img, radius=5.6, n_jobs=n_jobs, verbose=1, cv=cv) searchlight.fit(fmri_img, y) ######################################################################### # F-scores computation # ---------------------- from nilearn.input_data import NiftiMasker # For decoding, standardizing is often very important nifti_masker = NiftiMasker(mask_img=mask_img, sessions=session, standardize=True, memory='nilearn_cache', memory_level=1) fmri_masked = nifti_masker.fit_transform(fmri_img) from sklearn.feature_selection import f_classif f_values, p_values = f_classif(fmri_masked, y) p_values = -np.log10(p_values) p_values[p_values > 10] = 10 p_unmasked = get_data(nifti_masker.inverse_transform(p_values)) ######################################################################### # Visualization # -------------- # Use the fmri mean image as a surrogate of anatomical data from nilearn import image
average_ana = os.path.join(outpath, 'CS_avg_mprage_image.nii.gz') imag_mask = os.path.join(outpath, 'power_roimask_4bi.nii.gz') #plot mask (Power ROIs) over anatomical that is defined above #plotting.plot_roi(imag_mask,bg_img=average_ana,cmap='Paired') #load labels for the functional data #stim = os.path.join('/projects','niblab','scripts','nilean_stuff','label_67_sub.csv') stim = os.path.join('/projects', 'niblab', 'scripts', 'nilean_stuff', 'label_all_sub.csv') orig_data = pd.read_csv(stim, sep=",") conditions = orig_data["label"] #'labels' for half_func condition_mask = orig_data['label'].isin(['unapp', 'app', 'rest', 'H2O']) conditions = conditions[condition_mask] print(conditions.unique()) n_conditions = np.size(np.unique(conditions)) print(n_conditions) session = orig_data[condition_mask].to_records(index=False) print(session.dtype.names) nifti_masker = NiftiMasker(mask_img=imag_mask, smoothing_fwhm=4, standardize=True, memory_level=1) X = nifti_masker.fit_transform(fmri_subjs) feature_selection = SelectKBest(f_classif, k=1500) anova_svc = Pipeline([('anova', feature_selection), ('svc', svc)]) np.warnings.filterwarnings('ignore')
def wrapper( model, files, result_dir, subject_id, interest, delete_tmp=False, ncore=1, memory_limit_per_core=4, CDT=5, ): """ A wrapper for the BWAS library. Parameters ---------- :paramater model: each column is a regressor, and each row is a subject. :type model: pandas data frame :parameter files: files[subject] is the file associated with subject :type files: dictionary :parameter result_dir: where to save the results of the analysis. :type output_folder: string. :parameter subject_id: the name of the column in model containing subject IDs :type subject_id: string :parameter interest: the name of the column of interest in model. This would be for example a dummy variable with 0s and 1s for a group comparison. The other columns will be used as confonds in the analysis. :type interest: string :parameter ncore: Number of CPU to use for this analysis. :type ncore: int, optional :parameter memory_limit_per_core: The maximum memory limits per CPU (GB) :type memory_limit_per_core: int, optional :parameter CDT: Cluster defining threshold (z-value), better >= 4.5 for whole brain analysis. :type CDT: float, optional :parameter delete_tmp: turn on/off the deletion of the temporary folder for the analysis :type delete_tmp: boolean, optional :return path_analysis: the temporary folder used to run the analysis. This folder may be automatically deleted on exit (see ``delete_tmp``). :type path_analysis: string. Note ---- The files need to point to *fully preprocessed* 4D nifti files. No detrending or smoothing can be applied. The analysis mask is automatically generated using nilearn. User-specified mask is not currently supported. """ # Create temporary file name structure with sym links # BWAS uses the alphabetical order in files to make a match with the model variables # This is a risky strategy: any discrepancy between the model row order and # files will result in a catastrophic (silent) failure. # To address this issue, bwas_wrapper uses a python dictionary to list files # and creates a collection of symlinks that matches the model order just for # the analysis path_analysis = tempfile.mkdtemp() path_model = tempfile.mkdtemp() print("Creating sym links in the following temporary folder: {0}".format( path_analysis)) imgs = [] for idx, subject in enumerate(model[subject_id]): file = f"{idx: 12}.nii.gz".replace(" ", "0") imgs.append(os.path.join(path_analysis, file)) print("linking {0} with {1}".format(files[subject], imgs[-1])) os.symlink(src=files[subject], dst=imgs[-1]) # Create the output folder if it does not exist if not os.path.exists(result_dir): os.makedirs(result_dir) print(f"Result directory {result_dir} created.") else: print(f"Storing results in {result_dir} (already exists).") # Create the files related to the model print( "Creating files related to the model in the following temporary folder: {0}" .format(path_model)) # Create a mask, if not specified mask_file = os.path.join(path_model, 'mask.nii.gz') print(f"Generating a brain mask for the analysis in {mask_file}") masker = NiftiMasker() masker.fit(imgs) mask = masker.mask_img_ mask.to_filename(mask_file) # dump the target in a numpy file targets_file = os.path.join(path_model, 'targets.npy') np.save(targets_file, model[interest].to_numpy()) # dump the covariates in a numpy file cov_file = os.path.join(path_model, 'covariates.npy') labels = model.columns cov = labels[[item not in [interest, subject_id] for item in labels]] np.save(cov_file, model[cov].to_numpy()) # dump the # Run BWAS print("Running BWAS") BWAS_cpu.BWAS_run_full_analysis( result_dir=result_dir, image_dir=path_analysis, mask_file=mask_file, toolbox_path=os.path.dirname(BWAS_cpu.__file__), targets_file=targets_file, cov_file=cov_file, CDT=CDT, memory_limit_per_core=memory_limit_per_core, ncore=ncore, ) if delete_tmp: rmtree(path_analysis) rmtree(path_model) return path_analysis
def roi_masking( substitution, ts_file_template, betas_file_template, design_file_template, event_file_template, roi, scale_design=True, ): """Apply a substitution pattern to timecourse, beta, and design file templates - and mask the data of the former two according to a roi; optionally scales the design by the mean beta. Parameters ---------- substitution : dict A dictionary containing the template replacement fields as keys and identifiers as values. ts_file_template : string Timecourse file template with replacement fields. The file should be in NIfTI format. beta_file_template : string Beta file template with replacement fields. The file should be in NIfTI format. design_file_template : string Design file template with replacement fields. The file should be in TSV format. roi_path : string Path to the region of interest file based on which to create a mask for the time course and beta files. The file should be in NIfTI format. scale_design : string Whether or not to scale the design timecourse by the mean beta of the region of interest. Returns ------- timecourse : array_like Numpy array containing the mean timecourse in the region of interest. design : array_like Numpy array containing the regressor scaled by the mean beta value of the region of interest.. mask_map : data Nibabel image of the mask subplot_title : string Title for the subplot, computed from the substitution fields. """ from nibabel import processing ts_file = path.expanduser(ts_file_template.format(**substitution)) betas_file = path.expanduser(betas_file_template.format(**substitution)) design_file = path.expanduser(design_file_template.format(**substitution)) event_file = path.expanduser(event_file_template.format(**substitution)) # We specify a target affine to avoid a nilearn MemoryError bug: https://github.com/nilearn/nilearn/issues/1883 try: ts_img = nib.load(ts_file) except ValueError: print('Not found:', '\n', ts_file) return None, None, None, None, None if isinstance(roi, str): mask_map = nib.load(roi) else: mask_map = roi masker = NiftiMasker(mask_img=roi, target_affine=ts_img.affine, memory=path.expanduser('~/.nilearn_cache'), memory_level=1) timecourse = masker.fit_transform(ts_img) timecourse = timecourse.T try: betas = masker.fit_transform(betas_file).T except ValueError: print('Not found:', '\n', betas_file) return None, None, None, None, None try: design = pd.read_csv(design_file, skiprows=5, sep="\t", header=None, index_col=False) except ValueError: print('Not found:', '\n', design_file) return None, None, None, None, None try: event_df = pd.read_csv(event_file, sep="\t") except ValueError: print('Not found:', '\n', event_file) return None, None, None, None, None try: subplot_title = "Subject {} | Session {}".format( str(substitution["subject"]), str(substitution["session"])) except KeyError: subplot_title = '' timecourse = np.mean(timecourse, axis=0) if scale_design: for ix, i in enumerate(betas.T): design[ix] = design[ix] * np.mean(i) return timecourse, design, mask_map, event_df, subplot_title
# scikit-learn >= 0.14 supports text labels. You can replace this line by: # target = labels['labels'] _, target = np.unique(labels['labels'], return_inverse=True) ### Keep only data corresponding to faces or cats ############################# condition_mask = np.logical_or(labels['labels'] == b'face', labels['labels'] == b'cat') target = target[condition_mask] ### Prepare the data: apply the mask ########################################## from nilearn.input_data import NiftiMasker mask_filename = haxby_dataset.mask_vt[0] # For decoding, standardizing is often very important nifti_masker = NiftiMasker(mask_img=mask_filename, standardize=True) func_filename = haxby_dataset.func[0] # We give the nifti_masker a filename and retrieve a 2D array ready # for machine learning with scikit-learn fmri_masked = nifti_masker.fit_transform(func_filename) # Restrict the classification to the face vs cat discrimination fmri_masked = fmri_masked[condition_mask] ### Prediction ################################################################ # Here we use a Support Vector Classification, with a linear kernel from sklearn.svm import SVC svc = SVC(kernel='linear')
import matplotlib.pyplot as plt import os from sklearn.model_selection import train_test_split from sklearn import linear_model from sklearn.metrics import mean_squared_error, r2_score from sklearn import decomposition as dcm from sklearn import preprocessing as pp from sklearn import ensemble import json from nilearn.input_data import NiftiMasker from sklearn.pipeline import Pipeline from sklearn.model_selection import GridSearchCV import pickle # load the nifti data masker = NiftiMasker(mask_img='../data/playground/overlap_mask_3mm.nii.gz') subs_2d = masker.fit_transform('../data/playground/subs_3mm.nii.gz') for csv in glob('../data/raw/subs-405*.csv'): behav_df = pd.read_csv(csv, converters={'id': lambda x: str(x).zfill(4)}) behav_df.set_index('id', inplace=True) behav_df X_train, X_test, y_train, y_test = train_test_split( subs_2d, behav_df['score'].values, test_size=0.10) # preprocessing strategy # PCA pca = dcm.PCA(svd_solver='full') spca = dcm.SparsePCA() sparse_alpha_opts = [0.1, 0.5, 1, 2, 5, 10] kpca = dcm.KernelPCA()
import nibabel as nib from fg_config import * from bids_model import bids_events from nilearn.input_data import NiftiMasker from nilearn.image import new_img_like from collections import OrderedDict from scipy.stats import ttest_1samp, ttest_ind, wilcoxon sl_dir = os.path.join(SCRATCH, 'searchlight') conditions = {'CS+': 'CSp', 'CS-': 'CSm'} groups = ['healthy', 'ptsd'] phase3 = ['baseline', 'acquisition', 'extinction'] phases = ['baseline', 'acquisition', 'early_extinction', 'extinction'] masker = NiftiMasker(mask_img=std_2009_brain_mask_3mm) masker.fit() mats = {} # mem_ for phase in phases: mats[phase] = np.zeros((len(all_sub_args), 69880)) for s, sub in enumerate(all_sub_args): print(sub) subj = bids_meta(sub) with open(os.path.join(subj.rsa, 'sl_er.p'), 'rb') as file: mat = pickle.load(file) mat = new_img_like(std_2009_brain_3mm, mat)
for element in ['boxes', 'whiskers', 'fliers', 'means', 'medians', 'caps']: plt.setp(bp[element], color='r', linewidth=3) plt.yticks([0, 1], ['correlation of average', 'mean correlation']) plt.axis([-95, -40, -.5, 1.5]) plt.title('Complexity of correlation matrices') plt.subplots_adjust(left=.35, bottom=.1, right=.95, top=.9) plt.savefig(os.path.join(cache, 'correlation_complexity.pdf')) if __name__ == '__main__': db = data_parser(derivatives=SMOOTH_DERIVATIVES, subject_list=PTS, task_list=TASKS) mask_gm = nib.load( os.path.join(DERIVATIVES, 'group', 'anat', 'gm_mask.nii.gz')) masker = NiftiMasker(mask_img=mask_gm, memory=mem).fit() ### ANOVAs ### # ## Compute the ANOVAs design_matrix, subject_map, contrast_map, acq_map = anova(db, masker) # ## Store them... subject_map.to_filename( os.path.join(cache, 'subject_effect' + '_' + suffix + '.nii.gz')) contrast_map.to_filename( os.path.join(cache, 'condition_effect' + '_' + suffix + '.nii.gz')) acq_map.to_filename( os.path.join(cache, 'acq_effect' + '_' + suffix + '.nii.gz')) # ## ...or load them # subject_map = os.path.join(cache, # 'subject_effect' + '_' + suffix + '.nii.gz')
behavioral["Label"] = behavioral.replace(['LF_HS_receipt', 'HF_HS_receipt', 'HF_LS_receipt'], 'yummy') behavioral["Label"] = behavioral.replace(['LF_LS_receipt'], 'yucky') y = behavioral["Label"] print(y.unique()) #restrict data to our target analysis condition_mask = behavioral["Label"].isin('yummy', "yucky", "rest"]) y = y[condition_mask] #confirm we have the # of condtions needed print(y.unique()) masker = NiftiMasker(mask_img=imag_mask, standardize=True, memory="nilearn_cache", memory_level=3) X = masker.fit_transform(dataset) # Apply our condition_mask X = X[condition_mask] scaler = StandardScaler() X = scaler.fit_transform(X) from sklearn.svm import SVC svc = SVC(kernel='linear', max_iter=1000, decision_function_shape="ovr") from sklearn.feature_selection import SelectKBest, f_classif feature_selection = SelectKBest(f_classif, k=500)
def test_nifti_masker_empty_mask_warning(): X = Nifti1Image(np.ones((2, 2, 2, 5)), np.eye(4)) with pytest.raises( ValueError, match="The mask is invalid as it is empty: it masks all data"): NiftiMasker(mask_strategy="epi").fit_transform(X)
# on a homogeneous background # Load Miyawaki dataset from nilearn import datasets miyawaki_dataset = datasets.fetch_miyawaki2008() # print basic information on the dataset print('First functional nifti image (4D) is located at: %s' % miyawaki_dataset.func[0]) # 4D data miyawaki_filename = miyawaki_dataset.func[0] miyawaki_mean_img = image.mean_img(miyawaki_filename) plot_epi(miyawaki_mean_img, title='Mean EPI image') ############################################################################### # A NiftiMasker with the default strategy masker = NiftiMasker() masker.fit(miyawaki_filename) # Plot the generated mask using the mask_img_ attribute plot_roi(masker.mask_img_, miyawaki_mean_img, title="Mask from already masked data") ############################################################################### # Plot the generated mask using the .generate_report method report = masker.generate_report() report ############################################################################### # Computing a mask from raw EPI data ###############################################################################
# Quality check / Remove subjects with bad tested variate mask_quality_check_right = np.where(tested_var_right != b'n/a')[0] n_samples_right = mask_quality_check_right.size contrast_map_filenames_right = [ localizer_dataset_right.cmaps[i] for i in mask_quality_check_right ] tested_var_right = tested_var_right[mask_quality_check_right].astype( float).reshape((-1, 1)) # --------------- # smoothing # --------------- smt = 5 nifti_masker = NiftiMasker(smoothing_fwhm=smt, memory='nilearn_cache', memory_level=1) # cache options fmri_masked_left = nifti_masker.fit_transform(contrast_map_filenames_left) fmri_masked_left = np.transpose(fmri_masked_left) #fmri_masked_left.shape fmri_masked_right = nifti_masker.fit_transform(contrast_map_filenames_right) fmri_masked_right = np.transpose(fmri_masked_right) #fmri_masked_right.shape columns_ok_left = ["left"] * fmri_masked_left.shape[1] columns_ok_right = ["right"] * fmri_masked_right.shape[1] columns_ok = columns_ok_left + columns_ok_right #len(columns_ok) # ---------------
def check_sample_homogeneity(imgs, mask_img, participant_ids, group_labels=None, idx_annotations=True, average_type='mean', metric='euclidean', fence_factor=1.5, title=None, dst_dir=None, filename=None, **niftimasker_kwargs): '''Check sMRI Image Sample Homogeneity using Distance Measurement. This function provides similar functionality as CAT12's fourth module called "Check Sample". Parameters ---------- imgs: list, pd.Series A list of image paths. mask_img: 3D Niimg-like object, None A mask image to mask images to brain space. If None, default settings of nilearn.input_data.NiftiMasker will be used. participant_ids: pd.Series A series containing unique ids for each subject. The unique ids are used for marking outliers in the boxplot. group_labels: pd.Series A series containing labels for different groups (e.g. 'patient', 'control') idx_annotations: Boolean If True, annotations will be plotted as row indices. In addition a textbox will be plotted that maps each index to the corresponding participant ID. average_type: str How should the average subject be calculated? Choose between 'mean' or 'median'. metric: str or callable The metric to use when calculating between instances in a feature array. See sklearn.metrics.pairwise.paired_distances. fence_factor: float The factor with which the interquartile range is multiplied with to obtain lower and upper bounds used for identifying outliers. title: str or None Title for the boxplot dst_dir: str or None A path pointing to the directory where the plot should be saved. If None, the plot is not saved. filename: str The name of the saved file. niftimasker_kwargs: key-value pairs Further arguments that are passed to nilearn.input_data.NiftiMasker Notes ----- See: http://www.neuro.uni-jena.de/vbm/check-sample-homogeneity/ ''' if not isinstance(imgs, np.ndarray): niftimasker = NiftiMasker(mask_img=mask_img, **niftimasker_kwargs) imgs_data = niftimasker.fit_transform(imgs) else: imgs_data = imgs # Calculate the average. Create an array of sample size with this average # data. if average_type == 'mean': avg_img_data = np.mean(imgs_data, axis=0, keepdims=True) elif average_type == 'median': avg_img_data = np.median(imgs_data, axis=0, keepdims=True) # calculate the distance from each subject to the average subject. distances = cdist(imgs_data, avg_img_data, metric=metric).ravel() # get outliers interquartile_range = iqr(distances) q1 = np.quantile(distances, 0.25) q3 = np.quantile(distances, 0.75) lower_bound = q1 - fence_factor * interquartile_range upper_bound = q3 + fence_factor * interquartile_range outlier_booleans = np.where( (distances < lower_bound) | (distances > upper_bound), True, False) outlier_ids = participant_ids[outlier_booleans] outlier_values = distances[outlier_booleans] outlier_indices = np.where(outlier_booleans)[0] # boxplot data plt.figure() flierprops = dict(marker='o', markerfacecolor='0.75', markersize=2.5, linestyle='none') boxplot = sns.boxplot(x=distances, whis=fence_factor, flierprops=flierprops) boxplot.set_xlabel('Distance to Average') if title: boxplot.set_title(title) if idx_annotations: # use row indices as annotations texts = [ plt.text(x=outlier_values[idx], y=0, s=outlier_idx) for idx, outlier_idx in enumerate(outlier_indices) ] # create textbox that maps row indices to participant ids textbox_strings = [ str(outlier_idx) + ': ' + outlier_id for outlier_idx, outlier_id in zip(outlier_indices.tolist(), outlier_ids.tolist()) ] sep = '\n' textbox_content = sep.join(textbox_strings) textbox_props = dict(boxstyle='round', facecolor='white', alpha=0.5) plt.gcf().text(1.0, 1.0, textbox_content, fontsize=8, verticalalignment='top', bbox=textbox_props) else: # use participant ids as annotations texts = [ plt.text(x=outlier_values[idx], y=0, s=outlier_id) for idx, outlier_id in enumerate(outlier_ids) ] # if group labels are provided, color texts in different colors for each # group and add legend if group_labels is not None: unique_group_labels = group_labels.unique() current_palette = sns.color_palette() colors = [] for idx in range(0, len(unique_group_labels)): colors.append(current_palette[idx]) label_color_dict = dict(zip(unique_group_labels, colors)) outlier_groups = group_labels[outlier_booleans] for label, text in zip(outlier_groups, texts): text.set_color(label_color_dict[label]) patches = [ mpatches.Patch(color=color, label=label) for color, label in zip(colors, unique_group_labels) ] plt.legend(handles=patches) # use adjustText to properly place annotations adjust_text(texts, arrowprops=dict(arrowstyle="-", color='black', lw=0.75)) plt.tight_layout() if dst_dir: if not filename: raise ValueError('Please provide filename') dst_path = dst_dir + filename plt.savefig(dst_path, dpi=600) return outlier_indices, outlier_ids, imgs_data
dataset = datasets.fetch_development_fmri(n_subjects=1) func_filename = dataset.func[0] # print basic information on the dataset print('First subject functional nifti image (4D) is at: %s' % dataset.func[0]) # 4D data ##################################################################### # Preprocess from nilearn.input_data import NiftiMasker # This is fmri timeseries data: the background has not been removed yet, # thus we need to use mask_strategy='epi' to compute the mask from the # EPI images masker = NiftiMasker(smoothing_fwhm=8, memory='nilearn_cache', memory_level=1, mask_strategy='epi', standardize=True) data_masked = masker.fit_transform(func_filename) # Concatenate all the subjects # fmri_data = np.concatenate(data_masked, axis=1) fmri_data = data_masked ##################################################################### # Apply ICA from sklearn.decomposition import FastICA n_components = 10 ica = FastICA(n_components=n_components, random_state=42) components_masked = ica.fit_transform(data_masked.T).T
##### DATA FILES # By default 2nd subject will be fetched haxby_dataset = datasets.fetch_haxby(data_dir=dataDir) imgfMRI = haxby_dataset.func[0] # fMRI data file imgAnat = haxby_dataset.anat[0] # structural data file imgMaskVT = haxby_dataset.mask_vt[0] # ventral-temporal streaming mask tableTarget = haxby_dataset.session_target[0] # session target table file ###### EXTRACTING DATA FROM ROI MASK # Masking the image data with the VT mask, extracting voxels masker = NiftiMasker(mask_img=imgMaskVT, standardize=True, detrend=True, high_pass=0.008, t_r=TR) # Extracting the voxel time series within the mask X_fMRI = masker.fit_transform(imgfMRI) ##### BEHAVIORAL DATA # loading the behavior data into a dataframe targetData = pd.read_csv(tableTarget, sep=' ') # stimulus types targetNames = sorted(targetData.labels.unique()) # Creating numerical labels targetData['labelInd'] = 0 for i,iCat in enumerate(targetNames): targetData.loc[targetData.labels==iCat, 'labelInd'] = i
# Split data into training set and test set from sklearn.model_selection import train_test_split gm_imgs_train, gm_imgs_test, age_train, age_test = train_test_split( gray_matter_map_filenames, age, train_size=.6, random_state=0) # print basic information on the dataset print('First gray-matter anatomy image (3D) is located at: %s' % oasis_dataset.gray_matter_maps[0]) # 3D data print('First white-matter anatomy image (3D) is located at: %s' % oasis_dataset.white_matter_maps[0]) # 3D data ############################################################################# # Preprocess data # ---------------- nifti_masker = NiftiMasker(standardize=False, smoothing_fwhm=2, memory='nilearn_cache') # cache options gm_maps_masked = nifti_masker.fit_transform(gm_imgs_train) # The features with too low between-subject variance are removed using # :class:`sklearn.feature_selection.VarianceThreshold`. from sklearn.feature_selection import VarianceThreshold variance_threshold = VarianceThreshold(threshold=.01) gm_maps_thresholded = variance_threshold.fit_transform(gm_maps_masked) gm_maps_masked = variance_threshold.inverse_transform(gm_maps_thresholded) # Then we convert the data back to the mask image in order to use it for # decoding process mask = nifti_masker.inverse_transform(variance_threshold.get_support()) ############################################################################
from nilearn import datasets nyu_dataset = datasets.fetch_nyu_rest(n_subjects=1) func_filename = nyu_dataset.func[0] # print basic information on the dataset print('First anatomical nifti image (3D) is at: %s' % nyu_dataset.anat_anon[0]) print('First functional nifti image (4D) is at: %s' % func_filename) ########################################################################### # Compute the mask from nilearn.input_data import NiftiMasker # As this is raw resting-state EPI, the background is noisy and we cannot # rely on the 'background' masking strategy. We need to use the 'epi' one nifti_masker = NiftiMasker(standardize=False, mask_strategy='epi', memory="nilearn_cache", memory_level=2) nifti_masker.fit(func_filename) mask_img = nifti_masker.mask_img_ ########################################################################### # Visualize the mask from nilearn.plotting import plot_roi from nilearn.image.image import mean_img # calculate mean image for the background mean_func_img = mean_img(func_filename) plot_roi(mask_img, mean_func_img, display_mode='y', cut_coords=4, title="Mask") ###########################################################################
# Load Miyawaki dataset from nilearn import datasets miyawaki_dataset = datasets.fetch_miyawaki2008() # print basic information on the dataset print('First functional nifti image (4D) is located at: %s' % miyawaki_dataset.func[0]) # 4D data miyawaki_filename = miyawaki_dataset.func[0] miyawaki_mean_img = image.mean_img(miyawaki_filename) # This time, we can use the NiftiMasker without changing the default mask # strategy, as the data has already been masked, and thus lies on a # homogeneous background masker = NiftiMasker() masker.fit(miyawaki_filename) plot_roi(masker.mask_img_, miyawaki_mean_img, title="Mask from already masked data") ############################################################################### # From raw EPI data # Load NYU resting-state dataset nyu_dataset = datasets.fetch_nyu_rest(n_subjects=1) nyu_filename = nyu_dataset.func[0] # Restrict nyu to 100 frames to speed up computation from nilearn.image import index_img
def fit(self, run_imgs, events=None, confounds=None, design_matrices=None): """Fit the GLM For each run: 1. create design matrix X 2. do a masker job: fMRI_data -> Y 3. fit regression to (Y, X) Parameters ---------- run_imgs : Niimg-like object or list of Niimg-like objects, Data on which the GLM will be fitted. If this is a list, the affine is considered the same for all. events : pandas Dataframe or string or list of pandas DataFrames or strings, optional fMRI events used to build design matrices. One events object expected per run_img. Ignored in case designs is not None. If string, then a path to a csv file is expected. confounds : pandas Dataframe, numpy array or string or list of pandas DataFrames, numpy arays or strings, optional Each column in a DataFrame corresponds to a confound variable to be included in the regression model of the respective run_img. The number of rows must match the number of volumes in the respective run_img. Ignored in case designs is not None. If string, then a path to a csv file is expected. design_matrices : pandas DataFrame or list of pandas DataFrames, optional Design matrices that will be used to fit the GLM. If given it takes precedence over events and confounds. """ # Local import to prevent circular imports from nilearn.input_data import NiftiMasker # noqa # Check arguments # Check imgs type if events is not None: _check_events_file_uses_tab_separators(events_files=events) if not isinstance(run_imgs, (list, tuple)): run_imgs = [run_imgs] if design_matrices is None: if events is None: raise ValueError('events or design matrices must be provided') if self.t_r is None: raise ValueError('t_r not given to FirstLevelModel object' ' to compute design from events') else: design_matrices = _check_run_tables(run_imgs, design_matrices, 'design_matrices') # Check that number of events and confound files match number of runs # Also check that events and confound files can be loaded as DataFrame if events is not None: events = _check_run_tables(run_imgs, events, 'events') if confounds is not None: confounds = _check_run_tables(run_imgs, confounds, 'confounds') # Learn the mask if self.mask_img is False: # We create a dummy mask to preserve functionality of api ref_img = check_niimg(run_imgs[0]) self.mask_img = Nifti1Image(np.ones(ref_img.shape[:3]), ref_img.affine) if not isinstance(self.mask_img, NiftiMasker): self.masker_ = NiftiMasker(mask_img=self.mask_img, smoothing_fwhm=self.smoothing_fwhm, target_affine=self.target_affine, standardize=self.standardize, mask_strategy='epi', t_r=self.t_r, memory=self.memory, verbose=max(0, self.verbose - 2), target_shape=self.target_shape, memory_level=self.memory_level) self.masker_.fit(run_imgs[0]) else: if self.mask_img.mask_img_ is None and self.masker_ is None: self.masker_ = clone(self.mask_img) for param_name in [ 'target_affine', 'target_shape', 'smoothing_fwhm', 't_r', 'memory', 'memory_level' ]: our_param = getattr(self, param_name) if our_param is None: continue if getattr(self.masker_, param_name) is not None: warn('Parameter %s of the masker' ' overriden' % param_name) setattr(self.masker_, param_name, our_param) self.masker_.fit(run_imgs[0]) else: self.masker_ = self.mask_img # For each run fit the model and keep only the regression results. self.labels_, self.results_, self.design_matrices_ = [], [], [] n_runs = len(run_imgs) t0 = time.time() for run_idx, run_img in enumerate(run_imgs): # Report progress if self.verbose > 0: percent = float(run_idx) / n_runs percent = round(percent * 100, 2) dt = time.time() - t0 # We use a max to avoid a division by zero if run_idx == 0: remaining = 'go take a coffee, a big one' else: remaining = (100. - percent) / max(0.01, percent) * dt remaining = '%i seconds remaining' % remaining sys.stderr.write("Computing run %d out of %d runs (%s)\n" % (run_idx + 1, n_runs, remaining)) # Build the experimental design for the glm run_img = check_niimg(run_img, ensure_ndim=4) if design_matrices is None: n_scans = get_data(run_img).shape[3] if confounds is not None: confounds_matrix = confounds[run_idx].values if confounds_matrix.shape[0] != n_scans: raise ValueError('Rows in confounds does not match' 'n_scans in run_img at index %d' % (run_idx, )) confounds_names = confounds[run_idx].columns.tolist() else: confounds_matrix = None confounds_names = None start_time = self.slice_time_ref * self.t_r end_time = (n_scans - 1 + self.slice_time_ref) * self.t_r frame_times = np.linspace(start_time, end_time, n_scans) design = make_first_level_design_matrix( frame_times, events[run_idx], self.hrf_model, self.drift_model, self.high_pass, self.drift_order, self.fir_delays, confounds_matrix, confounds_names, self.min_onset) else: design = design_matrices[run_idx] self.design_matrices_.append(design) # Mask and prepare data for GLM if self.verbose > 1: t_masking = time.time() sys.stderr.write('Starting masker computation \r') Y = self.masker_.transform(run_img) del run_img # Delete unmasked image to save memory if self.verbose > 1: t_masking = time.time() - t_masking sys.stderr.write('Masker took %d seconds \n' % t_masking) if self.signal_scaling: Y, _ = mean_scaling(Y, self.scaling_axis) if self.memory: mem_glm = self.memory.cache(run_glm, ignore=['n_jobs']) else: mem_glm = run_glm # compute GLM if self.verbose > 1: t_glm = time.time() sys.stderr.write('Performing GLM computation\r') labels, results = mem_glm(Y, design.values, noise_model=self.noise_model, bins=100, n_jobs=self.n_jobs) if self.verbose > 1: t_glm = time.time() - t_glm sys.stderr.write('GLM took %d seconds \n' % t_glm) self.labels_.append(labels) # We save memory if inspecting model details is not necessary if self.minimize_memory: for key in results: results[key] = SimpleRegressionResults(results[key]) self.results_.append(results) del Y # Report progress if self.verbose > 0: sys.stderr.write( "\nComputation of %d runs done in %i seconds\n\n" % (n_runs, time.time() - t0)) return self
# Load the fMRI imaging import nibabel as nib import numpy as np func_img = nib.load('D:/FaceData/func_img/wrarun1.nii') # Gain the mask from nltools.mask import create_sphere mask_img = create_sphere([-29, -71, -4], radius=5) # from nilearn.masking import compute_epi_mask # mask_img=compute_epi_mask(func_icamcg) # Transform dimensional form 4D to 2D from nilearn.input_data import NiftiMasker masker = NiftiMasker(mask_img=mask_img, standardize=True, memory_level=1) time_series = masker.fit_transform(func_img) import matplotlib.pyplot as plt plt.plot(time_series) plt.show()