20 #include "itkArray2D.h"
26 #include <itkCSVArray2DFileReader.h>
27 #include <itkCSVArray2DDataObject.h>
28 #include <itkCSVNumericObjectFileWriter.h>
34 CPPUNIT_TEST_SUITE(mitkLibSVMClassifierTestSuite);
35 MITK_TEST(TrainSVMClassifier_MatlabDataSet_shouldReturnTrue);
36 MITK_TEST(TrainSVMClassifier_BreastCancerDataSet_shouldReturnTrue);
37 CPPUNIT_TEST_SUITE_END();
41 typedef Eigen::Matrix<double ,Eigen::Dynamic,Eigen::Dynamic> MatrixDoubleType;
42 typedef Eigen::Matrix<int, Eigen::Dynamic,Eigen::Dynamic> MatrixIntType;
44 Eigen::MatrixXd m_TrainingMatrixX;
45 Eigen::MatrixXi m_TrainingLabelMatrixY;
46 Eigen::MatrixXd m_TestXPredict;
47 Eigen::MatrixXi m_TestYPredict;
57 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)
60 fr->SetFileName(path);
61 fr->SetFieldDelimiterCharacter(delimiter);
62 fr->HasColumnHeadersOff();
63 fr->HasRowHeadersOff();
67 }
catch(itk::ExceptionObject& ex){
68 cout <<
"Exception caught!" << std::endl;
69 cout << ex << std::endl;
73 unsigned int maxrowrange = p->GetMatrix().rows();
74 unsigned int c = p->GetMatrix().cols();
75 unsigned int percentRange = (
unsigned int)(maxrowrange*range);
77 if(isXMatrix ==
true){
78 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> trainMatrixX(percentRange,c);
79 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> testMatrixXPredict(maxrowrange-percentRange,c);
81 for(
int row = 0; row < percentRange; row++){
82 for(
int col = 0; col < c; col++){
83 trainMatrixX(row,col) = p->GetData(row,col);
87 for(
int row = percentRange; row < maxrowrange; row++){
88 for(
int col = 0; col < c; col++){
89 testMatrixXPredict(row-percentRange,col) = p->GetData(row,col);
93 return std::make_pair(trainMatrixX,testMatrixXPredict);
95 else if(isXMatrix ==
false){
96 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> trainLabelMatrixY(percentRange,c);
97 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> testMatrixYPredict(maxrowrange-percentRange,c);
99 for(
int row = 0; row < percentRange; row++){
100 for(
int col = 0; col < c; col++){
101 trainLabelMatrixY(row,col) = p->GetData(row,col);
105 for(
int row = percentRange; row < maxrowrange; row++){
106 for(
int col = 0; col < c; col++){
107 testMatrixYPredict(row-percentRange,col) = p->GetData(row,col);
111 return std::make_pair(trainLabelMatrixY,testMatrixYPredict);
119 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> readCsvData(
const std::string &path,
char delimiter)
122 fr->SetFileName(path);
123 fr->SetFieldDelimiterCharacter(delimiter);
124 fr->HasColumnHeadersOff();
125 fr->HasRowHeadersOff();
129 }
catch(itk::ExceptionObject& ex){
130 cout <<
"Exception caught!" << std::endl;
131 cout << ex << std::endl;
135 unsigned int maxrowrange = p->GetMatrix().rows();
136 unsigned int maxcols = p->GetMatrix().cols();
137 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> matrix(maxrowrange,maxcols);
139 for(
int rows = 0; rows < maxrowrange; rows++){
140 for(
int cols = 0; cols < maxcols; cols++ ){
141 matrix(rows,cols) = p->GetData(rows,cols);
154 void writeMatrixToCsv(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> paramMatrix,
const std::string &path)
156 std::ofstream outputstream (path,std::ofstream::out);
158 if(outputstream.is_open()){
159 for(
int i = 0; i < paramMatrix.rows(); i++){
160 outputstream << paramMatrix(i,0);
161 for(
int j = 1; j < 11; j++){
162 outputstream <<
" " << j <<
":" << paramMatrix(i,j);
164 outputstream << endl;
166 outputstream.close();
169 cout <<
"Unable to write into CSV" << endl;
177 void TrainSVMClassifier_MatlabDataSet_shouldReturnTrue()
181 std::pair<MatrixDoubleType,MatrixDoubleType> matrixDouble;
182 matrixDouble = convertCSVToMatrix<double>(
GetTestDataFilePath(
"Classification/FeaturematrixMatlab.csv"),
';',0.5,
true);
183 m_TrainingMatrixX = matrixDouble.first;
184 m_TestXPredict = matrixDouble.second;
188 std::pair<MatrixIntType,MatrixIntType> matrixInt;
189 matrixInt = convertCSVToMatrix<int>(
GetTestDataFilePath(
"Classification/LabelmatrixMatlab.csv"),
';',0.5,
false);
190 m_TrainingLabelMatrixY = matrixInt.first;
191 m_TestYPredict = matrixInt.second;
195 classifier->SetGamma(1/(
double)(m_TrainingMatrixX.cols()));
196 classifier->SetSvmType(0);
197 classifier->SetKernelType(0);
201 classifier->Train(m_TrainingMatrixX,m_TrainingLabelMatrixY);
202 Eigen::MatrixXi classes = classifier->Predict(m_TestXPredict);
206 unsigned int maxrows = classes.rows();
208 bool isYPredictVector =
false;
211 for(
int i= 0; i < maxrows; i++){
212 if(classes(i,0) == m_TestYPredict(i,0)){
213 isYPredictVector =
true;
217 MITK_INFO << 100*count/(double)(maxrows) <<
"%";
218 MITK_TEST_CONDITION(isEqual<int>(m_TestYPredict,classes),
"Expected vector and occured vector match.");
223 bool isEqual(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> expected, Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> actual)
225 bool isSimilar =
true;
226 unsigned int mrow = expected.rows();
227 unsigned int mcol = expected.cols();
228 for(
int i = 0; i < mrow; i++){
229 for(
int j = 0; j < mcol; j++){
230 if(expected(i,j) != actual(i,j)){
240 bool isIntervall(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> expected, Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> actual,
double lowrange,
double toprange)
242 bool isInIntervall =
false;
244 unsigned int rowRange = expected.rows();
245 unsigned int colRange = expected.cols();
246 for(
int i = 0; i < rowRange; i++){
247 for(
int j = 0; j < colRange; j++){
248 if(expected(i,j) == actual(i,j)){
253 double valueOfMatch = 100*count/(double)(rowRange);
254 if((lowrange <= valueOfMatch) && (toprange >= valueOfMatch)){
255 isInIntervall =
true;
258 return isInIntervall;
265 void TrainSVMClassifier_BreastCancerDataSet_shouldReturnTrue()
269 std::pair<MatrixDoubleType,MatrixDoubleType> matrixDouble;
270 matrixDouble = convertCSVToMatrix<double>(
GetTestDataFilePath(
"Classification/FeaturematrixBreastcancer.csv"),
';',0.5,
true);
271 m_TrainingMatrixX = matrixDouble.first;
272 m_TestXPredict = matrixDouble.second;
276 std::pair<MatrixIntType,MatrixIntType> matrixInt;
277 matrixInt = convertCSVToMatrix<int>(
GetTestDataFilePath(
"Classification/LabelmatrixBreastcancer.csv"),
';',0.5,
false);
278 m_TrainingLabelMatrixY = matrixInt.first;
279 m_TestYPredict = matrixInt.second;
283 classifier->SetGamma(1/(
double)(m_TrainingMatrixX.cols()));
284 classifier->SetSvmType(0);
285 classifier->SetKernelType(2);
289 classifier->Train(m_TrainingMatrixX,m_TrainingLabelMatrixY);
290 Eigen::MatrixXi classes = classifier->Predict(m_TestXPredict);
294 unsigned int maxrows = classes.rows();
296 bool isYPredictVector =
false;
299 for(
int i= 0; i < maxrows; i++){
300 if(classes(i,0) == m_TestYPredict(i,0)){
301 isYPredictVector =
true;
305 MITK_INFO << 100*count/(double)(maxrows) <<
"%";
306 MITK_TEST_CONDITION(isIntervall<int>(m_TestYPredict,classes,75,100),
"Testvalue is in range.");
309 void TestThreadedDecisionForest()
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.