update
This commit is contained in:
+35
-10
@@ -26,7 +26,7 @@ def save_results_to_file(results: List[Any], result_dir: str = 'result') -> str:
|
||||
serializable_results = []
|
||||
for result in results:
|
||||
if hasattr(result, '__dict__'):
|
||||
# 如果是dataclass对象
|
||||
# 如果是dataclass对象,转换为字典
|
||||
result_dict = result.__dict__.copy()
|
||||
else:
|
||||
# 如果是字典
|
||||
@@ -36,14 +36,19 @@ def save_results_to_file(results: List[Any], result_dir: str = 'result') -> str:
|
||||
if 'time_consuming' in result_dict:
|
||||
result_dict['time_consuming'] = round(result_dict['time_consuming'], 2)
|
||||
|
||||
# 处理metrics对象
|
||||
for key in list(result_dict.keys()):
|
||||
if hasattr(result_dict[key], '__dict__'):
|
||||
result_dict[key] = result_dict[key].__dict__
|
||||
# 处理浮点数精度
|
||||
for metric_key, value in result_dict[key].items():
|
||||
if isinstance(value, float):
|
||||
result_dict[key][metric_key] = round(value, 6)
|
||||
# 只需要确保所有浮点数精度一致
|
||||
def round_floats(obj):
|
||||
if isinstance(obj, float):
|
||||
return round(obj, 6)
|
||||
elif isinstance(obj, dict):
|
||||
return {k: round_floats(v) for k, v in obj.items()}
|
||||
elif isinstance(obj, list):
|
||||
return [round_floats(item) for item in obj]
|
||||
else:
|
||||
return obj
|
||||
|
||||
# 对结果中的所有浮点数进行精度处理
|
||||
result_dict = round_floats(result_dict)
|
||||
|
||||
serializable_results.append(result_dict)
|
||||
|
||||
@@ -56,4 +61,24 @@ def save_results_to_file(results: List[Any], result_dir: str = 'result') -> str:
|
||||
json.dump(serializable_results, f, ensure_ascii=False, indent=2)
|
||||
|
||||
print(f"结果已保存到 {result_name}")
|
||||
return result_name
|
||||
return result_name
|
||||
|
||||
|
||||
def save_success_alpha(success_expression_list):
|
||||
"""
|
||||
将成功的表达式列表保存到 success.txt 文件中
|
||||
如果文件不存在则创建,如果存在则追加写入
|
||||
"""
|
||||
if not success_expression_list:
|
||||
print("没有成功的表达式需要保存")
|
||||
return
|
||||
|
||||
try:
|
||||
with open('success.txt', 'a', encoding='utf-8') as f:
|
||||
for expression in success_expression_list:
|
||||
f.write(expression + '\n')
|
||||
|
||||
print(f"成功保存 {len(success_expression_list)} 个表达式到 success.txt")
|
||||
|
||||
except Exception as e:
|
||||
print(f"保存文件时出错: {e}")
|
||||
|
||||
Reference in New Issue
Block a user