iode.Variables.copy

Variables.copy(pattern: str = None) Self[source]

Create a new Variables database in which each variable is a copy of the original variable from the global Variables workspace. Any change made to the copied database (subset) will not be applied to the global workspace. This can be useful for example if you want to save previous values of variables before making a simulation.

Parameters:
patternstr, optional

If provided, the copied database will only contain the variables whose name matches the provided pattern. By default (None), the copied database will contain all the variables from the global Variables workspace. The pattern syntax is the same as the one used for the __getitem__ method. If the pattern is an empty string, the copied database will be empty, creating a new detached database. Default to None.

Returns:
Variables

Examples

>>> from iode import SAMPLE_DATA_DIR
>>> from iode import variables
>>> variables.load(f"{SAMPLE_DATA_DIR}/fun.var")
Loading .../fun.var
394 objects loaded 

Variables subset

>>> # without using copy(), any modification made on 
>>> # the subset will also change the corresponding 
>>> # global Variables workspace
>>> var_subset = variables["A*"]
>>> var_subset.names
['ACAF', 'ACAG', 'AOUC', 'AOUC_', 'AQC']
>>> # a) add a variable
>>> var_subset["A0"] = 0.0
>>> "A0" in var_subset
True
>>> "A0" in variables
True
>>> variables["A0"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1960Y1:2015Y1
mode: LEVEL

name    1960Y1  1961Y1  1962Y1  ...  2013Y1  2014Y1  2015Y1
A0        0.00    0.00    0.00  ...    0.00    0.00    0.00
>>> # b) modify a variable
>>> var_subset["ACAF"] = 1.0
>>> var_subset["ACAF"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1960Y1:2015Y1
mode: LEVEL

name    1960Y1  1961Y1  1962Y1  ...  2013Y1  2014Y1  2015Y1
ACAF      1.00    1.00    1.00  ...    1.00    1.00    1.00

>>> variables["ACAF"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1960Y1:2015Y1
mode: LEVEL

name    1960Y1  1961Y1  1962Y1  ...  2013Y1  2014Y1  2015Y1
ACAF      1.00    1.00    1.00  ...    1.00    1.00    1.00

>>> # c) delete a variable
>>> del var_subset["ACAG"]
>>> "ACAG" in var_subset
False
>>> "ACAG" in variables
False

Copied database subset

>>> var_subset_copy = variables["B*"].copy()
>>> var_subset_copy.names
['BENEF', 'BQY', 'BRUGP', 'BVY']
>>> # or equivalently
>>> var_subset_copy = variables.copy("B*")
>>> var_subset_copy.names
['BENEF', 'BQY', 'BRUGP', 'BVY']
>>> # by using copy(), any modification made on the copy subset 
>>> # let the global workspace unchanged
>>> # a) add a variable -> only added in the copied subset
>>> var_subset_copy["B0"] = 0.0
>>> "B0" in var_subset_copy
True
>>> "B0" in variables
False
>>> # b) modify a variable -> only modified in the copied subset
>>> var_subset_copy["BENEF"] = 1.0
>>> var_subset_copy["BENEF"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1960Y1:2015Y1
mode: LEVEL

name    1960Y1  1961Y1  1962Y1  ...  2013Y1  2014Y1  2015Y1
BENEF     1.00    1.00    1.00  ...    1.00    1.00    1.00

>>> variables["BENEF"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1960Y1:2015Y1
mode: LEVEL

name    1960Y1  1961Y1  1962Y1  ...  2013Y1  2014Y1   2015Y1
BENEF    11.66   13.61   12.21  ...   19.00  -32.20  -117.38

>>> # c) delete a variable -> only deleted in the copied subset
>>> del var_subset_copy["BQY"]
>>> "BQY" in var_subset_copy
False
>>> "BQY" in variables
True

New detached Variables database

>>> # a new empty *detached* Variables database can be created by passing 
>>> # an empty string to the copy() method 
>>> var_detached = variables.copy("")
>>> var_detached.names
[]
>>> # or equivalently by using the new_detached() method 
>>> var_detached = variables.new_detached()
>>> var_detached.names
[]