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"
5#include "models/TreeLeaf.hpp"
6#include "models/Tree.hpp"
7#include "models/Forest.hpp"
10#include "stats/Metrics.hpp"
11
12#include <nlohmann/json.hpp>
13#include <optional>
14#include <ostream>
15
37 using json = nlohmann::json;
38
41
44
57 template<typename T> struct Export {
63 int n_features = 0;
65
68 std::optional<VariableImportance> variable_importance;
69 std::optional<Metrics> training_metrics;
70 std::optional<Metrics> oob_metrics;
72
74 json to_json() const;
75
82 };
83
84 // Explicit specialization declarations for Export<Model::Ptr>.
87
90 public:
94
95 JsonNodeVisitor() = default;
98
99 void visit(TreeBranch const& node) override;
100 void visit(TreeLeaf const& node) override;
101 };
102
105 public:
109
110 JsonModelVisitor() = default;
113
114 void visit(Tree const& tree) override;
115 void visit(Forest const& forest) override;
116 };
117
128
131 json to_json(Model const& model);
132 json to_json(TreeNode const& node);
133 json to_json(Tree const& tree);
134 json to_json(BaggedTree const& tree);
135 json to_json(Forest const& forest);
140 json to_json(Metrics const& metrics);
143
146 json to_json(Model const& model, Names const& group_names);
147 json to_json(TreeNode const& node, Names const& group_names);
148 json to_json(Tree const& tree, Names const& group_names);
149 json to_json(BaggedTree const& tree, Names const& group_names);
150 json to_json(Forest const& forest, Names const& group_names);
151 json to_json(stats::ConfusionMatrix const& cm, Names const& group_names);
152 json to_json(stats::ClassificationMetrics const& cm, Names const& group_names);
153 json to_json(stats::RegressionMetrics const& rm, Names const& group_names);
154 json to_json(Metrics const& metrics, Names const& group_names);
156
159 json to_json(std::optional<VariableImportance> const& vi);
160 json to_json(std::optional<Metrics> const& metrics);
161 json to_json(std::optional<Metrics> const& metrics, Names const& group_names);
163
166
177 template<typename T> T from_json(json const& j);
178
183
187
190 std::ostream& operator<<(std::ostream& os, TreeNode const& node);
191 std::ostream& operator<<(std::ostream& os, TreeBranch const& condition);
192 std::ostream& operator<<(std::ostream& os, TreeLeaf const& response);
193 std::ostream& operator<<(std::ostream& os, Tree const& tree);
194 std::ostream& operator<<(std::ostream& os, Forest const& forest);
195
196 template<typename V> std::ostream& operator<<(std::ostream& ostream, std::vector<V> const& vec) {
197 json json_vector(vec);
198 return ostream << json_vector.dump();
199 }
200
201 template<typename V, typename C1, typename C2>
202 std::ostream& operator<<(std::ostream& ostream, std::set<V, C1, C2> const& set) {
203 json json_set(set);
204 return ostream << json_set.dump();
205 }
206
207 template<typename K, typename V> std::ostream& operator<<(std::ostream& ostream, std::map<K, V> const& map) {
208 json json_map(map);
209 return ostream << json_map.dump();
210 }
211
212 template<typename V> std::ostream& operator<<(std::ostream& ostream, std::map<int, V> const& map) {
213 std::map<std::string, V> string_map;
214
215 for (auto const& [key, val] : map) {
216 string_map[std::to_string(key)] = val;
217 }
218
219 json json_map(string_map);
220 return ostream << json_map.dump();
221 }
222
224}
225
226// ADL serializer specializations — enables j.get<T>() for all types.
227namespace nlohmann {
228 template<> struct adl_serializer<ppforest2::stats::ConfusionMatrix> {
232 };
233
234 template<> struct adl_serializer<ppforest2::stats::ClassificationMetrics> {
238 };
239
240 template<> struct adl_serializer<ppforest2::VariableImportance> {
244 };
245
246 template<> struct adl_serializer<ppforest2::stats::RegressionMetrics> {
250 };
251
252 template<> struct adl_serializer<ppforest2::serialization::Export<ppforest2::Tree::Ptr>> {
254 };
255
256 template<> struct adl_serializer<ppforest2::serialization::Export<ppforest2::Forest::Ptr>> {
258 };
259
260 template<> struct adl_serializer<ppforest2::serialization::Export<ppforest2::Model::Ptr>> {
262 };
263
273 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
274 struct adl_serializer<Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>> {
275 using Matrix = Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>;
276
277 static void to_json(json& j, Matrix const& m) {
278 if constexpr (Cols == 1) {
279 j = json::array();
280 for (Eigen::Index i = 0; i < m.size(); ++i) {
281 j.push_back(m(i));
282 }
283 } else {
284 j = json::array();
285 for (Eigen::Index i = 0; i < m.rows(); ++i) {
286 json row = json::array();
287 for (Eigen::Index k = 0; k < m.cols(); ++k) {
288 row.push_back(m(i, k));
289 }
290 j.push_back(std::move(row));
291 }
292 }
293 }
294
295 static void from_json(json const& j, Matrix& m) {
296 if constexpr (Cols == 1) {
297 m.resize(static_cast<Eigen::Index>(j.size()));
298 for (std::size_t i = 0; i < j.size(); ++i) {
299 m(static_cast<Eigen::Index>(i)) = j[i].template get<Scalar>();
300 }
301 } else {
302 Eigen::Index const rows = static_cast<Eigen::Index>(j.size());
303 Eigen::Index const cols = rows > 0 ? static_cast<Eigen::Index>(j.front().size()) : 0;
304 m.resize(rows, cols);
305 for (Eigen::Index i = 0; i < rows; ++i) {
306 auto const& row = j[static_cast<std::size_t>(i)];
307 for (Eigen::Index k = 0; k < cols; ++k) {
308 m(i, k) = row[static_cast<std::size_t>(k)].template get<Scalar>();
309 }
310 }
311 }
312 }
313 };
314}
nlohmann::json json
Definition CLI.integration.hpp:22
Confusion matrix for classification model evaluation.
Free-function API for evaluating trained models — variable importance, out-of-bag diagnostics,...
constexpr int rows(int n)
Definition Macros.hpp:46
Mode-specific evaluation metric blocks.
Abstract base class for projection pursuit random forests.
Definition Forest.hpp:31
Visitor interface for model dispatch.
Definition Model.hpp:51
Abstract base class for predictive models (trees and forests).
Definition Model.hpp:29
std::shared_ptr< TrainingSpec > Ptr
Definition TrainingSpec.hpp:45
Internal split node in a projection pursuit tree.
Definition TreeBranch.hpp:15
Leaf node in a projection pursuit tree.
Definition TreeLeaf.hpp:12
Visitor interface for tree node dispatch.
Definition TreeNode.hpp:31
Abstract base class for nodes in a projection pursuit tree.
Definition TreeNode.hpp:19
Abstract base class for projection pursuit decision trees.
Definition Tree.hpp:29
JsonModelVisitor(Names group_names)
Definition Json.hpp:111
void visit(Tree const &tree) override
Names const group_names
When non-empty, the model's leaves and groups are written as label strings.
Definition Json.hpp:108
json result
Definition Json.hpp:106
void visit(Forest const &forest) override
JsonNodeVisitor(Names group_names)
Definition Json.hpp:96
json result
Definition Json.hpp:91
Names const group_names
When non-empty, leaf values and group sets are written as label strings.
Definition Json.hpp:93
void visit(TreeBranch const &node) override
void visit(TreeLeaf const &node) override
Definition Json.hpp:227
JSON serialization and deserialization for ppforest2 models.
Definition ExportValidation.hpp:5
Metrics metrics_from_json(json const &j, types::Mode mode)
Deserialize a Metrics block; mode picks the variant alternative.
types::Names Names
Re-export of types::Names for serialization callers.
Definition Json.hpp:40
stats::ClassificationMetrics from_json< stats::ClassificationMetrics >(json const &j)
T from_json(json const &j)
Deserialize a value block (confusion matrix, VI, metrics, …).
std::ostream & operator<<(std::ostream &os, TreeNode const &node)
stats::Metrics Metrics
Re-export of stats::Metrics for serialization callers.
Definition Json.hpp:43
VariableImportance from_json< VariableImportance >(json const &j)
json to_json(types::OutcomeVector const &y, types::Names const &names)
Serialize a prediction vector as JSON.
stats::ConfusionMatrix from_json< stats::ConfusionMatrix >(json const &j)
stats::RegressionMetrics from_json< stats::RegressionMetrics >(json const &j)
nlohmann::json json
Definition Json.hpp:37
std::variant< ClassificationMetrics, RegressionMetrics > Metrics
Mode-polymorphic metrics block.
Definition Metrics.hpp:116
std::vector< std::string > Names
Vector of name strings — used uniformly for class labels (group_names[i] is the label for GroupId == ...
Definition Types.hpp:49
Eigen::Matrix< Feature, Eigen::Dynamic, Eigen::Dynamic > FeatureMatrix
Dynamic-size matrix of feature values.
Definition Types.hpp:33
Eigen::Matrix< Outcome, Eigen::Dynamic, 1 > OutcomeVector
Dynamic-size column vector of predictions.
Definition Types.hpp:42
Mode
Training mode.
Definition Types.hpp:58
Binarization strategies for multiclass-to-binary reduction.
Definition Benchmark.hpp:25
Bagged< Tree > BaggedTree
Alias for the dominant Bagged instantiation in this codebase — a bootstrap-aggregated Tree....
Definition Tree.hpp:129
Eigen::Matrix< Scalar, Rows, Cols, Options, MaxRows, MaxCols > Matrix
Definition Json.hpp:275
static void to_json(json &j, Matrix const &m)
Definition Json.hpp:277
static void from_json(json const &j, Matrix &m)
Definition Json.hpp:295
static ppforest2::VariableImportance from_json(json const &j)
Definition Json.hpp:241
static ppforest2::serialization::Export< ppforest2::Forest::Ptr > from_json(json const &j)
static ppforest2::serialization::Export< ppforest2::Model::Ptr > from_json(json const &j)
static ppforest2::serialization::Export< ppforest2::Tree::Ptr > from_json(json const &j)
static ppforest2::stats::ClassificationMetrics from_json(json const &j)
Definition Json.hpp:235
static ppforest2::stats::ConfusionMatrix from_json(json const &j)
Definition Json.hpp:229
static ppforest2::stats::RegressionMetrics from_json(json const &j)
Definition Json.hpp:247
Grouped result of the variable importance measures.
Definition Evaluation.hpp:39
A model bundled with its export metadata and optional metrics.
Definition Json.hpp:57
void compute_metrics(types::FeatureMatrix const &x, types::OutcomeVector const &y)
Compute and store metrics on this export.
TrainingSpec::Ptr spec
Definition Json.hpp:61
int n_features
Definition Json.hpp:63
json to_json() const
Serialize to JSON. Only defined for Export<Model::Ptr>.
types::Names feature_names
Definition Json.hpp:64
std::optional< VariableImportance > variable_importance
Definition Json.hpp:68
std::optional< Metrics > training_metrics
Definition Json.hpp:69
T model
Definition Json.hpp:58
Names groups
Group name vector — empty for regression (no group concept), non-empty for classification.
Definition Json.hpp:60
std::optional< Metrics > oob_metrics
Definition Json.hpp:70
int n_observations
Definition Json.hpp:62
Classification evaluation metrics.
Definition Metrics.hpp:25
A confusion matrix comparing predicted vs actual group labels.
Definition ConfusionMatrix.hpp:38
Regression evaluation metrics.
Definition Metrics.hpp:52