certiflow 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- certiflow-0.1.0/LICENSE +21 -0
- certiflow-0.1.0/MANIFEST.in +5 -0
- certiflow-0.1.0/PKG-INFO +262 -0
- certiflow-0.1.0/README.md +229 -0
- certiflow-0.1.0/certiflow/__init__.py +59 -0
- certiflow-0.1.0/certiflow/confidence_config.py +118 -0
- certiflow-0.1.0/certiflow/confidence_fusion.py +113 -0
- certiflow-0.1.0/certiflow/demo.py +108 -0
- certiflow-0.1.0/certiflow/drift_monitor.py +200 -0
- certiflow-0.1.0/certiflow/event_merger.py +69 -0
- certiflow-0.1.0/certiflow/pipeline.py +140 -0
- certiflow-0.1.0/certiflow/py.typed +0 -0
- certiflow-0.1.0/certiflow/risk_engine.py +175 -0
- certiflow-0.1.0/certiflow.egg-info/PKG-INFO +262 -0
- certiflow-0.1.0/certiflow.egg-info/SOURCES.txt +19 -0
- certiflow-0.1.0/certiflow.egg-info/dependency_links.txt +1 -0
- certiflow-0.1.0/certiflow.egg-info/entry_points.txt +2 -0
- certiflow-0.1.0/certiflow.egg-info/requires.txt +8 -0
- certiflow-0.1.0/certiflow.egg-info/top_level.txt +1 -0
- certiflow-0.1.0/pyproject.toml +56 -0
- certiflow-0.1.0/setup.cfg +4 -0
certiflow-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 刘秉其
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
certiflow-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: certiflow
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Confidence fusion, risk scoring & drift detection pipeline — from raw logits to actionable risk levels in five layers.
|
|
5
|
+
Author: 刘秉其
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/your-org/certiflow
|
|
8
|
+
Project-URL: Repository, https://github.com/your-org/certiflow
|
|
9
|
+
Project-URL: Issues, https://github.com/your-org/certiflow/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/your-org/certiflow#readme
|
|
11
|
+
Keywords: confidence,fusion,risk,drift-detection,ema,pipeline
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: numpy>=1.22
|
|
27
|
+
Provides-Extra: yaml
|
|
28
|
+
Requires-Dist: pyyaml>=6.0; extra == "yaml"
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
31
|
+
Requires-Dist: pyyaml>=6.0; extra == "dev"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# CertiFlow
|
|
35
|
+
|
|
36
|
+
零依赖(仅 numpy)的置信度融合与风险评分工具包。从分类器的原始 logits 到最终的风险评级,五层流水线即插即用。
|
|
37
|
+
|
|
38
|
+
## 安装
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# 从 PyPI 安装(发布后)
|
|
42
|
+
pip install certiflow
|
|
43
|
+
|
|
44
|
+
# 或从源码安装(开发模式)
|
|
45
|
+
git clone https://github.com/your-org/certiflow.git
|
|
46
|
+
cd certiflow
|
|
47
|
+
pip install -e .
|
|
48
|
+
|
|
49
|
+
# 安装可选 YAML 支持
|
|
50
|
+
pip install -e ".[yaml]"
|
|
51
|
+
|
|
52
|
+
# 安装开发依赖
|
|
53
|
+
pip install -e ".[dev]"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 快速开始
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from certiflow import ConfidencePipeline, ConfidenceConfig
|
|
60
|
+
|
|
61
|
+
config = ConfidenceConfig.from_dict({"alpha": 0.6, "output_threshold": 0.2})
|
|
62
|
+
pipeline = ConfidencePipeline(config)
|
|
63
|
+
|
|
64
|
+
# 逐帧流式处理
|
|
65
|
+
result = pipeline.process_logits(logits_array) # -> FusionResult | None
|
|
66
|
+
if result:
|
|
67
|
+
print(result.event_type, result.confidence)
|
|
68
|
+
|
|
69
|
+
# 批量处理事件列表
|
|
70
|
+
events = [
|
|
71
|
+
{"event_type": "alarm", "confidence": 0.85, "start_sec": 0.0, "end_sec": 1.0},
|
|
72
|
+
{"event_type": "alarm", "confidence": 0.90, "start_sec": 0.8, "end_sec": 1.8},
|
|
73
|
+
]
|
|
74
|
+
full = pipeline.process_events(events)
|
|
75
|
+
print(full.events_merged) # 合并后的事件
|
|
76
|
+
print(full.risk.score) # 风险分 [0, 1]
|
|
77
|
+
print(full.drift.detected) # 是否漂移
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 架构
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
┌─────────────┐
|
|
84
|
+
│ logits │
|
|
85
|
+
└──────┬──────┘
|
|
86
|
+
│
|
|
87
|
+
▼
|
|
88
|
+
┌────────────────────────┐
|
|
89
|
+
│ Layer 1 · softmax │ logits → 概率向量
|
|
90
|
+
└───────────┬────────────┘
|
|
91
|
+
│
|
|
92
|
+
▼
|
|
93
|
+
┌────────────────────────┐
|
|
94
|
+
│ Layer 2 · EMA 融合 │ p̃_t = α·p_t + (1−α)·p̃_{t−1}
|
|
95
|
+
└───────────┬────────────┘
|
|
96
|
+
│
|
|
97
|
+
┌─────┴──────┐
|
|
98
|
+
│ events │
|
|
99
|
+
└─────┬──────┘
|
|
100
|
+
│
|
|
101
|
+
▼
|
|
102
|
+
┌────────────────────────┐
|
|
103
|
+
│ Layer 3 · 事件合并 │ 合并同类重叠/相邻事件
|
|
104
|
+
└───────────┬────────────┘
|
|
105
|
+
│
|
|
106
|
+
▼
|
|
107
|
+
┌────────────────────────┐
|
|
108
|
+
│ Layer 4 · 风险评分 │ → low / medium / high
|
|
109
|
+
└───────────┬────────────┘
|
|
110
|
+
│
|
|
111
|
+
▼
|
|
112
|
+
┌────────────────────────┐
|
|
113
|
+
│ Layer 5 · 漂移检测 │ threshold / z_test / page_hinkley
|
|
114
|
+
└───────────┬────────────┘
|
|
115
|
+
│
|
|
116
|
+
▼
|
|
117
|
+
┌───────────┐
|
|
118
|
+
│ result │
|
|
119
|
+
└───────────┘
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
| 层级 | 模块 | 类 / 函数 | 作用 |
|
|
123
|
+
|------|------|-----------|------|
|
|
124
|
+
| 1 | `confidence_fusion` | `softmax()` | logits → 概率向量 |
|
|
125
|
+
| 2 | `confidence_fusion` | `ConfidenceFusion` | 因果 EMA 平滑:`p̃_t = α·p_t + (1−α)·p̃_{t−1}` |
|
|
126
|
+
| 3 | `event_merger` | `merge_events()` | 合并同类重叠/相邻事件,保留最高置信度 |
|
|
127
|
+
| 4 | `risk_engine` | `RiskEngine` | 从置信度统计量加权评分 → low / medium / high |
|
|
128
|
+
| 5 | `drift_monitor` | `DriftMonitor` | 在线漂移检测(threshold / z_test / page_hinkley) |
|
|
129
|
+
|
|
130
|
+
每层可独立使用,也可通过 `ConfidencePipeline` 一键串联。
|
|
131
|
+
|
|
132
|
+
## 配置
|
|
133
|
+
|
|
134
|
+
所有参数通过 `ConfidenceConfig` 统一管理,支持三种初始化方式:
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
# 1. 直接构造
|
|
138
|
+
from certiflow import ConfidenceConfig, FusionConfig, DriftConfig
|
|
139
|
+
config = ConfidenceConfig(
|
|
140
|
+
fusion=FusionConfig(alpha=0.7, output_threshold=0.15),
|
|
141
|
+
drift=DriftConfig(mode="z_test"),
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
# 2. 扁平字典
|
|
145
|
+
config = ConfidenceConfig.from_dict({
|
|
146
|
+
"alpha": 0.6,
|
|
147
|
+
"output_threshold": 0.2,
|
|
148
|
+
"mode": "threshold",
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
# 3. YAML 文件(需安装 pyyaml)
|
|
152
|
+
config = ConfidenceConfig.from_yaml("config.yaml")
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### 核心参数
|
|
156
|
+
|
|
157
|
+
| 参数 | 默认值 | 说明 |
|
|
158
|
+
|------|--------|------|
|
|
159
|
+
| `alpha` | 0.6 | EMA 平滑因子 ∈ (0, 1],越大越偏向最新帧 |
|
|
160
|
+
| `output_threshold` | 0.2 | 融合后置信度低于此值的结果被过滤 |
|
|
161
|
+
| `base_threshold` | 0.05 | 单帧置信度低于此值时跳过(减少噪声) |
|
|
162
|
+
| `max_gap_sec` | 0.10 | 事件合并的最大时间间隔(秒) |
|
|
163
|
+
| `medium_threshold` | 0.40 | 风险等级 medium 的分界线 |
|
|
164
|
+
| `high_threshold` | 0.70 | 风险等级 high 的分界线 |
|
|
165
|
+
| `drift.mode` | `"threshold"` | 漂移检测模式:`threshold` / `z_test` / `page_hinkley` |
|
|
166
|
+
|
|
167
|
+
## 单独使用各层
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
import numpy as np
|
|
171
|
+
from certiflow import ConfidenceFusion, softmax, merge_events, RiskEngine, DriftMonitor
|
|
172
|
+
|
|
173
|
+
# Layer 1-2: 仅做置信度融合
|
|
174
|
+
fusion = ConfidenceFusion(alpha=0.6)
|
|
175
|
+
fusion.update(softmax(logits))
|
|
176
|
+
result = fusion.query()
|
|
177
|
+
|
|
178
|
+
# Layer 3: 仅做事件合并
|
|
179
|
+
merged = merge_events(raw_events)
|
|
180
|
+
|
|
181
|
+
# Layer 4: 仅做风险评分
|
|
182
|
+
risk = RiskEngine().assess(merged)
|
|
183
|
+
|
|
184
|
+
# Layer 5: 仅做漂移检测
|
|
185
|
+
monitor = DriftMonitor()
|
|
186
|
+
drift = monitor.evaluate(batch_1)
|
|
187
|
+
drift = monitor.evaluate(batch_2) # 与 batch_1 对比
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## 运行 Demo
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
python -m certiflow.demo
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
输出示例:
|
|
197
|
+
|
|
198
|
+
```
|
|
199
|
+
Synthetic data: 40 frames × 10 classes
|
|
200
|
+
|
|
201
|
+
============================================================
|
|
202
|
+
Layer 1-2: Softmax + EMA Fusion
|
|
203
|
+
============================================================
|
|
204
|
+
frame 15 → class=3 conf=0.2766 fused=True
|
|
205
|
+
frame 20 → class=3 conf=0.7439 fused=True
|
|
206
|
+
...
|
|
207
|
+
|
|
208
|
+
============================================================
|
|
209
|
+
Layer 3: Merged Events
|
|
210
|
+
============================================================
|
|
211
|
+
type=3 conf=0.7677 [7.50s – 15.50s] dur=8.00s
|
|
212
|
+
|
|
213
|
+
============================================================
|
|
214
|
+
Layer 4: Risk Assessment
|
|
215
|
+
============================================================
|
|
216
|
+
Score: 0.6152
|
|
217
|
+
Level: medium
|
|
218
|
+
Uncertainty: 0.5063
|
|
219
|
+
|
|
220
|
+
============================================================
|
|
221
|
+
Layer 5: Drift Detection
|
|
222
|
+
============================================================
|
|
223
|
+
Detected: False
|
|
224
|
+
Mode: threshold
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## 文件结构
|
|
228
|
+
|
|
229
|
+
```
|
|
230
|
+
CertiFlow/
|
|
231
|
+
├── pyproject.toml # 打包配置 (pip install -e .)
|
|
232
|
+
├── README.md
|
|
233
|
+
├── LICENSE
|
|
234
|
+
└── certiflow/ # Python 包
|
|
235
|
+
├── __init__.py # 包导出 & 版本号
|
|
236
|
+
├── confidence_config.py # 统一配置 dataclass
|
|
237
|
+
├── confidence_fusion.py # Layer 1-2: softmax + EMA
|
|
238
|
+
├── event_merger.py # Layer 3: 事件合并
|
|
239
|
+
├── risk_engine.py # Layer 4: 风险评分
|
|
240
|
+
├── drift_monitor.py # Layer 5: 漂移检测
|
|
241
|
+
├── pipeline.py # 全流水线编排
|
|
242
|
+
└── demo.py # 可运行演示
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## 设计原则
|
|
246
|
+
|
|
247
|
+
- **零耦合** — 仅依赖 numpy,不绑定任何框架、数据库或网络协议
|
|
248
|
+
- **不可变输出** — 每层返回 dataclass 结果对象(`FusionResult` / `RiskResult` / `DriftResult`)
|
|
249
|
+
- **关注点分离** — 每个模块单一职责,可独立导入使用
|
|
250
|
+
- **双模式 API** — streaming(逐帧 `process_logits`)和 batch(`process_events`)
|
|
251
|
+
- **配置集中** — 所有参数通过 `ConfidenceConfig` 统一管理,支持 dict / YAML / 直接构造
|
|
252
|
+
|
|
253
|
+
## 算法参考
|
|
254
|
+
|
|
255
|
+
- EMA 理论:Brown, R.G. (1956). *Exponential Smoothing for Predicting Demand*
|
|
256
|
+
- 当信号为随机游走时,Kalman 滤波退化为 EMA(最优线性因果估计器)
|
|
257
|
+
- Page-Hinkley 变点检测:Page (1954), Hinkley (1971)
|
|
258
|
+
- 总变差距离(TVD):离散分布间 L1 差的一半
|
|
259
|
+
|
|
260
|
+
## License
|
|
261
|
+
|
|
262
|
+
MIT
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# CertiFlow
|
|
2
|
+
|
|
3
|
+
零依赖(仅 numpy)的置信度融合与风险评分工具包。从分类器的原始 logits 到最终的风险评级,五层流水线即插即用。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# 从 PyPI 安装(发布后)
|
|
9
|
+
pip install certiflow
|
|
10
|
+
|
|
11
|
+
# 或从源码安装(开发模式)
|
|
12
|
+
git clone https://github.com/your-org/certiflow.git
|
|
13
|
+
cd certiflow
|
|
14
|
+
pip install -e .
|
|
15
|
+
|
|
16
|
+
# 安装可选 YAML 支持
|
|
17
|
+
pip install -e ".[yaml]"
|
|
18
|
+
|
|
19
|
+
# 安装开发依赖
|
|
20
|
+
pip install -e ".[dev]"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 快速开始
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from certiflow import ConfidencePipeline, ConfidenceConfig
|
|
27
|
+
|
|
28
|
+
config = ConfidenceConfig.from_dict({"alpha": 0.6, "output_threshold": 0.2})
|
|
29
|
+
pipeline = ConfidencePipeline(config)
|
|
30
|
+
|
|
31
|
+
# 逐帧流式处理
|
|
32
|
+
result = pipeline.process_logits(logits_array) # -> FusionResult | None
|
|
33
|
+
if result:
|
|
34
|
+
print(result.event_type, result.confidence)
|
|
35
|
+
|
|
36
|
+
# 批量处理事件列表
|
|
37
|
+
events = [
|
|
38
|
+
{"event_type": "alarm", "confidence": 0.85, "start_sec": 0.0, "end_sec": 1.0},
|
|
39
|
+
{"event_type": "alarm", "confidence": 0.90, "start_sec": 0.8, "end_sec": 1.8},
|
|
40
|
+
]
|
|
41
|
+
full = pipeline.process_events(events)
|
|
42
|
+
print(full.events_merged) # 合并后的事件
|
|
43
|
+
print(full.risk.score) # 风险分 [0, 1]
|
|
44
|
+
print(full.drift.detected) # 是否漂移
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 架构
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
┌─────────────┐
|
|
51
|
+
│ logits │
|
|
52
|
+
└──────┬──────┘
|
|
53
|
+
│
|
|
54
|
+
▼
|
|
55
|
+
┌────────────────────────┐
|
|
56
|
+
│ Layer 1 · softmax │ logits → 概率向量
|
|
57
|
+
└───────────┬────────────┘
|
|
58
|
+
│
|
|
59
|
+
▼
|
|
60
|
+
┌────────────────────────┐
|
|
61
|
+
│ Layer 2 · EMA 融合 │ p̃_t = α·p_t + (1−α)·p̃_{t−1}
|
|
62
|
+
└───────────┬────────────┘
|
|
63
|
+
│
|
|
64
|
+
┌─────┴──────┐
|
|
65
|
+
│ events │
|
|
66
|
+
└─────┬──────┘
|
|
67
|
+
│
|
|
68
|
+
▼
|
|
69
|
+
┌────────────────────────┐
|
|
70
|
+
│ Layer 3 · 事件合并 │ 合并同类重叠/相邻事件
|
|
71
|
+
└───────────┬────────────┘
|
|
72
|
+
│
|
|
73
|
+
▼
|
|
74
|
+
┌────────────────────────┐
|
|
75
|
+
│ Layer 4 · 风险评分 │ → low / medium / high
|
|
76
|
+
└───────────┬────────────┘
|
|
77
|
+
│
|
|
78
|
+
▼
|
|
79
|
+
┌────────────────────────┐
|
|
80
|
+
│ Layer 5 · 漂移检测 │ threshold / z_test / page_hinkley
|
|
81
|
+
└───────────┬────────────┘
|
|
82
|
+
│
|
|
83
|
+
▼
|
|
84
|
+
┌───────────┐
|
|
85
|
+
│ result │
|
|
86
|
+
└───────────┘
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
| 层级 | 模块 | 类 / 函数 | 作用 |
|
|
90
|
+
|------|------|-----------|------|
|
|
91
|
+
| 1 | `confidence_fusion` | `softmax()` | logits → 概率向量 |
|
|
92
|
+
| 2 | `confidence_fusion` | `ConfidenceFusion` | 因果 EMA 平滑:`p̃_t = α·p_t + (1−α)·p̃_{t−1}` |
|
|
93
|
+
| 3 | `event_merger` | `merge_events()` | 合并同类重叠/相邻事件,保留最高置信度 |
|
|
94
|
+
| 4 | `risk_engine` | `RiskEngine` | 从置信度统计量加权评分 → low / medium / high |
|
|
95
|
+
| 5 | `drift_monitor` | `DriftMonitor` | 在线漂移检测(threshold / z_test / page_hinkley) |
|
|
96
|
+
|
|
97
|
+
每层可独立使用,也可通过 `ConfidencePipeline` 一键串联。
|
|
98
|
+
|
|
99
|
+
## 配置
|
|
100
|
+
|
|
101
|
+
所有参数通过 `ConfidenceConfig` 统一管理,支持三种初始化方式:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
# 1. 直接构造
|
|
105
|
+
from certiflow import ConfidenceConfig, FusionConfig, DriftConfig
|
|
106
|
+
config = ConfidenceConfig(
|
|
107
|
+
fusion=FusionConfig(alpha=0.7, output_threshold=0.15),
|
|
108
|
+
drift=DriftConfig(mode="z_test"),
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
# 2. 扁平字典
|
|
112
|
+
config = ConfidenceConfig.from_dict({
|
|
113
|
+
"alpha": 0.6,
|
|
114
|
+
"output_threshold": 0.2,
|
|
115
|
+
"mode": "threshold",
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
# 3. YAML 文件(需安装 pyyaml)
|
|
119
|
+
config = ConfidenceConfig.from_yaml("config.yaml")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 核心参数
|
|
123
|
+
|
|
124
|
+
| 参数 | 默认值 | 说明 |
|
|
125
|
+
|------|--------|------|
|
|
126
|
+
| `alpha` | 0.6 | EMA 平滑因子 ∈ (0, 1],越大越偏向最新帧 |
|
|
127
|
+
| `output_threshold` | 0.2 | 融合后置信度低于此值的结果被过滤 |
|
|
128
|
+
| `base_threshold` | 0.05 | 单帧置信度低于此值时跳过(减少噪声) |
|
|
129
|
+
| `max_gap_sec` | 0.10 | 事件合并的最大时间间隔(秒) |
|
|
130
|
+
| `medium_threshold` | 0.40 | 风险等级 medium 的分界线 |
|
|
131
|
+
| `high_threshold` | 0.70 | 风险等级 high 的分界线 |
|
|
132
|
+
| `drift.mode` | `"threshold"` | 漂移检测模式:`threshold` / `z_test` / `page_hinkley` |
|
|
133
|
+
|
|
134
|
+
## 单独使用各层
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
import numpy as np
|
|
138
|
+
from certiflow import ConfidenceFusion, softmax, merge_events, RiskEngine, DriftMonitor
|
|
139
|
+
|
|
140
|
+
# Layer 1-2: 仅做置信度融合
|
|
141
|
+
fusion = ConfidenceFusion(alpha=0.6)
|
|
142
|
+
fusion.update(softmax(logits))
|
|
143
|
+
result = fusion.query()
|
|
144
|
+
|
|
145
|
+
# Layer 3: 仅做事件合并
|
|
146
|
+
merged = merge_events(raw_events)
|
|
147
|
+
|
|
148
|
+
# Layer 4: 仅做风险评分
|
|
149
|
+
risk = RiskEngine().assess(merged)
|
|
150
|
+
|
|
151
|
+
# Layer 5: 仅做漂移检测
|
|
152
|
+
monitor = DriftMonitor()
|
|
153
|
+
drift = monitor.evaluate(batch_1)
|
|
154
|
+
drift = monitor.evaluate(batch_2) # 与 batch_1 对比
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## 运行 Demo
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
python -m certiflow.demo
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
输出示例:
|
|
164
|
+
|
|
165
|
+
```
|
|
166
|
+
Synthetic data: 40 frames × 10 classes
|
|
167
|
+
|
|
168
|
+
============================================================
|
|
169
|
+
Layer 1-2: Softmax + EMA Fusion
|
|
170
|
+
============================================================
|
|
171
|
+
frame 15 → class=3 conf=0.2766 fused=True
|
|
172
|
+
frame 20 → class=3 conf=0.7439 fused=True
|
|
173
|
+
...
|
|
174
|
+
|
|
175
|
+
============================================================
|
|
176
|
+
Layer 3: Merged Events
|
|
177
|
+
============================================================
|
|
178
|
+
type=3 conf=0.7677 [7.50s – 15.50s] dur=8.00s
|
|
179
|
+
|
|
180
|
+
============================================================
|
|
181
|
+
Layer 4: Risk Assessment
|
|
182
|
+
============================================================
|
|
183
|
+
Score: 0.6152
|
|
184
|
+
Level: medium
|
|
185
|
+
Uncertainty: 0.5063
|
|
186
|
+
|
|
187
|
+
============================================================
|
|
188
|
+
Layer 5: Drift Detection
|
|
189
|
+
============================================================
|
|
190
|
+
Detected: False
|
|
191
|
+
Mode: threshold
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## 文件结构
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
CertiFlow/
|
|
198
|
+
├── pyproject.toml # 打包配置 (pip install -e .)
|
|
199
|
+
├── README.md
|
|
200
|
+
├── LICENSE
|
|
201
|
+
└── certiflow/ # Python 包
|
|
202
|
+
├── __init__.py # 包导出 & 版本号
|
|
203
|
+
├── confidence_config.py # 统一配置 dataclass
|
|
204
|
+
├── confidence_fusion.py # Layer 1-2: softmax + EMA
|
|
205
|
+
├── event_merger.py # Layer 3: 事件合并
|
|
206
|
+
├── risk_engine.py # Layer 4: 风险评分
|
|
207
|
+
├── drift_monitor.py # Layer 5: 漂移检测
|
|
208
|
+
├── pipeline.py # 全流水线编排
|
|
209
|
+
└── demo.py # 可运行演示
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## 设计原则
|
|
213
|
+
|
|
214
|
+
- **零耦合** — 仅依赖 numpy,不绑定任何框架、数据库或网络协议
|
|
215
|
+
- **不可变输出** — 每层返回 dataclass 结果对象(`FusionResult` / `RiskResult` / `DriftResult`)
|
|
216
|
+
- **关注点分离** — 每个模块单一职责,可独立导入使用
|
|
217
|
+
- **双模式 API** — streaming(逐帧 `process_logits`)和 batch(`process_events`)
|
|
218
|
+
- **配置集中** — 所有参数通过 `ConfidenceConfig` 统一管理,支持 dict / YAML / 直接构造
|
|
219
|
+
|
|
220
|
+
## 算法参考
|
|
221
|
+
|
|
222
|
+
- EMA 理论:Brown, R.G. (1956). *Exponential Smoothing for Predicting Demand*
|
|
223
|
+
- 当信号为随机游走时,Kalman 滤波退化为 EMA(最优线性因果估计器)
|
|
224
|
+
- Page-Hinkley 变点检测:Page (1954), Hinkley (1971)
|
|
225
|
+
- 总变差距离(TVD):离散分布间 L1 差的一半
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
MIT
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""CertiFlow — 可复用的置信度融合与风险评分工具包。
|
|
2
|
+
|
|
3
|
+
最小依赖:仅需 numpy。
|
|
4
|
+
|
|
5
|
+
作者:刘秉其
|
|
6
|
+
|
|
7
|
+
快速开始::
|
|
8
|
+
|
|
9
|
+
from certiflow import ConfidencePipeline, ConfidenceConfig
|
|
10
|
+
|
|
11
|
+
config = ConfidenceConfig.from_dict({"alpha": 0.6, "output_threshold": 0.2})
|
|
12
|
+
pipeline = ConfidencePipeline(config)
|
|
13
|
+
|
|
14
|
+
# 流式处理:逐帧输入
|
|
15
|
+
result = pipeline.process_logits(logits_array)
|
|
16
|
+
|
|
17
|
+
# 批量处理:传入事件列表
|
|
18
|
+
full = pipeline.process_events(events)
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
__author__ = "刘秉其"
|
|
22
|
+
__version__ = "0.1.0"
|
|
23
|
+
|
|
24
|
+
from .confidence_config import (
|
|
25
|
+
ConfidenceConfig,
|
|
26
|
+
DriftConfig,
|
|
27
|
+
FusionConfig,
|
|
28
|
+
MergerConfig,
|
|
29
|
+
RiskConfig,
|
|
30
|
+
)
|
|
31
|
+
from .confidence_fusion import ConfidenceFusion, FusionResult, softmax
|
|
32
|
+
from .drift_monitor import DriftMonitor, DriftResult
|
|
33
|
+
from .event_merger import merge_events
|
|
34
|
+
from .pipeline import ConfidencePipeline, PipelineResult
|
|
35
|
+
from .risk_engine import RiskEngine, RiskResult
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
# 配置
|
|
39
|
+
"ConfidenceConfig",
|
|
40
|
+
"FusionConfig",
|
|
41
|
+
"MergerConfig",
|
|
42
|
+
"RiskConfig",
|
|
43
|
+
"DriftConfig",
|
|
44
|
+
# 第 1-2 层
|
|
45
|
+
"softmax",
|
|
46
|
+
"ConfidenceFusion",
|
|
47
|
+
"FusionResult",
|
|
48
|
+
# 第 3 层
|
|
49
|
+
"merge_events",
|
|
50
|
+
# 第 4 层
|
|
51
|
+
"RiskEngine",
|
|
52
|
+
"RiskResult",
|
|
53
|
+
# 第 5 层
|
|
54
|
+
"DriftMonitor",
|
|
55
|
+
"DriftResult",
|
|
56
|
+
# 流水线
|
|
57
|
+
"ConfidencePipeline",
|
|
58
|
+
"PipelineResult",
|
|
59
|
+
]
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""统一配置模块。
|
|
2
|
+
|
|
3
|
+
为置信度融合流水线的各层提供集中化的参数管理,
|
|
4
|
+
支持直接构造、扁平字典与 YAML 文件三种初始化方式。
|
|
5
|
+
|
|
6
|
+
作者:刘秉其
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass, field, asdict
|
|
12
|
+
from typing import Any, Dict
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class FusionConfig:
|
|
17
|
+
"""EMA 置信度融合参数。"""
|
|
18
|
+
|
|
19
|
+
alpha: float = 0.6
|
|
20
|
+
output_threshold: float = 0.2
|
|
21
|
+
base_threshold: float = 0.05
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class MergerConfig:
|
|
26
|
+
"""事件合并参数。"""
|
|
27
|
+
|
|
28
|
+
max_gap_sec: float = 0.10
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass
|
|
32
|
+
class RiskConfig:
|
|
33
|
+
"""风险评分参数。"""
|
|
34
|
+
|
|
35
|
+
medium_threshold: float = 0.40
|
|
36
|
+
high_threshold: float = 0.70
|
|
37
|
+
review_uncertainty_threshold: float = 0.60
|
|
38
|
+
event_type_weights: Dict[str, float] = field(default_factory=dict)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class DriftConfig:
|
|
43
|
+
"""漂移检测参数。"""
|
|
44
|
+
|
|
45
|
+
enabled: bool = True
|
|
46
|
+
window_size: int = 200
|
|
47
|
+
min_samples: int = 30
|
|
48
|
+
confidence_shift_threshold: float = 0.20
|
|
49
|
+
type_shift_threshold: float = 0.35
|
|
50
|
+
mode: str = "threshold"
|
|
51
|
+
significance_level: float = 0.01
|
|
52
|
+
min_effect_size: float = 0.10
|
|
53
|
+
page_hinkley_delta: float = 0.005
|
|
54
|
+
page_hinkley_lambda: float = 0.05
|
|
55
|
+
page_hinkley_alpha: float = 0.999
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass
|
|
59
|
+
class ConfidenceConfig:
|
|
60
|
+
"""顶层配置,聚合所有子配置。
|
|
61
|
+
|
|
62
|
+
支持直接构造、从扁平字典构建或从 YAML 文件加载。
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
fusion: FusionConfig = field(default_factory=FusionConfig)
|
|
66
|
+
merger: MergerConfig = field(default_factory=MergerConfig)
|
|
67
|
+
risk: RiskConfig = field(default_factory=RiskConfig)
|
|
68
|
+
drift: DriftConfig = field(default_factory=DriftConfig)
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def alpha(self) -> float:
|
|
72
|
+
return self.fusion.alpha
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def output_threshold(self) -> float:
|
|
76
|
+
return self.fusion.output_threshold
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def base_threshold(self) -> float:
|
|
80
|
+
return self.fusion.base_threshold
|
|
81
|
+
|
|
82
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
83
|
+
return asdict(self)
|
|
84
|
+
|
|
85
|
+
@classmethod
|
|
86
|
+
def from_dict(cls, data: Dict[str, Any]) -> ConfidenceConfig:
|
|
87
|
+
"""从字典构建配置。
|
|
88
|
+
|
|
89
|
+
同时支持嵌套形式 ``{"fusion": {"alpha": 0.7}}``
|
|
90
|
+
和扁平形式 ``{"alpha": 0.7, "output_threshold": 0.3}``。
|
|
91
|
+
"""
|
|
92
|
+
if "fusion" in data and isinstance(data["fusion"], dict):
|
|
93
|
+
return cls(
|
|
94
|
+
fusion=FusionConfig(**{k: v for k, v in data.get("fusion", {}).items() if k in FusionConfig.__dataclass_fields__}),
|
|
95
|
+
merger=MergerConfig(**{k: v for k, v in data.get("merger", {}).items() if k in MergerConfig.__dataclass_fields__}),
|
|
96
|
+
risk=RiskConfig(**{k: v for k, v in data.get("risk", {}).items() if k in RiskConfig.__dataclass_fields__}),
|
|
97
|
+
drift=DriftConfig(**{k: v for k, v in data.get("drift", {}).items() if k in DriftConfig.__dataclass_fields__}),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
fusion_fields = {k for k in FusionConfig.__dataclass_fields__}
|
|
101
|
+
merger_fields = {k for k in MergerConfig.__dataclass_fields__}
|
|
102
|
+
risk_fields = {k for k in RiskConfig.__dataclass_fields__}
|
|
103
|
+
drift_fields = {k for k in DriftConfig.__dataclass_fields__}
|
|
104
|
+
|
|
105
|
+
return cls(
|
|
106
|
+
fusion=FusionConfig(**{k: v for k, v in data.items() if k in fusion_fields}),
|
|
107
|
+
merger=MergerConfig(**{k: v for k, v in data.items() if k in merger_fields}),
|
|
108
|
+
risk=RiskConfig(**{k: v for k, v in data.items() if k in risk_fields}),
|
|
109
|
+
drift=DriftConfig(**{k: v for k, v in data.items() if k in drift_fields}),
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
@classmethod
|
|
113
|
+
def from_yaml(cls, path: str) -> ConfidenceConfig:
|
|
114
|
+
"""从 YAML 文件加载配置(需安装 ``pyyaml``)。"""
|
|
115
|
+
import yaml
|
|
116
|
+
|
|
117
|
+
with open(path, "r", encoding="utf-8") as fh:
|
|
118
|
+
return cls.from_dict(yaml.safe_load(fh))
|