Skip to content

Bug fixes in the current parmest.py and example files #3635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pyomo/contrib/parmest/examples/semibatch/semibatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ def label_model(self):

m = self.model

m.experiment_outputs = Suffix(direction=Suffix.LOCAL)
m.experiment_outputs.update(
(m.Ca[t], self.data["Ca_meas"][f"{t}"]) for t in m.measT
)
m.experiment_outputs.update(
(m.Cb[t], self.data["Cb_meas"][f"{t}"]) for t in m.measT
)
m.experiment_outputs.update(
(m.Cc[t], self.data["Cc_meas"][f"{t}"]) for t in m.measT
)
m.experiment_outputs.update(
(m.Tr[t], self.data["Tr_meas"][f"{t}"]) for t in m.measT
)

m.unknown_parameters = Suffix(direction=Suffix.LOCAL)
m.unknown_parameters.update(
(k, ComponentUID(k)) for k in [m.k1, m.k2, m.E1, m.E2]
Expand Down
4 changes: 2 additions & 2 deletions pyomo/contrib/parmest/parmest.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ def __init__(
try:
outputs = [k.name for k, v in model.experiment_outputs.items()]
except:
RuntimeError(
raise RuntimeError(
'Experiment list model does not have suffix ' + '"experiment_outputs".'
)
try:
params = [k.name for k, v in model.unknown_parameters.items()]
except:
RuntimeError(
raise RuntimeError(
'Experiment list model does not have suffix ' + '"unknown_parameters".'
)

Expand Down
94 changes: 94 additions & 0 deletions pyomo/contrib/parmest/tests/test_parmest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ def SSE(model):
exp_list, obj_function=SSE, solver_options=solver_options, tee=True
)

def test_parmest_exception(self):
"""
Test the exception raised by parmest when the "experiment_outputs"
attribute is not defined in the model
"""
from pyomo.contrib.parmest.examples.rooney_biegler.rooney_biegler import (
RooneyBieglerExperiment,
)

# create an instance of the RooneyBieglerExperiment class
# without the "experiment_outputs" attribute
class RooneyBieglerExperimentException(RooneyBieglerExperiment):
def label_model(self):
m = self.model

# add the unknown parameters
m.unknown_parameters = pyo.Suffix(direction=pyo.Suffix.LOCAL)
m.unknown_parameters.update(
(k, pyo.ComponentUID(k)) for k in [m.asymptote, m.rate_constant]
)

# create an experiment list
exp_list = []
for i in range(self.data.shape[0]):
exp_list.append(RooneyBieglerExperimentException(self.data.loc[i, :]))

# check the exception raised by parmest due to not defining
# the "experiment_outputs"
with self.assertRaises(RuntimeError) as context:
parmest.Estimator(exp_list, obj_function="SSE", tee=True)

self.assertIn("experiment_outputs", str(context.exception))

def test_theta_est(self):
objval, thetavals = self.pest.theta_est()

Expand Down Expand Up @@ -800,6 +833,22 @@ def label_model(self):

m = self.model

if isinstance(self.data, pd.DataFrame):
meas_time_points = self.data.index
else:
meas_time_points = list(self.data["ca"].keys())

m.experiment_outputs = pyo.Suffix(direction=pyo.Suffix.LOCAL)
m.experiment_outputs.update(
(m.ca[t], self.data["ca"][t]) for t in meas_time_points
)
m.experiment_outputs.update(
(m.cb[t], self.data["cb"][t]) for t in meas_time_points
)
m.experiment_outputs.update(
(m.cc[t], self.data["cc"][t]) for t in meas_time_points
)

m.unknown_parameters = pyo.Suffix(direction=pyo.Suffix.LOCAL)
m.unknown_parameters.update(
(k, pyo.ComponentUID(k)) for k in [m.k1, m.k2]
Expand Down Expand Up @@ -869,6 +918,51 @@ def get_labeled_model(self):
self.m_df = ABC_model(data_df)
self.m_dict = ABC_model(data_dict)

# create an instance of the ReactorDesignExperimentDAE class
# without the "unknown_parameters" attribute
class ReactorDesignExperimentException(ReactorDesignExperimentDAE):
def label_model(self):

m = self.model

if isinstance(self.data, pd.DataFrame):
meas_time_points = self.data.index
else:
meas_time_points = list(self.data["ca"].keys())

m.experiment_outputs = pyo.Suffix(direction=pyo.Suffix.LOCAL)
m.experiment_outputs.update(
(m.ca[t], self.data["ca"][t]) for t in meas_time_points
)
m.experiment_outputs.update(
(m.cb[t], self.data["cb"][t]) for t in meas_time_points
)
m.experiment_outputs.update(
(m.cc[t], self.data["cc"][t]) for t in meas_time_points
)

# create an experiment list without the "unknown_parameters" attribute
exp_list_df_no_params = [ReactorDesignExperimentException(data_df)]
exp_list_dict_no_params = [ReactorDesignExperimentException(data_dict)]

self.exp_list_df_no_params = exp_list_df_no_params
self.exp_list_dict_no_params = exp_list_dict_no_params

def test_parmest_exception(self):
"""
Test the exception raised by parmest when the "unknown_parameters"
attribute is not defined in the model
"""
with self.assertRaises(RuntimeError) as context:
parmest.Estimator(self.exp_list_df_no_params, obj_function="SSE")

self.assertIn("unknown_parameters", str(context.exception))

with self.assertRaises(RuntimeError) as context:
parmest.Estimator(self.exp_list_dict_no_params, obj_function="SSE")

self.assertIn("unknown_parameters", str(context.exception))

def test_dataformats(self):
obj1, theta1 = self.pest_df.theta_est()
obj2, theta2 = self.pest_dict.theta_est()
Expand Down
Loading