ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Json.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "models/TreeNode.hpp"
6#include "models/Tree.hpp"
8#include "models/Forest.hpp"
11
12#include <nlohmann/json.hpp>
13#include <optional>
14#include <ostream>
15
37 using json = nlohmann::json;
38
40 using GroupNames = std::vector<std::string>;
41
54 template<typename T> struct Export {
59 int n_features = 0;
60 std::vector<std::string> feature_names;
61
64 std::optional<VariableImportance> variable_importance;
65 std::optional<stats::ConfusionMatrix> training_confusion_matrix;
66 std::optional<stats::ConfusionMatrix> oob_confusion_matrix;
67 std::optional<double> oob_error;
69
71 json to_json() const;
72
79 };
80
81 // Explicit specialization declarations for Export<Model::Ptr>.
84
88 GroupNames const* group_names = nullptr;
89 void visit(TreeCondition const& node) override;
90 void visit(TreeResponse const& node) override;
91 };
92
96 GroupNames const* group_names = nullptr;
97 void visit(Tree const& tree) override;
98 void visit(Forest const& forest) override;
99 };
100
102 std::vector<std::string> to_labels(types::ResponseVector const& predictions,
103 std::vector<std::string> const& group_names);
104
107 json to_json(Model const& model);
108 json to_json(TreeNode const& node);
109 json to_json(Tree const& tree);
111 json to_json(Forest const& forest);
116
119 json to_json(Model const& model, GroupNames const& group_names);
120 json to_json(TreeNode const& node, GroupNames const& group_names);
121 json to_json(Tree const& tree, GroupNames const& group_names);
122 json to_json(BootstrapTree const& tree, GroupNames const& group_names);
123 json to_json(Forest const& forest, GroupNames const& group_names);
124 json to_json(stats::ConfusionMatrix const& cm, GroupNames const& group_names);
126
129
136 template<typename T> T from_json(json const& j);
137
138 template<> Tree from_json<Tree>(json const& j);
139 template<> Forest from_json<Forest>(json const& j);
143
146 std::ostream& operator<<(std::ostream& os, TreeNode const& node);
147 std::ostream& operator<<(std::ostream& os, TreeCondition const& condition);
148 std::ostream& operator<<(std::ostream& os, TreeResponse const& response);
149 std::ostream& operator<<(std::ostream& os, Tree const& tree);
150 std::ostream& operator<<(std::ostream& os, Forest const& forest);
151
152 template<typename V> std::ostream& operator<<(std::ostream& ostream, std::vector<V> const& vec) {
153 json json_vector(vec);
154 return ostream << json_vector.dump();
155 }
156
157 template<typename V, typename C1, typename C2>
158 std::ostream& operator<<(std::ostream& ostream, std::set<V, C1, C2> const& set) {
159 json json_set(set);
160 return ostream << json_set.dump();
161 }
162
163 template<typename K, typename V> std::ostream& operator<<(std::ostream& ostream, std::map<K, V> const& map) {
164 json json_map(map);
165 return ostream << json_map.dump();
166 }
167
168 template<typename V> std::ostream& operator<<(std::ostream& ostream, std::map<int, V> const& map) {
169 std::map<std::string, V> string_map;
170
171 for (auto const& [key, val] : map) {
172 string_map[std::to_string(key)] = val;
173 }
174
175 json json_map(string_map);
176 return ostream << json_map.dump();
177 }
178
180}
181
182// ADL serializer specializations — enables j.get<T>() for all types.
183namespace nlohmann {
184 template<> struct adl_serializer<ppforest2::Tree> {
186 };
187
188 template<> struct adl_serializer<ppforest2::Forest> {
192 };
193
194 template<> struct adl_serializer<ppforest2::stats::ConfusionMatrix> {
198 };
199
200 template<> struct adl_serializer<ppforest2::VariableImportance> {
204 };
205
206 template<> struct adl_serializer<ppforest2::serialization::Export<ppforest2::Tree>> {
208 };
209
210 template<> struct adl_serializer<ppforest2::serialization::Export<ppforest2::Forest>> {
212 };
213
214 template<> struct adl_serializer<ppforest2::serialization::Export<ppforest2::Model::Ptr>> {
216 };
217}
nlohmann::json json
Definition CLI.integration.hpp:24
Confusion matrix for classification model evaluation.
Definition Json.hpp:183
JSON serialization and deserialization for ppforest2 models.
Definition Json.hpp:36
T from_json(json const &j)
Deserialize from a model block (integer labels only).
Forest from_json< Forest >(json const &j)
std::ostream & operator<<(std::ostream &os, TreeNode const &node)
VariableImportance from_json< VariableImportance >(json const &j)
std::vector< std::string > GroupNames
Group name vector for labeled serialization.
Definition Json.hpp:40
Tree from_json< Tree >(json const &j)
std::vector< std::string > to_labels(types::ResponseVector const &predictions, std::vector< std::string > const &group_names)
Map integer response codes to group name strings.
stats::ConfusionMatrix from_json< stats::ConfusionMatrix >(json const &j)
json to_json(Model const &model)
nlohmann::json json
Definition Json.hpp:37
Eigen::Matrix< Feature, Eigen::Dynamic, Eigen::Dynamic > FeatureMatrix
Dynamic-size matrix of feature values.
Definition Types.hpp:24
Eigen::Matrix< Response, Eigen::Dynamic, 1 > ResponseVector
Dynamic-size column vector of group labels.
Definition Types.hpp:29
Definition Benchmark.hpp:22
static ppforest2::Forest from_json(json const &j)
Definition Json.hpp:189
static ppforest2::Tree from_json(json const &j)
Definition Json.hpp:185
static ppforest2::VariableImportance from_json(json const &j)
Definition Json.hpp:201
static ppforest2::serialization::Export< ppforest2::Forest > from_json(json const &j)
static ppforest2::serialization::Export< ppforest2::Model::Ptr > from_json(json const &j)
static ppforest2::serialization::Export< ppforest2::Tree > from_json(json const &j)
static ppforest2::stats::ConfusionMatrix from_json(json const &j)
Definition Json.hpp:195
Definition BootstrapTree.hpp:9
A projection pursuit random forest.
Definition Forest.hpp:32
Visitor interface for model dispatch.
Definition Model.hpp:34
Abstract base class for predictive models (trees and forests).
Definition Model.hpp:25
std::shared_ptr< TrainingSpec > Ptr
Definition TrainingSpec.hpp:41
Internal split node in a projection pursuit tree.
Definition TreeCondition.hpp:19
Visitor interface for tree node dispatch.
Definition TreeNode.hpp:28
Abstract base class for nodes in a projection pursuit tree.
Definition TreeNode.hpp:19
Leaf node in a projection pursuit tree.
Definition TreeResponse.hpp:12
A single projection pursuit decision tree.
Definition Tree.hpp:26
Grouped result of the three variable importance measures.
Definition VariableImportance.hpp:27
A model bundled with its export metadata and optional metrics.
Definition Json.hpp:54
std::vector< std::string > feature_names
Definition Json.hpp:60
GroupNames groups
Definition Json.hpp:56
TrainingSpec::Ptr spec
Definition Json.hpp:57
int n_features
Definition Json.hpp:59
std::optional< double > oob_error
Definition Json.hpp:67
std::optional< stats::ConfusionMatrix > training_confusion_matrix
Definition Json.hpp:65
json to_json() const
Serialize to JSON. Only defined for Export<Model::Ptr>.
std::optional< VariableImportance > variable_importance
Definition Json.hpp:64
std::optional< stats::ConfusionMatrix > oob_confusion_matrix
Definition Json.hpp:66
T model
Definition Json.hpp:55
void compute_metrics(types::FeatureMatrix const &x, types::ResponseVector const &y)
Compute and store metrics on this export.
int n_observations
Definition Json.hpp:58
Visitor that serializes a model (Tree or Forest) to JSON.
Definition Json.hpp:94
void visit(Tree const &tree) override
void visit(Forest const &forest) override
GroupNames const * group_names
Definition Json.hpp:96
Visitor that serializes a tree node to JSON.
Definition Json.hpp:86
json result
Definition Json.hpp:87
void visit(TreeResponse const &node) override
void visit(TreeCondition const &node) override
GroupNames const * group_names
Definition Json.hpp:88
A confusion matrix comparing predicted vs actual group labels.
Definition ConfusionMatrix.hpp:34