mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
SpnWrapper.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <unordered_map>
5#include <util/Spn.hpp>
6#include <vector>
7
8
9namespace m {
10
13{
14 using Filter = std::unordered_map<unsigned, std::pair<Spn::SpnOperator, float>>;
15 using AttrFilter = std::unordered_map<ThreadSafePooledString, std::pair<Spn::SpnOperator, float>>;
16
17 private:
19 std::unordered_map<ThreadSafePooledString, unsigned> attribute_to_id_;
20
21 SpnWrapper(Spn spn, std::unordered_map<ThreadSafePooledString, unsigned> attribute_to_id)
22 : spn_(std::move(spn))
23 , attribute_to_id_(std::move(attribute_to_id))
24 { }
25
26 Filter translate_filter(const AttrFilter &attr_filter) const {
27 Filter filter;
28 for (auto &elem : attr_filter) { filter.emplace(translate_attribute(elem.first), elem.second); }
29 return filter;
30 };
31
32 unsigned translate_attribute(const ThreadSafePooledString &attribute) const {
33 unsigned spn_id = 0;
34 if (auto it = attribute_to_id_.find(attribute); it != attribute_to_id_.end()) {
35 spn_id = it->second;
36 } else { std::cerr << "could not find attribute: " << attribute << std::endl; }
37 return spn_id;
38 }
39
40 public:
41 SpnWrapper(const SpnWrapper&) = delete;
42 SpnWrapper(SpnWrapper&&) = default;
43
45 const std::unordered_map<ThreadSafePooledString, unsigned> & get_attribute_to_id() const { return attribute_to_id_; }
46
54 static SpnWrapper learn_spn_table(const ThreadSafePooledString &name_of_database,
55 const ThreadSafePooledString &name_of_table,
56 std::vector<Spn::LeafType> leaf_types = decltype(leaf_types)());
57
64 static std::unordered_map<ThreadSafePooledString, SpnWrapper*>
66 const ThreadSafePooledString &name_of_database,
67 std::unordered_map<ThreadSafePooledString, std::vector<Spn::LeafType>> leaf_types = decltype(leaf_types)()
68 );
69
70
72 std::size_t num_rows() const { return spn_.num_rows(); }
73
76 float likelihood(const AttrFilter &attr_filter) const { return spn_.likelihood(translate_filter(attr_filter)); };
79 float likelihood(const Filter &filter) const { return spn_.likelihood(filter); };
80
82 float upper_bound(const AttrFilter &attr_filter) const { return spn_.upper_bound(translate_filter(attr_filter)); };
84 float upper_bound(const Filter &filter) const { return spn_.upper_bound(filter); };
85
87 float lower_bound(const AttrFilter &attr_filter) const { return spn_.lower_bound(translate_filter(attr_filter)); };
89 float lower_bound(const Filter &filter) const { return spn_.lower_bound(filter); };
90
92 float expectation(const ThreadSafePooledString &attribute, const AttrFilter &attr_filter) const {
93 return spn_.expectation(translate_attribute(attribute), translate_filter(attr_filter));
94 }
96 float expectation(unsigned attribute_id, const Filter &filter) const {
97 return spn_.expectation(attribute_id, filter);
98 };
99
101 void update_row(Eigen::VectorXf &old_row, Eigen::VectorXf &updated_row) { spn_.update_row(old_row, updated_row); };
102
104 void insert_row(Eigen::VectorXf &row) { spn_.insert_row(row); };
105
107 void delete_row(Eigen::VectorXf &row) { spn_.delete_row(row); };
108
110 std::size_t estimate_number_distinct_values(const ThreadSafePooledString &attribute) const {
112 }
114 std::size_t estimate_number_distinct_values(unsigned attribute_id) const {
115 return spn_.estimate_number_distinct_values(attribute_id);
116 };
117
118 unsigned height() const { return spn_.height(); }
119 unsigned breadth() const { return spn_.breadth(); }
120 unsigned degree() const { return spn_.degree(); }
121 std::size_t memory_usage() const { return spn_.memory_usage(); }
122 void dump() const { spn_.dump(); };
123 void dump(std::ostream &out) const { spn_.dump(out); };
124};
125
126}
‍mutable namespace
Definition: Backend.hpp:10
ThreadSafeStringPool::proxy_type ThreadSafePooledString
Definition: Pool.hpp:464
STL namespace.
A wrapper class for an Spn to be used in the context of databases.
Definition: SpnWrapper.hpp:13
void dump(std::ostream &out) const
Definition: SpnWrapper.hpp:123
float expectation(unsigned attribute_id, const Filter &filter) const
Compute the expectation of the given attribute.
Definition: SpnWrapper.hpp:96
unsigned translate_attribute(const ThreadSafePooledString &attribute) const
Definition: SpnWrapper.hpp:32
std::size_t estimate_number_distinct_values(const ThreadSafePooledString &attribute) const
Estimate the number of distinct values of the given attribute.
Definition: SpnWrapper.hpp:110
void update_row(Eigen::VectorXf &old_row, Eigen::VectorXf &updated_row)
Update the SPN with the given row.
Definition: SpnWrapper.hpp:101
std::size_t num_rows() const
returns the number of rows in the SPN.
Definition: SpnWrapper.hpp:72
float upper_bound(const AttrFilter &attr_filter) const
Compute the upper bound probability for continuous domains.
Definition: SpnWrapper.hpp:82
static std::unordered_map< ThreadSafePooledString, SpnWrapper * > learn_spn_database(const ThreadSafePooledString &name_of_database, std::unordered_map< ThreadSafePooledString, std::vector< Spn::LeafType > > leaf_types=decltype(leaf_types)())
Learn SPNs over the tables in the given database.
Definition: SpnWrapper.cpp:133
std::size_t memory_usage() const
Definition: SpnWrapper.hpp:121
float lower_bound(const AttrFilter &attr_filter) const
Compute the lower bound probability for continuous domains.
Definition: SpnWrapper.hpp:87
std::unordered_map< ThreadSafePooledString, std::pair< Spn::SpnOperator, float > > AttrFilter
Definition: SpnWrapper.hpp:15
SpnWrapper(const SpnWrapper &)=delete
void delete_row(Eigen::VectorXf &row)
Delete the given row from the SPN.
Definition: SpnWrapper.hpp:107
float likelihood(const Filter &filter) const
Compute the likelihood of the given filter predicates given by a map from spn internal id to the resp...
Definition: SpnWrapper.hpp:79
std::unordered_map< unsigned, std::pair< Spn::SpnOperator, float > > Filter
Definition: SpnWrapper.hpp:14
void dump() const
Definition: SpnWrapper.hpp:122
std::size_t estimate_number_distinct_values(unsigned attribute_id) const
Estimate the number of distinct values of the given attribute.
Definition: SpnWrapper.hpp:114
float likelihood(const AttrFilter &attr_filter) const
Compute the likelihood of the given filter predicates given by a map from attribute to the respective...
Definition: SpnWrapper.hpp:76
unsigned degree() const
Definition: SpnWrapper.hpp:120
float lower_bound(const Filter &filter) const
Compute the lower bound probability for continuous domains.
Definition: SpnWrapper.hpp:89
float expectation(const ThreadSafePooledString &attribute, const AttrFilter &attr_filter) const
Compute the expectation of the given attribute.
Definition: SpnWrapper.hpp:92
unsigned height() const
Definition: SpnWrapper.hpp:118
static SpnWrapper learn_spn_table(const ThreadSafePooledString &name_of_database, const ThreadSafePooledString &name_of_table, std::vector< Spn::LeafType > leaf_types=decltype(leaf_types)())
Learn an SPN over the given table.
Definition: SpnWrapper.cpp:11
SpnWrapper(SpnWrapper &&)=default
std::unordered_map< ThreadSafePooledString, unsigned > attribute_to_id_
a map from attribute to spn internal id
Definition: SpnWrapper.hpp:19
const std::unordered_map< ThreadSafePooledString, unsigned > & get_attribute_to_id() const
Get the reference to the attribute to spn internal id mapping.
Definition: SpnWrapper.hpp:45
Filter translate_filter(const AttrFilter &attr_filter) const
Definition: SpnWrapper.hpp:26
unsigned breadth() const
Definition: SpnWrapper.hpp:119
float upper_bound(const Filter &filter) const
Compute the upper bound probability for continuous domains.
Definition: SpnWrapper.hpp:84
SpnWrapper(Spn spn, std::unordered_map< ThreadSafePooledString, unsigned > attribute_to_id)
Definition: SpnWrapper.hpp:21
void insert_row(Eigen::VectorXf &row)
Insert the given row into the SPN.
Definition: SpnWrapper.hpp:104
Tree structure for Sum Product Networks.
Definition: Spn.hpp:20
void dump() const
Definition: Spn.cpp:951
float lower_bound(const Filter &filter) const
Compute the lower bound probability for continuous domains.
Definition: Spn.cpp:908
void update_row(Eigen::VectorXf &old_row, Eigen::VectorXf &updated_row)
Update the SPN with the given row.
Definition: Spn.cpp:928
float likelihood(const Filter &filter) const
Compute the likelihood of the given filter predicates given by a map from attribute to the respective...
Definition: Spn.cpp:898
unsigned degree() const
Definition: Spn.hpp:392
void delete_row(Eigen::VectorXf &row)
Delete the given row from the SPN.
Definition: Spn.cpp:940
float upper_bound(const Filter &filter) const
Compute the upper bound probability for continuous domains.
Definition: Spn.cpp:903
std::size_t estimate_number_distinct_values(unsigned attribute_id) const
Estimate the number of distinct values of the given attribute.
Definition: Spn.cpp:946
float expectation(unsigned attribute_id, const Filter &filter) const
Compute the expectation of the given attribute.
Definition: Spn.cpp:913
unsigned height() const
Definition: Spn.hpp:390
std::size_t num_rows() const
returns the number of rows in the SPN.
Definition: Spn.hpp:314
void insert_row(Eigen::VectorXf &row)
Insert the given row into the SPN.
Definition: Spn.cpp:934
unsigned breadth() const
Definition: Spn.hpp:391
std::size_t memory_usage() const
Definition: Spn.hpp:393