Registration to T1 ex-vivo Template ERROR

Hi SCT Team,

I’m trying to register my high resolution Ex vivo human dwi data into T1 template here https://github.com/sct-pipeline/exvivo-template.

I did the following steps but ended up with an error in template registration…

1-Dicom data imported from Bruker animal MRI system. Converted to nifty using mrconvert as package from MRtrix dcm to nifty and also used dcm2niix software for comparison…

The fslhd output using dcm2niix converter is below

filename dwi_b0_b1500_reordered.nii.gz

sizeof_hdr 348

data_type FLOAT32

dim0 4

dim1 100

dim2 512

dim3 100

dim4 20

dim5 1

dim6 1

dim7 1

vox_units mm

time_units s

datatype 16

nbyper 4

bitpix 32

pixdim0 -1.000000

pixdim1 0.200000

pixdim2 0.156250

pixdim3 0.200000

pixdim4 0.300000

pixdim5 0.000000

pixdim6 0.000000

pixdim7 0.000000

vox_offset 352

cal_max 0.000000

cal_min 0.000000

scl_slope 1.000000

scl_inter 0.000000

phase_dim 0

freq_dim 0

slice_dim 0

slice_name Unknown

slice_code 0

slice_start 0

slice_end 0

slice_duration 0.000000

toffset 0.000000

intent Unknown

intent_code 0

intent_name

intent_p1 0.000000

intent_p2 0.000000

intent_p3 0.000000

qform_name Scanner Anat

qform_code 1

qto_xyz:1 0.000000 0.000000 0.200000 -9.800000

qto_xyz:2 -0.200000 0.000000 -0.000000 3.000000

qto_xyz:3 0.000000 0.156250 -0.000000 -39.843750

qto_xyz:4 0.000000 0.000000 0.000000 1.000000

qform_xorient Anterior-to-Posterior

qform_yorient Inferior-to-Superior

qform_zorient Left-to-Right

sform_name Scanner Anat

sform_code 1

sto_xyz:1 0.000000 0.000000 0.200000 -9.800000

sto_xyz:2 -0.200000 0.000000 -0.000000 3.000000

sto_xyz:3 0.000000 0.156250 0.000000 -39.843750

sto_xyz:4 0.000000 0.000000 0.000000 1.000000

sform_xorient Anterior-to-Posterior

sform_yorient Inferior-to-Superior

sform_zorient Left-to-Right

file_type NIFTI-1+

file_code 1

descrip 6.0.4:ddd0a010

The fslhd output using ***mrconvert*** converter is below

filename dwi_1500.nii.gz

sizeof_hdr 348

data_type FLOAT32

dim0 4

dim1 100

dim2 512

dim3 100

dim4 20

dim5 1

dim6 1

dim7 1

vox_units mm

time_units s

datatype 16

nbyper 4

bitpix 32

pixdim0 1.000000

pixdim1 0.156250

pixdim2 0.200000

pixdim3 0.200000

pixdim4 nan

pixdim5 0.000000

pixdim6 0.000000

pixdim7 0.000000

vox_offset 352

cal_max 0.000000

cal_min 0.000000

scl_slope 1.000000

scl_inter 0.000000

phase_dim 0

freq_dim 0

slice_dim 0

slice_name Unknown

slice_code 0

slice_start 0

slice_end 0

slice_duration 0.000000

toffset 0.000000

intent Unknown

intent_code 0

intent_name

intent_p1 0.000000

intent_p2 0.000000

intent_p3 0.000000

qform_name Scanner Anat

qform_code 1

qto_xyz:1 0.000000 0.000000 0.200000 -9.800000

qto_xyz:2 -0.156250 0.000000 0.000000 3.000000

qto_xyz:3 0.000000 -0.200000 0.000000 40.000000

qto_xyz:4 0.000000 0.000000 0.000000 1.000000

qform_xorient Anterior-to-Posterior

qform_yorient Superior-to-Inferior

qform_zorient Left-to-Right

sform_name Scanner Anat

sform_code 1

sto_xyz:1 0.000000 0.000000 0.200000 -9.800000

sto_xyz:2 -0.156250 0.000000 0.000000 3.000000

sto_xyz:3 0.000000 -0.200000 0.000000 40.000000

sto_xyz:4 0.000000 0.000000 0.000000 1.000000

sform_xorient Anterior-to-Posterior

sform_yorient Superior-to-Inferior

sform_zorient Left-to-Right

file_type NIFTI-1+

file_code 1

descrip 6.0.4:ddd0a010

I provided these information to make sure which software should be used to handle the post-processing in correct way when using SCT. Please note, I used dcm2niix nifty output for first registration and mrconvert output for second attempt. However, I got the same error…

**dwi mean was computed and cord segmentation produced using sct_propseg

3- dwi date splitted using sct_image flag -split t to get b0 image

4-b0 and cord seg were resampled to T1 template which is 0.08mm3

5-sct_straighten_spinalcord -i b0_resambeled.nii.gz -s cord_segmentation_resampled.nii.gz used to straighten the cord.

result..

6-I apply the transformation out of b0 in the previous step to the cord segmentation as well using sct_apply_transfo command.

7-Now, I tried to create the segments labels using the following syntax along Z direction…

sct_label_utils -i dwi_1500_T0000_res_straight.nii.gz -create 125,935,125,3:125,311,125,4:125,603,125,5:125,392,125,6:125,222,125,7 -o sc_labels.nii.gz

Terminal output error…

Traceback (most recent call last):

File "/data/home/s4333238/.ssh/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_label_utils.py", line 413, in <module>

main(sys.argv[1:])

File "/data/home/s4333238/.ssh/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_label_utils.py", line 261, in main

out = sct_labels.create_labels_empty(img, labels)

File "/data/home/s4333238/.ssh/spinalcordtoolbox/spinalcordtoolbox/labels.py", line 53, in create_labels_empty

out = _add_labels(zeros_like(img), coordinates)

File "/data/home/s4333238/.ssh/spinalcordtoolbox/spinalcordtoolbox/labels.py", line 84, in _add_labels

img.data[int(x), int(y), int(z)] = v

**IndexError: index 935 is out of bounds for axis 1 with size 195**

So, I tried to swapdim to get the labels correctly pointed in the right axis I used the following syntax

fslswapdim dwi_res_straight.nii.gz x z y dwi_res_straight_xzy.nii.gz

Then I re-ran the syntax using dwi_res_straight_xzy.nii.gz as input…

output…

Generating output files...

Done! To view results, type:

fsleyes dwi_1500_T0000_res_straight_swapxzy.nii.gz sc_labels.nii.gz &

8-After I had the data and cord seg. resampled,straightened,labeled, I ran the following command for registration…

sct_register_to_template -i dwi_stra_xzy.nii.gz -s dwi_mean_seg_res_reg_xzy.nii.gz -lspinal sc_labels.nii.gz -t exvivo-template/ -c t1

Output…ERROR

Check template files...

OK: exvivo-template/template/template.nii.gz

OK: exvivo-template/template/label_spinalsegments.nii.gz

OK: exvivo-template/template/mask_spinalcord.nii.gz

Check parameters:

Data: dwi_stra_xzy.nii.gz

Landmarks: sc_labels.nii.gz

Segmentation: dwi_mean_seg_res_reg_xzy.nii.gz

Path template: exvivo-template/

Remove temp files: 1

Check input labels...

Creating temporary folder (/tmp/sct-20210306153353.274717-register_to_template-3ql8kvwm)

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

Check if provided labels are available in the template

To avoid intensity overflow due to convertion to +uint8+, intensity will be rescaled to the maximum quantization scale

Binarize segmentation

Resample data to 1mm isotropic...

load data...

Done! To view results, type:

fsleyes data_1mm.nii &

load data...

Done! To view results, type:

fsleyes seg_bin_1mm.nii.gz &

To avoid intensity overflow due to convertion to +uint8+, intensity will be rescaled to the maximum quantization scale

Change orientation of input images to RPI...

Straighten the spinal cord using centerline/segmentation...

Creating temporary folder (/tmp/sct-20210306153822.455291-straighten_spinalcord-p1h_7a5e)

Copy files to tmp folder...

**Image /tmp/sct-20210306153353.274717-register_to_template-3ql8kvwm/template_seg.nii.gz has different qform and sform matrices. This can produce incorrect results. Consider setting the two matrices to be equal by running:**

sct_image -i /tmp/sct-20210306153353.274717-register_to_template-3ql8kvwm/template_seg.nii.gz -set-sform-to-qform -o /tmp/sct-20210306153353_same-affine.274717-register_to_template-3ql8kvwm/template_seg.nii.gz.

Traceback (most recent call last):

File "/data/home/s4333238/.ssh/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_register_to_template.py", line 1281, in <module>

main(sys.argv[1:])

File "/data/home/s4333238/.ssh/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_register_to_template.py", line 533, in main

sc_straight.straighten()

File "/data/home/s4333238/.ssh/spinalcordtoolbox/spinalcordtoolbox/straightening.py", line 111, in straighten

Image(self.centerline_reference_filename, check_sform=True).save(os.path.join(path_tmp, "centerline_ref.nii.gz"))

File "/data/home/s4333238/.ssh/spinalcordtoolbox/spinalcordtoolbox/image.py", line 311, in __init__

raise ValueError("Image sform does not match qform")

**ValueError: Image sform does not match qform**

Then I ran the following syntax to set sform and qform on my input data,seg and labels.

sct_image data.nii.gz -set-sform-to-qform data_corrected.nii.gz

After I’ve done it also for other inputs labels and segmentation, I re-ran the registration on the corrected one.

The final data used for registration after processing/ Step 1 I used dcm2niix output for post-processing…

filename dwi_T0000_straight_resa_xzy.nii.gz

sizeof_hdr 348

data_type FLOAT64

dim0 3

dim1 248

dim2 1004

dim3 248

dim4 1

dim5 1

dim6 1

dim7 1

vox_units mm

time_units s

datatype 64

nbyper 8

bitpix 64

pixdim0 -1.000000

pixdim1 0.079839

pixdim2 0.079993

pixdim3 0.079839

pixdim4 1.000000

pixdim5 1.000000

pixdim6 1.000000

pixdim7 1.000000

vox_offset 352

cal_max 0.000000

cal_min 0.000000

scl_slope 1.000000

scl_inter 0.000000

phase_dim 0

freq_dim 0

slice_dim 0

slice_name Unknown

slice_code 0

slice_start 0

slice_end 0

slice_duration 0.000000

toffset 0.000000

intent Unknown

intent_code 0

intent_name

intent_p1 0.000000

intent_p2 0.000000

intent_p3 0.000000

qform_name Unknown

qform_code 0

qto_xyz:1 0.079839 -0.000000 -0.000000 -9.720161

qto_xyz:2 0.000000 0.000000 0.079839 -16.800001

qto_xyz:3 0.000000 0.079993 -0.000000 -40.468750

qto_xyz:4 0.000000 0.000000 0.000000 1.000000

qform_xorient Left-to-Right

qform_yorient Inferior-to-Superior

qform_zorient Posterior-to-Anterior

sform_name Unknown

sform_code 2

sto_xyz:1 0.079839 0.000000 0.000000 -9.720161

sto_xyz:2 0.000000 0.000000 0.079839 -16.800001

sto_xyz:3 0.000000 0.079993 0.000000 -40.468750

sto_xyz:4 0.000000 0.000000 0.000000 1.000000

sform_xorient Left-to-Right

sform_yorient Inferior-to-Superior

sform_zorient Posterior-to-Anterior

file_type NIFTI-1+

file_code 1

descrip 6.0.4:ddd0a010

**The final data used for registration after processing/ Step 2, I used mrconvert output for post-processing..**

filename dwi_T0000_res_xzy.nii.gz

sizeof_hdr 348

data_type FLOAT64

dim0 3

dim1 249

dim2 1286

dim3 195

dim4 1

dim5 1

dim6 1

dim7 1

vox_units mm

time_units s

datatype 64

nbyper 8

bitpix 64

pixdim0 -1.000000

pixdim1 0.080000

pixdim2 0.080000

pixdim3 0.080128

pixdim4 0.000000

pixdim5 0.000000

pixdim6 0.000000

pixdim7 0.000000

vox_offset 352

cal_max 0.000000

cal_min 0.000000

scl_slope 1.000000

scl_inter 0.000000

phase_dim 0

freq_dim 0

slice_dim 0

slice_name Unknown

slice_code 0

slice_start 0

slice_end 0

slice_duration 0.000000

toffset 0.000000

intent Unknown

intent_code 0

intent_name

intent_p1 0.000000

intent_p2 0.000000

intent_p3 0.000000

qform_name Scanner Anat

qform_code 1

qto_xyz:1 0.080000 -0.000000 -0.000000 -9.720001

qto_xyz:2 0.000000 0.000000 0.080128 -12.544872

qto_xyz:3 0.000000 0.080000 -0.000000 -62.799995

qto_xyz:4 0.000000 0.000000 0.000000 1.000000

qform_xorient Left-to-Right

qform_yorient Inferior-to-Superior

qform_zorient Posterior-to-Anterior

sform_name Scanner Anat

sform_code 0

sto_xyz:1 0.000000 0.000000 0.000000 0.000000

sto_xyz:2 0.000000 0.000000 0.000000 0.000000

sto_xyz:3 0.000000 0.000000 0.000000 0.000000

sto_xyz:4 0.000000 0.000000 0.000000 1.000000

sform_xorient Unknown

sform_yorient Unknown

sform_zorient Unknown

file_type NIFTI-1+

file_code 1

descrip 6.0.4:ddd0a010

I ended up with the same error… I wonder whether mty steps were in right track or not. Is there any comment regarding the data header info. as I’m really confused about which oriention is recommended.

Sorry for the long post but I tried to provide you with more details as possible to track the error and for others if they get same issue in future…

Many thanks in advance…

Cheers,

Ibrahim

Hi @ihattan,

Thank you for reporting this error. In the future, could you please format your code using “```” (see the instruction above for opening new tickets). Otherwise it is extremely difficult for us to distinguish the output terminal from your comments. I’ve reformatted your post for you, as an example for you in the future.

Also, instead of commenting what was done, e.g.:

3- dwi date splitted using sct_image flag -split t to get b0 image
4-b0 and cord seg were resampled to T1 template which is 0.08mm3

It is way easier for us if you copy/paste from the Terminal the code that was run. Ideally, you would create a script that lists all the commands. Another reason for doing that, is because there are various ways of doing one thing (e.g. resampling an image), and when trying to debug we are interested in the how instead of the what. Thank you.

Now, trying to address your issues:

**IndexError: index 935 is out of bounds for axis 1 with size 195**

→ this issue is likely related to the fact that dwi_1500_T0000_res_straight.nii.gz has less than 935 slices. How did you choose this number 935? If you chose it on your un-straightened image, then you need to also straighten your labels.

So, I tried to swapdim to get the labels correctly pointed in the right axis I used the following syntax
fslswapdim dwi_res_straight.nii.gz x z y dwi_res_straight_xzy.nii.gz

I don’t recommend this command because you loose the actual orientation of your sample. Instead, if you want your data, let’s say in RPI, then use instead:

sct_image -i IMAGE -setorient RPI -o OUTPUT

I ended up with the same error… I wonder whether mty steps were in right track or not. Is there any comment regarding the data header info. as I’m really confused about which oriention is recommended.

Based on what I said earlier, it is extremely difficult for us to help without knowing exactly what was done. So, could you please package the relevant data in a single ZIP file, share, and also package your commands in a shell script, so we are able to reproduce exactly what you did by simply running the script.

Best,
Julien

1 Like

Hi Julien,

Thank you very much for your prompt response and valuable feedback. I shared the data and script as requested. Please check your email for the link and password. Please Let me know if further information is needed.

I appreciate your effort and time looking into these issues.

Please note…

All data and output that were shared with you I used mrconvert to nii to convert Bruker Dicom. I shared a file named “dwi_b0_b1500_reordered.nii.gz” for the same sample which I used dcm2niix for you for comparison. I used the same script to process dcm2niix output but had the same error as well.

slices number and labels, there are over 1000 slices and labels syntax used on the straightened data. Please see screenshots below…

fslswapdim/RPI, I couldn’t run sct_label_utils until I do swap the axis. I look forward a solution for this issue please to maintain the orientation (after you have a look into the actual data). In addition, I tried the -setorient RPI but didn’t work out in my end. Could you please re-produce it and test
sct_label_utils?

Many thanks once again for helping to fix this issue…

Cheers,

Ibrahim

Hi,
The script crashes at the first line:

julien-macbook:~/Desktop/julien_templat_re-test $ sct_dmri_separate_b0_and_dwi -i dwi_1500.nii.gz -bvec bvec_onl_conc.txt

--
Spinal Cord Toolbox (git-jn/3093-migrate-sct_testing-tests-not-implemented-1f68c39acaa7e6c301e975935ea30d1fa5ae7d79)

sct_dmri_separate_b0_and_dwi -i dwi_1500.nii.gz -bvec bvec_onl_conc.txt
--


Input parameters:
  input file ............dwi_1500.nii.gz
  bvecs file ............bvec_onl_conc.txt
  bvals file ............
  average ...............1
Creating temporary folder (/var/folders/s8/4qnm5q1n261ch35b5kkclsb00000gn/T/sct-20210307154011.409286-dmri_separate-6k_uu320)

Copy files into temporary folder...
sct_convert -i /Users/julien/Desktop/julien_templat_re-test/dwi_1500.nii.gz -o /var/folders/s8/4qnm5q1n261ch35b5kkclsb00000gn/T/sct-20210307154011.409286-dmri_separate-6k_uu320/dmri.nii
Traceback (most recent call last):
  File "/Users/julien/code/sct/python/envs/venv_sct/lib/python3.6/site-packages/nibabel/loadsave.py", line 42, in load
    stat_result = os.stat(filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/julien/Desktop/julien_templat_re-test/dwi_1500.nii.gz'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/julien/code/sct/spinalcordtoolbox/scripts/sct_dmri_separate_b0_and_dwi.py", line 322, in <module>
    main(sys.argv[1:])
  File "/Users/julien/code/sct/spinalcordtoolbox/scripts/sct_dmri_separate_b0_and_dwi.py", line 163, in main
    if not convert(fname_data, os.path.join(path_tmp, dmri_name + ext)):
  File "/Users/julien/code/sct/spinalcordtoolbox/scripts/sct_convert.py", line 77, in convert
    img = image.Image(fname_in)
  File "/Users/julien/code/sct/spinalcordtoolbox/image.py", line 283, in __init__
    self.loadFromPath(param, verbose)
  File "/Users/julien/code/sct/spinalcordtoolbox/image.py", line 404, in loadFromPath
    self.im_file = nib.load(path)
  File "/Users/julien/code/sct/python/envs/venv_sct/lib/python3.6/site-packages/nibabel/loadsave.py", line 44, in load
    raise FileNotFoundError(f"No such file or no access: '{filename}'")
FileNotFoundError: No such file or no access: '/Users/julien/Desktop/julien_templat_re-test/dwi_1500.nii.gz'

I could fix it by modifying the input file name ( dwi_1500.nii.gz --> dwi_1500_dwi.nii.gz) but it defeats the purpose of reproducibility. I need to be able to reproduce exactly what you did. Could you please send me the exact script that you ran. Thanks

1 Like

Hi @jcohenadad,

Sorry for that… Could you please use the same script with the data sent recently (dwi_1500.nii.gz). Please check email for the password and link. I re-ran the same script on this file and it re-produced the result. Please check the output terminal for each command for reference (attached).

terminal_output_script (17.5 KB)

Please note that I could not create the label file until did the swapdim step. Is there a specific orientation that sct_image accept it to create the label along Z direction?

I re-produce the same result and stopped at sct_register _to_template error…See please the terminal output for details…

Many thanks in advance…

Cheers,

Ibrahim

hi-- just to let you know i’m terribly busy-- i’ll work on it when i find a minute

@ihattan just to let you know that I’ve been working on this issue since yesterday. I’ve discovered a couple of issues. I will follow up shortly (1-2 days) with a temporary solution for you.

1 Like

Hi @jcohenadad,

Many thanks for your update and look forward to your solution…

Cheers,

Ibrahim

Hi @ihattan,

Just to let you know where I’m at:

I’ve identified the following issues/bugs while working on your data:

On SCT:

On ivadomed (used by SCT):

On ivadomed model (used by sct_deepseg):

On the exvivo human template:

I will not be able to solve all these issues in the next few days, however, I will fix the ones that are most problematic for you to be able to resume your analyses.

I currently have a working analysis pipeline (which includes a few hacks due to some of the listed issues above), however the only problematic part so far is the exvivo template which needs a few fixes. I’ll try to finish that today/tomorrow.

Best
Julien

2 Likes

Hi @ihattan, just a quick message to let you know that I have not forgotten this issue-- it is still on my priority list, unfortunately other urgent tasks keep piling up on my desk… Do you have a hard deadline on your end for when you would like this feature fixed? It will help me get organize wrt. priorities–
sorry about that–

1 Like

Hi @jcohenadad,

Thank you very much for getting back with update. I appreciate all your and other SCT team members effort and time to solve all theses issues. I’m sorry to say that deadline is very close to show the registration results of my DWI-FA to T1 ex vivo template to my supervisors using SCT software. I’m almost pending all my work till these issues are fixed. I understand that how much time and effort are taking these issues to be solved. So, I’ll be waiting till I can re-analyse my data if possible once you’ve finished

Many thanks once again.

Cheers,

Ibrahim

Noted, I will provide a solution within the next 2 days.

1 Like

sorry-- it will take maybe one more day-- we are doing our best…

1 Like

Many thanks @jcohenadad and your team. I highly appreciate your time and effort to fix these issues. No worries I’m using now git clone https://github.com/neuropoly/spinalcordtoolbox version as @Charley_Gros recommended to test couple of things.

We finally have a solution for you! :tada:

Firstly, you need to update SCT to the latest development version (the fix will be available in the next release: 5.3.0). If you already have it installed with git, you simply need to do:

cd $SCT_DIR
git checkout master
git pull

Given that you wish to register your exvivo MRI to the exvivo mouse template (not to the default PAM50 template), we first need to install this new template under SCT’s installation directory:

sct_download_data -d exvivo_template -o $SCT_DIR/data/exvivo_template

Now that the exvivo template is installed, we can register an exvivo MRI to the template.

On the topic of spinal labeling, I also noticed that the labels you manually set are inconsistent:

sct_label_utils -i dwi_1500_T0000_res_straight_swapxzy.nii.gz -create 125,935,125,3:125,311,125,4:125,603,125,5:125,392,125,6:125,222,125,7 -o sc_labels.nii.gz

Here, label 3 is at y=935, label 4 at y=311 and label 5 at y=603. This is anatomically impossible. So, I looked at the image (mean b=0), in the curved native space, after setting the proper orientation, and manually set the levels as I thought they should be. I assumed the top visible level was C2 spinal. I roughly considered the spinal level to be at the center of the nerve rootlets as shown below:

:warning: ‎ ‎I noticed that your data header is not consistent: Anterior is labeled as “Posterior” (and the other way around). It is extremely important to work with consistent orientation otherwise segmentation and registration will fail. So the first line of the pipeline below fixes the issue, but in the future you always need to be careful with data orientation.

Now, let’s run the registration pipeline!

# Pipeline tested with SCT git-master-16d3ce661023c9c998cf6df7a0fa772b57033fa2
# ~~~
# Reorient data to be consistent with header. 
sct_image -i dwi_1500.nii.gz -setorient-data PSL -o dwi_1500_orient.nii.gz
# Separate b=0 and DWI data, for subsequent segmentation
sct_dmri_separate_b0_and_dwi -i dwi_1500_orient.nii.gz -bvec bvec_onl_conc.txt
# Segment the spinal cord white matter and gray matter into two separate labels
sct_deepseg -i dwi_1500_orient_b0_mean.nii.gz -task seg_exvivo_gm-wm_t2
# Create spinal levels
sct_label_utils -i dwi_1500_orient_b0_mean.nii.gz -create 53,42,27,2.0:67,127,47,3.0:67,202,47,4.0:57,317,48,5.0:56,398,52,6.0:61,474,53,7.0 -o spinal_levels.nii.gz
# Register to template
# Tips 1: We use the spinal levels (not vertebral or disc), hence the flag "-lspinal"
# Tips 2: Given that the input image is almost straight, we prefer to use the option "-ref subject". This bypasses the cord straightening.
# Tips 3: We use the WM segmentation instead of the cord segmentation in order to take into account the GM shape for the registration to template. When doing so, we need to let SCT knows that the template destination should be the WM seg, hence the flag -s-template-id 4.
# Tips 4: We use 4 steps: the two first steps are slicewise affine registration with two different shrink factors, and the two last steps are non-linear SyN algo with two different smooth factors.
sct_register_to_template -i dwi_1500_orient_b0_mean.nii.gz -s dwi_1500_orient_b0_mean_wmseg.nii.gz -lspinal spinal_levels.nii.gz -s-template-id 4 -t $SCT_DIR/data/exvivo_template -c t2 -ref subject -qc qc -param step=1,type=seg,algo=affine,metric=MeanSquares,deformation=1x1x0,slicewise=1,shrink=4,iter=10:step=2,type=seg,algo=affine,metric=MeanSquares,deformation=1x1x0,slicewise=1,shrink=2,iter=10:step=3,type=seg,algo=syn,metric=CC,deformation=1x1x0,slicewise=1,iter=3,smooth=1:step=4,type=im,algo=syn,metric=CC,deformation=1x1x0,slicewise=0,iter=3,smooth=0
# Warp template
# Tips: We need to specify -a 0 because there is no WM atlas on this template (yet)
sct_warp_template -d dwi_1500_orient_b0_mean.nii.gz -w warp_template2anat.nii.gz -t $SCT_DIR/data/exvivo_template -a 0 -qc qc

Here are the results (click on the image to see the animation):

anim

:information_source: ‎ I played a bit with various parameters for sct_register_to_template, if you are interested in those investigations, here is the QC reports for all the combinations of parameters I tried. At the end I picked the one at “2021-04-01 11:47:29”

Let us know how it goes! And thank you @Charley_Gros for the help :pray:

2 Likes

Hi @jcohenadad and all SCT team,

Thank you so much for this fantastic job. I’ve successfully downloaded the updated sct via github and ex-vivo template. Also, I would like to confirm that same result has been reproduced using the same pipeline you’ve provided.
Couple of questions please if possible…

–> In regard to data orientation…

Indeed, I’ll make sure all inputs data, label, and spinal seg are in same orientation.

So, Why did you choose PSL orientation?

–> Regarding to the labels,
You are right they will give you wrong result unless you resample the dwi data to T1 template resolution.

These labels that I’ve provided used for T1 template construction by @Charley_Gros. Thank you Charley for that.
I wonder was these labels based on raw or straightened T1. Could you please @Charley_Gros confirm whether for raw or straight T1?

Thank you @jcohenadad for the investigation and coming up with correct one as the data I had already acquired by another group.

–> @jcohenadad Why you didn’t straight and resamples the data before the registration?

I ask this question as I tried to do so for FA using the same pipeline but didn’t work out.

I wonder whether or not to do straightening after registration ?

I would like at the end to crop the spinal segments for FA C2-C8 after registering to T1 template. Then average the segments from all samples to construct the FA template based on spinal levels. Please correct me if there is any confusion or need further clarification.

Many thanks once again for your time and effort for this amazing work using the best software ever :blush:

Cheers,

Ibrahim

So, Why did you choose PSL orientation?

I could have chosen any other orientation because SCT reorients the data during the analysis. The important thing is to have the correct orientation, i.e., a qform that is consistent with the data. I chose PSL because this is how your data matrix is encoded.

–> Regarding to the labels,
You are right they will give you wrong result unless you resample the dwi data to T1 template resolution.

No, the labels you indicated in Registration to T1 ex-vivo Template ERROR - #5 by ihattan will always give wrong results. Wether we resample or not. This is what you wrote in the script:

125,935,125,3:125,311,125,4:125,603,125,5:125,392,125,6:125,222,125,7

The SI dimension corresponds to Y here. So we have:
935 → 3
311 → 4
603 → 5
392 → 6
222 → 7

Which is not possible anatomically speaking.

–> @jcohenadad Why you didn’t straight and resamples the data before the registration?
I ask this question as I tried to do so for FA using the same pipeline but didn’t work out.
I wonder whether or not to do straightening after registration ?

Arguments:

  • To not introduce interpolation error on data with “thick slices”,
  • it is computationally faster,
  • it makes the processing pipeline a bit easier to understand. Once you get more familiar with SCT you can experiment with more complex steps.

I would like at the end to crop the spinal segments for FA C2-C8 after registering to T1 template. Then average the segments from all samples to construct the FA template based on spinal levels. Please correct me if there is any confusion or need further clarification.

You don’t need to physically crop the image to compute the average FA in specific segments. Once the template is warped, you can simply specify where you wish to extract the values. Example below, following on the script I wrote here Registration to T1 ex-vivo Template ERROR - #14 by ihattan :

# Compute DTI
sct_dmri_compute_dti -i /Users/julien/temp/20210313-ibrahim/dwi_1500_orient.nii.gz -bvec /Users/julien/temp/20210313-ibrahim/bvec_onl_conc.txt -bval bval.txt
# Extract average FA in the white matter at each spinal level between C3 and C7
sct_extract_metric -i dti_FA.nii.gz -f label/template/exvivo_wm.nii.gz -method wa -vertfile label/template/exvivo_spinal_levels.nii.gz -perlevel 1 -vert 3:7 -o fa.csv

The average FA is under the column “WA()”:

Timestamp SCT Version Filename Slice (I->S) VertLevel Label Size [vox] WA() STD()
2021-04-10 11:15:24 git-jca/3336-display-warp-7de29b49e9979eb22409e8a11eb2c79eb0277281 /Users/julien/temp/20210313-ibrahim/dti_FA.nii.gz 0:63 7 exvivo_wm 113383.58568826600 0.5866994886132520 0.1840241908106680
2021-04-10 11:15:24 git-jca/3336-display-warp-7de29b49e9979eb22409e8a11eb2c79eb0277281 /Users/julien/temp/20210313-ibrahim/dti_FA.nii.gz 64:157 6 exvivo_wm 211695.98038703100 0.5612822364753440 0.19923681548372500
2021-04-10 11:15:24 git-jca/3336-display-warp-7de29b49e9979eb22409e8a11eb2c79eb0277281 /Users/julien/temp/20210313-ibrahim/dti_FA.nii.gz 158:264 5 exvivo_wm 244358.1573564030 0.562023760877192 0.2003186381866740
2021-04-10 11:15:24 git-jca/3336-display-warp-7de29b49e9979eb22409e8a11eb2c79eb0277281 /Users/julien/temp/20210313-ibrahim/dti_FA.nii.gz 265:365 4 exvivo_wm 218316.16670902600 0.5928362786639040 0.20286217920780000
2021-04-10 11:15:24 git-jca/3336-display-warp-7de29b49e9979eb22409e8a11eb2c79eb0277281 /Users/julien/temp/20210313-ibrahim/dti_FA.nii.gz 366:448 3 exvivo_wm 167607.93572434900 0.5938122884592860 0.19572839953170500

Here is the output CSV: fa.csv (1.1 KB)

:information_source: ‎ Given that you did not send me the bval file, I created a dummy one: bval.txt (73 Bytes)

1 Like

As Julien said, there are obviously errors about the labels’ coordinates. What subject ID are we playing with here? I can double-check.

It was on the raw data (not the straightened). However, if I remember well, the data I used and your data (ie the DWI) do NOT have the same resolution! So you can’t directly use the labels’ coordinates I sent you on your DWI data. There are different workarounds for this: one I suggested was to resample the data (either “DWI_resolution to T1_resolution” or “T1_resolution to DWI_resolution”) in order to have the labels and your DWI data in the same resolution / space.