Issue with Motion correction and size of data

Hello SCT team,

When applying motion correction I’m getting this error despite bval and bvecs containing 21 columns, first being b=0.

--
Spinal Cord Toolbox (6.5)


--


Input parameters:
  Input file ............ IMAGES_AXIAL_DTI_AND_DIFFUSION_20160515110901_23_dwi.nii
  Group size ............ 3
Creating temporary folder (/tmp/sct_2025-06-11_22-10-12_moco-wrapper_iied6301)

Copying input data to tmp folder and convert to nii...

Get dimensions of data...
  166 x 66 x 20

Data orientation: RPI
  Treated as axial

Set suffix of transformation file name, which depends on the orientation:
Orientation is axial, suffix is 'Warp.nii.gz'. The estimated transformation is a 3D warping field, which is composed of a stack of 2D Tx-Ty transformations

Identify b=0 and DWI images...

Identify b=0 and DWI images...
  Number of b=0: 1 [0]
  Number of DWI: 20 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

ERROR in moco.py: Size of data (20) and size of bvecs (21) are not the same. Check your bvecs file.

How would i correct this?

Dear @sabaig,

Inferring from the error message (20 vs 21) as well as the reported input file name, it seems you may be applying the moco script to just the DWI volumes (20), when the moco script expects the full dMRI image with both b0 and DWI volumes: Motion correction for dMRI images - Spinal Cord Toolbox documentation

(Note that while the above dMRI tutorial does include an earlier step to separate the b0 and DWI volumes, this is only for the purposes of creating a mask to highlight the spinal cord.)

Kind regards,
Joshua

Dear @joshuacwnewton,
I think I solved it, the b0 volumes are included as the first column and there was a duplicated column for one of the DWI volumes which I removed. Unfortunately now, I’m having this issue and im wondering if when I generated the 3d image containing the mean if there is a problem:

Spinal Cord Toolbox (6.5)

sct_dmri_moco -i IMAGES_AXIAL_DTI_AND_DIFFUSION_20160515110901_23_dwi.nii -m mask_IMAGES_AXIAL_DTI_AND_DIFFUSION_20160515110901_23_dwi_mean.nii -bval IMAGES_AXIAL_DTI_AND_DIFFUSION_20160515110901_23_trimmed.bval -bvec IMAGES_AXIAL_DTI_AND_DIFFUSION_20160515110901_23_trimmed.bvec -qc /home/saif/qc_singleSubj -qc-seg IMAGES_AXIAL_DTI_AND_DIFFUSION_20160515110901_23_dwi_mean_seg.nii.gz
--


Input parameters:
  Input file ............ IMAGES_AXIAL_DTI_AND_DIFFUSION_20160515110901_23_dwi.nii
  Group size ............ 3
Creating temporary folder (/tmp/sct_2025-06-11_23-35-04_moco-wrapper_pgv4txtc)

Copying input data to tmp folder and convert to nii...

Get dimensions of data...
  166 x 66 x 20

Data orientation: RPI
  Treated as axial

Set suffix of transformation file name, which depends on the orientation:
Orientation is axial, suffix is 'Warp.nii.gz'. The estimated transformation is a 3D warping field, which is composed of a stack of 2D Tx-Ty transformations

Identify b=0 and DWI images...

Identify b=0 and DWI images...
  Number of b=0: 1 [0]
  Number of DWI: 19 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Split along T dimension...

Merge and average b=0 data...
Image header specifies datatype 'int16', but array is of type 'float64'. Header metadata will be overwritten to use 'float64'.
Merge within groups:   0%|                              | 0/7 [00:00<?, ?iter/s]Image header specifies datatype 'int16', but array is of type 'float64'. Header metadata will be overwritten to use 'float64'.
Image header specifies datatype 'int16', but array is of type 'float64'. Header metadata will be overwritten to use 'float64'.
Merge within groups:  29%|██████▎               | 2/7 [00:00<00:00, 15.80iter/s]Image header specifies datatype 'int16', but array is of type 'float64'. Header metadata will be overwritten to use 'float64'.
Image header specifies datatype 'int16', but array is of type 'float64'. Header metadata will be overwritten to use 'float64'.
Merge within groups:  57%|████████████▌         | 4/7 [00:00<00:00, 15.21iter/s]Image header specifies datatype 'int16', but array is of type 'float64'. Header metadata will be overwritten to use 'float64'.
Image header specifies datatype 'int16', but array is of type 'float64'. Header metadata will be overwritten to use 'float64'.
Merge within groups:  86%|██████████████████▊   | 6/7 [00:00<00:00, 15.18iter/s]Image header specifies datatype 'int16', but array is of type 'float64'. Header metadata will be overwritten to use 'float64'.
Merge within groups: 100%|██████████████████████| 7/7 [00:00<00:00, 16.35iter/s]

Merge across groups...

-------------------------------------------------------------------------------
  Estimating motion on b=0 images...
-------------------------------------------------------------------------------

Input parameters:
  Input file ............ b0.nii
  Reference file ........ data_T0000.nii.gz
  Polynomial degree ..... 2
  Smoothing kernel ...... 1
  Gradient step ......... 1
  Metric ................ MI
  Sampling .............. None
  Todo .................. estimate_and_apply
  Mask  ................. mask.nii
  Output mat folder ..... mat_b0groups

Data dimensions:
  166 x 66 x 20 x 1

Copy file_target to a temporary file...
Image header specifies datatype 'int64', but array is of type '<class 'numpy.uint8'>'. Header metadata will be overwritten to use '<class 'numpy.uint8'>'.

Register. Loop across Z (note: there is only one Z if orientation is axial)
Z=0/0:   0%|                                            | 0/1 [00:00<?, ?iter/s]
Traceback (most recent call last):
  File "/home/saif/sct_6.5/spinalcordtoolbox/scripts/sct_dmri_moco.py", line 235, in <module>
    main(sys.argv[1:])
  File "/home/saif/sct_6.5/spinalcordtoolbox/scripts/sct_dmri_moco.py", line 220, in main
    fname_output_image = moco_wrapper(param)
  File "/home/saif/sct_6.5/spinalcordtoolbox/moco.py", line 330, in moco_wrapper
    file_mat_b0, _ = moco(param_moco)
  File "/home/saif/sct_6.5/spinalcordtoolbox/moco.py", line 610, in moco
    failed_transfo[it] = register(param, file_data_splitZ_splitT[it], file_target_splitZ[iz], file_mat[iz][it],
  File "/home/saif/sct_6.5/spinalcordtoolbox/moco.py", line 754, in register
    status, output = run_proc(cmd, verbose=1 if param.verbose == 2 else 0, env=env, **kw)
  File "/home/saif/sct_6.5/spinalcordtoolbox/utils/sys.py", line 342, in run_proc
    raise RuntimeError(output)
RuntimeError: setting mask /tmp/sct_2025-06-11_23-35-04_moco-wrapper_pgv4txtc/mask_bin.nii
Exception caught:
itk::ExceptionObject (0x2b86520)
Location: "unknown"
File: /__w/build_ANTs/build_ANTs/antsbin/staging/include/ITK-5.1/itkMattesMutualInformationImageToImageMetricv4.hxx
Line: 312
Description: itk::ERROR: MattesMutualInformationImageToImageMetricv4(0x2996860): Joint PDF summed to zero

Dear @sabaig,

My apologies for misunderstanding your initial error, and thank you for sharing the resolution of that issue.

Regarding the second issue:

We have encountered this error twice before (1, 2). In both issues, @jcohenadad writes roughly the same comment:

this is likely related to a lack of overlap between the source and dest. Again, possibly related to poor registration performance, which we need to assess visually.

This is caused by a lack of overlap between src and dest images

Because the error occurred during the b0 motion estimation step, the src and dest refer to the input and reference files mentioned in the log:

  Input file ............ b0.nii
  Reference file ........ data_T0000.nii.gz

Suggesting that the b0 volume has poor overlap with the first DWI volume (as selected by this code). I would recommend double checking the b=0 volume and the first DWI volume in an image viewer and comparing their overlap. This could also potentially be due to orientation (sagittal image interpreted as axial, etc.) which is a potential cause that is mentioned in the linked issue, as well.

Note: If my response is not sufficient, and further debugging is necessary, I would mention that it will be tricky to debug further without assessing the data visually. Could you please share the data needed to run the command (if possible)?

Kind regards,
Joshua