You can download the code of this step here or all the steps here.
Step 5: Scopes¶
Scopes determine how Data Nodes are shared between cycles, scenarios, and pipelines. The developer may decide to:
-
Keep Data Nodes local to each pipeline.
-
Extend the scope by sharing data nodes between a given scenario's pipelines.
-
Extend the scope by sharing data nodes across all scenarios of a given cycle.
-
Finally, extend the scope globally (across all scenarios of all cycles). For example, the initial/historical dataset is usually shared by all the scenarios/pipelines/cycles. It has a Global Scope and will be unique in the entire application.
To summarize, the different possible scopes are:
-
Pipeline scope: two pipelines can reference different Data Nodes even if their names are the same. For example, we can have a prediction Data Node of an ARIMA model (ARIMA pipeline) and a prediction Data Node of a RandomForest model (RandomForest pipeline). A scenario can contain multiple pipelines.
-
Scenario scope (default): pipelines share the same Data Node within a scenario.
-
Cycle scope: scenarios from the same Cycle share the same Data Node.
-
Global scope: Data Nodes are shared across all the scenarios/pipelines/cycles.
It is worth noting that the default scope for Data nodes is the Scenario scope.
Configuration
Modifying the scope of a Data Node is as simple as changing its Scope parameter in the configuration.
The configuration is taken in the previous step, so you can copy the last TOML Config file directly.
-
Change the Scope of historical_data to be global
-
name: historical_data
-
Details: default_path=xxxx/yyyy.csv, storage_type=csv, scope=GLOBAL:SCOPE
-
-
Change the Scope of month_data and month to be Cycle
-
name: output
-
Details: storage_type:pickle, scope=CYCLE:SCOPE
-
-
Load the new configuration in the code
Modifying the scope of a Data Node is as simple as changing its Scope parameter inside the configuration.
The configuration is taken in the previous step so you can copy the previous code directly.
from taipy.config import Scope, Frequency
historical_data_cfg = Config.configure_csv_data_node(id="historical_data",
default_path="time_series.csv",
scope=Scope.GLOBAL)
month_cfg = Config.configure_data_node(id="month", scope=Scope.CYCLE)
month_values_cfg = Config.configure_data_node(id="month_data",
scope=Scope.CYCLE)
Cycles are created based on the creation_date of scenarios. In the example below, we force the creation_date to a given date (in real life, the actual creation date of the scenario gets used automatically).
tp.Core().run()
scenario_1 = tp.create_scenario(scenario_cfg,
creation_date=dt.datetime(2022,10,7),
name="Scenario 2022/10/7")
scenario_2 = tp.create_scenario(scenario_cfg,
creation_date=dt.datetime(2022,10,5),
name="Scenario 2022/10/5")
scenario_3 = tp.create_scenario(scenario_cfg,
creation_date=dt.datetime(2021,9,1),
name="Scenario 2021/9/1")
Scenario 1 and 2 belong to the same Cycle: since month now has a Cycle scope, we can define month just once for both scenarios: 1 and 2.
scenario_1.month.write(10)
scenario_3.month.write(9)
print("Scenario 1: month", scenario_1.month.read())
print("Scenario 2: month", scenario_2.month.read())
print("Scenario 3: month", scenario_2.month.read())
Scenario 1: month 10
Scenario 2: month 10
Scenario 3: month 9
Defining the month of scenario 1 will also determine the month of scenario 2 since they share the same Data Node.
This is not the case for nb_of_values that are of Scenario scope; each nb_of_values has its own value in each scenario.