Spaces:
Runtime error
Runtime error
Commit ·
8c7ca37
1
Parent(s): 5c971be
feat: implement intelligent caching for STA/LTA results to improve UI responsiveness
Browse files- app.py +32 -15
- changelog.md +3 -1
app.py
CHANGED
|
@@ -313,6 +313,8 @@ def select_nearest_stations(st, epicenter_lat, epicenter_lon, n_stations=25, eve
|
|
| 313 |
station_distances = {} # 改用字典避免重複
|
| 314 |
p_wave_detected_count = 0
|
| 315 |
p_wave_failed_count = 0
|
|
|
|
|
|
|
| 316 |
|
| 317 |
# 初始化此事件的 cache
|
| 318 |
if event_name and event_name not in sta_lta_cache:
|
|
@@ -348,14 +350,32 @@ def select_nearest_stations(st, epicenter_lat, epicenter_lon, n_stations=25, eve
|
|
| 348 |
lon = station_data["Longitude"].values[0]
|
| 349 |
elev = station_data["Elevation"].values[0]
|
| 350 |
|
| 351 |
-
# 偵測 P 波(使用 Z 分量)
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
# 只保留成功偵測到 P 波的測站
|
| 361 |
if p_arrival_time is None:
|
|
@@ -374,12 +394,6 @@ def select_nearest_stations(st, epicenter_lat, epicenter_lon, n_stations=25, eve
|
|
| 374 |
}
|
| 375 |
p_wave_detected_count += 1
|
| 376 |
|
| 377 |
-
# 快取 STA/LTA 結果(spec: 避免滑桿更新時重複計算,提升反應速度)
|
| 378 |
-
if event_name:
|
| 379 |
-
sta_lta_cache[event_name][station_code] = {
|
| 380 |
-
"p_arrival_time": p_arrival_time,
|
| 381 |
-
"cft": cft
|
| 382 |
-
}
|
| 383 |
|
| 384 |
except Exception as e:
|
| 385 |
logger.warning(f"測站 {station_code} 資訊查詢失敗: {e}")
|
|
@@ -393,7 +407,10 @@ def select_nearest_stations(st, epicenter_lat, epicenter_lon, n_stations=25, eve
|
|
| 393 |
|
| 394 |
# 記錄實際可用的測站數(少於 25 站也允許繼續)
|
| 395 |
actual_count = len(selected_stations)
|
| 396 |
-
logger.info(
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
if actual_count < n_stations:
|
| 399 |
logger.warning(
|
|
|
|
| 313 |
station_distances = {} # 改用字典避免重複
|
| 314 |
p_wave_detected_count = 0
|
| 315 |
p_wave_failed_count = 0
|
| 316 |
+
cache_hit_count = 0
|
| 317 |
+
cache_miss_count = 0
|
| 318 |
|
| 319 |
# 初始化此事件的 cache
|
| 320 |
if event_name and event_name not in sta_lta_cache:
|
|
|
|
| 350 |
lon = station_data["Longitude"].values[0]
|
| 351 |
elev = station_data["Elevation"].values[0]
|
| 352 |
|
| 353 |
+
# 偵測 P 波(使用 Z 分量)- 優先使用快取
|
| 354 |
+
if event_name and event_name in sta_lta_cache and station_code in sta_lta_cache[event_name]:
|
| 355 |
+
# 使用快取的 STA/LTA 結果
|
| 356 |
+
cached_result = sta_lta_cache[event_name][station_code]
|
| 357 |
+
p_arrival_time = cached_result["p_arrival_time"]
|
| 358 |
+
cft = cached_result["cft"]
|
| 359 |
+
cache_hit_count += 1
|
| 360 |
+
logger.debug(f"測站 {station_code} 使用快取的 STA/LTA 結果")
|
| 361 |
+
else:
|
| 362 |
+
# 重新計算 STA/LTA
|
| 363 |
+
z_trace = st.select(station=station_code, component="Z")
|
| 364 |
+
if len(z_trace) == 0:
|
| 365 |
+
logger.debug(f"測站 {station_code} 無 Z 分量,跳過")
|
| 366 |
+
p_wave_failed_count += 1
|
| 367 |
+
continue
|
| 368 |
+
|
| 369 |
+
p_arrival_time, cft = detect_p_wave_sta_lta(z_trace[0])
|
| 370 |
+
cache_miss_count += 1
|
| 371 |
+
|
| 372 |
+
# 快取 STA/LTA 結果
|
| 373 |
+
if event_name:
|
| 374 |
+
sta_lta_cache[event_name][station_code] = {
|
| 375 |
+
"p_arrival_time": p_arrival_time,
|
| 376 |
+
"cft": cft
|
| 377 |
+
}
|
| 378 |
+
logger.debug(f"測站 {station_code} STA/LTA 結果已快取")
|
| 379 |
|
| 380 |
# 只保留成功偵測到 P 波的測站
|
| 381 |
if p_arrival_time is None:
|
|
|
|
| 394 |
}
|
| 395 |
p_wave_detected_count += 1
|
| 396 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
except Exception as e:
|
| 399 |
logger.warning(f"測站 {station_code} 資訊查詢失敗: {e}")
|
|
|
|
| 407 |
|
| 408 |
# 記錄實際可用的測站數(少於 25 站也允許繼續)
|
| 409 |
actual_count = len(selected_stations)
|
| 410 |
+
logger.info(
|
| 411 |
+
f"P 波偵測結果: 成功 {p_wave_detected_count} 站, 失敗 {p_wave_failed_count} 站 | "
|
| 412 |
+
f"STA/LTA 快取: 命中 {cache_hit_count} 次, 未命中 {cache_miss_count} 次"
|
| 413 |
+
)
|
| 414 |
|
| 415 |
if actual_count < n_stations:
|
| 416 |
logger.warning(
|
changelog.md
CHANGED
|
@@ -5,10 +5,12 @@
|
|
| 5 |
## [Unreleased]
|
| 6 |
|
| 7 |
### Added
|
| 8 |
-
- **STA/LTA 計算結果快取**
|
| 9 |
- 新增全域快取 `sta_lta_cache`,在選擇事件時儲存所有測站的 STA/LTA 計算結果(P 波到時與 characteristic function)。
|
|
|
|
| 10 |
- 當使用者調整時間滑桿時,直接使用快取的 P 波到時資訊,避免重複計算 STA/LTA,大幅提升 UI 反應速度。
|
| 11 |
- 快取結構:`{event_name: {station_code: {"p_arrival_time": float, "cft": array}}}`
|
|
|
|
| 12 |
- P 波到時資訊已存於 `selected_stations` 並透過 `gr.State` 傳遞,無需每次從原始波形重新計算。
|
| 13 |
|
| 14 |
- **P 波自動偵測功能(STA/LTA)**
|
|
|
|
| 5 |
## [Unreleased]
|
| 6 |
|
| 7 |
### Added
|
| 8 |
+
- **STA/LTA 計算結果智能快取**
|
| 9 |
- 新增全域快取 `sta_lta_cache`,在選擇事件時儲存所有測站的 STA/LTA 計算結果(P 波到時與 characteristic function)。
|
| 10 |
+
- **智能快取檢查**:在 `select_nearest_stations` 函數中,優先檢查快取是否存在,若存在則直接使用,避免重複計算。
|
| 11 |
- 當使用者調整時間滑桿時,直接使用快取的 P 波到時資訊,避免重複計算 STA/LTA,大幅提升 UI 反應速度。
|
| 12 |
- 快取結構:`{event_name: {station_code: {"p_arrival_time": float, "cft": array}}}`
|
| 13 |
+
- **快取統計**:記錄快取命中率(cache hit/miss),方便除錯與效能分析。
|
| 14 |
- P 波到時資訊已存於 `selected_stations` 並透過 `gr.State` 傳遞,無需每次從原始波形重新計算。
|
| 15 |
|
| 16 |
- **P 波自動偵測功能(STA/LTA)**
|