Rimjhim Mittal commited on
Commit ·
8e9aba9
1
Parent(s): c00b6ca
node with parameter list
Browse files
app.py
CHANGED
|
@@ -20,6 +20,7 @@ def reset_simulation_state():
|
|
| 20 |
def run_simulation(param_inputs, mdf_model):
|
| 21 |
mod_graph = mdf_model.graphs[0]
|
| 22 |
nodes = mod_graph.nodes
|
|
|
|
| 23 |
duration = param_inputs["Simulation Duration (s)"]
|
| 24 |
dt = param_inputs["Time Step (s)"]
|
| 25 |
|
|
@@ -126,29 +127,40 @@ def display_and_edit_array(array, key):
|
|
| 126 |
|
| 127 |
return np.array(edited_array)
|
| 128 |
|
| 129 |
-
def parameter_form_to_update_model_and_view(mdf_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
with st.form(key="parameter_form"):
|
| 131 |
valid_inputs = True
|
| 132 |
st.write("Model Parameters:")
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
if isinstance(param.value, str) or param.value is None:
|
| 140 |
continue
|
| 141 |
-
key = f"{param.id}_{i}"
|
| 142 |
|
| 143 |
# Alternate between columns
|
| 144 |
-
|
| 145 |
-
current_col = col1
|
| 146 |
-
elif i%4 == 1:
|
| 147 |
-
current_col = col2
|
| 148 |
-
elif i%4 == 2:
|
| 149 |
-
current_col = col3
|
| 150 |
-
else:
|
| 151 |
-
current_col = col4
|
| 152 |
|
| 153 |
with current_col:
|
| 154 |
if isinstance(param.value, (list, np.ndarray)):
|
|
@@ -166,6 +178,7 @@ def parameter_form_to_update_model_and_view(mdf_model, parameters, param_inputs,
|
|
| 166 |
valid_inputs = False
|
| 167 |
|
| 168 |
param_inputs[param.id] = value
|
|
|
|
| 169 |
st.write("Simulation Parameters:")
|
| 170 |
with st.container(border=True):
|
| 171 |
# Add Simulation Duration and Time Step inputs
|
|
@@ -180,24 +193,23 @@ def parameter_form_to_update_model_and_view(mdf_model, parameters, param_inputs,
|
|
| 180 |
except ValueError:
|
| 181 |
st.error("Invalid input for Simulation Duration. Please enter a valid number.")
|
| 182 |
valid_inputs = False
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
|
| 189 |
run_button = st.form_submit_button("Run Simulation")
|
| 190 |
|
| 191 |
if run_button:
|
| 192 |
if valid_inputs:
|
| 193 |
-
for
|
| 194 |
-
for param in
|
| 195 |
if param.id in param_inputs:
|
| 196 |
param.value = param_inputs[param.id]
|
| 197 |
st.session_state.simulation_results = run_simulation(param_inputs, mdf_model)
|
| 198 |
|
| 199 |
view_tabs(mdf_model, param_inputs)
|
| 200 |
-
|
| 201 |
def upload_file_and_load_to_model():
|
| 202 |
|
| 203 |
|
|
@@ -273,21 +285,8 @@ def main():
|
|
| 273 |
with header2:
|
| 274 |
with st.container():
|
| 275 |
st.title("MDF: "+ mdf_model.id)
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
parameters = []
|
| 279 |
-
for node in nodes:
|
| 280 |
-
parameters.append(node.parameters)
|
| 281 |
-
param_inputs = {}
|
| 282 |
-
if mdf_model.metadata:
|
| 283 |
-
preferred_duration = float(mdf_model.metadata.get("preferred_duration", 10))
|
| 284 |
-
preferred_dt = float(mdf_model.metadata.get("preferred_dt", 0.1))
|
| 285 |
-
else:
|
| 286 |
-
preferred_duration = 100
|
| 287 |
-
preferred_dt = 0.1
|
| 288 |
-
param_inputs["Simulation Duration (s)"] = preferred_duration
|
| 289 |
-
param_inputs["Time Step (s)"] = preferred_dt
|
| 290 |
-
parameter_form_to_update_model_and_view(mdf_model, parameters, param_inputs, mod_graph, nodes)
|
| 291 |
else:
|
| 292 |
header1, header2 = st.columns([1, 8], vertical_alignment="top")
|
| 293 |
with header1:
|
|
|
|
| 20 |
def run_simulation(param_inputs, mdf_model):
|
| 21 |
mod_graph = mdf_model.graphs[0]
|
| 22 |
nodes = mod_graph.nodes
|
| 23 |
+
|
| 24 |
duration = param_inputs["Simulation Duration (s)"]
|
| 25 |
dt = param_inputs["Time Step (s)"]
|
| 26 |
|
|
|
|
| 127 |
|
| 128 |
return np.array(edited_array)
|
| 129 |
|
| 130 |
+
def parameter_form_to_update_model_and_view(mdf_model):
|
| 131 |
+
mod_graph = mdf_model.graphs[0]
|
| 132 |
+
nodes = mod_graph.nodes
|
| 133 |
+
parameters = []
|
| 134 |
+
for node in nodes:
|
| 135 |
+
parameters.append(node.parameters)
|
| 136 |
+
param_inputs = {}
|
| 137 |
+
if mdf_model.metadata:
|
| 138 |
+
preferred_duration = float(mdf_model.metadata.get("preferred_duration", 10))
|
| 139 |
+
preferred_dt = float(mdf_model.metadata.get("preferred_dt", 0.1))
|
| 140 |
+
else:
|
| 141 |
+
preferred_duration = 100
|
| 142 |
+
preferred_dt = 0.1
|
| 143 |
+
param_inputs["Simulation Duration (s)"] = preferred_duration
|
| 144 |
+
param_inputs["Time Step (s)"] = preferred_dt
|
| 145 |
+
|
| 146 |
with st.form(key="parameter_form"):
|
| 147 |
valid_inputs = True
|
| 148 |
st.write("Model Parameters:")
|
| 149 |
+
|
| 150 |
+
for node_index, node in enumerate(nodes):
|
| 151 |
+
with st.container(border=True):
|
| 152 |
+
st.write(f"Node: {node.id}")
|
| 153 |
+
|
| 154 |
+
# Create four columns for each node
|
| 155 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 156 |
+
|
| 157 |
+
for i, param in enumerate(node.parameters):
|
| 158 |
if isinstance(param.value, str) or param.value is None:
|
| 159 |
continue
|
| 160 |
+
key = f"{param.id}_{node_index}_{i}"
|
| 161 |
|
| 162 |
# Alternate between columns
|
| 163 |
+
current_col = [col1, col2, col3, col4][i % 4]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
with current_col:
|
| 166 |
if isinstance(param.value, (list, np.ndarray)):
|
|
|
|
| 178 |
valid_inputs = False
|
| 179 |
|
| 180 |
param_inputs[param.id] = value
|
| 181 |
+
|
| 182 |
st.write("Simulation Parameters:")
|
| 183 |
with st.container(border=True):
|
| 184 |
# Add Simulation Duration and Time Step inputs
|
|
|
|
| 193 |
except ValueError:
|
| 194 |
st.error("Invalid input for Simulation Duration. Please enter a valid number.")
|
| 195 |
valid_inputs = False
|
| 196 |
+
try:
|
| 197 |
+
param_inputs["Time Step (s)"] = float(time_step)
|
| 198 |
+
except ValueError:
|
| 199 |
+
st.error("Invalid input for Time Step. Please enter a valid number.")
|
| 200 |
+
valid_inputs = False
|
| 201 |
|
| 202 |
run_button = st.form_submit_button("Run Simulation")
|
| 203 |
|
| 204 |
if run_button:
|
| 205 |
if valid_inputs:
|
| 206 |
+
for node in nodes:
|
| 207 |
+
for param in node.parameters:
|
| 208 |
if param.id in param_inputs:
|
| 209 |
param.value = param_inputs[param.id]
|
| 210 |
st.session_state.simulation_results = run_simulation(param_inputs, mdf_model)
|
| 211 |
|
| 212 |
view_tabs(mdf_model, param_inputs)
|
|
|
|
| 213 |
def upload_file_and_load_to_model():
|
| 214 |
|
| 215 |
|
|
|
|
| 285 |
with header2:
|
| 286 |
with st.container():
|
| 287 |
st.title("MDF: "+ mdf_model.id)
|
| 288 |
+
|
| 289 |
+
parameter_form_to_update_model_and_view(mdf_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
else:
|
| 291 |
header1, header2 = st.columns([1, 8], vertical_alignment="top")
|
| 292 |
with header1:
|