Skip to content

Add type_caster classes for xt::xtensor, xt::xarray, and xt::xexpression.#113

Merged
SylvainCorlay merged 4 commits into
xtensor-stack:masterfrom
yoshipon:xtensor_type_caster
Sep 27, 2017
Merged

Add type_caster classes for xt::xtensor, xt::xarray, and xt::xexpression.#113
SylvainCorlay merged 4 commits into
xtensor-stack:masterfrom
yoshipon:xtensor_type_caster

Conversation

@yoshipon
Copy link
Copy Markdown
Contributor

They enable casing ndarray to xt::xexpression<xt::pyarray> and casing xt::xarray to ndarray.

With the following code,

struct Foo
{
  xt::xtensor<double, 1> x;

  template<class X>
  void set(const xt::xexpression<X> &x_)
  {
    x = x_.derived_cast();
  }

  xt::xtensor<double, 1>& get(void)
  {
    return x;
  }
};

py::class_<Foo>(m, "Foo")
  .def(py::init<>())
  .def("set", &Foo::set<xt::pyarray<double>>)
  .def("get", &Foo::get, py::return_value_policy::reference_internal);

the Foo.x can be modified from python as follows:

foo = Foo()
foo.set(np.array([1,2,3,4])) 
x = foo.get()
print(foo.get())  # -> [ 1.  2.  3.  4.]
x[0] = 100  
print(foo.get())  # -> [ 100.    2.    3.    4.]

They enable casing ndarray to xexpression<pyarray> and casing xarray to ndarray
@SylvainCorlay
Copy link
Copy Markdown
Member

Thanks @yoshipon, we really like this solution.

@SylvainCorlay
Copy link
Copy Markdown
Member

The main remark is about code formatting. Our convention is the following

Regarding formatting, here is the current convention:

namespace bar
{                                               // indent in namespace
    class Foo
    {                                           // new line before {
    public:
                                                // empty line after public /  private / protected
         Foo();
    };

    Foo::Foo()
    {
    }
}                                               // no use of PYBIND macros for NAMESPACE_END NAMESPACE_BEGIN

@yoshipon
Copy link
Copy Markdown
Contributor Author

@SylvainCorlay Thank you for the comments.
I formatted the files. Please confirm the revised codes.

@JohanMabille
Copy link
Copy Markdown
Member

JohanMabille commented Sep 27, 2017

@yoshipon Thanks for this PR.

Not sure about that, but the errors may be due to the fact that we do not use the same versions of pybind. The CI (travis and appveyor) install pybind11 2.1.1, which is not the last version of pybind.

@yoshipon
Copy link
Copy Markdown
Contributor Author

@JohanMabille Exactly, my code could not be compiled with pybind11 2.1.1 due to the following two reasons:

  1. move_cast_op_type does not exist
  2. strides and shape have to be std::vector<size_t> (std::array was not supported yet)

I fixed these bugs.

However, even now there are several problems on Travis CI, I'm still working for debugging.

@SylvainCorlay
Copy link
Copy Markdown
Member

SylvainCorlay commented Sep 27, 2017

The Travis CI errors seem to be coming from the new miniconda that was released yesterday. (Hence the build flag which seem to come from their python/distutils build).

Downloading Miniconda2-4.3.21-Linux-x86_64.sh instead of latest might solve the issue.

@yoshipon
Copy link
Copy Markdown
Contributor Author

@SylvainCorlay Thank you for the information. I re-pushed with a modified .travis.yml.

@SylvainCorlay
Copy link
Copy Markdown
Member

SylvainCorlay commented Sep 27, 2017

Success!

For your information, I have reported the issue upstream the new miniconda build (in their gitter channel).

@SylvainCorlay
Copy link
Copy Markdown
Member

Merging 🎉 congrats on your first PR into xtensor!

@SylvainCorlay SylvainCorlay merged commit 2562449 into xtensor-stack:master Sep 27, 2017
@SylvainCorlay
Copy link
Copy Markdown
Member

@yoshipon regardin the xtensor/xarray to ndarray cast, one thing that we will probably try to do before the next release is not make use of pybind11::array, and use xt::pyarray instead.

It should probably be a matter of adding a constructor to xt::pyarray that is optimized in that it would reuse the buffer of xtensor/xarray.

@yoshipon
Copy link
Copy Markdown
Contributor Author

@SylvainCorlay Thank you very much! Actually, this is my first PR. It was very impressive.
btw, it is a good idea. The current type_caster specifically for xarray/xtensor is not the best way.
With this extension, it will be more simplified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants