Scenario Object¶
Retrieving Scenarios¶
Scenarios should be accessed through the GetScenarios() API call. This function first tries to get scenarios from the current Study. If no scenarios were specified, it looks locally for a scenario in the input/scenarios directory. Returned scenarios is an array or list of scenarios.
Go Example¶
For GoLang the u.GetScenarios() function returns
two arguments - three-dimensional
structure (scen) and an error value:
import sdk
var scenarios []*sdk.VORScen
var err error
scenarios, err = u.GetScenarios()
The first dimension of scenarios is the scenario.
The second dimension is the dates/horizons of the specific scenario.
The third is a map of values for the scenario and date.
for _, scen:= range scenarios {
scenarioName := scen.Name
for _, d := range scen.Dates {
log.Println(d.Date)
for name, value := range d.Scen {
log.Println(name, value)
}
}
}
If you don't have equal numbers of horizons in each scenario, it is the user's responsibility to check for values at each horizon.
Python Example¶
Usage for Python follows the same pattern as scenarios for Golang with the following differences:
- Multiple copies of scenarios are loaded for Python threads. See Python Threads for more explanation.
- In Python, you call the routine
scenario.get_scenarios(runID, scenarioSetName). get_scenarios()returns a dictionary of scenarios. Each scenario is a list of dictionaries, one for each date. The dictionary at each date is a name-value pair of the and values of the scenario.
An example of processing a scenario in Python is the following:
from sdk import scenario
import logging
try :
scenarioSetName = handle.options["JobOptions"]["system"]["scenario"]
except KeyError :
scenarioSetName = ""
try :
runID = handle.options["RunID"]
except (TypeError,KeyError) :
runID = 0
scen = scenario.get_scenarios(runID, scenarioSetName)
logging.info("There are %d scenarios", len(scen))
## loop over scenarios
for scenarioName, s in scen.items():
# loop over horizons
logging.info(scenarioName)
for horizonNumber, horiz in enumerate(s):
for name, value in horiz.items():
logging.info("%s = %s", name, value)
Uploading Scenarios¶
You can add new scenarios to your playpen by uploading scenario files via the SDK.
Go Example¶
Use the UploadScenario function to upload a scenario file:
import (
"io"
"os"
log "github.com/sirupsen/logrus"
)
runID := frgutil.GetRunID(u.hh)
runDetails, err := sdkClient.GetRunDetails(runID)
if err != nil {
log.Fatal(err)
}
// Open and read file
file, err := os.Open("<path to scenario file>")
if err != nil {
log.Fatalf("Failed to open file: %v", err)
}
defer file.Close()
// Read the file content
content, err := io.ReadAll(file)
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
// Create upload request
request := sdk.UploadScenarioRequest{
PlaypenID: runDetails.GetPlaypenID(),
FileData: content,
FileName: "<file name>",
Comment: "<comment>",
Description: "<description>",
}
scenarioIDs, err := sdkClient.UploadScenario(request)
if err != nil {
log.Fatalf("Failed to upload scenario: %v", err)
}
log.Infoln("Uploaded scenario IDs:", scenarioIDs)
Parameters:
The UploadScenario function accepts an UploadScenarioRequest struct with the
following fields:
PlaypenID(int32): Playpen ID for the scenario uploadFileData([]byte): Contents of the scenario file as bytesFileName(string): Name of the scenario fileComment(string): Comment describing the uploadDescription(string): Description for the scenario
Returns:
- A slice of scenario IDs (
[]int32) representing the created scenarios
Python Example¶
Use the upload_scenario function:
from sdk import run, scenario
import logging
runID = handle.options["RunID"]
run_details = run.get_run_details(runID)
# Create upload request
request = scenario.UploadScenarioRequest(
playpen_id=run_details.PlaypenID,
file_data=open("<path to scenario file>", "rb").read(),
file_name="<file name>",
comment="<comment>",
description="<description>"
)
scenario_ids = scenario.upload_scenario(request)
logging.info(f"Uploaded scenario IDs: {scenario_ids}")
Parameters:
The upload_scenario function accepts an UploadScenarioRequest dataclass with
the following fields:
playpen_id(int): Playpen ID for the scenario uploadfile_data(bytes): Contents of the scenario file as bytesfile_name(str): Name of the scenario filecomment(str): Comment describing the uploaddescription(str): Description for the scenario
Returns:
- A list of scenario IDs
If the upload fails, an exception will be raised with the error message.