fMRIprep to MATLAB : outliers removal

Hi everyone !

We preprocessed our data using fMRIprep and now we are using MATLAB/SPM12 for the analysis. I was wondering how to remove from the signal, in matlab, the outliers detected by fMRIprep ? I found how to deal with the motion confounds but not with the outliers :sweat_smile:

Thank you for your help,

I wish everyone a very nice day !

Hello,

Assuming you are happy with the outlier threshold default in fMRIPrep (FD of 0.5 or DVARS of 1.5), then you would just need to take the motion_outlierXX confounds from the confounds.tsv file (along with the other nuisance regressors you want to include), and then make a txt file out of those. You can load that into SPM presumably the same way you are doing so with your head motion confounds.

Best,
Steven

I would actually avoid the txt format because it can be hard to figure out what each column is in the models after that.

Save them to a mat file with a names (n X 1 cellstring) and R (n X t array) variables, that you can pass to SPM the same way you would with a txt.

Here a script that should do it and save the output as BIDS like structure.

Note that you will need bids-matlab for this to work: GitHub - bids-standard/bids-matlab: MATLAB / Octave tools for BIDS datasets

% extracts confounds of interest from fmriprep timeseries.tsv
% and saves them for easier ingestion by SPM model specification

path_to_fmriprep = fullfile(pwd, 'fmriprep');
output_folder = fullfile(pwd, 'spm12');

task_label = 'facerepetition';
space_label = 'MNI152NLin2009cAsym';

% set up some regular expression to identify the confounds we want to keep
confounds_of_interest = {'^rot_[xyz]$', '^trans_[xyz]$', '^*outlier*$'};

% index the content of the fmriprep data set and
% figure out which subjects we have
BIDS = bids.layout(path_to_fmriprep, 'use_schema', false); 
subjects = bids.query(BIDS, 'subjects');

% prepare the output folder structure
folders = struct('subjects', {subjects}, 'modalities', {{'stats'}});
bids.init(output_folder, 'folders', folders);

for i_sub = 1:numel(subjects)
  
  % create the filter to 
  filter = struct('sub', subjects{i_sub}, ...
                  'task', task_label, ...
                  'desc', 'confounds', ...
                  'suffix', 'timeseries');
  confound_files = bids.query(BIDS, 'data', filter);

  % loop through all the desc-confounds_timeseries.tsv
  % load it
  % get only the condounds we need 
  % save it in the output folder as a mat file and a TSV
  for i = 1:numel(confound_files)

    % for mat file
    names = {};
    R = [];
    
    % for TSV
    new_content = struct();
    
    % load
    content = bids.util.tsvread(confound_files{i});
    confounds_names = fieldnames(content);
    
    % create a logical vector to identify which confounds to keep
    % and store confounds to keep in new variables
    confounds_to_keep = regexp(confounds_names, ...
                               strjoin(confounds_of_interest, '|'));
    confounds_to_keep = ~cellfun('isempty', confounds_to_keep);
    
    confounds_names = confounds_names(confounds_to_keep);
    
    for j = 1:numel(confounds_names)
      % for mat file
      names{j} = confounds_names{j};
      R(:,j) = content.(confounds_names{j});
      
      % for TSV
      new_content.(confounds_names{j}) = content.(confounds_names{j});
    end
    
    % save to mat and TSV
    output_file_name = bids.File(confound_files{i});
    output_file_name.entities.desc = '';
    output_file_name.suffix = 'confounds';
    
    output_file_name.path = fullfile(output_folder, ['sub-' subjects{i_sub}], 'stats');
    
    bids.util.tsvwrite(fullfile(output_file_name.path, output_file_name.filename), ...
                       new_content);

    
    output_file_name.extension = '.mat';
    save(fullfile(output_file_name.path, output_file_name.filename), 'names', 'R');
                     
  end
  
end

Tried on this demo dataset : SPM_datasets/spm_facerep_fmriprep - G-Node GIN

To give:

spm12
โ”œโ”€โ”€ CHANGES
โ”œโ”€โ”€ dataset_description.json
โ”œโ”€โ”€ README
โ””โ”€โ”€ sub-01
    โ””โ”€โ”€ stats
        โ”œโ”€โ”€ sub-01_task-facerepetition_confounds.mat
        โ””โ”€โ”€ sub-01_task-facerepetition_confounds.tsv
1 Like

will leave a copy of the script here: Extract fmriprep regressors for SPM model specification ยท GitHub