ageraustine commited on
Commit
ab27507
·
verified ·
1 Parent(s): d296c8e

Upload folder using huggingface_hub

Browse files
src/__pycache__/gradio_app.cpython-311.pyc ADDED
Binary file (28.8 kB). View file
 
src/gradio_app.py CHANGED
@@ -312,6 +312,7 @@ with gr.Blocks(title="River Network Explorer") as demo:
312
  def update_view(view_val, basin_id):
313
  basin_name = BASIN_NAMES[basin_id]
314
  color = _PALETTE[basin_id % len(_PALETTE)]
 
315
 
316
  try:
317
  nodes, edges = get_graph(DATA_ROOT)
@@ -319,7 +320,16 @@ with gr.Blocks(title="River Network Explorer") as demo:
319
  return {
320
  explore_group: gr.update(visible=True),
321
  validation_group: gr.update(visible=False),
322
- status_md: gr.update(value="❌ Could not find station_elevations.csv under data root.")
 
 
 
 
 
 
 
 
 
323
  }
324
 
325
  if view_val == "Network validation":
@@ -328,7 +338,8 @@ with gr.Blocks(title="River Network Explorer") as demo:
328
  return {
329
  explore_group: gr.update(visible=False),
330
  validation_group: gr.update(visible=True),
331
- val_caption: gr.update(value=f"No reach graph found for {basin_name}. Run `python -m scripts.build_reach_graphs` first.")
 
332
  }
333
  reach_nodes, reach_edges = reach
334
  n_confluences = int(reach_nodes["is_confluence"].sum()) if "is_confluence" in reach_nodes.columns else 0
@@ -343,8 +354,8 @@ with gr.Blocks(title="River Network Explorer") as demo:
343
  m_confl: n_confluences,
344
  m_gauges: n_gauged,
345
  val_plot_output: fig,
346
- val_caption: gr.update(value="Confirm visually: confluences (◆) should sit where a tributary joins."),
347
- val_snap_caption: gr.update(value="")
348
  }
349
  else:
350
  centerlines = get_centerlines(DATA_ROOT, nodes)
@@ -363,48 +374,48 @@ with gr.Blocks(title="River Network Explorer") as demo:
363
  return {
364
  explore_group: gr.update(visible=True),
365
  validation_group: gr.update(visible=False),
366
- status_md: gr.update(value=status_text),
367
  plot_output: fig,
368
  slider: 50
369
  }
370
 
371
- view_radio.change(update_view, inputs=[view_radio, basin_radio], outputs=[explore_group, validation_group, status_md, plot_output, slider])
372
- basin_radio.change(update_view, inputs=[view_radio, basin_radio], outputs=[explore_group, validation_group, status_md, plot_output, slider])
373
-
374
  def compute_position(basin_id, percent):
375
  fraction = percent / 100.0
376
  basin_name = BASIN_NAMES[basin_id]
377
  color = _PALETTE[basin_id % len(_PALETTE)]
 
378
 
379
  try:
380
  nodes, edges = get_graph(DATA_ROOT)
381
  except Exception:
382
- return [None] * 8
 
 
 
 
383
 
384
  centerlines = get_centerlines(DATA_ROOT, nodes)
385
  gw_df = get_groundwater(DATA_ROOT)
386
  has_centerline = basin_id in centerlines
387
 
388
- fig2 = None
389
- elev, lat, lon, dist_label, dist_sub = None, 0, 0, "", ""
390
  nearest_station = ""
391
 
392
  if has_centerline:
393
  info = centerlines[basin_id]
394
  cp = interpolate_by_fraction(info["centerline"], fraction)
395
- elev = elevation_at_km(info["gauges"], cp.distance_from_mouth_km)
396
  lat, lon = cp.latitude, cp.longitude
397
- dist_label = f"{cp.distance_from_mouth_km:.2f} km"
398
- dist_sub = f"from mouth, of {cp.total_length_km:.0f} km total"
399
  nearest_station = info["gauges"].iloc[(info["gauges"]["centerline_km"] - cp.distance_from_mouth_km).abs().idxmin()]["station_code"]
400
  fig2 = build_figure_real(info, basin_name, color, wells_df=gw_df, marker_lon=lon, marker_lat=lat)
401
  else:
402
  targets = schematic_click_targets(nodes, edges, basin_id)
403
  point = interpolate_along_chain(nodes, edges, basin_id, fraction)
404
- elev = point.elevation_m
405
  lat, lon = point.latitude, point.longitude
406
- dist_label = f"{point.distance_from_upstream_km:.2f} km"
407
- dist_sub = f"of {point.edge_length_km:.2f} km segment"
408
  nearest_station = point.upstream_station
409
  x_pos = fraction * (len(targets) - 1)
410
  fig2 = build_figure_schematic(targets, basin_name, color, marker_x=x_pos, marker_y=elev)
@@ -412,14 +423,14 @@ with gr.Blocks(title="River Network Explorer") as demo:
412
  gwl, nearest_well_km = None, None
413
  if gw_df is not None:
414
  pseudo_point = RiverPoint(basin_id=basin_id, upstream_station="", downstream_station="",
415
- fraction=0, latitude=lat, longitude=lon, elevation_m=elev or 0,
416
  distance_from_upstream_km=0, edge_length_km=0)
417
  gwl, nearest_well_km = groundwater_at_point(pseudo_point, gw_df)
418
 
419
  gw_text = f"{gwl:.1f} m" if gwl is not None else (f"No well (nearest {nearest_well_km:.0f}km away)" if nearest_well_km else "No data")
420
 
421
  hydro = get_hydrometric(DATA_ROOT)
422
- fig_wl, fig_disc, fig_rc = None, None, None
423
  if hydro is not None:
424
  loader, hydro_df = hydro
425
  station_df = hydro_df[hydro_df["station_code"] == nearest_station]
@@ -437,14 +448,31 @@ with gr.Blocks(title="River Network Explorer") as demo:
437
  except Exception:
438
  pass
439
 
440
- return [
441
- fig2, elev, dist_label, gw_text, f"Coordinates: {lat:.4f}, {lon:.4f}",
442
- fig_wl, fig_disc, fig_rc
443
- ]
444
-
445
- slider.change(compute_position, inputs=[basin_radio, slider], outputs=[plot_output, metric_elev, metric_dist, metric_gw, coords_md, plot_waterlevel, plot_discharge, plot_rating])
446
-
447
- demo.load(update_view, inputs=[view_radio, basin_radio], outputs=[explore_group, validation_group, status_md, plot_output, slider])
448
-
449
- # if __name__ == "__main__":
450
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  def update_view(view_val, basin_id):
313
  basin_name = BASIN_NAMES[basin_id]
314
  color = _PALETTE[basin_id % len(_PALETTE)]
315
+ default_fig = go.Figure()
316
 
317
  try:
318
  nodes, edges = get_graph(DATA_ROOT)
 
320
  return {
321
  explore_group: gr.update(visible=True),
322
  validation_group: gr.update(visible=False),
323
+ status_md: "❌ Could not find station_elevations.csv under data root.",
324
+ plot_output: default_fig,
325
+ slider: 50,
326
+ metric_elev: 0.0,
327
+ metric_dist: "",
328
+ metric_gw: "No data",
329
+ coords_md: "Coordinates: —",
330
+ plot_waterlevel: default_fig,
331
+ plot_discharge: default_fig,
332
+ plot_rating: default_fig
333
  }
334
 
335
  if view_val == "Network validation":
 
338
  return {
339
  explore_group: gr.update(visible=False),
340
  validation_group: gr.update(visible=True),
341
+ val_caption: f"No reach graph found for {basin_name}. Run `python -m scripts.build_reach_graphs` first.",
342
+ val_plot_output: default_fig
343
  }
344
  reach_nodes, reach_edges = reach
345
  n_confluences = int(reach_nodes["is_confluence"].sum()) if "is_confluence" in reach_nodes.columns else 0
 
354
  m_confl: n_confluences,
355
  m_gauges: n_gauged,
356
  val_plot_output: fig,
357
+ val_caption: "Confirm visually: confluences (◆) should sit where a tributary joins.",
358
+ val_snap_caption: ""
359
  }
360
  else:
361
  centerlines = get_centerlines(DATA_ROOT, nodes)
 
374
  return {
375
  explore_group: gr.update(visible=True),
376
  validation_group: gr.update(visible=False),
377
+ status_md: status_text,
378
  plot_output: fig,
379
  slider: 50
380
  }
381
 
 
 
 
382
  def compute_position(basin_id, percent):
383
  fraction = percent / 100.0
384
  basin_name = BASIN_NAMES[basin_id]
385
  color = _PALETTE[basin_id % len(_PALETTE)]
386
+ default_fig = go.Figure()
387
 
388
  try:
389
  nodes, edges = get_graph(DATA_ROOT)
390
  except Exception:
391
+ return {
392
+ plot_output: default_fig, metric_elev: 0.0, metric_dist: "",
393
+ metric_gw: "No data", coords_md: "Coordinates: —",
394
+ plot_waterlevel: default_fig, plot_discharge: default_fig, plot_rating: default_fig
395
+ }
396
 
397
  centerlines = get_centerlines(DATA_ROOT, nodes)
398
  gw_df = get_groundwater(DATA_ROOT)
399
  has_centerline = basin_id in centerlines
400
 
401
+ fig2 = default_fig
402
+ elev, lat, lon, dist_label = 0.0, 0.0, 0.0, ""
403
  nearest_station = ""
404
 
405
  if has_centerline:
406
  info = centerlines[basin_id]
407
  cp = interpolate_by_fraction(info["centerline"], fraction)
408
+ elev = float(elevation_at_km(info["gauges"], cp.distance_from_mouth_km))
409
  lat, lon = cp.latitude, cp.longitude
410
+ dist_label = f"{cp.distance_from_mouth_km:.2f} km from mouth"
 
411
  nearest_station = info["gauges"].iloc[(info["gauges"]["centerline_km"] - cp.distance_from_mouth_km).abs().idxmin()]["station_code"]
412
  fig2 = build_figure_real(info, basin_name, color, wells_df=gw_df, marker_lon=lon, marker_lat=lat)
413
  else:
414
  targets = schematic_click_targets(nodes, edges, basin_id)
415
  point = interpolate_along_chain(nodes, edges, basin_id, fraction)
416
+ elev = float(point.elevation_m)
417
  lat, lon = point.latitude, point.longitude
418
+ dist_label = f"{point.distance_from_upstream_km:.2f} km from upstream"
 
419
  nearest_station = point.upstream_station
420
  x_pos = fraction * (len(targets) - 1)
421
  fig2 = build_figure_schematic(targets, basin_name, color, marker_x=x_pos, marker_y=elev)
 
423
  gwl, nearest_well_km = None, None
424
  if gw_df is not None:
425
  pseudo_point = RiverPoint(basin_id=basin_id, upstream_station="", downstream_station="",
426
+ fraction=0, latitude=lat, longitude=lon, elevation_m=elev,
427
  distance_from_upstream_km=0, edge_length_km=0)
428
  gwl, nearest_well_km = groundwater_at_point(pseudo_point, gw_df)
429
 
430
  gw_text = f"{gwl:.1f} m" if gwl is not None else (f"No well (nearest {nearest_well_km:.0f}km away)" if nearest_well_km else "No data")
431
 
432
  hydro = get_hydrometric(DATA_ROOT)
433
+ fig_wl, fig_disc, fig_rc = default_fig, default_fig, default_fig
434
  if hydro is not None:
435
  loader, hydro_df = hydro
436
  station_df = hydro_df[hydro_df["station_code"] == nearest_station]
 
448
  except Exception:
449
  pass
450
 
451
+ return {
452
+ plot_output: fig2,
453
+ metric_elev: elev,
454
+ metric_dist: dist_label,
455
+ metric_gw: gw_text,
456
+ coords_md: f"Coordinates: {lat:.4f}, {lon:.4f}",
457
+ plot_waterlevel: fig_wl,
458
+ plot_discharge: fig_disc,
459
+ plot_rating: fig_rc
460
+ }
461
+
462
+ all_outputs = [
463
+ explore_group, validation_group, status_md, plot_output, slider,
464
+ metric_elev, metric_dist, metric_gw, coords_md,
465
+ plot_waterlevel, plot_discharge, plot_rating,
466
+ m_nodes, m_edges, m_confl, m_gauges, val_caption, val_plot_output, val_snap_caption
467
+ ]
468
+
469
+ view_radio.change(update_view, inputs=[view_radio, basin_radio], outputs=all_outputs)
470
+ basin_radio.change(update_view, inputs=[view_radio, basin_radio], outputs=all_outputs)
471
+
472
+ slider_outputs = [plot_output, metric_elev, metric_dist, metric_gw, coords_md, plot_waterlevel, plot_discharge, plot_rating]
473
+ slider.change(compute_position, inputs=[basin_radio, slider], outputs=slider_outputs)
474
+
475
+ demo.load(update_view, inputs=[view_radio, basin_radio], outputs=all_outputs)
476
+
477
+ if __name__ == "__main__":
478
+ demo.launch()