-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
382 lines (323 loc) · 13.7 KB
/
config.py
File metadata and controls
382 lines (323 loc) · 13.7 KB
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
"""
全局配置文件
定义项目的所有配置参数,包括路径、数据源、爬虫、数据库、可视化等
"""
import os
from pathlib import Path
from typing import Dict, List, Any, Optional
from dataclasses import dataclass, field
import yaml
# ==================== 路径配置 ====================
class PathConfig:
"""路径配置类"""
# 项目根目录
BASE_DIR = Path(__file__).parent
# 数据目录
DATA_DIR = BASE_DIR / "data"
CACHE_DIR = DATA_DIR / "cache"
EXPORT_DIR = DATA_DIR / "exports"
DATABASE_DIR = DATA_DIR / "database"
CHART_DIR = DATA_DIR / "charts"
MODELS_DIR = DATA_DIR / "models"
LOGS_DIR = BASE_DIR / "logs"
CONFIG_DIR = BASE_DIR / "config"
# 数据库文件
DATABASE_FILE = DATABASE_DIR / "national_stats.db"
REDIS_CACHE_FILE = CACHE_DIR / "redis_cache.rdb"
@classmethod
def ensure_dirs(cls):
"""确保所有必要目录存在"""
dirs = [
cls.DATA_DIR,
cls.CACHE_DIR,
cls.EXPORT_DIR,
cls.DATABASE_DIR,
cls.CHART_DIR,
cls.MODELS_DIR,
cls.LOGS_DIR,
cls.CONFIG_DIR
]
for d in dirs:
d.mkdir(parents=True, exist_ok=True)
# ==================== 爬虫配置 ====================
@dataclass
class ScraperConfig:
"""爬虫配置"""
timeout: int = 30
retry_times: int = 3
retry_delay: float = 1.0
request_delay: float = 0.5
max_workers: int = 10
cache_expire_hours: int = 24
user_agent: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
proxies: Dict[str, str] = field(default_factory=dict)
rate_limit: int = 10 # 每秒请求数限制
def __post_init__(self):
http_proxy = os.getenv("HTTP_PROXY") or os.getenv("http_proxy")
https_proxy = os.getenv("HTTPS_PROXY") or os.getenv("https_proxy")
if http_proxy:
self.proxies["http"] = http_proxy
if https_proxy:
self.proxies["https"] = https_proxy
# ==================== 数据库配置 ====================
@dataclass
class DatabaseConfig:
"""数据库配置"""
db_type: str = "sqlite"
connection_string: str = ""
pool_size: int = 10
max_overflow: int = 20
pool_timeout: int = 30
echo: bool = False
def __post_init__(self):
if not self.connection_string:
if self.db_type == "sqlite":
self.connection_string = f"sqlite:///{PathConfig.DATABASE_FILE}"
elif self.db_type == "postgresql":
self.connection_string = "postgresql://user:password@localhost/national_stats"
elif self.db_type == "mysql":
self.connection_string = "mysql+pymysql://user:password@localhost/national_stats"
# ==================== Redis缓存配置 ====================
@dataclass
class RedisConfig:
"""Redis缓存配置"""
host: str = "localhost"
port: int = 6379
db: int = 0
password: Optional[str] = None
expire_seconds: int = 86400 # 24小时
enabled: bool = True
# ==================== 指标配置 ====================
class IndicatorCategory:
PRODUCTION = "production"
DEMAND = "demand"
PRICE = "price"
EMPLOYMENT = "employment"
FINANCE = "finance"
TRADE = "trade"
FISCAL = "fiscal"
REAL_ESTATE = "real_estate"
INCOME = "income"
MONEY_SUPPLY = "money_supply"
INDUSTRY = "industry"
CONSUMER = "consumer"
class IndicatorFrequency:
DAILY = "daily"
WEEKLY = "weekly"
MONTHLY = "monthly"
QUARTERLY = "quarterly"
YEARLY = "yearly"
@dataclass
class IndicatorDefinition:
"""指标定义"""
code: str
name: str
category: str
frequency: str
unit: str
source: str
description: str = ""
is_leading: bool = False
is_coincident: bool = False
is_lagging: bool = False
parent_code: str = ""
weight: float = 1.0
api_endpoint: str = ""
data_format: str = "json"
def to_dict(self) -> Dict[str, Any]:
return {
"code": self.code, "name": self.name, "category": self.category,
"frequency": self.frequency, "unit": self.unit, "source": self.source,
"description": self.description, "is_leading": self.is_leading,
"is_coincident": self.is_coincident, "is_lagging": self.is_lagging,
"parent_code": self.parent_code, "weight": self.weight,
"api_endpoint": self.api_endpoint, "data_format": self.data_format
}
# 扩展指标库
class IndicatorLibrary:
NBS_INDICATORS: List[IndicatorDefinition] = [
# GDP相关
IndicatorDefinition("gdp", "国内生产总值", IndicatorCategory.PRODUCTION,
IndicatorFrequency.QUARTERLY, "亿元", "nbs",
is_coincident=True, api_endpoint="/api/gdp"),
IndicatorDefinition("gdp_yoy", "GDP同比增长率", IndicatorCategory.PRODUCTION,
IndicatorFrequency.QUARTERLY, "%", "nbs",
is_coincident=True, api_endpoint="/api/gdp/yoy"),
# 工业生产
IndicatorDefinition("industrial_yoy", "工业增加值同比增长率", IndicatorCategory.PRODUCTION,
IndicatorFrequency.MONTHLY, "%", "nbs", is_coincident=True, api_endpoint="/api/industry/value_added"),
# 价格指数
IndicatorDefinition("cpi", "居民消费价格指数", IndicatorCategory.PRICE,
IndicatorFrequency.MONTHLY, "上年同月=100", "nbs",
is_coincident=True, api_endpoint="/api/price/cpi"),
IndicatorDefinition("ppi", "工业生产者出厂价格指数", IndicatorCategory.PRICE,
IndicatorFrequency.MONTHLY, "上年同月=100", "nbs",
is_leading=True, api_endpoint="/api/price/ppi"),
# 投资消费
IndicatorDefinition("fixed_asset_investment_yoy", "固定资产投资同比增长率",
IndicatorCategory.DEMAND, IndicatorFrequency.MONTHLY, "%", "nbs",
is_leading=True, api_endpoint="/api/investment/fixed_asset"),
IndicatorDefinition("retail_sales_yoy", "社会消费品零售总额同比增长率",
IndicatorCategory.DEMAND, IndicatorFrequency.MONTHLY, "%", "nbs",
is_coincident=True, api_endpoint="/api/consumption/retail_sales"),
# 对外贸易
IndicatorDefinition("export", "出口总额", IndicatorCategory.TRADE,
IndicatorFrequency.MONTHLY, "亿美元", "customs",
is_coincident=True, api_endpoint="/api/trade/export"),
IndicatorDefinition("import", "进口总额", IndicatorCategory.TRADE,
IndicatorFrequency.MONTHLY, "亿美元", "customs",
is_coincident=True, api_endpoint="/api/trade/import"),
# 制造业PMI
IndicatorDefinition("pmi_manufacturing", "制造业PMI", IndicatorCategory.PRODUCTION,
IndicatorFrequency.MONTHLY, "%", "nbs",
is_leading=True, weight=1.5, api_endpoint="/api/pmi/manufacturing"),
# 货币供应量
IndicatorDefinition("m0", "货币供应量M0", IndicatorCategory.MONEY_SUPPLY,
IndicatorFrequency.MONTHLY, "万亿元", "pbc",
api_endpoint="/api/money_supply/m0"),
IndicatorDefinition("m1", "货币供应量M1", IndicatorCategory.MONEY_SUPPLY,
IndicatorFrequency.MONTHLY, "万亿元", "pbc",
api_endpoint="/api/money_supply/m1"),
IndicatorDefinition("m2", "货币供应量M2", IndicatorCategory.MONEY_SUPPLY,
IndicatorFrequency.MONTHLY, "万亿元", "pbc",
api_endpoint="/api/money_supply/m2"),
# 就业
IndicatorDefinition("urban_unemployment_rate", "城镇调查失业率", IndicatorCategory.EMPLOYMENT,
IndicatorFrequency.MONTHLY, "%", "nbs",
is_lagging=True, api_endpoint="/api/employment/unemployment_rate"),
# 房地产
IndicatorDefinition("house_price_index", "70个大中城市房价指数", IndicatorCategory.REAL_ESTATE,
IndicatorFrequency.MONTHLY, "上年同月=100", "nbs",
api_endpoint="/api/real_estate/price_index"),
]
@classmethod
def get_indicator(cls, code: str) -> IndicatorDefinition:
for ind in cls.NBS_INDICATORS:
if ind.code == code:
return ind
raise ValueError(f"未找到指标: {code}")
@classmethod
def get_leading_indicators(cls) -> List[IndicatorDefinition]:
return [ind for ind in cls.NBS_INDICATORS if ind.is_leading]
@classmethod
def get_coincident_indicators(cls) -> List[IndicatorDefinition]:
return [ind for ind in cls.NBS_INDICATORS if ind.is_coincident]
@classmethod
def get_lagging_indicators(cls) -> List[IndicatorDefinition]:
return [ind for ind in cls.NBS_INDICATORS if ind.is_lagging]
# ==================== 模型配置 ====================
@dataclass
class ModelConfig:
"""机器学习模型配置"""
# 时间序列预测
forecast_horizon: int = 24 # 预测期数
validation_split: float = 0.2 # 验证集比例
test_split: float = 0.1 # 测试集比例
# Prophet参数
prophet_seasonality_mode: str = "multiplicative"
prophet_yearly_seasonality: bool = True
prophet_weekly_seasonality: bool = False
prophet_daily_seasonality: bool = False
# ARIMA参数
arima_max_p: int = 5
arima_max_d: int = 2
arima_max_q: int = 5
# XGBoost参数
xgb_n_estimators: int = 100
xgb_max_depth: int = 6
xgb_learning_rate: float = 0.1
# 神经网络参数
nn_hidden_layers: List[int] = field(default_factory=lambda: [64, 32, 16])
nn_epochs: int = 100
nn_batch_size: int = 32
nn_learning_rate: float = 0.001
# ==================== 可视化配置 ====================
@dataclass
class VisualizationConfig:
figure_width: int = 12
figure_height: int = 8
chinese_font: str = "SimHei"
color_palette: str = "husl"
theme: str = "default"
interactive: bool = True
export_formats: List[str] = field(default_factory=lambda: ["png", "svg", "html"])
dpi: int = 300
# ==================== 分析配置 ====================
@dataclass
class AnalysisConfig:
ma_windows: List[int] = field(default_factory=lambda: [3, 6, 12])
forecast_years: int = 5
correlation_method: str = "pearson"
significance_level: float = 0.05
outlier_detection: bool = True
seasonal_decomposition: bool = True
# ==================== 日志配置 ====================
@dataclass
class LogConfig:
level: str = "INFO"
format: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
file_rotation: str = "10 MB"
retention_days: int = 30
enable_console: bool = True
# ==================== 性能配置 ====================
@dataclass
class PerformanceConfig:
"""性能优化配置"""
parallel_processing: bool = True
max_workers: int = 4
chunk_size: int = 1000
memory_limit_gb: float = 8.0
cache_enabled: bool = True
compression_enabled: bool = True
# ==================== 大数据配置 ====================
@dataclass
class BigDataConfig:
"""大数据处理配置"""
use_dask: bool = True
use_ray: bool = True
batch_size: int = 50
max_concurrent_requests: int = 20
distributed_computing: bool = True
data_partition_size: int = 10000
enable_caching: bool = True
# ==================== 全局配置管理 ====================
class Config:
paths = PathConfig
scraper = ScraperConfig()
database = DatabaseConfig()
redis = RedisConfig()
model = ModelConfig()
visualization = VisualizationConfig()
analysis = AnalysisConfig()
log = LogConfig()
performance = PerformanceConfig()
big_data = BigDataConfig()
@classmethod
def initialize(cls):
"""初始化配置"""
PathConfig.ensure_dirs()
# 设置matplotlib中文显示
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = [cls.visualization.chinese_font]
plt.rcParams["axes.unicode_minus"] = False
# 加载自定义配置文件(如果存在)
config_file = PathConfig.CONFIG_DIR / "config.yaml"
if config_file.exists():
cls._load_custom_config(config_file)
@classmethod
def _load_custom_config(cls, config_file: Path):
"""加载自定义配置文件"""
try:
with open(config_file, 'r', encoding='utf-8') as f:
custom_config = yaml.safe_load(f)
# 更新配置
for section, values in custom_config.items():
if hasattr(cls, section):
section_obj = getattr(cls, section)
for key, value in values.items():
if hasattr(section_obj, key):
setattr(section_obj, key, value)
except Exception as e:
print(f"加载自定义配置失败: {e}")
# 初始化配置
Config.initialize()