From c1227031c492877cbeb9a4f9f0b87a608ef56687 Mon Sep 17 00:00:00 2001
From: Arnaud Jaen <arnaud.jaen@c-s.fr>
Date: Fri, 22 Mar 2013 17:00:25 +0100
Subject: [PATCH] TEST: Add tests on CanReadFile method of MachineLearningModel
 derived classes

---
 Testing/Code/Learning/CMakeLists.txt          |  44 +++++++
 Testing/Code/Learning/otbLearningTests6.cxx   |   5 +
 .../otbMachineLearningModelCanRead.cxx        | 109 ++++++++++++++++++
 3 files changed, 158 insertions(+)
 create mode 100644 Testing/Code/Learning/otbMachineLearningModelCanRead.cxx

diff --git a/Testing/Code/Learning/CMakeLists.txt b/Testing/Code/Learning/CMakeLists.txt
index 4e401ce909..b95d0657f8 100644
--- a/Testing/Code/Learning/CMakeLists.txt
+++ b/Testing/Code/Learning/CMakeLists.txt
@@ -724,6 +724,48 @@ IF(OTB_USE_OPENCV)
 	     ${INPUTDATA}/letter.scale
 	     ${TEMP}/libsvm_model.txt
      )
+     
+     ADD_TEST(leTuImageClassificationFilterNew ${LEARNING_TESTS6}
+       	 otbImageClassificationFilterNew)
+       	 
+     ADD_TEST(leTvImageClassificationFilterLibSVM ${LEARNING_TESTS6}
+     	--compare-image ${NOTOL}
+       	 ${BASELINE}/leSVMImageClassificationFilterOutput.tif
+		 ${TEMP}/leImageClassificationFilterLibSVMOutput.tif
+		 otbImageClassificationFilter
+		 ${INPUTDATA}/ROI_QB_MUL_4.tif
+		 ${INPUTDATA}/svm_model_image
+		 ${TEMP}/leImageClassificationFilterLibSVMOutput.tif
+       	 )
+       	 
+     ADD_TEST(leTvImageClassificationFilterSVM ${LEARNING_TESTS6}
+     	--compare-image ${NOTOL}
+       	 ${BASELINE}/leImageClassificationFilterSVMOutput.tif
+		 ${TEMP}/leImageClassificationFilterSVMOutput.tif
+		 otbImageClassificationFilter
+		 ${INPUTDATA}/ROI_QB_MUL_4.tif
+		 ${INPUTDATA}/ROI_QB_MUL_4_svmModel.txt
+		 ${TEMP}/leImageClassificationFilterSVMOutput.tif
+       	 )
+       	 
+     ADD_TEST(leTuLibSVMMachineLearningModelCanRead ${LEARNING_TESTS6}
+       	 otbLibSVMMachineLearningModelCanRead
+       	 ${INPUTDATA}/svm_model_image
+       	 )
+       	 
+     ADD_TEST(leTuSVMMachineLearningModelCanRead ${LEARNING_TESTS6}
+       	 otbSVMMachineLearningModelCanRead
+       	 ${INPUTDATA}/svm_model_image
+       	 )
+     
+     ADD_TEST(leTuRandomForestsMachineLearningModelCanRead ${LEARNING_TESTS6}
+       	 otbRandomForestsMachineLearningModelCanRead
+       	 ${TEMP}/RandomForestsMachineLearningModel.txt
+       	 )
+     SET_TESTS_PROPERTIES(leTuRandomForestsMachineLearningModelCanRead
+     					PROPERTIES DEPENDS leTvRandomForestsMachineLearningModel)
+
+     
 ENDIF(OTB_USE_OPENCV)
 
 # Testing srcs
@@ -811,6 +853,8 @@ IF(OTB_USE_OPENCV)
 	SET(BasicLearning_SRCS6
 	otbLearningTests6.cxx
 	otbTrainMachineLearningModel.cxx
+	otbImageClassificationFilter.cxx
+	otbMachineLearningModelCanRead.cxx
 	)
 ENDIF(OTB_USE_OPENCV)
 
diff --git a/Testing/Code/Learning/otbLearningTests6.cxx b/Testing/Code/Learning/otbLearningTests6.cxx
index 7643f2c0b0..48d7f42b8c 100644
--- a/Testing/Code/Learning/otbLearningTests6.cxx
+++ b/Testing/Code/Learning/otbLearningTests6.cxx
@@ -32,4 +32,9 @@ void RegisterTests()
   REGISTER_TEST(otbKNearestNeighborsMachineLearningModel);
   REGISTER_TEST(otbRandomForestsMachineLearningModelNew);
   REGISTER_TEST(otbRandomForestsMachineLearningModel);
+  REGISTER_TEST(otbImageClassificationFilterNew);
+  REGISTER_TEST(otbImageClassificationFilter);
+  REGISTER_TEST(otbLibSVMMachineLearningModelCanRead);
+  REGISTER_TEST(otbSVMMachineLearningModelCanRead);
+  REGISTER_TEST(otbRandomForestsMachineLearningModelCanRead);
 }
diff --git a/Testing/Code/Learning/otbMachineLearningModelCanRead.cxx b/Testing/Code/Learning/otbMachineLearningModelCanRead.cxx
new file mode 100644
index 0000000000..daf4f76c4c
--- /dev/null
+++ b/Testing/Code/Learning/otbMachineLearningModelCanRead.cxx
@@ -0,0 +1,109 @@
+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+
+#include "otbMachineLearningModel.h"
+#include "otbLibSVMMachineLearningModel.h"
+#include "otbSVMMachineLearningModel.h"
+#include "otbRandomForestsMachineLearningModel.h"
+#include <iostream>
+
+typedef otb::MachineLearningModel<float,short>         MachineLearningModelType;
+typedef MachineLearningModelType::InputValueType       InputValueType;
+typedef MachineLearningModelType::InputSampleType      InputSampleType;
+typedef MachineLearningModelType::InputListSampleType  InputListSampleType;
+typedef MachineLearningModelType::TargetValueType      TargetValueType;
+typedef MachineLearningModelType::TargetSampleType     TargetSampleType;
+typedef MachineLearningModelType::TargetListSampleType TargetListSampleType;
+
+int otbLibSVMMachineLearningModelCanRead(int argc, char* argv[])
+{
+  if (argc != 2)
+    {
+    std::cerr << "Usage: " << argv[0]
+              << "<model>" << std::endl;
+    std::cerr << "Called here with " << argc << " arguments\n";
+    for (int i = 1; i < argc; ++i)
+      {
+      std::cerr << " - " << argv[i] << "\n";
+      }
+    return EXIT_FAILURE;
+    }
+  std::string filename(argv[1]);
+  typedef otb::LibSVMMachineLearningModel<InputValueType, TargetValueType> SVMType;
+  SVMType::Pointer classifier = SVMType::New();
+  bool lCanRead = classifier->CanReadFile(filename);
+  if (lCanRead == false)
+    {
+    std::cerr << "Erreur otb::LibSVMMachineLearningModel : impossible to open the file " << filename << "." << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}
+
+int otbSVMMachineLearningModelCanRead(int argc, char* argv[])
+{
+  if (argc != 2)
+    {
+    std::cerr << "Usage: " << argv[0]
+              << "<model>" << std::endl;
+    std::cerr << "Called here with " << argc << " arguments\n";
+    for (int i = 1; i < argc; ++i)
+      {
+      std::cerr << " - " << argv[i] << "\n";
+      }
+    return EXIT_FAILURE;
+    }
+  std::string filename(argv[1]);
+  typedef otb::SVMMachineLearningModel<InputValueType, TargetValueType> SVMType;
+  SVMType::Pointer classifier = SVMType::New();
+  bool lCanRead = classifier->CanReadFile(filename);
+  if (lCanRead == false)
+    {
+    std::cerr << "Erreur otb::SVMMachineLearningModel : impossible to open the file " << filename << "." << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}
+
+int otbRandomForestsMachineLearningModelCanRead(int argc, char* argv[])
+{
+  if (argc != 2)
+    {
+    std::cerr << "Usage: " << argv[0]
+              << "<model>" << std::endl;
+    std::cerr << "Called here with " << argc << " arguments\n";
+    for (int i = 1; i < argc; ++i)
+      {
+      std::cerr << " - " << argv[i] << "\n";
+      }
+    return EXIT_FAILURE;
+    }
+  std::string filename(argv[1]);
+  typedef otb::RandomForestsMachineLearningModel<InputValueType, TargetValueType> RFType;
+  RFType::Pointer classifier = RFType::New();
+  bool lCanRead = classifier->CanReadFile(filename);
+  if (lCanRead == false)
+    {
+    std::cerr << "Erreur otb::RandomForestsMachineLearningModel : impossible to open the file " << filename << "." << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}
-- 
GitLab