feat: add running time and submit time log
This commit is contained in:
+2
-1
@@ -160,4 +160,5 @@ cython_debug/
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
uploads
|
||||
uploads
|
||||
scores.csv
|
||||
@@ -48,7 +48,7 @@ def write_to_csv(data):
|
||||
"""
|
||||
file_exists = os.path.isfile(csv_file_path)
|
||||
with open(csv_file_path, mode="a", newline="", encoding="utf-8") as csvfile:
|
||||
fieldnames = ["id", "name", "score", "details"]
|
||||
fieldnames = ["time", "id", "name", "score", "running_time", "details"]
|
||||
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
||||
|
||||
if not file_exists:
|
||||
@@ -61,7 +61,15 @@ def write_to_csv(data):
|
||||
def upload_score():
|
||||
try:
|
||||
data = request.json
|
||||
if "name" in data and "id" in data and "score" in data and "details" in data:
|
||||
if (
|
||||
"name" in data
|
||||
and "id" in data
|
||||
and "score" in data
|
||||
and "details" in data
|
||||
and "running_time" in data
|
||||
and "time" in data
|
||||
):
|
||||
data["details"] = data["details"].replace("\n", "<br>")
|
||||
write_to_csv(data)
|
||||
return jsonify({"message": "成绩记录上传成功!"}), 200
|
||||
else:
|
||||
|
||||
+55
-17
@@ -1,7 +1,8 @@
|
||||
import requests
|
||||
import numpy as np
|
||||
import traceback
|
||||
import importlib.util
|
||||
import numpy as np
|
||||
import requests
|
||||
import time
|
||||
import traceback
|
||||
|
||||
|
||||
def load_user_function(file_path, function_name):
|
||||
@@ -14,7 +15,9 @@ def load_user_function(file_path, function_name):
|
||||
return getattr(user_code, function_name)
|
||||
|
||||
|
||||
def evaluate_knn(user_func, train_data, train_labels, test_data, test_labels):
|
||||
def evaluate_knn(
|
||||
user_func, train_data, train_labels, test_data, test_labels, log_details
|
||||
):
|
||||
"""
|
||||
使用学生编写的 KNN 函数对测试集进行分类,并计算准确率。
|
||||
|
||||
@@ -24,6 +27,7 @@ def evaluate_knn(user_func, train_data, train_labels, test_data, test_labels):
|
||||
- train_labels (numpy array): 训练标签集。
|
||||
- test_data (numpy array): 测试数据集。
|
||||
- test_labels (numpy array): 测试标签集。
|
||||
- log_details (list): 日志记录列表。
|
||||
|
||||
Returns:
|
||||
- float: 分类准确率。
|
||||
@@ -38,15 +42,22 @@ def evaluate_knn(user_func, train_data, train_labels, test_data, test_labels):
|
||||
if result == test_labels[i]:
|
||||
correct += 1
|
||||
else:
|
||||
log_details.append(
|
||||
f"第 {i+1} 次测试错误,数据 {test_data[i]} 被分类到 {result} 实际是 {test_labels[i]}"
|
||||
)
|
||||
print(
|
||||
f"第 {i+1} 次测试错误,数据 {test_data[i]} 被分类到 {result} 实际是 {test_labels[i]}"
|
||||
)
|
||||
|
||||
accuracy = correct / total
|
||||
log_details.append(f"分类准确率: {accuracy * 100:.2f}%")
|
||||
return accuracy
|
||||
except Exception as e:
|
||||
log_details.append(f"评测期间发生错误: {e}")
|
||||
print(f"评测期间发生错误: {e}")
|
||||
log_details.append(traceback.format_exc())
|
||||
print(traceback.format_exc())
|
||||
log_details.append("成绩为零!")
|
||||
print("成绩为零!")
|
||||
return 0.0
|
||||
|
||||
@@ -71,21 +82,23 @@ def upload_score(name, id, score_data):
|
||||
"""
|
||||
上传成绩到服务器。
|
||||
"""
|
||||
url = "http://10.2.1.195:5000/upload_score"
|
||||
url = "http://10.2.1.195:880/upload_score"
|
||||
try:
|
||||
print("上传成绩中...")
|
||||
print(score_data)
|
||||
# print(score_data)
|
||||
response = requests.post(
|
||||
url,
|
||||
json={
|
||||
"id": id,
|
||||
"name": name,
|
||||
"score": score_data["score"],
|
||||
"running_time": score_data["running_time"],
|
||||
"time": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
|
||||
"details": score_data["details"],
|
||||
},
|
||||
)
|
||||
if response.status_code == 200:
|
||||
print("上传成绩成功")
|
||||
print("上传成绩成功!")
|
||||
else:
|
||||
print("上传成绩失败:", response.json())
|
||||
except Exception as e:
|
||||
@@ -93,8 +106,8 @@ def upload_score(name, id, score_data):
|
||||
|
||||
|
||||
def main(name, id):
|
||||
user_code_file = "function.py" # 学生代码文件
|
||||
user_function_name = "classify0" # 学生需要实现的 KNN 函数
|
||||
user_code_file = "function.py" # 代码文件
|
||||
user_function_name = "classify0" # KNN 函数名
|
||||
|
||||
# 加载训练集和测试集
|
||||
train_file = "datingTrainSet2.tsv"
|
||||
@@ -103,27 +116,52 @@ def main(name, id):
|
||||
train_data, train_labels = load_data(train_file)
|
||||
test_data, test_labels = load_data(test_file)
|
||||
|
||||
# 加载学生的 KNN 函数
|
||||
# 加载自定义函数
|
||||
log_details = []
|
||||
try:
|
||||
user_func = load_user_function(user_code_file, user_function_name)
|
||||
except Exception as e:
|
||||
print(f"Error loading user function: {e}")
|
||||
log_details.append(f"Error loading user function: {e}")
|
||||
print("\n".join(log_details))
|
||||
return
|
||||
|
||||
# 记录评估时间
|
||||
start_time = time.time()
|
||||
|
||||
# 评估分类准确率
|
||||
accuracy = evaluate_knn(user_func, train_data, train_labels, test_data, test_labels)
|
||||
accuracy = evaluate_knn(
|
||||
user_func, train_data, train_labels, test_data, test_labels, log_details
|
||||
)
|
||||
|
||||
# 计算运行时间
|
||||
running_time = time.time() - start_time
|
||||
|
||||
# 输出结果
|
||||
print(f"得分: {accuracy * 100:.2f}")
|
||||
log_details.append(f"得分: {accuracy * 100:.2f}%")
|
||||
log_details.append(f"运行时间: {running_time * 1000:.10f}ms")
|
||||
print("\n".join(log_details))
|
||||
|
||||
# 上传成绩
|
||||
score_data = {
|
||||
"score": accuracy,
|
||||
"running_time": f"{running_time * 1000:.10f}ms",
|
||||
"details": "\n".join(log_details), # 将日志记录加入 details 中
|
||||
}
|
||||
|
||||
upload_score(name, id, score_data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
from function import name, id
|
||||
|
||||
print(f"正在评测 {name}@{id} 的代码...\n")
|
||||
main(name, id)
|
||||
except Exception as e:
|
||||
print(f"评测出错: {e}")
|
||||
print(traceback.format_exc())
|
||||
print("请检查代码和返回值类型是否正确!")
|
||||
print("本次评测无效!")
|
||||
log_details = [
|
||||
f"评测出错: {e}",
|
||||
traceback.format_exc(),
|
||||
"请检查代码和返回值类型是否正确!",
|
||||
"本次评测无效!",
|
||||
]
|
||||
print("\n".join(log_details))
|
||||
|
||||
Reference in New Issue
Block a user