Gradient table for dwi2adc command

Hi, jdtournier

What command could generate the bvec and bval for the dwi_b3000.mif?

Something like this would do:

mrinfo dwi_b3000.mif -export_grad_fsl bvecs bvals

See the dedicated page on how to handle the DW encoding in the main docs for details.

Hi, jdtournier

I used dwiextract to extract all b0 images from my dwi data and mean them to get one single B0 image, then I want to merge it with the rest b1500 images.
While I found that the generated meanB0 image losed its gradient table, so I write a bval and bvec for it and use the mrconvert to add the gradient table to the meanB0 image, while there exhibit this error:

mrconvert: [ERROR] bvecs and bvals files must have same number of diffusion directions as DW-image (gradients: 1, image: 49)
mrconvert: [ERROR] error importing diffusion gradient table for image “AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.mif”
It is weird that the B0 image only has one image, how comes the 49 images?

My commands are as below

mrconvert AP_PA_denoised_degibbs_eddy_biascorrect.nii.gz -fslgrad AP_PA_denoised_degibbs_eddy.eddy_rotated_bvecs AP_PA.bval AP_PA_denoised_degibbs_eddy_biascorrect.mif -force
#mean b0
dwiextract -bzero AP_PA_denoised_degibbs_eddy_biascorrect.mif AP_PA_denoised_degibbs_eddy_biascorrect_B0.mif -force
mrmath AP_PA_denoised_degibbs_eddy_biascorrect_B0.mif mean AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.mif -axis 3 -force
mrconvert AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.mif AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.mif -fslgrad  meanB0.bvec meanB0.bval -force
#extract min bvalue
dwiextract -no_bzero -shells 1500 AP_PA_denoised_degibbs_eddy_biascorrect.mif AP_PA_denoised_degibbs_eddy_biascorrect_B1500.mif -force
mrcat AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.mif AP_PA_denoised_degibbs_eddy_biascorrect_B1500.mif AP_PA_denoised_degibbs_eddy_biascorrect_meanB0_B1500.mif -force

That is odd… What does this report?

mrinfo AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.mif

Does this image genuinely have 49 volumes…?

Hi @LiuYuchen,

I’ve moved your question to a new thread since the original thread was very old, and having a thread with a dedicated title might make it easier to find for others encountering the same issue.

What command could generate the bvec and bval for the dwi_b3000.mif?

Something like this would do:
mrinfo dwi_b3000.mif -export_grad_fsl bvecs bvals

@jdtournier’s usage in the original post (to which I assume @LiuYuchen was referring):

dwiextract dwi.nii.gz -fslgrad bvec bvals -shell 0,3000 dwi_b3000.mif

The -fslgrad option is an import option. So these were simply the bvecs / bvals files that must have been provided alongside image dwi.nii.gz in that case, i.e. coming from whatever software was used to convert DICOM to NIfTI.

I used dwiextract to extract all b0 images from my dwi data and mean them to get one single B0 image, then I want to merge it with the rest b1500 images.

It’s not clear to me exactly why you would want to collapse your b=0 data into a single image volume, but then retain all of the individual b=1500 images. If your intention is to pass these data through dwi2adc, then you would need a mean b=0 volume and a mean b=1500 volume.

mrconvert AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.mif AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.mif -fslgrad meanB0.bvec meanB0.bval -force

Version 3.0.0 onwards does permit use of the same image for input and output; but it’s nevertheless worth being sceptical about when in the context of trying to fix erroneous behaviour.

dwiextract -no_bzero -shells 1500 AP_PA_denoised_degibbs_eddy_biascorrect.mif AP_PA_denoised_degibbs_eddy_biascorrect_B1500.mif -force

You should not need to use the -no_bzero command-line option here. That’s principally in place for utilisation by the dwi2response script wrapper to handle the myriad algorithms at its disposal with all of their different expectations of input data.

It is weird that the B0 image only has one image, how comes the 49 images?

I can’t provide any insight into this myself; I would recommend checking the contents of “command_history” in the image header to make sure that the production of that image does indeed match what you have provided here.


It’s also worth noting I’d previously added a feature request whereby the dwishellmath script should ideally write an updated gradient table for the output image, containing one row per shell (corresponding to the one volume per shell that that script produces). This would mean that the output of dwishellmath mean could be provided directly to the dwi2adc command. Given the potential for confusion around this command I’ve now added a second feature request such that DWIs could be fed directly into the dwi2adc command without having to deal with explicit generation of trace-weighted images.

Hi, rsmith
Thanks for your attention, I do meet this problem when deal with dwi2tensor and tensor2metric.

@jdtournier’s usage in the original post (to which I assume @LiuYuchen was referring): dwiextract dwi.nii.gz -fslgrad bvec bvals -shell 0,3000 dwi_b3000.mif

It seems that dwiextract could serve as the combination of mrconvert (import gradient table to dwi.nii.gz to generate dwi.mif) and dwiextract (extract b0 and b1500 exclusively), :grinning:cool!!

It’s not clear to me exactly why you would want to collapse your b=0 data into a single image volume, but then retain all of the individual b=1500 images.

Our dwi data contains two b values (1500/3000), my tutor recommends me use the b1500 exclusively to calculate the metrics (the signal attenuation is nonlinear, high b value does not fit the linear Diffusion fit) and mean all b0 images firstly (to increase SNR). So, here comes the problem that the gradient tables need to be changed for the new data set which contains meanb0+b1500. Would it be necessary to mean all b0 for use of metric calculation and fiber tracking?

It’s also worth noting I’d previously added a feature request whereby the dwishellmath script should ideally write an updated gradient table for the output image, containing one row per shell (corresponding to the one volume per shell that that script produces). This would mean that the output of dwishellmath mean could be provided directly to the dwi2adc command.

If I understand correctly, dwishellmath could be used to generated the gradient table for meanb0 form the extracted b0.mif, that would be a better solution :grinning:

My latest command are as below, someone has similar problem could refer to it:

mrconvert AP_PA_denoised_degibbs_eddy_biascorrect.nii.gz -fslgrad AP_PA_denoised_degibbs_eddy.eddy_rotated_bvecs AP_PA.bval AP_PA_denoised_degibbs_eddy_biascorrect.mif -force
#mean b0
dwiextract -bzero AP_PA_denoised_degibbs_eddy_biascorrect.mif AP_PA_denoised_degibbs_eddy_biascorrect_B0.nii.gz -force
mrmath AP_PA_denoised_degibbs_eddy_biascorrect_B0.nii.gz mean AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.nii.gz -axis 3 -force
rm AP_PA_denoised_degibbs_eddy_biascorrect_B0.nii.gz
#extract min bvalue
dwiextract -no_bzero -shells 1500 AP_PA_denoised_degibbs_eddy_biascorrect.mif AP_PA_denoised_degibbs_eddy_biascorrect_B1500.mif -force
mrinfo AP_PA_denoised_degibbs_eddy_biascorrect_B1500.mif -export_grad_fsl B1500.bvec B1500.bval -force
paste -d ' ' meanB0.bval B1500.bval > meanB0_B1500.bval
paste -d ' ' meanB0.bvec B1500.bvec > meanB0_B1500.bvec
mrcat AP_PA_denoised_degibbs_eddy_biascorrect_meanB0.nii.gz AP_PA_denoised_degibbs_eddy_biascorrect_B1500.mif AP_PA_denoised_degibbs_eddy_biascorrect_meanB0_B1500.nii.gz -force
#calculate metric
dwi2tensor AP_PA_denoised_degibbs_eddy_biascorrect_meanB0_B1500.nii.gz ${name1}_tensor.mif -fslgrad meanB0_B1500.bvec meanB0_B1500.bval -force
tensor2metric ${name1}_tensor.mif -adc ${name1}_ADC.nii.gz -fa ${name1}_FA.nii.gz -ad ${name1}_AD.nii.gz -rd ${name1}_RD.nii.gz -mask AP_PA_mean_b0_brain_mask.nii.gz -force

It’s not clear to me exactly why you would want to collapse your b=0 data into a single image volume, but then retain all of the individual b=1500 images.

Our dwi data contains two b values (1500/3000), my tutor recommends me use the b1500 exclusively to calculate the metrics (the signal attenuation is nonlinear, high b value does not fit the linear Diffusion fit) and mean all b0 images firstly (to increase SNR).

Okay, I think I found one source of confusion. When you “average all b=0 images to increase SNR”, this will improve the SNR in the appearance of the resulting mean b=0 in comparison to one individual original b=0 volume. However you have not increased the SNR of your dataset as a whole; you’ve just produced one image that looks nicer than the original; you’ve also broken intrinsic assumptions about the nature of the noise in your dataset.

Imagine the tensor fitting occurring within the dwi2tensor command. If you use your b=0 and b=1500 data as-is, then within the tensor fitting procedure, each volume contributes equally to the model fit, based on the assumption of equivalent noise level (not SNR, just the magnitude of the noise regardless of signal) across volumes. If you have e.g. 8 b=0 volumes, that’s 8 image intensities acquired at b=0 that all contribute to the diffusion model fit. Now imagine instead that you have one solitary mean b=0 image, and your however-many b=1500 volumes. Within the tensor fitting procedure, it sees just one b=0 volume, with an intensity equivalent to the mean of the eight b=0 volumes, and a bunch of b=1500 volumes. But the assumption of equivalent noise properties across the input volumes is now broken, because one of the volumes is the result of averaging whereas the others are not. So the internal weighting of volumes based on an assumption of constant noise floor across volumes that the tensor fitting procedure does is no longer appropriate.

Averaging b=0 volumes is a good idea if you need “one b=0 volume” for some specific purpose. However if you’re processing your DWI series as a whole, it is preferable to retain the original b=0 volumes.

If I understand correctly, dwishellmath could be used to generated the gradient table for meanb0 form the extracted b0.mif, that would be a better solution

Well, if you literally just wanted a “gradient table” for a mean b=0 image, that could be achieved using:
mrconvert meanb0.mif -set_property dw_scheme 0,0,0,0 meanb0_with_grad.mif
But I’m not convinced that it would be of any particular use…

Currently, when you provide an input image to specifically the dwi2adc command (nothing else), it expects that image to have a number of volumes equivalent to the number of unique b-values, with the mean signal intensity of that shell within each volume, and an entry in the gradient table where the fourth entry provides the b-value; but it’s slightly awkward to produce data in that form (if it has not immediately come in that form from the vendor, as was the case for the original justification for creation of that command). The specific reason why having dwishellmath output an updated gradient table is because the dwi2adc command would then immediately be applicable to its output.

To be clear, there’s an important distinction between generating a mean b=0 image “for higher SNR”, and generating a mean b=0 image in addition to e.g. the mean b=1500 image for the sake of estimating ADC. The former, as described previously, is purely about the appearance of specifically a single b=0 volume, and not ideal for other purposes. In the context of the dwi2adc command as currently implemented, generating the mean (“trace-weighted”) intensity for each b-value shell is requisite for the command, as that is the format in which it expects the input data to be, in order to perform a non-tensor-based estimation of ADC. If one were to instead want to estimate ADC using the tensor model using dwi2tensor | tensor2metric, then that involves a tensor fit, so as described previously, retaining the original b=0 volumes would be preferable.