Skip to content
Snippets Groups Projects
Commit c8b2cf0b authored by Maxence Naud's avatar Maxence Naud
Browse files

UPD: Python binding for Data, and add Python binding file for DataType and DataFormat

parent b2b034c4
No related branches found
No related tags found
2 merge requests!318[Upd] release verision 0.5.0,!307[UPD] Tensor formatting
...@@ -10,65 +10,14 @@ ...@@ -10,65 +10,14 @@
********************************************************************************/ ********************************************************************************/
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "aidge/data/Data.hpp" #include "aidge/data/Data.hpp"
#include "aidge/data/DataType.hpp"
#include "aidge/data/DataFormat.hpp"
namespace py = pybind11; namespace py = pybind11;
namespace Aidge { 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){ void init_Data(py::module& m){
bindEnum<DataType>(m, "dtype");
bindEnum<DataFormat>(m, "dformat");
py::class_<Data, std::shared_ptr<Data>>(m,"Data"); 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
/********************************************************************************
* 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
/********************************************************************************
* 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
...@@ -19,6 +19,8 @@ namespace Aidge { ...@@ -19,6 +19,8 @@ namespace Aidge {
void init_CoreSysInfo(py::module&); void init_CoreSysInfo(py::module&);
void init_Random(py::module&); void init_Random(py::module&);
void init_Data(py::module&); void init_Data(py::module&);
void init_DataFormat(py::module&);
void init_DataType(py::module&);
void init_Database(py::module&); void init_Database(py::module&);
void init_DataProvider(py::module&); void init_DataProvider(py::module&);
void init_Interpolation(py::module&); void init_Interpolation(py::module&);
...@@ -110,6 +112,8 @@ void init_Aidge(py::module& m) { ...@@ -110,6 +112,8 @@ void init_Aidge(py::module& m) {
init_Random(m); init_Random(m);
init_Data(m); init_Data(m);
init_DataFormat(m);
init_DataType(m);
init_Database(m); init_Database(m);
init_DataProvider(m); init_DataProvider(m);
init_Interpolation(m); init_Interpolation(m);
......
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