RailWise KB已发布
AI异常检测与预警工具
RailWise AI异常检测与预警工具基于统计学和机器学习算法,实时监测测量数据异常,自动判定预警等级,并提供智能处置建议。支持多种检测算法和灵活的预警规则配置。
ai-guide
AI异常检测与预警工具
Section titled “AI异常检测与预警工具”AI异常检测与预警工具是 RailWise 提供的专业监测数据异常检测和预警系统,面向工程监测领域的工程师和技术人员。工具基于统计学和机器学习算法,能够实时监测测量数据异常,自动判定预警等级,并提供智能处置建议,帮助用户及时发现和处理潜在的安全风险。
| 场景 | 说明 | 检测重点 |
|---|---|---|
| 盾构施工监测 | 盾构穿越期间的变形监测 | 沉降加速、突变 |
| 桥梁健康监测 | 桥梁结构长期监测 | 趋势突变、周期性异常 |
| 基坑工程监测 | 基坑开挖期间的监测 | 支撑轴力突变、水位异常 |
| 隧道收敛监测 | 隧道断面变形监测 | 收敛速率异常 |
| 高边坡监测 | 边坡稳定性监测 | 位移加速、裂缝扩展 |
| 数据质量监控 | 测量数据质量监控 | 仪器故障、操作失误 |
| 项目 | 版本 |
|---|---|
| AI异常检测与预警工具 | 2.5.0 |
| Python | 3.11+ |
| Scikit-learn | 1.4+ |
| PyTorch | 2.2+ |
| Statsmodels | 0.14+ |
| Redis | 7+ |
| 功能模块 | 说明 | 技术实现 |
|---|---|---|
| 实时异常检测 | 实时监测数据流异常 | 流式处理 + 在线检测 |
| 批量异常检测 | 历史数据异常检测 | 批量处理 + 离线检测 |
| 预警等级判定 | 自动判定预警等级 | 规则引擎 + 机器学习 |
| 智能处置建议 | 提供处置建议 | 知识图谱 + 规则库 |
| 预警通知 | 多渠道预警通知 | 消息队列 + 推送服务 |
| 预警升级 | 预警自动升级/降级 | 时间窗口 + 趋势分析 |
| 误报过滤 | 减少误报 | 多算法交叉验证 |
| 检测算法管理 | 算法选择、参数调优 | 算法库 + 配置中心 |
支持的监测参数
Section titled “支持的监测参数”| 参数类型 | 检测维度 | 异常类型 |
|---|---|---|
| 垂直位移 | 累计值、变化速率 | 突变、加速、振荡 |
| 水平位移 | X/Y方向、合位移 | 方向突变、速率异常 |
| 收敛 | 收敛量、收敛速率 | 收敛加速、收敛停滞 |
| 倾斜 | 倾斜度、倾斜方向 | 倾斜加速、方向突变 |
| 支撑轴力 | 轴力值、变化量 | 轴力突变、持续下降 |
| 地下水位 | 水位高程、变化速率 | 水位骤降、持续上升 |
| 裂缝 | 缝宽、开合速率 | 裂缝扩展、突变 |
| 温度 | 温度值、温差 | 温度异常影响 |
┌─────────────────────────────────────────────────────────────────────┐│ AI异常检测与预警工具架构 │├─────────────────────────────────────────────────────────────────────┤│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ││ │ 数据采集 │ │ 规则配置 │ │ 用户界面 │ ││ │ - TSM API │ │ - 阈值规则 │ │ - Web界面 │ ││ │ - 消息队列 │ │ - 算法配置 │ │ - 移动端 │ ││ │ - 文件导入 │ │ - 通知配置 │ │ - API接口 │ ││ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ ││ │ │ │ ││ └──────────────────┼──────────────────┘ ││ ▼ ││ ┌──────────────────────────────┐ ││ │ 异常检测引擎 │ ││ │ 数据预处理 → 特征提取 → 检测 │ ││ └──────────────┬───────────────┘ ││ │ ││ ┌───────────────────┼───────────────────┐ ││ ▼ ▼ ▼ ││ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ││ │ 统计检测层 │ │ 机器学习层 │ │ 深度学习层 │ ││ │ - 3σ准则 │ │ - Isolation │ │ - LSTM │ ││ │ - Grubbs │ │ Forest │ │ - Autoencoder│ ││ │ - IQR │ │ - LOF │ │ - Transformer│ ││ │ - CUSUM │ │ - One-Class │ │ - GAN │ ││ │ - EWMA │ │ SVM │ │ │ ││ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ ││ │ │ │ ││ └──────────────────┼──────────────────┘ ││ ▼ ││ ┌──────────────────────────────┐ ││ │ 预警判定引擎 │ ││ │ 多算法融合 → 等级判定 → 建议 │ ││ └──────────────┬───────────────┘ ││ │ ││ ┌──────────────┴───────────────┐ ││ │ 通知与响应层 │ ││ │ 邮件 / 短信 / App / 微信 / 钉钉 │ ││ └──────────────────────────────┘ │└─────────────────────────────────────────────────────────────────────┘| 项目 | 最低要求 | 推荐配置 |
|---|---|---|
| Python | 3.11 | 3.12 |
| 内存 | 8 GB | 32 GB |
| 磁盘 | 2 GB | 20 GB |
| GPU | 可选 | NVIDIA CUDA 12+ |
| Redis | 6 | 7 |
# 创建虚拟环境python -m venv railwise-anomalysource railwise-anomaly/bin/activate
# 安装异常检测工具pip install railwise-anomaly
# 安装完整版(包含深度学习)pip install "railwise-anomaly[deep-learning]"
# 验证安装python -c "import railwise_anomaly; print(railwise_anomaly.__version__)"# 核心依赖pip install numpy pandas scikit-learn scipy
# 深度学习(可选)pip install torch torchvision
# 实时处理pip install redis celery
# 可视化pip install matplotlib plotly初始化检测器
Section titled “初始化检测器”from railwise_anomaly import AnomalyDetector
# 创建检测器detector = AnomalyDetector( api_key="rw_anomaly_xxxxxxxx", base_url="https://api.railwise.cn/v2", config={ "algorithms": ["isolation_forest", "lstm", "grubbs"], "confidence_threshold": 0.95, "min_anomaly_duration": 2, # 至少连续2个异常点 "notification_channels": ["email", "app"] })实时异常检测
Section titled “实时异常检测”# 配置实时检测detector.configure_realtime( project_id="proj_rw_2026_001", point_ids=["P1", "P2", "P3", "P4", "P5"], check_interval=60, # 每60秒检测一次 window_size=24 # 使用最近24个数据点)
# 启动实时检测detector.start_realtime()
# 注册回调函数@detector.on_anomalydef handle_anomaly(anomaly): print(f"检测到异常!") print(f" 测点: {anomaly.point_id}") print(f" 时间: {anomaly.timestamp}") print(f" 类型: {anomaly.type}") print(f" 严重程度: {anomaly.severity}") print(f" 置信度: {anomaly.confidence:.2%}") print(f" 建议: {anomaly.recommendation}")
# 停止检测# detector.stop_realtime()批量异常检测
Section titled “批量异常检测”# 历史数据异常检测results = detector.detect_batch( project_id="proj_rw_2026_001", point_ids=["P1", "P2", "P3"], date_range=("2026-07-01", "2026-07-08"), algorithms=["isolation_forest", "grubbs", "lstm"], voting="majority" # 多数投票)
# 查看结果for result in results: print(f"测点 {result.point_id}:") print(f" 异常数: {len(result.anomalies)}") for anomaly in result.anomalies: print(f" {anomaly.timestamp}: {anomaly.description}")预警等级判定
Section titled “预警等级判定”from railwise_anomaly import AlertClassifier
# 创建预警分类器classifier = AlertClassifier( thresholds={ "yellow": 5.0, # 黄色预警阈值 "orange": 10.0, # 橙色预警阈值 "red": 15.0 # 红色预警阈值 }, rate_thresholds={ "yellow": 2.0, # 黄色预警速率阈值 (mm/天) "orange": 5.0, "red": 10.0 })
# 判定预警等级alert = classifier.classify( point_id="P7", cumulative_value=18.5, # 累计变形值 rate=8.5, # 变形速率 (mm/天) acceleration=1.2 # 变形加速度)
print(f"预警等级: {alert.level}") # redprint(f"预警原因: {alert.reason}")print(f"建议措施: {alert.recommendations}")from railwise_anomaly import NotificationManager
# 创建通知管理器nm = NotificationManager()
# 配置通知渠道nm.configure_channel("email", { "smtp_server": "smtp.railwise.cn", "from": "alerts@railwise.cn", "to": ["engineer@railwise.cn", "manager@railwise.cn"]})
nm.configure_channel("sms", { "provider": "aliyun", "template_code": "SMS_12345678", "phone_numbers": ["138****1234"]})
nm.configure_channel("app", { "push_service": "firebase", "topics": ["proj_rw_2026_001_alerts"]})
# 发送预警通知nm.send_alert(alert, channels=["email", "app"])检测算法详解
Section titled “检测算法详解”1. 统计方法
Section titled “1. 统计方法”3σ准则(Three Sigma Rule)
Section titled “3σ准则(Three Sigma Rule)”基于正态分布假设,超出均值±3倍标准差的数据视为异常。
from railwise_anomaly.algorithms import ThreeSigmaDetector
detector = ThreeSigmaDetector( sigma_threshold=3.0, window_size=30, # 滑动窗口大小 min_samples=10 # 最小样本数)
anomalies = detector.detect(data)适用场景: 数据量充足、近似正态分布的监测数据
优点: 简单直观,计算速度快 缺点: 对非正态分布数据效果差
Grubbs检验
Section titled “Grubbs检验”用于检测单变量数据中的离群值。
from railwise_anomaly.algorithms import GrubbsDetector
detector = GrubbsDetector( alpha=0.05, # 显著性水平 max_outliers=5 # 最多检测5个异常值)
anomalies = detector.detect(data)适用场景: 小样本数据的离群值检测
CUSUM(累积和控制图)
Section titled “CUSUM(累积和控制图)”检测均值偏移的累积和控制图。
from railwise_anomaly.algorithms import CUSUMDetector
detector = CUSUMDetector( target_mean=0.0, # 目标均值 threshold=5.0, # 累积和阈值 drift=0.5 # 允许漂移)
anomalies = detector.detect(data)适用场景: 检测微小的均值偏移
2. 机器学习方法
Section titled “2. 机器学习方法”Isolation Forest(孤立森林)
Section titled “Isolation Forest(孤立森林)”基于随机划分的异常检测算法,异常点更容易被孤立。
from railwise_anomaly.algorithms import IsolationForestDetector
detector = IsolationForestDetector( n_estimators=100, contamination=0.05, # 预期异常比例 max_samples=256, random_state=42)
# 训练detector.fit(training_data)
# 检测anomalies = detector.detect(data)适用场景: 高维数据、复杂数据分布
优点: 对高维数据效果好,无需假设分布 缺点: 需要调参,对局部异常不敏感
LOF(局部异常因子)
Section titled “LOF(局部异常因子)”基于局部密度的异常检测算法。
from railwise_anomaly.algorithms import LOFDetector
detector = LOFDetector( n_neighbors=20, contamination=0.05)
anomalies = detector.detect(data)适用场景: 局部密度差异明显的数据
3. 深度学习方法
Section titled “3. 深度学习方法”LSTM自编码器
Section titled “LSTM自编码器”使用LSTM自编码器检测时间序列异常。
from railwise_anomaly.algorithms import LSTMAutoencoderDetector
detector = LSTMAutoencoderDetector( sequence_length=24, # 序列长度 hidden_size=64, epochs=100, threshold_method="percentile", # 阈值确定方法 threshold_percentile=95)
# 训练detector.fit(training_data)
# 检测anomalies = detector.detect(data)适用场景: 复杂时间序列模式
优点: 能捕捉复杂时序模式 缺点: 需要大量训练数据,计算资源要求高
Transformer异常检测
Section titled “Transformer异常检测”基于Transformer架构的异常检测。
from railwise_anomaly.algorithms import TransformerDetector
detector = TransformerDetector( d_model=128, nhead=8, num_layers=3, sequence_length=48)
detector.fit(training_data)anomalies = detector.detect(data)适用场景: 长序列依赖、复杂模式
预警规则配置
Section titled “预警规则配置”规则配置文件
Section titled “规则配置文件”project_id: "proj_rw_2026_001"project_name: "绕城高速管廊工程1标段"
thresholds: settlement: yellow: 5.0 # mm orange: 10.0 red: 15.0 displacement: yellow: 5.0 orange: 10.0 red: 15.0 convergence: yellow: 10.0 orange: 20.0 red: 30.0 tilt: yellow: 2.0 # ‰ orange: 3.0 red: 5.0
rate_thresholds: settlement: yellow: 2.0 # mm/天 orange: 5.0 red: 10.0 displacement: yellow: 2.0 orange: 5.0 red: 10.0
acceleration_thresholds: settlement: yellow: 0.5 # mm/天² orange: 1.0 red: 2.0
# 预警升级规则escalation_rules: - condition: "yellow_count >= 3" action: "escalate_to_orange" - condition: "orange_duration >= 24h" action: "escalate_to_red" - condition: "red_count >= 1" action: "immediate_notification"
# 通知规则notification_rules: yellow: channels: ["app"] recipients: ["project_team"] delay: 0 orange: channels: ["email", "app"] recipients: ["project_team", "technical_director"] delay: 0 red: channels: ["email", "sms", "app"] recipients: ["project_team", "technical_director", "general_manager"] delay: 0# 从文件加载规则detector.load_rules("./alert_rules.yaml")
# 或动态配置规则detector.configure_rules({ "thresholds": { "settlement": {"yellow": 5.0, "orange": 10.0, "red": 15.0} }, "notification": { "red": {"channels": ["email", "sms"], "immediate": True} }})实时监测模式
Section titled “实时监测模式”from railwise_anomaly import StreamingDetector
# 创建流式检测器stream = StreamingDetector( detector=detector, buffer_size=100, # 缓冲区大小 processing_interval=30 # 处理间隔(秒))
# 注册数据源stream.register_source( name="tsm_stream", type="websocket", url="wss://tsm.railwise.cn/stream", api_key="rw_tsm_xxxxxxxx")
# 启动流式检测stream.start()
# 监控统计import timewhile True: stats = stream.get_stats() print(f"处理数据点: {stats.processed_count}") print(f"检测到异常: {stats.anomaly_count}") print(f"当前预警: {stats.active_alerts}") time.sleep(60)边缘检测模式
Section titled “边缘检测模式”from railwise_anomaly import EdgeDetector
# 创建边缘检测器(部署在本地设备)edge = EdgeDetector( model_path="./models/anomaly_model.onnx", threshold=0.8, device="cpu" # 或 "cuda")
# 本地检测result = edge.detect(local_data)
# 仅上传异常结果到云端if result.is_anomaly: edge.upload_alert(result)与RAILWISE-TSM集成
Section titled “与RAILWISE-TSM集成”自动检测配置
Section titled “自动检测配置”from railwise_tsm import TSMClientfrom railwise_anomaly import AnomalyDetector
# 创建TSM客户端tsm = TSMClient( base_url="https://tsm.railwise.cn", api_key="rw_tsm_xxxxxxxx")
# 创建检测器detector = AnomalyDetector(tsm_client=tsm)
# 配置自动检测detector.configure_auto_detect( project_id="proj_rw_2026_001", detect_interval=300, # 每5分钟检测 algorithms=["isolation_forest", "grubbs"], auto_alert=True # 自动发送预警)
# 启动自动检测detector.start_auto_detect()# 注册TSM回调@detector.on_alertdef handle_tsm_alert(alert): # 在TSM中创建预警记录 tsm.create_alert_record( project_id=alert.project_id, point_id=alert.point_id, level=alert.level, value=alert.value, threshold=alert.threshold, recommendation=alert.recommendation )
# 更新测点状态 tsm.update_point_status( point_id=alert.point_id, status=alert.level )1. 算法选择
Section titled “1. 算法选择”| 数据特点 | 推荐算法 | 说明 |
|---|---|---|
| 单变量、正态分布 | 3σ / Grubbs | 简单高效 |
| 单变量、非正态 | IQR / CUSUM | 不依赖分布假设 |
| 多变量、高维 | Isolation Forest | 处理高维数据 |
| 局部密度差异 | LOF | 检测局部异常 |
| 复杂时序模式 | LSTM | 捕捉时序依赖 |
| 长序列依赖 | Transformer | 处理长序列 |
2. 参数调优
Section titled “2. 参数调优”# 使用网格搜索调优from sklearn.model_selection import GridSearchCV
param_grid = { 'n_estimators': [50, 100, 200], 'contamination': [0.01, 0.05, 0.1], 'max_samples': [128, 256, 512]}
grid_search = GridSearchCV( estimator=IsolationForestDetector(), param_grid=param_grid, scoring='f1', cv=5)
grid_search.fit(training_data)print(f"最优参数: {grid_search.best_params_}")3. 减少误报
Section titled “3. 减少误报”- 使用多算法交叉验证
- 设置最小异常持续时间
- 结合业务规则过滤
- 定期回顾和调整阈值
# 多算法融合detector = AnomalyDetector( algorithms=["isolation_forest", "grubbs", "lstm"], voting="majority", # 多数投票 min_agreement=2 # 至少2个算法同意)4. 性能优化
Section titled “4. 性能优化”- 使用增量学习更新模型
- 对大数据集使用采样
- 启用GPU加速(深度学习模型)
- 使用Redis缓存中间结果
限制与注意事项
Section titled “限制与注意事项”| 限制项 | 说明 |
|---|---|
| 训练数据 | 深度学习模型需要至少1000条历史数据 |
| 检测延迟 | 实时检测有30-60秒延迟 |
| 误报率 | 统计方法约5-10%,机器学习方法约2-5% |
| 计算资源 | LSTM/Transformer需要GPU加速 |
| 模型更新 | 建议每月重新训练模型 |
| 数据质量 | 异常检测效果受数据质量影响 |
注意: 异常检测结果需由工程师审核确认,避免误报导致不必要的应急响应。
Q1: 检测算法如何选择?
Section titled “Q1: 检测算法如何选择?”根据数据特点选择:
- 数据量小、简单分布 → 统计方法
- 数据量大、复杂模式 → 机器学习方法
- 时序依赖强 → 深度学习方法
Q2: 误报太多怎么办?
Section titled “Q2: 误报太多怎么办?”- 提高阈值
- 增加最小异常持续时间
- 使用多算法交叉验证
- 检查数据质量
detector.configure( threshold=0.9, # 提高阈值 min_duration=3, # 至少3个连续异常点 voting="unanimous" # 所有算法一致才报警)Q3: 漏报怎么办?
Section titled “Q3: 漏报怎么办?”- 降低阈值
- 增加算法灵敏度
- 使用更多检测算法
- 检查训练数据是否包含类似异常模式
Q4: 如何评估检测效果?
Section titled “Q4: 如何评估检测效果?”from railwise_anomaly.evaluation import evaluate_detector
metrics = evaluate_detector( detector=detector, test_data=test_data, ground_truth=labels, metrics=["precision", "recall", "f1", "auc_roc"])
print(f"精确率: {metrics.precision:.3f}")print(f"召回率: {metrics.recall:.3f}")print(f"F1分数: {metrics.f1:.3f}")print(f"AUC-ROC: {metrics.auc_roc:.3f}")| 版本 | 日期 | 变更内容 |
|---|---|---|
| 2.5.0 | 2026-07-08 | 新增Transformer检测器;优化实时检测性能;新增边缘检测模式 |
| 2.0.0 | 2026-04-20 | 新增LSTM检测器;支持流式检测;新增预警升级规则 |
| 1.5.0 | 2026-01-15 | 新增Isolation Forest和LOF;支持多算法融合 |
| 1.0.0 | 2025-10-10 | 初始版本发布;支持3σ、Grubbs、CUSUM等统计方法 |
技术支持: 如有疑问,请联系 RailWise 技术支持团队
- 邮箱:support@railwise.cn
- 电话:0574-XXXXXXXX
- 文档反馈:GitHub Issues
