Files
Gset/tests/test_rootcause.py
treeandClaude Opus 5.5 e9616af34d 修复 P0:根因判定、退服率口径、影响评估与告警关联量化
- 根因判定改为三级(衍生关系/时间窗/疑似)并带置信度;小区退服告警 ±2 分钟匹配;
  衍生告警按父告警归类;优先选随业务恢复清除的告警;时间窗按历史流水 95% 分位数标定
- 退服率按日历统计周期计算;站点数分母支持站名关键字;重复退服率除以全部站点;
  月均按统计周期自然月数;导出与看板同一口径
- 影响评估新增受影响用户·小时与 500 米邻站覆盖补偿
- 新增告警—退服关联量化(支持度/置信度/提升度)及 /api/association,根因页展示
- 新增 tests/(扰动数据集留出检验、指标口径检验)与 bench.py;旧库启动时自动迁移

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 14:43:34 +08:00

55 lines
3.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""根因判定准确率检验:基线 + 6 组扰动数据集,时间窗只用 1~6 月流水标定,在 7~9 月(留出集)上计分。
运行:python tests/test_rootcause.py
"""
from harness import (WIDE_LEAD, add_noise, check, core, drop_root, finish, generate, jitter_clear, load, no_parent,
sample_data, score, skew_cell_alarm)
SPLIT = '2026-07-01' # 之前的流水用于标定时间窗,之后的用于计分
s, a, o = sample_data()
sw, aw, ow = generate('wide', lead=WIDE_LEAD)
# (名称, 数据, 准确率下限);下限为 None 的只报告不判定——根因告警缺失时,缺失的那部分本来就无法从告警判出
SCENARIOS = [
('S0 基线(仓库样例数据)', (s, a, o), 90),
('S1 去掉父告警(只靠时间窗)', (s, no_parent(a), o), 90),
('S2 小区退服告警时刻偏 5-120 秒', (s, skew_cell_alarm(a), o), 90),
('S3 S1 + 20% 根因告警缺失', (s, drop_root(no_parent(a), o, .2), o), None),
('S4 S1 + 30% 退服前干扰告警', (s, add_noise(no_parent(a), o, .3), o), 90),
('S5 S1 + 告警提前量变宽', (sw, no_parent(aw), ow), 90),
('S6 综合(S5 + 时刻偏移 + 30% 父告警 + 清除时刻 ±10 分 + 15% 缺失 + 25% 干扰)',
# 先删根因告警再扰动清除时刻:drop_root 靠“清除时间 = 恢复时间”识别根因告警
(sw, add_noise(jitter_clear(skew_cell_alarm(drop_root(no_parent(aw, keep=.3), ow, .15)), ow), ow, .25), ow),
None),
]
summary = []
for name, (sites, alarms, outages), floor in SCENARIOS:
df = load('rootcause', sites, alarms, outages, calibrate_until=SPLIT)
test = df[df['start_time'] >= SPLIT]
accuracy, rows = score(test)
other = (test['root_cause'] == '其他').mean() * 100
print(f'\n== {name}\n 留出集 {len(test)} 条,准确率 {accuracy}%,判为“其他” {other:.1f}%')
print(' 类别 precision recall F1')
for cause, precision, recall, f1 in rows:
print(f' {cause:<8} {precision:>7} {recall:>7} {f1:>6}')
confidence = test.groupby('confidence').apply(lambda x: (x['cause_category'] == x['root_cause']).mean() * 100,
include_groups=False).round(1)
print(' 各置信度准确率:', confidence.to_dict(), ' 条数:', test['confidence'].value_counts().to_dict())
print(' 时间窗:', {w['category']: w['window_min'] for w in core.window_table()})
summary.append((name, accuracy))
if floor is not None:
check(f'{name.split()[0]} 准确率 ≥ {floor}%', accuracy >= floor, f'({accuracy}%)')
else:
# 缺失场景的理论上限:真根因告警仍在的比例;判定应接近它,且“高/中”置信度的判定仍应可靠
reliable = test[test['confidence'].isin(['高', '中'])]
rel_acc = (reliable['cause_category'] == reliable['root_cause']).mean() * 100
check(f'{name.split()[0]} 高/中置信度判定准确率 ≥ 90%', rel_acc >= 90, f'({rel_acc:.1f}%,'
f'占 {len(reliable) / len(test) * 100:.0f}%)')
print('\n汇总(留出集准确率):')
for name, accuracy in summary:
print(f' {accuracy:>6}% {name}')
finish()