diff --git a/python_binding/data/pybind_Data.cpp b/python_binding/data/pybind_Data.cpp index 02a692dea47a1ba270df5b7c710db0c07da2043a..52e773b6962c5de8eb7499be03c4f2deb0be24c9 100644 --- a/python_binding/data/pybind_Data.cpp +++ b/python_binding/data/pybind_Data.cpp @@ -10,65 +10,14 @@ ********************************************************************************/ #include <pybind11/pybind11.h> -#include <pybind11/stl.h> #include "aidge/data/Data.hpp" -#include "aidge/data/DataType.hpp" -#include "aidge/data/DataFormat.hpp" namespace py = pybind11; namespace Aidge { -template <class T> -void bindEnum(py::module& m, const std::string& name) { - // Define enumeration names for python as lowercase type name - // This defined enum names compatible with basic numpy type - // name such as: float32, flot64, [u]int32, [u]int64, ... - auto python_enum_name = [](const T& type) { - auto str_lower = [](std::string& str) { - std::transform(str.begin(), str.end(), str.begin(), - [](unsigned char c){ - return std::tolower(c); - }); - }; - auto type_name = std::string(Aidge::format_as(type)); - str_lower(type_name); - return type_name; - }; - // Auto generate enumeration names from lowercase type strings - std::vector<std::string> enum_names; - for (auto type_str : EnumStrings<T>::data) { - auto type = static_cast<T>(enum_names.size()); - auto enum_name = python_enum_name(type); - enum_names.push_back(enum_name); - } - - // Define python side enumeration aidge_core.type - auto e_type = py::enum_<T>(m, name.c_str()); - - // Add enum value for each enum name - for (std::size_t idx = 0; idx < enum_names.size(); idx++) { - e_type.value(enum_names[idx].c_str(), static_cast<T>(idx)); - } - - // Define str() to return the bare enum name value, it allows - // to compare directly for instance str(tensor.type()) - // with str(nparray.type) - e_type.def("__str__", [enum_names](const T& type) { - return enum_names[static_cast<int>(type)]; - }, py::prepend());; -} - void init_Data(py::module& m){ - bindEnum<DataType>(m, "dtype"); - bindEnum<DataFormat>(m, "dformat"); - py::class_<Data, std::shared_ptr<Data>>(m,"Data"); - - - m.def("format_as", (const char* (*)(DataType)) &format_as, py::arg("dt")); - m.def("format_as", (const char* (*)(DataFormat)) &format_as, py::arg("df")); - m.def("get_data_format_transpose", &getDataFormatTranspose, py::arg("src"), py::arg("dst")); - -} } + +} // namespace Aidge diff --git a/python_binding/data/pybind_DataFormat.cpp b/python_binding/data/pybind_DataFormat.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a63df321c3298284df7de8fd2c3eb0fc0cecae24 --- /dev/null +++ b/python_binding/data/pybind_DataFormat.cpp @@ -0,0 +1,72 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <algorithm> // std::transform +#include <cctype> // std::tolower +#include <string> // std::string +#include <vector> + +#include <pybind11/pybind11.h> +#include <pybind11/stl.h> + +#include "aidge/data/DataFormat.hpp" + +namespace py = pybind11; +namespace Aidge { + +template <class T> +void bindEnum(py::module& m, const std::string& name) { + // Define enumeration names for python as lowercase type name + // This defined enum names compatible with basic numpy type + // name such as: float32, flot64, [u]int32, [u]int64, ... + auto python_enum_name = [](const T& type) { + auto str_lower = [](std::string& str) { + std::transform(str.begin(), str.end(), str.begin(), + [](unsigned char c){ + return std::tolower(c); + }); + }; + auto type_name = std::string(Aidge::format_as(type)); + str_lower(type_name); + return type_name; + }; + + // Auto generate enumeration names from lowercase type strings + std::vector<std::string> enum_names; + for (auto type_str : EnumStrings<T>::data) { + auto type = static_cast<T>(enum_names.size()); + auto enum_name = python_enum_name(type); + enum_names.push_back(enum_name); + } + + // Define python side enumeration aidge_core.type + auto e_type = py::enum_<T>(m, name.c_str()); + + // Add enum value for each enum name + for (std::size_t idx = 0; idx < enum_names.size(); idx++) { + e_type.value(enum_names[idx].c_str(), static_cast<T>(idx)); + } + + // Define str() to return the bare enum name value, it allows + // to compare directly for instance str(tensor.type()) + // with str(nparray.type) + e_type.def("__str__", [enum_names](const T& type) { + return enum_names[static_cast<int>(type)]; + }, py::prepend()); +} + +void init_DataFormat(py::module& m) { + bindEnum<DataFormat>(m, "dformat"); + m.def("format_as", (const char* (*)(DataFormat)) &format_as, py::arg("df")); + m.def("get_data_format_transpose", &getDataFormatTranspose, py::arg("src"), py::arg("dst")); +} + +} // namespace Aidge diff --git a/python_binding/data/pybind_DataType.cpp b/python_binding/data/pybind_DataType.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6aab399760c52b3886cddb0fb26c2b649252e7bb --- /dev/null +++ b/python_binding/data/pybind_DataType.cpp @@ -0,0 +1,71 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <algorithm> // std::transform +#include <cctype> // std::tolower +#include <string> // std::string +#include <vector> + +#include <pybind11/pybind11.h> +#include <pybind11/stl.h> + +#include "aidge/data/DataType.hpp" + +namespace py = pybind11; +namespace Aidge { + +template <class T> +void bindEnum(py::module& m, const std::string& name) { + // Define enumeration names for python as lowercase type name + // This defined enum names compatible with basic numpy type + // name such as: float32, flot64, [u]int32, [u]int64, ... + auto python_enum_name = [](const T& type) { + auto str_lower = [](std::string& str) { + std::transform(str.begin(), str.end(), str.begin(), + [](unsigned char c){ + return std::tolower(c); + }); + }; + auto type_name = std::string(Aidge::format_as(type)); + str_lower(type_name); + return type_name; + }; + + // Auto generate enumeration names from lowercase type strings + std::vector<std::string> enum_names; + for (auto type_str : EnumStrings<T>::data) { + auto type = static_cast<T>(enum_names.size()); + auto enum_name = python_enum_name(type); + enum_names.push_back(enum_name); + } + + // Define python side enumeration aidge_core.type + auto e_type = py::enum_<T>(m, name.c_str()); + + // Add enum value for each enum name + for (std::size_t idx = 0; idx < enum_names.size(); idx++) { + e_type.value(enum_names[idx].c_str(), static_cast<T>(idx)); + } + + // Define str() to return the bare enum name value, it allows + // to compare directly for instance str(tensor.type()) + // with str(nparray.type) + e_type.def("__str__", [enum_names](const T& type) { + return enum_names[static_cast<int>(type)]; + }, py::prepend()); +} + +void init_DataType(py::module& m) { + bindEnum<DataType>(m, "dtype"); + m.def("format_as", (const char* (*)(DataType)) &format_as, py::arg("dt")); +} + +} // namespace Aidge diff --git a/python_binding/pybind_core.cpp b/python_binding/pybind_core.cpp index c292a893779196707a3206f899d49d896bb7c2d6..435badb6cfe3628815d76f0105d8a1680f17a86a 100644 --- a/python_binding/pybind_core.cpp +++ b/python_binding/pybind_core.cpp @@ -19,6 +19,8 @@ namespace Aidge { void init_CoreSysInfo(py::module&); void init_Random(py::module&); void init_Data(py::module&); +void init_DataFormat(py::module&); +void init_DataType(py::module&); void init_Database(py::module&); void init_DataProvider(py::module&); void init_Interpolation(py::module&); @@ -110,6 +112,8 @@ void init_Aidge(py::module& m) { init_Random(m); init_Data(m); + init_DataFormat(m); + init_DataType(m); init_Database(m); init_DataProvider(m); init_Interpolation(m);