Skip to content

Commit

Permalink
A lot of minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Popov-Dmitriy-Ivanovich committed Dec 6, 2023
1 parent fb3f7d7 commit e4d9b33
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 347 deletions.
6 changes: 3 additions & 3 deletions src/core/algorithms/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ class Algorithm {

std::unordered_map<std::string_view, std::unique_ptr<config::OptValue>> GetOptValues() {
std::unordered_map<std::string_view, std::unique_ptr<config::OptValue>> opt_values;
for (auto& i : possible_options_) {
if (i.second->IsSet()) {
opt_values[i.second->GetName()] = i.second->GetOptValue();
for (auto& [name, option] : possible_options_) {
if (option->IsSet()) {
opt_values[name] = option->GetOptValue();
}
}
return opt_values;
Expand Down
2 changes: 1 addition & 1 deletion src/python_bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ PYBIND11_MODULE(desbordante, module) {
"Get info about the option's type")
.def("execute", &PyAlgorithmBase::Execute, "Process data")
.def("get_opts", &PyAlgorithmBase::GetOpts,
"Get options values represented as strings");
"Get options values represented as closest python type");

DEFINE_ALGORITHM_BASE(ArAlgorithm).def("get_ars", &PyArAlgorithmBase::GetARs);
DEFINE_ALGORITHM_BASE(FdAlgorithm).def("get_fds", &PyFdAlgorithmBase::GetFDs);
Expand Down
34 changes: 34 additions & 0 deletions src/python_bindings/opt_to_py.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "opt_to_py.h"
namespace {
namespace py = pybind11;
using ConvFunction = std::function<py::object(boost::any)>;

template <typename T>
std::pair<std::type_index, ConvFunction> normal_conv_pair{
std::type_index(typeid(T)),
[](boost::any value) { return py::cast(boost::any_cast<T>(value)); }};

template <typename T>
std::pair<std::type_index, ConvFunction> enum_conv_pair{
std::type_index(typeid(T)),
[](boost::any value) { return py::cast(boost::any_cast<T>(value)._to_string()); }};
const std::unordered_map<std::type_index, ConvFunction> converters{
normal_conv_pair<int>,
normal_conv_pair<double>,
normal_conv_pair<long double>,
normal_conv_pair<unsigned int>,
normal_conv_pair<bool>,
normal_conv_pair<config::ThreadNumType>,
normal_conv_pair<config::MaxLhsType>,
normal_conv_pair<config::ErrorType>,
normal_conv_pair<config::IndicesType>,
enum_conv_pair<algos::metric::MetricAlgo>,
enum_conv_pair<algos::metric::Metric>,
enum_conv_pair<algos::InputFormat>};
} // namespace

namespace python_bindings {
py::object opt_to_py(std::type_index type, boost::any val) {
return converters.at(type)(val);
}
} // namespace python_bindings
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
#pragma once

#include <memory>
#include <type_traits>
#include <unordered_map>
#include <vector>

#include <boost/any.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "algorithms/algorithm.h"
#include "algorithms/metric/enums.h"
#include "association_rules/ar_algorithm_enums.h"
#include "config/equal_nulls/type.h"
#include "config/error/type.h"
#include "config/indices/type.h"
#include "config/max_lhs/type.h"
#include "config/names.h"
#include "config/tabular_data/input_table_type.h"
#include "config/thread_number/type.h"
#include "model/types/types.h"

namespace python_bindings {
namespace py = pybind11;
py::object opt_to_str(std::type_index type, boost::any val);
pybind11::object opt_to_py(std::type_index type, boost::any val);
} // namespace python_bindings
52 changes: 0 additions & 52 deletions src/python_bindings/opt_to_str.cpp

This file was deleted.

18 changes: 6 additions & 12 deletions src/python_bindings/py_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@
#include <pybind11/stl.h>

#include "algorithms/algorithm.h"
#include "algorithms/metric/enums.h"
#include "config/equal_nulls/type.h"
#include "config/error/type.h"
#include "config/indices/type.h"
#include "config/max_lhs/type.h"
#include "config/names.h"
#include "config/tabular_data/input_table_type.h"
#include "config/thread_number/type.h"
#include "model/types/types.h"
#include "opt_to_str.h"
#include "opt_to_py.h"

namespace python_bindings {

Expand Down Expand Up @@ -58,14 +52,14 @@ class PyAlgorithmBase {
void LoadData();

pybind11::int_ Execute(pybind11::kwargs const& kwargs);
std::unordered_map<std::string, py::object> GetOpts() {
std::unordered_map<std::string, pybind11::object> GetOpts() {
auto opt_ = algorithm_->GetOptValues();
std::unordered_map<std::string, py::object> res;
for (const auto& [name, value] : opt_) {
if (name == config::names::kTable || name == config::names::kInputFormat) {
std::unordered_map<std::string, pybind11::object> res;
for (const auto& [name, value_info] : opt_) {
if (name == config::names::kTable) {
continue;
}
res[std::string(name)] = opt_to_str(value->type, value->value);
res[std::string(name)] = opt_to_py(value_info->type, value_info->value);
}
return res;
}
Expand Down
1 change: 0 additions & 1 deletion src/python_bindings/py_to_any.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <pybind11/pybind11.h>

namespace python_bindings {

[[nodiscard]] boost::any PyToAny(std::string_view option_name, std::type_index index,
pybind11::handle obj);
} // namespace python_bindings
Loading

0 comments on commit e4d9b33

Please sign in to comment.