Skip to content

Commit 1ca0a34

Browse files
authored
Merge pull request #222 from martinRenou/add_clip_support
Add suppport for clip
2 parents 5bf1404 + 630cd53 commit 1ca0a34

3 files changed

Lines changed: 50 additions & 9 deletions

File tree

include/xframe/xaxis_math.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace xf
6565
// Needs a fix in xtensor
6666
//using xt::maximum;
6767
//using xt::minimum;
68-
//using xt::clip;
68+
using xt::clip;
6969
using xt::sign;
7070

7171
using xt::exp;

include/xframe/xframe_expression.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
#ifndef XFRAME_XFRAME_EXPRESSION_HPP
1010
#define XFRAME_XFRAME_EXPRESSION_HPP
1111

12-
#include "xtensor/xexpression.hpp"
1312
#include "xtl/xtype_traits.hpp"
1413

14+
#include "xtensor/xexpression.hpp"
15+
1516
namespace xf
1617
{
1718
struct xvariable_expression_tag {};
@@ -40,4 +41,28 @@ namespace xf
4041
};
4142
}
4243

44+
namespace xt
45+
{
46+
namespace extension
47+
{
48+
template <>
49+
struct expression_tag_and<xf::xvariable_expression_tag, xoptional_expression_tag>
50+
{
51+
using type = xf::xvariable_expression_tag;
52+
};
53+
54+
template <>
55+
struct expression_tag_and<xoptional_expression_tag, xf::xvariable_expression_tag>
56+
: expression_tag_and<xf::xvariable_expression_tag, xoptional_expression_tag>
57+
{
58+
};
59+
60+
template <>
61+
struct expression_tag_and<xf::xvariable_expression_tag, xf::xvariable_expression_tag>
62+
{
63+
using type = xf::xvariable_expression_tag;
64+
};
65+
}
66+
}
67+
4368
#endif

test/test_xvariable_math.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,31 @@ namespace xf
345345
EXPECT_EQ(std::min(sa, a.select(sel)), minimum(sa, a).select(sel));
346346
}*/
347347

348-
// TODO: enable this once clamp functor is fixed in xtensor
349-
/*TEST(xvariable_math, clip)
348+
TEST(xvariable_math, clip)
350349
{
350+
auto missing = xtl::missing<double>();
351+
using data_type = xt::xoptional_assembly<xt::xarray<double>, xt::xarray<bool>>;
352+
351353
variable_type a = make_test_variable();
352-
dict_type sel = make_selector_aa();
353-
xtl::xoptional<double> floor = 1.2;
354-
xtl::xoptional<double> ceil = 2.4;
355-
EXPECT_EQ(clip(a.select(sel), floor, ceil), clip(a, floor, ceil).select(sel));
356-
}*/
354+
355+
variable_type res = clip(a, 2., 8.0);
356+
data_type expected = {{ 2, 2, missing},
357+
{missing, 5, 6},
358+
{ 7, 8, 8}};
359+
EXPECT_EQ(res.data(), expected);
360+
361+
variable_type res2 = clip(a, missing, missing);
362+
data_type expected2 = {{missing, missing, missing},
363+
{missing, missing, missing},
364+
{missing, missing, missing}};
365+
EXPECT_EQ(res2.data(), expected2);
366+
367+
variable_type res3 = clip(a, missing, 4.);
368+
data_type expected3 = {{missing, missing, missing},
369+
{missing, missing, missing},
370+
{missing, missing, missing}};
371+
EXPECT_EQ(res3.data(), expected3);
372+
}
357373

358374
TEST(xvariable_math, sign)
359375
{

0 commit comments

Comments
 (0)