Skip to content

Commit

Permalink
Sort column headers for csv logger (#19159)
Browse files Browse the repository at this point in the history
Co-authored-by: awaelchli <[email protected]>
  • Loading branch information
harishb00 and awaelchli authored Dec 18, 2023
1 parent 119039b commit d4cb46d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lightning/fabric/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Changed the `TransformerEnginePrecision(dtype=)` argument to `weights_dtype` and made it required ([#19082](https://github.com/Lightning-AI/lightning/pull/19082))


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


### Deprecated

-
Expand Down
1 change: 1 addition & 0 deletions src/lightning/fabric/loggers/csv_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def _record_new_keys(self) -> Set[str]:
current_keys = set().union(*self.metrics)
new_keys = current_keys - set(self.metrics_keys)
self.metrics_keys.extend(new_keys)
self.metrics_keys.sort()
return new_keys

def _rewrite_with_new_header(self, fieldnames: List[str]) -> None:
Expand Down
3 changes: 3 additions & 0 deletions src/lightning/pytorch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Changed the `TransformerEnginePrecision(dtype=)` argument to `weights_dtype` and made it required ([#19082](https://github.com/Lightning-AI/lightning/pull/19082))


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


### Deprecated

- 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))
Expand Down
18 changes: 18 additions & 0 deletions tests/tests_fabric/loggers/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,21 @@ def test_rewrite_with_new_header(tmp_path):
assert header == new_columns
logs = file.readline().strip().split(",")
assert logs == ["0", "1", "22", ""]


def test_log_metrics_column_order_sorted(tmp_path):
"""Test that the columns in the output metrics file are sorted by name."""
logger = CSVLogger(tmp_path)
logger.log_metrics({"c": 0.1})
logger.log_metrics({"c": 0.2})
logger.log_metrics({"b": 0.3})
logger.log_metrics({"a": 0.4})
logger.save()
logger.log_metrics({"d": 0.5})
logger.save()

path_csv = os.path.join(logger.log_dir, _ExperimentWriter.NAME_METRICS_FILE)
with open(path_csv) as fp:
lines = fp.readlines()

assert lines[0].strip() == "a,b,c,d,step"

0 comments on commit d4cb46d

Please sign in to comment.