File: elxConfiguration.h

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 (326 lines) | stat: -rw-r--r-- 10,625 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*=========================================================================
 *
 *  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 elxConfiguration_h
#define elxConfiguration_h

#include "itkObject.h"
#include "elxBaseComponent.h"

#include "itkParameterFileParser.h"
#include "itkParameterMapInterface.h"
#include <map>
#include "elxlog.h"

namespace elastix
{

/**
 * \class Configuration
 * \brief A class that deals with user given parameters and command line arguments.
 *
 * The Configuration class provides the functions
 * ReadParameter() (to read parameters from the parameter file) and
 * ReadCommandLineArgument(), and provides an easy way to get the
 * current elastix level.
 *
 * \parameter PrintErrorMessages: defines if warnings and errors should be
 * printed to screen, e.g. when a parameter cannot be found and the default
 * is used.
 * example: <tt>(PrintErrorMessages "false")</tt>\n
 * Default: "true"
 *
 * \ingroup Configuration
 */

class Configuration
  : public itk::Object
  , public BaseComponent
{
public:
  ITK_DISALLOW_COPY_AND_MOVE(Configuration);

  /** Standard itk.*/
  using Self = Configuration;
  using Superclass1 = itk::Object;
  using Superclass2 = BaseComponent;
  using Pointer = itk::SmartPointer<Self>;
  using ConstPointer = itk::SmartPointer<const Self>;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** Standard part of all itk objects. */
  itkTypeMacro(Configuration, itk::Object);

  /** Typedefs for the command line arguments. */
  using CommandLineArgumentMapType = std::map<std::string, std::string>;
  using CommandLineEntryType = CommandLineArgumentMapType::value_type;

  /** Get and Set CommandLine arguments into the argument map. */
  std::string
  GetCommandLineArgument(const std::string & key) const;

  void
  SetCommandLineArgument(const std::string & key, const std::string & value);

  /** Get/Set the name of the parameterFileName. */
  itkGetStringMacro(ParameterFileName);
  itkSetStringMacro(ParameterFileName);

  /** Pass the command line arguments as a map.
   * One of the command line arguments should be either:
   * -p \<parameter_file\>         or
   * -tp \<transform_parameter_file\>.
   * The specified (transform) parameter file is read by the
   * itk::ParameterFileParser and passed to the itk::ParameterMapInterface.
   */
  int
  Initialize(const CommandLineArgumentMapType & _arg);

  int
  Initialize(const CommandLineArgumentMapType & _arg, const itk::ParameterFileParser::ParameterMapType & inputMap);

  /** True, if Initialize was successfully called. */
  bool
  IsInitialized() const; // to elxconfigurationbase

  /** Other elastix related information. */

  /** Get and Set the elastix level. */
  itkSetMacro(ElastixLevel, unsigned int);
  itkGetConstMacro(ElastixLevel, unsigned int);

  /** Get and Set the total number of elastix levels. */
  itkSetMacro(TotalNumberOfElastixLevels, unsigned int);
  itkGetConstMacro(TotalNumberOfElastixLevels, unsigned int);

  /***/
  bool
  GetPrintErrorMessages() const
  {
    return m_ParameterMapInterface->GetPrintErrorMessages();
  }


  /** Set/Get whether warnings are allowed to be printed, when reading a parameter. *
  itkSetMacro( Silent, bool );
  itkGetConstMacro( Silent, bool );
  */

  /** Methods that is called at the very beginning of elastixTemplate::Run.
   * \li Prints the parameter file
   */
  int
  BeforeAll() override;

  /** Methods that is called at the very beginning of elastixTemplate::ApplyTransform.
   * \li Prints the parameter file
   */
  int
  BeforeAllTransformix();

  /** Interface to the ParameterMapInterface. */

  /** Count the number of parameters. */
  std::size_t
  CountNumberOfParameterEntries(const std::string & parameterName) const
  {
    return m_ParameterMapInterface->CountNumberOfParameterEntries(parameterName);
  }


  /** Read a parameter from the parameter file. */
  template <class T>
  bool
  ReadParameter(T &                 parameterValue,
                const std::string & parameterName,
                const unsigned int  entry_nr,
                const bool          produceWarningMessage) const
  {
    std::string warningMessage = "";
    bool        found = m_ParameterMapInterface->ReadParameter(
      parameterValue, parameterName, entry_nr, produceWarningMessage, warningMessage);
    if (!warningMessage.empty())
    {
      log::warn(warningMessage);
    }

    return found;
  }


  /** Read a parameter from the parameter file. */
  template <class T>
  bool
  ReadParameter(T & parameterValue, const std::string & parameterName, const unsigned int entry_nr) const
  {
    std::string warningMessage = "";
    bool        found = m_ParameterMapInterface->ReadParameter(parameterValue, parameterName, entry_nr, warningMessage);
    if (!warningMessage.empty())
    {
      log::warn(warningMessage);
    }

    return found;
  }


  /** Read a parameter from the parameter file. */
  template <class T>
  bool
  ReadParameter(T &                 parameterValue,
                const std::string & parameterName,
                const std::string & prefix,
                const unsigned int  entry_nr,
                const int           default_entry_nr,
                const bool          produceWarningMessage) const
  {
    std::string warningMessage = "";
    bool        found = m_ParameterMapInterface->ReadParameter(
      parameterValue, parameterName, prefix, entry_nr, default_entry_nr, produceWarningMessage, warningMessage);
    if (!warningMessage.empty())
    {
      log::warn(warningMessage);
    }

    return found;
  }


  /** Read a parameter from the parameter file. */
  template <class T>
  bool
  ReadParameter(T &                 parameterValue,
                const std::string & parameterName,
                const std::string & prefix,
                const unsigned int  entry_nr,
                const int           default_entry_nr) const
  {
    std::string warningMessage = "";
    bool        found = m_ParameterMapInterface->ReadParameter(
      parameterValue, parameterName, prefix, entry_nr, default_entry_nr, warningMessage);
    if (!warningMessage.empty())
    {
      log::warn(warningMessage);
    }

    return found;
  }


  /** Tells whether its parameter map has the parameter with the given name. */
  bool
  HasParameter(const std::string & parameterName) const
  {
    return m_ParameterMapInterface->HasParameter(parameterName);
  }


  /** Returns the values of the specified parameter (from the parameter file). */
  std::vector<std::string>
  GetValuesOfParameter(const std::string & parameterName) const
  {
    return m_ParameterMapInterface->GetValues(parameterName);
  }


  /** Retrieves the values of the specified parameter (from the parameter file).
   * Returns null when the parameter map does not contain the specified
   * parameter. Throws an exception when it fails to convert each of the
   * parameter values to the specified type `T`.
   */
  template <typename T>
  std::unique_ptr<std::vector<T>>
  RetrieveValuesOfParameter(const std::string & parameterName) const
  {
    return m_ParameterMapInterface->RetrieveValues<T>(parameterName);
  }

  /** Retrieves the value of the specified parameter (from the parameter file). Returns the specified default parameter
   * value if the parameter cannot be found. */
  template <typename T>
  T
  RetrieveParameterValue(const T &           defaultParameterValue,
                         const std::string & parameterName,
                         const unsigned int  entry_nr,
                         const bool          produceWarningMessage) const
  {
    auto parameterValue = defaultParameterValue;
    (void)Self::ReadParameter<T>(parameterValue, parameterName, entry_nr, produceWarningMessage);
    return parameterValue;
  }


  /** Retrieves the string value of the specified parameter (from the parameter file). Returns the specified default
   * parameter value if the parameter cannot be found. */
  std::string
  RetrieveParameterStringValue(const std::string & defaultParameterValue,
                               const std::string & parameterName,
                               const unsigned int  entry_nr,
                               const bool          produceWarningMessage) const
  {
    return Self::RetrieveParameterValue(defaultParameterValue, parameterName, entry_nr, produceWarningMessage);
  }


  /** Read a range of parameters from the parameter file. */
  template <class T>
  bool
  ReadParameter(std::vector<T> &    parameterValues,
                const std::string & parameterName,
                const unsigned int  entry_nr_start,
                const unsigned int  entry_nr_end,
                const bool          produceWarningMessage) const
  {
    std::string warningMessage = "";
    bool        found = m_ParameterMapInterface->ReadParameter(
      parameterValues, parameterName, entry_nr_start, entry_nr_end, produceWarningMessage, warningMessage);
    if (!warningMessage.empty())
    {
      log::warn(warningMessage);
    }

    return found;
  }


protected:
  Configuration();
  ~Configuration() override = default;

  /** Print the parameter file to the log file. Called by BeforeAll().
   * This function is not really generic. It's just added because it needs to be
   * called by both BeforeAll and BeforeAllTransformix.
   */
  void
  PrintParameterFile() const;

private:
  CommandLineArgumentMapType                m_CommandLineArgumentMap{};
  std::string                               m_ParameterFileName{};
  const itk::ParameterFileParser::Pointer   m_ParameterFileParser{ itk::ParameterFileParser::New() };
  const itk::ParameterMapInterface::Pointer m_ParameterMapInterface{ itk::ParameterMapInterface::New() };

  bool         m_IsInitialized{ false };
  unsigned int m_ElastixLevel{ 0 };
  unsigned int m_TotalNumberOfElastixLevels{ 1 };
};

} // end namespace elastix

#endif // end #ifndef elxConfiguration_h