Skip to content

Commit 635c7d7

Browse files
authored
Merge pull request #192 from tbirdso/exhaustive-optimizer
ENH: Add ExhaustiveOptimizerv4 Python example
2 parents c6dd70e + 9c4713d commit 635c7d7

File tree

8 files changed

+129
-7
lines changed

8 files changed

+129
-7
lines changed

.github/workflows/build-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ jobs:
157157
run: |
158158
python -m pip install --upgrade pip
159159
python -m pip install ninja
160-
python -m pip install itk==5.2rc1
160+
python -m pip install itk==5.2rc02
161161
162162
- name: Get specific version of CMake, Ninja
163163
uses: lukka/get-cmake@v3.18.3
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
add_example(AmoebaOptimizer)
22
add_example(LevenbergMarquardtOptimization)
3-
4-
#TODO: Example needs valid input
5-
#add_example(ExhaustiveOptimizer)
3+
add_example(ExhaustiveOptimizer)

src/Numerics/Optimizers/ExhaustiveOptimizer/CMakeLists.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@ install(TARGETS ExhaustiveOptimizer
1414
COMPONENT Runtime
1515
)
1616

17-
install(FILES Code.cxx CMakeLists.txt
17+
install(FILES Code.cxx Code.py CMakeLists.txt
1818
DESTINATION share/ITKExamples/Code/Numerics/Optimizers/ExhaustiveOptimizer/
1919
COMPONENT Code
2020
)
2121

2222

2323
enable_testing()
24-
add_test(NAME ExhaustiveOptimizerTest
25-
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ExhaustiveOptimizer)
2624

25+
set(fixed_image ${CMAKE_CURRENT_BINARY_DIR}/orange.jpg)
26+
set(moving_image ${CMAKE_CURRENT_BINARY_DIR}/apple.jpg)
27+
28+
add_test(NAME ExhaustiveOptimizerTest
29+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ExhaustiveOptimizer
30+
${fixed_image}
31+
${moving_image})
32+
33+
if(ITK_WRAP_PYTHON)
34+
add_test(NAME ExhaustiveOptimizerTestPython
35+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
36+
${fixed_image}
37+
${moving_image})
38+
endif()
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python
2+
#=========================================================================
3+
#
4+
# Copyright NumFOCUS
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0.txt
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
#
19+
20+
# Python example demonstrating itk.ExhaustiveOptimizer usage
21+
import sys
22+
from math import pi
23+
24+
import itk
25+
26+
EXIT_SUCCESS = 0
27+
EXIT_FAILURE = 1
28+
29+
def main(argv):
30+
if len(argv) < 3:
31+
raise Exception(f'Usage: {argv[0]} fixed_image moving_image')
32+
33+
fixed_image = itk.imread(argv[1], itk.F)
34+
moving_image = itk.imread(argv[2],itk.F)
35+
36+
dimension = fixed_image.GetImageDimension()
37+
FixedImageType = type(fixed_image)
38+
MovingImageType = type(moving_image)
39+
40+
if dimension == 2:
41+
transform = itk.Euler2DTransform[itk.D].New()
42+
else:
43+
raise Exception(f'Unsupported dimension: {dimension}')
44+
45+
TransformInitializerType = \
46+
itk.CenteredTransformInitializer[itk.MatrixOffsetTransformBase[itk.D,
47+
dimension,
48+
dimension],
49+
FixedImageType, MovingImageType]
50+
initializer = TransformInitializerType.New(Transform=transform,
51+
FixedImage=fixed_image,
52+
MovingImage=moving_image,)
53+
initializer.InitializeTransform()
54+
55+
metric = itk.MeanSquaresImageToImageMetricv4[FixedImageType, MovingImageType].New()
56+
57+
optimizer = itk.ExhaustiveOptimizerv4[itk.D].New()
58+
def print_iteration():
59+
print(f'Iteration:'
60+
f'{optimizer.GetCurrentIteration()}\t'
61+
f'{list(optimizer.GetCurrentIndex())} \t'
62+
f'{optimizer.GetCurrentValue():10.4f}\t'
63+
f'{list(optimizer.GetCurrentPosition())}\t')
64+
65+
optimizer.AddObserver(itk.IterationEvent(), print_iteration)
66+
67+
angles = 12
68+
optimizer.SetNumberOfSteps([int(angles / 2),0,0])
69+
70+
# Initialize scales and set back to optimizer
71+
scales = optimizer.GetScales()
72+
scales.SetSize(3)
73+
scales.SetElement(0, 2.0 * pi / angles)
74+
scales.SetElement(1, 1.0)
75+
scales.SetElement(2, 1.0)
76+
optimizer.SetScales(scales)
77+
78+
RegistrationType = itk.ImageRegistrationMethodv4[FixedImageType,MovingImageType]
79+
registration = RegistrationType.New(Metric=metric,
80+
Optimizer=optimizer,
81+
FixedImage=fixed_image,
82+
MovingImage=moving_image,
83+
InitialTransform=transform,
84+
NumberOfLevels=1)
85+
86+
try:
87+
registration.Update()
88+
89+
print(f'MinimumMetricValue: {optimizer.GetMinimumMetricValue():.4f}\t'
90+
f'MaximumMetricValue: {optimizer.GetMaximumMetricValue():.4f}\n'
91+
f'MinimumMetricValuePosition: {list(optimizer.GetMinimumMetricValuePosition())}\t'
92+
f'MaximumMetricValuePosition: {list(optimizer.GetMaximumMetricValuePosition())}\n'
93+
f'StopConditionDescription: {optimizer.GetStopConditionDescription()}\t')
94+
95+
except Exception as e:
96+
print(f'Exception caught: {e}')
97+
return EXIT_FAILURE
98+
99+
return EXIT_SUCCESS
100+
101+
if __name__ == "__main__":
102+
main(sys.argv)

src/Numerics/Optimizers/ExhaustiveOptimizer/Documentation.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ C++
2727
.. literalinclude:: Code.cxx
2828
:lines: 18-
2929

30+
Python
31+
...
32+
33+
.. literalinclude:: Code.py
34+
:lines: 20-
35+
36+
3037
Classes demonstrated
3138
--------------------
3239

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8dd0115569bc6dec91e5164ddb44366dbed265d130a5ad6cb68c9c2e7d8339920871f20f3df2865501890b0b8237c0292dd66a6ec6d450ed25bd17257a745985
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d3dca735afb71e29461854bc471e8c3b8c03b5ba6abf68e9fb8467bf7c42f16d915015cbad21e8b78aa66b4b8ab7675a2a6f64e6d710e66832e730c6d4d0c1d8
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
67ead1cd2c867ff6d2c58b014ea9d0af3d83f2b08d0762740f4104fe7518528cd5b6313ed228a8c896f48b77c61bd5c19fddec5112d3ff0916dab8b4ec4955a4

0 commit comments

Comments
 (0)