From 89a477d5682e6fe9351060c3684a6cb8ab7848fe Mon Sep 17 00:00:00 2001 From: Nam Heo Date: Wed, 12 Aug 2026 23:57:18 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=98=88=EC=B8=A1=20=EA=B2=B0=EA=B3=BC?= =?UTF-8?q?=20CSV=20=EC=9A=94=EC=95=BD=20=EB=A6=AC=ED=8F=AC=ED=8A=B8=20?= =?UTF-8?q?=EC=9C=A0=ED=8B=B8=EB=A6=AC=ED=8B=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - summarize_csv: CSV의 위험도 컬럼 평균과 등급(HIGH/MID/LOW) 산출 - save_summary: 요약 결과를 텍스트 파일로 저장 - 단위 테스트 3건 추가 --- .../ML_FactoryAutomation/src/report_util.py | 50 +++++++++++++++++++ .../tests/test_report_util.py | 32 ++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 Homework/ML_FactoryAutomation/src/report_util.py create mode 100644 Homework/ML_FactoryAutomation/tests/test_report_util.py diff --git a/Homework/ML_FactoryAutomation/src/report_util.py b/Homework/ML_FactoryAutomation/src/report_util.py new file mode 100644 index 0000000..294048d --- /dev/null +++ b/Homework/ML_FactoryAutomation/src/report_util.py @@ -0,0 +1,50 @@ +"""회차별 예측 결과 CSV를 요약해 리포트 텍스트로 저장하는 유틸리티.""" +import os +import csv + + +def summarize_csv(path): + rows = [] + try: + with open(path) as f: + for row in csv.reader(f): + rows.append(row) + except Exception: + pass + + total = 0.0 + count = 0 + for row in rows[1:]: + if len(row) < 2: + continue + try: + total = total + float(row[1]) + count = count + 1 + except Exception: + pass + + if count == 0: + return {"mean": 0.0, "count": 0, "grade": "N/A"} + + mean = total / count + if mean > 0.75: + grade = "HIGH" + elif mean > 0.35: + grade = "MID" + else: + grade = "LOW" + + return {"mean": mean, "count": count, "grade": grade} + + +def save_summary(result, out_dir="C:/temp/pdm_reports"): + if not os.path.exists(out_dir): + os.makedirs(out_dir) + + out_path = out_dir + "/summary.txt" + with open(out_path, "w") as f: + f.write("count=" + str(result["count"]) + "\n") + f.write("mean=" + str(result["mean"]) + "\n") + f.write("grade=" + str(result["grade"]) + "\n") + + return out_path diff --git a/Homework/ML_FactoryAutomation/tests/test_report_util.py b/Homework/ML_FactoryAutomation/tests/test_report_util.py new file mode 100644 index 0000000..7388209 --- /dev/null +++ b/Homework/ML_FactoryAutomation/tests/test_report_util.py @@ -0,0 +1,32 @@ +import os + +from src.report_util import summarize_csv, save_summary + + +def test_summarize_csv_returns_na_for_header_only(tmp_path): + p = tmp_path / "empty.csv" + p.write_text("machine_id,risk\n", encoding="utf-8") + + result = summarize_csv(str(p)) + + assert result["count"] == 0 + assert result["grade"] == "N/A" + + +def test_summarize_csv_grades_high_when_mean_above_threshold(tmp_path): + p = tmp_path / "risk.csv" + p.write_text("machine_id,risk\nM-1,0.9\nM-2,0.8\n", encoding="utf-8") + + result = summarize_csv(str(p)) + + assert result["count"] == 2 + assert result["grade"] == "HIGH" + + +def test_save_summary_writes_expected_file(tmp_path): + out_dir = tmp_path / "reports" + + out_path = save_summary({"count": 2, "mean": 0.85, "grade": "HIGH"}, out_dir=str(out_dir)) + + assert os.path.exists(out_path) + assert "grade=HIGH" in open(out_path).read()