Convert NIFTI (with bvec and bvals) to DICOM

Hi, I am using MRtrix within GitHub - ins-amu/scripts: prepare data for TVB to build a connectome from T1 and DWI.
For some reason, when I have to read the DWI, mrtrix seems not to accept the nifti format, which is present in the folder I’m working in, along with .bvec and .bval, but is not detected, and the following error is given

mrconvert: [ERROR] no DICOM images found in “/root/Desktop/data/DWI/”
mrconvert: [ERROR] error opening image “/root/Desktop/data/DWI/”
mrcat: [ERROR] failed to open key/value file “/root/Desktop/connectivity/predwi.mif”: No such file or directory
mrcat: [ERROR] error opening image “/root/Desktop/connectivity/predwi.mif”.

Keep in mind that while the input files I can edit are in /root/Desktop/data/DWI, the connectivity folder is created by the script itself, so theoretically it would not be editable.

Does anyone know if I can use mrconvert to edit nifti along with bvec and bval in dicom?

If you’re using the main_surface.sh script from the repo you’ve linked, the mrconvert call at lines 369-372 is configured for DICOM files in the DWI folder, not a nifti file. That’s why the first error message listed references looking for DICOM images.

You’ll need to either:

  1. Change the mrconvert line in the script to point to your *.nii file instead (making sure to include the -fslgrad flag so you can indicate filepaths to the bvec/bval files), or
  2. Put the DICOM files (*.ima or *.dcm) in that folder location.

Thank you, this was an oversight by me. I changed the code in the following:

DWI_FILE=$(ls $PRD/data/DWI/.nii | head -n 1)

BVEC_FILE=$(ls PRD/data/DWI/*.bvec | head -n 1) BVAL_FILE=(ls $PRD/data/DWI/*.bval | head -n 1)

mrconvert “$DWI_FILE” $PRD/connectivity/predwi_“$i_im”.mif
-fslgrad “$BVEC_FILE” “$BVAL_FILE”
-export_pe_table $PRD/connectivity/pe_table
-export_grad_mrtrix $PRD/connectivity/bvecs_bvals_init
-datatype float32 -stride 0,0,0,1 -force -nthreads “$NB_THREADS”
cp $PRD/connectivity/predwi_1.mif $PRD/connectivity/predwi.mif

Now only one error is present, from mrconvert, that says no phase encoding information found in predwi.mif, that is a file that the repository should create itself.

Is there a *.json file accompanying your nifti and bvec/bval files? That’s usually where the phase encoding direction info is. If so, you may need to add another flag to the mrconvert call to integrate the info from that *.json file into the *.mif (using -json_import).

Indeed there is a .json file. I updated the code in main_surface.sh like this:

DWI_FILE=$(ls $PRD/data/DWI/.nii | head -n 1)

BVEC_FILE=$(ls PRD/data/DWI/*.bvec | head -n 1) BVAL_FILE=(ls $PRD/data/DWI/*.bval | head -n 1)

JSON_FILE=$(ls $PRD/data/DWI/*.json | head -n 1)

mrconvert “$DWI_FILE” $PRD/connectivity/predwi_“$i_im”.mif
-fslgrad “$BVEC_FILE” “$BVAL_FILE”
-json_import “$JSON_FILE”
-export_pe_table $PRD/connectivity/pe_table
-export_grad_mrtrix $PRD/connectivity/bvecs_bvals_init
-datatype float32 -stride 0,0,0,1 -force -nthreads “$NB_THREADS”
cp $PRD/connectivity/predwi_1.mif $PRD/connectivity/predwi.mif

However, I still end up with the same exact error that I had without the json import:

generate dwi mif file
if asked, please select a series of images by typing a number
mrconvert: [ERROR] no phase-encoding information found within image “/root/Desktop/connectivity/predwi_1.mif”
Do you want to add another image serie (different phase encoding)? [y, n]

First, do you have \ at the end of each line for the mrconvert call (as described here)? Just want to make sure that it’s actually detecting the -fslgrad, -json_import, and other options because without the \, it wouldn’t know that it’s part of the same line.

Then, if you verified the above, did you look inside the *.json file? Is there a field for “PhaseEncodingDirection”? If not, then it may be that:

  • the data has been anonymized in such a way that’s stripped the phase encoding direction info from the DICOMs
  • the scanner itself (if it’s a Philips scanner) does not give PhaseEncodingDirection, but only PhaseEncodingAxis

If you have access to the original DICOMs, maybe you can try using that as direct input into your pipeline to also rule out that the conversion process didn’t fail to retain the phase encoding direction info.

You can read about phase encoding here:

Yes, I have \ at the end of each flag.
I looked at the .json file and indeed it is as you said.
Probably some info has been lost in the conversion from DICOM.

Thanks for all the help.