Skip to content

Commit 7aeb886

Browse files
committed
Increase logging for plot options
1 parent d74abf8 commit 7aeb886

12 files changed

Lines changed: 177 additions & 10 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import logging
2+
3+
from PyQt6.QtCore import pyqtBoundSignal
4+
5+
6+
def log_plot_option_usage_once(
7+
signal: pyqtBoundSignal, logger: logging.Logger, option_name: str
8+
) -> None:
9+
def log_usage(*_args: object) -> None:
10+
logger.info("Plot sidebar option used: '%s'", option_name)
11+
signal.disconnect(log_usage)
12+
13+
signal.connect(log_usage)

src/ert/gui/plotting/widgets/plot_controls/custom_palette_dialog.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from collections.abc import Sequence
23

34
from PyQt6.QtWidgets import (
@@ -10,8 +11,11 @@
1011
)
1112

1213
from ert.gui.plotting.customization_dialog.color_chooser import ColorBox
14+
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
1315
from ert.gui.plotting.utils.plot_color_palettes import MINIMUM_COLOR_CYCLE_LENGTH
1416

17+
logger = logging.getLogger(__name__)
18+
1519

1620
class CustomPaletteDialog(QDialog):
1721
def __init__(
@@ -45,6 +49,7 @@ def __init__(
4549
buttons.accepted.connect(self.accept)
4650
buttons.rejected.connect(self.reject)
4751
layout.addWidget(buttons)
52+
log_plot_option_usage_once(self.accepted, logger, "Custom palette colors")
4853

4954
def get_color_cycle(self) -> list[tuple[str, float]]:
5055
return [(box.color.name(), box.color.alphaF()) for box in self._color_boxes]

src/ert/gui/plotting/widgets/plot_controls/everest_controls_plot_options.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from collections.abc import Callable
23

34
from PyQt6.QtWidgets import (
@@ -6,8 +7,11 @@
67
QRadioButton,
78
)
89

10+
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
911
from ert.gui.plotting.utils.qt_creator import create_group_box, create_group_layout
1012

13+
logger = logging.getLogger(__name__)
14+
1115

1216
class EverestControlsPlotOptions:
1317
def __init__(self, connection_point: Callable[..., object]) -> None:
@@ -21,7 +25,11 @@ def __init__(self, connection_point: Callable[..., object]) -> None:
2125
self._display_over_button_group.addButton(self._display_over_batches_radio)
2226
self._display_over_button_group.addButton(self._display_over_controls_radio)
2327
self._display_over_button_group.buttonClicked.connect(connection_point)
24-
28+
log_plot_option_usage_once(
29+
self._display_over_button_group.buttonClicked,
30+
logger,
31+
"X-axis display option",
32+
)
2533
self._display_over_group = create_group_box(
2634
"X-axis:",
2735
create_group_layout(

src/ert/gui/plotting/widgets/plot_controls/general_options.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
QWidget,
1010
)
1111

12+
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
1213
from ert.gui.plotting.utils.qt_creator import create_group_box, create_group_layout
1314

1415
from .observation_color import ObservationColorEdit
@@ -121,14 +122,6 @@ def get_observations_color(self) -> tuple[str, float]:
121122
return self._observations_color_edit.get_observations_color()
122123

123124

124-
def log_option_usage(checkbox: QCheckBox, option_name: str) -> None:
125-
def log_usage(_checked: bool) -> None:
126-
logger.info("Plot sidebar option used: '%s'", option_name)
127-
checkbox.clicked.disconnect(log_usage)
128-
129-
checkbox.clicked.connect(log_usage)
130-
131-
132125
def create_checkbox_with_tooltip(
133126
name: str,
134127
tooltip: str,
@@ -141,5 +134,5 @@ def create_checkbox_with_tooltip(
141134
checkbox.setToolTip(tooltip)
142135
checkbox.setChecked(initial_checked)
143136
checkbox.stateChanged.connect(connection_point)
144-
log_option_usage(checkbox, name)
137+
log_plot_option_usage_once(checkbox.clicked, logger, name)
145138
return checkbox

src/ert/gui/plotting/widgets/plot_controls/misfits_options.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import logging
12
from collections.abc import Callable
23

34
from PyQt6.QtWidgets import (
45
QCheckBox,
56
QGroupBox,
67
)
78

9+
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
810
from ert.gui.plotting.utils.qt_creator import create_group_box, create_group_layout
911

12+
logger = logging.getLogger(__name__)
13+
1014

1115
class MisfitsOptions:
1216
def __init__(self, connection_point: Callable[..., object]) -> None:
@@ -35,6 +39,16 @@ def __init__(self, connection_point: Callable[..., object]) -> None:
3539
]
3640
),
3741
)
42+
log_plot_option_usage_once(self._toggle_mean.stateChanged, logger, "Show mean")
43+
log_plot_option_usage_once(
44+
self._toggle_outliers.stateChanged, logger, "Show outliers"
45+
)
46+
log_plot_option_usage_once(
47+
self._toggle_scatter_plot.stateChanged, logger, "Show scatter"
48+
)
49+
log_plot_option_usage_once(
50+
self._toggle_box.stateChanged, logger, "Show box plot"
51+
)
3852

3953
@property
4054
def mean_checkbox_state(self) -> bool:

src/ert/gui/plotting/widgets/plot_controls/observation_color.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import logging
12
from collections.abc import Callable
23

34
from PyQt6.QtWidgets import QCheckBox, QHBoxLayout, QLabel, QWidget
45

56
from ert.gui.plotting.customization_dialog.color_chooser import ColorBox
7+
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
68
from ert.gui.plotting.utils.plot_config import PlotConfig
79

10+
logger = logging.getLogger(__name__)
11+
812

913
class ObservationColorEdit(QWidget):
1014
def __init__(
@@ -19,6 +23,9 @@ def __init__(
1923
self._observations_color_box.colorChanged.connect(
2024
lambda _color: connection_point()
2125
)
26+
log_plot_option_usage_once(
27+
self._observations_color_box.colorChanged, logger, "Observation color"
28+
)
2229

2330
layout = QHBoxLayout(self)
2431
layout.setContentsMargins(20, 0, 0, 0)

src/ert/gui/plotting/widgets/plot_controls/plot_color_palette_selector.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import logging
12
from collections.abc import Callable
23

34
from PyQt6.QtCore import Qt
45
from PyQt6.QtWidgets import QComboBox, QPushButton
56

67
from ert.gui.icon_utils import load_icon
8+
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
79
from ert.gui.plotting.utils.plot_color_palettes import PALETTES_WITH_DESCRIPTIONS
810
from ert.gui.plotting.widgets.plot_controls import CustomPaletteDialog
911

12+
logger = logging.getLogger(__name__)
13+
1014

1115
class PlotColorPaletteSelector(QComboBox):
1216
CUSTOM_PALETTE_NAME = "Custom"
@@ -31,6 +35,7 @@ def __init__(self, connection_point: Callable[..., object]) -> None:
3135
Qt.ItemDataRole.ToolTipRole,
3236
)
3337
self.activated.connect(connection_point)
38+
log_plot_option_usage_once(self.activated, logger, "Color palette")
3439
self.custom_palette_button = QPushButton(
3540
load_icon("add_circle_outlined.svg"), "Create custom palette"
3641
)
@@ -39,6 +44,9 @@ def __init__(self, connection_point: Callable[..., object]) -> None:
3944
"The selected palette will be applied to all plots."
4045
)
4146
self.custom_palette_button.clicked.connect(self._open_custom_palette_dialog)
47+
log_plot_option_usage_once(
48+
self.custom_palette_button.clicked, logger, "Create custom palette"
49+
)
4250

4351
def _open_custom_palette_dialog(self) -> None:
4452
if self.CUSTOM_PALETTE_NAME in PALETTES_WITH_DESCRIPTIONS:

tests/ert/unit_tests/gui/plotting/widgets/test_custom_palette_dialog.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
from ert.gui.plotting.utils.plot_color_palettes import MINIMUM_COLOR_CYCLE_LENGTH
24
from ert.gui.plotting.widgets.plot_controls.custom_palette_dialog import (
35
CustomPaletteDialog,
@@ -19,3 +21,17 @@ def test_that_dialog_exposes_default_amount_of_color_boxes(qtbot):
1921
qtbot.addWidget(dialog)
2022

2123
assert len(dialog.get_color_cycle()) == MINIMUM_COLOR_CYCLE_LENGTH
24+
25+
26+
def test_that_accepting_the_dialog_logs_sidebar_usage_once(qtbot, caplog):
27+
dialog = CustomPaletteDialog([])
28+
qtbot.addWidget(dialog)
29+
30+
expected_message = "Plot sidebar option used: 'Custom palette colors'"
31+
32+
with caplog.at_level(logging.INFO):
33+
dialog.accept()
34+
assert [r.getMessage() for r in caplog.records].count(expected_message) == 1
35+
36+
dialog.accept()
37+
assert [r.getMessage() for r in caplog.records].count(expected_message) == 1

tests/ert/unit_tests/gui/plotting/widgets/test_everest_controls_plot_options.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from unittest.mock import Mock
23

34
from PyQt6.QtCore import Qt
@@ -31,3 +32,23 @@ def test_that_toggling_everest_controls_plot_options_invokes_the_connection_poin
3132
qtbot.mouseClick(controls_radio, Qt.MouseButton.LeftButton)
3233

3334
connection_point.assert_called()
35+
36+
37+
def test_that_selecting_x_axis_display_option_logs_sidebar_usage_once(qtbot, caplog):
38+
options = EverestControlsPlotOptions(Mock())
39+
widget = options.get_widget()
40+
qtbot.addWidget(widget)
41+
widget.show()
42+
43+
controls_radio = widget.findChild(QRadioButton, "display_over_controls_radio")
44+
batches_radio = widget.findChild(QRadioButton, "display_over_batches_radio")
45+
assert controls_radio is not None
46+
assert batches_radio is not None
47+
expected_message = "Plot sidebar option used: 'X-axis display option'"
48+
49+
with caplog.at_level(logging.INFO):
50+
qtbot.mouseClick(controls_radio, Qt.MouseButton.LeftButton)
51+
assert [r.getMessage() for r in caplog.records].count(expected_message) == 1
52+
53+
qtbot.mouseClick(batches_radio, Qt.MouseButton.LeftButton)
54+
assert [r.getMessage() for r in caplog.records].count(expected_message) == 1

tests/ert/unit_tests/gui/plotting/widgets/test_misfits_options.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import logging
12
from unittest.mock import Mock
23

4+
import pytest
5+
36
from ert.gui.plotting.widgets.plot_controls.misfits_options import MisfitsOptions
47

58

@@ -36,3 +39,29 @@ def test_that_toggling_a_misfits_checkbox_invokes_the_connection_point(qtbot):
3639
options.scatter_checkbox_state = not options.scatter_checkbox_state
3740

3841
connection_point.assert_called()
42+
43+
44+
@pytest.mark.parametrize(
45+
("checkbox_attribute", "option_name"),
46+
[
47+
("_toggle_mean", "Show mean"),
48+
("_toggle_outliers", "Show outliers"),
49+
("_toggle_scatter_plot", "Show scatter"),
50+
("_toggle_box", "Show box plot"),
51+
],
52+
)
53+
def test_that_toggling_a_misfits_option_logs_sidebar_usage_once(
54+
qtbot, caplog, checkbox_attribute, option_name
55+
):
56+
options = MisfitsOptions(Mock())
57+
qtbot.addWidget(options.get_widget())
58+
59+
checkbox = getattr(options, checkbox_attribute)
60+
expected_message = f"Plot sidebar option used: '{option_name}'"
61+
62+
with caplog.at_level(logging.INFO):
63+
checkbox.toggle()
64+
assert [r.getMessage() for r in caplog.records].count(expected_message) == 1
65+
66+
checkbox.toggle()
67+
assert [r.getMessage() for r in caplog.records].count(expected_message) == 1

0 commit comments

Comments
 (0)