From 4a63e91cc58b8937ed0513bbaf3788df0a75b33d Mon Sep 17 00:00:00 2001 From: Mathieu Dupont <108517594+mathieudpnt@users.noreply.github.com> Date: Wed, 26 Nov 2025 17:28:25 +0100 Subject: [PATCH 1/2] plot_utils --- src/post_processing/utils/plot_utils.py | 2 +- tests/test_plot_utils.py | 109 ++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 tests/test_plot_utils.py diff --git a/src/post_processing/utils/plot_utils.py b/src/post_processing/utils/plot_utils.py index 3c34fbd..a921270 100644 --- a/src/post_processing/utils/plot_utils.py +++ b/src/post_processing/utils/plot_utils.py @@ -438,7 +438,7 @@ def overview(df: DataFrame, annotator: list[str] | None = None) -> None: plt.tight_layout() # log - msg = f"{" Overview ":#^40}" + msg = f"""{" Overview ":#^40}""" msg += f"\n\n {summary_label}" logging.info(msg) diff --git a/tests/test_plot_utils.py b/tests/test_plot_utils.py new file mode 100644 index 0000000..9f0810f --- /dev/null +++ b/tests/test_plot_utils.py @@ -0,0 +1,109 @@ +import logging +from unittest.mock import patch + +import matplotlib.pyplot as plt +import pytest +from matplotlib.ticker import PercentFormatter +from numpy import arange, testing + +from post_processing.utils.plot_utils import ( + overview, + _wrap_xtick_labels, + set_y_axis_to_percentage, + get_legend, +) + + +def test_overview_runs_without_error(sample_df) -> None: + try: + overview(sample_df) + except ValueError: + pytest.fail("test_detection_perf raised ValueError unexpectedly.") + + +def test_wrap_xtick_labels_short_labels(): + fig, ax = plt.subplots() + labels = ["A", "B", "C"] + ax.set_xticks(range(len(labels))) + ax.set_xticklabels(labels) + + _wrap_xtick_labels(ax, max_chars=2) + + wrapped_labels = [label.get_text() for label in ax.get_xticklabels()] + assert wrapped_labels == labels + + +def test_wrap_xtick_labels_long_label(): + fig, ax = plt.subplots() + labels = ["This is a long label"] + ax.set_xticks([0]) + ax.set_xticklabels(labels) + + _wrap_xtick_labels(ax, max_chars=5) + + wrapped_labels = [label.get_text() for label in ax.get_xticklabels()] + expected = "This\nis a\nlong\nlabel" + assert wrapped_labels[0] == expected + + +def test_wrap_xtick_labels_no_spaces(): + fig, ax = plt.subplots() + labels = ["abcdefghijk"] + ax.set_xticks([0]) + ax.set_xticklabels(labels) + + _wrap_xtick_labels(ax, max_chars=4) + + wrapped_labels = [label.get_text() for label in ax.get_xticklabels()] + + expected = "abcd\nefgh\nijk" + assert wrapped_labels[0] == expected + + +def test_y_axis_formatter_and_ticks(): + fig, ax = plt.subplots() + + set_y_axis_to_percentage(ax) + + assert isinstance(ax.yaxis.get_major_formatter(), PercentFormatter) + assert ax.yaxis.get_major_formatter().xmax == 1.0 + + expected_ticks = arange(0, 1.02, 0.2) + testing.assert_allclose(ax.get_yticks(), expected_ticks) + + +def test_single_annotator_multiple_labels(): + annotators = ["Alice"] + labels = ["Label1", "Label2", "Label3"] + result = get_legend(annotators, labels) + assert result == labels + + +def test_multiple_annotators_single_label(): + annotators = ["Alice", "Bob", "Charlie"] + labels = ["CommonLabel"] + result = get_legend(annotators, labels) + assert result == annotators + + +def test_multiple_annotators_multiple_labels(): + annotators = ["Alice", "Bob", "Charlie"] + labels = ["Label1", "Label2", "Label3"] + result = get_legend(annotators, labels) + expected = ["Alice\nLabel1", "Bob\nLabel2", "Charlie\nLabel3"] + assert result == expected + + +def test_single_annotator_single_label(): + annotators = ["Alice"] + labels = ["Label1"] + result = get_legend(annotators, labels) + assert result == ["Alice\nLabel1"] + + +def test_lists_and_strings_combined(): + annotators = ["Alice", "Bob"] + labels = ["Label1", "Label2"] + result = get_legend(annotators, labels) + expected = ["Alice\nLabel1", "Bob\nLabel2"] + assert result == expected \ No newline at end of file From 5aae8662886393e92924697154c0ae8c5d366aea Mon Sep 17 00:00:00 2001 From: Mathieu Dupont <108517594+mathieudpnt@users.noreply.github.com> Date: Wed, 26 Nov 2025 17:29:38 +0100 Subject: [PATCH 2/2] ruff --- tests/test_plot_utils.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_plot_utils.py b/tests/test_plot_utils.py index 9f0810f..d7392cf 100644 --- a/tests/test_plot_utils.py +++ b/tests/test_plot_utils.py @@ -1,6 +1,3 @@ -import logging -from unittest.mock import patch - import matplotlib.pyplot as plt import pytest from matplotlib.ticker import PercentFormatter