Skip to content

Commit

Permalink
Merge pull request #432 from ami-iit/bindings/toml
Browse files Browse the repository at this point in the history
Develop the python bindings for toml implementation of the parameters handler
  • Loading branch information
GiulioRomualdi authored Oct 26, 2021
2 parents 9303539 + 1e3a6fb commit 9844d83
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project are documented in this file.
### Added
- Implement Python bindings for the TSID component (https://github.com/ami-iit/bipedal-locomotion-framework/pull/428)
- Add the possibility to set the name of each element of a variable stored in the variables handler (https://github.com/ami-iit/bipedal-locomotion-framework/pull/429)
- Develop the python bindings for toml implementation of the parameters handler (https://github.com/ami-iit/bipedal-locomotion-framework/pull/432)
- Implement the VariableRegularizationTask in TSID (https://github.com/ami-iit/bipedal-locomotion-framework/pull/431)

### Changed
Expand Down
15 changes: 15 additions & 0 deletions bindings/python/ParametersHandler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@ if(FRAMEWORK_COMPILE_YarpImplementation)


endif()


if(FRAMEWORK_COMPILE_TomlImplementation)

add_bipedal_locomotion_python_module(
NAME ParametersHandlerTomlImplementationBindings
SOURCES src/TomlParametersHandler.cpp src/TomlModule.cpp
HEADERS ${H_PREFIX}/TomlParametersHandler.h ${H_PREFIX}/TomlModule.h
LINK_LIBRARIES BipedalLocomotion::ParametersHandlerTomlImplementation
TESTS tests/test_parameters_handler_toml.py
)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/config.toml ${PROJECT_BINARY_DIR}/config.toml COPYONLY)

endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file TomlModule.h
* @authors Giulio Romualdi
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version.
*/

#ifndef BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_TOML_MODULE_H
#define BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_TOML_MODULE_H

#include <pybind11/pybind11.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ParametersHandler
{

void CreateTomlModule(pybind11::module& module);

} // namespace ParametersHandler
} // namespace bindings
} // namespace BipedalLocomotion

#endif // BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_TOML_MODULE_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file TomlParametersHandler
* @authors Giulio Romualdi
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version.
*/

#ifndef BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_TOML_PARAMETERS_HANDLER_H
#define BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_TOML_PARAMETERS_HANDLER_H

#include <pybind11/pybind11.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ParametersHandler
{

void CreateTomlParameterHandler(pybind11::module& module);

} // namespace ParametersHandler
} // namespace bindings
} // namespace BipedalLocomotion

#endif // BIPEDAL_LOCOMOTION_BINDINGS_PARAMETERS_HANDLER_TOML_PARAMETERS_HANDLER_H
25 changes: 25 additions & 0 deletions bindings/python/ParametersHandler/src/TomlModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file TomlModule.cpp
* @authors Giulio Romualdi
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version.
*/

#include <pybind11/pybind11.h>

#include <BipedalLocomotion/bindings/ParametersHandler/TomlModule.h>
#include <BipedalLocomotion/bindings/ParametersHandler/TomlParametersHandler.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ParametersHandler
{
void CreateTomlModule(pybind11::module& module)
{
CreateTomlParameterHandler(module);
}
} // namespace ParametersHandler
} // namespace bindings
} // namespace BipedalLocomotion
36 changes: 36 additions & 0 deletions bindings/python/ParametersHandler/src/TomlParametersHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @file TomlParametersHandler.cpp
* @authors Giulio Romualdi
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version.
*/

#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <BipedalLocomotion/ParametersHandler/IParametersHandler.h>
#include <BipedalLocomotion/ParametersHandler/TomlImplementation.h>
#include <BipedalLocomotion/bindings/ParametersHandler/TomlParametersHandler.h>

namespace BipedalLocomotion
{
namespace bindings
{
namespace ParametersHandler
{

void CreateTomlParameterHandler(pybind11::module& module)
{
namespace py = ::pybind11;
using namespace BipedalLocomotion::ParametersHandler;
py::class_<TomlImplementation, //
std::shared_ptr<TomlImplementation>,
IParametersHandler>(module, "TomlParametersHandler")
.def(py::init())
.def("set_from_file", &TomlImplementation::setFromFile, py::arg("file_name"));
}

} // namespace ParametersHandler
} // namespace bindings
} // namespace BipedalLocomotion
9 changes: 9 additions & 0 deletions bindings/python/ParametersHandler/tests/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
answer_to_the_ultimate_question_of_life = 42
pi = 3.14
John = "Smith"
"Fibonacci Numbers" = [1, 1, 2, 3, 5, 8, 13, 21]

[CARTOONS]
"Donald's nephews" = ["Huey", "Dewey", "Louie"]
Fibonacci_Numbers = [1, 1, 2, 3, 5, 8, 13, 21]
John = "Doe"
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import pytest
pytestmark = pytest.mark.parameters_handler_toml

import bipedal_locomotion_framework.bindings.parameters_handler as blf
import numpy as np

def test_bool():

handler = blf.TomlParametersHandler()

handler.set_parameter_bool(name="my_bool", value=True)

assert handler.get_parameter_bool(name="my_bool") is True

with pytest.raises(ValueError):
handler.get_parameter_int(name="my_bool")

with pytest.raises(ValueError):
handler.get_parameter_float(name="my_bool")

with pytest.raises(ValueError):
handler.get_parameter_string(name="my_bool")


def test_int():

handler = blf.TomlParametersHandler()

handler.set_parameter_int(name="my_int", value=42)

assert handler.get_parameter_int(name="my_int") == 42

with pytest.raises(ValueError):
handler.get_parameter_bool(name="my_int")

with pytest.raises(ValueError):
handler.get_parameter_float(name="my_int")

with pytest.raises(ValueError):
handler.get_parameter_string(name="my_int")


def test_float():

handler = blf.TomlParametersHandler()

handler.set_parameter_float(name="my_float", value=3.1415)

assert handler.get_parameter_float(name="my_float") == pytest.approx(3.1415)

with pytest.raises(ValueError):
handler.get_parameter_bool(name="my_float")

with pytest.raises(ValueError):
handler.get_parameter_int(name="my_float")

with pytest.raises(ValueError):
handler.get_parameter_string(name="my_float")


def test_string():

handler = blf.TomlParametersHandler()

handler.set_parameter_string(name="my_string", value="foo")

assert handler.get_parameter_string(name="my_string") == "foo"

with pytest.raises(ValueError):
handler.get_parameter_bool(name="my_string")

with pytest.raises(ValueError):
handler.get_parameter_int(name="my_string")

with pytest.raises(ValueError):
handler.get_parameter_float(name="my_string")


def test_vector_bool():

handler = blf.TomlParametersHandler()

handler.set_parameter_vector_bool(name="my_vector_bool",value= [True, False, True])

assert handler.get_parameter_vector_bool(name="my_vector_bool") == [True, False, True]

with pytest.raises(ValueError):
handler.get_parameter_vector_int(name="my_vector_bool")

with pytest.raises(ValueError):
handler.get_parameter_vector_float(name="my_vector_bool")

with pytest.raises(ValueError):
handler.get_parameter_vector_string(name="my_vector_bool")


def test_vector_int():

handler = blf.TomlParametersHandler()

handler.set_parameter_vector_int(name="my_vector_int", value=[-1, 2, 10])

assert handler.get_parameter_vector_int(name="my_vector_int") == [-1, 2, 10]

with pytest.raises(ValueError):
handler.get_parameter_vector_bool(name="my_vector_int")

with pytest.raises(ValueError):
handler.get_parameter_vector_float(name="my_vector_int")

with pytest.raises(ValueError):
handler.get_parameter_vector_string(name="my_vector_int")


def test_vector_float():

handler = blf.TomlParametersHandler()

handler.set_parameter_vector_float(name="my_vector_float",
value=[-3.14, 2.7182, 42.0])

assert handler.get_parameter_vector_float(name="my_vector_float") == \
pytest.approx([-3.14, 2.7182, 42.0])

with pytest.raises(ValueError):
handler.get_parameter_vector_bool(name="my_vector_float")

with pytest.raises(ValueError):
handler.get_parameter_vector_int(name="my_vector_float")

with pytest.raises(ValueError):
handler.get_parameter_vector_string(name="my_vector_float")


def test_vector_string():

handler = blf.TomlParametersHandler()

handler.set_parameter_vector_string(name="my_vector_string",
value=["foo", "bar", "bipedal", "locomotion"])

assert handler.get_parameter_vector_string(name="my_vector_string") == \
["foo", "bar", "bipedal", "locomotion"]

with pytest.raises(ValueError):
handler.get_parameter_vector_bool(name="my_vector_string")

with pytest.raises(ValueError):
handler.get_parameter_vector_int(name="my_vector_string")

with pytest.raises(ValueError):
handler.get_parameter_vector_float(name="my_vector_string")


def test_vector_mixed():

handler = blf.TomlParametersHandler()

# 1. Mixed vector: store as more general type float
handler.set_parameter_vector_float(name="to_float", value=[42.0, 1, -3.14, False])

assert handler.get_parameter_vector_float(name="to_float") == \
pytest.approx([42.0, 1.0, -3.14, 0.0])

# 2. Mixed vector: store as more general type int
handler.set_parameter_vector_float(name="to_int", value=[42, 1, -3, False])

assert handler.get_parameter_vector_float(name="to_int") == \
pytest.approx([42, 1, -3, 0])

# 3. Mixed vector: store as less general type int
with pytest.raises(TypeError):
handler.set_parameter_vector_int(name="to_int_fail",
value=[42.0, 1, -3.14, False])


def test_clear():

handler = blf.TomlParametersHandler()

handler.set_parameter_bool(name="my_bool1", value=False)
handler.set_parameter_bool(name="my_bool2", value=True)
handler.set_parameter_float(name="my_float", value=-42.42)
handler.set_parameter_vector_string(name="my_vector_string", value=["bar", "foo"])

handler.clear()

with pytest.raises(ValueError):
_ = handler.get_parameter_bool(name="my_bool1")

with pytest.raises(ValueError):
_ = handler.get_parameter_bool(name="my_bool2")

with pytest.raises(ValueError):
_ = handler.get_parameter_float(name="my_float")

with pytest.raises(ValueError):
_ = handler.get_parameter_vector_string(name="my_float")


def test_load_from_file():

handler = blf.TomlParametersHandler()
assert handler.set_from_file('config.toml') == True

assert handler.get_parameter_int("answer_to_the_ultimate_question_of_life") == 42
assert handler.get_group("CARTOONS").get_parameter_string("John") == "Doe"
8 changes: 8 additions & 0 deletions bindings/python/bipedal_locomotion_framework.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <BipedalLocomotion/bindings/ParametersHandler/YarpModule.h>
@endcmakeif FRAMEWORK_COMPILE_YarpImplementation

@cmakeif FRAMEWORK_COMPILE_TomlImplementation
#include <BipedalLocomotion/bindings/ParametersHandler/TomlModule.h>
@endcmakeif FRAMEWORK_COMPILE_TomlImplementation

@cmakeif FRAMEWORK_COMPILE_System
#include <BipedalLocomotion/bindings/System/Module.h>
@endcmakeif FRAMEWORK_COMPILE_System
Expand Down Expand Up @@ -67,6 +71,10 @@ PYBIND11_MODULE(bindings, m)
bindings::ParametersHandler::CreateYarpModule(parametersHandlerModule);
@endcmakeif FRAMEWORK_COMPILE_YarpImplementation

@cmakeif FRAMEWORK_COMPILE_TomlImplementation
bindings::ParametersHandler::CreateTomlModule(parametersHandlerModule);
@endcmakeif FRAMEWORK_COMPILE_TomlImplementation

py::module mathModule = m.def_submodule("math");
bindings::Math::CreateModule(mathModule);

Expand Down

0 comments on commit 9844d83

Please sign in to comment.