Spaces:
Runtime error
Runtime error
Commit ·
fc9d4ef
1
Parent(s): 2d4076c
add function to load Ground Truth images and update prediction display
Browse files728 unmodified lines729730731732733734120 unmodified lines8558568578588598608612 unmodified lines86486586686786886987087187287387487587642 unmodified lines9199209219229239249259269279289299309319329339349359367 unmodified lines944945946947948949950728 unmodified linesreturn mdef load_and_display_waveform(event_name, start_time, end_time, epicenter_lon, epicenter_lat):"""載入並顯示波形,讓使用者確認範圍"""try:120 unmodified linesintensity_map = create_intensity_map(pga_list, target_names, epicenter_lat, epicenter_lon)map_html = intensity_map._repr_html_()# 9. 統計資訊max_intensity = max([calculate_intensity(pga, label=True) for pga in pga_list])stats = f"✅ 預測完成!\n"stats += f"選取時間範圍: {start_time:.1f} - {end_time:.1f} 秒\n"2 unmodified linesstats += f"預測最大震度: {max_intensity}"logger.info("預測完成!")return map_html, statsexcept Exception as e:logger.error(f"預測過程發生錯誤: {e}")import tracebacktraceback.print_exc()return None, f"錯誤: {str(e)}"# ============ Gradio 介面 ============42 unmodified lines""")info_output = gr.Textbox(label="狀態資訊", lines=5, interactive=False)# 右側:震度分布圖with gr.Column(scale=1):gr.Markdown("## 預測震度分布")intensity_map = gr.HTML(label="互動式震度地圖")stats_output = gr.Textbox(label="預測統計", lines=4)# 下方:波形圖with gr.Row():gr.Markdown("## 輸入波形")with gr.Row():waveform_plot = gr.Plot(label="地震波形(前 10 個最近測站)")# 綁定事件# 第一步:載入波形7 unmodified linespredict_btn.click(fn=predict_intensity,inputs=[event_dropdown, start_slider, end_slider, epicenter_lon_input, epicenter_lat_input, vs30_input],outputs=[intensity_map, stats_output])demo.launch()728 unmodified lines729730731732733734735736737738739740741742743744745746747748749750751752753754755756757120 unmodified lines8788798808818828838848858868872 unmodified lines89089189289389489589689789889990090190242 unmodified lines9459469479489499509519529539549559569579589599609619629639649659669679687 unmodified lines976977978979980981982728 unmodified linesreturn mdef load_ground_truth_image(event_name):"""從 ground_truth 資料夾載入對應的 Ground Truth 圖片"""import os# 根據事件名稱找對應的圖片# 假設圖片命名格式為:20240403.png 或類似event_file = EARTHQUAKE_EVENTS[event_name]event_date = os.path.basename(event_file).replace('.mseed', '')# 嘗試不同的圖片格式ground_truth_dir = "ground_truth"possible_extensions = ['.png', '.jpg', '.jpeg', '.gif']for ext in possible_extensions:image_path = os.path.join(ground_truth_dir, f"{event_date}{ext}")if os.path.exists(image_path):logger.info(f"載入 Ground Truth 圖片: {image_path}")return image_pathlogger.warning(f"找不到 Ground Truth 圖片: {event_date}")return Nonedef load_and_display_waveform(event_name, start_time, end_time, epicenter_lon, epicenter_lat):"""載入並顯示波形,讓使用者確認範圍"""try:120 unmodified linesintensity_map = create_intensity_map(pga_list, target_names, epicenter_lat, epicenter_lon)map_html = intensity_map._repr_html_()# 9. 載入 Ground Truth 圖片ground_truth_path = load_ground_truth_image(event_name)# 10. 統計資訊max_intensity = max([calculate_intensity(pga, label=True) for pga in pga_list])stats = f"✅ 預測完成!\n"stats += f"選取時間範圍: {start_time:.1f} - {end_time:.1f} 秒\n"2 unmodified linesstats += f"預測最大震度: {max_intensity}"logger.info("預測完成!")return ground_truth_path, map_html, statsexcept Exception as e:logger.error(f"預測過程發生錯誤: {e}")import tracebacktraceback.print_exc()return None, None, f"錯誤: {str(e)}"# ============ Gradio 介面 ============42 unmodified lines""")info_output = gr.Textbox(label="狀態資訊", lines=5, interactive=False)stats_output = gr.Textbox(label="預測統計", lines=4)# 右側:波形圖with gr.Column(scale=1):gr.Markdown("## 輸入波形")waveform_plot = gr.Plot(label="地震波形(選定的最近測站)")# 下方:Ground Truth vs 預測結果with gr.Row():# 左側:Ground Truthwith gr.Column(scale=1):gr.Markdown("## Ground Truth 震度分布")ground_truth_image = gr.Image(label="實際觀測震度", type="filepath")# 右側:預測震度地圖with gr.Column(scale=1):gr.Markdown("## 預測震度分布")intensity_map = gr.HTML(label="互動式震度地圖")# 綁定事件# 第一步:載入波形7 unmodified linespredict_btn.click(fn=predict_intensity,inputs=[event_dropdown, start_slider, end_slider, epicenter_lon_input, epicenter_lat_input, vs30_input],outputs=[ground_truth_image, intensity_map, stats_output])demo.launch()
app.py+44-12