From 2bcb0e074dce33d2aa965e63e1d647fa4aa7403c Mon Sep 17 00:00:00 2001 From: kaklise Date: Thu, 15 Feb 2024 16:04:57 -0800 Subject: [PATCH] Data formatting moved to create_model --- .../rooney_biegler/bootstrap_example.py | 2 +- .../likelihood_ratio_example.py | 2 +- .../parameter_estimation_example.py | 2 +- .../examples/rooney_biegler/rooney_biegler.py | 12 ++++--- .../rooney_biegler_with_constraint.py | 12 ++++--- pyomo/contrib/parmest/tests/test_parmest.py | 32 +++++++++++-------- 6 files changed, 35 insertions(+), 27 deletions(-) diff --git a/pyomo/contrib/parmest/examples/rooney_biegler/bootstrap_example.py b/pyomo/contrib/parmest/examples/rooney_biegler/bootstrap_example.py index 953de98a48e..b9ef114c2b3 100644 --- a/pyomo/contrib/parmest/examples/rooney_biegler/bootstrap_example.py +++ b/pyomo/contrib/parmest/examples/rooney_biegler/bootstrap_example.py @@ -35,7 +35,7 @@ def SSE(model): # Create an experiment list exp_list = [] for i in range(data.shape[0]): - exp_list.append(RooneyBieglerExperiment(data.loc[i, :].to_frame().transpose())) + exp_list.append(RooneyBieglerExperiment(data.loc[i, :])) # View one model # exp0_model = exp_list[0].get_labeled_model() diff --git a/pyomo/contrib/parmest/examples/rooney_biegler/likelihood_ratio_example.py b/pyomo/contrib/parmest/examples/rooney_biegler/likelihood_ratio_example.py index b08d5456982..7799148389a 100644 --- a/pyomo/contrib/parmest/examples/rooney_biegler/likelihood_ratio_example.py +++ b/pyomo/contrib/parmest/examples/rooney_biegler/likelihood_ratio_example.py @@ -36,7 +36,7 @@ def SSE(model): # Create an experiment list exp_list = [] for i in range(data.shape[0]): - exp_list.append(RooneyBieglerExperiment(data.loc[i, :].to_frame().transpose())) + exp_list.append(RooneyBieglerExperiment(data.loc[i, :])) # View one model # exp0_model = exp_list[0].get_labeled_model() diff --git a/pyomo/contrib/parmest/examples/rooney_biegler/parameter_estimation_example.py b/pyomo/contrib/parmest/examples/rooney_biegler/parameter_estimation_example.py index 9c851ecd9c8..aa810453883 100644 --- a/pyomo/contrib/parmest/examples/rooney_biegler/parameter_estimation_example.py +++ b/pyomo/contrib/parmest/examples/rooney_biegler/parameter_estimation_example.py @@ -35,7 +35,7 @@ def SSE(model): # Create an experiment list exp_list = [] for i in range(data.shape[0]): - exp_list.append(RooneyBieglerExperiment(data.loc[i, :].to_frame().transpose())) + exp_list.append(RooneyBieglerExperiment(data.loc[i, :])) # View one model # exp0_model = exp_list[0].get_labeled_model() diff --git a/pyomo/contrib/parmest/examples/rooney_biegler/rooney_biegler.py b/pyomo/contrib/parmest/examples/rooney_biegler/rooney_biegler.py index 8fa0fd70ec6..920bd2987e0 100644 --- a/pyomo/contrib/parmest/examples/rooney_biegler/rooney_biegler.py +++ b/pyomo/contrib/parmest/examples/rooney_biegler/rooney_biegler.py @@ -52,15 +52,17 @@ def __init__(self, data): self.model = None def create_model(self): - self.model = rooney_biegler_model(self.data) + # rooney_biegler_model expects a dataframe + data_df = self.data.to_frame().transpose() + self.model = rooney_biegler_model(data_df) def label_model(self): m = self.model m.experiment_outputs = pyo.Suffix(direction=pyo.Suffix.LOCAL) - m.experiment_outputs.update([(m.hour, self.data.iloc[0]['hour'])]) - m.experiment_outputs.update([(m.y, self.data.iloc[0]['y'])]) + m.experiment_outputs.update([(m.hour, self.data['hour'])]) + m.experiment_outputs.update([(m.y, self.data['y'])]) m.unknown_parameters = pyo.Suffix(direction=pyo.Suffix.LOCAL) m.unknown_parameters.update( @@ -72,8 +74,8 @@ def finalize_model(self): m = self.model # Experiment output values - m.hour = self.data.iloc[0]['hour'] - m.y = self.data.iloc[0]['y'] + m.hour = self.data['hour'] + m.y = self.data['y'] def get_labeled_model(self): self.create_model() diff --git a/pyomo/contrib/parmest/examples/rooney_biegler/rooney_biegler_with_constraint.py b/pyomo/contrib/parmest/examples/rooney_biegler/rooney_biegler_with_constraint.py index 463c876dc43..499f6eb505b 100644 --- a/pyomo/contrib/parmest/examples/rooney_biegler/rooney_biegler_with_constraint.py +++ b/pyomo/contrib/parmest/examples/rooney_biegler/rooney_biegler_with_constraint.py @@ -56,15 +56,17 @@ def __init__(self, data): self.model = None def create_model(self): - self.model = rooney_biegler_model_with_constraint(self.data) + # rooney_biegler_model_with_constraint expects a dataframe + data_df = self.data.to_frame().transpose() + self.model = rooney_biegler_model_with_constraint(data_df) def label_model(self): m = self.model m.experiment_outputs = pyo.Suffix(direction=pyo.Suffix.LOCAL) - m.experiment_outputs.update([(m.hour, self.data.iloc[0]['hour'])]) - m.experiment_outputs.update([(m.y, self.data.iloc[0]['y'])]) + m.experiment_outputs.update([(m.hour, self.data['hour'])]) + m.experiment_outputs.update([(m.y, self.data['y'])]) m.unknown_parameters = pyo.Suffix(direction=pyo.Suffix.LOCAL) m.unknown_parameters.update( @@ -76,8 +78,8 @@ def finalize_model(self): m = self.model # Experiment output values - m.hour = self.data.iloc[0]['hour'] - m.y = self.data.iloc[0]['y'] + m.hour = self.data['hour'] + m.y = self.data['y'] def get_labeled_model(self): self.create_model() diff --git a/pyomo/contrib/parmest/tests/test_parmest.py b/pyomo/contrib/parmest/tests/test_parmest.py index ff8d1663bc9..bbffa982bcf 100644 --- a/pyomo/contrib/parmest/tests/test_parmest.py +++ b/pyomo/contrib/parmest/tests/test_parmest.py @@ -77,7 +77,7 @@ def SSE(model): exp_list = [] for i in range(data.shape[0]): exp_list.append( - RooneyBieglerExperiment(data.loc[i, :].to_frame().transpose()) + RooneyBieglerExperiment(data.loc[i, :]) ) # Create an instance of the parmest estimator @@ -386,13 +386,14 @@ def response_rule(m, h): class RooneyBieglerExperimentParams(RooneyBieglerExperiment): def create_model(self): - self.model = rooney_biegler_params(self.data) + data_df = self.data.to_frame().transpose() + self.model = rooney_biegler_params(data_df) rooney_biegler_params_exp_list = [] for i in range(self.data.shape[0]): rooney_biegler_params_exp_list.append( RooneyBieglerExperimentParams( - self.data.loc[i, :].to_frame().transpose() + self.data.loc[i, :] ) ) @@ -422,15 +423,16 @@ def response_rule(m, h): class RooneyBieglerExperimentIndexedParams(RooneyBieglerExperiment): def create_model(self): - self.model = rooney_biegler_indexed_params(self.data) + data_df = self.data.to_frame().transpose() + self.model = rooney_biegler_indexed_params(data_df) def label_model(self): m = self.model m.experiment_outputs = pyo.Suffix(direction=pyo.Suffix.LOCAL) - m.experiment_outputs.update([(m.hour, self.data.iloc[0]['hour'])]) - m.experiment_outputs.update([(m.y, self.data.iloc[0]['y'])]) + m.experiment_outputs.update([(m.hour, self.data['hour'])]) + m.experiment_outputs.update([(m.y, self.data['y'])]) m.unknown_parameters = pyo.Suffix(direction=pyo.Suffix.LOCAL) m.unknown_parameters.update((k, pyo.ComponentUID(k)) for k in [m.theta]) @@ -439,7 +441,7 @@ def label_model(self): for i in range(self.data.shape[0]): rooney_biegler_indexed_params_exp_list.append( RooneyBieglerExperimentIndexedParams( - self.data.loc[i, :].to_frame().transpose() + self.data.loc[i, :] ) ) @@ -465,12 +467,13 @@ def response_rule(m, h): class RooneyBieglerExperimentVars(RooneyBieglerExperiment): def create_model(self): - self.model = rooney_biegler_vars(self.data) + data_df = self.data.to_frame().transpose() + self.model = rooney_biegler_vars(data_df) rooney_biegler_vars_exp_list = [] for i in range(self.data.shape[0]): rooney_biegler_vars_exp_list.append( - RooneyBieglerExperimentVars(self.data.loc[i, :].to_frame().transpose()) + RooneyBieglerExperimentVars(self.data.loc[i, :]) ) def rooney_biegler_indexed_vars(data): @@ -501,15 +504,16 @@ def response_rule(m, h): class RooneyBieglerExperimentIndexedVars(RooneyBieglerExperiment): def create_model(self): - self.model = rooney_biegler_indexed_vars(self.data) + data_df = self.data.to_frame().transpose() + self.model = rooney_biegler_indexed_vars(data_df) def label_model(self): m = self.model m.experiment_outputs = pyo.Suffix(direction=pyo.Suffix.LOCAL) - m.experiment_outputs.update([(m.hour, self.data.iloc[0]['hour'])]) - m.experiment_outputs.update([(m.y, self.data.iloc[0]['y'])]) + m.experiment_outputs.update([(m.hour, self.data['hour'])]) + m.experiment_outputs.update([(m.y, self.data['y'])]) m.unknown_parameters = pyo.Suffix(direction=pyo.Suffix.LOCAL) m.unknown_parameters.update((k, pyo.ComponentUID(k)) for k in [m.theta]) @@ -518,7 +522,7 @@ def label_model(self): for i in range(self.data.shape[0]): rooney_biegler_indexed_vars_exp_list.append( RooneyBieglerExperimentIndexedVars( - self.data.loc[i, :].to_frame().transpose() + self.data.loc[i, :] ) ) @@ -985,7 +989,7 @@ def SSE(model): exp_list = [] for i in range(data.shape[0]): exp_list.append( - RooneyBieglerExperiment(data.loc[i, :].to_frame().transpose()) + RooneyBieglerExperiment(data.loc[i, :]) ) solver_options = {"tol": 1e-8}