ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
ppforest2::serialization Namespace Reference

JSON serialization and deserialization for ppforest2 models. More...

Classes

struct  Export
 A model bundled with its export metadata and optional metrics. More...
 
class  JsonModelVisitor
 Visitor that serializes a model (Tree or Forest) to JSON. More...
 
class  JsonNodeVisitor
 Visitor that serializes a tree node to JSON. More...
 

Typedefs

using json = nlohmann::json
 
using Metrics = stats::Metrics
 Re-export of stats::Metrics for serialization callers.
 
using Names = types::Names
 Re-export of types::Names for serialization callers.
 

Functions

bool has_value (nlohmann::json const &j, std::string_view key)
 True iff j has a non-null value at key.
 
json to_json (types::OutcomeVector const &y, types::Names const &names)
 Serialize a prediction vector as JSON.
 
void validate_forest_export (nlohmann::json const &j)
 Validate the structural shape of a loaded model Export JSON.
 
void validate_tree_export (nlohmann::json const &j)
 Validate the structural shape of a loaded model Export JSON.
 
Serialization
json to_json (Model const &model)
 
json to_json (TreeNode const &node)
 
json to_json (Tree const &tree)
 
json to_json (BaggedTree const &tree)
 
json to_json (Forest const &forest)
 
json to_json (stats::ConfusionMatrix const &cm)
 
json to_json (stats::ClassificationMetrics const &cm)
 
json to_json (VariableImportance const &vi)
 
json to_json (stats::RegressionMetrics const &rm)
 
json to_json (Metrics const &metrics)
 
json to_json (types::FeatureMatrix const &matrix)
 
Labeled serialization (uses group names instead of integer codes)
json to_json (Model const &model, Names const &group_names)
 
json to_json (TreeNode const &node, Names const &group_names)
 
json to_json (Tree const &tree, Names const &group_names)
 
json to_json (BaggedTree const &tree, Names const &group_names)
 
json to_json (Forest const &forest, Names const &group_names)
 
json to_json (stats::ConfusionMatrix const &cm, Names const &group_names)
 
json to_json (stats::ClassificationMetrics const &cm, Names const &group_names)
 
json to_json (stats::RegressionMetrics const &rm, Names const &group_names)
 
json to_json (Metrics const &metrics, Names const &group_names)
 
Optional serialization — <tt>nullopt</tt> round-trips as JSON <tt>null</tt>.
json to_json (std::optional< VariableImportance > const &vi)
 
json to_json (std::optional< Metrics > const &metrics)
 
json to_json (std::optional< Metrics > const &metrics, Names const &group_names)
 
Deserialization
template<typename T>
from_json (json const &j)
 Deserialize a value block (confusion matrix, VI, metrics, …).
 
template<>
stats::ConfusionMatrix from_json< stats::ConfusionMatrix > (json const &j)
 
template<>
stats::ClassificationMetrics from_json< stats::ClassificationMetrics > (json const &j)
 
template<>
VariableImportance from_json< VariableImportance > (json const &j)
 
template<>
stats::RegressionMetrics from_json< stats::RegressionMetrics > (json const &j)
 
Metrics metrics_from_json (json const &j, types::Mode mode)
 Deserialize a Metrics block; mode picks the variant alternative.
 
Stream operators
std::ostream & operator<< (std::ostream &os, TreeNode const &node)
 
std::ostream & operator<< (std::ostream &os, TreeBranch const &condition)
 
std::ostream & operator<< (std::ostream &os, TreeLeaf const &response)
 
std::ostream & operator<< (std::ostream &os, Tree const &tree)
 
std::ostream & operator<< (std::ostream &os, Forest const &forest)
 
template<typename V>
std::ostream & operator<< (std::ostream &ostream, std::vector< V > const &vec)
 
template<typename V, typename C1, typename C2>
std::ostream & operator<< (std::ostream &ostream, std::set< V, C1, C2 > const &set)
 
template<typename K, typename V>
std::ostream & operator<< (std::ostream &ostream, std::map< K, V > const &map)
 
template<typename V>
std::ostream & operator<< (std::ostream &ostream, std::map< int, V > const &map)
 

Detailed Description

JSON serialization and deserialization for ppforest2 models.

Uses nlohmann/json. Provides to_json() for serializing trees, forests, confusion matrices, and variable importance to JSON. Deserialization uses j.get<T>() via nlohmann ADL:

// Serialize and write to file:
auto j = serialization::to_json(forest);
io::json::write_file(j, "model.json");
// Read a full export (model + config + meta):
auto j2 = io::json::read_file("model.json");
auto e = j2.get<Export<Forest>>(); // e.model, e.groups, e.spec
// Read bare model block (integer labels):
Tree tree = model_json.get<Tree>();
Abstract base class for projection pursuit decision trees.
Definition Tree.hpp:29
json to_json(types::OutcomeVector const &y, types::Names const &names)
Serialize a prediction vector as JSON.
A model bundled with its export metadata and optional metrics.
Definition Json.hpp:57

Typedef Documentation

◆ json

using ppforest2::serialization::json = nlohmann::json

◆ Metrics

Re-export of stats::Metrics for serialization callers.

◆ Names

Re-export of types::Names for serialization callers.

Function Documentation

◆ from_json()

template<typename T>
T ppforest2::serialization::from_json ( json const & j)

Deserialize a value block (confusion matrix, VI, metrics, …).

Models go through j.get<Export<Model::Ptr>>() (or the typed Export<Tree::Ptr> / Export<Forest::Ptr> overloads) which rehydrate the real TrainingSpec from the JSON's config block. There's no standalone from_json<Tree::Ptr> because constructing a concrete tree without its spec would require a placeholder, which masks the missing config silently.

◆ from_json< stats::ClassificationMetrics >()

◆ from_json< stats::ConfusionMatrix >()

◆ from_json< stats::RegressionMetrics >()

◆ from_json< VariableImportance >()

◆ has_value()

bool ppforest2::serialization::has_value ( nlohmann::json const & j,
std::string_view key )
inline

True iff j has a non-null value at key.

Centralises the "key is present and not null" check used when reading optional fields. Callers that previously wrote j.contains(key) must now use this helper (or equivalent) to tolerate the null that the writer emits for std::nullopt.

◆ metrics_from_json()

Metrics ppforest2::serialization::metrics_from_json ( json const & j,
types::Mode mode )

Deserialize a Metrics block; mode picks the variant alternative.

◆ operator<<() [1/9]

std::ostream & ppforest2::serialization::operator<< ( std::ostream & os,
Forest const & forest )

◆ operator<<() [2/9]

std::ostream & ppforest2::serialization::operator<< ( std::ostream & os,
Tree const & tree )

◆ operator<<() [3/9]

std::ostream & ppforest2::serialization::operator<< ( std::ostream & os,
TreeBranch const & condition )

◆ operator<<() [4/9]

std::ostream & ppforest2::serialization::operator<< ( std::ostream & os,
TreeLeaf const & response )

◆ operator<<() [5/9]

std::ostream & ppforest2::serialization::operator<< ( std::ostream & os,
TreeNode const & node )

◆ operator<<() [6/9]

template<typename V>
std::ostream & ppforest2::serialization::operator<< ( std::ostream & ostream,
std::map< int, V > const & map )

◆ operator<<() [7/9]

template<typename K, typename V>
std::ostream & ppforest2::serialization::operator<< ( std::ostream & ostream,
std::map< K, V > const & map )

◆ operator<<() [8/9]

template<typename V, typename C1, typename C2>
std::ostream & ppforest2::serialization::operator<< ( std::ostream & ostream,
std::set< V, C1, C2 > const & set )

◆ operator<<() [9/9]

template<typename V>
std::ostream & ppforest2::serialization::operator<< ( std::ostream & ostream,
std::vector< V > const & vec )

◆ to_json() [1/24]

json ppforest2::serialization::to_json ( BaggedTree const & tree)

◆ to_json() [2/24]

json ppforest2::serialization::to_json ( BaggedTree const & tree,
Names const & group_names )

◆ to_json() [3/24]

json ppforest2::serialization::to_json ( Forest const & forest)

◆ to_json() [4/24]

json ppforest2::serialization::to_json ( Forest const & forest,
Names const & group_names )

◆ to_json() [5/24]

json ppforest2::serialization::to_json ( Metrics const & metrics)

◆ to_json() [6/24]

json ppforest2::serialization::to_json ( Metrics const & metrics,
Names const & group_names )

◆ to_json() [7/24]

json ppforest2::serialization::to_json ( Model const & model)

◆ to_json() [8/24]

json ppforest2::serialization::to_json ( Model const & model,
Names const & group_names )

◆ to_json() [9/24]

json ppforest2::serialization::to_json ( stats::ClassificationMetrics const & cm)

◆ to_json() [10/24]

json ppforest2::serialization::to_json ( stats::ClassificationMetrics const & cm,
Names const & group_names )

◆ to_json() [11/24]

json ppforest2::serialization::to_json ( stats::ConfusionMatrix const & cm)

◆ to_json() [12/24]

json ppforest2::serialization::to_json ( stats::ConfusionMatrix const & cm,
Names const & group_names )

◆ to_json() [13/24]

json ppforest2::serialization::to_json ( stats::RegressionMetrics const & rm)

◆ to_json() [14/24]

json ppforest2::serialization::to_json ( stats::RegressionMetrics const & rm,
Names const & group_names )

◆ to_json() [15/24]

json ppforest2::serialization::to_json ( std::optional< Metrics > const & metrics)

◆ to_json() [16/24]

json ppforest2::serialization::to_json ( std::optional< Metrics > const & metrics,
Names const & group_names )

◆ to_json() [17/24]

json ppforest2::serialization::to_json ( std::optional< VariableImportance > const & vi)

◆ to_json() [18/24]

json ppforest2::serialization::to_json ( Tree const & tree)

◆ to_json() [19/24]

json ppforest2::serialization::to_json ( Tree const & tree,
Names const & group_names )

◆ to_json() [20/24]

json ppforest2::serialization::to_json ( TreeNode const & node)

◆ to_json() [21/24]

json ppforest2::serialization::to_json ( TreeNode const & node,
Names const & group_names )

◆ to_json() [22/24]

json ppforest2::serialization::to_json ( types::FeatureMatrix const & matrix)

◆ to_json() [23/24]

json ppforest2::serialization::to_json ( types::OutcomeVector const & y,
types::Names const & names )

Serialize a prediction vector as JSON.

With non-empty names, the predictions are interpreted as integer-coded class labels and mapped to label strings (names[code]). With empty names, the values are passed through verbatim as a flat JSON array — letting callers stay mode-agnostic (regression: continuous floats; classification without label metadata: integer codes).

◆ to_json() [24/24]

json ppforest2::serialization::to_json ( VariableImportance const & vi)

◆ validate_forest_export()

void ppforest2::serialization::validate_forest_export ( nlohmann::json const & j)

Validate the structural shape of a loaded model Export JSON.

Runs before any from_json decoding so failures surface with domain-specific, path-annotated messages instead of nlohmann's cryptic type errors.

Concentrates every cross-field and required-field check in one place. Strategy internals are not validated here — each strategy's own from_json performs that via JsonReader. This keeps the validator responsibilities flat (skeleton + config + meta + variant-specific model block) and avoids turning it into a schema library.

Each validator asserts model_type == "tree" / "forest" matches the call site; mismatches throw a UserError.

Exceptions
UserErrorwith a dotted path on validation failure.

◆ validate_tree_export()

void ppforest2::serialization::validate_tree_export ( nlohmann::json const & j)

Validate the structural shape of a loaded model Export JSON.

Runs before any from_json decoding so failures surface with domain-specific, path-annotated messages instead of nlohmann's cryptic type errors.

Concentrates every cross-field and required-field check in one place. Strategy internals are not validated here — each strategy's own from_json performs that via JsonReader. This keeps the validator responsibilities flat (skeleton + config + meta + variant-specific model block) and avoids turning it into a schema library.

Each validator asserts model_type == "tree" / "forest" matches the call site; mismatches throw a UserError.

Exceptions
UserErrorwith a dotted path on validation failure.