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

Fix plot splitter #2060

Merged
merged 2 commits into from
Sep 6, 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed performance issues in `RecallAtFixedPrecision` for large batch sizes ([#2042](https://github.com/Lightning-AI/torchmetrics/pull/2042))
-


- Fixed bug when creating multiple plots that lead to not all plots being shown ([#2060](https://github.com/Lightning-AI/torchmetrics/pull/2060))


## [1.1.1] - 2023-08-29
Expand Down
4 changes: 2 additions & 2 deletions src/torchmetrics/utilities/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ def plot_single_or_multi_val(
def _get_col_row_split(n: int) -> Tuple[int, int]:
"""Split `n` figures into `rows` x `cols` figures."""
nsq = sqrt(n)
if nsq * nsq == n:
if int(nsq) == nsq: # square number
return int(nsq), int(nsq)
if floor(nsq) * ceil(nsq) > n:
if floor(nsq) * ceil(nsq) >= n:
return floor(nsq), ceil(nsq)
return ceil(nsq), ceil(nsq)

Expand Down
12 changes: 12 additions & 0 deletions tests/unittests/utilities/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
_TORCH_GREATER_EQUAL_1_10,
_TORCHAUDIO_GREATER_EQUAL_0_10,
)
from torchmetrics.utilities.plot import _get_col_row_split
from torchmetrics.wrappers import (
BootStrapper,
ClasswiseWrapper,
Expand Down Expand Up @@ -794,6 +795,17 @@ def test_plot_methods_retrieval(metric_class, preds, target, indexes, num_vals):
plt.close(fig)


@pytest.mark.parametrize(
("n", "expected_row", "expected_col"),
[(1, 1, 1), (2, 1, 2), (3, 2, 2), (4, 2, 2), (5, 2, 3), (6, 2, 3), (7, 3, 3), (8, 3, 3), (9, 3, 3), (10, 3, 4)],
)
def test_row_col_splitter(n, expected_row, expected_col):
"""Test the row col splitter function works as expected."""
row, col = _get_col_row_split(n)
assert row == expected_row
assert col == expected_col


@pytest.mark.parametrize(
("metric_class", "preds", "target", "labels"),
[
Expand Down
Loading