<commit_before>/*=========================================================================

  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 "otbSamplingRateCalculator.h"
#include "otbStringUtils.h"
#include "otbMacro.h"
#include <sstream>
#include <fstream>
#include <iterator>
#include "itksys/SystemTools.hxx"


namespace otb
{

bool
SamplingRateCalculator::TripletType::operator==(const SamplingRateCalculator::TripletType & triplet) const
{
  return bool((Required == triplet.Required)||
              (Tot == triplet.Tot)||
              (Rate == triplet.Rate));
}

SamplingRateCalculator
::SamplingRateCalculator()
{
}

void 
SamplingRateCalculator
::SetMinimumNbOfSamplesByClass(void)
{
  unsigned long smallestNbofSamples = itk::NumericTraits<unsigned long>::max();
  MapRateType::iterator it = m_RatesByClass.begin();
  for (; it != m_RatesByClass.end() ; ++it)
    {
    if (smallestNbofSamples > it->second.Tot)
      {
      smallestNbofSamples = it->second.Tot;
      }
    }
  // Check if there is an empty class
  if (smallestNbofSamples == 0UL)
    {
    otbWarningMacro("There is an empty class, sample size is set to zero!");
    }
  this->SetNbOfSamplesAllClasses( smallestNbofSamples );
}

void 
SamplingRateCalculator
::SetNbOfSamplesAllClasses(unsigned long dRequiredNbSamples)
{
  MapRateType::iterator it = m_RatesByClass.begin();
  for (; it != m_RatesByClass.end() ; ++it)
    {
    it->second.Required = dRequiredNbSamples;
    this->UpdateRate(it->first);
    }
}

void 
SamplingRateCalculator
::SetNbOfSamplesByClass(const ClassCountMapType &required)
{
  ClassCountMapType::const_iterator it = required.begin();
  for (; it != required.end() ; ++it)
    {
    if (m_RatesByClass.count(it->first))<commit_msg>BUG: Mantis-1319: ignore empty classes<commit_after>/*=========================================================================

  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 "otbSamplingRateCalculator.h"
#include "otbStringUtils.h"
#include "otbMacro.h"
#include <sstream>
#include <fstream>
#include <iterator>
#include "itksys/SystemTools.hxx"


namespace otb
{

bool
SamplingRateCalculator::TripletType::operator==(const SamplingRateCalculator::TripletType & triplet) const
{
  return bool((Required == triplet.Required)||
              (Tot == triplet.Tot)||
              (Rate == triplet.Rate));
}

SamplingRateCalculator
::SamplingRateCalculator()
{
}

void 
SamplingRateCalculator
::SetMinimumNbOfSamplesByClass(void)
{
  unsigned long smallestNbofSamples = itk::NumericTraits<unsigned long>::max();
  MapRateType::iterator it = m_RatesByClass.begin();
  for (; it != m_RatesByClass.end() ; ++it)
    {
    if (it->second.Tot)
      {
      if (smallestNbofSamples > it->second.Tot)
        {
        smallestNbofSamples = it->second.Tot;
        }
      }
    else
      {
      otbWarningMacro("Ignoring empty class " << it->first);
      }
    }
  // Check if there is at least one non-empty class
  if (smallestNbofSamples == itk::NumericTraits<unsigned long>::max())
    {
    otbWarningMacro("There are only empty classes, sample size is set to zero!");
    smallestNbofSamples = 0UL;
    }
  this->SetNbOfSamplesAllClasses( smallestNbofSamples );
}

void 
SamplingRateCalculator
::SetNbOfSamplesAllClasses(unsigned long dRequiredNbSamples)
{
  MapRateType::iterator it = m_RatesByClass.begin();
  for (; it != m_RatesByClass.end() ; ++it)
    {
    if (it->second.Tot)
      {
      it->second.Required = dRequiredNbSamples;
      }
    else
      {
      // ignore empty classes
      it->second.Required = 0UL;
      }
    this->UpdateRate(it->first);
    }
}

void 
SamplingRateCalculator
::SetNbOfSamplesByClass(const ClassCountMapType &required)
{
  ClassCountMapType::const_iterator it = required.begin();
  for (; it != required.end() ; ++it)
    {
    if (m_RatesByClass.count(it->first))
{'fraction_non_alphanumeric': 0.16989066442388562, 'fraction_numerical': 0.001471825063078217, 'mean_word_length': 4.012644889357218, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2140231', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1447, 'n_words': 329}