Skip to content

Commit

Permalink
Add Python bindings for INDs (Spider and Faida)
Browse files Browse the repository at this point in the history
Add Python bindings for Spider and Faida include
dependency detection algorithms
  • Loading branch information
p-senichenkov authored and chernishev committed Mar 8, 2024
1 parent 9c85c8a commit 1fafc37
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/python_bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "data/bind_data_types.h"
#include "fd/bind_fd.h"
#include "fd/bind_fd_verification.h"
#include "ind/bind_ind.h"
#include "mfd/bind_mfd_verification.h"
#include "statistics/bind_statistics.h"
#include "ucc/bind_ucc.h"
Expand All @@ -31,7 +32,7 @@ PYBIND11_MODULE(desbordante, module) {

for (auto bind_func :
{BindMainClasses, BindDataTypes, BindFd, BindAr, BindUcc, BindAc, BindFdVerification,
BindMfdVerification, BindUccVerification, BindStatistics}) {
BindMfdVerification, BindUccVerification, BindStatistics, BindInd}) {
bind_func(module);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/python_bindings/data/bind_data_types.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "bind_data_types.h"

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

#include "config/tabular_data/input_table_type.h"
#include "model/table/column_combination.h"

namespace {
namespace py = pybind11;
Expand All @@ -17,5 +19,11 @@ void BindDataTypes(py::module_& main_module) {
Currently only used as tags for Algorithm.get_option_type
)doc";
py::class_<config::InputTable>(data_module, "Table");

using namespace model;
py::class_<ColumnCombination>(data_module, "ColumnCombination")
.def("__str__", &ColumnCombination::ToString)
.def_property_readonly("table_index", &ColumnCombination::GetTableIndex)
.def_property_readonly("column_indices", &ColumnCombination::GetColumnIndices);
}
} // namespace python_bindings
27 changes: 27 additions & 0 deletions src/python_bindings/ind/bind_ind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "ind/bind_ind.h"

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

#include "algorithms/ind/ind.h"
#include "algorithms/ind/ind_algorithm.h"
#include "algorithms/ind/mining_algorithms.h"
#include "py_util/bind_primitive.h"

namespace py = pybind11;

namespace python_bindings {
void BindInd(py::module_& main_module) {
using namespace model;

auto ind_module = main_module.def_submodule("ind");
py::class_<IND>(ind_module, "IND")
.def("__str__", &IND::ToString)
.def("get_lhs", &IND::GetLhs)
.def("get_rhs", &IND::GetRhs);

using namespace algos;
auto ind_algos_module = BindPrimitive<Spider, Faida>(
ind_module, &INDAlgorithm::INDList, "IndAlgorithm", "get_inds", {"Spider", "Faida"});
}
} // namespace python_bindings
7 changes: 7 additions & 0 deletions src/python_bindings/ind/bind_ind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <pybind11/pybind11.h>

namespace python_bindings {
void BindInd(pybind11::module_& main_module);
} // namespace python_bindings
4 changes: 4 additions & 0 deletions src/python_bindings/py_util/get_py_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "algorithms/metric/enums.h"
#include "association_rules/ar_algorithm_enums.h"
#include "config/tabular_data/input_table_type.h"
#include "config/tabular_data/input_tables_type.h"
#include "model/table/column_combination.h"

namespace py = pybind11;

Expand Down Expand Up @@ -73,6 +75,8 @@ py::tuple GetPyType(std::type_index type_index) {
PyTypePair<std::vector<unsigned int>, py_list, py_int>,
{typeid(config::InputTable),
[]() { return MakeTypeTuple(py::type::of<config::InputTable>()); }},
{typeid(config::InputTables),
[]() { return MakeTypeTuple(py_list, py::type::of<config::InputTable>()); }},
};
return type_map.at(type_index)();
}
Expand Down
21 changes: 17 additions & 4 deletions src/python_bindings/py_util/py_to_any.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "association_rules/ar_algorithm_enums.h"
#include "config/exceptions.h"
#include "config/tabular_data/input_table_type.h"
#include "config/tabular_data/input_tables_type.h"
#include "parser/csv_parser/csv_parser.h"
#include "py_util/create_dataframe_reader.h"
#include "util/enum_to_available_values.h"
Expand Down Expand Up @@ -42,6 +43,13 @@ config::InputTable CreateCsvParser(std::string_view option_name, py::tuple const
CastAndReplaceCastError<bool>(option_name, arguments[2]));
}

config::InputTable PythonObjToInputTable(std::string_view option_name, py::handle obj) {
if (py::isinstance<py::tuple>(obj)) {
return CreateCsvParser(option_name, py::cast<py::tuple>(obj));
}
return python_bindings::CreateDataFrameReader(obj);
}

template <typename Type>
std::pair<std::type_index, ConvFunc> const NormalConvPair{
std::type_index(typeid(Type)), [](std::string_view option_name, py::handle value) {
Expand Down Expand Up @@ -88,10 +96,14 @@ std::pair<std::type_index, ConvFunc> const CharEnumConvPair{
}};

boost::any InputTableToAny(std::string_view option_name, py::handle obj) {
if (py::isinstance<py::tuple>(obj)) {
return CreateCsvParser(option_name, py::cast<py::tuple>(obj));
}
return python_bindings::CreateDataFrameReader(obj);
return PythonObjToInputTable(option_name, obj);
}

boost::any InputTablesToAny(std::string_view option_name, py::handle obj) {
auto tables = CastAndReplaceCastError<std::vector<py::handle>>(option_name, obj);
std::vector<config::InputTable> parsers;
for (auto const& table : tables) parsers.push_back(PythonObjToInputTable(option_name, table));
return parsers;
}

std::unordered_map<std::type_index, ConvFunc> const converters{
Expand All @@ -108,6 +120,7 @@ std::unordered_map<std::type_index, ConvFunc> const converters{
EnumConvPair<algos::InputFormat>,
CharEnumConvPair<algos::Binop>,
{typeid(config::InputTable), InputTableToAny},
{typeid(config::InputTables), InputTablesToAny},
};

} // namespace
Expand Down

0 comments on commit 1fafc37

Please sign in to comment.