Commit 4fcf55ff authored by Julien Michel's avatar Julien Michel
Browse files

TEST: Add unit tests for all radiometric indices and for new classes

No related merge requests found
Showing with 303 additions and 1 deletion
+303 -1
......@@ -22,7 +22,7 @@ otb_module_test()
set(OTBIndicesTests
otbIndicesTestDriver.cxx
otbIndices.cxx
otbRadiometricIndicesTest.cxx
otbNDVIDataNodeFeatureFunction.cxx
otbLandsatTMIndexNDSITest.cxx
otbLandsatTMIndexBIOTest.cxx
......@@ -342,5 +342,20 @@ otb_add_test(NAME raTvLandsatTMThickCloudTest COMMAND otbIndicesTestDriver
)
otb_add_test(NAME raTvRadiometricIndicesBaseClassTest COMMAND otbIndicesTestDriver
otbRadiometricIndiceTest)
otb_add_test(NAME raTvVegetationIndicesTest COMMAND otbIndicesTestDriver
otbVegetationIndicesTest)
otb_add_test(NAME raTvWaterIndicesTest COMMAND otbIndicesTestDriver
otbWaterIndicesTest)
otb_add_test(NAME raTvBuiltUpIndicesTest COMMAND otbIndicesTestDriver
otbBuiltUpIndicesTest)
otb_add_test(NAME raTvSoilIndicesTest COMMAND otbIndicesTestDriver
otbSoilIndicesTest)
otb_add_test(NAME raTvIndicesStackFunctorTest COMMAND otbIndicesTestDriver
otbIndicesStackFunctorTest)
......@@ -47,4 +47,9 @@ void RegisterTests()
REGISTER_TEST(otbLandsatTMIndexNDBSI);
REGISTER_TEST(otbLandsatTMThickCloudTest);
REGISTER_TEST(otbVegetationIndicesTest);
REGISTER_TEST(otbWaterIndicesTest);
REGISTER_TEST(otbBuiltUpIndicesTest);
REGISTER_TEST(otbSoilIndicesTest);
REGISTER_TEST(otbRadiometricIndiceTest);
REGISTER_TEST(otbIndicesStackFunctorTest);
}
......@@ -19,6 +19,11 @@
*/
#include "otbVegetationIndicesFunctor.h"
#include "otbWaterIndicesFunctor.h"
#include "otbBuiltUpIndicesFunctor.h"
#include "otbSoilIndicesFunctor.h"
#include "otbIndicesStackFunctor.h"
#include <iomanip>
template <typename T> itk::VariableLengthVector<T> build_pixel(const std::initializer_list<T> & il)
......@@ -53,7 +58,6 @@ template <class TIndice> bool CheckResult(const std::string & testName, std::map
}
else
{
std::cerr<<testName <<"\t- success"<<std::endl;
return true;
}
}
......@@ -88,7 +92,38 @@ int otbVegetationIndicesTest(int, char**)
res = res & CheckResult< IPVI<int,double> >("ipvi_pixel",bandMap,{0,0,1,2},0.6666666667);
res = res & CheckResult< LAIFromNDVILogarithmic<int,double> >("lailog_pixel",bandMap,{0,0,1,2},0.4930511672);
res = res & CheckResult< LAIFromReflectancesLinear<int,double> >("lailog_pixel",bandMap,{0,0,1,2},6.61);
res = res & CheckResult< LAIFromNDVIFormosat2Functor<int,double> >("laiform_pixel",bandMap,{0,0,1,2},-0.08836147592);
res = res & CheckResult< LAIFromNDVIFormosat2Functor<int,double> >("laifrom_pixel",bandMap,{0,0,1,2},-0.08836147592);
if(res)
{
return EXIT_SUCCESS;
}
else
{
return EXIT_FAILURE;
}
}
int otbWaterIndicesTest(int, char**)
{
const std::map<CommonBandNames,size_t> bandMap = {{CommonBandNames::BLUE,0},{CommonBandNames::GREEN,1},{CommonBandNames::RED,2},{CommonBandNames::NIR,3}, {CommonBandNames::MIR,4}};
// Syntax: CheckResult<Indice Class>("test_name",bandMap,{red_value,nir_value},expected_result)
bool res = CheckResult< NDWI<int,double> >("ndwi_null ",bandMap,{0,0,0,0,0},0.);
res = res & CheckResult< NDWI<int,double> >("ndwi_pixel",bandMap,{1,2,3,4,5},-0.1111111111);
res = res & CheckResult< NDWI2<int,double> >("ndwi2_null",bandMap,{0,0,0,0,0},0.);
res = res & CheckResult< NDWI2<int,double> >("ndwi2_pixel",bandMap,{1,2,3,4,5},-0.3333333333);
res = res & CheckResult< MNDWI<int,double> >("mndwi_null",bandMap,{0,0,0,0,0},0.);
res = res & CheckResult< MNDWI<int,double> >("mndwi_pixel",bandMap,{1,2,3,4,5},-0.4285714286);
res = res & CheckResult< NDPI<int,double> >("ndpi_null",bandMap,{0,0,0,0,0},0.);
res = res & CheckResult< NDPI<int,double> >("ndpi_pixel",bandMap,{1,2,3,4,5},0.4285714286);
res = res & CheckResult< NDTI<int,double> >("ndti_null",bandMap,{0,0,0,0,0},0.);
res = res & CheckResult< NDTI<int,double> >("ndti_pixel",bandMap,{1,2,3,4,5},0.2);
const std::map<ModisBandNames,size_t> bandMapModis = {{ModisBandNames::M860,0},{ModisBandNames::M1240,1}};
res = res & CheckResult< NDWI2<int,double> >("srwi_null",bandMap,{0,0},0.5);
res = res & CheckResult< NDWI2<int,double> >("srwi_pixel",bandMap,{1,2},0.5);
if(res)
{
......@@ -99,3 +134,149 @@ int otbVegetationIndicesTest(int, char**)
return EXIT_FAILURE;
}
}
int otbSoilIndicesTest(int, char**)
{
const std::map<CommonBandNames,size_t> bandMap = {{CommonBandNames::BLUE,0},{CommonBandNames::GREEN,1},{CommonBandNames::RED,2},{CommonBandNames::NIR,3}, {CommonBandNames::MIR,4}};
// Syntax: CheckResult<Indice Class>("test_name",bandMap,{red_value,nir_value},expected_result)
bool res = CheckResult< CI<int,double> >("ci_null ",bandMap,{0,0,0,0,0},0.);
res = res & CheckResult< CI<int,double> >("ci_pixel",bandMap,{1,2,3,4,5},-0.1111111111);
res = res & CheckResult< BI<int,double> >("bi_pixel",bandMap,{1,2,3,4,5},-0.3333333333);
res = res & CheckResult< BI2<int,double> >("bi2_pixel",bandMap,{1,2,3,4,5},-0.4285714286);
if(res)
{
return EXIT_SUCCESS;
}
else
{
return EXIT_FAILURE;
}
}
int otbBuiltUpIndicesTest(int, char**)
{
const std::map<CommonBandNames,size_t> bandMap = {{CommonBandNames::BLUE,0},{CommonBandNames::GREEN,1},{CommonBandNames::RED,2},{CommonBandNames::NIR,3}, {CommonBandNames::MIR,4}};
// Syntax: CheckResult<Indice Class>("test_name",bandMap,{red_value,nir_value},expected_result)
bool res = CheckResult< NDBI<int,double> >("ndbi_null ",bandMap,{0,0,0,0,0},0.);
res = res & CheckResult< NDBI<int,double> >("ndbi_pixel",bandMap,{1,2,3,4,5},0.1428571429);
res = res & CheckResult< ISU<int,double> >("isu_null",bandMap,{0,0,0,0,0},0.);
res = res & CheckResult< ISU<int,double> >("isu_pixel",bandMap,{1,2,3,4,5},81.25);
if(res)
{
return EXIT_SUCCESS;
}
else
{
return EXIT_FAILURE;
}
}
int otbRadiometricIndiceTest(int,char**)
{
auto ndvi = NDVI<double,double>();
auto requiredBands = ndvi.GetRequiredBands();
bool success = true;
if(requiredBands.size() != 2
|| requiredBands.find(CommonBandNames::RED) == requiredBands.end()
|| requiredBands.find(CommonBandNames::NIR) == requiredBands.end())
{
std::cerr<<"Required bands is not {RED,NIR} for NDVI"<<std::endl;
success = false;
}
ndvi.SetBandIndex(CommonBandNames::RED,10);
if(ndvi.GetBandIndex(CommonBandNames::RED) != 10)
{
std::cerr<<"Could not Set/Get band index properly"<<std::endl;
success = false;
}
const std::map<CommonBandNames,size_t> bandMap = {{CommonBandNames::RED,100},{CommonBandNames::NIR,200}};
ndvi.SetBandsIndices(bandMap);
if(ndvi.GetBandIndex(CommonBandNames::RED) != 100 || ndvi.GetBandIndex(CommonBandNames::NIR) != 200)
{
std::cerr<<"Could not set all band indices at once with SetBandIndices"<<std::endl;
success = false;
}
try
{
ndvi.SetBandIndex(CommonBandNames::MAX,1);
std::cerr<<"Calling SetBandIndices with ::MAX should raise a runtime_error exception."<<std::endl;
success = false;
}
catch(const std::runtime_error & e) {}
if(success)
{
return EXIT_SUCCESS;
}
else
{
return EXIT_FAILURE;
}
}
int otbIndicesStackFunctorTest(int, char**)
{
using IndicesType = RadiometricIndice<double,int,CommonBandNames>;
using StackFunctorType = IndicesStackFunctor<IndicesType>;
auto ndvi = NDVI<double,int>();
auto ndwi = NDWI<double,int>();
std::vector<IndicesType*> indices = {&ndvi, &ndwi};
auto stack = StackFunctorType(indices);
bool success = true;
if(stack.OutputSize() != 2)
{
std::cerr<<"Size of output pixel for stack functor should be 2"<<std::endl;
success = false;
}
const std::map<CommonBandNames,size_t> bandMap = {{CommonBandNames::BLUE,0},{CommonBandNames::GREEN,1},{CommonBandNames::RED,2},{CommonBandNames::NIR,3}, {CommonBandNames::MIR,4}};
ndvi.SetBandsIndices(bandMap);
ndwi.SetBandsIndices(bandMap);
StackFunctorType::OutputType out(2);
auto in = build_pixel<double>({1,2,3,4,5});
stack(out,in);
if(out[0] != ndvi(in))
{
std::cerr<<"First output band should correspond to ndvi"<<std::endl;
success = false;
}
if(out[1] != ndwi(in))
{
std::cerr<<"Second output band should correspond to ndwi"<<std::endl;
success = false;
}
if(success)
{
return EXIT_SUCCESS;
}
else
{
return EXIT_FAILURE;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment