File: elxOptimizerBase.hxx

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (219 lines) | stat: -rw-r--r-- 6,308 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef elxOptimizerBase_hxx
#define elxOptimizerBase_hxx

#include "elxOptimizerBase.h"
#include <itkDeref.h>

#include "itkSingleValuedNonLinearOptimizer.h"
#include "itk_zlib.h"
#include <cmath> // For round.

namespace elastix
{

/**
 * ****************** SetCurrentPositionPublic ************************
 */

template <class TElastix>
void
OptimizerBase<TElastix>::SetCurrentPositionPublic(const ParametersType & /** param */)
{
  log::error(std::ostringstream{}
             << "ERROR: This function should be overridden or just not used.\n"
             << "  Are you using BSplineTransformWithDiffusion in combination with another optimizer than the "
                "StandardGradientDescentOptimizer? Don't!");

  /** Throw an exception if this function is not overridden. */
  itkExceptionMacro("ERROR: The SetCurrentPositionPublic method is not implemented in your optimizer");

} // end SetCurrentPositionPublic()


/**
 * ****************** BeforeEachResolutionBase **********************
 */

template <class TElastix>
void
OptimizerBase<TElastix>::BeforeEachResolutionBase()
{
  /** Get the current resolution level. */
  unsigned int level = this->GetRegistration()->GetAsITKBaseType()->GetCurrentLevel();

  const Configuration & configuration = itk::Deref(Superclass::GetConfiguration());

  /** Check if after every iteration a new sample set should be created. */
  this->m_NewSamplesEveryIteration = false;
  configuration.ReadParameter(
    this->m_NewSamplesEveryIteration, "NewSamplesEveryIteration", this->GetComponentLabel(), level, 0);

} // end BeforeEachResolutionBase()


/**
 * ****************** AfterRegistrationBase **********************
 */

template <class TElastix>
void
OptimizerBase<TElastix>::AfterRegistrationBase()
{
  using ParametersValueType = typename ParametersType::ValueType;

  /** Get the final parameters, round to six decimals. */
  ParametersType      finalTP = this->GetAsITKBaseType()->GetCurrentPosition();
  const unsigned long N = finalTP.GetSize();
  ParametersType      roundedTP(N);
  for (unsigned int i = 0; i < N; ++i)
  {
    roundedTP[i] = std::round(finalTP[i] * 1.0e6);
  }

  /** Compute the crc checksum using zlib crc32 function. */
  const unsigned char * crcInputData = reinterpret_cast<const unsigned char *>(roundedTP.data_block());
  uLong                 crc = crc32(0L, Z_NULL, 0);
  crc = crc32(crc, crcInputData, N * sizeof(ParametersValueType));

  log::info(std::ostringstream{} << "\nRegistration result checksum: " << crc);

} // end AfterRegistrationBase()


/**
 * ****************** SelectNewSamples ****************************
 */

template <class TElastix>
void
OptimizerBase<TElastix>::SelectNewSamples()
{
  /** Force the metric to base its computation on a new subset of image samples.
   * Not every metric may have implemented this.
   */
  for (unsigned int i = 0; i < this->GetElastix()->GetNumberOfMetrics(); ++i)
  {
    this->GetElastix()->GetElxMetricBase(i)->SelectNewSamples();
  }

} // end SelectNewSamples()


/**
 * ****************** GetNewSamplesEveryIteration ********************
 */

template <class TElastix>
bool
OptimizerBase<TElastix>::GetNewSamplesEveryIteration() const
{
  /** itkGetConstMacro Without the itkDebugMacro. */
  return this->m_NewSamplesEveryIteration;

} // end GetNewSamplesEveryIteration()


/**
 * **************** PrintSettingsVector **********************
 */

template <class TElastix>
void
OptimizerBase<TElastix>::PrintSettingsVector(const SettingsVectorType & settings)
{
  const unsigned long nrofres = settings.size();

  std::ostringstream outputStringStream;
  outputStringStream << std::showpoint << std::fixed;

  /** Print to log file */
  outputStringStream << "( SP_a ";
  for (unsigned int i = 0; i < nrofres; ++i)
  {
    outputStringStream << settings[i].a << " ";
  }
  outputStringStream << ")\n"

                     << "( SP_A ";
  for (unsigned int i = 0; i < nrofres; ++i)
  {
    outputStringStream << settings[i].A << " ";
  }
  outputStringStream << ")\n"

                     << "( SP_alpha ";
  for (unsigned int i = 0; i < nrofres; ++i)
  {
    outputStringStream << settings[i].alpha << " ";
  }
  outputStringStream << ")\n"

                     << "( SigmoidMax ";
  for (unsigned int i = 0; i < nrofres; ++i)
  {
    outputStringStream << settings[i].fmax << " ";
  }
  outputStringStream << ")\n"

                     << "( SigmoidMin ";
  for (unsigned int i = 0; i < nrofres; ++i)
  {
    outputStringStream << settings[i].fmin << " ";
  }
  outputStringStream << ")\n"

                     << "( SigmoidScale ";
  for (unsigned int i = 0; i < nrofres; ++i)
  {
    outputStringStream << settings[i].omega << " ";
  }
  outputStringStream << ")\n";

  log::info(outputStringStream.str());

} // end PrintSettingsVector()


/**
 * ****************** SetSinusScales ********************
 */

template <class TElastix>
void
OptimizerBase<TElastix>::SetSinusScales(double amplitude, double frequency, unsigned long numberOfParameters)
{
  using ScalesType = typename ITKBaseType::ScalesType;

  const double nrofpar = static_cast<double>(numberOfParameters);
  ScalesType   scales(numberOfParameters);

  for (unsigned long i = 0; i < numberOfParameters; ++i)
  {
    const double x = static_cast<double>(i) / nrofpar * 2.0 * vnl_math::pi * frequency;
    scales[i] = std::pow(amplitude, std::sin(x));
  }
  this->GetAsITKBaseType()->SetScales(scales);

} // end SetSinusScales()


} // end namespace elastix

#endif // end #ifndef elxOptimizerBase_hxx