personalrobotics / boost_numpy_eigen Goto Github PK
View Code? Open in Web Editor NEW:snake: Python bindings for conversion between numpy and Eigen objects
License: BSD 3-Clause "New" or "Revised" License
:snake: Python bindings for conversion between numpy and Eigen objects
License: BSD 3-Clause "New" or "Revised" License
Hi!
It seems that https://github.com/ndarray/Boost.NumPy is deprecated,
as NumPy support is now natively supported by Boost.Python.
Do you have any plans to update this library accordingly?
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?
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:
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.