aigroup-econ-mcp 1.3.3__py3-none-any.whl → 1.4.3__py3-none-any.whl
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.
- .gitignore +253 -0
- PKG-INFO +710 -0
- README.md +672 -0
- __init__.py +14 -0
- aigroup_econ_mcp-1.4.3.dist-info/METADATA +710 -0
- aigroup_econ_mcp-1.4.3.dist-info/RECORD +92 -0
- aigroup_econ_mcp-1.4.3.dist-info/entry_points.txt +2 -0
- aigroup_econ_mcp-1.4.3.dist-info/licenses/LICENSE +21 -0
- cli.py +28 -0
- econometrics/README.md +18 -0
- econometrics/__init__.py +191 -0
- econometrics/advanced_methods/modern_computing_machine_learning/__init__.py +0 -0
- econometrics/basic_parametric_estimation/__init__.py +31 -0
- econometrics/basic_parametric_estimation/gmm/__init__.py +13 -0
- econometrics/basic_parametric_estimation/gmm/gmm_model.py +256 -0
- econometrics/basic_parametric_estimation/mle/__init__.py +13 -0
- econometrics/basic_parametric_estimation/mle/mle_model.py +241 -0
- econometrics/basic_parametric_estimation/ols/__init__.py +13 -0
- econometrics/basic_parametric_estimation/ols/ols_model.py +141 -0
- econometrics/causal_inference/causal_identification_strategy/__init__.py +0 -0
- econometrics/missing_data/missing_data_measurement_error/__init__.py +0 -0
- econometrics/model_specification_diagnostics_robust_inference/README.md +173 -0
- econometrics/model_specification_diagnostics_robust_inference/__init__.py +78 -0
- econometrics/model_specification_diagnostics_robust_inference/diagnostic_tests/__init__.py +20 -0
- econometrics/model_specification_diagnostics_robust_inference/diagnostic_tests/diagnostic_tests_model.py +149 -0
- econometrics/model_specification_diagnostics_robust_inference/generalized_least_squares/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/generalized_least_squares/gls_model.py +130 -0
- econometrics/model_specification_diagnostics_robust_inference/model_selection/__init__.py +18 -0
- econometrics/model_specification_diagnostics_robust_inference/model_selection/model_selection_model.py +286 -0
- econometrics/model_specification_diagnostics_robust_inference/regularization/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/regularization/regularization_model.py +177 -0
- econometrics/model_specification_diagnostics_robust_inference/robust_errors/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/robust_errors/robust_errors_model.py +122 -0
- econometrics/model_specification_diagnostics_robust_inference/simultaneous_equations/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/simultaneous_equations/simultaneous_equations_model.py +246 -0
- econometrics/model_specification_diagnostics_robust_inference/weighted_least_squares/__init__.py +15 -0
- econometrics/model_specification_diagnostics_robust_inference/weighted_least_squares/wls_model.py +127 -0
- econometrics/nonparametric/nonparametric_semiparametric_methods/__init__.py +0 -0
- econometrics/spatial_econometrics/spatial_econometrics_new/__init__.py +0 -0
- econometrics/specific_data_modeling/micro_discrete_limited_data/__init__.py +0 -0
- econometrics/specific_data_modeling/survival_duration_data/__init__.py +0 -0
- econometrics/specific_data_modeling/time_series_panel_data/__init__.py +143 -0
- econometrics/specific_data_modeling/time_series_panel_data/arima_model.py +104 -0
- econometrics/specific_data_modeling/time_series_panel_data/cointegration_vecm.py +334 -0
- econometrics/specific_data_modeling/time_series_panel_data/dynamic_panel_models.py +653 -0
- econometrics/specific_data_modeling/time_series_panel_data/exponential_smoothing.py +176 -0
- econometrics/specific_data_modeling/time_series_panel_data/garch_model.py +198 -0
- econometrics/specific_data_modeling/time_series_panel_data/panel_diagnostics.py +125 -0
- econometrics/specific_data_modeling/time_series_panel_data/panel_var.py +60 -0
- econometrics/specific_data_modeling/time_series_panel_data/structural_break_tests.py +87 -0
- econometrics/specific_data_modeling/time_series_panel_data/time_varying_parameter_models.py +106 -0
- econometrics/specific_data_modeling/time_series_panel_data/unit_root_tests.py +204 -0
- econometrics/specific_data_modeling/time_series_panel_data/var_svar_model.py +372 -0
- econometrics/statistical_inference/statistical_inference_techniques/__init__.py +0 -0
- econometrics/statistics/distribution_decomposition_methods/__init__.py +0 -0
- econometrics/tests/basic_parametric_estimation_tests/__init__.py +3 -0
- econometrics/tests/basic_parametric_estimation_tests/test_gmm.py +128 -0
- econometrics/tests/basic_parametric_estimation_tests/test_mle.py +127 -0
- econometrics/tests/basic_parametric_estimation_tests/test_ols.py +100 -0
- econometrics/tests/model_specification_diagnostics_tests/__init__.py +3 -0
- econometrics/tests/model_specification_diagnostics_tests/test_diagnostic_tests.py +86 -0
- econometrics/tests/model_specification_diagnostics_tests/test_robust_errors.py +89 -0
- econometrics/tests/specific_data_modeling_tests/__init__.py +3 -0
- econometrics/tests/specific_data_modeling_tests/test_arima.py +98 -0
- econometrics/tests/specific_data_modeling_tests/test_dynamic_panel.py +198 -0
- econometrics/tests/specific_data_modeling_tests/test_exponential_smoothing.py +105 -0
- econometrics/tests/specific_data_modeling_tests/test_garch.py +118 -0
- econometrics/tests/specific_data_modeling_tests/test_unit_root.py +156 -0
- econometrics/tests/specific_data_modeling_tests/test_var.py +124 -0
- prompts/__init__.py +0 -0
- prompts/analysis_guides.py +43 -0
- pyproject.toml +78 -0
- resources/MCP_MASTER_GUIDE.md +422 -0
- resources/MCP_TOOLS_DATA_FORMAT_GUIDE.md +185 -0
- resources/__init__.py +0 -0
- server.py +83 -0
- tools/README.md +88 -0
- tools/__init__.py +45 -0
- tools/data_loader.py +213 -0
- tools/decorators.py +38 -0
- tools/econometrics_adapter.py +286 -0
- tools/mcp_tool_groups/__init__.py +1 -0
- tools/mcp_tool_groups/basic_parametric_tools.py +173 -0
- tools/mcp_tool_groups/model_specification_tools.py +402 -0
- tools/mcp_tool_groups/time_series_tools.py +494 -0
- tools/mcp_tools_registry.py +114 -0
- tools/model_specification_adapter.py +369 -0
- tools/output_formatter.py +563 -0
- tools/time_series_panel_data_adapter.py +858 -0
- tools/time_series_panel_data_tools.py +65 -0
- aigroup_econ_mcp/__init__.py +0 -19
- aigroup_econ_mcp/cli.py +0 -82
- aigroup_econ_mcp/config.py +0 -561
- aigroup_econ_mcp/server.py +0 -452
- aigroup_econ_mcp/tools/__init__.py +0 -19
- aigroup_econ_mcp/tools/base.py +0 -470
- aigroup_econ_mcp/tools/cache.py +0 -533
- aigroup_econ_mcp/tools/data_loader.py +0 -195
- aigroup_econ_mcp/tools/file_parser.py +0 -1027
- aigroup_econ_mcp/tools/machine_learning.py +0 -60
- aigroup_econ_mcp/tools/ml_ensemble.py +0 -210
- aigroup_econ_mcp/tools/ml_evaluation.py +0 -272
- aigroup_econ_mcp/tools/ml_models.py +0 -54
- aigroup_econ_mcp/tools/ml_regularization.py +0 -186
- aigroup_econ_mcp/tools/monitoring.py +0 -555
- aigroup_econ_mcp/tools/optimized_example.py +0 -229
- aigroup_econ_mcp/tools/panel_data.py +0 -619
- aigroup_econ_mcp/tools/regression.py +0 -214
- aigroup_econ_mcp/tools/statistics.py +0 -154
- aigroup_econ_mcp/tools/time_series.py +0 -698
- aigroup_econ_mcp/tools/timeout.py +0 -283
- aigroup_econ_mcp/tools/tool_descriptions.py +0 -410
- aigroup_econ_mcp/tools/tool_handlers.py +0 -1016
- aigroup_econ_mcp/tools/tool_registry.py +0 -478
- aigroup_econ_mcp/tools/validation.py +0 -482
- aigroup_econ_mcp-1.3.3.dist-info/METADATA +0 -525
- aigroup_econ_mcp-1.3.3.dist-info/RECORD +0 -30
- aigroup_econ_mcp-1.3.3.dist-info/entry_points.txt +0 -2
- /aigroup_econ_mcp-1.3.3.dist-info/licenses/LICENSE → /LICENSE +0 -0
- {aigroup_econ_mcp-1.3.3.dist-info → aigroup_econ_mcp-1.4.3.dist-info}/WHEEL +0 -0
aigroup_econ_mcp/config.py
DELETED
|
@@ -1,561 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
配置管理系统
|
|
3
|
-
提供统一的配置管理和环境设置功能
|
|
4
|
-
"""
|
|
5
|
-
|
|
6
|
-
import os
|
|
7
|
-
import json
|
|
8
|
-
import yaml
|
|
9
|
-
from typing import Dict, Any, Optional, List, Union
|
|
10
|
-
from pathlib import Path
|
|
11
|
-
from dataclasses import dataclass, field
|
|
12
|
-
from enum import Enum
|
|
13
|
-
import logging
|
|
14
|
-
from dotenv import load_dotenv
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
class ConfigSource(Enum):
|
|
18
|
-
"""配置来源"""
|
|
19
|
-
ENV = "environment"
|
|
20
|
-
FILE = "file"
|
|
21
|
-
DEFAULT = "default"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@dataclass
|
|
25
|
-
class ConfigValue:
|
|
26
|
-
"""配置值"""
|
|
27
|
-
value: Any
|
|
28
|
-
source: ConfigSource
|
|
29
|
-
key: str
|
|
30
|
-
description: str = ""
|
|
31
|
-
required: bool = False
|
|
32
|
-
validated: bool = False
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
class ConfigurationError(Exception):
|
|
36
|
-
"""配置错误"""
|
|
37
|
-
pass
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
class ConfigManager:
|
|
41
|
-
"""
|
|
42
|
-
配置管理器
|
|
43
|
-
提供统一的配置加载、验证和管理功能
|
|
44
|
-
"""
|
|
45
|
-
|
|
46
|
-
def __init__(self, config_dir: Optional[str] = None, env_file: Optional[str] = None):
|
|
47
|
-
"""
|
|
48
|
-
初始化配置管理器
|
|
49
|
-
|
|
50
|
-
Args:
|
|
51
|
-
config_dir: 配置文件目录
|
|
52
|
-
env_file: 环境变量文件路径
|
|
53
|
-
"""
|
|
54
|
-
self.config_dir = Path(config_dir) if config_dir else Path.cwd() / "config"
|
|
55
|
-
self.env_file = Path(env_file) if env_file else Path.cwd() / ".env"
|
|
56
|
-
|
|
57
|
-
# 创建配置目录(如果不存在)
|
|
58
|
-
self.config_dir.mkdir(exist_ok=True)
|
|
59
|
-
|
|
60
|
-
# 加载环境变量
|
|
61
|
-
self._load_environment_variables()
|
|
62
|
-
|
|
63
|
-
# 配置存储
|
|
64
|
-
self._config: Dict[str, ConfigValue] = {}
|
|
65
|
-
self._defaults: Dict[str, Any] = {}
|
|
66
|
-
self._validators: Dict[str, callable] = {}
|
|
67
|
-
|
|
68
|
-
# 日志设置
|
|
69
|
-
self._setup_logging()
|
|
70
|
-
|
|
71
|
-
def _load_environment_variables(self) -> None:
|
|
72
|
-
"""加载环境变量"""
|
|
73
|
-
if self.env_file.exists():
|
|
74
|
-
load_dotenv(self.env_file)
|
|
75
|
-
else:
|
|
76
|
-
load_dotenv()
|
|
77
|
-
|
|
78
|
-
def _setup_logging(self) -> None:
|
|
79
|
-
"""设置日志"""
|
|
80
|
-
logging.basicConfig(
|
|
81
|
-
level=os.getenv("LOG_LEVEL", "INFO"),
|
|
82
|
-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
|
83
|
-
)
|
|
84
|
-
self.logger = logging.getLogger("aigroup_econ_mcp")
|
|
85
|
-
|
|
86
|
-
def set_default(self, key: str, value: Any, description: str = "", required: bool = False) -> None:
|
|
87
|
-
"""
|
|
88
|
-
设置默认配置值
|
|
89
|
-
|
|
90
|
-
Args:
|
|
91
|
-
key: 配置键
|
|
92
|
-
value: 默认值
|
|
93
|
-
description: 配置描述
|
|
94
|
-
required: 是否必需
|
|
95
|
-
"""
|
|
96
|
-
self._defaults[key] = {
|
|
97
|
-
"value": value,
|
|
98
|
-
"description": description,
|
|
99
|
-
"required": required
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
def add_validator(self, key: str, validator: callable) -> None:
|
|
103
|
-
"""
|
|
104
|
-
添加配置验证器
|
|
105
|
-
|
|
106
|
-
Args:
|
|
107
|
-
key: 配置键
|
|
108
|
-
validator: 验证函数
|
|
109
|
-
"""
|
|
110
|
-
self._validators[key] = validator
|
|
111
|
-
|
|
112
|
-
def load_from_file(self, filename: str, file_type: str = "auto") -> None:
|
|
113
|
-
"""
|
|
114
|
-
从文件加载配置
|
|
115
|
-
|
|
116
|
-
Args:
|
|
117
|
-
filename: 文件名
|
|
118
|
-
file_type: 文件类型 (json, yaml, auto)
|
|
119
|
-
"""
|
|
120
|
-
file_path = self.config_dir / filename
|
|
121
|
-
|
|
122
|
-
if not file_path.exists():
|
|
123
|
-
self.logger.warning(f"配置文件不存在: {file_path}")
|
|
124
|
-
return
|
|
125
|
-
|
|
126
|
-
try:
|
|
127
|
-
if file_type == "auto":
|
|
128
|
-
if filename.endswith(".json"):
|
|
129
|
-
file_type = "json"
|
|
130
|
-
elif filename.endswith((".yaml", ".yml")):
|
|
131
|
-
file_type = "yaml"
|
|
132
|
-
else:
|
|
133
|
-
raise ConfigurationError(f"无法自动识别文件类型: {filename}")
|
|
134
|
-
|
|
135
|
-
if file_type == "json":
|
|
136
|
-
with open(file_path, 'r', encoding='utf-8') as f:
|
|
137
|
-
config_data = json.load(f)
|
|
138
|
-
elif file_type == "yaml":
|
|
139
|
-
with open(file_path, 'r', encoding='utf-8') as f:
|
|
140
|
-
config_data = yaml.safe_load(f)
|
|
141
|
-
else:
|
|
142
|
-
raise ConfigurationError(f"不支持的文件类型: {file_type}")
|
|
143
|
-
|
|
144
|
-
# 更新配置
|
|
145
|
-
self._update_config_from_dict(config_data, ConfigSource.FILE)
|
|
146
|
-
|
|
147
|
-
self.logger.info(f"从文件加载配置: {file_path}")
|
|
148
|
-
|
|
149
|
-
except Exception as e:
|
|
150
|
-
raise ConfigurationError(f"加载配置文件失败: {file_path}, 错误: {e}")
|
|
151
|
-
|
|
152
|
-
def load_from_environment(self, prefix: str = "AIGROUP_ECON_") -> None:
|
|
153
|
-
"""
|
|
154
|
-
从环境变量加载配置
|
|
155
|
-
|
|
156
|
-
Args:
|
|
157
|
-
prefix: 环境变量前缀
|
|
158
|
-
"""
|
|
159
|
-
env_config = {}
|
|
160
|
-
|
|
161
|
-
for env_key, env_value in os.environ.items():
|
|
162
|
-
if env_key.startswith(prefix):
|
|
163
|
-
# 移除前缀并转换为小写
|
|
164
|
-
config_key = env_key[len(prefix):].lower()
|
|
165
|
-
|
|
166
|
-
# 尝试解析值
|
|
167
|
-
try:
|
|
168
|
-
# 尝试解析为JSON
|
|
169
|
-
parsed_value = json.loads(env_value)
|
|
170
|
-
except (json.JSONDecodeError, ValueError):
|
|
171
|
-
# 如果不是JSON,保持原样
|
|
172
|
-
parsed_value = env_value
|
|
173
|
-
|
|
174
|
-
env_config[config_key] = parsed_value
|
|
175
|
-
|
|
176
|
-
self._update_config_from_dict(env_config, ConfigSource.ENV)
|
|
177
|
-
self.logger.info(f"从环境变量加载配置,前缀: {prefix}")
|
|
178
|
-
|
|
179
|
-
def _update_config_from_dict(self, config_dict: Dict[str, Any], source: ConfigSource) -> None:
|
|
180
|
-
"""
|
|
181
|
-
从字典更新配置
|
|
182
|
-
|
|
183
|
-
Args:
|
|
184
|
-
config_dict: 配置字典
|
|
185
|
-
source: 配置来源
|
|
186
|
-
"""
|
|
187
|
-
for key, value in config_dict.items():
|
|
188
|
-
description = self._defaults.get(key, {}).get("description", "")
|
|
189
|
-
required = self._defaults.get(key, {}).get("required", False)
|
|
190
|
-
|
|
191
|
-
# 验证配置值
|
|
192
|
-
if key in self._validators:
|
|
193
|
-
try:
|
|
194
|
-
value = self._validators[key](value)
|
|
195
|
-
validated = True
|
|
196
|
-
except Exception as e:
|
|
197
|
-
if required:
|
|
198
|
-
raise ConfigurationError(f"配置验证失败: {key}={value}, 错误: {e}")
|
|
199
|
-
else:
|
|
200
|
-
self.logger.warning(f"配置验证失败: {key}={value}, 错误: {e}")
|
|
201
|
-
validated = False
|
|
202
|
-
else:
|
|
203
|
-
validated = False
|
|
204
|
-
|
|
205
|
-
self._config[key] = ConfigValue(
|
|
206
|
-
value=value,
|
|
207
|
-
source=source,
|
|
208
|
-
key=key,
|
|
209
|
-
description=description,
|
|
210
|
-
required=required,
|
|
211
|
-
validated=validated
|
|
212
|
-
)
|
|
213
|
-
|
|
214
|
-
def get(self, key: str, default: Any = None) -> Any:
|
|
215
|
-
"""
|
|
216
|
-
获取配置值
|
|
217
|
-
|
|
218
|
-
Args:
|
|
219
|
-
key: 配置键
|
|
220
|
-
default: 默认值
|
|
221
|
-
|
|
222
|
-
Returns:
|
|
223
|
-
Any: 配置值
|
|
224
|
-
"""
|
|
225
|
-
if key in self._config:
|
|
226
|
-
return self._config[key].value
|
|
227
|
-
|
|
228
|
-
# 检查默认值
|
|
229
|
-
if key in self._defaults:
|
|
230
|
-
default_value = self._defaults[key]["value"]
|
|
231
|
-
self._config[key] = ConfigValue(
|
|
232
|
-
value=default_value,
|
|
233
|
-
source=ConfigSource.DEFAULT,
|
|
234
|
-
key=key,
|
|
235
|
-
description=self._defaults[key]["description"],
|
|
236
|
-
required=self._defaults[key]["required"],
|
|
237
|
-
validated=False
|
|
238
|
-
)
|
|
239
|
-
return default_value
|
|
240
|
-
|
|
241
|
-
# 使用提供的默认值
|
|
242
|
-
if default is not None:
|
|
243
|
-
return default
|
|
244
|
-
|
|
245
|
-
# 如果配置不存在且没有默认值
|
|
246
|
-
if key in self._defaults and self._defaults[key]["required"]:
|
|
247
|
-
raise ConfigurationError(f"必需的配置项缺失: {key}")
|
|
248
|
-
|
|
249
|
-
return None
|
|
250
|
-
|
|
251
|
-
def set(self, key: str, value: Any, description: str = "") -> None:
|
|
252
|
-
"""
|
|
253
|
-
设置配置值
|
|
254
|
-
|
|
255
|
-
Args:
|
|
256
|
-
key: 配置键
|
|
257
|
-
value: 配置值
|
|
258
|
-
description: 配置描述
|
|
259
|
-
"""
|
|
260
|
-
# 验证配置值
|
|
261
|
-
validated = False
|
|
262
|
-
if key in self._validators:
|
|
263
|
-
try:
|
|
264
|
-
value = self._validators[key](value)
|
|
265
|
-
validated = True
|
|
266
|
-
except Exception as e:
|
|
267
|
-
raise ConfigurationError(f"配置验证失败: {key}={value}, 错误: {e}")
|
|
268
|
-
|
|
269
|
-
required = self._defaults.get(key, {}).get("required", False)
|
|
270
|
-
|
|
271
|
-
self._config[key] = ConfigValue(
|
|
272
|
-
value=value,
|
|
273
|
-
source=ConfigSource.FILE, # 手动设置的配置视为文件配置
|
|
274
|
-
key=key,
|
|
275
|
-
description=description,
|
|
276
|
-
required=required,
|
|
277
|
-
validated=validated
|
|
278
|
-
)
|
|
279
|
-
|
|
280
|
-
def save_to_file(self, filename: str, file_type: str = "json") -> None:
|
|
281
|
-
"""
|
|
282
|
-
保存配置到文件
|
|
283
|
-
|
|
284
|
-
Args:
|
|
285
|
-
filename: 文件名
|
|
286
|
-
file_type: 文件类型 (json, yaml)
|
|
287
|
-
"""
|
|
288
|
-
file_path = self.config_dir / filename
|
|
289
|
-
|
|
290
|
-
try:
|
|
291
|
-
config_dict = {}
|
|
292
|
-
for key, config_value in self._config.items():
|
|
293
|
-
if config_value.source != ConfigSource.ENV: # 不保存环境变量配置
|
|
294
|
-
config_dict[key] = config_value.value
|
|
295
|
-
|
|
296
|
-
if file_type == "json":
|
|
297
|
-
with open(file_path, 'w', encoding='utf-8') as f:
|
|
298
|
-
json.dump(config_dict, f, indent=2, ensure_ascii=False)
|
|
299
|
-
elif file_type == "yaml":
|
|
300
|
-
with open(file_path, 'w', encoding='utf-8') as f:
|
|
301
|
-
yaml.dump(config_dict, f, default_flow_style=False, allow_unicode=True)
|
|
302
|
-
else:
|
|
303
|
-
raise ConfigurationError(f"不支持的文件类型: {file_type}")
|
|
304
|
-
|
|
305
|
-
self.logger.info(f"配置保存到文件: {file_path}")
|
|
306
|
-
|
|
307
|
-
except Exception as e:
|
|
308
|
-
raise ConfigurationError(f"保存配置文件失败: {file_path}, 错误: {e}")
|
|
309
|
-
|
|
310
|
-
def validate(self) -> List[str]:
|
|
311
|
-
"""
|
|
312
|
-
验证配置
|
|
313
|
-
|
|
314
|
-
Returns:
|
|
315
|
-
List[str]: 验证错误列表
|
|
316
|
-
"""
|
|
317
|
-
errors = []
|
|
318
|
-
|
|
319
|
-
# 检查必需配置
|
|
320
|
-
for key, default_info in self._defaults.items():
|
|
321
|
-
if default_info["required"] and key not in self._config:
|
|
322
|
-
errors.append(f"必需的配置项缺失: {key}")
|
|
323
|
-
|
|
324
|
-
# 验证配置值
|
|
325
|
-
for key, config_value in self._config.items():
|
|
326
|
-
if key in self._validators and not config_value.validated:
|
|
327
|
-
try:
|
|
328
|
-
self._validators[key](config_value.value)
|
|
329
|
-
config_value.validated = True
|
|
330
|
-
except Exception as e:
|
|
331
|
-
errors.append(f"配置验证失败: {key}={config_value.value}, 错误: {e}")
|
|
332
|
-
|
|
333
|
-
return errors
|
|
334
|
-
|
|
335
|
-
def get_config_info(self) -> Dict[str, Any]:
|
|
336
|
-
"""
|
|
337
|
-
获取配置信息
|
|
338
|
-
|
|
339
|
-
Returns:
|
|
340
|
-
Dict[str, Any]: 配置信息
|
|
341
|
-
"""
|
|
342
|
-
config_info = {}
|
|
343
|
-
|
|
344
|
-
for key, config_value in self._config.items():
|
|
345
|
-
config_info[key] = {
|
|
346
|
-
"value": config_value.value,
|
|
347
|
-
"source": config_value.source.value,
|
|
348
|
-
"description": config_value.description,
|
|
349
|
-
"required": config_value.required,
|
|
350
|
-
"validated": config_value.validated
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
return config_info
|
|
354
|
-
|
|
355
|
-
def reload(self) -> None:
|
|
356
|
-
"""重新加载配置"""
|
|
357
|
-
self._config.clear()
|
|
358
|
-
self._load_environment_variables()
|
|
359
|
-
self.load_from_environment()
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
# 预定义配置验证器
|
|
363
|
-
class ConfigValidators:
|
|
364
|
-
"""配置验证器集合"""
|
|
365
|
-
|
|
366
|
-
@staticmethod
|
|
367
|
-
def validate_positive_int(value: Any) -> int:
|
|
368
|
-
"""验证正整数"""
|
|
369
|
-
try:
|
|
370
|
-
int_value = int(value)
|
|
371
|
-
if int_value <= 0:
|
|
372
|
-
raise ValueError("必须为正整数")
|
|
373
|
-
return int_value
|
|
374
|
-
except (ValueError, TypeError):
|
|
375
|
-
raise ValueError("必须为有效的整数")
|
|
376
|
-
|
|
377
|
-
@staticmethod
|
|
378
|
-
def validate_positive_float(value: Any) -> float:
|
|
379
|
-
"""验证正浮点数"""
|
|
380
|
-
try:
|
|
381
|
-
float_value = float(value)
|
|
382
|
-
if float_value <= 0:
|
|
383
|
-
raise ValueError("必须为正数")
|
|
384
|
-
return float_value
|
|
385
|
-
except (ValueError, TypeError):
|
|
386
|
-
raise ValueError("必须为有效的数字")
|
|
387
|
-
|
|
388
|
-
@staticmethod
|
|
389
|
-
def validate_boolean(value: Any) -> bool:
|
|
390
|
-
"""验证布尔值"""
|
|
391
|
-
if isinstance(value, bool):
|
|
392
|
-
return value
|
|
393
|
-
if isinstance(value, str):
|
|
394
|
-
if value.lower() in ("true", "1", "yes", "on"):
|
|
395
|
-
return True
|
|
396
|
-
elif value.lower() in ("false", "0", "no", "off"):
|
|
397
|
-
return False
|
|
398
|
-
raise ValueError("必须为布尔值 (true/false, 1/0, yes/no)")
|
|
399
|
-
|
|
400
|
-
@staticmethod
|
|
401
|
-
def validate_list(value: Any) -> list:
|
|
402
|
-
"""验证列表"""
|
|
403
|
-
if isinstance(value, list):
|
|
404
|
-
return value
|
|
405
|
-
if isinstance(value, str):
|
|
406
|
-
try:
|
|
407
|
-
return json.loads(value)
|
|
408
|
-
except json.JSONDecodeError:
|
|
409
|
-
return [item.strip() for item in value.split(",")]
|
|
410
|
-
raise ValueError("必须为列表或逗号分隔的字符串")
|
|
411
|
-
|
|
412
|
-
@staticmethod
|
|
413
|
-
def validate_file_path(value: Any) -> str:
|
|
414
|
-
"""验证文件路径"""
|
|
415
|
-
path = Path(str(value))
|
|
416
|
-
if not path.exists():
|
|
417
|
-
raise ValueError(f"文件不存在: {path}")
|
|
418
|
-
return str(path.absolute())
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
# 计量经济学专用配置
|
|
422
|
-
class EconometricConfig:
|
|
423
|
-
"""计量经济学专用配置"""
|
|
424
|
-
|
|
425
|
-
def __init__(self, config_manager: ConfigManager):
|
|
426
|
-
"""
|
|
427
|
-
初始化计量经济学配置
|
|
428
|
-
|
|
429
|
-
Args:
|
|
430
|
-
config_manager: 配置管理器实例
|
|
431
|
-
"""
|
|
432
|
-
self.config = config_manager
|
|
433
|
-
|
|
434
|
-
# 设置默认配置
|
|
435
|
-
self._set_defaults()
|
|
436
|
-
|
|
437
|
-
# 添加验证器
|
|
438
|
-
self._add_validators()
|
|
439
|
-
|
|
440
|
-
def _set_defaults(self) -> None:
|
|
441
|
-
"""设置默认配置"""
|
|
442
|
-
defaults = {
|
|
443
|
-
# 性能配置
|
|
444
|
-
"cache_enabled": (True, "是否启用缓存", False),
|
|
445
|
-
"cache_ttl": (3600, "缓存生存时间(秒)", False),
|
|
446
|
-
"cache_max_size": (1000, "最大缓存条目数", False),
|
|
447
|
-
|
|
448
|
-
# 监控配置
|
|
449
|
-
"monitoring_enabled": (True, "是否启用性能监控", False),
|
|
450
|
-
"memory_monitoring_interval": (1.0, "内存监控间隔(秒)", False),
|
|
451
|
-
|
|
452
|
-
# 模型配置
|
|
453
|
-
"default_n_estimators": (100, "默认树的数量", False),
|
|
454
|
-
"default_max_depth": (None, "默认最大深度", False),
|
|
455
|
-
"default_learning_rate": (0.1, "默认学习率", False),
|
|
456
|
-
"default_alpha": (1.0, "默认正则化强度", False),
|
|
457
|
-
|
|
458
|
-
# 数据配置
|
|
459
|
-
"min_sample_size": (10, "最小样本量", False),
|
|
460
|
-
"max_feature_count": (100, "最大特征数量", False),
|
|
461
|
-
"data_validation_strict": (True, "严格数据验证", False),
|
|
462
|
-
|
|
463
|
-
# 日志配置
|
|
464
|
-
"log_level": ("INFO", "日志级别", False),
|
|
465
|
-
"log_file": (None, "日志文件路径", False),
|
|
466
|
-
|
|
467
|
-
# 并行配置
|
|
468
|
-
"parallel_processing": (False, "是否启用并行处理", False),
|
|
469
|
-
"max_workers": (None, "最大工作线程数", False),
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
for key, (value, description, required) in defaults.items():
|
|
473
|
-
self.config.set_default(key, value, description, required)
|
|
474
|
-
|
|
475
|
-
def _add_validators(self) -> None:
|
|
476
|
-
"""添加验证器"""
|
|
477
|
-
validators = {
|
|
478
|
-
"cache_enabled": ConfigValidators.validate_boolean,
|
|
479
|
-
"cache_ttl": ConfigValidators.validate_positive_int,
|
|
480
|
-
"cache_max_size": ConfigValidators.validate_positive_int,
|
|
481
|
-
"monitoring_enabled": ConfigValidators.validate_boolean,
|
|
482
|
-
"memory_monitoring_interval": ConfigValidators.validate_positive_float,
|
|
483
|
-
"default_n_estimators": ConfigValidators.validate_positive_int,
|
|
484
|
-
"default_learning_rate": ConfigValidators.validate_positive_float,
|
|
485
|
-
"default_alpha": ConfigValidators.validate_positive_float,
|
|
486
|
-
"min_sample_size": ConfigValidators.validate_positive_int,
|
|
487
|
-
"max_feature_count": ConfigValidators.validate_positive_int,
|
|
488
|
-
"data_validation_strict": ConfigValidators.validate_boolean,
|
|
489
|
-
"parallel_processing": ConfigValidators.validate_boolean,
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
for key, validator in validators.items():
|
|
493
|
-
self.config.add_validator(key, validator)
|
|
494
|
-
|
|
495
|
-
def get_model_config(self) -> Dict[str, Any]:
|
|
496
|
-
"""获取模型配置"""
|
|
497
|
-
return {
|
|
498
|
-
"n_estimators": self.config.get("default_n_estimators"),
|
|
499
|
-
"max_depth": self.config.get("default_max_depth"),
|
|
500
|
-
"learning_rate": self.config.get("default_learning_rate"),
|
|
501
|
-
"alpha": self.config.get("default_alpha"),
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
def get_cache_config(self) -> Dict[str, Any]:
|
|
505
|
-
"""获取缓存配置"""
|
|
506
|
-
return {
|
|
507
|
-
"enabled": self.config.get("cache_enabled"),
|
|
508
|
-
"ttl": self.config.get("cache_ttl"),
|
|
509
|
-
"max_size": self.config.get("cache_max_size"),
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
def get_monitoring_config(self) -> Dict[str, Any]:
|
|
513
|
-
"""获取监控配置"""
|
|
514
|
-
return {
|
|
515
|
-
"enabled": self.config.get("monitoring_enabled"),
|
|
516
|
-
"memory_interval": self.config.get("memory_monitoring_interval"),
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
# 全局配置实例
|
|
521
|
-
global_config_manager = ConfigManager()
|
|
522
|
-
econometric_config = EconometricConfig(global_config_manager)
|
|
523
|
-
|
|
524
|
-
# 便捷配置访问函数
|
|
525
|
-
def get_config(key: str, default: Any = None) -> Any:
|
|
526
|
-
"""便捷配置访问函数"""
|
|
527
|
-
return global_config_manager.get(key, default)
|
|
528
|
-
|
|
529
|
-
def set_config(key: str, value: Any, description: str = "") -> None:
|
|
530
|
-
"""便捷配置设置函数"""
|
|
531
|
-
global_config_manager.set(key, value, description)
|
|
532
|
-
|
|
533
|
-
def load_config_files() -> None:
|
|
534
|
-
"""加载配置文件"""
|
|
535
|
-
# 加载环境变量配置
|
|
536
|
-
global_config_manager.load_from_environment()
|
|
537
|
-
|
|
538
|
-
# 尝试加载配置文件
|
|
539
|
-
config_files = ["config.json", "config.yaml", "config.yml"]
|
|
540
|
-
for config_file in config_files:
|
|
541
|
-
if (global_config_manager.config_dir / config_file).exists():
|
|
542
|
-
global_config_manager.load_from_file(config_file)
|
|
543
|
-
break
|
|
544
|
-
|
|
545
|
-
# 初始化时加载配置
|
|
546
|
-
load_config_files()
|
|
547
|
-
|
|
548
|
-
# 导出主要类和函数
|
|
549
|
-
__all__ = [
|
|
550
|
-
"ConfigSource",
|
|
551
|
-
"ConfigValue",
|
|
552
|
-
"ConfigurationError",
|
|
553
|
-
"ConfigManager",
|
|
554
|
-
"ConfigValidators",
|
|
555
|
-
"EconometricConfig",
|
|
556
|
-
"global_config_manager",
|
|
557
|
-
"econometric_config",
|
|
558
|
-
"get_config",
|
|
559
|
-
"set_config",
|
|
560
|
-
"load_config_files"
|
|
561
|
-
]
|