euler314 commited on
Commit
373b768
·
verified ·
1 Parent(s): 3c29d09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -58
app.py CHANGED
@@ -37,48 +37,30 @@ CACHE_EXPIRY_DAYS = 1
37
  # Color maps for Plotly (RGB)
38
  color_map = {
39
  'C5 Super Typhoon': 'rgb(255, 0, 0)',
40
- 'C4 Very Strong Typhoon': 'rgb(255, 63, 0)',
41
- 'C3 Strong Typhoon': 'rgb(255, 127, 0)',
42
- 'C2 Typhoon': 'rgb(255, 191, 0)',
43
- 'C1 Typhoon': 'rgb(255, 255, 0)',
44
- 'Tropical Storm': 'rgb(0, 255, 255)',
45
- 'Tropical Depression': 'rgb(173, 216, 230)'
46
  }
47
 
48
- # Classification standards with color names for Matplotlib
49
  atlantic_standard = {
50
- 'C5 Super Typhoon': {'wind_speed': 137, 'color': 'Red'},
51
- 'C4 Very Strong Typhoon': {'wind_speed': 113, 'color': 'Orange-Red'},
52
- 'C3 Strong Typhoon': {'wind_speed': 96, 'color': 'Orange'},
53
- 'C2 Typhoon': {'wind_speed': 83, 'color': 'Light Orange'},
54
- 'C1 Typhoon': {'wind_speed': 64, 'color': 'Yellow'},
55
- 'Tropical Storm': {'wind_speed': 34, 'color': 'Cyan'},
56
- 'Tropical Depression': {'wind_speed': 0, 'color': 'Light Blue'}
57
  }
58
 
59
  taiwan_standard = {
60
- 'Strong Typhoon': {'wind_speed': 51.0, 'color': 'Red'},
61
- 'Medium Typhoon': {'wind_speed': 33.7, 'color': 'Orange'},
62
- 'Mild Typhoon': {'wind_speed': 17.2, 'color': 'Yellow'},
63
- 'Tropical Depression': {'wind_speed': 0, 'color': 'Light Blue'}
64
- }
65
-
66
- # HEX codes for Matplotlib plotting (unchanged)
67
- atlantic_hex = {
68
- 'C5 Super Typhoon': '#FF0000',
69
- 'C4 Very Strong Typhoon': '#FF3F00',
70
- 'C3 Strong Typhoon': '#FF7F00',
71
- 'C2 Typhoon': '#FFBF00',
72
- 'C1 Typhoon': '#FFFF00',
73
- 'Tropical Storm': '#00FFFF',
74
- 'Tropical Depression': '#ADD8E6'
75
- }
76
-
77
- taiwan_hex = {
78
- 'Strong Typhoon': '#FF0000',
79
- 'Medium Typhoon': '#FF7F00',
80
- 'Mild Typhoon': '#FFFF00',
81
- 'Tropical Depression': '#ADD8E6'
82
  }
83
 
84
  # Data loading and preprocessing functions
@@ -320,31 +302,31 @@ def generate_main_analysis(start_year, start_month, end_year, end_month, enso_ph
320
 
321
  return tracks_fig, wind_scatter, pressure_scatter, regression_fig, slopes_text
322
 
323
- # Video animation function with adjusted size, color names, and sidebar
324
  def categorize_typhoon_by_standard(wind_speed, standard):
325
  if standard == 'taiwan':
326
  wind_speed_ms = wind_speed * 0.514444
327
  if wind_speed_ms >= 51.0:
328
- return 'Strong Typhoon', taiwan_hex['Strong Typhoon']
329
  elif wind_speed_ms >= 33.7:
330
- return 'Medium Typhoon', taiwan_hex['Medium Typhoon']
331
  elif wind_speed_ms >= 17.2:
332
- return 'Mild Typhoon', taiwan_hex['Mild Typhoon']
333
- return 'Tropical Depression', taiwan_hex['Tropical Depression']
334
  else:
335
  if wind_speed >= 137:
336
- return 'C5 Super Typhoon', atlantic_hex['C5 Super Typhoon']
337
  elif wind_speed >= 113:
338
- return 'C4 Very Strong Typhoon', atlantic_hex['C4 Very Strong Typhoon']
339
  elif wind_speed >= 96:
340
- return 'C3 Strong Typhoon', atlantic_hex['C3 Strong Typhoon']
341
  elif wind_speed >= 83:
342
- return 'C2 Typhoon', atlantic_hex['C2 Typhoon']
343
  elif wind_speed >= 64:
344
- return 'C1 Typhoon', atlantic_hex['C1 Typhoon']
345
  elif wind_speed >= 34:
346
- return 'Tropical Storm', atlantic_hex['Tropical Storm']
347
- return 'Tropical Depression', atlantic_hex['Tropical Depression']
348
 
349
  def generate_track_video(year, typhoon, standard):
350
  if not typhoon:
@@ -361,7 +343,7 @@ def generate_track_video(year, typhoon, standard):
361
 
362
  # Set up the figure with adjusted size to fit Gradio (900x700 pixels at 100 DPI)
363
  fig = plt.figure(figsize=(9, 7), dpi=100) # 9x7 inches = 900x700 pixels
364
- ax = plt.axes([0.05, 0.15, 0.60, 0.80], projection=ccrs.PlateCarree()) # Map on left 60%, adjusted for legend space
365
  ax.set_extent([min_lon - lon_padding, max_lon + lon_padding, min_lat - lat_padding, max_lat + lat_padding], crs=ccrs.PlateCarree())
366
 
367
  # Add world map features
@@ -379,15 +361,17 @@ def generate_track_video(year, typhoon, standard):
379
  date_text = ax.text(0.02, 0.02, '', transform=ax.transAxes, fontsize=10, bbox=dict(facecolor='white', alpha=0.8))
380
 
381
  # Add sidebar with typhoon details on the right
382
- details_title = fig.text(0.70, 0.90, "Typhoon Details", fontsize=12, fontweight='bold', verticalalignment='top', horizontalalignment='left')
383
- details_text = fig.text(0.70, 0.85, '', fontsize=10, verticalalignment='top', horizontalalignment='left',
384
  bbox=dict(facecolor='white', alpha=0.8, boxstyle='round,pad=0.5'))
385
 
386
- # Add color legend below the details
387
  standard_dict = atlantic_standard if standard == 'atlantic' else taiwan_standard
388
- color_legend_text = "\n".join([f"{cat}: {details['color']}" for cat, details in standard_dict.items()])
389
- color_legend = fig.text(0.70, 0.60, f"Color Legend\n{color_legend_text}", fontsize=10, verticalalignment='top', horizontalalignment='left',
390
- bbox=dict(facecolor='white', alpha=0.8, boxstyle='round,pad=0.5'))
 
 
391
 
392
  def init():
393
  line.set_data([], [])
@@ -468,7 +452,7 @@ with gr.Blocks(title="Typhoon Analysis Dashboard") as demo:
468
  ### Features:
469
  - **Track Visualization**: View typhoon tracks by time period and ENSO phase
470
  - **Statistical Analysis**: Examine relationships between ONI values and typhoon characteristics
471
- - **Path Animation**: Watch an animated typhoon path with video controls, world map, color legend, and details sidebar
472
  - **Regression Analysis**: Perform statistical regression on typhoon data
473
 
474
  Select a tab above to begin your analysis.
@@ -638,8 +622,8 @@ with gr.Blocks(title="Typhoon Analysis Dashboard") as demo:
638
  4. Use the video player's built-in controls to play, pause, or scrub through the animation
639
  5. The animation shows the typhoon track growing over a world map, with:
640
  - Date on the bottom left
641
- - Sidebar on the right showing typhoon details (name, date, wind speed, category)
642
- - Color legend below the sidebar using color names (e.g., "Red: C5 Super Typhoon")
643
  """)
644
 
645
  def update_typhoon_options(year):
 
37
  # Color maps for Plotly (RGB)
38
  color_map = {
39
  'C5 Super Typhoon': 'rgb(255, 0, 0)',
40
+ 'C4 Very Strong Typhoon': 'rgb(255, 165, 0)',
41
+ 'C3 Strong Typhoon': 'rgb(255, 255, 0)',
42
+ 'C2 Typhoon': 'rgb(0, 255, 0)',
43
+ 'C1 Typhoon': 'rgb(0, 255, 255)',
44
+ 'Tropical Storm': 'rgb(0, 0, 255)',
45
+ 'Tropical Depression': 'rgb(128, 128, 128)'
46
  }
47
 
48
+ # Classification standards with distinct colors for Matplotlib
49
  atlantic_standard = {
50
+ 'C5 Super Typhoon': {'wind_speed': 137, 'color': 'Red', 'hex': '#FF0000'}, # Pure Red
51
+ 'C4 Very Strong Typhoon': {'wind_speed': 113, 'color': 'Orange', 'hex': '#FFA500'}, # Bright Orange
52
+ 'C3 Strong Typhoon': {'wind_speed': 96, 'color': 'Yellow', 'hex': '#FFFF00'}, # Pure Yellow
53
+ 'C2 Typhoon': {'wind_speed': 83, 'color': 'Green', 'hex': '#00FF00'}, # Pure Green
54
+ 'C1 Typhoon': {'wind_speed': 64, 'color': 'Cyan', 'hex': '#00FFFF'}, # Pure Cyan
55
+ 'Tropical Storm': {'wind_speed': 34, 'color': 'Blue', 'hex': '#0000FF'}, # Pure Blue
56
+ 'Tropical Depression': {'wind_speed': 0, 'color': 'Gray', 'hex': '#808080'} # Medium Gray
57
  }
58
 
59
  taiwan_standard = {
60
+ 'Strong Typhoon': {'wind_speed': 51.0, 'color': 'Red', 'hex': '#FF0000'}, # Pure Red
61
+ 'Medium Typhoon': {'wind_speed': 33.7, 'color': 'Orange', 'hex': '#FFA500'}, # Bright Orange
62
+ 'Mild Typhoon': {'wind_speed': 17.2, 'color': 'Yellow', 'hex': '#FFFF00'}, # Pure Yellow
63
+ 'Tropical Depression': {'wind_speed': 0, 'color': 'Gray', 'hex': '#808080'} # Medium Gray
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  }
65
 
66
  # Data loading and preprocessing functions
 
302
 
303
  return tracks_fig, wind_scatter, pressure_scatter, regression_fig, slopes_text
304
 
305
+ # Video animation function with color examples, distinct colors, and dynamic sidebar
306
  def categorize_typhoon_by_standard(wind_speed, standard):
307
  if standard == 'taiwan':
308
  wind_speed_ms = wind_speed * 0.514444
309
  if wind_speed_ms >= 51.0:
310
+ return 'Strong Typhoon', taiwan_standard['Strong Typhoon']['hex']
311
  elif wind_speed_ms >= 33.7:
312
+ return 'Medium Typhoon', taiwan_standard['Medium Typhoon']['hex']
313
  elif wind_speed_ms >= 17.2:
314
+ return 'Mild Typhoon', taiwan_standard['Mild Typhoon']['hex']
315
+ return 'Tropical Depression', taiwan_standard['Tropical Depression']['hex']
316
  else:
317
  if wind_speed >= 137:
318
+ return 'C5 Super Typhoon', atlantic_standard['C5 Super Typhoon']['hex']
319
  elif wind_speed >= 113:
320
+ return 'C4 Very Strong Typhoon', atlantic_standard['C4 Very Strong Typhoon']['hex']
321
  elif wind_speed >= 96:
322
+ return 'C3 Strong Typhoon', atlantic_standard['C3 Strong Typhoon']['hex']
323
  elif wind_speed >= 83:
324
+ return 'C2 Typhoon', atlantic_standard['C2 Typhoon']['hex']
325
  elif wind_speed >= 64:
326
+ return 'C1 Typhoon', atlantic_standard['C1 Typhoon']['hex']
327
  elif wind_speed >= 34:
328
+ return 'Tropical Storm', atlantic_standard['Tropical Storm']['hex']
329
+ return 'Tropical Depression', atlantic_standard['Tropical Depression']['hex']
330
 
331
  def generate_track_video(year, typhoon, standard):
332
  if not typhoon:
 
343
 
344
  # Set up the figure with adjusted size to fit Gradio (900x700 pixels at 100 DPI)
345
  fig = plt.figure(figsize=(9, 7), dpi=100) # 9x7 inches = 900x700 pixels
346
+ ax = plt.axes([0.05, 0.15, 0.60, 0.80], projection=ccrs.PlateCarree()) # Map on left 60%
347
  ax.set_extent([min_lon - lon_padding, max_lon + lon_padding, min_lat - lat_padding, max_lat + lat_padding], crs=ccrs.PlateCarree())
348
 
349
  # Add world map features
 
361
  date_text = ax.text(0.02, 0.02, '', transform=ax.transAxes, fontsize=10, bbox=dict(facecolor='white', alpha=0.8))
362
 
363
  # Add sidebar with typhoon details on the right
364
+ details_title = fig.text(0.65, 0.90, "Typhoon Details", fontsize=12, fontweight='bold', verticalalignment='top', horizontalalignment='left')
365
+ details_text = fig.text(0.65, 0.85, '', fontsize=10, verticalalignment='top', horizontalalignment='left',
366
  bbox=dict(facecolor='white', alpha=0.8, boxstyle='round,pad=0.5'))
367
 
368
+ # Add color legend with colored markers below the details
369
  standard_dict = atlantic_standard if standard == 'atlantic' else taiwan_standard
370
+ legend_elements = [plt.Line2D([0], [0], marker='o', color='w', label=f"{cat}",
371
+ markerfacecolor=details['hex'], markersize=10)
372
+ for cat, details in standard_dict.items()]
373
+ fig.legend(handles=legend_elements, title="Color Legend", loc='center right',
374
+ bbox_to_anchor=(0.95, 0.5), fontsize=10)
375
 
376
  def init():
377
  line.set_data([], [])
 
452
  ### Features:
453
  - **Track Visualization**: View typhoon tracks by time period and ENSO phase
454
  - **Statistical Analysis**: Examine relationships between ONI values and typhoon characteristics
455
+ - **Path Animation**: Watch an animated typhoon path with video controls, world map, color legend with examples, and dynamic sidebar
456
  - **Regression Analysis**: Perform statistical regression on typhoon data
457
 
458
  Select a tab above to begin your analysis.
 
622
  4. Use the video player's built-in controls to play, pause, or scrub through the animation
623
  5. The animation shows the typhoon track growing over a world map, with:
624
  - Date on the bottom left
625
+ - Sidebar on the right showing typhoon details (name, date, wind speed, category) as it moves
626
+ - Color legend with colored markers (e.g., Red: C5 Super Typhoon) on the right
627
  """)
628
 
629
  def update_typhoon_options(year):