• Thibault Hallouin's avatar
    add dimensions for sites/lead times to probabilistic evaluator · 295b3208
    Thibault Hallouin authored
    Internally, rather than using the multi-dimensional character of
    tensors to compute all sites and all lead times at once, loops are
    performed for each site and each lead time, in turn, in order to
    minimise memory imprint. Although at the moment, the input tensors are
    expected to feature the sites and lead times dimensions. If memory is
    an issue, the user can still send smaller tensors with size 1 for those
    dimensions and recompose multi-sites/multi-lead times output arrays
    externally.
    295b3208
test_probabilist.cpp 1.99 KiB
#include <fstream>
#include <vector>
#include <gtest/gtest.h>
#include <xtensor/xtensor.hpp>
#include <xtensor/xmanipulation.hpp>
#include <xtensor/xcsv.hpp>
#include "evalhyd/evalp.hpp"
TEST(ProbabilistTests, TestBrier) {
    // 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();
    // add "artificial" site dimension (size 1) to observations
    auto obs = xt::view(observed, xt::newaxis(), xt::all());
    // add "artificial" site dimension (size 1) and lead time dimension (size 1)
    // to predictions
    auto prd = xt::view(predicted, xt::newaxis(), xt::newaxis(), xt::all(), xt::all());
    // compute scores
    xt::xtensor<double, 1> thresholds = {690, 534, 445};
    std::vector<xt::xarray<double>> metrics =
            evalhyd::evalp(
                    obs, prd, {"BS", "BSS", "BS_CRD", "BS_LBD"},
                    thresholds
    // check results
    // Brier scores
    xt::xtensor<double, 4> bs =
            {{{{0.10615136, 0.07395622, 0.08669186}}}};
    EXPECT_TRUE(xt::allclose(metrics[0], bs));
    // Brier skill scores
    xt::xtensor<double, 4> bss =
            {{{{0.5705594, 0.6661165, 0.5635126}}}};
    EXPECT_TRUE(xt::allclose(metrics[1], bss));
    // Brier calibration-refinement decompositions
    xt::xtensor<double, 5> bs_crd =
            {{{{{0.011411758, 0.1524456, 0.2471852},
                {0.005532413, 0.1530793, 0.2215031},
                {0.010139431, 0.1220601, 0.1986125}}}}};
    EXPECT_TRUE(xt::allclose(metrics[2], bs_crd));
    // Brier likelihood-base rate decompositions
    xt::xtensor<double, 5> bs_lbd =
            {{{{{0.012159881, 0.1506234, 0.2446149},
                {0.008031746, 0.1473869, 0.2133114},
                {0.017191279, 0.1048221, 0.1743227}}}}};
    EXPECT_TRUE(xt::allclose(metrics[3], bs_lbd));