From 77550b3a02aeb27aeb4869b9fc1d807c9a00ed73 Mon Sep 17 00:00:00 2001 From: Christopher Teubert Date: Tue, 5 Nov 2024 09:00:15 -0800 Subject: [PATCH 1/3] Fix 0 case --- src/progpy/utils/calc_error.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/progpy/utils/calc_error.py b/src/progpy/utils/calc_error.py index 4957bc7..a4fb0ea 100644 --- a/src/progpy/utils/calc_error.py +++ b/src/progpy/utils/calc_error.py @@ -190,6 +190,8 @@ def MSE(m, times: List[float], inputs: List[dict], outputs: List[dict], **kwargs warn(f"Model unstable- NAN reached in simulation (t={t}) before cutoff threshold. " f"Cutoff threshold is {cutoffThreshold}, or roughly {stability_tol * 100}% of the data. Penalty added to score.") # Return value with Penalty added + if counter == 0: + return 100*short_sim_penalty return err_total/counter + (100-(t/cutoffThreshold)*100)*short_sim_penalty else: warn("Model unstable- NaN reached in simulation (t={})".format(t)) From 69b7c7a54350bdf56fe067b77dad654b8ddfc0c0 Mon Sep 17 00:00:00 2001 From: Christopher Teubert Date: Tue, 5 Nov 2024 09:50:33 -0800 Subject: [PATCH 2/3] Fixed tests --- src/progpy/utils/calc_error.py | 2 +- tests/test_calc_error.py | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/progpy/utils/calc_error.py b/src/progpy/utils/calc_error.py index a4fb0ea..c6f1c8c 100644 --- a/src/progpy/utils/calc_error.py +++ b/src/progpy/utils/calc_error.py @@ -142,7 +142,7 @@ def MSE(m, times: List[float], inputs: List[dict], outputs: List[dict], **kwargs If the model goes unstable before stability_tol is met and short_sim_penalty is None, then exception is raised Else if the model goes unstable before stability_tol is met and short_sim_penalty is not None- the penalty is added to the score Else, model goes unstable after stability_tol is met, the error calculated from data up to the instability is returned. - short_sim_penalty (float, optional): penalty added for simulation becoming unstable before stability_tol, added for each % below tol. Default is 100 + short_sim_penalty (float, optional): penalty added for simulation becoming unstable before stability_tol, added for each % below tol. If set to None, operation will return an error if simulation becomes unstable before stability_tol. Default is 100 Returns: float: Total error diff --git a/tests/test_calc_error.py b/tests/test_calc_error.py index e0be802..d41aa76 100644 --- a/tests/test_calc_error.py +++ b/tests/test_calc_error.py @@ -111,11 +111,14 @@ def future_loading(t, x=None): # With our current set parameters, our model goes unstable immediately with self.assertRaises(ValueError) as cm: - m.calc_error(simulated_results.times, simulated_results.inputs, simulated_results.outputs, dt=1) + m.calc_error(simulated_results.times, simulated_results.inputs, simulated_results.outputs, dt=1, short_sim_penalty=None) self.assertEqual( "Model unstable- NAN reached in simulation (t=0.0) before cutoff threshold. Cutoff threshold is 1900.0, or roughly 95.0% of the data", str(cm.exception) - ) + ) + + # Shouldn't raise error for default case (i.e., short_sim_penalty is not None) + m.calc_error(simulated_results.times, simulated_results.inputs, simulated_results.outputs, dt=1) # Creating duplicate model to check if consistent results occur m1 = BatteryElectroChemEOD() @@ -131,20 +134,26 @@ def future_loading(t, x=None): # Checks to see if model goes unstable before default stability tolerance is met. with self.assertRaises(ValueError) as cm: - m.calc_error(simulated_results.times, simulated_results.inputs, simulated_results.outputs, dt = 1) + m.calc_error(simulated_results.times, simulated_results.inputs, simulated_results.outputs, dt=1, short_sim_penalty=None) self.assertEqual( "Model unstable- NAN reached in simulation (t=1800.0) before cutoff threshold. Cutoff threshold is 1900.0, or roughly 95.0% of the data", str(cm.exception) ) + + # Shouldn't happen for default case (short_sim_penalty is not none) + m.calc_error(simulated_results.times, simulated_results.inputs, simulated_results.outputs, dt=1) # Checks to see if m1 throws the same exception. with self.assertRaises(ValueError): - m1.calc_error(m1_sim_results.times, m1_sim_results.inputs, m1_sim_results.outputs, dt = 1) + m1.calc_error(m1_sim_results.times, m1_sim_results.inputs, m1_sim_results.outputs, dt=1, short_sim_penalty=None) self.assertEqual( "Model unstable- NAN reached in simulation (t=1800.0) before cutoff threshold. Cutoff threshold is 1900.0, or roughly 95.0% of the data", str(cm.exception) ) + # Shouldn't for default case + m1.calc_error(m1_sim_results.times, m1_sim_results.inputs, m1_sim_results.outputs, dt=1) + # Checks to see if stability_tolerance throws Warning rather than an Error when the model goes unstable after threshold with self.assertWarns(UserWarning) as cm: m.calc_error(simulated_results.times, simulated_results.inputs, simulated_results.outputs, From ba06ba9f24a4ea8ac167e7a0e1f5913f18f15a74 Mon Sep 17 00:00:00 2001 From: Christopher Teubert Date: Tue, 5 Nov 2024 10:18:39 -0800 Subject: [PATCH 3/3] Fix mae issue from tensorflow error --- src/progpy/data_models/lstm_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/progpy/data_models/lstm_model.py b/src/progpy/data_models/lstm_model.py index a4d143f..bec1b86 100644 --- a/src/progpy/data_models/lstm_model.py +++ b/src/progpy/data_models/lstm_model.py @@ -585,7 +585,7 @@ def from_data(cls, inputs, outputs, event_states=None, t_met=None, **kwargs): output_data.append(t_all) model = keras.Model(inputs, outputs) - model.compile(optimizer="rmsprop", loss="mse", metrics=["mae"]*len(outputs)) + model.compile(optimizer="rmsprop", loss="mse", metrics=[["mae"]]*len(outputs)) # Train model history = model.fit(