Skip to content
Snippets Groups Projects
Commit f5c79abe authored by Cyril Moineau's avatar Cyril Moineau
Browse files

[Binding] [Node] The operator() now use the python args system, avoiding the use of vector.

parent c994acd5
No related branches found
No related tags found
1 merge request!50Onnx tuto
...@@ -16,136 +16,150 @@ ...@@ -16,136 +16,150 @@
#include "aidge/graph/GraphView.hpp" #include "aidge/graph/GraphView.hpp"
#include "aidge/graph/Node.hpp" #include "aidge/graph/Node.hpp"
#include "aidge/graph/Connector.hpp"
#include "aidge/utils/Types.h" #include "aidge/utils/Types.h"
namespace py = pybind11; namespace py = pybind11;
namespace Aidge { namespace Aidge {
void init_Node(py::module& m) { void init_Node(py::module& m) {
py::class_<Node, std::shared_ptr<Node>>(m, "Node") py::class_<Node, std::shared_ptr<Node>>(m, "Node")
.def("name", &Node::name, .def("name", &Node::name,
R"mydelimiter( R"mydelimiter(
Name of the Node. Name of the Node.
)mydelimiter") )mydelimiter")
.def("type", &Node::type, .def("type", &Node::type,
R"mydelimiter( R"mydelimiter(
Type of the node. Type of the node.
)mydelimiter") )mydelimiter")
.def("get_operator", &Node::getOperator, .def("get_operator", &Node::getOperator,
R"mydelimiter( R"mydelimiter(
Get the Operator object of the Node. Get the Operator object of the Node.
)mydelimiter") )mydelimiter")
.def("set_name", &Node::setName, py::arg("name"), .def("set_name", &Node::setName, py::arg("name"),
R"mydelimiter( R"mydelimiter(
Set the Node name. Set the Node name.
:param name: New name for the node. :param name: New name for the node.
:type name: str :type name: str
:rtype: str :rtype: str
)mydelimiter") )mydelimiter")
.def("add_child", .def("add_child",
(void (Node::*)(std::shared_ptr<Node>, const IOIndex_t, IOIndex_t)) & (void (Node::*)(std::shared_ptr<Node>, const IOIndex_t, IOIndex_t)) &
Node::addChild, Node::addChild,
py::arg("other_node"), py::arg("out_id") = 0, py::arg("other_in_id") = gk_IODefaultIndex, py::arg("other_node"), py::arg("out_id") = 0, py::arg("other_in_id") = gk_IODefaultIndex,
R"mydelimiter( R"mydelimiter(
Link another Node to an output of the current Node. Link another Node to an output of the current Node.
:param other_node: Pointer to the other Node. :param other_node: Pointer to the other Node.
:type other_node: :py:class: Node :type other_node: :py:class: Node
:param out_id: ID of the current Node output to connect to the other Node. Default to 0. :param out_id: ID of the current Node output to connect to the other Node. Default to 0.
:type out_id: int :type out_id: int
:param other_in_id: ID of the other Node input to connect to the current Node. Default to the first avaible data input. :param other_in_id: ID of the other Node input to connect to the current Node. Default to the first avaible data input.
:type other_in_id: int :type other_in_id: int
)mydelimiter") )mydelimiter")
.def("add_child", .def("add_child",
(void (Node::*)(std::shared_ptr<GraphView>, const IOIndex_t, (void (Node::*)(std::shared_ptr<GraphView>, const IOIndex_t,
std::pair<std::shared_ptr<Node>, IOIndex_t>)) & std::pair<std::shared_ptr<Node>, IOIndex_t>)) &
Node::addChild, Node::addChild,
py::arg("other_graph"), py::arg("out_id") = 0, py::arg("other_graph"), py::arg("out_id") = 0,
py::arg("other_in_id") = py::arg("other_in_id") =
std::pair<std::shared_ptr<Node>, IOIndex_t>(nullptr, gk_IODefaultIndex), std::pair<std::shared_ptr<Node>, IOIndex_t>(nullptr, gk_IODefaultIndex),
R"mydelimiter( R"mydelimiter(
Link a Node from a specific GraphView to the current Node. Link a Node from a specific GraphView to the current Node.
:param other_view: Pointer to the GraphView whose content should be linked to the current Node. :param other_view: Pointer to the GraphView whose content should be linked to the current Node.
:type other_view: :py:class: GraphView :type other_view: :py:class: GraphView
:param out_id: ID of the current Node output to connect to the other Node. Default to 0. :param out_id: ID of the current Node output to connect to the other Node. Default to 0.
:type out_id: int :type out_id: int
:param other_in_id: Pair of Node and input connection ID for specifying the connection. If the GraphView whose content is linked has only one input Node, then it defaults to the first available data input ID of this Node. :param other_in_id: Pair of Node and input connection ID for specifying the connection. If the GraphView whose content is linked has only one input Node, then it defaults to the first available data input ID of this Node.
:type other_in_id: tuple[:py:class: Node, int] :type other_in_id: tuple[:py:class: Node, int]
)mydelimiter") )mydelimiter")
.def("inputs", &Node::inputs, .def("inputs", &Node::inputs,
R"mydelimiter( R"mydelimiter(
Get ordered list of parent Node and the associated output index connected to the current Node's inputs. Get ordered list of parent Node and the associated output index connected to the current Node's inputs.
:return: List of connections. When an input is not linked to any parent, the default value is (None, default_index) :return: List of connections. When an input is not linked to any parent, the default value is (None, default_index)
:rtype: list[tuple[Node, int]] :rtype: list[tuple[Node, int]]
)mydelimiter") )mydelimiter")
.def("input", &Node::input, py::arg("in_id"), .def("input", &Node::input, py::arg("in_id"),
R"mydelimiter( R"mydelimiter(
Get the parent Node and the associated output index connected to the i-th input of the current Node. Get the parent Node and the associated output index connected to the i-th input of the current Node.
:param in_id: input index of the current Node object. :param in_id: input index of the current Node object.
:type in_id: int :type in_id: int
:return: i-th connection. When an input is not linked to any parent, the default value is (None, default_index) :return: i-th connection. When an input is not linked to any parent, the default value is (None, default_index)
:rtype: tuple[Node, int] :rtype: tuple[Node, int]
)mydelimiter") )mydelimiter")
.def("outputs", &Node::outputs, .def("outputs", &Node::outputs,
R"mydelimiter( R"mydelimiter(
Get, for each output of the Node, a list of the children Node and the associated input index connected to it. Get, for each output of the Node, a list of the children Node and the associated input index connected to it.
:return: List of a list of connections. When an outut is not linked to any child, its list a empty. :return: List of a list of connections. When an outut is not linked to any child, its list a empty.
:rtype: list[list[tuple[Node, int]]] :rtype: list[list[tuple[Node, int]]]
)mydelimiter") )mydelimiter")
.def("output", &Node::output, py::arg("out_id"), .def("output", &Node::output, py::arg("out_id"),
R"mydelimiter( R"mydelimiter(
Get a list of the children Node for a specific output and the associated input index connected to it. Get a list of the children Node for a specific output and the associated input index connected to it.
:param out_id: input index of the current Node object. :param out_id: input index of the current Node object.
:type out_id: int :type out_id: int
:return: i-th connection. When an input is not linked to any parent, the default value is (None, default_index) :return: i-th connection. When an input is not linked to any parent, the default value is (None, default_index)
:rtype: list[tuple[Node, int]] :rtype: list[tuple[Node, int]]
)mydelimiter") )mydelimiter")
.def("get_nb_inputs", &Node::nbInputs, .def("get_nb_inputs", &Node::nbInputs,
R"mydelimiter( R"mydelimiter(
Number of inputs. Number of inputs.
:rtype: int :rtype: int
)mydelimiter") )mydelimiter")
.def("get_nb_datainputs", &Node::nbDataInputs, .def("get_nb_datainputs", &Node::nbDataInputs,
R"mydelimiter( R"mydelimiter(
Number of data inputs. Number of data inputs.
:rtype: int :rtype: int
)mydelimiter") )mydelimiter")
.def("get_nb_outputs", &Node::nbOutputs, .def("get_nb_outputs", &Node::nbOutputs,
R"mydelimiter( R"mydelimiter(
Number of outputs. Number of outputs.
:rtype: int :rtype: int
)mydelimiter") )mydelimiter")
.def("get_parents", &Node::getParents, .def("get_parents", &Node::getParents,
R"mydelimiter( R"mydelimiter(
Get parents. Get parents.
)mydelimiter") )mydelimiter")
.def("get_children", (std::set<std::shared_ptr<Node>> (Node::*)() const) &Node::getChildren, .def("get_children", (std::set<std::shared_ptr<Node>> (Node::*)() const) &Node::getChildren,
R"mydelimiter( R"mydelimiter(
Get children. Get children.
)mydelimiter") )mydelimiter")
.def("__call__", &Node::operator(), py::arg("connectors")); .def("__call__",
[](Node &self, pybind11::args args) {
std::vector<Connector> connectors;
for (const auto &arg : args) {
// Check if the argument is an instance of Connector
if (pybind11::isinstance<Connector>(arg)) {
// Convert Python object to C++ object adn push it ot vector
connectors.push_back(arg.cast<Connector>());
} else {
throw std::runtime_error("One of the arguments was not a Connector.");
}
}
return self(connectors);
});
} }
} // namespace Aidge } // namespace Aidge
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment