|
a |
|
b/ants/registration/simulate_displacement_field.py |
|
|
1 |
__all__ = ["simulate_displacement_field"] |
|
|
2 |
|
|
|
3 |
import numpy as np |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
import ants |
|
|
7 |
from ants.internal import get_lib_fn |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
def simulate_displacement_field(domain_image, |
|
|
12 |
field_type="bspline", |
|
|
13 |
number_of_random_points=1000, |
|
|
14 |
sd_noise=10.0, |
|
|
15 |
enforce_stationary_boundary=True, |
|
|
16 |
number_of_fitting_levels=4, |
|
|
17 |
mesh_size=1, |
|
|
18 |
sd_smoothing=4.0): |
|
|
19 |
""" |
|
|
20 |
simulate displacement field using either b-spline or exponential transform |
|
|
21 |
|
|
|
22 |
ANTsR function: `simulateDisplacementField` |
|
|
23 |
|
|
|
24 |
Arguments |
|
|
25 |
--------- |
|
|
26 |
domain_image : ANTsImage |
|
|
27 |
Domain image |
|
|
28 |
|
|
|
29 |
field_type : string |
|
|
30 |
Either "bspline" or "exponential". |
|
|
31 |
|
|
|
32 |
number_of_random_points : integer |
|
|
33 |
Number of displacement points. |
|
|
34 |
|
|
|
35 |
sd_noise : float |
|
|
36 |
Standard deviation of the displacement field noise. |
|
|
37 |
|
|
|
38 |
enforce_stationary_boundary : boolean |
|
|
39 |
Determines fixed boundary conditions. |
|
|
40 |
|
|
|
41 |
number_of_fitting_levels : integer |
|
|
42 |
Number of fitting levels (b-spline only). |
|
|
43 |
|
|
|
44 |
mesh_size : integer or n-D tuple |
|
|
45 |
Determines fitting resolution at base level (b-spline only). |
|
|
46 |
|
|
|
47 |
sd_smoothing : float |
|
|
48 |
Standard deviation of the Gaussian smoothing in mm (exponential only). |
|
|
49 |
|
|
|
50 |
Returns |
|
|
51 |
------- |
|
|
52 |
ANTs vector image. |
|
|
53 |
|
|
|
54 |
Example |
|
|
55 |
------- |
|
|
56 |
>>> import ants |
|
|
57 |
>>> domain = ants.image_read( ants.get_ants_data('r16')) |
|
|
58 |
>>> exp_field = ants.simulate_displacement_field(domain, field_type="exponential") |
|
|
59 |
>>> bsp_field = ants.simulate_displacement_field(domain, field_type="bspline") |
|
|
60 |
>>> bsp_xfrm = ants.transform_from_displacement_field(bsp_field * 3) |
|
|
61 |
>>> domain_warped = ants.apply_ants_transform_to_image(bsp_xfrm, domain, domain) |
|
|
62 |
""" |
|
|
63 |
|
|
|
64 |
image_dimension = domain_image.dimension |
|
|
65 |
|
|
|
66 |
if field_type == 'bspline': |
|
|
67 |
if isinstance(mesh_size, int) == False and len(mesh_size) != image_dimension: |
|
|
68 |
raise ValueError("Incorrect specification for mesh_size.") |
|
|
69 |
|
|
|
70 |
spline_order = 3 |
|
|
71 |
number_of_control_points = mesh_size + spline_order |
|
|
72 |
|
|
|
73 |
if isinstance(number_of_control_points, int) == True: |
|
|
74 |
number_of_control_points = np.repeat(number_of_control_points, image_dimension) |
|
|
75 |
|
|
|
76 |
libfn = get_lib_fn("simulateBsplineDisplacementField%iD" % image_dimension) |
|
|
77 |
field = libfn(domain_image.pointer, number_of_random_points, sd_noise, |
|
|
78 |
enforce_stationary_boundary, number_of_fitting_levels, number_of_control_points) |
|
|
79 |
bspline_field = ants.from_pointer(field).clone('float') |
|
|
80 |
return bspline_field |
|
|
81 |
|
|
|
82 |
elif field_type == 'exponential': |
|
|
83 |
libfn = get_lib_fn("simulateExponentialDisplacementField%iD" % image_dimension) |
|
|
84 |
field = libfn(domain_image.pointer, number_of_random_points, sd_noise, |
|
|
85 |
enforce_stationary_boundary, sd_smoothing) |
|
|
86 |
exp_field = ants.from_pointer(field).clone('float') |
|
|
87 |
return exp_field |
|
|
88 |
|
|
|
89 |
else: |
|
|
90 |
raise ValueError("Unrecognized field type.") |