-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
pydrake systems: Add initial custom scalar conversion test / example #8701
pydrake systems: Add initial custom scalar conversion test / example #8701
Conversation
+@jwnimmer-tri for feature review, please. Review status: 0 of 6 files reviewed at latest revision, all discussions resolved. Comments from Reviewable |
Reviewed 4 of 6 files at r1. bindings/pydrake/systems/framework_py_systems.cc, line 416 at r1 (raw file):
Its not immediately obvious to me why the python signature for bindings/pydrake/systems/scalar_conversion.py, line 47 at r1 (raw file):
Do we expect a pydrake user to call this method directly. If so, I think this doc is quite a bit too obscure and brief. Even as domain expert trying to read the code, I am pretty confused by this whole file. systems/framework/system_scalar_converter.h, line 104 at r1 (raw file):
BTW This phrasing could be slightly improved to more clearly be an instruction to an author changing this list, rather than a reader trying to understand it (i.e., this could be misread as explaining what is happening). Maybe more like "When changing the pairs of supported types below, be sure to also change foo to be the same list of pairs". Comments from Reviewable |
10c581b
to
6559bd2
Compare
Review status: 1 of 11 files reviewed at latest revision, 3 unresolved discussions. bindings/pydrake/systems/framework_py_systems.cc, line 416 at r1 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/scalar_conversion.py, line 47 at r1 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. Unfortunately, I'm not sure how to clarify the structure of it; however, I have made the documentation more example-based. Users will unfortunately need this for defining scalar-covertible Python classes, but we can always author a trampoline to minimize the pain. systems/framework/system_scalar_converter.h, line 104 at r1 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. Comments from Reviewable |
Reviewed 8 of 10 files at r2. bindings/pydrake/systems/scalar_conversion.py, line 26 at r3 (raw file):
I am not loving the way this ends up working on for users... the example below is fairly convoluted and unpythonic. Can we chat f2f sometime about the background here and some alternatives? My intuition points more towards (1) a way to declare the class at the top level instead of nested -- even if we still also have the def function wrapping it, and (2) always requiring the user to write regular ctor and copy ctor separately, with magic name(s), instead of calling "check_if_copying" explicitly. bindings/pydrake/util/wrap_pybind.h, line 51 at r3 (raw file):
FYI Is this TODO already fixed by the is_generic_pybind check? bindings/pydrake/util/wrap_pybind.h, line 77 at r3 (raw file):
BTW While its clear to me in the context of this PR, in the future it might not be clear to a unprimed reader that this sentence is trying to explain why WrapCallbacks is useful or should be used, as opposed to explaining some API contract or semantic detail of the implementation. Comments from Reviewable |
Review status: 9 of 11 files reviewed at latest revision, 3 unresolved discussions, some commit checks failed. bindings/pydrake/systems/scalar_conversion.py, line 26 at r3 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
OK Definitely up for f2f! Writing out some initial thoughts: For (1), I don't think there will be a way to make a good and useful class-generator without nesting (constrained by decorators and Python's parsing), and I don't think the class nesting is that high of a price to pay. I've looked at things like For (2), yup, I think that can be done. Comments from Reviewable |
Review status: 9 of 11 files reviewed at latest revision, 3 unresolved discussions, some commit checks failed. bindings/pydrake/systems/scalar_conversion.py, line 26 at r3 (raw file): Previously, EricCousineau-TRI (Eric Cousineau) wrote…
Aha, I tried (1) locally and see the challenge now. We need to know what Comments from Reviewable |
4995306
to
205aa4a
Compare
Review status: 7 of 12 files reviewed at latest revision, 3 unresolved discussions. bindings/pydrake/systems/scalar_conversion.py, line 26 at r3 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. Gave a shot at condensing the boiler-platey things:
bindings/pydrake/util/wrap_pybind.h, line 51 at r3 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. D'oh! bindings/pydrake/util/wrap_pybind.h, line 77 at r3 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. Comments from Reviewable |
Checkpoint. The shape of the API and example seem better. FYI The code is subtle and complicated enough that its taking me a while to read through it all. I'll keep pushing on it. Reviewed 2 of 6 files at r4, 1 of 3 files at r5. bindings/pydrake/systems/scalar_conversion.py, line 34 at r5 (raw file):
I don't understand what part bindings/pydrake/systems/test/scalar_conversion_test.py, line 15 at r5 (raw file):
Testing locally, instead of naming this I like bindings/pydrake/util/cpp_template.py, line 109 at r5 (raw file):
FYI I would have followed this better if bindings/pydrake/util/cpp_template.py, line 136 at r5 (raw file):
I am unable to foresee a case where someone calls bindings/pydrake/util/cpp_template.py, line 158 at r5 (raw file):
BTW Unclear why we want I guess when I reflect on it more I can see some reasons, but in any case for a method named Comments from Reviewable |
Reviewed 1 of 3 files at r5. bindings/pydrake/systems/test/scalar_conversion_test.py, line 39 at r5 (raw file):
FYI Meta I think unittest has a list-comparison primitive, with better error messages? bindings/pydrake/systems/test/scalar_conversion_test.py, line 130 at r5 (raw file):
BTW By "param list" does this mean "T_list" specifically? bindings/pydrake/systems/test/scalar_conversion_test.py, line 205 at r5 (raw file):
Hmm, why is the error message we expect here "should not define"? Comments from Reviewable |
First pass complete. Reviewed 1 of 3 files at r5. bindings/pydrake/systems/scalar_conversion.py, line 16 at r5 (raw file):
If I am reading correctly, the T_compat filter is pointless. Needing it would imply that SupportedConversionPairs has scalars that are not in SupportedScalars. It seems like the T, U loop is all we need, checking that both T and U are in T_list. bindings/pydrake/systems/scalar_conversion.py, line 39 at r5 (raw file):
Why should bindings/pydrake/systems/scalar_conversion.py, line 56 at r5 (raw file):
I am having a hard time understanding why converter should ever be non-None here? I see that sometimes it is (per the bindings/pydrake/systems/scalar_conversion.py, line 58 at r5 (raw file):
To reduce brittleness, should this prefer to delegate instead? def _construct_copy(self, other, converter=None):
self._construct(other.value, converter=converter) That's what we recommend for C++ transmog ctors, and I think the rationale applies equally well here? bindings/pydrake/systems/scalar_conversion.py, line 101 at r5 (raw file):
FYI Should we copy the lists here? What's the python convention on list aliasing for passed arguments? Do we just presume that callers should never pass a list and then mutate it after we return? bindings/pydrake/systems/scalar_conversion.py, line 137 at r5 (raw file):
BTW typo bindings/pydrake/systems/scalar_conversion.py, line 151 at r5 (raw file):
BTW Since this is likely to be a easy user mistake to make, I think we should have a specific, distinct error message for all three types of user error -- present init, missing constructor, missing construct_copy. bindings/pydrake/systems/scalar_conversion.py, line 174 at r5 (raw file):
Unclear when when bindings/pydrake/systems/scalar_conversion.py, line 177 at r5 (raw file):
FYI Is there anything we can check on kwargs to identify a copy? It seems like it should have Comments from Reviewable |
bindings/pydrake/systems/test/scalar_conversion_test.py, line 15 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
How about something like Comments from Reviewable |
Review status: all files reviewed at latest revision, 17 unresolved discussions, some commit checks failed. bindings/pydrake/systems/test/scalar_conversion_test.py, line 15 at r5 (raw file): Previously, EricCousineau-TRI (Eric Cousineau) wrote…
Hm, what about just Comments from Reviewable |
bindings/pydrake/systems/test/scalar_conversion_test.py, line 15 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Hm... If completely generic names is acceptable, then then my preference would be towards your earlier suggestion, Comments from Reviewable |
bindings/pydrake/systems/test/scalar_conversion_test.py, line 15 at r5 (raw file): Previously, EricCousineau-TRI (Eric Cousineau) wrote…
Sure Comments from Reviewable |
963cbf3
to
af91966
Compare
Review status: 7 of 12 files reviewed at latest revision, 16 unresolved discussions. bindings/pydrake/systems/scalar_conversion.py, line 16 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/scalar_conversion.py, line 34 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. Sorry, wording was ambiguous, tried to clarify. Can remove this clause if it's still unclear. bindings/pydrake/systems/scalar_conversion.py, line 39 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/scalar_conversion.py, line 56 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/scalar_conversion.py, line 58 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. Good point! bindings/pydrake/systems/scalar_conversion.py, line 101 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/scalar_conversion.py, line 137 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/scalar_conversion.py, line 151 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/scalar_conversion.py, line 174 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/scalar_conversion.py, line 177 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/test/scalar_conversion_test.py, line 15 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/test/scalar_conversion_test.py, line 39 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. Thanks! bindings/pydrake/systems/test/scalar_conversion_test.py, line 130 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/systems/test/scalar_conversion_test.py, line 205 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/util/cpp_template.py, line 109 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. bindings/pydrake/util/cpp_template.py, line 136 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. (Generating exception.) bindings/pydrake/util/cpp_template.py, line 158 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
Done. Was not being strict on equality mechanism, thanks! Comments from Reviewable |
Reviewed 5 of 5 files at r6. bindings/pydrake/systems/scalar_conversion.py, line 34 at r5 (raw file): Previously, EricCousineau-TRI (Eric Cousineau) wrote…
OK This is good now, thanks. bindings/pydrake/systems/scalar_conversion.py, line 56 at r5 (raw file): Previously, EricCousineau-TRI (Eric Cousineau) wrote…
OK I think my error in reasoning was that while It would be great to figure out some sugar where that's not required, but I can see why its difficult. Comments from Reviewable |
4bb79a1
to
dc148cd
Compare
-@sherm1 Review status: 7 of 12 files reviewed at latest revision, 1 unresolved discussion. bindings/pydrake/systems/scalar_conversion.py, line 56 at r5 (raw file): Previously, jwnimmer-tri (Jeremy Nimmer) wrote…
OK Good point! I've made some TODOs for now, as I'm not aware of how to do it without deprecated features. (We could pass the template itself around, but that still requires the user to propagate information.) Comments from Reviewable |
Reviewed 10 of 10 files at r8. Comments from Reviewable |
Reviewed 3 of 10 files at r2, 1 of 5 files at r6, 7 of 10 files at r7, 8 of 10 files at r8. bindings/pydrake/systems/framework_py_systems.cc, line 56 at r8 (raw file):
BTW, I think this comment should be placed after the bindings/pydrake/systems/framework_py_systems.cc, line 57 at r8 (raw file):
BTW, trivial, bindings/pydrake/util/cpp_template.py, line 79 at r8 (raw file):
Can you add more high-level documentation such as 1) what is deferred instantiation/evaluation, 2) why do we need to handle? I was able to find some clues from #8666 (comment) but definitely would appreciate if you can provide more explanations. bindings/pydrake/util/wrap_pybind.h, line 76 at r8 (raw file):
BTW, Comments from Reviewable |
Reviewed 2 of 10 files at r8. Comments from Reviewable |
dc148cd
to
d13e917
Compare
Review status: 9 of 12 files reviewed at latest revision, 4 unresolved discussions. bindings/pydrake/systems/framework_py_systems.cc, line 56 at r8 (raw file): Previously, soonho-tri (Soonho Kong) wrote…
OK I'd prefer to keep it at the top, since we're not able to use bindings/pydrake/systems/framework_py_systems.cc, line 57 at r8 (raw file): Previously, soonho-tri (Soonho Kong) wrote…
Done. bindings/pydrake/util/cpp_template.py, line 79 at r8 (raw file): Previously, soonho-tri (Soonho Kong) wrote…
Done. Let me know if that helps! bindings/pydrake/util/wrap_pybind.h, line 76 at r8 (raw file): Previously, soonho-tri (Soonho Kong) wrote…
Done. Comments from Reviewable |
Reviewed 3 of 3 files at r9. bindings/pydrake/util/cpp_template.py, line 79 at r8 (raw file): Previously, EricCousineau-TRI (Eric Cousineau) wrote…
Nice! Thank you! Comments from Reviewable |
Reviewed 3 of 3 files at r9. Comments from Reviewable |
This shows authoring a scalar-convertible class in Python.
This can be used to author a class, or can be used to write a Python trampoline class, which can then call a simpler Python implementation.
\cc @RussTedrake
This change is