Skip to content

Commit

Permalink
Cleanup in Python bindings and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
BUYT-1 committed Jun 3, 2024
1 parent 0fa47e5 commit 46470fd
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 45 deletions.
4 changes: 2 additions & 2 deletions examples/comparison_pfd_vs_afd.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def get_pfds():
pfds = set(get_pfds())
afds = set(get_afds())

print("pFDs \ AFDs =", stringify(pfds - afds))
print("AFDs \ pFDs =", stringify(afds - pfds))
print("pFDs \\ AFDs =", stringify(pfds - afds))
print("AFDs \\ pFDs =", stringify(afds - pfds))
print("AFDs ∩ pFDs =", stringify(afds & pfds))

print("1 - PerValue([DeviceId] -> Data) =", 0.1714285714)
Expand Down
22 changes: 11 additions & 11 deletions src/python_bindings/ac/bind_ac.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "bind_ac.h"
#include "ac/bind_ac.h"

#include <cstddef>
#include <string>
#include <utility>
#include <vector>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand All @@ -7,9 +12,7 @@
#include "algorithms/algebraic_constraints/mining_algorithms.h"
#include "py_util/bind_primitive.h"

namespace {
namespace py = pybind11;
} // namespace

namespace python_bindings {
void BindAc(py::module_& main_module) {
Expand All @@ -27,7 +30,7 @@ void BindAc(py::module_& main_module) {
std::vector<std::pair<pybind11::float_, pybind11::float_>> res;
res.reserve(ranges.ranges.size() / 2);
assert(ranges.ranges.size() % 2 == 0);
for (size_t i = 0; i < ranges.ranges.size(); i += 2) {
for (std::size_t i = 0; i < ranges.ranges.size(); i += 2) {
// TODO: change this once a proper conversion mechanism from
// `model::INumericType` is implemented
std::string l_endpoint =
Expand All @@ -42,12 +45,9 @@ void BindAc(py::module_& main_module) {
BindPrimitiveNoBase<ACAlgorithm>(ac_module, "AcAlgorithm")
.def("get_ac_ranges", &ACAlgorithm::GetRangesCollections,
py::return_value_policy::reference_internal)
.def(
"get_ac_exceptions",
[](ACAlgorithm& algo) {
algo.CollectACExceptions();
return algo.GetACExceptions();
},
py::return_value_policy::reference_internal);
.def("get_ac_exceptions", [](ACAlgorithm& algo) {
algo.CollectACExceptions();
return algo.GetACExceptions();
});
}
} // namespace python_bindings
4 changes: 1 addition & 3 deletions src/python_bindings/ar/bind_ar.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "bind_ar.h"
#include "ar/bind_ar.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand All @@ -7,9 +7,7 @@
#include "algorithms/association_rules/mining_algorithms.h"
#include "py_util/bind_primitive.h"

namespace {
namespace py = pybind11;
} // namespace

namespace python_bindings {
void BindAr(py::module_& main_module) {
Expand Down
3 changes: 2 additions & 1 deletion src/python_bindings/bind_main_classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
#include "py_util/opt_to_py.h"
#include "py_util/py_to_any.h"

namespace {
namespace py = pybind11;

namespace {
using algos::Algorithm;
auto const kVoidIndex = std::type_index{typeid(void)};

Expand Down
24 changes: 24 additions & 0 deletions src/python_bindings/data/bind_data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "data/bind_data.h"

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

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

namespace py = pybind11;

namespace python_bindings {
void BindDataModule(py::module_& main_module) {
auto data_module = main_module.def_submodule("data");
data_module.doc() = "Contains everything related to data itself.";
auto table_tag = py::class_<config::InputTable>(data_module, "Table");
table_tag.doc() = "Tag type for tabular data.";

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
7 changes: 7 additions & 0 deletions src/python_bindings/data/bind_data.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 BindDataModule(pybind11::module_& main_module);
} // namespace python_bindings
4 changes: 1 addition & 3 deletions src/python_bindings/dd/bind_split.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "bind_split.h"
#include "dd/bind_split.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand All @@ -7,9 +7,7 @@
#include "algorithms/dd/mining_algorithms.h"
#include "py_util/bind_primitive.h"

namespace {
namespace py = pybind11;
} // namespace

namespace python_bindings {
void BindSplit(py::module_& main_module) {
Expand Down
8 changes: 6 additions & 2 deletions src/python_bindings/fd/bind_fd.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "bind_fd.h"
#include "fd/bind_fd.h"

#include <cstddef>
#include <initializer_list>
#include <vector>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand All @@ -10,9 +14,9 @@
#include "py_util/bind_primitive.h"
#include "util/bitset_utils.h"

namespace {
namespace py = pybind11;

namespace {
template <typename ElementType>
py::tuple VectorToTuple(std::vector<ElementType> vec) {
std::size_t const size = vec.size();
Expand Down
6 changes: 3 additions & 3 deletions src/python_bindings/fd/bind_fd_verification.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "bind_fd_verification.h"
#include "fd/bind_fd_verification.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand All @@ -8,9 +8,7 @@
#include "algorithms/fd/verification_algorithms.h"
#include "py_util/bind_primitive.h"

namespace {
namespace py = pybind11;
} // namespace

namespace python_bindings {
void BindFdVerification(pybind11::module_& main_module) {
Expand All @@ -30,6 +28,8 @@ void BindFdVerification(pybind11::module_& main_module) {
.def("get_num_error_rows", &FDVerifier::GetNumErrorRows)
.def("get_highlights", &FDVerifier::GetHighlights);

// Create AFD verification module alias. We currently consider FD verification and AFD
// verification to be the same.
main_module.attr("afd_verification") = fd_verification_module;
}
} // namespace python_bindings
2 changes: 0 additions & 2 deletions src/python_bindings/gfd/bind_gfd_verification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include "algorithms/gfd/verification_algorithms.h"
#include "py_util/bind_primitive.h"

namespace {
namespace py = pybind11;
} // namespace

namespace python_bindings {
void BindGfdVerification(pybind11::module_& main_module) {
Expand Down
4 changes: 1 addition & 3 deletions src/python_bindings/mfd/bind_mfd_verification.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "bind_mfd_verification.h"
#include "mfd/bind_mfd_verification.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand All @@ -7,9 +7,7 @@
#include "algorithms/metric/verification_algorithms.h"
#include "py_util/bind_primitive.h"

namespace {
namespace py = pybind11;
} // namespace

namespace python_bindings {
void BindMfdVerification(py::module_& main_module) {
Expand Down
6 changes: 3 additions & 3 deletions src/python_bindings/py_util/bind_primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ auto BindPrimitiveNoBase(pybind11::module_& module, char const* algo_name) {
using algos::Algorithm;

auto algos_module = module.def_submodule("algorithms");
auto default_module =
auto default_algorithm =
detail::RegisterAlgorithm<AlgorithmType, Algorithm>(algos_module, algo_name);
algos_module.attr("Default") = default_module;
return default_module;
algos_module.attr("Default") = default_algorithm;
return default_algorithm;
}

} // namespace python_bindings
3 changes: 2 additions & 1 deletion src/python_bindings/py_util/opt_to_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
#include "config/max_lhs/type.h"
#include "config/thread_number/type.h"

namespace {
namespace py = pybind11;

namespace {
using ConvFunction = std::function<py::object(boost::any)>;

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions src/python_bindings/py_util/py_to_any.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include "py_util/create_dataframe_reader.h"
#include "util/enum_to_available_values.h"

namespace {

namespace py = pybind11;

namespace {
using ConvFunc = std::function<boost::any(std::string_view, py::handle)>;

template <typename T>
Expand Down
4 changes: 1 addition & 3 deletions src/python_bindings/statistics/bind_statistics.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include "bind_statistics.h"
#include "statistics/bind_statistics.h"

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

#include "algorithms/statistics/data_stats.h"
#include "py_util/bind_primitive.h"

namespace {
namespace py = pybind11;
} // namespace

namespace PYBIND11_NAMESPACE {
namespace detail {
Expand Down
4 changes: 1 addition & 3 deletions src/python_bindings/ucc/bind_ucc.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "bind_ucc.h"
#include "ucc/bind_ucc.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand All @@ -9,10 +9,8 @@
#include "py_util/bind_primitive.h"
#include "util/bitset_utils.h"

namespace {
namespace py = pybind11;
using model::UCC;
} // namespace

namespace python_bindings {
void BindUcc(py::module_& main_module) {
Expand Down
6 changes: 3 additions & 3 deletions src/python_bindings/ucc/bind_ucc_verification.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include "bind_ucc_verification.h"
#include "ucc/bind_ucc_verification.h"

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

#include "algorithms/ucc/verification_algorithms.h"
#include "py_util/bind_primitive.h"

namespace {
namespace py = pybind11;
} // namespace

namespace python_bindings {
void BindUccVerification(pybind11::module_& main_module) {
Expand All @@ -22,6 +20,8 @@ void BindUccVerification(pybind11::module_& main_module) {
.def("get_num_rows_violating_ucc", &UCCVerifier::GetNumRowsViolatingUCC)
.def("get_clusters_violating_ucc", &UCCVerifier::GetClustersViolatingUCC)
.def("get_error", &UCCVerifier::GetError);
// Create AUCC verification module alias. We currently consider UCC verification and AUCC
// verification to be the same.
main_module.attr("aucc_verification") = ucc_verification_module;
}
} // namespace python_bindings

0 comments on commit 46470fd

Please sign in to comment.