diff --git a/dashboard/app.py b/dashboard/app.py index a742f68..78376ef 100644 --- a/dashboard/app.py +++ b/dashboard/app.py @@ -143,6 +143,7 @@ def update_on_change_model(**kwargs): "parameters_max", "parameters_show_all", "simulation_calibration", + "use_inferred_calibration", ) def update_on_change_others(**kwargs): # skip if triggered on server ready (all state variables marked as modified) diff --git a/dashboard/calibration_manager.py b/dashboard/calibration_manager.py index b25e6c5..7401639 100644 --- a/dashboard/calibration_manager.py +++ b/dashboard/calibration_manager.py @@ -1,5 +1,6 @@ from trame.widgets import client, vuetify3 as vuetify, html from state_manager import state +from error_manager import add_error import copy @@ -10,18 +11,43 @@ def __init__(self, simulation_calibration): def convert_sim_to_exp(self, df_sim): """ Apply calibration to the simulation points, so as to reconstruct - the same input/output variables as the experimental points + the same input/output variables as the experimental points. """ + + def convert(value, alpha, beta): + return value / alpha + beta + for value in state.simulation_calibration.values(): sim_name = value["name"] exp_name = value["depends_on"] - df_sim[exp_name] = df_sim[sim_name] / value["alpha"] + value["beta"] + df_sim[exp_name] = convert( + df_sim[sim_name], value["alpha_guess"], value["beta_guess"] + ) + if state.use_inferred_calibration: + if all( + inferred_key in value.values() + for inferred_key in ["alpha_inferred", "beta_inferred"] + ): + df_sim[exp_name] = convert( + df_sim[sim_name], + value["alpha_inferred"], + value["beta_inferred"], + ) + else: + title = "Inferrred calibration does not exist" + msg = "Attempted to use the inferred calibration values to apply to the simulation points but calibration hasn't been inferred yet. Applying the guess calibration instead." + add_error(title, msg) + print(msg) def convert_exp_to_sim(self, exp_dict): """ Apply calibration to the experimental points, to be passed as - parameters for simulations on NERSC + parameters for simulations on NERSC. """ + + def convert(value, alpha, beta): + return alpha * (value - beta) + sim_dict = {} for _, value in state.simulation_calibration.items(): sim_name = value["name"] @@ -38,8 +64,25 @@ def convert_exp_to_sim(self, exp_dict): ) # fill the dictionary if exp_name in exp_dict: - sim_val = (exp_dict[exp_name] - value["beta"]) * value["alpha"] - sim_dict[sim_name] = sim_val + sim_dict[sim_name] = convert( + exp_dict[exp_name], value["alpha_guess"], value["beta_guess"] + ) + if state.use_inferred_calibration: + if all( + inferred_key in value.values() + for inferred_key in ["alpha_inferred", "beta_inferred"] + ): + sim_dict[sim_name] = convert( + exp_dict[exp_name], + value["alpha_inferred"], + value["beta_inferred"], + ) + else: + title = "Inferrred calibration does not exist" + msg = "Attempted to use the inferred calibration values to apply to the experimental points but the calibration hasn't been inferred yet. Applying the guess calibration instead." + add_error(title, msg) + print(msg) + return sim_dict def panel(self): @@ -49,39 +92,111 @@ def panel(self): title="Control: Calibrate simulation points", style="font-size: 20px; font-weight: 500;", ): - with vuetify.VExpansionPanelText(): + with vuetify.VExpansionPanelText( + style="font-weight: lighter; font-size: 16px;" + ): + with vuetify.VRow(): + vuetify.VCheckbox( + v_model="use_inferred_calibration", + density="compact", + label="Use inferred calibration", + ) with client.DeepReactive("simulation_calibration"): for key in state.simulation_calibration.keys(): - # create a row for the parameter label + # create a row for the calibration formula with vuetify.VRow(): html.Small( - f" {state.simulation_calibration[key]['name']} = α × ({state.simulation_calibration[key]['depends_on']} - β)", - style="font-weight: lighter; font-size: 70%;", + f"{state.simulation_calibration[key]['name']} = α × ({state.simulation_calibration[key]['depends_on']} - β)", ) - with vuetify.VRow(): - with vuetify.VCol(): - vuetify.VTextField( - v_model_number=( - f"simulation_calibration['{key}']['alpha']", - ), - change="flushState('simulation_calibration')", - density="compact", - hide_details=True, - hide_spin_buttons=True, - style="width: 100px;", - type="number", - label="α", - ) - with vuetify.VCol(): - vuetify.VTextField( - v_model_number=( - f"simulation_calibration['{key}']['beta']", - ), - change="flushState('simulation_calibration')", - density="compact", - hide_details=True, - hide_spin_buttons=True, - style="width: 100px;", - type="number", - label="β", - ) + # create a row for alpha values + with vuetify.VRow( + style="display: flex; align-items: center; margin: 20px; justify-content: space-between;" + ): + with vuetify.VCard(subtitle="α guess"): + with vuetify.VCardText(): + with vuetify.VRow(style="align-items: center"): + vuetify.VTextField( + v_model_number=( + f"simulation_calibration['{key}']['alpha_guess']", + ), + change="flushState('simulation_calibration')", + density="compact", + hide_details=True, + hide_spin_buttons=True, + style="width: 100px;", + type="number", + ) + html.Small( + "±", + style="margin-left: 5px; margin-right: 5px;", + ) + vuetify.VTextField( + v_model_number=( + f"simulation_calibration['{key}']['alpha_uncertainty']", + ), + density="compact", + hide_details=True, + hide_spin_buttons=True, + style="width: 100px;", + type="number", + ) + with vuetify.VCard(subtitle="α inferred"): + with vuetify.VCardText(): + with vuetify.VRow(): + vuetify.VTextField( + v_model_number=( + f"simulation_calibration['{key}']['alpha_inferred']", + ), + density="compact", + hide_details=True, + hide_spin_buttons=True, + style="width: 100px;", + type="number", + disabled=True, + ) + # create a row for beta values + with vuetify.VRow( + style="display: flex; align-items: center; margin: 20px; justify-content: space-between;" + ): + with vuetify.VCard(subtitle="β guess"): + with vuetify.VCardText(): + with vuetify.VRow(style="align-items: center"): + vuetify.VTextField( + v_model_number=( + f"simulation_calibration['{key}']['beta_guess']", + ), + change="flushState('simulation_calibration')", + density="compact", + hide_details=True, + hide_spin_buttons=True, + style="width: 100px;", + type="number", + ) + html.Small( + "±", + style="margin-left: 5px; margin-right: 5px;", + ) + vuetify.VTextField( + v_model_number=( + f"simulation_calibration['{key}']['beta_uncertainty']", + ), + density="compact", + hide_details=True, + hide_spin_buttons=True, + style="width: 100px;", + type="number", + ) + with vuetify.VCard(subtitle="β inferred"): + with vuetify.VCardText(): + with vuetify.VRow(): + vuetify.VTextField( + v_model_number=( + f"simulation_calibration['{key}']['beta_inferred']", + ), + density="compact", + hide_details=True, + hide_spin_buttons=True, + style="width: 100px;", + type="number", + disabled=True, + ) diff --git a/dashboard/state_manager.py b/dashboard/state_manager.py index b2e7c2e..1ece270 100644 --- a/dashboard/state_manager.py +++ b/dashboard/state_manager.py @@ -52,3 +52,5 @@ def initialize_state(): # Errors management state.errors = [] state.error_counter = 0 + # Calibration toggles + state.use_inferred_calibration = False diff --git a/ml/train_model.py b/ml/train_model.py index 65795fc..705bebe 100644 --- a/ml/train_model.py +++ b/ml/train_model.py @@ -117,7 +117,7 @@ for _, value in simulation_calibration.items(): sim_name = value["name"] exp_name = value["depends_on"] - df_sim[exp_name] = df_sim[sim_name] / value["alpha"] + value["beta"] + df_sim[exp_name] = df_sim[sim_name] / value["alpha_guess"] + value["beta_guess"] # Concatenate experimental and simulation data for training and validation variables = input_names + output_names + ["experiment_flag"]