[ERROR] Cannot perform operation along axis 3; image only has 3 axes

Congratulations to the developers on an outstanding release and thank you for all the hard work.

I am attempting to follow the structural connectome tutorial but substituting my own data. My data only has a single b=0 image at the beginning. I am new to this field so I may have missed something.

This problem appears related to Error in dwi2response with msmt_5tt

“… the command responsible for averaging the b=0 volumes fails because a single volume comes out from the previous command as a 3D image, so there is no 4th axis to average over.”

On the diffusion image processing sub heading step 2 and step 3 fail.

For step 2:

dwiextract DWI.mif - -bzero | mrmath - mean meanb0.mif -axis 3
dwiextract: [100%] extracting volumes
mrmath: [ERROR] Cannot perform operation along axis 3; image only has 3 axes

For step 3:

dwi2response msmt_5tt DWI.mif 5TT.mif RF_GM.txt RF_WM.txt RF_CSF.txt -voxels RF_voxels.mif
dwi2response:
dwi2response: Note that this script makes use of commands / algorithms that have relevant articles for citation; INCLUDING FROM EXTERNAL SOFTWARE PACKAGES. Please consult the help page (-help option) for more information.
dwi2response:
dwi2response: Generated temporary directory: /tmp/dwi2response-tmp-DTXN5P/
Command: mrconvert 5TT.mif /tmp/dwi2response-tmp-DTXN5P/5tt.mif -quiet
Command: mrconvert DWI.mif /tmp/dwi2response-tmp-DTXN5P/dwi.mif -stride 0,0,0,1 -quiet
dwi2response: Changing to temporary directory (/tmp/dwi2response-tmp-DTXN5P/)
Command: dwi2mask dwi.mif mask.mif -quiet
dwi2response: [WARNING] Less than three b-value shells; response functions will not be applicable in MSMT CSD algorithm
Command: dwi2tensor dwi.mif - -mask mask.mif -quiet | tensor2metric - -fa fa.mif -vector vector.mif -quiet
Command: mrtransform 5tt.mif 5tt_regrid.mif -template fa.mif -interp linear -quiet
Command: mrconvert 5tt_regrid.mif - -coord 3 0 -axes 0,1,2 -quiet | mrcalc - 0.95 -gt fa.mif 0.2 -lt -mult mask.mif -mult gm_mask.mif -quiet
Command: mrconvert 5tt_regrid.mif - -coord 3 2 -axes 0,1,2 -quiet | mrcalc - 0.95 -gt mask.mif -mult wm_mask.mif -quiet
Command: mrconvert 5tt_regrid.mif - -coord 3 3 -axes 0,1,2 -quiet | mrcalc - 0.95 -gt fa.mif 0.2 -lt -mult mask.mif -mult csf_mask.mif -quiet
dwi2response: Calling dwi2response recursively to select WM single-fibre voxels using ‘tournier’ algorithm
Command: dwi2response -quiet tournier dwi.mif wm_ss_response.txt -mask wm_mask.mif -voxels wm_sf_mask.mif
Command: dwiextract dwi.mif -shell 0.0 dwi_b0.mif -quiet
Command: amp2sh dwi_b0.mif - -quiet | sh2response - wm_sf_mask.mif dirs.mif wm_response_b0.txt -quiet
amp2sh: [ERROR] dwi image should contain 4 dimensions
sh2response: [ERROR] no filename supplied to standard input (broken pipe?)
sh2response: [ERROR] error opening image "-"
dwi2response: [ERROR] Command failed: amp2sh dwi_b0.mif - -quiet | sh2response - wm_sf_mask.mif dirs.mif wm_response_b0.txt -quiet
dwi2response: Changing back to original directory (/home/tom/ACT/FAST)
dwi2response: Deleting temporary directory /tmp/dwi2response-tmp-DTXN5P/

Hi Tom,

Yes, both of these are related to the same issue. dwiextract -bzero will produce a 3D image (rather than a 4D image with a single volume) if there is only a single b=0 volume. However this seems to be creating problems rather than solving them.

The first error can be bypassed by replacing:
dwiextract DWI.mif - -bzero | mrmath - mean meanb0.mif -axis 3
with:
dwiextract DWI.mif meanb0.mif -bzero
Because there’s only one b=0 volume, there’s no need to perform an explicit averaging across volumes.

The second error is unfortunately more difficult to deal with. It does however highlight why this behaviour should probably be changed in MRtrix3. With MSMT we now regularly treat b=0 images as a ‘shell’, and therefore it makes more sense for this ‘shell’ to be stored as a 4D image, since it needs to be possible to convert this ‘shell’ from a set of amplitudes ‘on a shell’ to a spherical harmonic representation (though only at lmax=0). I’ve created an issue for this on GitHub and will make the change soon.

Until then, here’s a sneaky little fix that might just be enough to get you through:
dwiextract DWI.mif -bzero - | mrcat - DWI.mif - -axis 3 | dwi2response msmt_5tt - 5TT.mif RF_GM.txt RF_WM.txt RF_CSF.txt -voxels RF_voxels.mif
Isn’t piping great!

Rob

I have a feeling that substituting every occurrence of ‘-bzero’ with ‘-shell 0’ might do the trick…?

In the long run, I do agree that it would be best to remove the inherent conversion to 3D feature from dwiextract… As discussed in this issue and this one, it’s just bound to cause issues like this…

Thanks guys, the hack worked!

Which hack, exactly…? Rob pushed out some changes to fix this recently too.

Apologies, hack is the wrong word, the pipe work around Rob provided above.

No, hack is a perfectly acceptable term in this context…

I just wasn’t sure whether you were referring to Rob’s pipeline, my suggested workaround, or the latest changes in master…