CHEERSafe Tools, Sampling, and Covariance¶
This notebook demonstrates utility JSON loading, deterministic seed derivation, Latin Hypercube sampling, and covariance matrix construction.
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
Config.reset_flags()
Config.NUM_OF_WORKERS = 1
Config.NUM_OF_WORKERS_SAMPLING = 1
Config.RANDOM_SEED = 42
from cheersafe.config import Config
Config.reset_flags()
Config.NUM_OF_WORKERS = 1
Config.NUM_OF_WORKERS_SAMPLING = 1
Config.RANDOM_SEED = 42
In [3]:
Copied!
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from cheersafe.tools import Covariance, Sampling, Utils
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from cheersafe.tools import Covariance, Sampling, Utils
In [4]:
Copied!
wind = Utils.from_json("Inputs/WindSpeed.json")
pd.DataFrame(wind[:5], columns=["ID", "mph", "m/s"])
wind = Utils.from_json("Inputs/WindSpeed.json")
pd.DataFrame(wind[:5], columns=["ID", "mph", "m/s"])
Out[4]:
| ID | mph | m/s | |
|---|---|---|---|
| 0 | 1.0 | 0.0 | 0.0 |
| 1 | 2.0 | 50.0 | 22.4 |
| 2 | 3.0 | 55.0 | 24.6 |
| 3 | 4.0 | 60.0 | 26.8 |
| 4 | 5.0 | 65.0 | 29.1 |
In [5]:
Copied!
mean = np.array([50.0, 70.0, 100.0])
cv = np.array([0.3, 0.3, 0.25])
num_component = [1, 1, 1]
sigma = Covariance.create_cov_matrix(num_component, mean, cv)
samples = Sampling.generate_lhs_samples_norm(25, mean, sigma, seed=Sampling.derive_seed("docs", 1))
pd.DataFrame(samples, columns=["RC", "RS", "RWC"]).head()
mean = np.array([50.0, 70.0, 100.0])
cv = np.array([0.3, 0.3, 0.25])
num_component = [1, 1, 1]
sigma = Covariance.create_cov_matrix(num_component, mean, cv)
samples = Sampling.generate_lhs_samples_norm(25, mean, sigma, seed=Sampling.derive_seed("docs", 1))
pd.DataFrame(samples, columns=["RC", "RS", "RWC"]).head()
Out[5]:
| RC | RS | RWC | |
|---|---|---|---|
| 0 | 45.900267 | 86.662773 | 93.131695 |
| 1 | 47.933084 | 93.625396 | 85.647669 |
| 2 | 35.563607 | 90.099257 | 106.525827 |
| 3 | 57.853936 | 81.760623 | 123.493018 |
| 4 | 67.864456 | 90.823721 | 113.562629 |
In [6]:
Copied!
fig, ax = plt.subplots(figsize=(6, 4))
ax.scatter(samples[:, 0], samples[:, 1], alpha=0.8)
ax.set_xlabel("Roof covering sample")
ax.set_ylabel("Roof sheathing sample")
ax.set_title("Deterministic LHS samples")
ax.grid(True, alpha=0.3)
plt.show()
fig, ax = plt.subplots(figsize=(6, 4))
ax.scatter(samples[:, 0], samples[:, 1], alpha=0.8)
ax.set_xlabel("Roof covering sample")
ax.set_ylabel("Roof sheathing sample")
ax.set_title("Deterministic LHS samples")
ax.grid(True, alpha=0.3)
plt.show()
In [7]:
Copied!
Config.reset_flags()
print(Sampling.resolve_seed())
Config.reset_flags()
print(Sampling.resolve_seed())
42