Skip to content

Commit 4165475

Browse files
committed
Fix score log overwriting on re-initialization
- Modify project_score_save_class to check if the log file exists before writing headers. - Prevent data loss when the logger is instantiated multiple times within the same experiment folder. - Add regression test to verify that existing log entries are preserved during re-initialization.
1 parent dcf4bf8 commit 4165475

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

ml_grid/util/project_score_save.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ def __init__(self, experiment_dir: str):
127127
self.log_path.parent.mkdir(parents=True, exist_ok=True)
128128
self.models_dir.mkdir(parents=True, exist_ok=True)
129129

130-
df.to_csv(self.log_path, mode="w", header=True, index=False)
130+
if not self.log_path.exists():
131+
df.to_csv(self.log_path, mode="w", header=True, index=False)
131132

132133
def update_score_log(
133134
self,

tests/test_project_score_save.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,23 @@ def test_update_score_log_typo_and_missing_safety(self):
130130
df = pd.read_csv(log_path)
131131
self.assertEqual(len(df), 1)
132132

133+
def test_initialization_does_not_overwrite(self):
134+
"""Test that re-initializing the class does not wipe an existing log file."""
135+
# First initialization
136+
saver1 = project_score_save_class(str(self.experiment_dir))
137+
log_path = self.experiment_dir / "final_grid_score_log.csv"
138+
139+
# Simulate writing some data
140+
with open(log_path, "a") as f:
141+
f.write("test_data_entry\n")
142+
143+
# Second initialization on same directory
144+
saver2 = project_score_save_class(str(self.experiment_dir))
145+
146+
# Verify data persists
147+
with open(log_path, "r") as f:
148+
content = f.read()
149+
self.assertIn("test_data_entry", content)
150+
133151
if __name__ == "__main__":
134152
unittest.main()

0 commit comments

Comments
 (0)