File: sitkImageRegistrationMethodTests.cxx

package info (click to toggle)
simpleitk 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 75,056 kB
  • sloc: cpp: 25,403; python: 3,060; sh: 1,131; ansic: 369; java: 260; cs: 215; makefile: 51; ruby: 47; tcl: 22
file content (1035 lines) | stat: -rw-r--r-- 33,251 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
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
/*=========================================================================
*
*  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 <SimpleITKTestHarness.h>
#include <SimpleITK.h>

#include <memory>

namespace
{


class IterationUpdate
  : public itk::simple::Command
{
public:
  IterationUpdate( const itk::simple::ImageRegistrationMethod &m)
    : m_Method(m)
    {}

  std::vector<double> scales;
  std::string toString;

  virtual void Execute( )
    {
      // use sitk's output operator for std::vector etc..
      using itk::simple::operator<<;

      // stash the stream state
      std::ios  state(NULL);
      state.copyfmt(std::cout);

      if ( m_Method.GetOptimizerIteration() == 0 )
        {
        std::cout << "\tLevel: " << std::setw(3) << m_Method.GetCurrentLevel() << std::endl;
        std::cout << "\tScales: " << m_Method.GetOptimizerScales() << std::endl;
        std::cout << "\tLearning Rate: " <<  m_Method.GetOptimizerLearningRate() << std::endl;
        this->scales = m_Method.GetOptimizerScales();
        this->toString = m_Method.ToString();
        }

      std::cout << std::fixed << std::setfill(' ') << std::setprecision( 5 );
      std::cout << std::setw(3) << m_Method.GetOptimizerIteration();
      std::cout << " = " << std::setw(10) << m_Method.GetMetricValue();
      std::cout << " : " << m_Method.GetOptimizerPosition();
      if ( m_Method.GetOptimizerConvergenceValue() != 0.0 )
        {
        std::cout << " ( " << m_Method.GetOptimizerConvergenceValue() << " )";
        }
      std::cout << std::endl;

      std::cout.copyfmt(state);
    }

private:
  const itk::simple::ImageRegistrationMethod &m_Method;

};

}

//
//
//

TEST(Registration,ImageRegistrationMethod_Basic) {
  // This test is to perform some basic coverage of methods.

  namespace sitk = itk::simple;

  sitk::ImageRegistrationMethod registration;
  std::cout << registration.ToString();
}


//
// Fixture based tests
//

namespace sitk = itk::simple;

class sitkRegistrationMethodTest
  : public ::testing::Test
{
public:

  sitk::Image MakeGaussianBlob( const std::vector<double> &pt,
                               const std::vector<unsigned int> &size)
    {
      sitk::GaussianImageSource source;
      source.SetMean(pt);
      source.SetScale(1.0);

      std::vector<double> sigma;
      for(unsigned int i = 0; i < size.size(); ++i)
        {
        sigma.push_back(size[i]/10.0);
        }
      source.SetSigma(sigma);
      source.SetSize(size);
      source.SetOutputPixelType(sitk::sitkFloat32);


      return source.Execute();
    }

  sitk::Image MakeDualGaussianBlobs(const std::vector<double> &pt0,
                                    const std::vector<double> &pt1,
                                    const std::vector<unsigned int> &size)
    {
      sitk::GaussianImageSource source1;
      source1.SetMean(pt0);
      source1.SetScale(1.0);

      std::vector<double> sigma;
      for(unsigned int i = 0; i < size.size(); ++i)
        {
        sigma.push_back(size[i]/10.0);
        }
      source1.SetSigma(sigma);
      source1.SetSize(size);
      source1.SetOutputPixelType(sitk::sitkFloat32);

      sitk::GaussianImageSource source2;
      source2.SetMean(pt1);
      source2.SetScale(-1.0);
      source2.SetSigma(sigma);
      source2.SetSize(size);
      source2.SetOutputPixelType(sitk::sitkFloat32);

      return sitk::Add(source1.Execute(), source2.Execute());
    }

protected:
  virtual void SetUp()
  {
    fixedBlobs = MakeDualGaussianBlobs(v2(64,64), v2(192,192), std::vector<unsigned int>(2,256));
    movingBlobs = MakeDualGaussianBlobs(v2(54,74), v2(192,192), std::vector<unsigned int>(2,256));
  }


  // virtual void TearDown() {}

  sitk::Image fixedBlobs;
  sitk::Image movingBlobs;

};


TEST_F(sitkRegistrationMethodTest, Metric_Evaluate)
{
  sitk::Image fixed = fixedBlobs;
  sitk::Image moving = fixedBlobs;

  sitk::ImageRegistrationMethod R;
  R.SetInitialTransform(sitk::Transform(fixed.GetDimension(),sitk::sitkIdentity));

  EXPECT_NEAR(-1.5299437083119216, R.MetricEvaluate(fixed,moving), 1e-10 );

  R.SetMetricAsCorrelation();
  EXPECT_NEAR(-1.0, R.MetricEvaluate(fixed,moving), 1e-10 );

  // tolerance adjusted for i386, why is it so much more?
  R.SetMetricAsJointHistogramMutualInformation( 20, 1.5);
  EXPECT_NEAR(-0.52624100016564002, R.MetricEvaluate(fixed,moving), 2e-6 );

  R.SetMetricAsMeanSquares();
  EXPECT_NEAR(0.0, R.MetricEvaluate(fixed,moving), 1e-10 );

  R.SetMetricAsMattesMutualInformation();
  EXPECT_NEAR(-1.5299437083119216, R.MetricEvaluate(fixed,moving), 1e-10 );

  R.SetMetricAsMeanSquares();

  // test that the transforms are used

  R.SetInitialTransform(sitk::TranslationTransform(fixed.GetDimension(),v2(5,-7)));
  EXPECT_NEAR(0.0036468516797954148, R.MetricEvaluate(fixed,moving), 1e-10 );

  R.SetMovingInitialTransform(sitk::TranslationTransform(fixed.GetDimension(),v2(-5,7)));
  EXPECT_NEAR(0.0, R.MetricEvaluate(fixed,moving), 1e-10 );

  R.SetFixedInitialTransform(sitk::TranslationTransform(fixed.GetDimension(),v2(-5,7)));
  EXPECT_NEAR(0.0036468516797954148, R.MetricEvaluate(fixed,moving), 1e-10 );

  sitk::ImageRegistrationMethod R2;
  R2.SetInitialTransform(sitk::Transform(fixed.GetDimension(),sitk::sitkIdentity));
  R2.SetMetricAsMeanSquares();
  R2.SetMetricFixedMask(sitk::Greater(fixedBlobs,0));
  EXPECT_NEAR(0.0091550861657971119,R2.MetricEvaluate(fixedBlobs,movingBlobs), 1e-10);

  sitk::ImageRegistrationMethod R3;
  R3.SetInitialTransform(sitk::Transform(fixed.GetDimension(),sitk::sitkIdentity));
  R3.SetMetricAsMeanSquares();
  R3.SetMetricMovingMask(sitk::Less(movingBlobs,0));
  EXPECT_NEAR(3.34e-09 ,R3.MetricEvaluate(fixedBlobs,movingBlobs), 1e-10);
}

TEST_F(sitkRegistrationMethodTest, Transform_InPlaceOn)
{
  // This test is to check the inplace operation of the initial
  // transform
  sitk::ImageRegistrationMethod R;
  EXPECT_TRUE(R.GetInitialTransformInPlace());

  sitk::Image fixed = fixedBlobs;
  sitk::Image moving = fixedBlobs;

  double minStep=1e-4;
  unsigned int numberOfIterations=100;
  double relaxationFactor=0.5;
  double gradientMagnitudeTolerance = 1e-10;
  R.SetOptimizerAsRegularStepGradientDescent(1.0,
                                             minStep,
                                             numberOfIterations,
                                             relaxationFactor,
                                             gradientMagnitudeTolerance);

  R.SetInterpolator(sitk::sitkLinear);

  sitk::TranslationTransform tx(fixed.GetDimension());
  tx.SetOffset(v2(1.1,-2.2));
  R.SetInitialTransform(tx,false);
  EXPECT_TRUE(!R.GetInitialTransformInPlace());

  R.SetMetricAsMeanSquares();

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(fixed,moving);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-4);
  // expect input not to be modified
  EXPECT_EQ(v2(1.1,-2.2), tx.GetParameters());

  // optimize in place this time
  R.SetInitialTransform(tx,true);
  EXPECT_TRUE(R.GetInitialTransformInPlace());
  outTx = R.Execute(fixed,moving);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-4);
  // expect input to have been modified
  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), tx.GetParameters(), 1e-4);


  // set with const method, with inplace constant
  const sitk::Transform &ctx =  sitk::TranslationTransform(fixed.GetDimension(),v2(0.1,-0.2));
  R.SetInitialTransform(ctx);
  EXPECT_TRUE(R.GetInitialTransformInPlace());
  outTx = R.Execute(fixed,moving);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-4);
  // expect input not to be modified
  EXPECT_EQ(v2(0.1,-0.2), ctx.GetParameters());

}

TEST_F(sitkRegistrationMethodTest, Transform_Initial)
{
  // This test is to check the initial transforms
  sitk::ImageRegistrationMethod R;

  sitk::Image fixed = fixedBlobs;
  sitk::Image moving = fixedBlobs;

  double minStep=1e-4;
  unsigned int numberOfIterations=100;
  double relaxationFactor=0.5;
  double gradientMagnitudeTolerance = 1e-10;
  R.SetOptimizerAsRegularStepGradientDescent(1.0,
                                             minStep,
                                             numberOfIterations,
                                             relaxationFactor,
                                             gradientMagnitudeTolerance);

  R.SetInterpolator(sitk::sitkLinear);

  sitk::TranslationTransform tx(fixed.GetDimension());
  sitk::TranslationTransform txMoving(fixed.GetDimension());
  sitk::TranslationTransform txFixed(fixed.GetDimension());
  R.SetInitialTransform(tx,false);
  R.SetMovingInitialTransform(txMoving);
  R.SetFixedInitialTransform(txFixed);

  R.SetMetricAsMeanSquares();

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(fixed,moving);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-4);

  txMoving.SetOffset(v2(0.0,3.0));
  R.SetInitialTransform(tx,false);
  R.SetMovingInitialTransform(txMoving);
  R.SetFixedInitialTransform(txFixed);

  outTx = R.Execute(fixed,moving);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,-3.0), outTx.GetParameters(), 1e-4);

  txMoving.SetOffset(v2(0.0,3.0));
  txFixed.SetOffset(v2(0.0,2.0));
  R.SetInitialTransform(tx,false);
  R.SetMovingInitialTransform(txMoving);
  R.SetFixedInitialTransform(txFixed);

  outTx = R.Execute(fixed,moving);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,-1.0), outTx.GetParameters(), 1e-4);

  EXPECT_EQ(R.GetMovingInitialTransform().GetParameters(), v2(0.0,3.0));
  EXPECT_EQ(R.GetFixedInitialTransform().GetParameters(), v2(0.0,2.0));

  // test some expected exception cases

  R.SetInitialTransform(tx,false);
  R.SetMovingInitialTransform(sitk::TranslationTransform(3));
  R.SetFixedInitialTransform(txFixed);

  EXPECT_THROW(R.Execute(fixed,moving), sitk::GenericException);

  R.SetInitialTransform(tx,false);
  R.SetMovingInitialTransform(txMoving);
  R.SetFixedInitialTransform(sitk::TranslationTransform(3));

  EXPECT_THROW(R.Execute(fixed,moving), sitk::GenericException);

}



TEST_F(sitkRegistrationMethodTest, Mask_Test0)
{
  // This test is to check some excpetional cases for using masks
  sitk::ImageRegistrationMethod R;

  R.SetOptimizerAsGradientDescent(1.0, 100);

  R.SetInterpolator(sitk::sitkLinear);


  sitk::TranslationTransform tx(fixedBlobs.GetDimension());
  R.SetInitialTransform(tx);

  // wrong dimension should produce error
  R.SetMetricFixedMask(sitk::Image(100,100,100,sitk::sitkUInt8));

  EXPECT_THROW(R.Execute(fixedBlobs,movingBlobs), sitk::GenericException);

  R.SetMetricFixedMask(sitk::Image());
  // wrong dimension should produce error
  R.SetMetricMovingMask(sitk::Image(100,100,100,sitk::sitkUInt8));

  EXPECT_THROW(R.Execute(fixedBlobs,movingBlobs), sitk::GenericException);
}


TEST_F(sitkRegistrationMethodTest, Mask_Test1)
{
  // This test is to check that the metric masks have the correct
  // effect.

  sitk::ImageRegistrationMethod R;

  double learningRate=2.0;
  double minStep=1e-7;
  unsigned int numberOfIterations=100;
  double relaxationFactor=0.5;
  double gradientMagnitudeTolerance=1e-8;
  R.SetOptimizerAsRegularStepGradientDescent(learningRate,
                                             minStep,
                                             numberOfIterations,
                                             relaxationFactor,
                                             gradientMagnitudeTolerance );
  R.SetInterpolator(sitk::sitkLinear);


  sitk::TranslationTransform tx(fixedBlobs.GetDimension());
  R.SetInitialTransform(tx);

  R.SetMetricAsCorrelation();
  R.SetMetricFixedMask(sitk::Cast(sitk::Greater(fixedBlobs,0),sitk::sitkFloat32));

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(fixedBlobs,movingBlobs);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(-10,10), outTx.GetParameters(), 1e-4);
}


TEST_F(sitkRegistrationMethodTest, Mask_Test2)
{
  // This test is to check that the metric masks have the correct
  // effect.

  sitk::ImageRegistrationMethod R;

  double learningRate=1.0;
  double minStep=1e-7;
  unsigned int numberOfIterations=100;
  double relaxationFactor=0.5;
  double gradientMagnitudeTolerance=1e-8;
  R.SetOptimizerAsRegularStepGradientDescent(learningRate,
                                             minStep,
                                             numberOfIterations,
                                             relaxationFactor,
                                             gradientMagnitudeTolerance );
  R.SetInterpolator(sitk::sitkLinear);


  sitk::TranslationTransform tx(fixedBlobs.GetDimension());
  tx.SetOffset(v2(120,99));
  R.SetInitialTransform(tx);

  R.SetMetricAsCorrelation();
  R.SetMetricFixedMask(sitk::Greater(fixedBlobs,0));
  R.SetMetricMovingMask(sitk::Less(movingBlobs,0));

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(fixedBlobs,movingBlobs);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(128.0,128.0), outTx.GetParameters(), 1e-3);
}


TEST_F(sitkRegistrationMethodTest, VirtualDomain_Test)
{
  // Test usage of setting virtual domain

  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);
  //R.DebugOn();
  sitk::Image virtualImage = MakeGaussianBlob( v2(32,32), std::vector<unsigned int>(2,64) );

  R.SetVirtualDomainFromImage(virtualImage);

  // transform to optimize
  sitk::TranslationTransform tx(virtualImage.GetDimension());
  tx.SetOffset(v2(3.2,-1.2));
  R.SetInitialTransform(tx, false);

  sitk::Image fixedImage = virtualImage;
  fixedImage.SetOrigin(v2(100, 0));

  // virtual image to fixed image
  sitk::TranslationTransform fixedTransform(fixedImage.GetDimension());
  fixedTransform.SetOffset(v2(100, 0));
  R.SetFixedInitialTransform(fixedTransform);

  sitk::Image movingImage = virtualImage;
  movingImage.SetOrigin(v2(0, 200));

  // transform from virtual domain to moving image with "optimizing" transform
  sitk::TranslationTransform movingTransform(movingImage.GetDimension());
  movingTransform.SetOffset(v2(0, 200));
  R.SetMovingInitialTransform(movingTransform);

  R.SetMetricAsCorrelation();

  double minStep=1e-5;
  unsigned int numberOfIterations=100;
  double relaxationFactor=0.75;
  double gradientMagnitudeTolerance = 1e-20;
  R.SetOptimizerAsRegularStepGradientDescent(.1,
                                             minStep,
                                             numberOfIterations,
                                             relaxationFactor,
                                             gradientMagnitudeTolerance);


  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(fixedImage, movingImage);


  std::cout << "-------" << std::endl;
  std::cout << outTx.ToString() << std::endl;
  std::cout << "Optimizer stop condition: " << R.GetOptimizerStopConditionDescription() << std::endl;
  std::cout << " Iteration: " << R.GetOptimizerIteration() << std::endl;
  std::cout << " Metric value: " << R.GetMetricValue() << std::endl;

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-3);
  EXPECT_GT( R.GetOptimizerIteration(), 1u );

}

TEST_F(sitkRegistrationMethodTest, VirtualDomain_MultiRes_Test)
{
  // Test usage of setting virtual domain

  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);
  //R.DebugOn();
  sitk::Image virtualImage = MakeGaussianBlob( v3(32,32,32), std::vector<unsigned int>(3,64) );

  R.SetVirtualDomainFromImage(virtualImage);

  // transform to optimize
  sitk::TranslationTransform tx(virtualImage.GetDimension());
  tx.SetOffset(v3(5.21231, 3.2,-1.2));
  R.SetInitialTransform(tx, false);

  sitk::Image fixedImage = virtualImage;
  fixedImage.SetOrigin(v3(1000, 100, 0));

  // virtual image to fixed image
  sitk::TranslationTransform fixedTransform(fixedImage.GetDimension());
  fixedTransform.SetOffset(v3(1000, 100, 0));
  R.SetFixedInitialTransform(fixedTransform);

  sitk::Image movingImage = virtualImage;
  movingImage.SetOrigin(v3(0, 200, 512));

  // transform from virtual domain to moving image with "optimizing" transform
  sitk::TranslationTransform movingTransform(movingImage.GetDimension());
  movingTransform.SetOffset(v3(0, 200, 512));
  R.SetMovingInitialTransform(movingTransform);

  R.SetMetricAsMeanSquares();

  double minStep=1e-3;
  unsigned int numberOfIterations=10;
  double relaxationFactor=0.6;
  double gradientMagnitudeTolerance = 1e-10;
  sitk::ImageRegistrationMethod::EstimateLearningRateType estimateLearningRate = sitk::ImageRegistrationMethod::Never;
  R.SetOptimizerAsRegularStepGradientDescent(2,
                                             minStep,
                                             numberOfIterations,
                                             relaxationFactor,
                                             gradientMagnitudeTolerance,
                                             estimateLearningRate);

  std::vector<unsigned int> shrinkFactors(2);
  shrinkFactors[0] = 8;
  shrinkFactors[1] = 1;
  R.SetShrinkFactorsPerLevel( shrinkFactors );
  R.SetSmoothingSigmasPerLevel( v2(0.0, 0.0) );

  R.SetOptimizerScalesFromJacobian();

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(fixedImage, movingImage);


  std::cout << "-------" << std::endl;
  std::cout << outTx.ToString() << std::endl;
  std::cout << "Optimizer stop condition: " << R.GetOptimizerStopConditionDescription() << std::endl;
  std::cout << " Iteration: " << R.GetOptimizerIteration() << std::endl;
  std::cout << " Metric value: " << R.GetMetricValue() << std::endl;

  EXPECT_VECTOR_DOUBLE_NEAR(v3(0.0,0.0,0.0), outTx.GetParameters(), 1e-1);
  EXPECT_GT( R.GetOptimizerIteration(), 1u );

}



TEST_F(sitkRegistrationMethodTest, VirtualDomain_Parameters)
{

  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);

  sitk::Image virtualImage = MakeGaussianBlob( v3(32,32,32), std::vector<unsigned int>(3,64) );

  std::vector<uint32_t> size(3, 64);

  EXPECT_NO_THROW(
    R.SetVirtualDomain(size,
                       v3(0.0,0.0,0.0),
                       v3(1.0,1.0,1.0),
                       v9(1.0,0.0,0.0,
                          0.0,1.0,0.0,
                          0.0,0.0,1.0)) );

  EXPECT_THROW(
    R.SetVirtualDomain(size,
                       v2(0.0,0.0),
                       v3(1.0,1.0,1.0),
                       v9(1.0,0.0,0.0,
                          0.0,1.0,0.0,
                          0.0,0.0,1.0)),
    sitk::GenericException);


  EXPECT_THROW(
    R.SetVirtualDomain(size,
                       v3(0.0,0.0,0.0),
                       v2(1.0,1.0),
                       v9(1.0,0.0,0.0,
                          0.0,1.0,0.0,
                          0.0,0.0,1.0)),
    sitk::GenericException );

  EXPECT_THROW(
    R.SetVirtualDomain(size,
                       v3(0.0,0.0,0.0),
                       v3(1.0,1.0,1.0),
                       v3(1.0,0.0,0.0)),
    sitk::GenericException );
}

TEST_F(sitkRegistrationMethodTest, OptimizerWeights_Test)
{
  // Test the usage of optimizer weights

  sitk::ImageRegistrationMethod R;

  double learningRate=1.0;
  double minStep=1e-7;
  unsigned int numberOfIterations=100;
  double relaxationFactor=0.5;
  double gradientMagnitudeTolerance=1e-8;
  R.SetOptimizerAsRegularStepGradientDescent(learningRate,
                                             minStep,
                                             numberOfIterations,
                                             relaxationFactor,
                                             gradientMagnitudeTolerance );
  R.SetInterpolator(sitk::sitkLinear);

  sitk::Image &fixedImage = fixedBlobs;
  sitk::Image &movingImage = fixedBlobs;

  sitk::TranslationTransform tx(fixedImage.GetDimension());
  tx.SetOffset(v2(12,9));
  R.SetInitialTransform(tx);

  R.SetMetricAsCorrelation();



  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  std::cout << "check1\n";
  R.SetOptimizerWeights(v3(1.0,2.0,3.0));
  std::cout << "check2\n";
  EXPECT_ANY_THROW(R.Execute(fixedImage, movingImage));

  std::cout << "check3\n";
  R.SetOptimizerWeights(v2(1.0,1e10));
  sitk::Transform outTx = R.Execute(fixedImage, movingImage);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(12.0,0.0031), outTx.GetParameters(), 1e-3);
}



TEST_F(sitkRegistrationMethodTest, Optimizer_LBFGSB)
{
  sitk::Image image = MakeGaussianBlob( v2(64, 64), std::vector<unsigned int>(2,256) );


  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);

  sitk::TranslationTransform tx(image.GetDimension());
  tx.SetOffset(v2(-1,-2));
  R.SetInitialTransform(tx, false);

  R.SetMetricAsMeanSquares();
  R.SetOptimizerScalesFromIndexShift();
  R.SetOptimizerAsLBFGSB(1e-20,
                         20,
                         5,
                         2000,
                         1e10,
                         std::numeric_limits<double>::min(),
                         std::numeric_limits<double>::max(),
                         true);

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(image, image);


  std::cout << "-------" << std::endl;
  std::cout << outTx.ToString() << std::endl;
  std::cout << "Optimizer stop condition: " << R.GetOptimizerStopConditionDescription() << std::endl;
  std::cout << " Iteration: " << R.GetOptimizerIteration() << std::endl;
  std::cout << " Metric value: " << R.GetMetricValue() << std::endl;

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-3);
  EXPECT_VECTOR_DOUBLE_NEAR(v2(1.0,1.0), cmd.scales, 1e-1);

  tx.SetOffset(v2(-1,-2));
  R.SetOptimizerAsLBFGSB(1e-20,
                         1,
                         5,
                         2000,
                         1e10,
                         std::numeric_limits<double>::min(),
                         std::numeric_limits<double>::max(),
                         true);

  outTx = R.Execute(image, image);
  EXPECT_EQ(1u, R.GetOptimizerIteration()) << "Checking iteration.";
}


TEST_F(sitkRegistrationMethodTest, Optimizer_Exhaustive)
{
  sitk::Image image = MakeGaussianBlob( v2(64, 64), std::vector<unsigned int>(2,256) );


  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);

  sitk::TranslationTransform tx(image.GetDimension());
  tx.SetOffset(v2(-1,-2));
  R.SetInitialTransform(tx, false);

  R.SetMetricAsMeanSquares();

  // Search grid of size 11x11
  R.SetOptimizerAsExhaustive(std::vector<unsigned int>(2,5), 0.5);

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(image, image);


  std::cout << "-------" << std::endl;
  std::cout << outTx.ToString() << std::endl;
  std::cout << "Optimizer stop condition: " << R.GetOptimizerStopConditionDescription() << std::endl;
  std::cout << " Iteration: " << R.GetOptimizerIteration() << std::endl;
  std::cout << " Metric value: " << R.GetMetricValue() << std::endl;

  // We expect the returned metric value after registration to
  // correspond to the best value and not necessarily to the last value
  double metric_value = R.GetMetricValue();
  R.SetInitialTransform(outTx);
  EXPECT_DOUBLE_EQ(R.MetricEvaluate(image,image), metric_value);
  //final metric value is expected to be zero as this is the same image
  EXPECT_DOUBLE_EQ(0.0, metric_value);

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-3);
  EXPECT_EQ(0u, cmd.scales.size());

  // Execute in place

  R.SetOptimizerScalesFromIndexShift();

  tx.SetOffset(v2(-1,-2));
  R.SetInitialTransform(tx, true);

  R.SetOptimizerAsExhaustive( std::vector<unsigned int>(2,5), 0.5);

  outTx = R.Execute(image, image);


  std::cout << "-------" << std::endl;
  std::cout << outTx.ToString() << std::endl;
  std::cout << "Optimizer stop condition: " << R.GetOptimizerStopConditionDescription() << std::endl;
  std::cout << " Iteration: " << R.GetOptimizerIteration() << std::endl;
  std::cout << " Metric value: " << R.GetMetricValue() << std::endl;

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-3);
  EXPECT_VECTOR_DOUBLE_NEAR(v2(1.0,1.0), cmd.scales, 1e-1);
}


TEST_F(sitkRegistrationMethodTest, Optimizer_Amoeba)
{
  sitk::Image image = MakeGaussianBlob( v2(64, 64), std::vector<unsigned int>(2,256) );


  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);

  sitk::TranslationTransform tx(image.GetDimension());
  tx.SetOffset(v2(-1,-2));
  R.SetInitialTransform(tx, false);

  R.SetMetricAsMeanSquares();

  R.SetOptimizerScalesFromIndexShift();

  R.SetOptimizerAsAmoeba(2.0, 200);

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(image, image);


  std::cout << "-------" << std::endl;
  std::cout << outTx.ToString() << std::endl;
  std::cout << "Optimizer stop condition: " << R.GetOptimizerStopConditionDescription() << std::endl;
  std::cout << " Iteration: " << R.GetOptimizerIteration() << std::endl;
  std::cout << " Metric value: " << R.GetMetricValue() << std::endl;

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-3);
  EXPECT_VECTOR_DOUBLE_NEAR(v2(1.0,1.0), cmd.scales, 1e-1);

}



TEST_F(sitkRegistrationMethodTest, Optimizer_Powell)
{
  sitk::Image image = MakeGaussianBlob( v2(64, 64), std::vector<unsigned int>(2,256) );


  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);

  sitk::TranslationTransform tx(image.GetDimension());
  tx.SetOffset(v2(-1,-2));
  R.SetInitialTransform(tx, false);

  R.SetMetricAsMeanSquares();

  R.SetOptimizerScalesFromIndexShift();

  R.SetOptimizerAsPowell(10, 50, .2, .01, .0001 );

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(image, image);


  std::cout << "-------" << std::endl;
  std::cout << outTx.ToString() << std::endl;
  std::cout << "Optimizer stop condition: " << R.GetOptimizerStopConditionDescription() << std::endl;
  std::cout << " Iteration: " << R.GetOptimizerIteration() << std::endl;
  std::cout << " Metric value: " << R.GetMetricValue() << std::endl;

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-3);
  EXPECT_VECTOR_DOUBLE_NEAR(v2(1.0,1.0), cmd.scales, 1e-1);

}

TEST_F(sitkRegistrationMethodTest, Optimizer_OnePlusOneEvolutionary)
{
  sitk::Image image = MakeGaussianBlob( v2(64, 64), std::vector<unsigned int>(2,256) );


  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);

  sitk::TranslationTransform tx(image.GetDimension());
  tx.SetOffset(v2(-1,-2));
  R.SetInitialTransform(tx, false);

  R.SetMetricAsMeanSquares();

  R.SetOptimizerScalesFromIndexShift();

  R.SetOptimizerAsOnePlusOneEvolutionary(200,
                                         1.5e-4,
                                         1.0,
                                         1.1,
                                         .9,
                                         1u);

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx = R.Execute(image, image);


  std::cout << "-------" << std::endl;
  std::cout << outTx.ToString() << std::endl;
  std::cout << "Optimizer stop condition: " << R.GetOptimizerStopConditionDescription() << std::endl;
  std::cout << " Iteration: " << R.GetOptimizerIteration() << std::endl;
  std::cout << " Convergence value: " << R.GetOptimizerConvergenceValue() << std::endl;
  std::cout << " Metric value: " << R.GetMetricValue() << std::endl;

  EXPECT_VECTOR_DOUBLE_NEAR(v2(0.0,0.0), outTx.GetParameters(), 1e-3);
  EXPECT_VECTOR_DOUBLE_NEAR(v2(1.0,1.0), cmd.scales, 1e-1);

}


TEST_F(sitkRegistrationMethodTest, Optimizer_ScalesEstimator)
{
  sitk::Image fixedImage = MakeDualGaussianBlobs( v2(64, 64), v2(54, 74), std::vector<unsigned int>(2,256) );
  sitk::Image movingImage = MakeDualGaussianBlobs( v2(61.2, 73.5), v2(51.2, 83.5), std::vector<unsigned int>(2,256) );


  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);

  sitk::Euler2DTransform tx;
  R.SetInitialTransform(tx, false);

  R.SetMetricAsMeanSquares();

  unsigned int numberOfIterations=100;
  double convergenceMinimumValue = 1e-6;
  unsigned int convergenceWindowSize = 2;
  R.SetOptimizerAsConjugateGradientLineSearch( 1.0,
                                               numberOfIterations,
                                               convergenceMinimumValue,
                                               convergenceWindowSize );


  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  R.SetOptimizerScalesFromIndexShift();
  sitk::Transform outTx = R.Execute(fixedImage, movingImage);

  EXPECT_VECTOR_DOUBLE_NEAR(v3(0.0,-2.8,9.5), outTx.GetParameters(), 0.6);
  EXPECT_VECTOR_DOUBLE_NEAR(v3(130049,1.0,1.0), cmd.scales, 1.0);\
  EXPECT_TRUE( cmd.toString.find("ScalesFromIndexShift") != std::string::npos );


  R.SetOptimizerScalesFromJacobian();
  outTx = R.Execute(fixedImage, movingImage);

  EXPECT_VECTOR_DOUBLE_NEAR(v3(0.0,-2.8,9.5), outTx.GetParameters(), 0.6);
  EXPECT_VECTOR_DOUBLE_NEAR(v3(65025,1.0,1.0), cmd.scales, 1.0);
  EXPECT_TRUE( cmd.toString.find("ScalesFromJacobian") != std::string::npos );


  R.SetOptimizerScalesFromPhysicalShift();
  outTx = R.Execute(fixedImage, movingImage);

  EXPECT_VECTOR_DOUBLE_NEAR(v3(0.0,-2.8,9.5), outTx.GetParameters(), 0.6);
  EXPECT_VECTOR_DOUBLE_NEAR(v3(130049,1.0,1.0), cmd.scales, 1.0);
  EXPECT_TRUE( cmd.toString.find("ScalesFromPhysicalShift") != std::string::npos );

  R.SetOptimizerScales(v3(200000,1.0,1.0));
  outTx = R.Execute(fixedImage, movingImage);

  EXPECT_VECTOR_DOUBLE_NEAR(v3(0.0,-2.8,9.5), outTx.GetParameters(), 0.4);
  EXPECT_VECTOR_DOUBLE_NEAR(v3(200000,1.0,1.0), cmd.scales, 1e-10);


}


TEST_F(sitkRegistrationMethodTest, Optimizer_Sampling)
{
  sitk::Image fixedImage = MakeDualGaussianBlobs( v2(64, 64), v2(54, 74), std::vector<unsigned int>(2,256) );
  sitk::Image movingImage = MakeDualGaussianBlobs( v2(61.2, 65.5), v2(51.2, 75.5), std::vector<unsigned int>(2,256) );

  fixedImage = sitk::AdditiveGaussianNoise(fixedImage,  0.5, 0, 1u);

  sitk::ImageRegistrationMethod R;
  R.SetInterpolator(sitk::sitkLinear);

  sitk::TranslationTransform tx(2u);
  R.SetInitialTransform(tx, false);

  R.SetMetricAsMeanSquares();

  double minStep=1e-4;
  unsigned int numberOfIterations=100;
  double relaxationFactor=0.5;
  double gradientMagnitudeTolerance = 1e-5;
  R.SetOptimizerAsRegularStepGradientDescent(1.0,
                                             minStep,
                                             numberOfIterations,
                                             relaxationFactor,
                                             gradientMagnitudeTolerance);

  IterationUpdate cmd(R);
  R.AddCommand(sitk::sitkIterationEvent, cmd);

  sitk::Transform outTx1, outTx2;


  // set fixed seed and expect the same results
  R.SetMetricSamplingStrategy(R.RANDOM);
  R.SetMetricSamplingPercentage(.02,1u);

  outTx1 = R.Execute(fixedImage, movingImage);
  outTx2 = R.Execute(fixedImage, movingImage);

  EXPECT_VECTOR_DOUBLE_NEAR(outTx1.GetParameters(), outTx1.GetParameters(), 1e-10) << "Same registration with fixed seed and random sampling";

  // set fixed seed and expect the same results
  R.SetMetricSamplingStrategy(R.REGULAR);
  R.SetMetricSamplingPercentage(.02,1u);

  outTx1 = R.Execute(fixedImage, movingImage);
  outTx2 = R.Execute(fixedImage, movingImage);

  EXPECT_VECTOR_DOUBLE_NEAR(outTx1.GetParameters(), outTx1.GetParameters(), 1e-10)  << "Same registration with fixed seed and regular sampling";

  // set wall clock seed and expect the same results with full sampling
  R.SetMetricSamplingStrategy(R.NONE);
  R.SetMetricSamplingPercentage(.02,sitk::sitkWallClock);

  outTx1 = R.Execute(fixedImage, movingImage);
  outTx2 = R.Execute(fixedImage, movingImage);

  EXPECT_VECTOR_DOUBLE_NEAR(outTx1.GetParameters(), outTx1.GetParameters(), 1e-10)  << "Same registration with fixed seed and regular sampling";


  // Use wall clock random seed.
 R.SetMetricSamplingStrategy(R.RANDOM);
 R.SetMetricSamplingPercentage(.02,sitk::sitkWallClock);

 R.Execute(fixedImage, movingImage);
 double firstValue = R.GetMetricValue();
 double totalDiff = 0.0;

 for( unsigned int i=1; i<5; ++i)
   {
   R.Execute(fixedImage, movingImage);
   totalDiff += std::abs(firstValue -R.GetMetricValue());
   }
 EXPECT_TRUE(totalDiff > 1e-10) << "Expect difference between metric values with random sampling\n";
}