Reference
avenir_goals_scenario.draw_simulations(definition_path, base_year, n_simulations=100, seed=None)
¶
Draw scenario simulations in memory from a scenario definitions file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
definition_path
|
Path | str
|
Path to the scenario definitions file ( |
required |
n_simulations
|
int
|
Number of simulations drawn per scenario. |
100
|
seed
|
int | None
|
Optional integer seed for reproducible draws. |
None
|
base_year
|
int
|
If provided, used as the minimum value for |
required |
Returns:
| Type | Description |
|---|---|
ScenarioSimulations
|
A |
ScenarioSimulations
|
all scenarios and their draws. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
ValueError
|
If |
Source code in src/avenir_goals_scenario/scenarios.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
avenir_goals_scenario.write_simulations(simulations, path)
¶
Write scenario simulations to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulations
|
ScenarioSimulations
|
The scenario simulations to write. |
required |
path
|
Path | str
|
Destination path for the JSON file. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The resolved path that was written to. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the parent directory of |
Source code in src/avenir_goals_scenario/scenarios.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
avenir_goals_scenario.read_simulations(path)
¶
Read scenario simulations from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to the scenario simulations JSON file. |
required |
Returns:
| Type | Description |
|---|---|
ScenarioSimulations
|
The loaded |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
ValueError
|
If the file contents fail schema validation. |
Source code in src/avenir_goals_scenario/scenarios.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
avenir_goals_scenario.run_scenario_analysis(config, simulations)
¶
Run scenario analysis across a directory of PJNZ files.
Converts each PJNZ to leapfrog params once in the main process, dumps them
to a temporary file using pickle.dump, then distributes (PJNZ, batch)
work units — each a batch of config.scenarios_per_file scenarios — across
worker processes. Workers load params via pickle.load.
Results are written to Parquet files under config.output_dir in
long format, one file per (indicator, PJNZ, batch) at
{output_dir}/{indicator}/pjnz_name={pjnz_stem}/{part_name}.parquet. Each
file holds every scenario in the batch, identified by a scenario_id
column.
Scenarios whose simulations fail are logged and skipped rather than aborting
the run; the rest still complete. Failed scenarios are recorded in the
returned :class:RunResult and written to a failures.json manifest under
config.output_dir for re-running. PJNZ import failures remain fatal and
raise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
RunConfig
|
Validated run configuration. |
required |
simulations
|
ScenarioSimulations
|
Scenario simulations to run. Use
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
RunResult
|
class: |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If no PJNZ files are found in |
ValueError
|
If any output indicator is not present in the Goals output, or if a PJNZ file cannot be parsed. |
Source code in src/avenir_goals_scenario/runner.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
avenir_goals_scenario.RunConfig
¶
Bases: BaseModel
Configuration for avenir_goals_scenario.run_scenario_analysis.
Field names are case-insensitive: PJNZ_Dir, pjnz_dir, and
PJNZ_DIR are all accepted.
output_dir is created if it does not exist, but only one level deep -
its parent must already exist.
Attributes:
| Name | Type | Description |
|---|---|---|
pjnz_dir |
Path
|
Directory containing |
definition_path |
Path | None
|
Path to the scenario definition JSON file.
Required for the |
scenario_path |
Path | None
|
Path to a scenario simulations JSON file.
For |
output_dir |
Path
|
Directory where per-PJNZ result subdirectories are written. Created automatically if absent (parent must exist). |
base_year |
int
|
First year of the output projection range. |
output_indicators |
list[str]
|
Names of the Goals output indicators to
write. Each name must be a key in the dict returned by |
n_simulations |
int
|
Number of simulations drawn per scenario (default
100). Ignored when loading draws from an existing |
seed |
int | None
|
Optional RNG seed for reproducible draws. |
n_workers |
int
|
Number of parallel worker processes. |
scenarios_per_file |
int
|
Number of scenarios written to each output
Parquet file, which is also the unit of parallel work (default 128).
The run parallelises over |
Source code in src/avenir_goals_scenario/models/run_config.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |