Skip to content

Scenario config

In this section, we delve into the specifics of configuring scenarios for what if analysis in Taipy.

A scenario configuration is necessary to instantiate a Scenario. To create a ScenarioConfig, you can use the Config.configure_scenario() method with the following parameters:

  • id: The id of a new scenario configuration to be created. This id is mandatory and must be a unique and valid Python identifier.
  • tasks: A list of task configurations.
  • additional_data_nodes: A list of additional data node configurations.
  • frequency: The recurrence of the scenarios instantiated from this configuration. The scenarios are attached to the proper cycles based on this frequency.
  • sequences: A dictionary of sequence descriptions.
  • comparators: A list of functions used to compare scenarios. A comparator function is attached to a scenario's data node configuration. During the scenario comparison, each comparator is applied to all the data nodes instantiated from the data node configuration attached to the comparator.
  • properties: A dictionary of additional properties.

Reserved keys

Note that we cannot use the word "_entity_owner" as a key in the properties as it has been reserved for internal use.

Additional data nodes

A scenario configuration ScenarioConfig holds some data node configurations. These configurations can be added to the scenario configuration using the Config.configure_scenario() method.

Example

1
2
3
4
5
from taipy import Config, Scope

data_node_cfg = Config.configure_data_node("data", scope=Scope.GLOBAL)
scenario_cfg = Config.configure_scenario("scenario_with_one_data_node",
                                         additional_data_nodes=[data_node_cfg])

In this example, we have a scenario configuration scenario_config containing one single data node configuration data_node_cfg. This data node configuration is added to the scenario configuration as an additional data node configuration., which means that the data node configuration is not part of any task, or any execution graph.

A more realistic example is available in the multiple scenario section.

Frequency for cycle management

A ScenarioConfig can also hold a frequency to model recurrent business problem to solve. When a frequency is provided to a configuration, the scenarios instantiated from this configuration are automatically attached to the Cycle (a time period) corresponding to the date of the scenario creation.

Example

In the example below, we have a scenario configuration scenario_config with a weekly frequency. Each scenario instantiated from this configuration will be attached to a cycle corresponding to the week of the creation date of the scenario.

1
2
3
4
5
6
7
8
9
from taipy import Config, Scope, Frequency

global_data_cfg = Config.configure_data_node("global_data", scope=Scope.GLOBAL)
cycle_data_cfg = Config.configure_data_node("cycle_data", scope=Scope.CYCLE)
scenario_data_cfg = Config.configure_data_node("data", scope=Scope.SCENARIO)

scenario_cfg = Config.configure_scenario("scenario",
                                         frequency=Frequency.WEEKLY,
                                         additional_data_nodes=[global_data_cfg, cycle_data_cfg, scenario_data_cfg])

Three data node configurations are added to the scenario configuration as additional data node configurations (a global data node configuration, a cycle data node configuration, and a scenario data node configuration). When a scenario is created, the global data node configuration is instantiated once and shared by all the scenarios. The cycle data node configuration is instantiated once per cycle and shared by all the scenarios attached to the same cycle. The scenario data node configuration is instantiated once per scenario.

For more details, on scopes and cycles see the recurrent Scenarios page.

Adding scenario comparator

A ScenarioConfig can also hold some scenario comparators. A scenario comparator is a user function used to compare multiple scenarios from the same configuration.

Example

In the example below, we have a scenario configuration scenario_config with a scenario comparator compare_kpis. This comparator is used to compare the kpi data nodes of the compared scenarios.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from taipy import Config


def compare_kpis(*data_to_compare):
    return max(*data_to_compare)


kpi_cfg = Config.configure_data_node("KPI")
scenario_cfg = Config.configure_scenario("scenario",
                                         additional_data_node_configs=[kpi_cfg],
                                         comparators={kpi_cfg.id: compare_kpis})

The compare_kpis function compares the kpi data (the result of the DataNode.read() method) and returns the max value as a comparison result.

For more details and examples, see the scenario comparison page.

Adding task configurations

In addition to the data nodes configurations, a scenario configuration ScenarioConfig can also hold some task configurations connecting data node configurations together and forming an execution graph. These task configurations can be added to the scenario configuration using the Config.configure_scenario() method. This is useful when you want to orchestrate the execution of tasks for data processing or data transformation.
For more details, see the task orchestration page.

Adding sequence descriptions

In addition to the data nodes configurations, a scenario configuration ScenarioConfig can also hold some task configurations connecting data node configurations together and forming an execution graph. These task configurations can be added to the scenario configuration using the Config.configure_scenario() method. For more details, see the Sequence configuration page.