GithubHelp home page GithubHelp logo

personalrobotics / boost_numpy_eigen Goto Github PK

View Code? Open in Web Editor NEW
11.0 11.0 9.0 81 KB

:snake: Python bindings for conversion between numpy and Eigen objects

License: BSD 3-Clause "New" or "Revised" License

Shell 2.10% CMake 26.91% Python 10.78% C++ 59.36% C 0.84%

boost_numpy_eigen's People

Contributors

algomorph avatar jslee02 avatar julienr avatar psigen avatar pyrym avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

boost_numpy_eigen's Issues

Export through structures

Hello!
I'm having troubles to export data structures that contain eigen members.

Example
This is a minimal example of what I would be able to do, but doesn't work.
Suppose that I have a struct like this:

struct Foo {
    int a;
    Eigen::Vector3d b;
};

I export it:

BOOST_PYTHON_MODULE(libhello) {
  SetupEigenConverters();

  class_<Foo>("Foo", init<>())
    .def_readonly("a", &Foo::a)
    .def_readonly("b", &Foo::b)
  ;
}

Then I try to work with it in Python:

import libhello
foo = libhello.Foo()
print(foo.b)

Unfortunately, I get an error like this:

TypeError: No Python class registered for C++ class Eigen::Matrix<double, 3, 1, 0, 3, 1>

Another example
This example works, but it is not exactly what I want.

struct Foo {
    int a;
    Eigen::Vector3e b;
    Eigen::Vector3d getB(void) { return b; }
};
class_<Foo>("Foo", init<>())
    .def_readonly("a", &Foo::a)
    .add_property("b", &Foo::getB)    // <- note the explicit getter
  ;

What is the issue?
These are just minimal examples, but in a real use case I have a complex hierarchy of classes. If I were to define an explicit getter for each attribute, then the complexity would easily explode. I don't really understand why I get the above error, the class should be already defined, no? If not, then why do the methods that directly return an eigen type work?

Eigen Tensor conversion logic -- potential problem

I realized recently that the way I implemented the conversion might not be quite logically correct.

I was guided by the principle: "in conversion from numpy array to Eigen Tensor, the indexing with the same subscripts should remain the same".

This rule would basically mean that tensor_as_numpy_array[1,2,3] and tensor_as_Eigen_Tensor(1,2,3) should be the same.

This rule tends to hold alright for the Eigen matrices, which are column-major but are indexed in a row-major fashion, i.e. eigen_tensor(2,3) means "row 2, column 3". I guess Eigen developers noticed they cannot contain the inconsistency between actual layout and indexing scheme beyond 2 dimensions, so the Tensors are actually indexed and stored consistently, as evidenced by the following statement:

By default, Tensors are stored in "column-major" order, &t(x+1, ...) == &t(x, ...) + 1, &t(x, y+1, ...) == &t(x, y, ...) + t.dimension(0), etc.

So, it might make more sense to convert fully without reversing axis order (the way I did it originally).
In this case, entries tensor_as_numpy_array[1,2,3] and tensor_as_Eigen_Tensor(3,2,1) should be the same.So, the last axis is (and should be) most significant in Eigen and the first axis is (and should be) the most significant axis in numpy. Preserving correspondence of the data ordering may is not only important to avoid unnecessary operations (axis swapping) during the conversion, but also important to be better able to optimize code that depends on traversing axes in least-to-most-significant way.

Please let me know what you think and I can alter the design accordingly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.