Sct_dmri_moco issue with sagittal data: missing 'mat_groups/mat.Z0000T00000GenericAffine.mat' file

Hi,

I am having some trouble with the ‘sct_dmri_moco’ function.

I am using sagittal diffusion-weighted data and think it may be a similar issue to this previous post:

Command:

--
Spinal Cord Toolbox (7.1)

sct_dmri_moco -i sub-2D2A24-1103_ses-01_acq-cervical_dwi.nii.gz -bvec sub-2D2A24-1103_ses-01_acq-cervical_dwi.bvec -bval sub-2D2A24-1103_ses-01_acq-cervical_dwi.bval -m sub-2D2A24-1103_ses-01_acq-cervical_dwi_dwi_mean_mask.nii.gz -g 1
--


Input parameters:
  Input file ............ sub-2D2A24-1103_ses-01_acq-cervical_dwi.nii.gz
  Group size ............ 1
Creating temporary folder (/var/folders/_c/8y3689616c9d70cywhybh33w0000gn/T/sct_2026-04-30_20-52-05_moco-wrapper_v__ypvd2)

Copying input data to tmp folder and convert to nii...
e[33mImage header specifies datatype 'int16', but array is of type 'float64'. Header metadata will be overwritten to use 'float64'.e[0m

Get dimensions of data...
  80 x 80 x 13

Data orientation: AIL
  Treated as sagittal

Set suffix of transformation file name, which depends on the orientation:
Orientation is sagittal, suffix is '0GenericAffine.mat'. The image is split across the R-L direction, and the estimated transformation is a 2D affine transfo.

Identify b=0 and DWI images...
NumExpr defaulting to 8 threads.

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

Split along T dimension...

Merge and average b=0 data...

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:
  80 x 80 x 13 x 1

Copy file_target to a temporary file...
e[33mImage header specifies datatype 'float64', but array is of type '<class 'numpy.uint8'>'. Header metadata will be overwritten to use '<class 'numpy.uint8'>'.e[0m

Register. Loop across Z (note: there is only one Z if orientation is axial)

-------------------------------------------------------------------------------
  Estimating motion across groups...
-------------------------------------------------------------------------------

Input parameters:
  Input file ............ datasub-groups.nii
  Reference file ........ datasub_0_mean.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_groups

Data dimensions:
  80 x 80 x 13 x 16

Copy file_target to a temporary file...
e[33mImage header specifies datatype 'float64', but array is of type '<class 'numpy.uint8'>'. Header metadata will be overwritten to use '<class 'numpy.uint8'>'.e[0m
e[33mFile /private/var/folders/_c/8y3689616c9d70cywhybh33w0000gn/T/sct_2026-04-30_20-52-05_moco-wrapper_v__ypvd2/mask_bin.nii already exists. Will overwrite it.e[0m

Register. Loop across Z (note: there is only one Z if orientation is axial)
Traceback (most recent call last):
  File "/Users/georgiabright/sct_7.1/spinalcordtoolbox/scripts/sct_dmri_moco.py", line 220, in <module>
    main(sys.argv[1:])
  File "/Users/georgiabright/sct_7.1/spinalcordtoolbox/scripts/sct_dmri_moco.py", line 205, in main
    fname_output_image = moco_wrapper(param)
  File "/Users/georgiabright/sct_7.1/spinalcordtoolbox/moco.py", line 405, in moco_wrapper
    copy_mat_files(nt, file_mat_datasub, index_moco, mat_final, param)
  File "/Users/georgiabright/sct_7.1/spinalcordtoolbox/moco.py", line 165, in copy_mat_files
    copyfile(fsrc, fdest)
  File "/Users/georgiabright/sct_7.1/python/envs/venv_sct/lib/python3.10/shutil.py", line 254, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'mat_groups/mat.Z0000T00000GenericAffine.mat'
Total runtime; 32.445 seconds.
Error: Processing of subject sub-2D2A24 failed

Output after running the command “sct_check_dependencies”:

Hi @georgia.bright,

Thanks so much for your bug report! I appreciate the detail that you included, as well as linking to the related post. This helps a lot for our debugging. :blush:

I agree that this is likely the same bug as the other post due to the similar conditions (sagittal data, mask file). Reading that thread:

  • In 2024, I think I may have falsely assumed that -g 1 was a sufficient fix. But, because you’ve specified -g 1, we can probably conclude that there is more to this issue.
  • Because of this, I would guess that the “technical investigation” part of my post still applies, because the # TODO that I previously identified is still present in the code for 7.1.

So, I think the way forward will be for me to continue with my investigation from before.

Just to check – are you able to share your subject’s data (privately, if need be)? If not, I can try to continue with the sample data from the previous thread.

Warmly,
Joshua


Side note: If you need a way to quickly unblock your work, you may wish to apply the patch that I previously described in the other post. You can do this by going into your installation of SCT (location can be found by running “echo $SCT_DIR” in a terminal), then finding the relevant source code file, and modifying the file as I described in the other post:

Though, I would mention that this is experimental and likely subject to change once we review the effects of this change within our dev team.

All good, I fixed the issue.

Turns out I used the wrong mask file; previous segmentation used to generate the mask did not fully propagate along the z direction (that’s what I get for doing pre-processing late at night).

I’m so sorry and very grateful for the trouble that you went to.

I do have another question.
The contrast agnostic segmentation (sct_deepseg spinalcord) function does not fully propagate along the z-direction due to low contrast at the lesion site.
Is there a way to fix this, such as creating a centerline file to be used by the contrast agnostic segmentation function?