CHEERSafe Configuration and Execution Modes¶
This notebook shows how Config.AUTO_LOAD_DATASETS and Config.AUTO_EXECUTE_METHODS change object initialization. It is for users who want to choose between automatic setup and explicit method calls.
In [1]:
Copied!
from pathlib import Path
import os
repo_root = Path.cwd()
while not (repo_root / "pyproject.toml").exists() and repo_root != repo_root.parent:
repo_root = repo_root.parent
os.chdir(repo_root)
repo_root
from pathlib import Path
import os
repo_root = Path.cwd()
while not (repo_root / "pyproject.toml").exists() and repo_root != repo_root.parent:
repo_root = repo_root.parent
os.chdir(repo_root)
repo_root
Out[1]:
PosixPath('/home/mammad/Documents/EDU/cope/CHEERSafe/LossModel')
In [2]:
Copied!
from cheersafe.config import Config
from cheersafe.inventory import Building, BuildingInventory
from cheersafe.tools import Utils
Config.reset_flags()
print(Config.AUTO_LOAD_DATASETS, Config.AUTO_EXECUTE_METHODS, Config.INPUTS_PATH)
from cheersafe.config import Config
from cheersafe.inventory import Building, BuildingInventory
from cheersafe.tools import Utils
Config.reset_flags()
print(Config.AUTO_LOAD_DATASETS, Config.AUTO_EXECUTE_METHODS, Config.INPUTS_PATH)
True True Inputs
With both flags on, loaders and calculation methods run during initialization for classes that support those flags.
In [3]:
Copied!
Config.AUTO_LOAD_DATASETS = True
Config.AUTO_EXECUTE_METHODS = True
inventory = BuildingInventory(buildings_file_name="BuildingGeometry_Old.json")
building = inventory.buildings[0]
print(f"loaded buildings: {len(inventory.buildings)}")
print(f"panel coordinate shape: {building.PanelCoordinate.shape}")
print(f"connection coordinate shape: {building.ConnectionCoordinate.shape}")
Config.AUTO_LOAD_DATASETS = True
Config.AUTO_EXECUTE_METHODS = True
inventory = BuildingInventory(buildings_file_name="BuildingGeometry_Old.json")
building = inventory.buildings[0]
print(f"loaded buildings: {len(inventory.buildings)}")
print(f"panel coordinate shape: {building.PanelCoordinate.shape}")
print(f"connection coordinate shape: {building.ConnectionCoordinate.shape}")
loaded buildings: 8 panel coordinate shape: (6, 96) connection coordinate shape: (94,)
With both flags off, users load data and assign returned calculation outputs themselves.
In [4]:
Copied!
Config.AUTO_LOAD_DATASETS = False
Config.AUTO_EXECUTE_METHODS = False
data = Utils.from_json("Inputs/BuildingGeometry_Old.json")
manual_building = Building(*data[:, 0])
panel_coordinate, num_panel = manual_building.fn_panel_coordinate()
connection_coordinate, num_connection = manual_building.fn_connection_coordinate()
manual_building.PanelCoordinate = panel_coordinate
manual_building.NumPanel = num_panel
manual_building.ConnectionCoordinate = connection_coordinate
manual_building.NumConnection = num_connection
print(num_panel)
print(num_connection)
Config.AUTO_LOAD_DATASETS = False
Config.AUTO_EXECUTE_METHODS = False
data = Utils.from_json("Inputs/BuildingGeometry_Old.json")
manual_building = Building(*data[:, 0])
panel_coordinate, num_panel = manual_building.fn_panel_coordinate()
connection_coordinate, num_connection = manual_building.fn_connection_coordinate()
manual_building.PanelCoordinate = panel_coordinate
manual_building.NumPanel = num_panel
manual_building.ConnectionCoordinate = connection_coordinate
manual_building.NumConnection = num_connection
print(num_panel)
print(num_connection)
[34 34 14 14] [31, 31, 16, 16]
Always reset global configuration after an example or experiment so later code starts from package defaults.
In [5]:
Copied!
Config.reset_flags()
print(Config.AUTO_LOAD_DATASETS, Config.AUTO_EXECUTE_METHODS)
Config.reset_flags()
print(Config.AUTO_LOAD_DATASETS, Config.AUTO_EXECUTE_METHODS)
True True