Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/anomaly_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
27 changes: 27 additions & 0 deletions tests/test_atomic_csv.py
Original file line number Diff line number Diff line change
@@ -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]
Loading