forpy  2
types.h
Go to the documentation of this file.
1 /* Author: Christoph Lassner. */
2 #pragma once
3 #ifndef FORPY_TYPES_H_
4 #define FORPY_TYPES_H_
5 
6 #include <Eigen/Dense>
7 #include <map>
8 #include <memory>
9 #include <numeric>
10 #include <utility>
11 #include <vector>
12 
13 #include "./util/hash.h"
14 #include "./util/storage.h"
15 
16 namespace forpy {
17 
21 template <typename T>
22 struct Name {
23  static std::string value() { return "unknown"; };
24 };
25 template <>
26 struct Name<double> {
27  static std::string value() { return "d"; }
28 };
29 template <>
30 struct Name<float> {
31  static std::string value() { return "f"; }
32 };
33 template <>
34 struct Name<uint> {
35  static std::string value() { return "ui"; }
36 };
37 template <>
38 struct Name<uint8_t> {
39  static std::string value() { return "ui8"; }
40 };
41 template <>
42 struct Name<int16_t> {
43  static std::string value() { return "i16"; }
44 };
45 template <>
46 struct Name<int> {
47  static std::string value() { return "i"; }
48 };
49 
51 template <typename DT>
52 using Mat = Eigen::Matrix<DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
53 
55 template <typename DT>
56 using MatCM =
57  Eigen::Matrix<DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
58 
60 template <typename DT>
61 using MatCRef = Eigen::Ref<const Mat<DT>>;
62 
64 template <typename DT>
65 using MatCMCRef = Eigen::Ref<const MatCM<DT>>;
66 
68 template <typename DT>
69 using MatRef = Eigen::Ref<Mat<DT>>;
70 
72 template <typename DT>
73 using Vec = Eigen::Matrix<DT, Eigen::Dynamic, 1, Eigen::ColMajor>;
74 
75 template <typename DT>
76 using VecRM = Eigen::Matrix<DT, 1, Eigen::Dynamic, Eigen::RowMajor>;
77 
78 template <typename DT>
79 using VecRef = Eigen::Ref<Vec<DT>>;
80 
81 template <typename DT>
82 using VecRMRef = Eigen::Ref<VecRM<DT>>;
83 
84 template <typename DT>
85 using VecCRef = Eigen::Ref<const Vec<DT>>;
86 
87 template <typename DT>
88 using VecCMap =
89  Eigen::Map<const Eigen::Matrix<DT, 1, Eigen::Dynamic, Eigen::RowMajor>,
90  Eigen::Unaligned, Eigen::InnerStride<>>;
91 
95 enum class ECompletionLevel {
97  Node,
100  Level,
102  Complete
103 };
104 
106 typedef size_t id_t;
107 
108 typedef std::function<id_t(const Data<MatCRef> &, const id_t &,
109  const std::function<void(void *)> &)>
111 
113 typedef unsigned int uint;
114 
117 
118 template <typename FT>
119 struct SplitOptRes {
121  FT thresh;
122  float gain;
123  bool valid;
124 
125  inline friend std::ostream &operator<<(std::ostream &stream,
126  const SplitOptRes<FT> &self) {
127  stream << "forpy::SplitOptRes_X[<=" << self.thresh
128  << "; gain: " << self.gain << ", valid: " << self.valid << "]";
129  return stream;
130  };
131 
132  bool operator==(SplitOptRes<FT> const &rhs) const {
133  return split_idx == rhs.split_idx && thresh == rhs.thresh &&
134  gain == rhs.gain && valid == rhs.valid;
135  }
136 };
137 typedef mu::variant<SplitOptRes<float>, SplitOptRes<double>, SplitOptRes<uint>,
138  SplitOptRes<uint8_t>>
140 
141 typedef std::pair<id_t, id_t> interv_t;
142 
152 struct TodoMark {
153  inline TodoMark() : node_id(0), depth(0){};
154  inline TodoMark(std::shared_ptr<std::vector<id_t>> sample_ids,
155  const interv_t &interv, const id_t &node_id,
156  const uint &depth)
158  interv(interv),
159  node_id(node_id),
161  std::shared_ptr<std::vector<id_t>> sample_ids;
165  inline bool operator==(TodoMark const &rhs) const {
166  return node_id == rhs.node_id && depth == rhs.depth &&
167  interv == rhs.interv && *sample_ids == *(rhs.sample_ids);
168  }
169  inline friend std::ostream &operator<<(std::ostream &stream,
170  const TodoMark &self) {
171  stream << "forpy::TodoMark[node_id: " << self.node_id << ", depth "
172  << self.depth << "]";
173  return stream;
174  };
176 
177  private:
178  friend class cereal::access;
179  template <class Archive>
180  void serialize(Archive &ar, const uint &) {
181  ar(CEREAL_NVP(sample_ids), CEREAL_NVP(interv), CEREAL_NVP(node_id),
182  CEREAL_NVP(depth));
183  };
185 };
186 
187 typedef std::pair<ptrdiff_t, ptrdiff_t> regint_t;
188 
190 typedef std::vector<std::pair<std::shared_ptr<std::vector<size_t>>,
191  std::shared_ptr<std::vector<float> const>>>
193 
202 typedef std::pair<std::shared_ptr<std::vector<id_t>>, id_t> include_pair_t;
203 
205 enum class ESearchType { DFS, BFS };
206 
208 typedef std::unordered_set<std::vector<size_t>, vector_hasher> proposal_set_t;
209 
210 #pragma clang diagnostic push
211 #pragma clang diagnostic ignored "-Wunused-variable"
212 const double GAIN_EPS = 1E-7;
213 #pragma clang diagnostic pop
215 }; // namespace forpy
216 #endif // FORPY_TYPES_H_
std::pair< std::shared_ptr< std::vector< id_t > >, id_t > include_pair_t
A pair containing information about newly included samples.
Definition: types.h:202
uint depth
Definition: types.h:164
friend class cereal::access
Definition: types.h:178
const double GAIN_EPS
Definition: types.h:212
static std::string value()
Definition: types.h:23
interv_t interv
Definition: types.h:162
bool operator==(TodoMark const &rhs) const
Definition: types.h:165
size_t id_t
Element id type.
Definition: types.h:106
Eigen::Ref< VecRM< DT > > VecRMRef
Definition: types.h:82
const MatCRef< float > FORPY_ZERO_MATR(Mat< float >::Zero(0, 1))
MOVE_ASSIGN(TodoMark)
std::vector< std::pair< std::shared_ptr< std::vector< size_t > >, std::shared_ptr< std::vector< float > const > > > usage_map_t
Describes how each sample is used for each tree.
Definition: types.h:192
Eigen::Ref< const MatCM< DT > > MatCMCRef
Parameterized const matrix column major matrix ref type.
Definition: types.h:65
std::pair< id_t, id_t > interv_t
Definition: types.h:141
void serialize(Archive &ar, const uint &)
Definition: types.h:180
Eigen::Ref< Vec< DT > > VecRef
Definition: types.h:79
id_t node_id
Definition: types.h:163
std::pair< ptrdiff_t, ptrdiff_t > regint_t
Definition: types.h:187
TodoMark(std::shared_ptr< std::vector< id_t >> sample_ids, const interv_t &interv, const id_t &node_id, const uint &depth)
Definition: types.h:154
ESearchType
Definition: types.h:205
Stores the parameters for one marked tree node.
Definition: types.h:152
friend std::ostream & operator<<(std::ostream &stream, const TodoMark &self)
Definition: types.h:169
std::unordered_set< std::vector< size_t >, vector_hasher > proposal_set_t
The type of a set of dimension selections.
Definition: types.h:208
static std::string value()
Definition: types.h:47
static std::string value()
Definition: types.h:31
Eigen::Matrix< DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatCM
Parameterized column major matrix type.
Definition: types.h:57
Eigen::Ref< Mat< DT > > MatRef
Parameterized standard non-const matrix ref type.
Definition: types.h:69
std::shared_ptr< std::vector< id_t > > sample_ids
Definition: types.h:160
DISALLOW_COPY_AND_ASSIGN(TodoMark)
mu::variant< SplitOptRes< float >, SplitOptRes< double >, SplitOptRes< uint >, SplitOptRes< uint8_t > > OptSplitV
Definition: types.h:139
EThresholdSelection
Specifies which thresholds should be used for a decision.
Definition: types.h:116
bool operator==(SplitOptRes< FT > const &rhs) const
Definition: types.h:132
static std::string value()
Definition: types.h:39
Struct for translating primitive types to a short name.
Definition: types.h:22
Eigen::Ref< const Vec< DT > > VecCRef
Definition: types.h:85
Eigen::Matrix< DT, Eigen::Dynamic, 1, Eigen::ColMajor > Vec
Definition: types.h:73
Eigen::Matrix< DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Mat
Parameterized Matrix type (row major).
Definition: types.h:52
static std::string value()
Definition: types.h:35
A simple vector<size_t> hasher.
Definition: hash.h:34
friend std::ostream & operator<<(std::ostream &stream, const SplitOptRes< FT > &self)
Definition: types.h:125
static std::string value()
Definition: types.h:43
Eigen::Map< const Eigen::Matrix< DT, 1, Eigen::Dynamic, Eigen::RowMajor >, Eigen::Unaligned, Eigen::InnerStride<> > VecCMap
Definition: types.h:90
ECompletionLevel
Specifies the completion level for one training step.
Definition: types.h:95
Eigen::Ref< const Mat< DT > > MatCRef
Parameterized const matrix ref type.
Definition: types.h:61
unsigned int uint
Convenience typedef for unsigned int.
Definition: types.h:113
Eigen::Matrix< DT, 1, Eigen::Dynamic, Eigen::RowMajor > VecRM
Definition: types.h:76
std::function< id_t(const Data< MatCRef > &, const id_t &, const std::function< void(void *)> &)> node_predf
Definition: types.h:110
static std::string value()
Definition: types.h:27