|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 | import os
|
| 15 | +from unittest import mock |
15 | 16 | from unittest.mock import MagicMock
|
16 | 17 |
|
17 | 18 | import pytest
|
@@ -50,6 +51,21 @@ def test_manual_versioning(tmp_path):
|
50 | 51 | assert logger.version == 1
|
51 | 52 |
|
52 | 53 |
|
| 54 | +def test_manual_versioning_file_exists(tmp_path): |
| 55 | + """Test that a warning is emitted and existing files get overwritten.""" |
| 56 | + |
| 57 | + # Simulate an existing 'version_0' vrom a previous run |
| 58 | + (tmp_path / "exp" / "version_0").mkdir(parents=True) |
| 59 | + previous_metrics_file = tmp_path / "exp" / "version_0" / "metrics.csv" |
| 60 | + previous_metrics_file.touch() |
| 61 | + |
| 62 | + logger = CSVLogger(root_dir=tmp_path, name="exp", version=0) |
| 63 | + assert previous_metrics_file.exists() |
| 64 | + with pytest.warns(UserWarning, match="Experiment logs directory .* exists and is not empty"): |
| 65 | + _ = logger.experiment |
| 66 | + assert not previous_metrics_file.exists() |
| 67 | + |
| 68 | + |
53 | 69 | def test_named_version(tmp_path):
|
54 | 70 | """Verify that manual versioning works for string versions, e.g. '2020-02-05-162402'."""
|
55 | 71 | exp_name = "exp"
|
@@ -130,7 +146,11 @@ def test_automatic_step_tracking(tmp_path):
|
130 | 146 | assert logger.experiment.metrics[2]["step"] == 2
|
131 | 147 |
|
132 | 148 |
|
133 |
| -def test_append_metrics_file(tmp_path): |
| 149 | +@mock.patch( |
| 150 | + # Mock the existance check, so we can simulate appending to the metrics file |
| 151 | + "lightning.fabric.loggers.csv_logs._ExperimentWriter._check_log_dir_exists" |
| 152 | +) |
| 153 | +def test_append_metrics_file(_, tmp_path): |
134 | 154 | """Test that the logger appends to the file instead of rewriting it on every save."""
|
135 | 155 | logger = CSVLogger(tmp_path, name="test", version=0, flush_logs_every_n_steps=1)
|
136 | 156 |
|
@@ -167,7 +187,11 @@ def test_append_columns(tmp_path):
|
167 | 187 | assert set(header.split(",")) == {"step", "a", "b", "c"}
|
168 | 188 |
|
169 | 189 |
|
170 |
| -def test_rewrite_with_new_header(tmp_path): |
| 190 | +@mock.patch( |
| 191 | + # Mock the existance check, so we can simulate appending to the metrics file |
| 192 | + "lightning.fabric.loggers.csv_logs._ExperimentWriter._check_log_dir_exists" |
| 193 | +) |
| 194 | +def test_rewrite_with_new_header(_, tmp_path): |
171 | 195 | # write a csv file manually
|
172 | 196 | with open(tmp_path / "metrics.csv", "w") as file:
|
173 | 197 | file.write("step,metric1,metric2\n")
|
|
0 commit comments