20 #include "itkArray2D.h"
22 #include <itkCSVArray2DFileReader.h>
23 #include <itkCSVArray2DDataObject.h>
26 #include <itkAddImageFilter.h>
32 CPPUNIT_TEST_SUITE(mitkVigraRandomForestTestSuite );
39 MITK_TEST(TrainThreadedDecisionForest_MatlabDataSet_shouldReturnTrue);
40 MITK_TEST(PredictWeightedDecisionForest_SetWeightsToZero_shouldReturnTrue);
41 MITK_TEST(TrainThreadedDecisionForest_BreastCancerDataSet_shouldReturnTrue);
42 CPPUNIT_TEST_SUITE_END();
46 typedef Eigen::Matrix<double ,Eigen::Dynamic,Eigen::Dynamic> MatrixDoubleType;
47 typedef Eigen::Matrix<int, Eigen::Dynamic,Eigen::Dynamic> MatrixIntType;
49 std::pair<MatrixDoubleType, MatrixDoubleType> FeatureData_Cancer;
50 std::pair<MatrixIntType, MatrixIntType> LabelData_Cancer;
52 std::pair<MatrixDoubleType, MatrixDoubleType> FeatureData_Matlab;
53 std::pair<MatrixIntType, MatrixIntType> LabelData_Matlab;
65 FeatureData_Cancer = convertCSVToMatrix<double>(
GetTestDataFilePath(
"Classification/FeaturematrixBreastcancer.csv"),
';',0.5,
true);
66 LabelData_Cancer = convertCSVToMatrix<int>(
GetTestDataFilePath(
"Classification/LabelmatrixBreastcancer.csv"),
';',0.5,
false);
67 FeatureData_Matlab = convertCSVToMatrix<double>(
GetTestDataFilePath(
"Classification/FeaturematrixMatlab.csv"),
';',0.5,
true);
68 LabelData_Matlab = convertCSVToMatrix<int>(
GetTestDataFilePath(
"Classification/LabelmatrixMatlab.csv"),
';',0.5,
false);
83 void TrainThreadedDecisionForest_MatlabDataSet_shouldReturnTrue()
86 auto & Features_Training = FeatureData_Matlab.first;
87 auto & Labels_Training = LabelData_Matlab.first;
89 auto & Features_Testing = FeatureData_Matlab.second;
90 auto & Labels_Testing = LabelData_Matlab.second;
94 classifier->Train(Features_Training,Labels_Training);
95 Eigen::MatrixXi classes = classifier->Predict(Features_Testing);
98 unsigned int testmatrix_rows = classes.rows();
100 unsigned int correctly_classified_rows = 0;
101 for(
unsigned int i= 0; i < testmatrix_rows; i++){
102 if(classes(i,0) == Labels_Testing(i,0)){
103 correctly_classified_rows++;
107 MITK_TEST_CONDITION(correctly_classified_rows == testmatrix_rows,
"Matlab Data correctly classified");
116 void TrainThreadedDecisionForest_BreastCancerDataSet_shouldReturnTrue()
119 auto & Features_Training = FeatureData_Cancer.first;
120 auto & Features_Testing = FeatureData_Cancer.second;
121 auto & Labels_Training = LabelData_Cancer.first;
122 auto & Labels_Testing = LabelData_Cancer.second;
127 classifier->Train(Features_Training,Labels_Training);
128 Eigen::MatrixXi classes = classifier->Predict(Features_Testing);
131 unsigned int maxrows = classes.rows();
133 bool isYPredictVector =
false;
136 for(
unsigned int i= 0; i < maxrows; i++){
137 if(classes(i,0) == Labels_Testing(i,0)){
138 isYPredictVector =
true;
142 MITK_TEST_CONDITION(isIntervall<int>(Labels_Testing,classes,98,99),
"Testvalue of cancer data set is in range.");
148 void PredictWeightedDecisionForest_SetWeightsToZero_shouldReturnTrue()
151 auto & Features_Training = FeatureData_Matlab.first;
152 auto & Features_Testing = FeatureData_Matlab.second;
153 auto & Labels_Training = LabelData_Matlab.first;
156 classifier->Train(Features_Training,Labels_Training);
159 auto weights = classifier->GetTreeWeights();
160 weights.resize(classifier->GetRandomForest().tree_count(),1);
163 classifier->SetTreeWeights(weights);
166 Eigen::MatrixXi classes = classifier->PredictWeighted(Features_Testing);
169 unsigned int maxrows = classes.rows();
170 unsigned int count = 0;
173 for(
unsigned int i= 0; i < maxrows; i++)
174 if(classes(i,0) == 1)
177 MITK_TEST_CONDITION( (count == maxrows) ,
"Weighted prediction - weights applied (all weights = 0).");
187 std::pair<Eigen::Matrix<T ,Eigen::Dynamic,Eigen::Dynamic>,Eigen::Matrix<T ,Eigen::Dynamic,Eigen::Dynamic> >convertCSVToMatrix(
const std::string &path,
char delimiter,
double range,
bool isXMatrix)
190 fr->SetFileName(path);
191 fr->SetFieldDelimiterCharacter(delimiter);
192 fr->HasColumnHeadersOff();
193 fr->HasRowHeadersOff();
197 }
catch(itk::ExceptionObject& ex){
198 cout <<
"Exception caught!" << std::endl;
199 cout << ex << std::endl;
203 unsigned int maxrowrange = p->GetMatrix().rows();
204 unsigned int c = p->GetMatrix().cols();
205 unsigned int percentRange = (
unsigned int)(maxrowrange*range);
207 if(isXMatrix ==
true){
208 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> trainMatrixX(percentRange,c);
209 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> testMatrixXPredict(maxrowrange-percentRange,c);
211 for(
unsigned int row = 0; row < percentRange; row++){
212 for(
unsigned int col = 0; col < c; col++){
213 trainMatrixX(row,col) = p->GetData(row,col);
217 for(
unsigned int row = percentRange; row < maxrowrange; row++){
218 for(
unsigned int col = 0; col < c; col++){
219 testMatrixXPredict(row-percentRange,col) = p->GetData(row,col);
223 return std::make_pair(trainMatrixX,testMatrixXPredict);
226 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> trainLabelMatrixY(percentRange,c);
227 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> testMatrixYPredict(maxrowrange-percentRange,c);
229 for(
unsigned int row = 0; row < percentRange; row++){
230 for(
unsigned int col = 0; col < c; col++){
231 trainLabelMatrixY(row,col) = p->GetData(row,col);
235 for(
unsigned int row = percentRange; row < maxrowrange; row++){
236 for(
unsigned int col = 0; col < c; col++){
237 testMatrixYPredict(row-percentRange,col) = p->GetData(row,col);
240 return std::make_pair(trainLabelMatrixY,testMatrixYPredict);
249 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> readCsvData(
const std::string &path,
char delimiter)
252 fr->SetFileName(path);
253 fr->SetFieldDelimiterCharacter(delimiter);
254 fr->HasColumnHeadersOff();
255 fr->HasRowHeadersOff();
259 }
catch(itk::ExceptionObject& ex){
260 cout <<
"Exception caught!" << std::endl;
261 cout << ex << std::endl;
265 unsigned int maxrowrange = p->GetMatrix().rows();
266 unsigned int maxcols = p->GetMatrix().cols();
267 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> matrix(maxrowrange,maxcols);
269 for(
unsigned int rows = 0; rows < maxrowrange; rows++){
270 for(
unsigned int cols = 0; cols < maxcols; cols++ ){
271 matrix(rows,cols) = p->GetData(rows,cols);
284 void writeMatrixToCsv(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> paramMatrix,
const std::string &path)
286 std::ofstream outputstream (path,std::ofstream::out);
288 if(outputstream.is_open()){
289 for(
int i = 0; i < paramMatrix.rows(); i++){
290 outputstream << paramMatrix(i,0);
291 for(
int j = 1; j < 11; j++){
292 outputstream <<
" " << j <<
":" << paramMatrix(i,j);
294 outputstream << endl;
296 outputstream.close();
299 cout <<
"Unable to write into CSV" << endl;
305 bool isIntervall(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> expected, Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> actual,
double lowrange,
double toprange)
307 bool isInIntervall =
false;
309 unsigned int rowRange = expected.rows();
310 unsigned int colRange = expected.cols();
311 for(
unsigned int i = 0; i < rowRange; i++){
312 for(
unsigned int j = 0; j < colRange; j++){
313 if(expected(i,j) == actual(i,j)){
318 double valueOfMatch = 100*count/(double)(rowRange);
319 if((lowrange <= valueOfMatch) && (toprange >= valueOfMatch)){
320 isInIntervall =
true;
323 return isInIntervall;
itk::SmartPointer< Self > Pointer
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
#define MITK_TEST_CONDITION(COND, MSG)
Test fixture for parameterized tests.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.