ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
608 stars 161 forks source link

B-spline SyN registration has too few control points #518

Closed dipterix closed 3 months ago

dipterix commented 8 months ago

Describe the bug

This error does not display every time, but every now and then it errors out on some images...

Trying to run ants.registration with type of transformation antsRegistrationSyN[b] and encountered error:

Stage 2
  iterations = 100x70x50x20
  convergence threshold = 1.0000e-06
  convergence window size = 10
  number of levels = 4
  using the CC metric (radius = 2, weight = 1.0000e+00, use gradient filter = 0)
  Shrink factors (level 1 out of 4): [8, 8, 8]
  Shrink factors (level 2 out of 4): [4, 4, 4]
  Shrink factors (level 3 out of 4): [2, 2, 2]
  Shrink factors (level 4 out of 4): [1, 1, 1]
  smoothing sigmas per level: [3, 2, 1, 0]
  Using default NONE metricSamplingStrategy 
*** Running B-spline SyN registration (updateMeshSizeAtBaseLevel = [8, 9, 8], totalMeshSizeAtBaseLevel = [4294967295, 4294967295, 4294967295]) ***
Exception caught: 
itk::ExceptionObject (0x112e8d420)
Location: "unknown" 
File: /Users/stnava/code/ANTsPy/itksource/Modules/Filtering/ImageGrid/include/itkBSplineScatteredDataPointSetToImageFilter.hxx
Line: 223
Description: ITK ERROR: BSplineScatteredDataPointSetToImageFilter(0x112ed0ae0): The number of control points must be greater than the spline order.
antsRegistration -d 3 -r [0x152f4fb80,0x142e5e4e0,1] -o [/Users/dipterix/rave_data/raw_dir/UPennOCD3_new/rave-imaging/template_mapping/mni_icbm152_nlin_asym_09a/mni_icbm152_nlin_asym_09a_deformable_,0x29b6f3040,0x29aed55b0] --transform Rigid[0.1] --metric MI[0x152f4fb80,0x142e5e4e0,1,32,Regular,0.25] --convergence [1000x500x250x100,1e-6,10] --shrink-factors 8x4x2x1 --smoothing-sigmas 3x2x1x0vox --transform Affine[0.1] --metric MI[0x152f4fb80,0x142e5e4e0,1,32,Regular,0.25] --convergence [1000x500x250x100,1e-6,10] --shrink-factors 8x4x2x1 --smoothing-sigmas 3x2x1x0vox --transform BSplineSyN[0.1,26,0,3] --metric CC[0x152f4fb80,0x142e5e4e0,1,2] --convergence [100x70x50x20,1e-6,10] --shrink-factors 8x4x2x1 --smoothing-sigmas 3x2x1x0vox -x [0x29ae62c10,NA] --float 1 --write-composite-transform 0 -v 1

Code:

ants.registration(
    fixed = fix_img,
    moving = mov_img,
    mask = mask_img,
    outprefix = outprefix2,
    type_of_transform = "antsRegistrationSyN[b]",
    aff_metric = "CC",
    syn_metric = "CC",
    write_composite_transform = False,
    aff_sampling = 2,
    syn_sampling = 2,
    verbose = True
  )
ntustison commented 8 months ago

I'm guessing this is some system-specific issue. Notice that it's calling the correct command line option with --transform BSplineSyN[0.1,26,0,3] but somehow that 0 in the third parameter is getting translating incorrectly to totalMeshSizeAtBaseLevel = [4294967295, 4294967295, 4294967295].

dipterix commented 8 months ago

4294967295 is $2^{32}-1$. I guess there might be some integer issues...

ntustison commented 8 months ago

Exactly. And considering it's calling the underlying C++ program correctly and I have never produced that, it would be hard for me to debug.

cookpa commented 8 months ago

I've never seen that either but I'll give it a go if given a reproducible example

dipterix commented 8 months ago

What should be totalMeshSizeAtBaseLevel in this case?

ntustison commented 8 months ago

It should be [0,0,0]. That parameter value is by far the most commonly used which is why I'm surprised you're getting it on your machine.

ntustison commented 3 months ago

Closing due to inactivity.