Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkClassificationTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3  The Medical Imaging Interaction Toolkit (MITK)
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics.
7  All rights reserved.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without
10  even the implied warranty of MERCHANTABILITY or FITNESS FOR
11  A PARTICULAR PURPOSE.
12 
13  See LICENSE.txt or http://www.mitk.org for details.
14 
15  ===================================================================*/
16 #pragma warning(disable : 4996)
17 
18 #include "mitkTestFixture.h"
19 #include "mitkTestingMacros.h"
20 
22 #include "mitkIOUtil.h"
23 
24 #include "mitkCollectionReader.h"
25 #include "mitkCollectionWriter.h"
26 #include "mitkDataCollection.h"
27 
29 #include <vtkSmartPointer.h>
30 
39 class mitkClassificationTestSuite : public mitk::TestFixture
40 {
41  CPPUNIT_TEST_SUITE(mitkClassificationTestSuite);
42  MITK_TEST(TestClassification);
43  CPPUNIT_TEST_SUITE_END();
44 
45 public:
50  void setUp() {}
51  void tearDown() {}
52  void TestClassification()
53  {
54  size_t forestSize = 10;
55  size_t treeDepth = 2;
56 
57  std::string train = GetTestDataFilePath("DiffusionImaging/ProgressionAnalysis/Classification/Train.xml");
58  std::string eval = GetTestDataFilePath("DiffusionImaging/ProgressionAnalysis/Classification/Test.xml");
59 
60  std::vector<std::string> modalities;
61 
62  modalities.push_back("MOD0");
63  modalities.push_back("MOD1");
64 
65  mitk::CollectionReader colReader;
66  mitk::DataCollection::Pointer collection = colReader.LoadCollection(train);
67  colReader.Clear();
68  // read evaluation collection
69 
70  mitk::DataCollection::Pointer evaluation = colReader.LoadCollection(eval);
71 
73 
74  progression.SetClassRatio(1);
75  progression.SetTrainMargin(4, 0);
76  progression.SetMaskID("MASK");
77 
78  progression.SelectTrainingSamples(collection);
79  progression.LearnProgressionFeatures(collection, modalities, forestSize, treeDepth);
80 
81  progression.PredictInvasion(evaluation, modalities);
82 
84  GetTestDataFilePath("DiffusionImaging/ProgressionAnalysis/Classification/TESTING_RESULT.nrrd"));
85 
86  mitk::DataCollection *patCol = dynamic_cast<mitk::DataCollection *>(evaluation->GetData(0).GetPointer());
87  mitk::DataCollection *subCol = dynamic_cast<mitk::DataCollection *>(patCol->GetData(0).GetPointer());
88  mitk::Image::Pointer resultImage = subCol->GetMitkImage("RESULT");
89 
90  // Test result against fixed reference.
91  // Require more than 90% to be correct.
92  // 10% margin due to
93  // 1) unsure classification in transitional regions and
94  // 2) stochastic training procedure
95 
96  // Total number of voxels 2400 -> 10% err -> 240 voxels margin
97 
99  compareFilter->SetInput(0, refImage.GetPointer());
100  compareFilter->SetInput(1, resultImage);
101  compareFilter->SetTolerance(.1);
102  compareFilter->Update();
103 
104  MITK_TEST_CONDITION_REQUIRED(compareFilter->GetResult(240), "Compare prediction results to reference image.")
105  }
106 };
107 MITK_TEST_SUITE_REGISTRATION(mitkClassification)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
DataCollection::Pointer LoadCollection(const std::string &xmlFileName)
Build up a mitk::DataCollection from a XML resource.
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
void SetTrainMargin(vcl_size_t dil2d, vcl_size_t dil3d)
void SetMaskID(std::string maskID="BRAINMASK")
SetMaskID sets binary mask denoting area that is evluated/included in training.
mitk::Image::Pointer GetMitkImage(vcl_size_t index)
GetMitkImage - casts data to mitk::Image and returns it.
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
void SelectTrainingSamples(DataCollection *collection, unsigned int mode=0)
SelectTrainingSamples.
void PredictInvasion(DataCollection *collection, std::vector< std::string > modalitiesList)
PredictGrowth - Classify voxels into remaining healthy / turning into tumor.
static Pointer New()
Test fixture for parameterized tests.
The TumorInvasionAnalysis class - Classifies Tumor progression using RF and predicts on new cases...
void LearnProgressionFeatures(DataCollection *collection, std::vector< std::string > modalitiesList, vcl_size_t forestSize=300, vcl_size_t treeDepth=10)
LearnProgressionFeatures.
void SetClassRatio(ScalarType ratio)
SetClassRatio - set ratio of tumor voxels to healthy voxels that is to be used for training...
itk::DataObject::Pointer GetData(vcl_size_t index)
GetData Get original data by index.
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:597