test_probabilist.cpp 35.74 KiB
// Copyright (c) 2023, INRAE.
// Distributed under the terms of the GPL-3 Licence.
// The full licence is in the file LICENCE, distributed with this software.
#include <fstream>
#include <vector>
#include <tuple>
#include <array>
#include <gtest/gtest.h>
#include <xtl/xoptional.hpp>
#include <xtensor/xtensor.hpp>
#include <xtensor/xview.hpp>
#include <xtensor/xsort.hpp>
#include <xtensor/xmanipulation.hpp>
#include <xtensor/xcsv.hpp>
#include "evalhyd/evalp.hpp"
#ifndef EVALHYD_DATA_DIR
#error "need to define data directory"
#endif
using namespace xt::placeholders;  // required for `_` to work
std::vector<std::string> all_metrics_p = {
        "BS", "BSS", "BS_CRD", "BS_LBD",
        "QS", "CRPS",
        "POD", "POFD", "FAR", "CSI", "ROCSS",
        "RANK_DIAG", "DS", "AS"
std::tuple<xt::xtensor<double, 1>, xt::xtensor<double, 2>> load_data_p()
    // read in data
    std::ifstream ifs;
    ifs.open(EVALHYD_DATA_DIR "/q_obs.csv");
    xt::xtensor<double, 1> observed = xt::squeeze(xt::load_csv<int>(ifs));
    ifs.close();
    ifs.open(EVALHYD_DATA_DIR "/q_prd.csv");
    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_p();
    // compute scores
    xt::xtensor<double, 2> thresholds = {{690, 534, 445, NAN}};
    std::vector<xt::xarray<double>> metrics =
            evalhyd::evalp(
                    // shape: (sites [1], time [t])
                    xt::eval(xt::view(observed, xt::newaxis(), xt::all())),
                    // shape: (sites [1], lead times [1], members [m], time [t])
                    xt::eval(xt::view(predicted, xt::newaxis(), xt::newaxis(), xt::all(), xt::all())),
                    {"BS", "BSS", "BS_CRD", "BS_LBD"},
                    thresholds,
                    "high"