Skip to content
Snippets Groups Projects
Commit 65b1b09b authored by Julien Michel's avatar Julien Michel
Browse files

ENH: Adding a sampling strategy to set the total number of samples to be used...

ENH: Adding a sampling strategy to set the total number of samples to be used for all classes, using class proportions
parent 4e3cc88f
No related branches found
No related tags found
No related merge requests found
......@@ -174,6 +174,14 @@ private:
SetMinimumParameterFloatValue("strategy.percent.p",0);
SetMaximumParameterFloatValue("strategy.percent.p",1);
SetDefaultParameterFloat("strategy.percent.p",0.5);
AddChoice("strategy.total","Set the total number of samples to generate, and use class proportions.");
SetParameterDescription("strategy.total","Set the total number of samples to generate, and use class proportions.");
AddParameter(ParameterType_Int,"strategy.total.v","The number of samples to generate");
SetParameterDescription("strategy.total.v","The number of samples to generate");
SetMinimumParameterIntValue("strategy.total.v",1);
SetDefaultParameterInt("strategy.total.v",1000);
AddChoice("strategy.smallest","Set same number of samples for all classes, with the smallest class fully sampled");
SetParameterDescription("strategy.smallest","Set same number of samples for all classes, with the smallest class fully sampled");
......@@ -246,19 +254,27 @@ private:
// percent
case 2:
{
otbAppLogINFO("Sampluing strategy: set a percentage of samples for each class.");
otbAppLogINFO("Sampling strategy: set a percentage of samples for each class.");
m_RateCalculator->SetPercentageOfSamples(this->GetParameterFloat("strategy.percent.p"));
}
break;
// smallest class
// total
case 3:
{
otbAppLogINFO("Sampling strategy: set the total number of samples to generate, use classes proportions.");
m_RateCalculator->SetTotalNumberOfSamples(this->GetParameterInt("strategy.total.v"));
}
break;
// smallest class
case 4:
{
otbAppLogINFO("Sampling strategy : fit the number of samples based on the smallest class");
m_RateCalculator->SetMinimumNbOfSamplesByClass();
}
break;
// all samples
case 4:
case 5:
{
otbAppLogINFO("Sampling strategy : take all samples");
m_RateCalculator->SetAllSamples();
......
......@@ -70,6 +70,9 @@ public:
/** Method to set a percentage of samples for each class */
void SetPercentageOfSamples(double percent);
/** Method to set the total number of samples to generate */
void SetTotalNumberOfSamples(unsigned long value);
/** Method to choose a sampling strategy based on the smallest class.
* The number of samples in each class is set to this minimum size*/
......
......@@ -120,7 +120,28 @@ void SamplingRateCalculator
it->second.Required = static_cast<unsigned long>(vcl_floor(0.5+percent * it->second.Tot));
it->second.Rate = percent;
}
}
void SamplingRateCalculator
::SetTotalNumberOfSamples(unsigned long value)
{
// First, get total number of samples
unsigned long totalNumberOfSamplesAvailable = 0;
MapRateType::iterator it = m_RatesByClass.begin();
for (; it != m_RatesByClass.end() ; ++it)
{
totalNumberOfSamplesAvailable+=it->second.Tot;
}
// Then compute number of samples for each class
for (it = m_RatesByClass.begin(); it != m_RatesByClass.end() ; ++it)
{
double ratio = it->second.Tot / static_cast<double>(totalNumberOfSamplesAvailable);
it->second.Required = static_cast<unsigned long>(0.5+ratio*value);
this->UpdateRate(it->first);
}
}
void
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment