Skip to content

Commit f8aadd1

Browse files
committed
Resolve issues with deprecation
1 parent 0a35810 commit f8aadd1

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

pyomo/contrib/parmest/parmest.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,18 @@ def _Q_opt(
985985
values as well as the objective function value.
986986
987987
"""
988-
if calc_cov is not NOTSET and not isinstance(calc_cov, bool):
989-
raise TypeError("Expected a boolean for the 'calc_cov' argument.")
988+
if calc_cov is not NOTSET:
989+
deprecation_warning(
990+
"theta_est_leaveNout(): `calc_cov` and `cov_n` are deprecated options and "
991+
"will be removed in the future. Please use the `cov_est()` function "
992+
"for covariance calculation.",
993+
version="6.9.4.dev0",
994+
)
995+
else:
996+
calc_cov = False
997+
998+
if cov_n is NOTSET:
999+
cov_n = 0
9901000

9911001
if solver == "k_aug":
9921002
raise RuntimeError("k_aug no longer supported.")
@@ -1035,7 +1045,7 @@ def _Q_opt(
10351045

10361046
# Solve the extensive form with ipopt
10371047
if solver == "ef_ipopt":
1038-
if calc_cov is NOTSET or not calc_cov:
1048+
if not calc_cov:
10391049
# Do not calculate the reduced hessian
10401050

10411051
solver = SolverFactory('ipopt')
@@ -1045,7 +1055,7 @@ def _Q_opt(
10451055

10461056
solve_result = solver.solve(self.ef_instance, tee=self.tee)
10471057
assert_optimal_termination(solve_result)
1048-
elif calc_cov is not NOTSET and calc_cov:
1058+
else:
10491059
# parmest makes the fitted parameters stage 1 variables
10501060
ind_vars = []
10511061
for nd_name, Var, sol_val in ef_nonants(ef):
@@ -1078,11 +1088,13 @@ def _Q_opt(
10781088
self.obj_value = obj_val
10791089
self.estimated_theta = theta_vals
10801090

1081-
if calc_cov is not NOTSET and calc_cov:
1091+
if calc_cov:
10821092
# Calculate the covariance matrix
10831093

10841094
if not isinstance(cov_n, int):
1085-
raise TypeError("Expected an integer for the 'cov_n' argument.")
1095+
raise TypeError(
1096+
f"Expected an integer for the 'cov_n' argument. Got {type(cov_n)}."
1097+
)
10861098
num_unknowns = max(
10871099
[
10881100
len(experiment.get_labeled_model().unknown_parameters)
@@ -1150,14 +1162,14 @@ def _Q_opt(
11501162
if len(vals) > 0:
11511163
var_values.append(vals)
11521164
var_values = pd.DataFrame(var_values)
1153-
if calc_cov is not NOTSET and calc_cov:
1165+
if calc_cov:
11541166
return obj_val, theta_vals, var_values, cov
1155-
elif calc_cov is NOTSET or not calc_cov:
1167+
else:
11561168
return obj_val, theta_vals, var_values
11571169

1158-
if calc_cov is not NOTSET and calc_cov:
1170+
if calc_cov:
11591171
return obj_val, theta_vals, cov
1160-
elif calc_cov is NOTSET or not calc_cov:
1172+
else:
11611173
return obj_val, theta_vals
11621174

11631175
else:
@@ -1628,9 +1640,13 @@ def theta_est(
16281640
List of Variable names, used to return values from the model
16291641
for data reconciliation
16301642
calc_cov: boolean, optional
1643+
DEPRECATED.
1644+
16311645
If True, calculate and return the covariance matrix
16321646
(only for "ef_ipopt" solver). Default is NOTSET
16331647
cov_n: int, optional
1648+
DEPRECATED.
1649+
16341650
If calc_cov=True, then the user needs to supply the number of datapoints
16351651
that are used in the objective function. Default is NOTSET
16361652
@@ -1647,13 +1663,18 @@ def theta_est(
16471663
assert isinstance(solver, str)
16481664
assert isinstance(return_values, list)
16491665

1650-
if calc_cov is not NOTSET or cov_n is not NOTSET:
1666+
if calc_cov is not NOTSET:
16511667
deprecation_warning(
16521668
"theta_est(): `calc_cov` and `cov_n` are deprecated options and "
16531669
"will be removed in the future. Please use the `cov_est()` function "
16541670
"for covariance calculation.",
16551671
version="6.9.4.dev0",
16561672
)
1673+
else:
1674+
calc_cov = False
1675+
1676+
if cov_n is NOTSET:
1677+
cov_n = 0
16571678

16581679
# check if we are using deprecated parmest
16591680
if self.pest_deprecated is not None and calc_cov:
@@ -2841,7 +2862,7 @@ def theta_est(
28412862
"""
28422863
assert isinstance(solver, str)
28432864
assert isinstance(return_values, list)
2844-
assert isinstance(calc_cov, bool)
2865+
assert (calc_cov is NOTSET) or isinstance(calc_cov, bool)
28452866
if calc_cov:
28462867
assert isinstance(
28472868
cov_n, int

pyomo/contrib/parmest/tests/test_parmest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ def test_leaveNout(self):
471471
self.assertEqual(bootstrap_theta.shape[0], 3) # bootstrap for sample 1
472472
self.assertEqual(bootstrap_theta[1.0].sum(), 3) # all true
473473

474+
@pytest.mark.expensive
474475
def test_diagnostic_mode(self):
475476
self.pest.diagnostic_mode = True
476477

0 commit comments

Comments
 (0)