From 876480b0214e1b26e60557737953ca7b176956b2 Mon Sep 17 00:00:00 2001 From: Ayush Tiwari <147244828+Ayushdevo@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:17:54 +0530 Subject: [PATCH] Write production analysis CSV atomically --- src/anomaly_detector.py | 8 ++------ src/artifacts.py | 21 +++++++++++++++++++++ tests/test_atomic_csv.py | 27 +++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 tests/test_atomic_csv.py diff --git a/src/anomaly_detector.py b/src/anomaly_detector.py index 0835b77..c14a2cd 100644 --- a/src/anomaly_detector.py +++ b/src/anomaly_detector.py @@ -5,6 +5,7 @@ import pandas as pd from sklearn.ensemble import IsolationForest from sklearn.preprocessing import StandardScaler +from src.artifacts import write_csv_atomic from src.model_contracts import positive_class_index @@ -141,12 +142,7 @@ def analyze_production(): # Save results # ---------------------------- - OUTPUT_PATH.parent.mkdir(exist_ok=True) - - production.to_csv( - OUTPUT_PATH, - index=False, - ) + write_csv_atomic(OUTPUT_PATH, production) # ---------------------------- # Summary diff --git a/src/artifacts.py b/src/artifacts.py index b41ddb3..cb41d48 100644 --- a/src/artifacts.py +++ b/src/artifacts.py @@ -29,6 +29,27 @@ def write_json_atomic(path, data): pass +def write_csv_atomic(path, dataframe): + """Replace a CSV report only after it has been written successfully.""" + path = Path(path) + path.parent.mkdir(parents=True, exist_ok=True) + temporary_path = None + try: + with tempfile.NamedTemporaryFile( + mode="w", encoding="utf-8", newline="", dir=path.parent, + prefix=f".{path.name}.", suffix=".tmp", delete=False, + ) as file: + temporary_path = Path(file.name) + dataframe.to_csv(file, index=False) + os.replace(temporary_path, path) + finally: + if temporary_path is not None: + try: + temporary_path.unlink() + except FileNotFoundError: + pass + + def _reject_json_constant(value): raise ValueError(f"Nonfinite JSON number: {value}") diff --git a/tests/test_atomic_csv.py b/tests/test_atomic_csv.py new file mode 100644 index 0000000..7b4e914 --- /dev/null +++ b/tests/test_atomic_csv.py @@ -0,0 +1,27 @@ +import pandas as pd +import pytest + +from src.artifacts import write_csv_atomic + + +def test_atomic_csv_writes_rows_and_creates_parent_directory(tmp_path): + path = tmp_path / "nested" / "analysis.csv" + frame = pd.DataFrame({"value": [1.5, 2.0], "label": ["one", "two"]}) + write_csv_atomic(path, frame) + pd.testing.assert_frame_equal(pd.read_csv(path), frame) + assert list(path.parent.iterdir()) == [path] + + +def test_atomic_csv_preserves_previous_report_on_write_failure(tmp_path, monkeypatch): + path = tmp_path / "analysis.csv" + path.write_text("previous report\n", encoding="utf-8") + frame = pd.DataFrame({"value": [1]}) + + def fail(*args, **kwargs): + raise OSError("disk error") + + monkeypatch.setattr(frame, "to_csv", fail) + with pytest.raises(OSError, match="disk error"): + write_csv_atomic(path, frame) + assert path.read_text(encoding="utf-8") == "previous report\n" + assert list(tmp_path.iterdir()) == [path]