Back to Documentations

Signature Description Parameters
#include <DataFrame/DataFrameTransformVisitors.h>

template<typename T, typename I = unsigned long>
struct AbsVisitor;
This is a transformer visitor. It means the column(s) passed to this visitor is not read-only and its values may change

This visitor sets negative numeric values to their absolute values.

This works with both scalar and multidimensional (i.e. vector and arrays) datasets.

get_result() returns the number of items changed.
T: Column data type
I: Index type
static void test_AbsVisitor()  {

    std::cout << "\nTesting AbsVisitor{ } ..." << std::endl;

    MyDataFrame df;

    StlVecType<unsigned long>  idxvec = { 1UL, 2UL, 3UL, 10UL, 5UL, 7UL, 8UL, 12UL, 9UL, 12UL, 10UL, 13UL, 10UL, 15UL, 14UL };
    StlVecType<double>         dblvec = { 0.0, -15.0, 14.0, 15.0, -1.0, 12.0, 11.0, -8.0, 15.0, 6.0, -1, 4.0, 14.0, 14.0, -20.0 };
    StlVecType<int>            intvec = { -1, 2, 3, 4, 5, 8, -6, 7, 11, -14, 9, -3, -5, -4, 9 };
    StlVecType<std::string>    strvec = { "zz", "bb", "zz", "ww", "ee", "ff", "gg", "zz", "ii", "jj", "kk", "ll", "mm", "ee", "" };

    df.load_data(std::move(idxvec),
                 std::make_pair("dbl_col", dblvec),
                 std::make_pair("int_col", intvec),
                 std::make_pair("str_col", strvec));

    AbsVisitor<double>  abs_v;
    AbsVisitor<int>     abs_v_int;
    auto                result = df.visit<double>("dbl_col", abs_v).get_result();
    auto                result_int = df.single_act_visit<int>("int_col", abs_v_int).get_result();

    assert(result == 5);
    assert(result_int == 6);

    StlVecType<double> abs_dblvec = { 0.0, 15.0, 14.0, 15.0, 1.0, 12.0, 11.0, 8.0, 15.0, 6.0, 1, 4.0, 14.0, 14.0, 20.0 };
    StlVecType<int>    abs_intvec = { 1, 2, 3, 4, 5, 8, 6, 7, 11, 14, 9, 3, 5, 4, 9 };

    assert((df.get_column<double>("dbl_col") == abs_dblvec));
    assert((df.get_column<int>("int_col") == abs_intvec));

    // Now multidimensional data
    //
    constexpr std::size_t   dim { 3 };

    using ary_col_t = std::array<double, dim>;
    using vec_col_t = std::vector<double>;

    StlVecType<ary_col_t>   ary_col  {
        { 1.0, 2.0, -1.0 }, { 2.0, -1.0, 3.0 }, { 3.0, 4.0, 2.0 }, { 2.0, 1.0, 2.0 }, { -1.0, -3.0, -1.0 }, { 4.0, 2.0, 4.0 },
        { 2.0, 1.0, 3.0 }, { 3.0, 3.0, 2.0 }, { -1.0, -2.0, 1.0 }, { 2.0, 4.0, 3.0 },
    };
    StlVecType<vec_col_t>   vec_col  {
        { 1.0, 2.0, -1.0 }, { 2.0, -1.0, 3.0 }, { 3.0, 4.0, 2.0 }, { 2.0, 1.0, 2.0 }, { -1.0, -3.0, -1.0 }, { 4.0, 2.0, 4.0 },
        { 2.0, 1.0, 3.0 }, { 3.0, 3.0, 2.0 }, { -1.0, -2.0, 1.0 }, { 2.0, 4.0, 3.0 },
    };

    df.load_column<ary_col_t>("ARY COL", std::move(ary_col), nan_policy::dont_pad_with_nans);
    df.load_column<vec_col_t>("VEC COL", std::move(vec_col), nan_policy::dont_pad_with_nans);

    AbsVisitor<ary_col_t>   ary_abs;
    AbsVisitor<vec_col_t>   vec_abs;

    df.visit<ary_col_t>("ARY COL", ary_abs);
    df.single_act_visit<vec_col_t>("VEC COL", vec_abs);

    const auto  &vec_md_ref { df.get_column<vec_col_t>("VEC COL") };
    const auto  &ary_md_ref { df.get_column<ary_col_t>("ARY COL") };

    assert(vec_abs.get_result() == 7);
    assert(ary_abs.get_result() == 7);

    for (const auto &vec : vec_md_ref)
        for (const auto val : vec)
            assert(val >= 0.0);
    for (const auto &ary : ary_md_ref)
        for (const auto val : ary)
            assert(val >= 0.0);
}

C++ DataFrame