Skip to content

Saving results

The output can be stored either in a single JSON file or in multiple JSON files. Each JSON file contains an array of objects.

Output

The save_json function is responsible for saving the results. A dedicated directory named results_yyyymmdd_hhmmss is automatically created to store all generated files.

scentree.io.writer.save_json

save_json(output_dir: Union[str, Path], num_stages: int, predicted_value: List[NDArray], observed_value: Union[List[NDArray], None], scenario_trees: ScenarioTrees, mapping_datasets_columns: DatasetMappings, in_sample_prediction: bool = True, multiple_files: bool = False) -> None

Save model results and scenario trees into a JSON file.

This function stores simulation results, predictions, and tree structures into a timestamped output directory. It supports both single-file and multi-file export modes.

Parameters:

Name Type Description Default
output_dir Union[str, Path]

Directory where results will be saved.

required
num_stages int

Number of stages in the scenario model.

required
predicted_value List[NDArray]

List of predicted value arrays, one per tree.

required
observed_value Union[List[NDArray], None]

List of observed values, or None if not available.

required
scenario_trees ScenarioTrees

List of scenario tree. Each position contains a scenario tree.

required
mapping_datasets_columns DatasetMappings

Mapping between dataset names, their column indices and their stages.

required
in_sample_prediction bool

Whether predictions are in-sample. Default to true.

True
multiple_files bool

If True, results are saved in separate files (not yet implemented). If False, all results are stored in a single JSON file.

False

Raises:

Type Description
FileNotFoundError

If output_dir does not exist.

NotADirectoryError

If output_dir is not a directory.

Description of the file

The JSON file contains an array of objects. Each object includes the following fields:

Field Type Description
num_scenarios integer Number of generated scenarios.
num_stages integer Number of stages in the stochastic problem.
in_sample_prediction boolean Indicates whether the generated scenarios correspond to observed days (true) or future days (false).
scenario_tree_data array of array of float Scenario data. It contains one array per scenario, where each inner array represents a single scenario.
mean_value_scenario_tree array of float Mean value of the generated scenarios.
predicted_value array of float Predicted values.
observed_value array of float or null Observed values. If in_sample_prediction is true, this field contains an array of length data_dim; otherwise, it is null.
scenario_probabilities array of float Probability associated with each generated scenario.
mapping_datasets_columns array of object Mapping between the original datasets and the corresponding columns in the scenario data. Each object contains the dataset name and the associated column indices.
tree array of object Description of the generated scenario tree. The structure of each object is described in the table below.

Tree object

Field Type Description
key int[2] A pair representing (stage, position in stage).
scenario_ids int[] Array of scenario IDs that belong to the same cluster.
parent_key int[2] or null Parent key of the current node. It is null for root nodes.
description string Textual description of the key field.