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

Start wrapping the verbosity options for GNC #845

Merged
merged 4 commits into from
Sep 18, 2021

Conversation

johnwlambert
Copy link
Contributor

Allow specification of GNC verbosity options via the wrapper.

@varunagrawal do we have gtwrap configured such that we can pipe stdout to the terminal? (ie that this verbosity will do anything for the wrapper?)

@ProfFan
Copy link
Collaborator

ProfFan commented Aug 14, 2021

@johnwlambert If you want to export the stdout log to a Python string, we do have a RedirectCout, but it needs a C++ side function. If not then the stdout will just print to the console, no problem, but you cannot access the log on the Python side.

@varunagrawal
Copy link
Collaborator

Fan is correct. If you're printing to the terminal, then the C++ cout will print without issues. If you've configured a logger on the python side, then the logger will not get the C++ output unless the printing is properly routed via the RedirectCout. Looking at the Pybind docs might help.

@johnwlambert
Copy link
Contributor Author

Got it, thanks @varunagrawal and @ProfFan. Just printing to the terminal is sufficient, no need to redirect the logger for my use case.

On another note, the wrap parser is complaining about how I'm treating the enum here from https://github.com/borglab/gtsam/blob/develop/gtsam/nonlinear/GncParams.h#L48.

Could one of yall take a look to see what I'm doing wrong?

@varunagrawal
Copy link
Collaborator

I'm not sure we support typedefs for enum values? I'm going to have to check.

@varunagrawal
Copy link
Collaborator

So yes we don't support typedefs for enums (yet). For your particular use case, you can follow this example from the tests in gtwrap:

class MCU {
  MCU();

  enum Avengers {
    CaptainAmerica,
    IronMan,
    Hulk,
    Hawkeye,
    Thor
  };

  enum GotG {
    Starlord,
    Gamorra,
    Rocket,
    Drax,
    Groot
  };

};

Comment on lines 527 to 536
virtual class GncParams {
GncParams(const PARAMS& baseOptimizerParams);
GncParams();
void setVerbosityGNC(const gtsam::GncParams::Verbosity value);
void print(const string& str) const;
};

typedef gtsam::GncParams::Verbosity::SILENT GncVerbositySilent;
typedef gtsam::GncParams::Verbosity::SUMMARY GncVerbositySummary;
typedef gtsam::GncParams::Verbosity::VALUES GncVerbosityValues;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
virtual class GncParams {
GncParams(const PARAMS& baseOptimizerParams);
GncParams();
void setVerbosityGNC(const gtsam::GncParams::Verbosity value);
void print(const string& str) const;
};
typedef gtsam::GncParams::Verbosity::SILENT GncVerbositySilent;
typedef gtsam::GncParams::Verbosity::SUMMARY GncVerbositySummary;
typedef gtsam::GncParams::Verbosity::VALUES GncVerbosityValues;
template<PARAMS>
virtual class GncParams {
GncParams(const PARAMS& baseOptimizerParams);
GncParams();
void setVerbosityGNC(const This::Verbosity value);
void print(const string& str) const;
enum Verbosity {
SILENT,
SUMMARY,
VALUES
};
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, Varun

@varunagrawal
Copy link
Collaborator

@johnwlambert you'll be interested in borglab/wrap#120 after which my suggested changes should work as expected.

@johnwlambert
Copy link
Contributor Author

it looks like gtwrap is not happy with this syntax, saying:

[ 94%] Building CXX object gtsam_unstable/CMakeFiles/gtsam_unstable.dir/slam/DummyFactor.cpp.o
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp: In function ‘void nonlinear(pybind11::module_&)’:
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1128:91: error: ‘This’ does not name a type
         .def("setVerbosityGNC",[](gtsam::GncParams<gtsam::GaussNewtonParams>* self, const This::Verbosity value){ self->setVerbosityGNC(value);}, py::arg("value"))
                                                                                           ^
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1128:155: error: expected ‘)’ before string constant
         .def("setVerbosityGNC",[](gtsam::GncParams<gtsam::GaussNewtonParams>* self, const This::Verbosity value){ self->setVerbosityGNC(value);}, py::arg("value"))
                                                                                                                                                           ^
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1128:155: error: expected ‘)’ before string constant
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp: In lambda function:
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1128:155: error: expected ‘{’ before string constant
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp: In function ‘void nonlinear(pybind11::module_&)’:
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1128:155: error: expected ‘)’ before string constant
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1128:163: error: expected ‘;’ before ‘)’ token
         .def("setVerbosityGNC",[](gtsam::GncParams<gtsam::GaussNewtonParams>* self, const This::Verbosity value){ self->setVerbosityGNC(value);}, py::arg("value"))
                                                                                                                                                                   ^
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1129:149: error: expected primary-expression before ‘,’ token
         .def("print",[](gtsam::GncParams<gtsam::GaussNewtonParams>* self, const string& str){ py::scoped_ostream_redirect output; self->print(str);}, py::arg("str"))
                                                                                                                                                     ^
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1135:22: error: expected primary-expression before ‘,’ token
                     }, py::arg("str"));
                      ^
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1147:98: error: ‘This’ does not name a type
         .def("setVerbosityGNC",[](gtsam::GncParams<gtsam::LevenbergMarquardtParams>* self, const This::Verbosity value){ self->setVerbosityGNC(value);}, py::arg("value"))
                                                                                                  ^
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1147:162: error: expected ‘)’ before string constant
         .def("setVerbosityGNC",[](gtsam::GncParams<gtsam::LevenbergMarquardtParams>* self, const This::Verbosity value){ self->setVerbosityGNC(value);}, py::arg("value"))
                                                                                                                                                                  ^
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1147:162: error: expected ‘)’ before string constant
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp: In lambda function:
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1147:162: error: expected ‘{’ before string constant
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp: In function ‘void nonlinear(pybind11::module_&)’:
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1147:162: error: expected ‘)’ before string constant
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1147:170: error: expected ‘;’ before ‘)’ token
         .def("setVerbosityGNC",[](gtsam::GncParams<gtsam::LevenbergMarquardtParams>* self, const This::Verbosity value){ self->setVerbosityGNC(value);}, py::arg("value"))
                                                                                                                                                                          ^
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1148:156: error: expected primary-expression before ‘,’ token
         .def("print",[](gtsam::GncParams<gtsam::LevenbergMarquardtParams>* self, const string& str){ py::scoped_ostream_redirect output; self->print(str);}, py::arg("str"))
                                                                                                                                                            ^
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1154:22: error: expected primary-expression before ‘,’ token
                     }, py::arg("str"));
                      ^
[ 94%] Building CXX object gtsam_unstable/CMakeFiles/gtsam_unstable.dir/slam/LocalOrientedPlane3Factor.cpp.o
In file included from /home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/pytypes.h:12:0,
                 from /home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/cast.h:13,
                 from /home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/attr.h:13,
                 from /home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/pybind11.h:44,
                 from /home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/numpy.h:12,
                 from /home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/eigen.h:12,
                 from /home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:13:
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/detail/common.h: In instantiation of ‘struct pybind11::detail::strip_function_object<nonlinear(pybind11::module_&)::<lambda(...)> >’:
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/detail/common.h:672:2:   required by substitution of ‘template<class Function, class F> using function_signature_t = pybind11::detail::conditional_t<std::is_function<_Arg>::value, F, typename std::conditional<(std::is_pointer<_Dp>::value || std::is_member_pointer<F>::value), std::remove_pointer<F>, pybind11::detail::strip_function_object<F> >::type::type> [with Function = nonlinear(pybind11::module_&)::<lambda(...)>; F = std::remove_reference<nonlinear(pybind11::module_&)::<lambda(...)> >::type]’
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/pybind11.h:71:9:   required from ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = nonlinear(pybind11::module_&)::<lambda(...)>; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}; <template-parameter-1-3> = void]’
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/pybind11.h:1194:73:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const char*, Func&&, const Extra& ...) [with Func = nonlinear(pybind11::module_&)::<lambda(...)>; Extra = {}; type_ = gtsam::GncParams<gtsam::GaussNewtonParams>; options = {boost::shared_ptr<gtsam::GncParams<gtsam::GaussNewtonParams> >}]’
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1128:162:   required from here
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/detail/common.h:659:71: error: no type named ‘type’ in ‘struct pybind11::detail::remove_class<void (nonlinear(pybind11::module_&)::<lambda(...)>::*)(...) const>’
     using type = typename remove_class<decltype(&F::operator())>::type;
                                                                       ^
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/detail/common.h: In instantiation of ‘struct pybind11::detail::strip_function_object<nonlinear(pybind11::module_&)::<lambda(...)> >’:
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/detail/common.h:672:2:   required by substitution of ‘template<class Function, class F> using function_signature_t = pybind11::detail::conditional_t<std::is_function<_Arg>::value, F, typename std::conditional<(std::is_pointer<_Dp>::value || std::is_member_pointer<F>::value), std::remove_pointer<F>, pybind11::detail::strip_function_object<F> >::type::type> [with Function = nonlinear(pybind11::module_&)::<lambda(...)>; F = std::remove_reference<nonlinear(pybind11::module_&)::<lambda(...)> >::type]’
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/pybind11.h:71:9:   required from ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = nonlinear(pybind11::module_&)::<lambda(...)>; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}; <template-parameter-1-3> = void]’
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/pybind11.h:1194:73:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const char*, Func&&, const Extra& ...) [with Func = nonlinear(pybind11::module_&)::<lambda(...)>; Extra = {}; type_ = gtsam::GncParams<gtsam::LevenbergMarquardtParams>; options = {boost::shared_ptr<gtsam::GncParams<gtsam::LevenbergMarquardtParams> >}]’
/home/runner/work/gtsam/gtsam/build/python/nonlinear.cpp:1147:169:   required from here
/home/runner/work/gtsam/gtsam/wrap/pybind11/include/pybind11/detail/common.h:659:71: error: no type named ‘type’ in ‘struct pybind11::detail::remove_class<void (nonlinear(pybind11::module_&)::<lambda(...)>::*)(...) const>’

@varunagrawal
Copy link
Collaborator

varunagrawal commented Sep 3, 2021

That's because you didn't pull the latest changes into the wrap subtree with the update_wrap.sh script.

@varunagrawal
Copy link
Collaborator

@johnwlambert you need to run ./update_wrap.sh in the top level directory of GTSAM before the This keyword is parsed correctly. This is because we made the changes to gtwrap but those changes haven't been pulled into the git subtree inside the GTSAM repo.

add6075e8 Merge pull request #121 from borglab/feature/constructor-templates
42d4145bb update instantiate_ctors to handle constructor level templates
455ce6169 update test fixtures
3c37fc2a0 update wrapper test fixtures
ffdad925d update interface_parser to pass the tests
bf7416213 add interface_parser test for templated constructor
9fe05d0c9 Merge pull request #120 from borglab/feature/templated-namespace
7622e6432 typo fix
88779c5e6 update instantiator to handle templates in the namespace
0ee86f9a3 add test for templated type in namespace

git-subtree-dir: wrap
git-subtree-split: add6075e8ec0e28d5f47d0a2ecab00deaa9a3da7
@varunagrawal varunagrawal requested review from ProfFan and removed request for ProfFan September 18, 2021 13:02
@varunagrawal varunagrawal merged commit a83992f into develop Sep 18, 2021
@varunagrawal varunagrawal deleted the wrapper-set-gnc-verbosity branch September 18, 2021 13:03
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