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
586 stars 161 forks source link

broken b-spline fitting #655

Closed ntustison closed 1 month ago

ntustison commented 1 month ago

@ncullen93 With the recent changes, the following example no longer works as the types seem to have gotten changed. Any ideas?

>>> import ants
>>> import numpy as np
>>> 
>>> output_size = (256, 256)
>>> bspline_epsilon = 1e-4
>>> number_of_fitting_levels = 4
>>> 
>>> image = ants.image_read(ants.get_ants_data("r16"))
>>> image = ants.resample_image(image, (100, 100), use_voxels=True)
>>> 
>>> indices = np.meshgrid(list(range(image.shape[0])), 
...                         list(range(image.shape[1])))
>>> indices_array = np.stack((indices[1].flatten(), 
...                           indices[0].flatten()), axis=0)
>>> 
>>> image_parametric_values = indices_array.transpose()
>>> 
>>> weight_array = np.ones(image.shape)
>>> parametric_values = image_parametric_values
>>> scattered_data = np.atleast_2d(image.numpy().flatten()).transpose()
>>> weight_values = np.atleast_2d(weight_array.flatten()).transpose()
>>> 
>>> min_parametric_values = np.min(parametric_values, axis=0)
>>> max_parametric_values = np.max(parametric_values, axis=0)
>>> 
>>> spacing = np.zeros((2,))
>>> for d in range(2):
...     spacing[d] = (max_parametric_values[d] - min_parametric_values[d]) / (output_size[d] - 1) + bspline_epsilon
... 
>>> bspline_image = ants.fit_bspline_object_to_scattered_data(scattered_data, parametric_values, 
...                                                         parametric_domain_origin=min_parametric_values - bspline_epsilon,
...                                                         parametric_domain_spacing=spacing,
...                                                         parametric_domain_size=output_size,
...                                                         data_weights=weight_values,
...                                                         number_of_fitting_levels=number_of_fitting_levels,
...                                                         mesh_size=1)   
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ntustison/anaconda3/envs/antsx/lib/python3.11/site-packages/ants/registration/fit_bspline_object_to_scattered_data.py", line 177, in fit_bspline_object_to_scattered_data
    bspline_object = libfn(scattered_data.tolist(), parametric_data.tolist(), data_weights.tolist(),
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: fitBsplineObjectToScatteredDataP2D1(): incompatible function arguments. The following argument types are supported:
    1. fitBsplineObjectToScatteredDataP2D1(arg0: collections.abc.Sequence[collections.abc.Sequence[float]], arg1: collections.abc.Sequence[collections.abc.Sequence[float]], arg2: collections.abc.Sequence[float], arg3: collections.abc.Sequence[float], arg4: collections.abc.Sequence[float], arg5: collections.abc.Sequence[int], arg6: collections.abc.Sequence[bool], arg7: int, arg8: collections.abc.Sequence[int], arg9: int, /) -> ants.lib.AntsImageF2

Invoked with types: list, list, list, ndarray, ndarray, tuple, list, int, list, int
ntustison commented 1 month ago

Okay, nm. Although there is a change and this example no longer works when it used to, it's due to a more restrictive input of the data_weights which is consistent with the help menu so I'm perfectly fine with this.

ncullen93 commented 1 month ago

I see.. the data_weights had an extra dim. I wish the error messages were more informative... I will work on that. Anyways I added a line to squeeze the data_weights just to make sure existing code doesnt break. So the example should work after the PR.