Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eigen: Return type "annotation" does not reflect actual shape for vectors? #2400

Open
EricCousineau-TRI opened this issue Aug 17, 2020 · 1 comment

Comments

@EricCousineau-TRI
Copy link
Collaborator

Issue description

Per docs here (as of 2.5.0):
https://pybind11.readthedocs.io/en/stable/advanced/cast/eigen.html#vectors-versus-column-row-matrices

When returning an Eigen vector to numpy, [...] if it is a compile-time vector–that is, the type has either the number of rows or columns set to 1 at compile time–pybind11 converts to a 1D numpy array when returning the value. [...]

Makes total sense. However, looking at the docstrings:

def test_dense_signature(doc):
assert doc(m.double_col) == """
double_col(arg0: numpy.ndarray[numpy.float32[m, 1]]) -> numpy.ndarray[numpy.float32[m, 1]]
"""
assert doc(m.double_row) == """
double_row(arg0: numpy.ndarray[numpy.float32[1, n]]) -> numpy.ndarray[numpy.float32[1, n]]
"""
assert doc(m.double_complex) == ("""
double_complex(arg0: numpy.ndarray[numpy.complex64[m, 1]])"""
""" -> numpy.ndarray[numpy.complex64[m, 1]]
""")
assert doc(m.double_mat_rm) == ("""
double_mat_rm(arg0: numpy.ndarray[numpy.float32[m, n]])"""
""" -> numpy.ndarray[numpy.float32[m, n]]
""")

For m.double_col, the input type makes sense, because that matters to C++ (as mentioned in the docs).

However, the return type is a bit misleading: it states that the output types is numpy.ndarray[numpy.float32[m, 1]].

This is a bit misleading, because the return type's shape is actually (m,).
Additionally, when the return type has

This bit us in RobotLocomotion/drake#13885.
The docs do have a suggested workaround, which is what we'll do, but it'd be nice if the annotation were a bit more truthful.

Reproducible example code

See aforementioned example.

Possible solutions

  1. Somehow distinguish the type string based on return-type? (that could be down by "rerouting" the return type somehow?)
  2. Discard shape information (at the cost of sacrificing info in the argument - probably a non-starter)
  3. As-is
  4. Other?
@EricCousineau-TRI
Copy link
Collaborator Author

FWIW The main "jarring" aspect is that the shape of float[m, n] is truly (m, n), while the shape of float[m, 1] is (m,) (as seen by Python), so it'd be nice to somehow collapse that.

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

No branches or pull requests

1 participant