Spaces:
Running
on
Zero
Running
on
Zero
Delete scoring_diagnostics.py
Browse files- scoring_diagnostics.py +0 -137
scoring_diagnostics.py
DELETED
|
@@ -1,137 +0,0 @@
|
|
| 1 |
-
class ScoringDiagnostics:
|
| 2 |
-
"""評分系統診斷工具類"""
|
| 3 |
-
|
| 4 |
-
def __init__(self, log_dir="scoring_diagnostics"):
|
| 5 |
-
"""
|
| 6 |
-
初始化診斷工具
|
| 7 |
-
|
| 8 |
-
參數:
|
| 9 |
-
log_dir (str): 日誌文件存放目錄
|
| 10 |
-
"""
|
| 11 |
-
self.log_dir = log_dir
|
| 12 |
-
self._ensure_log_directory()
|
| 13 |
-
self.current_session = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 14 |
-
|
| 15 |
-
def _ensure_log_directory(self):
|
| 16 |
-
"""確保日誌目錄存在"""
|
| 17 |
-
if not os.path.exists(self.log_dir):
|
| 18 |
-
os.makedirs(self.log_dir)
|
| 19 |
-
|
| 20 |
-
def _create_log_filename(self, prefix):
|
| 21 |
-
"""生成日誌文件名"""
|
| 22 |
-
return os.path.join(
|
| 23 |
-
self.log_dir,
|
| 24 |
-
f"{prefix}_{self.current_session}.json"
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
def diagnostic_wrapper(self, original_func):
|
| 28 |
-
"""
|
| 29 |
-
包裝原始評分函數的診斷裝飾器
|
| 30 |
-
|
| 31 |
-
使用方式:
|
| 32 |
-
@scoring_diagnostics.diagnostic_wrapper
|
| 33 |
-
def calculate_compatibility_score(breed_info, user_prefs):
|
| 34 |
-
...
|
| 35 |
-
"""
|
| 36 |
-
@wraps(original_func)
|
| 37 |
-
def wrapper(breed_info, user_prefs, *args, **kwargs):
|
| 38 |
-
# 準備診斷信息
|
| 39 |
-
diagnostic_info = {
|
| 40 |
-
"timestamp": datetime.now().isoformat(),
|
| 41 |
-
"breed": breed_info.get('Breed', 'Unknown'),
|
| 42 |
-
"input_data": {
|
| 43 |
-
"breed_info": breed_info,
|
| 44 |
-
"user_preferences": vars(user_prefs)
|
| 45 |
-
}
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
try:
|
| 49 |
-
# 執行原始函數
|
| 50 |
-
result = original_func(breed_info, user_prefs, *args, **kwargs)
|
| 51 |
-
|
| 52 |
-
# 記錄成功結果
|
| 53 |
-
diagnostic_info.update({
|
| 54 |
-
"status": "success",
|
| 55 |
-
"result": result
|
| 56 |
-
})
|
| 57 |
-
|
| 58 |
-
# 檢查是否所有分數都相同或接近預設值
|
| 59 |
-
scores = [v for k, v in result.items() if k != 'overall']
|
| 60 |
-
if all(abs(score - 0.5) < 0.1 for score in scores):
|
| 61 |
-
diagnostic_info["warnings"] = {
|
| 62 |
-
"type": "uniform_scores",
|
| 63 |
-
"message": "所有分數都接近預設值 0.5",
|
| 64 |
-
"scores": scores
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
except Exception as e:
|
| 68 |
-
# 記錄錯誤信息
|
| 69 |
-
diagnostic_info.update({
|
| 70 |
-
"status": "error",
|
| 71 |
-
"error": {
|
| 72 |
-
"type": str(type(e).__name__),
|
| 73 |
-
"message": str(e),
|
| 74 |
-
"traceback": traceback.format_exc()
|
| 75 |
-
}
|
| 76 |
-
})
|
| 77 |
-
raise # 重新拋出異常
|
| 78 |
-
|
| 79 |
-
finally:
|
| 80 |
-
# 保存診斷信息
|
| 81 |
-
self._save_diagnostic_info(diagnostic_info)
|
| 82 |
-
|
| 83 |
-
return result
|
| 84 |
-
|
| 85 |
-
return wrapper
|
| 86 |
-
|
| 87 |
-
def _save_diagnostic_info(self, diagnostic_info):
|
| 88 |
-
"""保存診斷信息到文件"""
|
| 89 |
-
filename = self._create_log_filename("scoring_diagnostic")
|
| 90 |
-
try:
|
| 91 |
-
# 如果文件存在,讀取現有記錄
|
| 92 |
-
if os.path.exists(filename):
|
| 93 |
-
with open(filename, 'r', encoding='utf-8') as f:
|
| 94 |
-
records = json.load(f)
|
| 95 |
-
else:
|
| 96 |
-
records = []
|
| 97 |
-
|
| 98 |
-
# 添加新記錄
|
| 99 |
-
records.append(diagnostic_info)
|
| 100 |
-
|
| 101 |
-
# 保存所有記錄
|
| 102 |
-
with open(filename, 'w', encoding='utf-8') as f:
|
| 103 |
-
json.dump(records, f, ensure_ascii=False, indent=2)
|
| 104 |
-
|
| 105 |
-
except Exception as e:
|
| 106 |
-
print(f"保存診斷信息時發生錯誤: {str(e)}")
|
| 107 |
-
|
| 108 |
-
def analyze_diagnostics(self):
|
| 109 |
-
"""分析診斷結果並生成報告"""
|
| 110 |
-
try:
|
| 111 |
-
filename = self._create_log_filename("scoring_diagnostic")
|
| 112 |
-
if not os.path.exists(filename):
|
| 113 |
-
return "沒有找到診斷記錄"
|
| 114 |
-
|
| 115 |
-
with open(filename, 'r', encoding='utf-8') as f:
|
| 116 |
-
records = json.load(f)
|
| 117 |
-
|
| 118 |
-
# 分析結果
|
| 119 |
-
analysis = {
|
| 120 |
-
"total_records": len(records),
|
| 121 |
-
"success_count": sum(1 for r in records if r["status"] == "success"),
|
| 122 |
-
"error_count": sum(1 for r in records if r["status"] == "error"),
|
| 123 |
-
"uniform_scores_count": sum(1 for r in records if "warnings" in r and
|
| 124 |
-
r["warnings"]["type"] == "uniform_scores"),
|
| 125 |
-
"error_types": {}
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
# 統計錯誤類型
|
| 129 |
-
for record in records:
|
| 130 |
-
if record["status"] == "error":
|
| 131 |
-
error_type = record["error"]["type"]
|
| 132 |
-
analysis["error_types"][error_type] = analysis["error_types"].get(error_type, 0) + 1
|
| 133 |
-
|
| 134 |
-
return analysis
|
| 135 |
-
|
| 136 |
-
except Exception as e:
|
| 137 |
-
return f"分析診斷記錄時發生錯誤: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|