File: itkSubsampleTest2.cxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (334 lines) | stat: -rw-r--r-- 9,354 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
327
328
329
330
331
332
333
334
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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.
 *
 *=========================================================================*/

#include "itkListSample.h"
#include "itkSubsample.h"

int
itkSubsampleTest2(int, char *[])
{
  std::cout << "Subsample Test \n \n";

  using MeasurementVectorType = itk::Array<float>;

  using SampleType = itk::Statistics::ListSample<MeasurementVectorType>;

  SampleType::MeasurementVectorSizeType measurementVectorSize = 3;
  std::cerr << "Measurement vector size: " << measurementVectorSize << std::endl;

  unsigned int sampleSize = 10;

  auto sample = SampleType::New();

  sample->SetMeasurementVectorSize(measurementVectorSize);

  MeasurementVectorType mv(measurementVectorSize);
  for (unsigned int i = 0; i < sampleSize; ++i)
  {
    for (unsigned int j = 0; j < measurementVectorSize; ++j)
    {
      mv[j] = j + i * measurementVectorSize;
    }
    std::cout << "Adding measurement vector: " << mv << std::endl;
    sample->PushBack(mv);
  }

  // tests begin
  using SubsampleType = itk::Statistics::Subsample<SampleType>;
  auto subSample = SubsampleType::New();
  subSample->SetSample(sample);

  // Add measurement vectors in sample with even id number to subSample
  for (unsigned int i = 0; i < sample->Size(); i = i + 2)
  {
    subSample->AddInstance(i);
    std::cout << "Adding instance: " << i << " to subSample" << std::endl;
  }

  if (subSample->Size() != 5)
  {
    std::cerr << "Size of the subsample container should be 5" << std::endl;
    return EXIT_FAILURE;
  }

  for (unsigned int i = 0; i < subSample->Size(); ++i)
  {
    std::cout << "Measurement Vector: " << i << '\t' << subSample->GetMeasurementVector(i) << std::endl;

    if (subSample->GetMeasurementVector(i) != sample->GetMeasurementVector(i * 2))
    {
      std::cerr << "Subsampling is not correctly done!" << std::endl;
      return EXIT_FAILURE;
    }
  }

  using CascadedSubsampleType = itk::Statistics::Subsample<SubsampleType>;
  auto subSample2 = CascadedSubsampleType::New();
  subSample2->SetSample(subSample);

  // Add measurement vectors in subsample with even id number to subSample2
  for (unsigned int i = 0; i < subSample->Size(); i = i + 2)
  {
    std::cout << "Adding instance: " << i << " to subSample2" << std::endl;
    subSample2->AddInstance(i);
  }

  if (subSample2->Size() != 3)
  {
    std::cerr << "Size of the subsample2 container should be 3" << std::endl;
    return EXIT_FAILURE;
  }

  subSample2->Swap(0, 2);

  // swap back
  subSample2->Swap(2, 0);

  for (unsigned int i = 0; i < subSample2->Size(); ++i)
  {
    std::cout << "Measurement Vector: " << i << '\t' << subSample2->GetMeasurementVector(i) << std::endl;

    if (subSample2->GetMeasurementVector(i) != sample->GetMeasurementVector(i * 4))
    {
      std::cerr << "Subsampling of a subsample is not correctly done!" << std::endl;
      return EXIT_FAILURE;
    }
  }

  std::cout << subSample2->GetSample() << std::endl;

  using IteratorType = CascadedSubsampleType::Iterator;

  IteratorType iter = subSample2->Begin();
  while (iter != subSample2->End())
  {
    std::cout << iter.GetInstanceIdentifier() << ' ';
    std::cout << iter.GetMeasurementVector() << ' ';
    std::cout << iter.GetFrequency() << std::endl;
    ++iter;
  }

  using ConstIteratorType = CascadedSubsampleType::ConstIterator;

  IteratorType citer = subSample2->Begin();
  while (citer != subSample2->End())
  {
    std::cout << citer.GetInstanceIdentifier() << ' ';
    std::cout << citer.GetMeasurementVector() << ' ';
    std::cout << citer.GetFrequency() << std::endl;
    ++citer;
  }

  IteratorType iter1 = subSample2->Begin();
  IteratorType iter2 = subSample2->Begin();

  citer = iter1;

  if (citer != iter1)
  {
    std::cerr << "Error in iterator != operator " << std::endl;
    return EXIT_FAILURE;
  }


  if (!(citer == iter1))
  {
    std::cerr << "Error in iterator == operator " << std::endl;
    return EXIT_FAILURE;
  }


  if (iter1 != iter2)
  {
    std::cerr << "Error in iterator != operator " << std::endl;
    return EXIT_FAILURE;
  }


  if (!(iter1 == iter2))
  {
    std::cerr << "Error in iterator == operator " << std::endl;
    return EXIT_FAILURE;
  }


  //
  // Additional iterator tests
  //

  // Testing methods specific to Iterators
  {
    IteratorType iter7 = subSample2->Begin();
    IteratorType iter8 = subSample2->End();

    iter8 = iter7;
    if (iter8 != iter7)
    {
      std::cerr << "Iterator operator=() failed" << std::endl;
      return EXIT_FAILURE;
    }

    IteratorType iter3 = subSample2->Begin();
    if (iter3 != subSample2->Begin())
    {
      std::cerr << "Iterator constructor from sample failed" << std::endl;
      return EXIT_FAILURE;
    }

    unsigned int counter = 0;
    while (iter3 != subSample2->End())
    {
      ++iter3;
      counter++;
    }

    if (counter != subSample2->Size())
    {
      std::cerr << "Iterator walk failed" << std::endl;
      return EXIT_FAILURE;
    }

    IteratorType iter4(iter8);
    if (iter4 != iter8)
    {
      std::cerr << "Iterator copy constructor failed" << std::endl;
      return EXIT_FAILURE;
    }

    IteratorType iter5 = iter8;
    if (iter5 != iter8)
    {
      std::cerr << "Iterator operator= failed" << std::endl;
      return EXIT_FAILURE;
    }

    IteratorType iter6(subSample2);
    unsigned int targetEntry = 2;
    for (unsigned int kk = 0; kk < targetEntry; ++kk)
    {
      std::cout << "GetInstanceIdentifier() = " << iter6.GetInstanceIdentifier() << std::endl;
      ++iter6;
    }

    if (iter6.GetInstanceIdentifier() != targetEntry)
    {
      std::cerr << "Iterator Constructor with instance identifier failed" << std::endl;
      std::cerr << "Expected identifier = " << targetEntry << std::endl;
      std::cerr << "identifier returned = " << iter6.GetInstanceIdentifier() << std::endl;
      return EXIT_FAILURE;
    }
  }

  // Testing methods specific to ConstIterators
  {
    ConstIteratorType iter11 = subSample2->Begin();
    ConstIteratorType iter12 = subSample2->End();

    iter12 = iter11;

    if (iter12 != iter11)
    {
      std::cerr << "ConstIterator operator!=() or operator=() failed" << std::endl;
      return EXIT_FAILURE;
    }

    if (!(iter12 == iter11))
    {
      std::cerr << "ConstIterator operator==() failed" << std::endl;
      return EXIT_FAILURE;
    }

    ConstIteratorType iter3(iter12);
    if (iter3 != iter12)
    {
      std::cerr << "ConstIterator copy constructor failed" << std::endl;
      return EXIT_FAILURE;
    }

    const CascadedSubsampleType * constSample = subSample2.GetPointer();

    ConstIteratorType iter4(constSample->Begin());
    ConstIteratorType iter5(subSample2->Begin());
    if (iter4 != iter5)
    {
      std::cerr << "Constructor from const container Begin() differs from non-const Begin() " << std::endl;
      return EXIT_FAILURE;
    }

    ConstIteratorType iter6(constSample);
    ConstIteratorType iter7(subSample2);
    if (iter6 != iter7)
    {
      std::cerr << "ConstIterator Constructor from const container differs from non-const container" << std::endl;
      return EXIT_FAILURE;
    }

    ConstIteratorType iter8(subSample2);
    if (iter8.GetInstanceIdentifier() != 0)
    {
      std::cerr << "Constructor with instance identifier 0 failed" << std::endl;
      return EXIT_FAILURE;
    }


    ConstIteratorType iter9(subSample2);
    unsigned int      targetEntry = 2;
    for (unsigned int kk = 0; kk < targetEntry; ++kk)
    {
      std::cout << "Instance identifier = " << iter9.GetInstanceIdentifier() << std::endl;
      ++iter9;
    }

    std::cout << "Instance identifier = " << iter9.GetInstanceIdentifier() << std::endl;
    MeasurementVectorType vector9a = iter9.GetMeasurementVector();
    MeasurementVectorType vector9b = subSample2->GetMeasurementVector(targetEntry);
    for (unsigned int kitr = 0; kitr < measurementVectorSize; ++kitr)
    {
      if (itk::Math::abs(vector9b[kitr] - vector9a[kitr]))
      {
        std::cerr << "Constructor with container followed by increments failed" << std::endl;
        std::cerr << "Expected " << vector9b << std::endl;
        std::cerr << "Received " << vector9a << std::endl;
        return EXIT_FAILURE;
      }
    }

    unsigned int      counter = 0;
    ConstIteratorType iter10(constSample);
    if (iter10 != constSample->Begin())
    {
      std::cerr << "ConstIterator constructor from sample failed" << std::endl;
      return EXIT_FAILURE;
    }


    while (iter10 != constSample->End())
    {
      ++iter10;
      counter++;
    }

    if (counter != constSample->Size())
    {
      std::cerr << "Iterator walk failed" << std::endl;
      return EXIT_FAILURE;
    }
  }

  return EXIT_SUCCESS;
}