Skip to content

Commit d4cb46d

Browse files
harishb00awaelchli
andauthored
Sort column headers for csv logger (#19159)
Co-authored-by: awaelchli <[email protected]>
1 parent 119039b commit d4cb46d

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

src/lightning/fabric/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2626
- Changed the `TransformerEnginePrecision(dtype=)` argument to `weights_dtype` and made it required ([#19082](https://github.com/Lightning-AI/lightning/pull/19082))
2727

2828

29+
- The columns in the `metrics.csv` file produced by `CSVLogger` are now sorted alphabetically ([#19159](https://github.com/Lightning-AI/lightning/pull/19159))
30+
31+
2932
### Deprecated
3033

3134
-

src/lightning/fabric/loggers/csv_logs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def _record_new_keys(self) -> Set[str]:
253253
current_keys = set().union(*self.metrics)
254254
new_keys = current_keys - set(self.metrics_keys)
255255
self.metrics_keys.extend(new_keys)
256+
self.metrics_keys.sort()
256257
return new_keys
257258

258259
def _rewrite_with_new_header(self, fieldnames: List[str]) -> None:

src/lightning/pytorch/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
4141
- Changed the `TransformerEnginePrecision(dtype=)` argument to `weights_dtype` and made it required ([#19082](https://github.com/Lightning-AI/lightning/pull/19082))
4242

4343

44+
- The columns in the `metrics.csv` file produced by `CSVLogger` are now sorted alphabetically ([#19159](https://github.com/Lightning-AI/lightning/pull/19159))
45+
46+
4447
### Deprecated
4548

4649
- Deprecated all precision plugin classes under `lightning.pytorch.plugins` with the suffix `Plugin` in the name ([#18840](https://github.com/Lightning-AI/lightning/pull/18840))

tests/tests_fabric/loggers/test_csv.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,21 @@ def test_rewrite_with_new_header(tmp_path):
183183
assert header == new_columns
184184
logs = file.readline().strip().split(",")
185185
assert logs == ["0", "1", "22", ""]
186+
187+
188+
def test_log_metrics_column_order_sorted(tmp_path):
189+
"""Test that the columns in the output metrics file are sorted by name."""
190+
logger = CSVLogger(tmp_path)
191+
logger.log_metrics({"c": 0.1})
192+
logger.log_metrics({"c": 0.2})
193+
logger.log_metrics({"b": 0.3})
194+
logger.log_metrics({"a": 0.4})
195+
logger.save()
196+
logger.log_metrics({"d": 0.5})
197+
logger.save()
198+
199+
path_csv = os.path.join(logger.log_dir, _ExperimentWriter.NAME_METRICS_FILE)
200+
with open(path_csv) as fp:
201+
lines = fp.readlines()
202+
203+
assert lines[0].strip() == "a,b,c,d,step"

0 commit comments

Comments
 (0)