Commit d994478a authored by Thibault Hallouin's avatar Thibault Hallouin
Browse files

refactor data reading into separate function in unit tests

No related merge requests found
Pipeline #39769 passed with stages
in 4 minutes and 55 seconds
Showing with 79 additions and 81 deletions
+79 -81
#include <fstream>
#include <vector>
#include <tuple>
#include <array>
#include <gtest/gtest.h>
#include <xtensor/xtensor.hpp>
......@@ -11,19 +12,48 @@
using namespace xt::placeholders; // required for `_` to work
TEST(DeterministTests, TestMetrics2D)
template <std::size_t N>
std::tuple<xt::xtensor<double, N>, xt::xtensor<double, N>> load_data()
{
// read in data
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 2> observed =xt::load_csv<int>(ifs);
ifs.close();
if (N == 2)
{
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 2> observed = xt::load_csv<int>(ifs);
ifs.close();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 2> predicted = xt::view(
xt::load_csv<double>(ifs), xt::range(0, 5), xt::all()
);
ifs.close();
return std::make_tuple(observed, predicted);
}
else
{
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 1> observed = xt::squeeze(xt::load_csv<int>(ifs));
ifs.close();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 1> predicted = xt::view(
xt::load_csv<double>(ifs), 0, xt::all()
);
ifs.close();
return std::make_tuple(observed, predicted);
}
}
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 2> predicted = xt::view(
xt::load_csv<double>(ifs), xt::range(0, 5), xt::all()
);
ifs.close();
TEST(DeterministTests, TestMetrics2D)
{
// read in data
xt::xtensor<double, 2> observed;
xt::xtensor<double, 2> predicted;
std::tie(observed, predicted) = load_data<2>();
// compute scores (with 2D tensors)
std::vector<xt::xarray<double>> metrics =
......@@ -68,16 +98,9 @@ TEST(DeterministTests, TestMetrics2D)
TEST(DeterministTests, TestMetrics1D)
{
// read in data
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 1> observed = xt::squeeze(xt::load_csv<int>(ifs));
ifs.close();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 1> predicted = xt::row(
xt::view(xt::load_csv<double>(ifs), xt::range(0, 5), xt::all()), 0
);
ifs.close();
xt::xtensor<double, 1> observed;
xt::xtensor<double, 1> predicted;
std::tie(observed, predicted) = load_data<1>();
// compute scores (with 1D tensors)
std::vector<xt::xarray<double>> metrics =
......@@ -102,16 +125,9 @@ TEST(DeterministTests, TestMetrics1D)
TEST(DeterministTests, TestTransform)
{
// read in data
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 2> observed =xt::load_csv<int>(ifs);
ifs.close();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 2> predicted = xt::view(
xt::load_csv<double>(ifs), xt::range(0, 5), xt::all()
);
ifs.close();
xt::xtensor<double, 2> observed;
xt::xtensor<double, 2> predicted;
std::tie(observed, predicted) = load_data<2>();
// compute and check results on square-rooted streamflow series
std::vector<xt::xarray<double>> metrics =
......@@ -163,14 +179,9 @@ TEST(DeterministTests, TestTransform)
TEST(DeterministTests, TestMasks)
{
// read in data
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 2> observed = xt::load_csv<int>(ifs);
ifs.close();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 2> predicted = xt::load_csv<double>(ifs);
ifs.close();
xt::xtensor<double, 2> observed;
xt::xtensor<double, 2> predicted;
std::tie(observed, predicted) = load_data<2>();
// generate temporal subset by dropping 20 first time steps
xt::xtensor<double, 2> masks =
......@@ -205,14 +216,9 @@ TEST(DeterministTests, TestMaskingConditions)
{"RMSE", "NSE", "KGE", "KGEPRIME"};
// read in data
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 2> observed = xt::load_csv<int>(ifs);
ifs.close();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 2> predicted = xt::load_csv<double>(ifs);
ifs.close();
xt::xtensor<double, 2> observed;
xt::xtensor<double, 2> predicted;
std::tie(observed, predicted) = load_data<2>();
// generate dummy empty masks required to access next optional argument
xt::xtensor<bool, 2> masks;
......@@ -319,16 +325,9 @@ TEST(DeterministTests, TestMissingData)
{"RMSE", "NSE", "KGE", "KGEPRIME"};
// read in data
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 2> observed = xt::load_csv<int>(ifs);
ifs.close();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 2> predicted = xt::view(
xt::load_csv<double>(ifs), xt::range(0, 5), xt::all()
);
ifs.close();
xt::xtensor<double, 2> observed;
xt::xtensor<double, 2> predicted;
std::tie(observed, predicted) = load_data<2>();
// add some missing observations artificially by assigning NaN values
xt::view(observed, xt::all(), xt::range(0, 20)) = NAN;
......
#include <fstream>
#include <vector>
#include <tuple>
#include <array>
#include <gtest/gtest.h>
#include <xtensor/xtensor.hpp>
......@@ -10,7 +11,8 @@
using namespace xt::placeholders; // required for `_` to work
TEST(ProbabilistTests, TestBrier)
std::tuple<xt::xtensor<double, 1>, xt::xtensor<double, 2>> load_data()
{
// read in data
std::ifstream ifs;
......@@ -22,6 +24,16 @@ TEST(ProbabilistTests, TestBrier)
xt::xtensor<double, 2> predicted = xt::load_csv<double>(ifs);
ifs.close();
return std::make_tuple(observed, predicted);
}
TEST(ProbabilistTests, TestBrier)
{
// read in data
xt::xtensor<double, 1> observed;
xt::xtensor<double, 2> predicted;
std::tie(observed, predicted) = load_data();
// compute scores
xt::xtensor<double, 2> thresholds = {{690, 534, 445, NAN}};
......@@ -78,14 +90,9 @@ TEST(ProbabilistTests, TestBrier)
TEST(ProbabilistTests, TestQuantiles)
{
// read in data
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 1> observed = xt::squeeze(xt::load_csv<int>(ifs));
ifs.close();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 2> predicted = xt::load_csv<double>(ifs);
ifs.close();
xt::xtensor<double, 1> observed;
xt::xtensor<double, 2> predicted;
std::tie(observed, predicted) = load_data();
// compute scores
std::vector<xt::xarray<double>> metrics =
......@@ -122,14 +129,9 @@ TEST(ProbabilistTests, TestQuantiles)
TEST(ProbabilistTests, TestMasks)
{
// read in data
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 1> observed = xt::squeeze(xt::load_csv<int>(ifs));
ifs.close();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 2> predicted = xt::load_csv<double>(ifs);
ifs.close();
xt::xtensor<double, 1> observed;
xt::xtensor<double, 2> predicted;
std::tie(observed, predicted) = load_data();
// generate temporal subset by dropping 20 first time steps
xt::xtensor<double, 4> masks =
......@@ -180,15 +182,12 @@ TEST(ProbabilistTests, TestMaskingConditions)
{"BS", "BSS", "BS_CRD", "BS_LBD", "QS", "CRPS"};
// read in data
// read in data
std::ifstream ifs;
ifs.open("./data/q_obs.csv");
xt::xtensor<double, 2> observed = xt::load_csv<int>(ifs);
ifs.close();
xt::xtensor<double, 1> observed_;
xt::xtensor<double, 2> predicted;
std::tie(observed_, predicted) = load_data();
ifs.open("./data/q_prd.csv");
xt::xtensor<double, 2> predicted = xt::load_csv<double>(ifs);
ifs.close();
// turn observed into 2D view (to simplify syntax later on)
auto observed = xt::view(observed_, xt::newaxis(), xt::all());
// generate dummy empty masks required to access next optional argument
xt::xtensor<bool, 4> masks;
......
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