MLPlatform/function/method_reader_metric.py

79 lines
2.5 KiB
Python

import yaml
from typing import Dict, List
import os
import logging
from pathlib import Path
class MethodReader:
"""方法配置读取器"""
def __init__(self):
"""初始化方法读取器"""
self.logger = logging.getLogger(__name__)
self.method_config = self._load_metrics()
def _load_metrics(self) -> Dict:
"""加载方法配置文件"""
try:
config_path = Path('model/metrics.yaml')
if not config_path.exists():
raise FileNotFoundError(f"Method config file not found at {config_path}")
with open(config_path, 'r', encoding='utf-8') as f:
config = yaml.safe_load(f)
self.logger.info("Successfully loaded method config")
return config
except Exception as e:
self.logger.error(f"Error loading method config: {str(e)}")
raise
def get_metrics(self) -> Dict:
"""获取预处理方法列表"""
try:
metrics = []
# 分类方法
classification_metrics = self.method_config.get('classification', {})
if classification_metrics:
metrics.append({
"name": "classification_metrics",
"description": "分类方法评价指标",
"metric": classification_metrics
})
# 回归方法
regression_metrics = self.method_config.get('regression', {})
if regression_metrics:
metrics.append({
"name": "regression_metrics",
"description": "回归方法评价指标",
"metric": regression_metrics
})
# 聚类方法
clustering_metrics = self.method_config.get('clustering', {})
if clustering_metrics:
metrics.append({
"name": "clustering_metrics",
"description": "聚类方法评价指标",
"metric": clustering_metrics
})
return {
"status": "success",
"metric": metrics
}
except Exception as e:
self.logger.error(f"Error getting preprocessing methods: {str(e)}")
return {
"status": "error",
"error": str(e)
}