File: itkHistogramTest.cxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (851 lines) | stat: -rw-r--r-- 25,059 bytes parent folder | download | duplicates (4)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  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.
 *
 *=========================================================================*/

#include "itkMath.h"
#include "itkHistogram.h"
#include "itkSample.h"
#include "itkTestingMacros.h"

int itkHistogramTest( int, char* [] )
{
  std::cout << "Histogram Test \n \n";
  bool pass = true;
  std::string whereFail = "";

  typedef float MeasurementType;
  const unsigned int numberOfComponents = 3;

  // create a histogram with 3 components measurement vectors
  typedef itk::Statistics::Histogram< MeasurementType,
          itk::Statistics::DenseFrequencyContainer2 > HistogramType;
  HistogramType::Pointer histogram = HistogramType::New();

  EXERCISE_BASIC_OBJECT_METHODS( histogram, Histogram, Sample );

  typedef HistogramType::MeasurementVectorType MeasurementVectorType;
  typedef HistogramType::InstanceIdentifier    InstanceIdentifier;
  typedef HistogramType::IndexType             IndexType;

  // initializes a 64 x 64 x 64 histogram with equal size interval
  HistogramType::SizeType size( numberOfComponents );
  size.Fill(64);
  unsigned long totalSize = size[0] * size[1] * size[2];

  MeasurementVectorType lowerBound( numberOfComponents );
  MeasurementVectorType upperBound( numberOfComponents );

  lowerBound.Fill(0);
  upperBound.Fill(1024);

  //
  // Exercise exception case
  //
  try
    {
    // purposely calling Initialize() before calling SetMeasurementVectorSize()
    // in order to trigger an expected exception.
    histogram->Initialize(size);
    pass = false;
    whereFail = "Initialize(size) before SetMeasurementVectorSize() didn't throw expected exception";
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cout << "Expected exception ";
    std::cout << excp << std::endl;
    }


  //
  // Now call SetMeasurementVectorSize() correctly
  //
  histogram->SetMeasurementVectorSize( numberOfComponents );

  if( histogram->GetMeasurementVectorSize() != numberOfComponents )
    {
    std::cerr << "Error in Get/SetMeasurementVectorSize() " << std::endl;
    return EXIT_FAILURE;
    }

  //
  //  Exercise Initialize with size and bounds
  //
  histogram->Initialize(size, lowerBound, upperBound );

  histogram->SetToZero();

  double interval =
    (upperBound[0] - lowerBound[0]) /
    static_cast< HistogramType::MeasurementType >(size[0]);

  // tests begin
  MeasurementVectorType measurements( numberOfComponents );
  measurements.Fill(512);

  IndexType index( numberOfComponents );
  IndexType ind( numberOfComponents );
  index.Fill(32);
  if(histogram->GetIndex(measurements,ind))
    {
    if(index != ind)
      {
      pass = false;
      whereFail = "GetIndex(MeasurementVectorType&)";
      }
    }
  else
    {
    pass = false;
    whereFail = "GetIndex(MeasurementVectorType&)";
    }

  InstanceIdentifier id = histogram->GetInstanceIdentifier(index);
  if (index != histogram->GetIndex(id))
    {
    pass = false;
    whereFail = "GetIndex(InstanceIdentifier&)";
    }

  index.Fill( -5 ); // test for outside below

  if( !histogram->IsIndexOutOfBounds(index) )
    {
    std::cerr << "IsIndexOutOfBounds() for " << index << std::endl;
    pass = false;
    whereFail = "IsIndexOutOfBounds(IndexType)";
    }


  index.Fill(32); // test for inside

  if( histogram->IsIndexOutOfBounds(index) )
    {
    std::cerr << "IsIndexOutOfBounds() for " << index << std::endl;
    pass = false;
    whereFail = "IsIndexOutOfBounds(IndexType)";
    }

  index.Fill(100); // test for outside

  if( !histogram->IsIndexOutOfBounds(index) )
    {
    std::cerr << "IsIndexOutOfBounds() for " << index << std::endl;
    pass = false;
    whereFail = "IsIndexOutOfBounds(IndexType)";
    }

  if (totalSize != histogram->Size())
    {
    pass = false;
    whereFail = "Size()";
    }

  if (size != histogram->GetSize())
    {
    pass = false;
    whereFail = "GetSize()";
    }

  // Query the bounds of the bin using the index of the bin.

  if (itk::Math::NotAlmostEquals( (lowerBound[0] + interval * 31), histogram->GetBinMin(0,31) ))
    {
    pass = false;
    whereFail = "GetBinMin(Dimension, nthBin)";
    }

  if (itk::Math::NotAlmostEquals( (lowerBound[0] + interval * 32), histogram->GetBinMax(0,31) ))
    {
    pass = false;
    whereFail = "GetBinMax(Dimension, nthBin)";
    }

  // Query the histogram bin extremes using a value within the bin

  if (itk::Math::NotAlmostEquals( (lowerBound[0] + interval * 31         ), histogram->GetBinMinFromValue(0, lowerBound[0] + interval * 31.5 ) )
   || itk::Math::NotAlmostEquals( (lowerBound[0]                         ), histogram->GetBinMinFromValue(0, itk::NumericTraits< float >::min()   ) )
   || itk::Math::NotAlmostEquals( (lowerBound[0] + interval * (size[0]-1)), histogram->GetBinMinFromValue(0, itk::NumericTraits< float >::max() ) ) )
    {
    pass = false;
    whereFail = "GetBinMinFromValue(Dimension, A Value Within The Nth Bin)";
    }

  if (itk::Math::NotAlmostEquals( (lowerBound[0] + interval * 32         ), histogram->GetBinMaxFromValue(0, lowerBound[0] + interval * 31.5 ) )
   || itk::Math::NotAlmostEquals( (lowerBound[0] + interval              ), histogram->GetBinMaxFromValue(0, itk::NumericTraits< float >::min()   ) )
   || itk::Math::NotAlmostEquals( (upperBound[0]                         ), histogram->GetBinMaxFromValue(0, itk::NumericTraits< float >::max() ) ) )
    {
    pass = false;
    whereFail = "GetBinMaxFromValue(Dimension, A Value Within The Nth Bin)";
    }

  index.Fill(31);
  if (itk::Math::NotAlmostEquals( (lowerBound[0] + interval * 31), histogram->GetHistogramMinFromIndex(index)[0]) )
    {
    pass = false;
    whereFail = "GetHistogramMinFromIndex(Dimension, nthBin)";
    }

  if (itk::Math::NotAlmostEquals( (lowerBound[0] + interval * 32), histogram->GetHistogramMaxFromIndex(index)[0]) )
    {
    pass = false;
    whereFail = "GetHistogramMaxFromIndex(Dimension, nthBin)";
    }

  for (id = 0;
       id < static_cast< InstanceIdentifier >(totalSize);
       id++)
    {
    histogram->SetFrequency(id, 1);
    histogram->IncreaseFrequency(id, 1);
    if (histogram->GetFrequency(id) != 2)
      {
      pass = false;
      whereFail =
        "SetFrequency(InstanceIdentifier, 1) + IncreaseFrequency(InstanceIdentifier, 1) + GetFrequency(InstanceIdentifier)";
      }
    }

  double quantile1 = histogram->Quantile(0, 0.3);
  if( itk::Math::NotAlmostEquals( quantile1, 307.2) )
    {
    std::cerr << "quantile1 = " << quantile1 << std::endl;
    pass = false;
    whereFail = "Quantile(Dimension, percent)";
    }

  double quantile2 = histogram->Quantile(0, 0.5);
  if( quantile2 != 512.0)
    {
    std::cerr << "quantile2 = " << quantile2 << std::endl;
    pass = false;
    whereFail = "Quantile(Dimension, percent)";
    }

  double quantile3 = histogram->Quantile(0, 0.7);
  if( itk::Math::NotAlmostEquals( quantile3, 716.8) )
    {
    std::cerr << "quantile3 = " << quantile3 << std::endl;
    pass = false;
    whereFail = "Quantile(Dimension, percent)";
    }


  if( !pass )
    {
    std::cerr << "Test failed in " << whereFail << "." << std::endl;
    return EXIT_FAILURE;
    }


  // Histogram with SparseFrequencyContainer2
  typedef itk::Statistics::Histogram< MeasurementType,
    itk::Statistics::SparseFrequencyContainer2 > SparseHistogramType;
  SparseHistogramType::Pointer sparseHistogram = SparseHistogramType::New();

  sparseHistogram->SetMeasurementVectorSize( numberOfComponents );

  // initializes a 64 x 64 x 64 histogram with equal size interval
  sparseHistogram->Initialize(size, lowerBound, upperBound );
  sparseHistogram->SetToZero();
  interval = (upperBound[0] - lowerBound[0]) /
    static_cast< SparseHistogramType::MeasurementType >(size[0]);

  measurements.Fill(512);
  index.Fill(32);
  sparseHistogram->GetIndex(measurements,ind);

  if (index != ind)
    {
    pass = false;
    whereFail = "Sparse Histogram: GetIndex(MeasurementVectorType&)";
    }

  id = sparseHistogram->GetInstanceIdentifier(index);
  if (index != sparseHistogram->GetIndex(id))
    {
    pass = false;
    whereFail = "Sparse Histogram: GetIndex(InstanceIdentifier&)";
    }

  index.Fill(100);

  if (!sparseHistogram->IsIndexOutOfBounds(index))
    {
    pass = false;
    whereFail = "Sparse Histogram: IsIndexOutOfBounds(IndexType)";
    }

  if (totalSize != sparseHistogram->Size())
    {
    pass = false;
    whereFail = "Sparse Histogram: Size()";
    }

  if (size != sparseHistogram->GetSize())
    {
    pass = false;
    whereFail = "Sparse Histogram: GetSize()";
    }

  if (itk::Math::NotAlmostEquals( (lowerBound[0] + interval * 31), sparseHistogram->GetBinMin(0,31) ))
    {
    pass = false;
    whereFail = "Sparse Histogram: GetBinMin(Dimension, nthBin)";
    }

  if (itk::Math::NotAlmostEquals( (lowerBound[0] + interval * 32), sparseHistogram->GetBinMax(0,31) ))
    {
    pass = false;
    whereFail = "Sparse Histogram: GetBinMax(Dimension, nthBin)";
    }


  for (id = 0;
       id < static_cast< SparseHistogramType::InstanceIdentifier >(totalSize);
       id++)
    {
    bool result = sparseHistogram->SetFrequency(id, 1);
    if( !result )
      {
      pass = false;
      whereFail =
        "SetFrequency(InstanceIdentifier, 1) ";
      break;

      }

    result = sparseHistogram->IncreaseFrequency(id, 1);
    if( !result )
      {
      pass = false;
      whereFail =
        "IncreaseFrequency(InstanceIdentifier, 1) ";
      break;
      }

    if (sparseHistogram->GetFrequency(id) != 2)
      {
      pass = false;
      whereFail =
        "SetFrequency(InstanceIdentifier, 1) + IncreaseFrequency(InstanceIdentifier, 1) + GetFrequency(InstanceIdentifier)";
      break;
      }
    }

  if (pass && (sparseHistogram->Quantile(0, 0.5) != 512.0))
    {
    pass = false;
    whereFail = "Sparse Histogram: Quantile(Dimension, percent)";
    }


  histogram->SetClipBinsAtEnds( true );
  if( !histogram->GetClipBinsAtEnds() )
    {
    pass = false;
    whereFail = "Set/GetClipBinsAtEnds()";
    }

  histogram->SetClipBinsAtEnds( false );
  if( histogram->GetClipBinsAtEnds() )
    {
    pass = false;
    whereFail = "Set/GetClipBinsAtEnds()";
    }

  if( histogram->GetMeasurementVectorSize() != numberOfComponents )
    {
    pass = false;
    whereFail = "Set/GetMeasurementVectorSize()";
    }

  const unsigned int measurementVectorSize = 17;

  try
    {
    histogram->SetMeasurementVectorSize( measurementVectorSize );
    pass = false;
    whereFail = "SetMeasurementVectorSize() didn't throw expected exception";
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cout << "Expected exception ";
    std::cout << excp << std::endl;
    }

  index.Fill(0);
  MeasurementVectorType measurement = histogram->GetMeasurementVector( index );
  for( unsigned kid0 = 0; kid0 < numberOfComponents; kid0++ )
    {
    if( itk::Math::NotAlmostEquals( measurement[kid0], 8 ))
      {
      std::cerr << "GetMeasurementVector() for index = ";
      std::cerr << index << std::endl;
      pass = false;
      whereFail = "GetMeasurementVector() failed for index";
      break;
      }
    }

  histogram->SetClipBinsAtEnds( true );

  measurement = histogram->GetMeasurementVector( index );
  for( unsigned kid1 = 0; kid1 < numberOfComponents; kid1++ )
    {
    if( itk::Math::NotAlmostEquals( measurement[kid1], 8 ) )
      {
      std::cerr << "GetMeasurementVector() for index = ";
      std::cerr << index << std::endl;
      pass = false;
      whereFail = "GetMeasurementVector() failed for index";
      break;
      }
    }

  const InstanceIdentifier instanceId = 0;
  measurement = histogram->GetMeasurementVector( instanceId );
  for( unsigned kid2 = 0; kid2 < numberOfComponents; kid2++ )
    {
    if( itk::Math::NotAlmostEquals( measurement[kid2], 8 ) )
      {
      std::cerr << "GetMeasurementVector() for instanceId = ";
      std::cerr << instanceId << std::endl;
      pass = false;
      whereFail = "GetMeasurementVector() failed for instanceId";
      break;
      }
    }

  // Test GetIndex with different settings of SetClipBinsAtEnds
  MeasurementVectorType outOfLowerRange( numberOfComponents );
  MeasurementVectorType outOfUpperRange( numberOfComponents );

  for(unsigned int k = 0; k < numberOfComponents; k++)
    {
    outOfLowerRange[k] = lowerBound[k] - 13;
    outOfUpperRange[k] = upperBound[k] + 23;
    }

  histogram->SetClipBinsAtEnds( false );

  IndexType index1( numberOfComponents );
  bool getindex1 = histogram->GetIndex( outOfLowerRange, index1 );

  std::cout << "GetIndex() with SetClipBinsAtEnds() = false " << std::endl;
  std::cout << "Boolean " << getindex1 << " Index " << index1 << std::endl;

  if( !getindex1 )
    {
    pass = false;
    whereFail = "GetIndex() returned boolean failed for outOfLowerRange";
    }

  for(unsigned k1=0; k1 < numberOfComponents; k1++ )
    {
    if( index1[ k1 ] != 0 )
      {
      pass = false;
      whereFail = "GetIndex() index value failed for outOfLowerRange";
      }
    }


  histogram->SetClipBinsAtEnds( true );

  getindex1 = histogram->GetIndex( outOfLowerRange, index1 );

  std::cout << "GetIndex() with SetClipBinsAtEnds() = true " << std::endl;
  std::cout << "Boolean " << getindex1 << " Index " << index1 << std::endl;

  if( getindex1 )
    {
    pass = false;
    whereFail = "GetIndex() failed for outOfLowerRange";
    }

  histogram->SetClipBinsAtEnds( false );

  IndexType index2( numberOfComponents );
  bool getindex2 = histogram->GetIndex( outOfUpperRange, index2 );

  std::cout << "GetIndex() with SetClipBinsAtEnds() = false " << std::endl;
  std::cout << "Boolean " << getindex2 << " Index " << index2 << std::endl;

  if( !getindex2 )
    {
    pass = false;
    whereFail = "GetIndex() returned boolean failed for outOfUpperRange";
    }

  for(unsigned k2=0; k2 < numberOfComponents; k2++ )
    {
    if( index2[ k2 ] != (long)size[k2] - 1 )
      {
      pass = false;
      whereFail = "GetIndex() index value failed for outOfUpperRange";
      }
    }


  histogram->SetClipBinsAtEnds( true );

  getindex2 = histogram->GetIndex( outOfUpperRange, index2 );

  std::cout << "GetIndex() with SetClipBinsAtEnds() = true " << std::endl;
  std::cout << "Boolean " << getindex2 << " Index " << index2 << std::endl;

  if( getindex2 )
    {
    pass = false;
    whereFail = "GetIndex() failed for outOfUpperRange";
    }


  // Testing GetIndex() for values that are above the median value of the Bin.
  IndexType pindex( numberOfComponents );
  pindex.Fill( 32 );
  MeasurementVectorType measurementVector =
    histogram->GetMeasurementVector( pindex );

  for( unsigned int gik1=0; gik1<numberOfComponents; gik1++)
    {
    measurementVector[gik1] += 0.3;
    }

  IndexType gindex;
  histogram->GetIndex( measurementVector, gindex );

  for( unsigned int gik2=0; gik2<numberOfComponents; gik2++)
    {
    if( gindex[gik2] != 32 )
      {
      std::cerr << "GetIndex() / GetMeasurementVector() failed " << std::endl;
      std::cerr << "MeasurementVector = " << measurementVector << std::endl;
      std::cerr << "Index returned = " << gindex << std::endl;
      return EXIT_FAILURE;
      }
    }

  // Testing GetIndex() for values that are below the median value of the Bin.
  for( unsigned int gik3=0; gik3<numberOfComponents; gik3++)
    {
    measurementVector[gik3] -= 0.6;
    }

  histogram->GetIndex( measurementVector ,gindex);

  for( unsigned int gik4=0; gik4<numberOfComponents; gik4++)
    {
    if( gindex[gik4] != 32 )
      {
      std::cerr << "GetIndex() / GetMeasurementVector() failed " << std::endl;
      std::cerr << "MeasurementVector = " << measurementVector << std::endl;
      std::cerr << "Index returned = " << gindex << std::endl;
      return EXIT_FAILURE;
      }
    }

  // Testing GetIndex on the upper and lower bounds
  IndexType upperIndex( numberOfComponents );
  bool upperIndexBool = histogram->GetIndex( upperBound, upperIndex );
  if( !upperIndexBool )
    {
    pass = false;
    whereFail = "GetIndex() returned boolean failed for upper bound";
    }

  for(unsigned k1=0; k1 < numberOfComponents; k1++ )
    {
    if( upperIndex[ k1 ] != 63 )
      {
      pass = false;
      whereFail = "GetIndex() index value failed for upperBound, as upper bound should map to the last bin.";
      }
    }

  IndexType lowerIndex( numberOfComponents );
  bool lowerIndexBool = histogram->GetIndex( lowerBound, lowerIndex );
  if( !lowerIndexBool )
    {
    pass = false;
    whereFail = "GetIndex() returned boolean failed for lower bound";
    }

  for(unsigned k1=0; k1 < numberOfComponents; k1++ )
    {
    if( lowerIndex[ k1 ] != 0 )
      {
      pass = false;
      whereFail = "GetIndex() index value failed for lowerIndex, as lower bound should map to the first bin.";
      }
    }

  // Testing GetIndex above the upper bound of a bin
  histogram->SetClipBinsAtEnds( false );
  MeasurementVectorType measurementVectorAbove( numberOfComponents );
  for( unsigned int gupk1 = 0; gupk1<numberOfComponents; gupk1++)
    {
    measurementVectorAbove[gupk1] = 129.9;
    }

  IndexType aboveUpperIndex( numberOfComponents );
  bool aboveUpperIndexBool =
    histogram->GetIndex( measurementVectorAbove, aboveUpperIndex );
  if( !aboveUpperIndexBool )
    {
    std::cerr << "Upper bound index = " << aboveUpperIndex << std::endl;
    }

  // Get the mean value for a dimension
  unsigned int dimension = 0;
  double mean = histogram->Mean( dimension );
  std::cout << "Mean value along dimension " << dimension << " : " << mean << std::endl;

  HistogramType::Iterator itr = histogram->Begin();
  HistogramType::Iterator end = histogram->End();

  HistogramType::TotalAbsoluteFrequencyType totalFrequency =
    histogram->GetTotalFrequency();

  InstanceIdentifier histogramSize = histogram->Size();

  while( itr != end )
    {
    itr.SetFrequency( itr.GetFrequency() + 1 );
    ++itr;
    }

  HistogramType::TotalAbsoluteFrequencyType newTotalFrequency =
    histogram->GetTotalFrequency();

  if( newTotalFrequency != histogramSize + totalFrequency )
    {
    std::cerr << "Get/SetFrequency error in the Iterator" << std::endl;
    return EXIT_FAILURE;
    }


  //
  // Exercise GetIndex() method in the iterator.
  //
  std::cout << "TEST GetIndex() and GetFrequency() in the iterator" << std::endl;
  itr = histogram->Begin();
  end = histogram->End();

  // Print out only some of them, the first 10...
  for( unsigned int kk = 0; kk < 10 && itr != end; ++itr, ++kk )
    {
    std::cout << itr.GetIndex() <<  " : " << itr.GetFrequency() << std::endl;
    }


  //
  // Exercise GetMin / GetMax methods
  //
    {
    const double epsilon = 1e-6;
    HistogramType::SizeType size2 = histogram->GetSize();

    HistogramType::BinMinContainerType binMinimums = histogram->GetMins();

    for( unsigned int dim = 0; dim < numberOfComponents; dim++ )
      {
      HistogramType::BinMinVectorType binDimensionMinimums = histogram->GetDimensionMins( dim );
      for( unsigned int k = 0; k < size2[dim]; k++ )
        {
        HistogramType::MeasurementType minA = binMinimums[dim][k];
        HistogramType::MeasurementType minB = binDimensionMinimums[k];
        HistogramType::MeasurementType minC = histogram->GetBinMin( dim, k );
        if( ( itk::Math::abs( minA - minB ) > epsilon ) ||
            ( itk::Math::abs( minA - minC ) > epsilon )    )
          {
          std::cerr << "Error in Get Bin Mins methods" << std::endl;
          std::cerr << "dim = " << dim << " k = " << k << std::endl;
          std::cerr << "GetMins()          = " << minA << std::endl;
          std::cerr << "GetDimensionMins() = " << minB << std::endl;
          std::cerr << "GetMin()           = " << minC << std::endl;
          return EXIT_FAILURE;
          }
        }
      }

    HistogramType::BinMaxContainerType binMaximums = histogram->GetMaxs();

    for( unsigned int dim = 0; dim < numberOfComponents; dim++ )
      {
      HistogramType::BinMaxVectorType binDimensionMaximums = histogram->GetDimensionMaxs( dim );
      for( unsigned int k = 0; k < size2[dim]; k++ )
        {
        HistogramType::MeasurementType maxA = binMaximums[dim][k];
        HistogramType::MeasurementType maxB = binDimensionMaximums[k];
        HistogramType::MeasurementType maxC = histogram->GetBinMax( dim, k );
        if( ( itk::Math::abs( maxA - maxB ) > epsilon ) ||
            ( itk::Math::abs( maxA - maxC ) > epsilon )    )
          {
          std::cerr << "Error in Get Bin Maxs methods" << std::endl;
          std::cerr << "dim = " << dim << " k = " << k << std::endl;
          std::cerr << "GetMaxs()          = " << maxA << std::endl;
          std::cerr << "GetDimensionMaxs() = " << maxB << std::endl;
          std::cerr << "GetMax()           = " << maxC << std::endl;
          return EXIT_FAILURE;
          }
        }
      }
    }


  // Testing methods specific to Iterators
    {
    typedef HistogramType::Iterator IteratorType;
    IteratorType iter = histogram->Begin();
    IteratorType iter2 = histogram->End();

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

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

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

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

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

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

    IteratorType iter6( 7, histogram );
    if( iter6.GetInstanceIdentifier() != 7 )
      {
      std::cerr << "Iterator Constructor with instance identifier 7 failed" << std::endl;
      return EXIT_FAILURE;
      }

    }

  // Testing methods specific to ConstIterators
    {
    typedef HistogramType::ConstIterator ConstIteratorType;
    ConstIteratorType iter = histogram->Begin();
    ConstIteratorType iter2 = histogram->End();

    iter2 = iter;

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

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

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

    const HistogramType * constHistogram = histogram.GetPointer();

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

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

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

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


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

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

    }

  if( !pass )
    {
    std::cout << "Test failed in " << whereFail << "." << std::endl;
    return EXIT_FAILURE;
    }

  std::cout << "Test passed." << std::endl;
  return EXIT_SUCCESS;

}