From 524de569b73336fdd687a71de5f6a28d7cab0b49 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 24 Jan 2019 14:56:03 +0100 Subject: [PATCH 1/2] Add where function and tests --- include/xframe/xaxis_math.hpp | 2 ++ test/test_xvariable_math.cpp | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/xframe/xaxis_math.hpp b/include/xframe/xaxis_math.hpp index 659f42b..bfe4cf7 100644 --- a/include/xframe/xaxis_math.hpp +++ b/include/xframe/xaxis_math.hpp @@ -114,6 +114,8 @@ namespace xf // Needs a fix in xtensor /*using xt::isclose; using xt::allclose;*/ + + using xt::where; } #endif diff --git a/test/test_xvariable_math.cpp b/test/test_xvariable_math.cpp index 5b8a529..44f8791 100644 --- a/test/test_xvariable_math.cpp +++ b/test/test_xvariable_math.cpp @@ -7,7 +7,12 @@ ****************************************************************************/ #include + #include "gtest/gtest.h" + +#include "xtensor/xarray.hpp" +#include "xtensor/xoptional_assembly.hpp" + #include "test_fixture.hpp" namespace xf @@ -278,7 +283,7 @@ namespace xf EXPECT_EQ(fma(sa, sb, a.select(sel)), xf::fma(sa, sb, a).select(sel)); } - + TEST(xvariable_math, fmax) { variable_type a = make_test_variable(); @@ -657,6 +662,29 @@ namespace xf EXPECT_EQ(isnan(a.select(sel)), xf::isnan(a).select(sel)); } + TEST(xvariable_math, where) + { + auto missing = xtl::missing(); + using data_type = xt::xoptional_assembly, xt::xarray>; + + variable_type a = make_test_variable(); + variable_type b = make_test_variable2(); + + variable_type res = where(a < 6, b, a); + + data_type expected = {{{ 1., 2., missing}, + {missing, missing, missing}}, + {{ 7., 7., 7.}, + { 9., 9., 9.}}}; + EXPECT_EQ(res.data(), expected); + + variable_type res2 = where(a < 6, 0., a); + data_type expected2 = {{ 0, 0, missing}, + {missing, 0, 6}, + { 7, 8, 9}}; + EXPECT_EQ(res2.data(), expected2); + } + // Needs a fix in xtensor /*TEST(xvariable_math, isclose) { @@ -664,4 +692,4 @@ namespace xf dict_type sel = make_selector_aa(); EXPECT_TRUE(isclose(a, a).select(sel)); }*/ -} \ No newline at end of file +} From 0226c15ed1787b20cad694040cc755f73023e055 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 24 Jan 2019 14:58:16 +0100 Subject: [PATCH 2/2] Add documentation about where --- docs/source/xarray.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/xarray.rst b/docs/source/xarray.rst index 0fa403a..1bd99dd 100644 --- a/docs/source/xarray.rst +++ b/docs/source/xarray.rst @@ -126,6 +126,7 @@ where ``condition`` is falsy, and it does not evaluate ``b`` where ``condition`` | Python 3 - xarray | C++ 14 - xframe | +===============================================+===============================================+ | ``xr.where(a > 5, a, b)`` | ``xf::where(a > 5, a, b)`` | +| ``xr.where(a > 5, 100, a)`` | ``xf::where(a > 5, 100, a)`` | +-----------------------------------------------+-----------------------------------------------+ | ``np.any(a)`` | ``xf::any(a)`` | +-----------------------------------------------+-----------------------------------------------+ @@ -251,4 +252,3 @@ xframe universal functions are provided for a large set number of mathematical f +-----------------------------------------------+-----------------------------------------------+ | ``scipy.special.gammaln(a)`` | ``xf::lgamma(a)`` | +-----------------------------------------------+-----------------------------------------------+ -