Skip to content

Commit 5768bfa

Browse files
committed
'add_plot'
1 parent a17f171 commit 5768bfa

37 files changed

+1109
-316
lines changed

UQPyL/analysis/base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ def generateNetCDF(self):
236236
ds = xr.Dataset(
237237

238238
data_vars = {
239-
"X" : (("n", "nI"), X, {"description": "decision variables"}),
240-
"Y" : (("n", "nO"), Y, {"description": "objectives or constraints"}),
239+
"X" : (("idx", "nI"), X, {"description": "decision variables"}),
240+
"Y" : (("idx", "nO"), Y, {"description": "objectives or constraints"}),
241241
},
242242

243243
coords = {
@@ -251,7 +251,10 @@ def generateNetCDF(self):
251251
"problem" : f"{self.obj.problem.name}_{self.obj.problem.nInput}D_{self.obj.problem.nOutput}O_{self.obj.problem.nCons}C",
252252
"method" : self.obj.name,
253253
"created": datetime.now().isoformat(timespec='seconds'),
254-
**self.setting.dicts,
254+
**{
255+
k: (str(v) if isinstance(v, bool) else v)
256+
for k, v in self.obj.setting.dict.items()
257+
}
255258
}
256259

257260
)

UQPyL/analysis/rbd_fast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class RBDFAST(AnalysisABC):
3838
-------------------------------------------------
3939
"""
4040

41-
name = "RBD_FAST"
41+
name = "RBDFAST"
4242

4343
def __init__(self, scalers: Tuple[Optional[Scaler], Optional[Scaler]] = (None, None),
4444
M: int = 4,

UQPyL/analysis/sobol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ def analyze(self, problem: Problem, X: np.ndarray, Y: Optional[np.ndarray] = Non
214214

215215
# Calculate first-order and total-order sensitivity indices for each input variable
216216
for j in range(nInput):
217-
S1[i] = self._firstOrder(A, AB[:, j:j + 1], B)
218-
ST[i] = self._totalOrder(A, AB[:, j:j + 1], B)
217+
S1[i, j] = self._firstOrder(A, AB[:, j:j + 1], B)
218+
ST[i, j] = self._totalOrder(A, AB[:, j:j + 1], B)
219219

220220
if secondOrder:
221221
# S2 = []

UQPyL/inference/amh.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class AMH(InferenceABC):
1414

1515
name = "Adaptive Metropolis-Hastings"
1616

17-
def __init__(self, nChain: int = 1, warmUp: int = 1000, maxIterTimes: int = 1000,
17+
def __init__(self, nChains: int = 1, warmUp: int = 1000, maxIterTimes: int = 1000,
1818
propDist: Literal['gauss', 'uniform'] = 'gauss',
1919
verboseFlag: bool = True, verboseFreq: int = 10,
2020
logFlag: bool = False, saveFlag: bool = True):
2121
super().__init__(maxIterTimes, verboseFlag, verboseFreq, logFlag, saveFlag)
2222

23-
self.setParaVal('nChain', nChain)
23+
self.setParaVal('nChains', nChains)
2424
self.setParaVal('warmUp', warmUp)
2525
self.setParaVal('propDist', propDist)
2626

@@ -35,27 +35,27 @@ def run(self, problem: ProblemABC, gamma: Union[float, np.ndarray] = 0.1, seed:
3535

3636
self.setup(problem, seed)
3737

38-
nChain = self.getParaVal('nChain')
38+
nChains = self.getParaVal('nChains')
3939
warmUp = self.getParaVal('warmUp')
4040
propDist = self.getParaVal('propDist')
4141
self.setParaVal('gamma', gamma)
4242

4343
gamma = self._check_gamma_(gamma)
4444
sd = 2.38**2 / problem.nInput
4545

46-
X_init, Objs_init, Cons_init = self.initialSampling(problem, nChain)
46+
X_init, Objs_init, Cons_init = self.initialSampling(problem, nChains)
4747

48-
chains = self.initChains(nChain, X_init, Objs_init, Cons_init)
48+
chains = self.initChains(nChains, X_init, Objs_init, Cons_init)
4949

5050
# calculate proposal covariance matrix
5151
gamma = self._check_gamma_(gamma)
5252
propCovs = []
5353

54-
for i in range(nChain):
54+
for i in range(nChains):
5555
cov = (gamma[i] * (problem.ub - problem.lb))**2
5656
propCovs.append(np.diag(cov.ravel()))
5757

58-
ac_rate = np.zeros(nChain)
58+
ac_rate = np.zeros(nChains)
5959

6060
X_cur = X_init; Objs_cur = Objs_init; Cons_cur = Cons_init
6161

@@ -65,7 +65,7 @@ def run(self, problem: ProblemABC, gamma: Union[float, np.ndarray] = 0.1, seed:
6565

6666
Objs_star, Cons_star = self.evaluate(X_star)
6767

68-
for i in range(nChain):
68+
for i in range(nChains):
6969

7070
if np.log(np.random.rand()) < (self.log_prob(Objs_star[i]) - self.log_prob(Objs_cur[i])) \
7171
and (problem.nCons == 0 or (problem.nCons > 0 and all(Cons_star[i] <= 0))):
@@ -86,7 +86,7 @@ def run(self, problem: ProblemABC, gamma: Union[float, np.ndarray] = 0.1, seed:
8686
if np.log(np.random.rand()) < (self.log_prob(Objs_star[i]) - self.log_prob(Objs_cur[i])) \
8787
and (problem.nCons == 0 or (problem.nCons > 0 and all(Cons_star[i] <= 0))):
8888

89-
ac_rate[i] += 1 / self.maxIter
89+
ac_rate[i] += 1 / self.maxIters
9090

9191
X_cur[i] = X_star[i]; Objs_cur[i] = Objs_star[i]
9292

@@ -110,7 +110,7 @@ def run(self, problem: ProblemABC, gamma: Union[float, np.ndarray] = 0.1, seed:
110110
"acceptanceRate_mean" : ((), mean_ac_rate, {"long_name": "mean of acceptance rate for all chains", "description": "mean of acceptance rate for all chains"})
111111
},
112112
coords = {
113-
"chain": np.arange(nChain),
113+
"chain": np.arange(nChains),
114114
},
115115
)
116116

@@ -121,7 +121,7 @@ def run(self, problem: ProblemABC, gamma: Union[float, np.ndarray] = 0.1, seed:
121121
"lg" : (("chain", "draw", "objsDim"), lg, {"long_name": "log probability", "description": "log probability for each sample"}),
122122
},
123123
coords = {
124-
"chain": np.arange(nChain),
124+
"chain": np.arange(nChains),
125125
"draw": np.arange(self.iter),
126126
"objsDim": np.arange(self.problem.nOutput),
127127
},
@@ -144,21 +144,21 @@ def updateCovs(self, chains, sd):
144144

145145
def _check_alpha(self, gamma):
146146

147-
nChain = self.getParaVal('nChain')
147+
nChains = self.getParaVal('nChains')
148148
nInput = self.problem.nInput
149149

150150
if isinstance(gamma, float):
151-
gamma = np.full((nChain, nInput), gamma)
151+
gamma = np.full((nChains, nInput), gamma)
152152

153153
elif isinstance(gamma, np.ndarray):
154154
gamma = np.atleast_2d(gamma)
155155
n, _ = gamma.shape
156156
if n == 1:
157-
gamma = np.tile(gamma, (nChain, 1))
158-
elif n == nChain:
157+
gamma = np.tile(gamma, (nChains, 1))
158+
elif n == nChains:
159159
gamma = gamma
160160
else:
161-
raise ValueError("The shape of gamma must be (nChain, nInput) or (1, nInput)")
161+
raise ValueError("The shape of gamma must be (nChains, nInput) or (1, nInput)")
162162
else:
163163
raise ValueError("gamma must be a float or a numpy array")
164164

UQPyL/inference/base.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class InferenceABC(metaclass = abc.ABCMeta):
1313

14-
def __init__(self, maxIter: int = 1000,
14+
def __init__(self, maxIters: int = 1000,
1515
verboseFlag: bool = True, verboseFreq: int = 10,
1616
logFlag: bool = False, saveFlag: bool = False):
1717

@@ -20,7 +20,7 @@ def __init__(self, maxIter: int = 1000,
2020
self.logFlag = logFlag
2121
self.saveFlag = saveFlag
2222

23-
self.maxIter = maxIter
23+
self.maxIters = maxIters
2424

2525
self.setting = Setting()
2626

@@ -43,11 +43,11 @@ def setup(self, problem: ProblemABC, seed: int = None):
4343
self.setParaVal('seed', seed)
4444
np.random.seed(seed)
4545

46-
def initialSampling(self, problem: ProblemABC, nChain: int, seed: int = None):
46+
def initialSampling(self, problem: ProblemABC, nChains: int, seed: int = None):
4747

4848
sampler = LHS()
4949
sample_seed = np.random.randint(1, 1000000)
50-
X0 = sampler.sample(self.problem, nChain, sample_seed)
50+
X0 = sampler.sample(self.problem, nChains, sample_seed)
5151
Objs0, Cons0 = self.evaluate(X0)
5252

5353
return X0, Objs0, Cons0
@@ -63,12 +63,12 @@ def _check_bound_(self, X, ub, lb):
6363

6464
def _check_gamma_(self, gamma):
6565

66-
nChain = self.getParaVal('nChain')
66+
nChains = self.getParaVal('nChains')
6767
nInput = self.problem.nInput
6868

6969
if isinstance(gamma, float):
7070

71-
gamma = np.full((nChain, nInput), gamma)
71+
gamma = np.full((nChains, nInput), gamma)
7272

7373
elif isinstance(gamma, np.ndarray):
7474

@@ -77,11 +77,11 @@ def _check_gamma_(self, gamma):
7777
n, _ = gamma.shape
7878

7979
if n == 1:
80-
gamma = np.tile(gamma, (nChain, 1))
81-
elif n == nChain:
80+
gamma = np.tile(gamma, (nChains, 1))
81+
elif n == nChains:
8282
gamma = gamma
8383
else:
84-
raise ValueError("The shape of gamma must be (nChain, nInput) or (1, nInput)")
84+
raise ValueError("The shape of gamma must be (nChains, nInput) or (1, nInput)")
8585
else:
8686
raise ValueError("gamma must be a float or a numpy array")
8787

@@ -95,7 +95,7 @@ def initChains(self, nChains: int, X: np.ndarray, Objs: np.ndarray, Cons: np.nda
9595

9696
nI = self.problem.nInput; nO = self.problem.nOutput; nC = self.problem.nCons
9797

98-
iters = self.maxIter
98+
iters = self.maxIters
9999

100100
chains = []
101101

@@ -118,7 +118,7 @@ def checkTermination(self, chains):
118118

119119
Verbose.verboseInference(verbRes, self.problem)
120120

121-
if self.iter >= self.maxIter:
121+
if self.iter >= self.maxIters:
122122
return False
123123

124124
return True
@@ -173,7 +173,7 @@ def genNetCDF(self, chains, problem):
173173

174174
res = {};
175175

176-
nChain = len(chains); draw = chains[0].count
176+
nChains = len(chains); draw = chains[0].count
177177

178178
nInput = problem.nInput; nOutput = problem.nOutput; nCons = problem.nCons
179179

@@ -185,7 +185,7 @@ def genNetCDF(self, chains, problem):
185185
cons = np.stack([c.cons for c in chains])
186186
feasibleMask = (cons <= 0).all("consDim")
187187
else:
188-
feasibleMask = np.ones((nChain, decs.shape[1]), dtype=bool)
188+
feasibleMask = np.ones((nChains, decs.shape[1]), dtype=bool)
189189

190190
posterior_ds = xr.Dataset(
191191
data_vars = {
@@ -197,7 +197,7 @@ def genNetCDF(self, chains, problem):
197197
"proLB" : (("decsDim"), problem.lb.ravel(), {"long_name": "lower bound of decision variables / parameters", "description": "lower bound of decision variables / parameters"}),
198198
},
199199
coords = {
200-
"chain": np.arange(nChain),
200+
"chain": np.arange(nChains),
201201
"draw": np.arange(draw),
202202
"decsDim": np.arange(nInput),
203203
"objsDim": np.arange(nOutput),
@@ -222,7 +222,7 @@ def genNetCDF(self, chains, problem):
222222
bestDecs = []
223223
bestObjs = []
224224

225-
for i in range(nChain):
225+
for i in range(nChains):
226226

227227
decs_i = decs[i]
228228
objs_i = objs_min[i]
@@ -282,7 +282,7 @@ def genNetCDF(self, chains, problem):
282282
"stdAll" : (("decsDim"), stdAll, {"long_name": "standard deviation of feasible decision variables for all chains", "description": "standard deviation of feasible decision variables for all chains"}),
283283
},
284284
coords = {
285-
"chain": np.arange(nChain),
285+
"chain": np.arange(nChains),
286286
"decsDim": np.arange(nInput),
287287
"objsDim": np.arange(nOutput),
288288
**({"consDim": np.arange(nCons)} if nCons > 0 else {}),
@@ -309,7 +309,7 @@ def genNetCDF(self, chains, problem):
309309
},
310310

311311
coords = {
312-
"chain": np.arange(nChain),
312+
"chain": np.arange(nChains),
313313
"decsDim": np.arange(nInput),
314314
"objsDim": np.arange(nOutput),
315315
},

0 commit comments

Comments
 (0)