From 23129321b6774fac505cbbc556b082d12aea0e7f Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Mon, 9 Jan 2017 17:17:50 +0100
Subject: [PATCH] BUG: Mantis-1319: ignore empty classes

---
 .../src/otbSamplingRateCalculator.cxx         | 28 +++++++++++++++----
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/Modules/Learning/Sampling/src/otbSamplingRateCalculator.cxx b/Modules/Learning/Sampling/src/otbSamplingRateCalculator.cxx
index 121ed2b588..934e2d480a 100644
--- a/Modules/Learning/Sampling/src/otbSamplingRateCalculator.cxx
+++ b/Modules/Learning/Sampling/src/otbSamplingRateCalculator.cxx
@@ -50,15 +50,23 @@ SamplingRateCalculator
   MapRateType::iterator it = m_RatesByClass.begin();
   for (; it != m_RatesByClass.end() ; ++it)
     {
-    if (smallestNbofSamples > it->second.Tot)
+    if (it->second.Tot)
       {
-      smallestNbofSamples = it->second.Tot;
+      if (smallestNbofSamples > it->second.Tot)
+        {
+        smallestNbofSamples = it->second.Tot;
+        }
+      }
+    else
+      {
+      otbWarningMacro("Ignoring empty class " << it->first);
       }
     }
-  // Check if there is an empty class
-  if (smallestNbofSamples == 0UL)
+  // Check if there is at least one non-empty class
+  if (smallestNbofSamples == itk::NumericTraits<unsigned long>::max())
     {
-    otbWarningMacro("There is an empty class, sample size is set to zero!");
+    otbWarningMacro("There are only empty classes, sample size is set to zero!");
+    smallestNbofSamples = 0UL;
     }
   this->SetNbOfSamplesAllClasses( smallestNbofSamples );
 }
@@ -70,7 +78,15 @@ SamplingRateCalculator
   MapRateType::iterator it = m_RatesByClass.begin();
   for (; it != m_RatesByClass.end() ; ++it)
     {
-    it->second.Required = dRequiredNbSamples;
+    if (it->second.Tot)
+      {
+      it->second.Required = dRequiredNbSamples;
+      }
+    else
+      {
+      // ignore empty classes
+      it->second.Required = 0UL;
+      }
     this->UpdateRate(it->first);
     }
 }
-- 
GitLab