aigroup-econ-mcp 0.1.6__py3-none-any.whl → 0.2.0__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.
- aigroup_econ_mcp/config.py +561 -0
- aigroup_econ_mcp/server.py +2503 -16
- aigroup_econ_mcp/tools/__init__.py +17 -7
- aigroup_econ_mcp/tools/base.py +271 -0
- aigroup_econ_mcp/tools/cache.py +533 -0
- aigroup_econ_mcp/tools/machine_learning.py +673 -0
- aigroup_econ_mcp/tools/monitoring.py +555 -0
- aigroup_econ_mcp/tools/optimized_example.py +229 -0
- aigroup_econ_mcp/tools/panel_data.py +527 -0
- aigroup_econ_mcp/tools/time_series.py +838 -260
- aigroup_econ_mcp/tools/validation.py +482 -0
- {aigroup_econ_mcp-0.1.6.dist-info → aigroup_econ_mcp-0.2.0.dist-info}/METADATA +5 -1
- aigroup_econ_mcp-0.2.0.dist-info/RECORD +20 -0
- aigroup_econ_mcp-0.1.6.dist-info/RECORD +0 -12
- {aigroup_econ_mcp-0.1.6.dist-info → aigroup_econ_mcp-0.2.0.dist-info}/WHEEL +0 -0
- {aigroup_econ_mcp-0.1.6.dist-info → aigroup_econ_mcp-0.2.0.dist-info}/entry_points.txt +0 -0
- {aigroup_econ_mcp-0.1.6.dist-info → aigroup_econ_mcp-0.2.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
"""
|
|
2
|
-
计量经济学工具模块
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from . import regression, statistics, time_series
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
"""
|
|
2
|
+
计量经济学工具模块
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from . import regression, statistics, time_series, machine_learning, panel_data
|
|
6
|
+
from . import validation, cache, monitoring
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"regression",
|
|
10
|
+
"statistics",
|
|
11
|
+
"time_series",
|
|
12
|
+
"machine_learning",
|
|
13
|
+
"panel_data",
|
|
14
|
+
"validation",
|
|
15
|
+
"cache",
|
|
16
|
+
"monitoring"
|
|
17
|
+
]
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"""
|
|
2
|
+
工具模块基类
|
|
3
|
+
提供统一的优化组件集成和错误处理
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import functools
|
|
7
|
+
from typing import Any, Dict, List, Optional, Callable, Type
|
|
8
|
+
from .validation import ValidationError, validate_econometric_data, validate_model_parameters
|
|
9
|
+
from .cache import cache_result, cache_model, global_econometric_cache
|
|
10
|
+
from .monitoring import monitor_performance, track_progress, global_performance_monitor
|
|
11
|
+
from ..config import get_config, econometric_config
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class EconometricToolError(Exception):
|
|
15
|
+
"""计量经济学工具错误基类"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, message: str, tool_name: str = None, original_error: Exception = None):
|
|
18
|
+
self.message = message
|
|
19
|
+
self.tool_name = tool_name
|
|
20
|
+
self.original_error = original_error
|
|
21
|
+
super().__init__(self.message)
|
|
22
|
+
|
|
23
|
+
def __str__(self):
|
|
24
|
+
base_msg = f"计量经济学工具错误"
|
|
25
|
+
if self.tool_name:
|
|
26
|
+
base_msg += f" ({self.tool_name})"
|
|
27
|
+
base_msg += f": {self.message}"
|
|
28
|
+
if self.original_error:
|
|
29
|
+
base_msg += f"\n原始错误: {self.original_error}"
|
|
30
|
+
return base_msg
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class DataValidationError(EconometricToolError):
|
|
34
|
+
"""数据验证错误"""
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class ModelFittingError(EconometricToolError):
|
|
39
|
+
"""模型拟合错误"""
|
|
40
|
+
pass
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class ConfigurationError(EconometricToolError):
|
|
44
|
+
"""配置错误"""
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class EconometricTool:
|
|
49
|
+
"""
|
|
50
|
+
计量经济学工具基类
|
|
51
|
+
提供统一的参数验证、缓存、性能监控和错误处理
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(self, tool_name: str):
|
|
55
|
+
"""
|
|
56
|
+
初始化工具
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
tool_name: 工具名称
|
|
60
|
+
"""
|
|
61
|
+
self.tool_name = tool_name
|
|
62
|
+
self._cache_enabled = get_config("cache_enabled", True)
|
|
63
|
+
self._monitoring_enabled = get_config("monitoring_enabled", True)
|
|
64
|
+
self._validation_strict = get_config("data_validation_strict", True)
|
|
65
|
+
|
|
66
|
+
def _validate_input_data(self, data: Any, data_type: str = "generic") -> Any:
|
|
67
|
+
"""
|
|
68
|
+
验证输入数据
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
data: 输入数据
|
|
72
|
+
data_type: 数据类型
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
Any: 验证后的数据
|
|
76
|
+
|
|
77
|
+
Raises:
|
|
78
|
+
DataValidationError: 数据验证失败
|
|
79
|
+
"""
|
|
80
|
+
try:
|
|
81
|
+
if data_type == "econometric":
|
|
82
|
+
return validate_econometric_data(data)
|
|
83
|
+
elif data_type == "time_series":
|
|
84
|
+
from .validation import validate_time_series_data
|
|
85
|
+
return validate_time_series_data(data)
|
|
86
|
+
elif data_type == "model_parameters":
|
|
87
|
+
return validate_model_parameters(data)
|
|
88
|
+
else:
|
|
89
|
+
return data
|
|
90
|
+
except ValidationError as e:
|
|
91
|
+
error_msg = f"数据验证失败: {e.message}"
|
|
92
|
+
if self._validation_strict:
|
|
93
|
+
raise DataValidationError(error_msg, self.tool_name, e)
|
|
94
|
+
else:
|
|
95
|
+
# 在非严格模式下记录警告并继续
|
|
96
|
+
import warnings
|
|
97
|
+
warnings.warn(f"{self.tool_name}: {error_msg}")
|
|
98
|
+
return data
|
|
99
|
+
|
|
100
|
+
def _handle_errors(self, func: Callable) -> Callable:
|
|
101
|
+
"""
|
|
102
|
+
错误处理装饰器
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
func: 被装饰的函数
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
Callable: 装饰后的函数
|
|
109
|
+
"""
|
|
110
|
+
@functools.wraps(func)
|
|
111
|
+
def wrapper(*args, **kwargs):
|
|
112
|
+
try:
|
|
113
|
+
return func(*args, **kwargs)
|
|
114
|
+
except (DataValidationError, ModelFittingError, ConfigurationError):
|
|
115
|
+
# 重新抛出已知的错误类型
|
|
116
|
+
raise
|
|
117
|
+
except ValidationError as e:
|
|
118
|
+
# 转换验证错误
|
|
119
|
+
raise DataValidationError(f"参数验证失败: {e.message}", self.tool_name, e)
|
|
120
|
+
except Exception as e:
|
|
121
|
+
# 处理其他未知错误
|
|
122
|
+
error_msg = f"执行过程中发生未知错误: {str(e)}"
|
|
123
|
+
raise EconometricToolError(error_msg, self.tool_name, e)
|
|
124
|
+
|
|
125
|
+
return wrapper
|
|
126
|
+
|
|
127
|
+
def _apply_optimizations(self, func: Callable) -> Callable:
|
|
128
|
+
"""
|
|
129
|
+
应用优化装饰器
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
func: 被装饰的函数
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
Callable: 优化后的函数
|
|
136
|
+
"""
|
|
137
|
+
# 应用性能监控
|
|
138
|
+
if self._monitoring_enabled:
|
|
139
|
+
func = monitor_performance(self.tool_name)(func)
|
|
140
|
+
|
|
141
|
+
# 应用缓存
|
|
142
|
+
if self._cache_enabled:
|
|
143
|
+
cache_config = econometric_config.get_cache_config()
|
|
144
|
+
func = cache_result(ttl=cache_config["ttl"], max_size=cache_config["max_size"])(func)
|
|
145
|
+
|
|
146
|
+
# 应用错误处理
|
|
147
|
+
func = self._handle_errors(func)
|
|
148
|
+
|
|
149
|
+
return func
|
|
150
|
+
|
|
151
|
+
def create_optimized_function(self, func: Callable) -> Callable:
|
|
152
|
+
"""
|
|
153
|
+
创建优化函数
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
func: 原始函数
|
|
157
|
+
|
|
158
|
+
Returns:
|
|
159
|
+
Callable: 优化后的函数
|
|
160
|
+
"""
|
|
161
|
+
return self._apply_optimizations(func)
|
|
162
|
+
|
|
163
|
+
def execute_with_progress(self, total_steps: int, description: str = ""):
|
|
164
|
+
"""
|
|
165
|
+
进度跟踪上下文管理器
|
|
166
|
+
|
|
167
|
+
Args:
|
|
168
|
+
total_steps: 总步骤数
|
|
169
|
+
description: 进度描述
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
ContextManager: 进度跟踪上下文管理器
|
|
173
|
+
"""
|
|
174
|
+
return track_progress(total_steps, f"{self.tool_name}: {description}")
|
|
175
|
+
|
|
176
|
+
def get_performance_stats(self) -> Dict[str, Any]:
|
|
177
|
+
"""
|
|
178
|
+
获取性能统计
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
Dict[str, Any]: 性能统计信息
|
|
182
|
+
"""
|
|
183
|
+
if not self._monitoring_enabled:
|
|
184
|
+
return {}
|
|
185
|
+
|
|
186
|
+
stats = global_performance_monitor.get_function_stats(self.tool_name)
|
|
187
|
+
if stats:
|
|
188
|
+
return {
|
|
189
|
+
"execution_time": stats.execution_time,
|
|
190
|
+
"peak_memory_mb": stats.peak_memory_mb,
|
|
191
|
+
"cpu_percent": stats.cpu_percent,
|
|
192
|
+
"timestamp": stats.timestamp
|
|
193
|
+
}
|
|
194
|
+
return {}
|
|
195
|
+
|
|
196
|
+
def get_cache_stats(self) -> Dict[str, Any]:
|
|
197
|
+
"""
|
|
198
|
+
获取缓存统计
|
|
199
|
+
|
|
200
|
+
Returns:
|
|
201
|
+
Dict[str, Any]: 缓存统计信息
|
|
202
|
+
"""
|
|
203
|
+
if not self._cache_enabled:
|
|
204
|
+
return {}
|
|
205
|
+
|
|
206
|
+
return global_econometric_cache.result_cache.get_function_cache_stats(self.tool_name) or {}
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
# 便捷装饰器函数
|
|
210
|
+
def econometric_tool(tool_name: str = None):
|
|
211
|
+
"""
|
|
212
|
+
计量经济学工具装饰器
|
|
213
|
+
|
|
214
|
+
Args:
|
|
215
|
+
tool_name: 工具名称
|
|
216
|
+
|
|
217
|
+
Returns:
|
|
218
|
+
Callable: 装饰器函数
|
|
219
|
+
"""
|
|
220
|
+
def decorator(func):
|
|
221
|
+
name = tool_name or func.__name__
|
|
222
|
+
tool = EconometricTool(name)
|
|
223
|
+
return tool.create_optimized_function(func)
|
|
224
|
+
|
|
225
|
+
return decorator
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def validate_input(data_type: str = "generic"):
|
|
229
|
+
"""
|
|
230
|
+
输入验证装饰器
|
|
231
|
+
|
|
232
|
+
Args:
|
|
233
|
+
data_type: 数据类型
|
|
234
|
+
|
|
235
|
+
Returns:
|
|
236
|
+
Callable: 装饰器函数
|
|
237
|
+
"""
|
|
238
|
+
def decorator(func):
|
|
239
|
+
@functools.wraps(func)
|
|
240
|
+
def wrapper(*args, **kwargs):
|
|
241
|
+
# 验证第一个位置参数(通常是数据)
|
|
242
|
+
if args:
|
|
243
|
+
validated_args = list(args)
|
|
244
|
+
validated_args[0] = EconometricTool(func.__name__)._validate_input_data(args[0], data_type)
|
|
245
|
+
args = tuple(validated_args)
|
|
246
|
+
|
|
247
|
+
# 验证关键字参数
|
|
248
|
+
validated_kwargs = {}
|
|
249
|
+
for key, value in kwargs.items():
|
|
250
|
+
if key in ["data", "y_data", "x_data"]:
|
|
251
|
+
validated_kwargs[key] = EconometricTool(func.__name__)._validate_input_data(value, data_type)
|
|
252
|
+
else:
|
|
253
|
+
validated_kwargs[key] = value
|
|
254
|
+
|
|
255
|
+
return func(*args, **validated_kwargs)
|
|
256
|
+
|
|
257
|
+
return wrapper
|
|
258
|
+
|
|
259
|
+
return decorator
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
# 导出主要类和函数
|
|
263
|
+
__all__ = [
|
|
264
|
+
"EconometricToolError",
|
|
265
|
+
"DataValidationError",
|
|
266
|
+
"ModelFittingError",
|
|
267
|
+
"ConfigurationError",
|
|
268
|
+
"EconometricTool",
|
|
269
|
+
"econometric_tool",
|
|
270
|
+
"validate_input"
|
|
271
|
+
]
|