From 615ed9d0cc42b36501e22773f24280732db708b1 Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Fri, 17 Jul 2015 16:51:50 +0200
Subject: [PATCH] ENH: update classifiers to new base class
 MachineLearningModel

---
 .../Supervised/include/otbBoostMachineLearningModel.h      | 3 ++-
 .../Supervised/include/otbBoostMachineLearningModel.txx    | 7 ++++++-
 .../include/otbDecisionTreeMachineLearningModel.h          | 3 ++-
 .../include/otbDecisionTreeMachineLearningModel.txx        | 7 ++++++-
 .../include/otbGradientBoostedTreeMachineLearningModel.h   | 3 ++-
 .../include/otbGradientBoostedTreeMachineLearningModel.txx | 7 ++++++-
 .../include/otbKNearestNeighborsMachineLearningModel.h     | 3 ++-
 .../include/otbKNearestNeighborsMachineLearningModel.txx   | 7 ++++++-
 .../Supervised/include/otbLibSVMMachineLearningModel.h     | 3 ++-
 .../Supervised/include/otbLibSVMMachineLearningModel.txx   | 7 ++++++-
 .../include/otbNeuralNetworkMachineLearningModel.h         | 3 ++-
 .../include/otbNeuralNetworkMachineLearningModel.txx       | 7 ++++++-
 .../include/otbNormalBayesMachineLearningModel.h           | 3 ++-
 .../include/otbNormalBayesMachineLearningModel.txx         | 7 ++++++-
 .../include/otbRandomForestsMachineLearningModel.h         | 3 ++-
 .../include/otbRandomForestsMachineLearningModel.txx       | 7 ++++++-
 .../Supervised/include/otbSVMMachineLearningModel.h        | 3 ++-
 .../Supervised/include/otbSVMMachineLearningModel.txx      | 7 ++++++-
 18 files changed, 72 insertions(+), 18 deletions(-)

diff --git a/Modules/Learning/Supervised/include/otbBoostMachineLearningModel.h b/Modules/Learning/Supervised/include/otbBoostMachineLearningModel.h
index 1aa14608bb..185d10ba44 100644
--- a/Modules/Learning/Supervised/include/otbBoostMachineLearningModel.h
+++ b/Modules/Learning/Supervised/include/otbBoostMachineLearningModel.h
@@ -46,6 +46,7 @@ public:
   typedef typename Superclass::TargetValueType            TargetValueType;
   typedef typename Superclass::TargetSampleType           TargetSampleType;
   typedef typename Superclass::TargetListSampleType       TargetListSampleType;
+  typedef typename Superclass::ConfidenceValueType        ConfidenceValueType;
 
   /** Run-time type information (and related methods). */
   itkNewMacro(Self);
@@ -119,7 +120,7 @@ protected:
   /** Train the machine learning model */
   virtual void TrainClassification();
   /** Predict values using the model */
-  virtual TargetSampleType PredictClassification(const InputSampleType& input) const;
+  virtual TargetSampleType PredictClassification(const InputSampleType& input, ConfidenceValueType *quality=NULL) const;
 
 private:
   BoostMachineLearningModel(const Self &); //purposely not implemented
diff --git a/Modules/Learning/Supervised/include/otbBoostMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbBoostMachineLearningModel.txx
index 825b119fc6..a3cdd68712 100644
--- a/Modules/Learning/Supervised/include/otbBoostMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbBoostMachineLearningModel.txx
@@ -76,7 +76,7 @@ template <class TInputValue, class TOutputValue>
 typename BoostMachineLearningModel<TInputValue,TOutputValue>
 ::TargetSampleType
 BoostMachineLearningModel<TInputValue,TOutputValue>
-::PredictClassification(const InputSampleType & input) const
+::PredictClassification(const InputSampleType & input, ConfidenceValueType *quality) const
 {
   //convert listsample to Mat
   cv::Mat sample;
@@ -91,6 +91,11 @@ BoostMachineLearningModel<TInputValue,TOutputValue>
 
   target[0] = static_cast<TOutputValue>(result);
 
+  if (quality != NULL)
+    {
+    itkExceptionMacro("Confidence index not available for this classifier !");
+    }
+
   return target;
 }
 
diff --git a/Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.h b/Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.h
index 4e73de9e84..6415fa940e 100644
--- a/Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.h
+++ b/Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.h
@@ -46,6 +46,7 @@ public:
   typedef typename Superclass::TargetValueType            TargetValueType;
   typedef typename Superclass::TargetSampleType           TargetSampleType;
   typedef typename Superclass::TargetListSampleType       TargetListSampleType;
+  typedef typename Superclass::ConfidenceValueType        ConfidenceValueType;
 
   /** Run-time type information (and related methods). */
   itkNewMacro(Self);
@@ -180,7 +181,7 @@ protected:
   /** Train the machine learning model */
   virtual void TrainClassification();
   /** Predict values using the model */
-  virtual TargetSampleType PredictClassification(const InputSampleType& input) const;
+  virtual TargetSampleType PredictClassification(const InputSampleType& input, ConfidenceValueType *quality=NULL) const;
 
 private:
   DecisionTreeMachineLearningModel(const Self &); //purposely not implemented
diff --git a/Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.txx
index ef98de235b..5edf838c74 100644
--- a/Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbDecisionTreeMachineLearningModel.txx
@@ -83,7 +83,7 @@ template <class TInputValue, class TOutputValue>
 typename DecisionTreeMachineLearningModel<TInputValue,TOutputValue>
 ::TargetSampleType
 DecisionTreeMachineLearningModel<TInputValue,TOutputValue>
-::PredictClassification(const InputSampleType & input) const
+::PredictClassification(const InputSampleType & input, ConfidenceValueType *quality) const
 {
   //convert listsample to Mat
   cv::Mat sample;
@@ -96,6 +96,11 @@ DecisionTreeMachineLearningModel<TInputValue,TOutputValue>
 
   target[0] = static_cast<TOutputValue>(result);
 
+  if (quality != NULL)
+    {
+    itkExceptionMacro("Confidence index not available for this classifier !");
+    }
+
   return target;
 }
 
diff --git a/Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.h b/Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.h
index e371eaf877..3126f44c0f 100644
--- a/Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.h
+++ b/Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.h
@@ -46,6 +46,7 @@ public:
   typedef typename Superclass::TargetValueType            TargetValueType;
   typedef typename Superclass::TargetSampleType           TargetSampleType;
   typedef typename Superclass::TargetListSampleType       TargetListSampleType;
+  typedef typename Superclass::ConfidenceValueType        ConfidenceValueType;
 
   /** Run-time type information (and related methods). */
   itkNewMacro(Self);
@@ -127,7 +128,7 @@ protected:
   /** Train the machine learning model */
   virtual void TrainClassification();
   /** Predict values using the model */
-  virtual TargetSampleType PredictClassification(const InputSampleType& input) const;
+  virtual TargetSampleType PredictClassification(const InputSampleType& input, ConfidenceValueType *quality=NULL) const;
 
 private:
   GradientBoostedTreeMachineLearningModel(const Self &); //purposely not implemented
diff --git a/Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.txx
index c7421a55b1..98adf028f2 100644
--- a/Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbGradientBoostedTreeMachineLearningModel.txx
@@ -80,7 +80,7 @@ template <class TInputValue, class TOutputValue>
 typename GradientBoostedTreeMachineLearningModel<TInputValue,TOutputValue>
 ::TargetSampleType
 GradientBoostedTreeMachineLearningModel<TInputValue,TOutputValue>
-::PredictClassification(const InputSampleType & input) const
+::PredictClassification(const InputSampleType & input, ConfidenceValueType *quality) const
 {
   //convert listsample to Mat
   cv::Mat sample;
@@ -93,6 +93,11 @@ GradientBoostedTreeMachineLearningModel<TInputValue,TOutputValue>
 
   target[0] = static_cast<TOutputValue>(result);
 
+  if (quality != NULL)
+    {
+    itkExceptionMacro("Confidence index not available for this classifier !");
+    }
+
   return target;
 }
 
diff --git a/Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.h b/Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.h
index 8b9ef1670f..b8524b7fb6 100644
--- a/Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.h
+++ b/Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.h
@@ -45,6 +45,7 @@ public:
   typedef typename Superclass::TargetValueType            TargetValueType;
   typedef typename Superclass::TargetSampleType           TargetSampleType;
   typedef typename Superclass::TargetListSampleType       TargetListSampleType;
+  typedef typename Superclass::ConfidenceValueType        ConfidenceValueType;
 
   /** Run-time type information (and related methods). */
   itkNewMacro(Self);
@@ -92,7 +93,7 @@ protected:
   /** Train the machine learning model */
   virtual void TrainClassification();
   /** Predict values using the model */
-  virtual TargetSampleType PredictClassification(const InputSampleType& input) const;
+  virtual TargetSampleType PredictClassification(const InputSampleType& input, ConfidenceValueType *quality=NULL) const;
 
 private:
   KNearestNeighborsMachineLearningModel(const Self &); //purposely not implemented
diff --git a/Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.txx
index c1048f6360..9c36f67418 100644
--- a/Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbKNearestNeighborsMachineLearningModel.txx
@@ -66,7 +66,7 @@ template <class TInputValue, class TTargetValue>
 typename KNearestNeighborsMachineLearningModel<TInputValue,TTargetValue>
 ::TargetSampleType
 KNearestNeighborsMachineLearningModel<TInputValue,TTargetValue>
-::PredictClassification(const InputSampleType & input) const
+::PredictClassification(const InputSampleType & input, ConfidenceValueType *quality) const
 {
   //convert listsample to Mat
   cv::Mat sample;
@@ -78,6 +78,11 @@ KNearestNeighborsMachineLearningModel<TInputValue,TTargetValue>
 
   target[0] = static_cast<TTargetValue>(result);
 
+  if (quality != NULL)
+    {
+    itkExceptionMacro("Confidence index not available for this classifier !");
+    }
+
   return target;
 }
 
diff --git a/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.h b/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.h
index d3ea001002..18eba7fbb2 100644
--- a/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.h
+++ b/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.h
@@ -46,6 +46,7 @@ public:
   typedef typename Superclass::TargetValueType            TargetValueType;
   typedef typename Superclass::TargetSampleType           TargetSampleType;
   typedef typename Superclass::TargetListSampleType       TargetListSampleType;
+  typedef typename Superclass::ConfidenceValueType        ConfidenceValueType;
 
   // LibSVM related typedefs
   typedef otb::Functor::VariableLengthVectorToMeasurementVectorFunctor<InputSampleType> MeasurementVectorFunctorType;
@@ -105,7 +106,7 @@ protected:
   /** Train the machine learning model */
   virtual void TrainClassification();
   /** Predict values using the model */
-  virtual TargetSampleType PredictClassification(const InputSampleType& input) const;
+  virtual TargetSampleType PredictClassification(const InputSampleType& input, ConfidenceValueType *quality=NULL) const;
 
 private:
   LibSVMMachineLearningModel(const Self &); //purposely not implemented
diff --git a/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.txx
index 520441d9c2..adb725294c 100644
--- a/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.txx
@@ -80,7 +80,7 @@ template <class TInputValue, class TOutputValue>
 typename LibSVMMachineLearningModel<TInputValue,TOutputValue>
 ::TargetSampleType
 LibSVMMachineLearningModel<TInputValue,TOutputValue>
-::PredictClassification(const InputSampleType & input) const
+::PredictClassification(const InputSampleType & input, ConfidenceValueType *quality) const
 {
   TargetSampleType target;
 
@@ -90,6 +90,11 @@ LibSVMMachineLearningModel<TInputValue,TOutputValue>
 
   target = m_SVMestimator->GetModel()->EvaluateLabel(mfunctor(input));
 
+  if (quality != NULL)
+    {
+    itkExceptionMacro("Confidence index not available for this classifier !");
+    }
+
   return target;
 }
 
diff --git a/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.h b/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.h
index 9bf28f9def..2eab72042d 100644
--- a/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.h
+++ b/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.h
@@ -46,6 +46,7 @@ public:
   typedef typename Superclass::TargetValueType            TargetValueType;
   typedef typename Superclass::TargetSampleType           TargetSampleType;
   typedef typename Superclass::TargetListSampleType       TargetListSampleType;
+  typedef typename Superclass::ConfidenceValueType        ConfidenceValueType;
 
   typedef std::map<TargetValueType, unsigned int>         MapOfLabelsType;
 
@@ -180,7 +181,7 @@ protected:
   /** Train the machine learning model */
   virtual void TrainClassification();
   /** Predict values using the model */
-  virtual TargetSampleType PredictClassification(const InputSampleType& input) const;
+  virtual TargetSampleType PredictClassification(const InputSampleType& input, ConfidenceValueType *quality=NULL) const;
 
 private:
   NeuralNetworkMachineLearningModel(const Self &); //purposely not implemented
diff --git a/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.txx
index 95b4c1502f..dabcf5dbcb 100644
--- a/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbNeuralNetworkMachineLearningModel.txx
@@ -169,7 +169,7 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TrainClassifi
 
 template<class TInputValue, class TOutputValue>
 typename NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TargetSampleType NeuralNetworkMachineLearningModel<
-  TInputValue, TOutputValue>::PredictClassification(const InputSampleType & input) const
+  TInputValue, TOutputValue>::PredictClassification(const InputSampleType & input, ConfidenceValueType *quality) const
 {
   //convert listsample to Mat
   cv::Mat sample;
@@ -195,6 +195,11 @@ typename NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TargetSam
       }
     }
 
+  if (quality != NULL)
+    {
+    itkExceptionMacro("Confidence index not available for this classifier !");
+    }
+
   return target;
 }
 
diff --git a/Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.h b/Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.h
index a1badef39d..31b5373ce1 100644
--- a/Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.h
+++ b/Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.h
@@ -46,6 +46,7 @@ public:
   typedef typename Superclass::TargetValueType            TargetValueType;
   typedef typename Superclass::TargetSampleType           TargetSampleType;
   typedef typename Superclass::TargetListSampleType       TargetListSampleType;
+  typedef typename Superclass::ConfidenceValueType        ConfidenceValueType;
 
   /** Run-time type information (and related methods). */
   itkNewMacro(Self);
@@ -79,7 +80,7 @@ protected:
   /** Train the machine learning model */
   virtual void TrainClassification();
   /** Predict values using the model */
-  virtual TargetSampleType PredictClassification(const InputSampleType& input) const;
+  virtual TargetSampleType PredictClassification(const InputSampleType& input, ConfidenceValueType *quality=NULL) const;
 
 private:
   NormalBayesMachineLearningModel(const Self &); //purposely not implemented
diff --git a/Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.txx
index 1e9d8f1721..89d4e4e8fa 100644
--- a/Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbNormalBayesMachineLearningModel.txx
@@ -61,7 +61,7 @@ template <class TInputValue, class TOutputValue>
 typename NormalBayesMachineLearningModel<TInputValue,TOutputValue>
 ::TargetSampleType
 NormalBayesMachineLearningModel<TInputValue,TOutputValue>
-::PredictClassification(const InputSampleType & input) const
+::PredictClassification(const InputSampleType & input, ConfidenceValueType *quality) const
 {
   //convert listsample to Mat
   cv::Mat sample;
@@ -76,6 +76,11 @@ NormalBayesMachineLearningModel<TInputValue,TOutputValue>
 
   target[0] = static_cast<TOutputValue>(result);
 
+  if (quality != NULL)
+    {
+    itkExceptionMacro("Confidence index not available for this classifier !");
+    }
+
   return target;
 }
 
diff --git a/Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.h b/Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.h
index 8d889a0978..20aff52a7e 100644
--- a/Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.h
+++ b/Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.h
@@ -46,6 +46,7 @@ public:
   typedef typename Superclass::TargetValueType            TargetValueType;
   typedef typename Superclass::TargetSampleType           TargetSampleType;
   typedef typename Superclass::TargetListSampleType       TargetListSampleType;
+  typedef typename Superclass::ConfidenceValueType        ConfidenceValueType;
   
   // Other
   typedef itk::VariableSizeMatrix<float>                VariableImportanceMatrixType;
@@ -141,7 +142,7 @@ protected:
   /** Train the machine learning model */
   virtual void TrainClassification();
   /** Predict values using the model */
-  virtual TargetSampleType PredictClassification(const InputSampleType& input) const;
+  virtual TargetSampleType PredictClassification(const InputSampleType& input, ConfidenceValueType *quality=NULL) const;
 
 private:
   RandomForestsMachineLearningModel(const Self &); //purposely not implemented
diff --git a/Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.txx
index 9c06e330a2..e295d30c9a 100644
--- a/Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbRandomForestsMachineLearningModel.txx
@@ -109,7 +109,7 @@ template <class TInputValue, class TOutputValue>
 typename RandomForestsMachineLearningModel<TInputValue,TOutputValue>
 ::TargetSampleType
 RandomForestsMachineLearningModel<TInputValue,TOutputValue>
-::PredictClassification(const InputSampleType & value) const
+::PredictClassification(const InputSampleType & value, ConfidenceValueType *quality) const
 {
   //convert listsample to Mat
   cv::Mat sample;
@@ -122,6 +122,11 @@ RandomForestsMachineLearningModel<TInputValue,TOutputValue>
 
   target[0] = static_cast<TOutputValue>(result);
 
+  if (quality != NULL)
+    {
+    itkExceptionMacro("Confidence index not available for this classifier !");
+    }
+
   return target[0];
 }
 
diff --git a/Modules/Learning/Supervised/include/otbSVMMachineLearningModel.h b/Modules/Learning/Supervised/include/otbSVMMachineLearningModel.h
index 5dca5da4f6..406bdd10e6 100644
--- a/Modules/Learning/Supervised/include/otbSVMMachineLearningModel.h
+++ b/Modules/Learning/Supervised/include/otbSVMMachineLearningModel.h
@@ -45,6 +45,7 @@ public:
   typedef typename Superclass::TargetValueType            TargetValueType;
   typedef typename Superclass::TargetSampleType           TargetSampleType;
   typedef typename Superclass::TargetListSampleType       TargetListSampleType;
+  typedef typename Superclass::ConfidenceValueType        ConfidenceValueType;
 
   /** Run-time type information (and related methods). */
   itkNewMacro(Self);
@@ -128,7 +129,7 @@ protected:
   /** Train the machine learning model */
   virtual void TrainClassification();
   /** Predict values using the model */
-  virtual TargetSampleType PredictClassification(const InputSampleType& input) const;
+  virtual TargetSampleType PredictClassification(const InputSampleType& input, ConfidenceValueType *quality=NULL) const;
 
 private:
   SVMMachineLearningModel(const Self &); //purposely not implemented
diff --git a/Modules/Learning/Supervised/include/otbSVMMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbSVMMachineLearningModel.txx
index beca36102a..3c5c6c228e 100644
--- a/Modules/Learning/Supervised/include/otbSVMMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbSVMMachineLearningModel.txx
@@ -108,7 +108,7 @@ template <class TInputValue, class TOutputValue>
 typename SVMMachineLearningModel<TInputValue,TOutputValue>
 ::TargetSampleType
 SVMMachineLearningModel<TInputValue,TOutputValue>
-::PredictClassification(const InputSampleType & input) const
+::PredictClassification(const InputSampleType & input, ConfidenceValueType *quality) const
 {
   //convert listsample to Mat
   cv::Mat sample;
@@ -121,6 +121,11 @@ SVMMachineLearningModel<TInputValue,TOutputValue>
 
   target[0] = static_cast<TOutputValue>(result);
 
+  if (quality != NULL)
+    {
+    itkExceptionMacro("Confidence index not available for this classifier !");
+    }
+
   return target;
 }
 
-- 
GitLab