log
2026-07-11 2fa3e25841826aa4e8f95d988024f16149793368
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/env python3
"""
数据质量管理框架使用示例
展示各种使用场景和最佳实践
 
作者: 架构师
日期: 2026-03-02
"""
 
import sys
import os
 
# 添加项目根目录到Python路径
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)
 
from code.data_quality_framework import (
    DataQualityChecker,
    DataCleaner,
    DataValidator,
    DataQualityPipeline
)
import pickle
import json
import logging
 
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')
logger = logging.getLogger(__name__)
 
 
def example_1_basic_check():
    """示例1: 基本数据质量检查"""
    print("\n" + "=" * 70)
    print("示例1: 基本数据质量检查")
    print("=" * 70)
 
    # 加载数据
    try:
        with open('data/real_stock_data.pkl', 'rb') as f:
            data = pickle.load(f)
 
        print(f"\n✓ 成功加载数据: {len(data):,} 条记录")
 
        # 执行质量检查
        checker = DataQualityChecker()
        report = checker.check_data(data)
 
        # 输出结果
        print(f"\n数据质量结果: {'✓ 合格' if report.is_valid else '⚠ 不合格'}")
        print(f"\n问题 ({len(report.issues)} 个):")
        for issue in report.issues:
            print(f"  • {issue}")
 
        print(f"\n警告 ({len(report.warnings)} 个):")
        for warning in report.warnings[:5]:  # 只显示前5个
            print(f"  • {warning}")
 
        if 'stock_data_coverage' in report.stock_data_coverage:
            cov = report.stock_data_coverage
            print(f"\n数据覆盖度:")
            print(f"  • 股票总数: {cov['total_stocks']}")
            print(f"  • 平均每只股票记录数: {cov['avg_records_per_stock']}")
            print(f"  • 最少记录数: {cov['min_records']}")
            print(f"  • 最多记录数: {cov['max_records']}")
 
    except FileNotFoundError:
        print("✗ 未找到数据文件 data/real_stock_data.pkl")
 
 
def example_2_data_cleaning():
    """示例2: 数据清洗"""
    print("\n" + "=" * 70)
    print("示例2: 数据清洗")
    print("=" * 70)
 
    # 加载数据
    try:
        with open('data/real_stock_data.pkl', 'rb') as f:
            data = pickle.load(f)
 
        print(f"\n原始数据: {len(data):,} 条记录")
        print(f"列名: {list(data.columns)}")
 
        # 执行清洗
        cleaner = DataCleaner()
        cleaned_data = cleaner.clean_data(data)
 
        print(f"\n清洗后数据: {len(cleaned_data):,} 条记录")
        print(f"新列名: {list(cleaned_data.columns)}")
 
        # 显示清洗日志
        print(f"\n清洗步骤 ({len(cleaner.cleaning_log)} 个):")
        for step in cleaner.cleaning_log:
            print(f"  • {step}")
 
    except FileNotFoundError:
        print("✗ 未找到数据文件 data/real_stock_data.pkl")
    except Exception as e:
        print(f"✗ 清洗失败: {e}")
 
 
def example_3_full_pipeline():
    """示例3: 完整Pipeline"""
    print("\n" + "=" * 70)
    print("示例3: 完整数据质量Pipeline")
    print("=" * 70)
 
    # 加载数据
    try:
        with open('data/real_stock_data.pkl', 'rb') as f:
            data = pickle.load(f)
 
        print(f"\n✓ 成功加载数据: {len(data):,} 条记录")
 
        # 运行Pipeline
        pipeline = DataQualityPipeline()
        results = pipeline.run(data, source_name="example_data")
 
        # 输出结果
        print(f"\n{'='*70}")
        print("Pipeline执行结果:")
        print(f"{'='*70}")
        print(f"数据源: {results['source_name']}")
        print(f"原始记录: {results['raw_records']:,}")
        print(f"最终记录: {results['final_records']:,}")
        print(f"状态: {'✓ 成功' if results['is_valid'] else '⚠ 存在问题'}")
 
        if 'steps' in results:
            print("\n质量检查:")
            qc = results['steps']['quality_check']
            print(f"  是否合格: {qc['is_valid']}")
            print(f"  问题数: {qc['issues_count']}")
            print(f"  警告数: {qc['warnings_count']}")
 
            print("\n数据清洗:")
            cl = results['steps']['cleaning']
            print(f"  清洗后记录: {cl['cleaned_records']:,}")
            print(f"  清洗步骤: {cl['cleaning_steps']}")
 
        # 显示生成的文件
        print("\n生成的文件:")
        if os.path.exists('data/example_data_quality_report.json'):
            print("  ✓ data/example_data_quality_report.json")
        if os.path.exists('data/example_data_cleaning_log.json'):
            print("  ✓ data/example_data_cleaning_log.json")
        if os.path.exists('data/example_data_cleaned.pkl'):
            print("  ✓ data/example_data_cleaned.pkl")
 
    except FileNotFoundError:
        print("✗ 未找到数据文件 data/real_stock_data.pkl")
    except Exception as e:
        print(f"✗ Pipeline执行失败: {e}")
        import traceback
        print(traceback.format_exc())
 
 
def example_4_load_cleaned_data():
    """示例4: 加载和检查清洗后的数据"""
    print("\n" + "=" * 70)
    print("示例4: 加载清洗后的数据")
    print("=" * 70)
 
    # 尝试加载清洗后的数据
    cleaned_files = [
        'data/example_data_cleaned.pkl',
        'data/akshare_real_data_cleaned.pkl',
        'data/real_stock_data_cleaned.pkl'
    ]
 
    for filepath in cleaned_files:
        if os.path.exists(filepath):
            try:
                with open(filepath, 'rb') as f:
                    cleaned_data = pickle.load(f)
 
                print(f"\n✓ 成功加载: {filepath}")
                print(f"  记录数: {len(cleaned_data):,}")
                print(f"  股票数: {cleaned_data['ts_code'].nunique()}")
                print(f"  日期范围: {cleaned_data['trade_date'].min()} ~ {cleaned_data['trade_date'].max()}")
 
                # 检查字段
                print(f"  字段: {list(cleaned_data.columns)}")
 
                # 显示前几条记录
                print(f"\n  前5条记录:")
                print(cleaned_data.head().to_string(index=False))
 
                break
            except Exception as e:
                print(f"✗ 加载 {filepath} 失败: {e}")
    else:
        print("✗ 未找到清洗后的数据文件")
        print("  请先运行示例3生成清洗后的数据")
 
 
def example_5_multiple_sources():
    """示例5: 模拟多数据源交叉验证"""
    print("\n" + "=" * 70)
    print("示例5: 多数据源交叉验证(模拟)")
    print("=" * 70)
 
    # 加载数据
    try:
        with open('data/real_stock_data.pkl', 'rb') as f:
            main_data = pickle.load(f)
 
        print(f"\n✓ 成功加载数据: {len(main_data):,} 条记录")
 
        # 模拟第二个数据源(取主数据源的50%并添加一些噪声)
        print("\n创建模拟的第二数据源...")
        import numpy as np
 
        np.random.seed(42)
        sample_size = len(main_data) // 2
        sample_indices = np.random.choice(len(main_data), sample_size, replace=False)
 
        backup_data = main_data.iloc[sample_indices].copy()
 
        # 添加一些噪声
        backup_data['close'] = backup_data['close'] * (1 + np.random.randn(len(backup_data)) * 0.001)
 
        print(f"  模拟数据源记录数: {len(backup_data):,}")
 
        # 运行交叉验证
        validator = DataValidator()
        results = validator.cross_validate({
            "main_source": main_data,
            "backup_source": backup_data
        })
 
        # 输出结果
        print(f"\n交叉验证结果: {'✓ 通过' if results['is_valid'] else '⚠ 有问题'}")
 
        print(f"\n对比结果 ({len(results['comparisons'])} 个):")
        for comp in results['comparisons']:
            print(f"  {comp['source1']} vs {comp['source2']}:")
            print(f"    共同股票: {comp['stock_overlap']}")
            print(f"    共同记录: {comp['date_overlap']}")
 
            if 'price_diff_stats' in comp and comp['price_diff_stats']:
                print(f"    价格差异:")
                for col, stats in comp['price_diff_stats'].items():
                    print(f"      {col}: 平均差={stats['mean_diff']:.4f}, 最大差={stats['max_abs_diff']:.4f}")
 
            if 'issues' in comp and comp['issues']:
                print(f"    问题: {comp['issues']}")
 
        print(f"\n警告 ({len(results['warnings'])} 个):")
        for warning in results['warnings']:
            print(f"  • {warning}")
 
        print(f"\n建议:")
        for recommendation in results['recommendations']:
            print(f"  • {recommendation}")
 
    except FileNotFoundError:
        print("✗ 未找到数据文件 data/real_stock_data.pkl")
    except Exception as e:
        print(f"✗ 交叉验证失败: {e}")
        import traceback
        print(traceback.format_exc())
 
 
def example_6_custom_thresholds():
    """示例6: 自定义质量阈值"""
    print("\n" + "=" * 70)
    print("示例6: 自定义质量阈值(更严格的检查)")
    print("=" * 70)
 
    # 加载数据
    try:
        with open('data/real_stock_data.pkl', 'rb') as f:
            data = pickle.load(f)
 
        print(f"\n✓ 成功加载数据: {len(data):,} 条记录")
 
        # 定义更严格的质量阈值
        custom_thresholds = {
            'max_missing_rate': 0.02,  # 缺失率不超过2%(默认5%)
            'max_price_change': 0.15,  # 涨跌幅不超过15%(默认20%)
            'min_price': 2.0,           # 最低股价(默认1.0)
            'min_data_records_per_stock': 200  # 每只股票至少200条记录(默认100)
        }
 
        print("\n自定义质量阈值:")
        for key, value in custom_thresholds.items():
            print(f"  {key}: {value}")
 
        # 使用自定义阈值执行检查
        checker = DataQualityChecker(thresholds=custom_thresholds)
        report = checker.check_data(data)
 
        # 输出结果
        print(f"\n数据质量结果: {'✓ 合格' if report.is_valid else '⚠ 不合格'}")
 
        print(f"\n问题 ({len(report.issues)} 个):")
        for issue in report.issues:
            print(f"  • {issue}")
 
        print(f"\n警告 ({len(report.warnings)} 个):")
        for warning in report.warnings[:5]:
            print(f"  • {warning}")
 
    except FileNotFoundError:
        print("✗ 未找到数据文件 data/real_stock_data.pkl")
    except Exception as e:
        print(f"✗ 检查失败: {e}")
 
 
def example_7_analyze_cleaning_log():
    """示例7: 分析清洗日志"""
    print("\n" + "=" * 70)
    print("示例7: 分析清洗日志")
    print("=" * 70)
 
    # 尝试加载清洗日志
    log_files = [
        'data/example_data_cleaning_log.json',
        'data/akshare_real_data_cleaning_log.json'
    ]
 
    for filepath in log_files:
        if os.path.exists(filepath):
            try:
                with open(filepath, 'r', encoding='utf-8') as f:
                    log = json.load(f)
 
                print(f"\n✓ 成功加载: {filepath}")
                print(f"\n清洗统计:")
                print(f"  最终记录数: {log['final_records']:,}")
                print(f"  清洗步骤数: {len(log['cleaning_log'])}")
 
                print(f"\n清洗步骤详情:")
                for i, step in enumerate(log['cleaning_log'], 1):
                    print(f"\n  步骤 {i}: {step['step']}")
                    for key, value in step.items():
                        if key != 'step':
                            print(f"    {key}: {value}")
 
                break
            except Exception as e:
                print(f"✗ 加载 {filepath} 失败: {e}")
    else:
        print("✗ 未找到清洗日志文件")
        print("  请先运行示例3生成清洗日志")
 
 
def example_8_batch_processing():
    """示例8: 分批处理大数据集"""
    print("\n" + "=" * 70)
    print("示例8: 分批处理大数据集")
    print("=" * 70)
 
    # 加载数据
    try:
        with open('data/real_stock_data.pkl', 'rb') as f:
            data = pickle.load(f)
 
        print(f"\n✓ 成功加载数据: {len(data):,} 条记录")
 
        # 分批大小
        batch_size = 100000
        print(f"\n分批大小: {batch_size:,}")
 
        # 清洗器
        cleaner = DataCleaner()
 
        # 分批处理
        cleaned_batches = []
        total_batches = (len(data) + batch_size - 1) // batch_size
 
        print(f"\n开始分批处理...")
        print(f"总批次数: {total_batches}")
 
        for i in range(0, len(data), batch_size):
            batch_num = i // batch_size + 1
            print(f"  处理第 {batch_num}/{total_batches} 批 ({len(data[i:i+batch_size]):,} 条)...")
 
            try:
                cleaned_batch = cleaner.clean_data(data.iloc[i:i+batch_size])
                cleaned_batches.append(cleaned_batch)
                print(f"    ✓ 完成")
            except Exception as e:
                print(f"    ✗ 失败: {e}")
 
        # 合并结果
        if cleaned_batches:
            final_data = pd.concat(cleaned_batches, ignore_index=True)
            print(f"\n✓ 分批处理完成: {len(final_data):,} 条记录")
        else:
            print("\n✗ 没有成功处理任何批次")
 
    except FileNotFoundError:
        print("✗ 未找到数据文件 data/real_stock_data.pkl")
    except Exception as e:
        print(f"✗ 分批处理失败: {e}")
        import traceback
        print(traceback.format_exc())
 
 
def main():
    """主函数 - 运行所有示例"""
    print("\n" + "=" * 70)
    print("数据质量管理框架 - 使用示例集合")
    print("=" * 70)
 
    examples = [
        ("1. 基本数据质量检查", example_1_basic_check),
        ("2. 数据清洗", example_2_data_cleaning),
        ("3. 完整Pipeline", example_3_full_pipeline),
        ("4. 加载清洗后的数据", example_4_load_cleaned_data),
        ("5. 多数据源交叉验证(模拟)", example_5_multiple_sources),
        ("6. 自定义质量阈值", example_6_custom_thresholds),
        ("7. 分析清洗日志", example_7_analyze_cleaning_log),
        ("8. 分批处理大数据集", example_8_batch_processing),
    ]
 
    print("\n可用示例:")
    for i, (desc, _) in enumerate(examples, 1):
        print(f"  {i}. {desc}")
 
    print(f"\n  0. 运行所有示例")
    print(f"  q. 退出")
 
    while True:
        choice = input("\n请选择示例 (0-8, q): ").strip()
 
        if choice.lower() == 'q':
            print("\n👋 再见!")
            break
        elif choice == '0':
            print("\n运行所有示例...")
            for _, func in examples:
                try:
                    func()
                except Exception as e:
                    print(f"\n✗ 示例执行失败: {e}")
        elif choice.isdigit() and 1 <= int(choice) <= len(examples):
            idx = int(choice) - 1
            try:
                examples[idx][1]()
            except Exception as e:
                print(f"\n✗ 示例执行失败: {e}")
        else:
            print("\n⚠ 无效选择,请重新输入")
 
 
if __name__ == '__main__':
    import pandas as pd
    main()