Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort column headers for csv logger #19159

Merged
merged 6 commits into from
Dec 18, 2023
Merged
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
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"