ApplyWarp applies to raw anatomical NOT to BET extracted anatomical

Hi everyone,

I’ve searched around and have not found anyone with this exact issue. I’ve been running a preprocessing pipeline based on miykael’s tutorial: https://miykael.github.io/nipype_tutorial/notebooks/example_preprocessing.html

While the code executes, my preprocessed images are coregistered to the original anatatomicals without the skull stripping and transformation. Extraction and segmentation both work as they should. However, despite what the code says somehow the non-extracted original image is being piped in to the ApplyWarp node. I’m not sure how the first line could pipe in the BET output image correctly to segmentation, but then this doesn’t happen int he 7th line. Here is the code:

coregwf.connect([(bet_anat, segmentation, [(‘out_file’, ‘in_files’)]),
(segmentation, threshold, [((‘partial_volume_files’, get_wm),
‘in_file’)]),
(bet_anat, coreg_pre, [(‘out_file’, ‘reference’)]),
(threshold, coreg_bbr, [(‘out_file’, ‘wm_seg’)]),# possibly where things are going wrong
(coreg_pre, coreg_bbr, [(‘out_matrix_file’, ‘in_matrix_file’)]),
(coreg_bbr, applywarp, [(‘out_matrix_file’, ‘in_matrix_file’)]),
(bet_anat, applywarp, [(‘out_file’, ‘reference’)]),
(coreg_bbr, applywarp_mean, [(‘out_matrix_file’, ‘in_matrix_file’)]),
(bet_anat, applywarp_mean, [(‘out_file’, ‘reference’)]),
])

Any help would be much appreciated. Please ask further details that you may need. Thanks!

Hi @MZMG,

I’m not sure if I understand your problem correct. The goal of this co-registration step is to coregister the functional image to the anatomical image. The anatomical image and the BET extracted brain have the exact same coordinates, therefore the co-registered functional images should be the same if the used reference image is the raw or the BET file, no?

The code that you copied is identical to the one from the nipype_tutorial, therefore it’s difficult to say where the problem might have arisen. But, in the working directory of the pipeline, you have for each node a specific folder. And in each of those folders, you should have a report.rst file that contains all the information about the node, i.e. input parameters, output parameters and for FSL nodes, the command that was used. Could you verify in those report.rst files what’s happening and perhaps copy paste those commands here?

Good luck,
Michael

Hi @miykael!

Firstly: thank you so much for all the work you do! I’m coming from fsl/bashing and I simply love nipype. Its totally the future and I’m trying to get everyone on it.

I think it might be best to show you the problem through images and I’ve also included the rst file text. Something tells me this isn’t a code problem, but something about our scanning parameters. Its just so confusing that it does one thing on one line, but another on the functional…

Here is the image I expect (and get it a bet extract skull directly from functional images using FSL Gui):

Image I get:

rst for applywarp, where things seem to go wrong:

Node: coregwf (applywarp (fsl)

Hierarchy : preproc.coregwf.applywarp
Exec ID : applywarp.a46

Original Inputs

  • angle_rep :
  • apply_isoxfm : 4.0
  • apply_xfm :
  • args :
  • bbrslope :
  • bbrtype :
  • bgvalue :
  • bins :
  • coarse_search :
  • cost :
  • cost_func :
  • datatype :
  • display_init :
  • dof :
  • echospacing :
  • environ : {‘FSLOUTPUTTYPE’: ‘NIFTI_GZ’}
  • fieldmap :
  • fieldmapmask :
  • fine_search :
  • force_scaling :
  • ignore_exception : False
  • in_file : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/slicetimer/Functional_mcf_st.nii
  • in_matrix_file : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/coregwf/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/coreg_bbr/Functional_mcf.nii_mean_reg_flirt.mat
  • in_weight :
  • interp : spline
  • min_sampling :
  • no_clamp :
  • no_resample :
  • no_resample_blur :
  • no_search :
  • out_file :
  • out_log :
  • out_matrix_file :
  • output_type : NIFTI_GZ
  • padding_size :
  • pedir :
  • ref_weight :
  • reference : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/coregwf/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/bet_anat/Raw_Anatomical_brain.nii.gz
  • rigid2D :
  • save_log :
  • schedule :
  • searchr_x :
  • searchr_y :
  • searchr_z :
  • sinc_width :
  • sinc_window :
  • terminal_output :
  • uses_qform :
  • verbose :
  • wm_seg :
  • wmcoords :
  • wmnorms :

Execution Inputs

  • angle_rep :
  • apply_isoxfm : 4.0
  • apply_xfm :
  • args :
  • bbrslope :
  • bbrtype :
  • bgvalue :
  • bins :
  • coarse_search :
  • cost :
  • cost_func :
  • datatype :
  • display_init :
  • dof :
  • echospacing :
  • environ : {‘FSLOUTPUTTYPE’: ‘NIFTI_GZ’}
  • fieldmap :
  • fieldmapmask :
  • fine_search :
  • force_scaling :
  • ignore_exception : False
  • in_file : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/slicetimer/Functional_mcf_st.nii
  • in_matrix_file : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/coregwf/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/coreg_bbr/Functional_mcf.nii_mean_reg_flirt.mat
  • in_weight :
  • interp : spline
  • min_sampling :
  • no_clamp :
  • no_resample :
  • no_resample_blur :
  • no_search :
  • out_file :
  • out_log :
  • out_matrix_file :
  • output_type : NIFTI_GZ
  • padding_size :
  • pedir :
  • ref_weight :
  • reference : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/coregwf/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/bet_anat/Raw_Anatomical_brain.nii.gz
  • rigid2D :
  • save_log :
  • schedule :
  • searchr_x :
  • searchr_y :
  • searchr_z :
  • sinc_width :
  • sinc_window :
  • terminal_output :
  • uses_qform :
  • verbose :
  • wm_seg :
  • wmcoords :
  • wmnorms :

Execution Outputs

  • out_file : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/coregwf/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/applywarp/Functional_mcf_st_flirt.nii.gz
  • out_log :
  • out_matrix_file :

Runtime info

  • command : flirt -in /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/slicetimer/Functional_mcf_st.nii -ref /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/coregwf/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/bet_anat/Raw_Anatomical_brain.nii.gz -out Functional_mcf_st_flirt.nii.gz -omat Functional_mcf_st_flirt.mat -applyisoxfm 4.000000 -init /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/coregwf/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/coreg_bbr/Functional_mcf.nii_mean_reg_flirt.mat -interp spline
  • duration : 9.573769
  • hostname : Lisas-iMac.iop.musc.edu
  • prev_wd : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data
  • working_dir : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/workingdir_PREPROC/preproc/coregwf/_ROI_id_L_DLPFC_BLOCK_TMS_subject_id_P212/applywarp

Terminal output






Terminal - standard output

Terminal - standard error






Environment
~~~~~~~~~~~


* ANTSPATH : /usr/local/bin/ants/bin
* Apple_PubSub_Socket_Render : /private/tmp/com.apple.launchd.Q1b1906wN3/Render
* DISPLAY : /private/tmp/com.apple.launchd.fBcifEro2l/org.macosforge.xquartz:0
* DYLD_LIBRARY_PATH : /opt/X11/lib/flat_namespace
* FIX_VERTEX_AREA : 
* FMRI_ANALYSIS_DIR : /Applications/freesurfer/fsfast
* FREESURFER_HOME : /Applications/freesurfer
* FSFAST_HOME : /Applications/freesurfer/fsfast
* FSF_OUTPUT_FORMAT : nii.gz
* FSLDIR : /usr/local/fsl
* FSLGECUDAQ : cuda.q
* FSLLOCKDIR : 
* FSLMACHINELIST : 
* FSLMULTIFILEQUIT : TRUE
* FSLOUTPUTTYPE : NIFTI_GZ
* FSLREMOTECALL : 
* FSLTCLSH : /usr/local/fsl/bin/fsltclsh
* FSLWISH : /usr/local/fsl/bin/fslwish
* FSL_BIN : /usr/local/fsl/bin
* FSL_DIR : /usr/local/fsl
* FS_OVERRIDE : 0
* FUNCTIONALS_DIR : /Applications/freesurfer/sessions
* HOME : /Users/marlengonzalez
* LANG : en_US.UTF-8
* LOCAL_DIR : /Applications/freesurfer/local
* LOGNAME : marlengonzalez
* MINC_BIN_DIR : /Applications/freesurfer/mni/bin
* MINC_LIB_DIR : /Applications/freesurfer/mni/lib
* MNI_DATAPATH : /Applications/freesurfer/mni/data
* MNI_DIR : /Applications/freesurfer/mni
* MNI_PERL5LIB : /Applications/freesurfer/mni/lib/../Library/Perl/Updates/5.12.3
* OLDPWD : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data/P100
* OS : Darwin
* PATH : /Applications/freesurfer/bin:/Applications/freesurfer/fsfast/bin:/Applications/freesurfer/tktools:/usr/local/fsl/bin:/Applications/freesurfer/mni/bin:/Applications/MATLAB_R2017b.app/bin:/usr/local/bin/ants/bin:/usr/local/fsl/bin:/Users/marlengonzalez/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/marlengonzalez/abin
* PERL5LIB : /Applications/freesurfer/mni/lib/../Library/Perl/Updates/5.12.3
* PWD : /Users/marlengonzalez/Documents/Projects/spTMS-fMRI/fMRI_data
* SECURITYSESSIONID : 186a7
* SHELL : /bin/bash
* SHLVL : 1
* SPM_PATH : /Applications/MATLAB_R2017b.app/toolbox/spm12/
* SSH_AUTH_SOCK : /private/tmp/com.apple.launchd.ykY0rfxSPa/Listeners
* SUBJECTS_DIR : /Applications/freesurfer/subjects
* TERM : xterm-256color
* TERM_PROGRAM : Apple_Terminal
* TERM_PROGRAM_VERSION : 400
* TERM_SESSION_ID : 9ED58CEE-48FC-4154-9EC5-956A123371AD
* TMPDIR : /var/folders/48/_q3xrnms1sqbtkhrvrj52x400000gp/T/
* USER : marlengonzalez
* XPC_FLAGS : 0x0
* XPC_SERVICE_NAME : 0
* _ : /Users/marlengonzalez/anaconda3/bin/python
* __CF_USER_TEXT_ENCODING : 0x1F6:0x0:0x0


It maybe as simple is running the fslorient2std before running this node, but I wanted to put it here just in case others are getting similar weirdness. 

THANK YOU!

@MZMG, thank you for your very nice feedback. Yes, I think Nipype is a huge part of the neuroimaging future. But if you want to convince everyone on it, I recommend to also look into BIDS, BIDS-App, OpenNeuro and anything else that is on Nipy.org. So many amazing tools that make the life of a neuroimaging researcher so much easier.

But getting back to your problem: Is the issue with the two figures that they are (1) not reoriented in the same direction or (2) that BET was not run on the functional image?

(1) As you say, this would probably be an issue with the recording, and fslorient2std is probably a good way to solve it. I’m not sure if BIDS looks at orientation issues, but for any other issues, and also just to verify that your dataset is coherent, I recommend putting your dataset into a BIDS format and applying BIDS Validator. And anyhow, having your data in BIDS format is anyhow the best way to store your data :slight_smile:

(2) The pipeline is not set up to extract the brain from the functional images. The BET output of the anatomy is only used to get the optimal co-registration matrix, not skull strip the functional images.

I’m not sure if that solves your problem or not?

@miykael.I’ll definitely be checking these tools out as I’m looking to streamline things as much as possible and get into the openscience state of mind.

  1. I oriented everything just using a bash script before and this helps. I’ll try BIDS

  2. Coming from fsl, when we do preprocessing, creating a mask and then using that during the applywarp phase is the standard scheme. I think that what I need to do is update the code to make a mask out of the BET anat file and use that mask in the applywarp step. Otherwise, I can also create a bet_func node to remove the eyes before doing applywarp.

I suppose this is normal growing pains for anyone switching over. Hopefully someone else will be less frustrated by seeing this. What I love about this is how much you really get to know the under the hood stuff.

Thanks so much!