Creating mask: 3dSkullstrip + 3dAutomask ok?

Hi, I would like to create a binary mask of the brain from dwi. I thought the following two steps should be ok:

  1. 3dSkullstrip -input data.nii.gz -prefix data_skullstripped.nii.gz
  2. 3dAutomask data_skullstripped.nii.gz -prefix data_masked.nii.gz

My questions:

  1. Is there any reason I should perform 3dcalc afterward or should my mask be ready as is?
  2. The output of 3dAutomask did not adhere to my prefix name. Instead, the output are .BRIK and .HEAD. Can I use the .BRIK file directly as my mask in 3dDWUncert?
  3. Shouldn’t the output of 3dSkullStrip be brain only? If you look at the attached file, it doesn’t appear that way, which also seemed to screw up my mask. (note- this is only for some subjects. Others looked ok.)

Thank you very much for your input.

Hi-

Re. Q1a) The output of 3dAutomask should be binarized already; no need for 3dcalc.

Re. Q1b) The output of 3dSkullStrip by default will be the skullstripped version of the input, not a binarized mask. The brightness values might also be scaled; to keep the same values (for the parts that are not stripped away), add the “-orig_vol” flag. You can binarize the output dset with:


3dcalc -a DSET_FROM_SKULLSTRIP -expr 'bool(a)' -prefix DSET_FROM_SKULLSTRIP_MASK

Re. Q2a) I think your 3dAutomask command should have the input file name last:


3dAutomask  -prefix data_masked.nii.gz data_skullstripped.nii.gz

Re. Q2b) Yes, AFNI reads/writes both BRIK/HEAD and NIFTI (*a couple old programs are exceptions to this rule, but in the vast majority of programs, the I/O generality applies).

Re. Q3) 3dSkullStrip is actually mainly for application to T1w volumes-- it will have a hard time finding appropriate edges on T2w ones, because the boundary isn’t as sharp, and the relative brightnesses of brain tissues and skull are different. I think you are seeing evidence of this in your dataset.
3dAutomask is a quick approach, mostly for approximate masking of EPI dsets (that aren’t too noisy or distorted, ideally).

Do you have any other dsets in the same space that could be skullstripped (like a T1w anatomical)?

–pt

ok thank you. so basically I could:
Run fat_proc_align_anat_pair to align T1 to T2 and then create the mask from the aligned T1 using 3dSkullStrip and 3dAutomask.

Sounds good to me!

-pt

Hm, so the alignment trims off part of the T1 and it seems to be related to square surrounding the dwi, thus limiting the area (photo attached)

fat_proc_align_anat_pair -in_t1w $SUBJECTS_DIR/$subj/mri/T1.nii.gz -in_t2w $dtipath/$subj/data.nii.gz -out_t2w_grid -prefix t1_aligned

I’m not sure if the output is informative but I am pasting it here:
++ Start script version: 1.2
++ Found input file: /home/eji/projects/spins/preproc/freesurfer/subjects/w0/SPN01_CMH_0001_01/mri/T1.nii.gz
++ Found input file: /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz

++ Based on prefix, the output directory will be:
.
++ Based on prefix, the output prefix will be:
t1_aligned

++ Making working directory: ./__WORKING_align_anat_pair_t1_aligned
++ Note: final volume grid resolution will be 1.0 mm isotropic.

-----> STARTING fat_proc_align_anat_pair ---->

This command:
fat_proc_align_anat_pair -in_t1w /home/eji/projects/spins/preproc/freesurfer/subjects/w0/SPN01_CMH_0001_01/mri/T1.nii.gz -in_t2w /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz -out_t2w_grid -prefix t1_aligned

++ Echoing the command to: ./t1_aligned_cmd.txt
++ Unifize T1w proc vol.
++ 3dUnifize: AFNI version=AFNI_20.1.06 (May 4 2020) [64-bit]

  • Pre-processing: ADV…UW[s192914]Gm
    ++ Output dataset ./__WORKING_align_anat_pair_t1_aligned/f01_t1uni.nii
    ++ ===== Elapsed = 26.5 sec
    ++ Threshold T1w proc vol.
    1281.796509
    ++ 3dcalc: AFNI version=AFNI_20.1.06 (May 4 2020) [64-bit]
    ++ Authored by: A cast of thousands
    ++ Output dataset ././__WORKING_align_anat_pair_t1_aligned/f02_t1thr.nii
    ++ Calculating alignment (t2w → t1w) matrix.

+++ Command Echo:
3dAllineate -1Dmatrix_save ./__WORKING_align_anat_pair_t1_aligned/map_t2w_to_t1w.aff12.1D -prefix ./__WORKING_align_anat_pair_t1_aligned/f05_t1align.nii -source /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz -base ./__WORKING_align_anat_pair_t1_aligned/f02_t1thr.nii -twopass -cost lpc -cmass -warp shift_rotate -source_automask -wtprefix ./__WORKING_align_anat_pair_t1_aligned/wtvol.nii.gz -autoweight -final wsinc5 -overwrite

++ 3dAllineate: AFNI version=AFNI_20.1.06 (May 4 2020) [64-bit]
++ Authored by: Zhark the Registrator
++ Source dataset: /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz
++ Base dataset: ./__WORKING_align_anat_pair_t1_aligned/f02_t1thr.nii
++ Loading datasets into memory
++ Output dataset ./__WORKING_align_anat_pair_t1_aligned/wtvol.nii.gz
++ Local correlation: blok type = ‘RHDD(6.54321)’
++ master dataset for output = base
++ OpenMP thread count = 15
++ ======= Allineation of 65 sub-bricks using Local Pearson Correlation Signed =======
volume 0++ *** Coarse pass begins ***
++ *** Fine pass begins ***

  • Final fine fit Parameters:
    x-shift= -1.2558 y-shift= 20.3702 z-shift= 12.0026 … enorm= 23.6766 mm
    z-angle= 0.3099 x-angle= 1.1459 y-angle= 5.0282 … total= 5.1693 deg
    ++ wsinc5 interpolation setup:
  • taper function = Min sidelobe 3 term
  • taper cut point = 0.000
  • window radius = 5 voxels
  • window shape = Cubical
  • The above can be altered via the AFNI_WSINC5_* environment variables.
  • (To avoid this message, ‘setenv AFNI_WSINC5_SILENT YES’.)
  • wsinc5 CUBE(5) mask has 1000 points
    …1++ *** Coarse pass begins ***
    …2++ *** Coarse pass begins ***
    …3++ *** Coarse pass begins ***
    …4++ *** Coarse pass begins ***
    …5++ *** Coarse pass begins ***
    …6++ *** Coarse pass begins ***
    …7++ *** Coarse pass begins ***
    …8++ *** Coarse pass begins ***
    …9++ *** Coarse pass begins ***
    …10++ *** Coarse pass begins ***
    …11++ *** Coarse pass begins ***
    …12++ *** Coarse pass begins ***
    …13++ *** Coarse pass begins ***
    …14++ *** Coarse pass begins ***
    …15++ *** Coarse pass begins ***
    …16++ *** Coarse pass begins ***
    …17++ *** Coarse pass begins ***
    …18++ *** Coarse pass begins ***
    …19++ *** Coarse pass begins ***
    …20++ *** Coarse pass begins ***
    …21++ *** Coarse pass begins ***
    …22++ *** Coarse pass begins ***
    …23++ *** Coarse pass begins ***
    …24++ *** Coarse pass begins ***
    …25++ *** Coarse pass begins ***
    …26++ *** Coarse pass begins ***
    …27++ *** Coarse pass begins ***
    …28++ *** Coarse pass begins ***
    …29++ *** Coarse pass begins ***
    …30++ *** Coarse pass begins ***
    …31++ *** Coarse pass begins ***
    …32++ *** Coarse pass begins ***
    …33++ *** Coarse pass begins ***
    …34++ *** Coarse pass begins ***
    …35++ *** Coarse pass begins ***
    …36++ *** Coarse pass begins ***
    …37++ *** Coarse pass begins ***
    …38++ *** Coarse pass begins ***
    …39++ *** Coarse pass begins ***
    …40++ *** Coarse pass begins ***
    …41++ *** Coarse pass begins ***
    …42++ *** Coarse pass begins ***
    …43++ *** Coarse pass begins ***
    …44++ *** Coarse pass begins ***
    …45++ *** Coarse pass begins ***
    …46++ *** Coarse pass begins ***
    …47++ *** Coarse pass begins ***
    …48++ *** Coarse pass begins ***
    …49++ *** Coarse pass begins ***
    …50++ *** Coarse pass begins ***
    …51++ *** Coarse pass begins ***
    …52++ *** Coarse pass begins ***
    …53++ *** Coarse pass begins ***
    …54++ *** Coarse pass begins ***
    …55++ *** Coarse pass begins ***
    …56++ *** Coarse pass begins ***
    …57++ *** Coarse pass begins ***
    …58++ *** Coarse pass begins ***
    …59++ *** Coarse pass begins ***
    …60++ *** Coarse pass begins ***
    …61++ *** Coarse pass begins ***
    …62++ *** Coarse pass begins ***
    …63++ *** Coarse pass begins ***
    …64
    ++ *** Coarse pass begins ***
    ++ Output dataset ./__WORKING_align_anat_pair_t1_aligned/f05_t1align.nii
    ++ Wrote -1Dmatrix_save ./__WORKING_align_anat_pair_t1_aligned/map_t2w_to_t1w.aff12.1D
    ++ 3dAllineate: total CPU time = 0.0 sec Elapsed = 1848.3
    ++ ###########################################################
    ++ # Please check results visually for alignment quality #
    ++ ###########################################################
    ++ Inverting alignment matrix (to get t1w → t2w).
    ++ Apply alignment matrix.
    ++ Outputting final image to t2w space

+++ Command Echo:
3dAllineate -1Dmatrix_apply ./__WORKING_align_anat_pair_t1_aligned/map_t1w_to_t2w.aff12.1D -source ./__WORKING_align_anat_pair_t1_aligned/f00_t1res.nii -master /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz -prefix ./__WORKING_align_anat_pair_t1_aligned/f06_t1mapped.nii -float -final wsinc5

++ 3dAllineate: AFNI version=AFNI_20.1.06 (May 4 2020) [64-bit]
++ Authored by: Zhark the Registrator
++ Source dataset: ./__WORKING_align_anat_pair_t1_aligned/f00_t1res.nii
++ Base dataset: (not given)
++ Loading datasets into memory

  •    -cmass x y z shifts =    0.000    0.000    0.000
    
  • shift search range is +/- = 81.855 81.855 81.855
    ++ OpenMP thread count = 15
    ++ ========== Applying transformation to 1 sub-bricks ==========
    ++ wsinc5 interpolation setup:
  • taper function = Min sidelobe 3 term
  • taper cut point = 0.000
  • window radius = 5 voxels
  • window shape = Cubical
  • The above can be altered via the AFNI_WSINC5_* environment variables.
  • (To avoid this message, ‘setenv AFNI_WSINC5_SILENT YES’.)
  • wsinc5 CUBE(5) mask has 1000 points
    ++ Output dataset ./__WORKING_align_anat_pair_t1_aligned/f06_t1mapped.nii
    ++ 3dAllineate: total CPU time = 0.0 sec Elapsed = 1.7
    ++ ###########################################################
    ++ Perhaps for prepping for FreeSurfing:
    → making sure that there are even numbers of voxels
    along each spatial dimension.
    ++ The dimensions are: 128 128 71
    +* Found an odd dimension in the [3]th axis!
    → will hopefully fix.
    ++ Zeropadding: -I 1

+++ Command Echo:
3dZeropad -overwrite -prefix ./__WORKING_align_anat_pair_t1_aligned/f08_t1padded.nii -I 1 ./__WORKING_align_anat_pair_t1_aligned/f06_t1mapped.nii

++ 3dZeropad: AFNI version=AFNI_20.1.06 (May 4 2020) [64-bit]
++ output dataset: ./__WORKING_align_anat_pair_t1_aligned/f08_t1padded.nii
++ Done workin’. Just need to copy the final dset.
++ 3dcalc: AFNI version=AFNI_20.1.06 (May 4 2020) [64-bit]
++ Authored by: A cast of thousands
++ Output dataset ././t1_aligned.nii.gz
++ More QC images: b0 on initial ref.
++ 3dedge3: AFNI version=AFNI_20.1.06 (May 4 2020) [64-bit]

++ QC image 00 (./t1_aligned.nii.gz edges on /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz): t1_aligned_qc_t2w_t1wE

++ My command:
@chauffeur_afni -ulay /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz -ulay_range 2% 98% -olay ./__WORKING_align_anat_pair_t1_aligned/f10_edges.nii -func_range_perc_nz 50 -pbar_posonly -cbar red_monochrome -opacity 6 -prefix ./QC/t1_aligned_qc_t2w_t1wE -montx 5 -monty 3 -set_xhairs OFF -label_mode 1 -label_size 3 -do_clean

------------------ start of optionizing ------------------

++ Found input file: /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz
++ Found input file: ./__WORKING_align_anat_pair_t1_aligned/f10_edges.nii
++ Using blowup factor: 2
++ Using opacity: 6
++ Making temporary work directory to copy vis files: ./QC/__tmp_chauf_oC6qIY1FE4T
++ Converted 0 to labels
++ Final subbrick indices: -1 -1 -1
++ Copy ulay to visualize (volumetric) within user’s range:
** FATAL ERROR: -percentile can only be used on one sub-brick only.
Use sub-brick selectors ‘[.]’ to specify sub-brick of interest.
** Program compile date = May 4 2020
** FATAL ERROR: -percentile can only be used on one sub-brick only.
Use sub-brick selectors ‘[.]’ to specify sub-brick of interest.
** Program compile date = May 4 2020
amin: Subscript out of range.

++ QC image 01 (./t1_aligned.nii.gz olay on /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz): t1_aligned_qc_t2w_t1w

++ My command:
@chauffeur_afni -ulay /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz -ulay_range 2% 98% -olay ./t1_aligned.nii.gz -pbar_posonly -opacity 4 -prefix ./QC/t1_aligned_qc_t2w_t1w -montx 5 -monty 3 -set_xhairs OFF -label_mode 1 -label_size 3 -do_clean

------------------ start of optionizing ------------------

++ Found input file: /home/eji/data3/spins/dwi/SPN01_CMH_0001_01/data.nii.gz
++ Found input file: ./t1_aligned.nii.gz
++ Using blowup factor: 2
++ Using opacity: 4
++ Making temporary work directory to copy vis files: ./QC/__tmp_chauf_xhg9XLwFz8m
++ Converted 0 to labels
++ Final subbrick indices: -1 -1 -1
++ Copy ulay to visualize (volumetric) within user’s range:
** FATAL ERROR: -percentile can only be used on one sub-brick only.
Use sub-brick selectors ‘[.]’ to specify sub-brick of interest.
** Program compile date = May 4 2020
** FATAL ERROR: -percentile can only be used on one sub-brick only.
Use sub-brick selectors ‘[.]’ to specify sub-brick of interest.
** Program compile date = May 4 2020
amin: Subscript out of range.

++ Cleaning working directory!

++ The final data set is here: ./t1_aligned.nii.gz
++ The final spatial dims are: 128 128 72
++ The spatial resolution is: 2.000000 2.000000 2.000000

Hi-

My guess is that the T1w volume and the T2w “data.nii.gz” are very far apart from each other to start with.

What do the images created by:


@djunct_overlap_check -ulay [T1w dset] -olay [T2w dset] -prefix IMAGE

look like? (say, axial and sagittal)?

–pt

Hm, I’m getting:
@djunct_overlap_check: command not found

We have Version AFNI_20.1.06 ‘Otho’.

That AFNI is so old :frowning: .

Can you update it?


@update.afni.binaries -d

-pt

ok we have updated our AFNI version :smiley:

Attached is the screenshot from @djunct_overlap_check with the T1 and T2 data. Does it seem problematic?

Is there a solution to the issue of the aligned T1 (output of fat_proc_align_anat_pair) being cut off? I hope!
Additionally, the command itself (fat_proc_align_anat_pair) takes several hours per subject, even on a 32 core machine!

Happy new year pt!
Just following up on my previous message: in the case that the T1 and T2 are very far apart to start with (I attached an image before), is there a solution to the problem that fat_proc_align_anat_pair cuts off part of the T1?

Thank you!

Hi, Ellen-

Sorry, this slipped off of my slippery brain.

The overlap doesn’t look that bad to me, but I think it would just be easiest if I took a look at the datasets (esp. to see about the running-time issue, which surprises me a bit because the T2w volume is not so huge). If I share a Box drive link, is that fine?

I imagine that part of the issue might be the at the T1w volume has so much neck and non-brain material, while the T2w doesn’t… but I will check.

–pt

Hi, Ellen-

So, I think there are a couple of issues here:

  • You are aligning a T1w volume not to another T2w anatomical, 3D volume, but instead to a 4D volume that appears to be a DWI dset, with some b=0 volumes that do have some T2w contrast.
  • This program does linear affine alignment. The assumption is that the 2 dsets to be aligned are from the same subject, and that they have veeery similar structure-- no major distortional differences.
  • If one of the dsets has >1 volume, then this program will try to align each of those to the other input-- that is what is happening here, and why things might be taking a while.
  • I ran this command, to select out just the [0]th volume from the DWI/TW2-like dset:

fat_proc_align_anat_pair \
    -in_t1w ./T1.nii.gz \
    -in_t2w ./data.nii.gz'[0]'        \
    -out_t2w_grid               \
    -prefix t1_al_NEW

… and the alignment looked pretty good-- see the attached images.

  • Note that the data.nii.gz dset, as a DWI dset, does appear to have some noticeable EPI distortion at the anterior part of the brain-- this kind of alignment cannot overcome that.
  • The DWI dset should be distortion corrected, for example with TORTOISE’s DIFFPREP+DR_BUDDI, to have distortion correction-- note that being able to run DR_BUDDI depends on having a dual phase encoded acquisition for the DWIs, namely a full set of “blip up” and a full set of “blip down” data (that is one set with phase encode along one axis direction-- such as AP-- and another set with the phase encode along the opposite direction-- then PA).

–pt

thanks, looking at the 0th volume def helped and it took 2 min to complete.

However, the top is still cut off for me. I’ve attached what I see when I view t1_aligned_NEW in freeview and also when I align it with data.nii.gz using @djunct_overlap_check.

The t1_aligned_NEW didn’t seem to be cut off for you if I saw/understood correctly… or were you suggesting that the EPI distortion at the anterior part is what could be causing it to be cut out?

also, what command did you use to view all those slices and the alignment?

best, E

Screenshot (260).png

Hi, Ellen-

Those images I showed are from the QC/ output dir from the fat_proc_align_anat_pair command (as written).

I also just checked this again in the AFNI GUI, where:
ulay = data.nii.gz
olay = t1_al_NEW.nii.gz
… and things looked fine (like in the QC images).

I also just ran:


@djunct_overlap_check -ulay data.nii.gz'[0]' -olay t1_al_NEW.nii.gz -prefix IMAGE

to check the overlap, and that looks as expected (see attached). No cut-off.

I don’t know what freeview is doing—can you try viewing the data in AFNI? (None of these datasets are even oblique, so I really don’t know what could be happening there.)

–pt

Hi pt,

I am now sure that fat_proc_align_anat_pair is not working/aligning on my end. I even went back to the Box drive and downloaded the T1 and T2 I uploaded for you to be sure that we were running it on the same subject. I also tested fat_proc_align_anat_pair for other subjects and each time, the output is not aligned to the T2.

Attached are screenshots from the subject data we both processed (on Box). They should correspond to the @djunct_overlap_check and QC/ output .png that you attached in the prior AFNI forum messages above.

However, our images don’t look the same.

We updated AFNI as you suggested: Precompiled binary linux_ubuntu_16_64: Dec 7 2020 (Version AFNI_20.3.03 ‘Vespasian’).
Is there anything you can think of that could explain why the alignment doesn’t work for us?

Hi, Ellen-

That is weird… Can you please copy+paste the command you are running?

–pt

fat_proc_align_anat_pair
-in_t1w ./T1.nii.gz
-in_t2w ./data.nii.gz’[0]’
-out_t2w_grid
-prefix t1_al

@djunct_overlap_check -ulay data.nii.gz’[0]’ -olay t1_al.nii.gz -prefix IMAGE

aiv IMAGE.jpg