malcolmSQ commited on
Commit ·
bd976f6
1
Parent(s): 9b6d653
Refactor dashboard to show only the Declining Increment model tab
Browse files- dashboard/app.py +6 -124
dashboard/app.py
CHANGED
|
@@ -272,137 +272,19 @@ def create_summary(results, species_results, config, model):
|
|
| 272 |
return summary
|
| 273 |
|
| 274 |
with gr.Blocks() as demo:
|
| 275 |
-
gr.Markdown("# Mangrove ER Model Dashboard\
|
| 276 |
with gr.Tabs():
|
| 277 |
-
# Original Model tab with planting schedule editing
|
| 278 |
-
with gr.Tab("Original Model"):
|
| 279 |
-
gr.Markdown("## Planting Schedule (ha per year)")
|
| 280 |
-
year_1 = gr.Number(value=2500, label="Year 1 Area (ha)")
|
| 281 |
-
year_2 = gr.Number(value=2500, label="Year 2 Area (ha)")
|
| 282 |
-
year_3 = gr.Number(value=0, label="Year 3 Area (ha)")
|
| 283 |
-
year_4 = gr.Number(value=0, label="Year 4 Area (ha)")
|
| 284 |
-
year_5 = gr.Number(value=0, label="Year 5 Area (ha)")
|
| 285 |
-
update_btn = gr.Button("Update Results", variant="primary")
|
| 286 |
-
with gr.Row():
|
| 287 |
-
carbon_plot = gr.Plot()
|
| 288 |
-
annual_plot = gr.Plot()
|
| 289 |
-
biomass_plot = gr.Plot()
|
| 290 |
-
# Now re-enable carbon_plot as well
|
| 291 |
-
# growth_plot = gr.Plot(label="Growth & Increment Curves")
|
| 292 |
-
summary_box = gr.Textbox(label="Summary", lines=12)
|
| 293 |
-
results_box = gr.Dataframe()
|
| 294 |
-
species_box = gr.Dataframe()
|
| 295 |
-
survival_box = gr.Dataframe(label="Surviving Trees Table")
|
| 296 |
-
def update_original_model(y1, y2, y3, y4, y5):
|
| 297 |
-
config = update_planting_schedule(MODEL_CONFIGS["Original Model"], [y1, y2, y3, y4, y5])
|
| 298 |
-
import tempfile
|
| 299 |
-
import yaml
|
| 300 |
-
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as tmp:
|
| 301 |
-
yaml.dump(config, tmp)
|
| 302 |
-
tmp_path = tmp.name
|
| 303 |
-
model = ERModel(Path(tmp_path))
|
| 304 |
-
results, species_results = model.run()
|
| 305 |
-
plots = create_all_plots(results, species_results, config)
|
| 306 |
-
summary = create_summary(results, species_results, config, model)
|
| 307 |
-
survival_table = create_survival_table(model)
|
| 308 |
-
results_fmt = results.copy()
|
| 309 |
-
species_results_fmt = species_results.copy()
|
| 310 |
-
for col in results_fmt.columns:
|
| 311 |
-
if results_fmt[col].dtype in [float, int]:
|
| 312 |
-
results_fmt[col] = results_fmt[col].apply(lambda x: f"{x:,.2f}" if isinstance(x, float) else f"{x:,}")
|
| 313 |
-
for col in species_results_fmt.columns:
|
| 314 |
-
if species_results_fmt[col].dtype in [float, int]:
|
| 315 |
-
species_results_fmt[col] = species_results_fmt[col].apply(lambda x: f"{x:,.2f}" if isinstance(x, float) else f"{x:,}")
|
| 316 |
-
# Return the carbon plot (plots[0]), biomass plot (plots[2]), and annual plot (plots[1])
|
| 317 |
-
return plots[0], plots[2], plots[1], summary, results_fmt.head(30), species_results_fmt.head(30), survival_table.head(30)
|
| 318 |
-
update_btn.click(
|
| 319 |
-
update_original_model,
|
| 320 |
-
inputs=[year_1, year_2, year_3, year_4, year_5],
|
| 321 |
-
outputs=[carbon_plot, biomass_plot, annual_plot, summary_box, results_box, species_box, survival_box]
|
| 322 |
-
)
|
| 323 |
-
# Show initial results
|
| 324 |
-
c, a, b, summary, r, s, surv = run_model(MODEL_CONFIGS["Original Model"])
|
| 325 |
-
# Assign the carbon plot (c), biomass plot (b), and annual plot (a)
|
| 326 |
-
carbon_plot.value = c
|
| 327 |
-
biomass_plot.value = b
|
| 328 |
-
annual_plot.value = a
|
| 329 |
-
summary_box.value = summary
|
| 330 |
-
results_box.value = r
|
| 331 |
-
species_box.value = s
|
| 332 |
-
survival_box.value = surv
|
| 333 |
-
# Simple Linear tab
|
| 334 |
-
with gr.Tab("Simple Linear"):
|
| 335 |
-
gr.Markdown("## Simple Linear Model Results (base config)")
|
| 336 |
-
carbon_plot, annual_plot, biomass_plot, summary, results, species, survival = run_model(MODEL_CONFIGS["Simple Linear"])
|
| 337 |
-
growth_fig = create_growth_increment_plots(yaml.safe_load(open(MODEL_CONFIGS["Simple Linear"])), model_type="linear")
|
| 338 |
-
with gr.Row():
|
| 339 |
-
carbon_plot_comp = gr.Plot(value=carbon_plot)
|
| 340 |
-
biomass_plot_comp = gr.Plot(value=biomass_plot)
|
| 341 |
-
annual_plot_comp = gr.Plot(value=annual_plot)
|
| 342 |
-
# gr.Plot(value=growth_fig, label="Growth & Increment Curves")
|
| 343 |
-
gr.Textbox(value=summary, label="Summary", lines=12)
|
| 344 |
-
gr.Markdown("### Results Table")
|
| 345 |
-
gr.Dataframe(value=results)
|
| 346 |
-
gr.Markdown("### Species Table")
|
| 347 |
-
gr.Dataframe(value=species)
|
| 348 |
-
gr.Markdown("### Surviving Trees Table")
|
| 349 |
-
gr.Dataframe(value=survival)
|
| 350 |
-
# Linear Plateau tab
|
| 351 |
-
with gr.Tab("Linear Plateau"):
|
| 352 |
-
gr.Markdown("## Linear Plateau Model Results (base config)")
|
| 353 |
-
carbon_plot, annual_plot, biomass_plot, summary, results, species, survival = run_model(MODEL_CONFIGS["Linear Plateau"])
|
| 354 |
-
growth_fig = create_growth_increment_plots(yaml.safe_load(open(MODEL_CONFIGS["Linear Plateau"])), model_type="linear_plateau")
|
| 355 |
-
with gr.Row():
|
| 356 |
-
carbon_plot_comp = gr.Plot(value=carbon_plot)
|
| 357 |
-
biomass_plot_comp = gr.Plot(value=biomass_plot)
|
| 358 |
-
annual_plot_comp = gr.Plot(value=annual_plot)
|
| 359 |
-
# gr.Plot(value=growth_fig, label="Growth & Increment Curves")
|
| 360 |
-
gr.Textbox(value=summary, label="Summary", lines=12)
|
| 361 |
-
gr.Markdown("### Results Table")
|
| 362 |
-
gr.Dataframe(value=results)
|
| 363 |
-
gr.Markdown("### Species Table")
|
| 364 |
-
gr.Dataframe(value=species)
|
| 365 |
-
gr.Markdown("### Surviving Trees Table")
|
| 366 |
-
gr.Dataframe(value=survival)
|
| 367 |
-
# Declining Increment tab
|
| 368 |
with gr.Tab("Declining Increment"):
|
| 369 |
gr.Markdown("## Declining Increment Model Results (base config)")
|
| 370 |
carbon_plot, annual_plot, biomass_plot, summary, results, species, survival = run_model(MODEL_CONFIGS["Declining Increment"])
|
| 371 |
growth_fig = create_growth_increment_plots(yaml.safe_load(open(MODEL_CONFIGS["Declining Increment"])), model_type="declining_increment")
|
| 372 |
-
# Diagnostics for DBH, height, and biomass arrays before plotting (biomass plot)
|
| 373 |
-
config = yaml.safe_load(open(MODEL_CONFIGS["Declining Increment"]))
|
| 374 |
-
years = results["year"]
|
| 375 |
-
from src.er_model import ERModel
|
| 376 |
-
for sp in config["species"]:
|
| 377 |
-
name = sp["name"]
|
| 378 |
-
initial_dbh = sp["initial_values"]["dbh"]
|
| 379 |
-
initial_height = sp["initial_values"]["height"]
|
| 380 |
-
dbh_params = sp["declining_increment"]["dbh"]
|
| 381 |
-
height_params = sp["declining_increment"]["height"]
|
| 382 |
-
dbh = np.array([ERModel.declining_increment_growth(float(t), dbh_params, initial_dbh) for t in years])
|
| 383 |
-
height = np.array([ERModel.declining_increment_growth(float(t), height_params, initial_height) for t in years])
|
| 384 |
-
if "Zanvo" in sp["allometry"]["equation"]:
|
| 385 |
-
if "1.938" in sp["allometry"]["equation"]:
|
| 386 |
-
biomass = 1.938 * (dbh ** 2 * height) ** 0.67628 / 1000
|
| 387 |
-
else:
|
| 388 |
-
biomass = 1.486 * (dbh ** 2 * height) ** 0.55864 / 1000
|
| 389 |
-
else:
|
| 390 |
-
biomass = dbh
|
| 391 |
-
print(f"[DEBUG] {name} DBH: min={dbh.min()}, max={dbh.max()}, any_negative={np.any(dbh < 0)}, any_complex={np.iscomplexobj(dbh)}")
|
| 392 |
-
print(f"[DEBUG] {name} Height: min={height.min()}, max={height.max()}, any_negative={np.any(height < 0)}, any_complex={np.iscomplexobj(height)}")
|
| 393 |
-
print(f"[DEBUG] {name} Biomass: min={biomass.min()}, max={biomass.max()}, any_negative={np.any(biomass < 0)}, any_complex={np.iscomplexobj(biomass)}")
|
| 394 |
with gr.Row():
|
| 395 |
carbon_plot_comp = gr.Plot(value=carbon_plot)
|
| 396 |
biomass_plot_comp = gr.Plot(value=biomass_plot)
|
| 397 |
annual_plot_comp = gr.Plot(value=annual_plot)
|
| 398 |
-
# gr.Plot(value=growth_fig, label="Growth & Increment Curves")
|
| 399 |
gr.Textbox(value=summary, label="Summary", lines=12)
|
| 400 |
-
gr.
|
| 401 |
-
gr.Dataframe(value=
|
| 402 |
-
gr.
|
| 403 |
-
gr.
|
| 404 |
-
|
| 405 |
-
gr.Dataframe(value=survival)
|
| 406 |
-
|
| 407 |
-
if __name__ == "__main__":
|
| 408 |
-
demo.launch()
|
|
|
|
| 272 |
return summary
|
| 273 |
|
| 274 |
with gr.Blocks() as demo:
|
| 275 |
+
gr.Markdown("# Mangrove ER Model Dashboard\nDeclining Increment Model Only")
|
| 276 |
with gr.Tabs():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
with gr.Tab("Declining Increment"):
|
| 278 |
gr.Markdown("## Declining Increment Model Results (base config)")
|
| 279 |
carbon_plot, annual_plot, biomass_plot, summary, results, species, survival = run_model(MODEL_CONFIGS["Declining Increment"])
|
| 280 |
growth_fig = create_growth_increment_plots(yaml.safe_load(open(MODEL_CONFIGS["Declining Increment"])), model_type="declining_increment")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
with gr.Row():
|
| 282 |
carbon_plot_comp = gr.Plot(value=carbon_plot)
|
| 283 |
biomass_plot_comp = gr.Plot(value=biomass_plot)
|
| 284 |
annual_plot_comp = gr.Plot(value=annual_plot)
|
|
|
|
| 285 |
gr.Textbox(value=summary, label="Summary", lines=12)
|
| 286 |
+
gr.Dataframe(value=results, label="Project Results (Annual)")
|
| 287 |
+
gr.Dataframe(value=species, label="Species Results (Annual)")
|
| 288 |
+
gr.Dataframe(value=survival, label="Surviving Trees Table")
|
| 289 |
+
# gr.Plot(value=growth_fig, label="Growth & Increment Curves")
|
| 290 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|