Skip to content

Commit

Permalink
Adjust histogram boundaries when having constant distribution: min==max
Browse files Browse the repository at this point in the history
- Add test for plot histogram when having a constant distribution
  • Loading branch information
xjules committed Oct 21, 2024
1 parent ebc4d03 commit 3b1587a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/ert/gui/tools/plot/plottery/plots/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def plotHistogram(
),
)
else:
if minimum is not None and maximum is not None and minimum == maximum:
minimum -= 0.1
maximum += 0.1
config.addLegendItem(
ensemble.name,
_plotHistogram(
Expand Down
42 changes: 41 additions & 1 deletion tests/ert/unit_tests/gui/plottery/test_histogram.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from unittest.mock import Mock
from unittest.mock import ANY, Mock

import pandas as pd
import pytest
from matplotlib.figure import Figure

import ert
from ert.gui.tools.plot.plot_api import EnsembleObject
from ert.gui.tools.plot.plottery import PlotConfig, PlotContext
from ert.gui.tools.plot.plottery.plots import HistogramPlot
Expand Down Expand Up @@ -70,3 +71,42 @@ def test_histogram(plot_context: PlotContext, ensemble_to_data_map):
{},
)
return figure


def test_histogram_plot_for_constant_distribution(monkeypatch):
# test that the histogram plot is called with the correct min and max values
# when all the parameter values are the same
context = Mock(spec=PlotContext)
context.log_scale = False
context.ensembles.return_value = [
EnsembleObject("ensemble_1", "id", False, "experiment_1")
]
title = "Histogram with same values"
context.plotConfig.return_value = PlotConfig(title=title)
value = 0
data_map = dict.fromkeys(context.ensembles(), pd.DataFrame([10 * [value]]))
min_value = value - 0.1
max_value = value + 0.1
figure = Figure()
mock_plot_histogram = Mock()
monkeypatch.setattr(
ert.gui.tools.plot.plottery.plots.histogram,
"_plotHistogram",
mock_plot_histogram,
)
HistogramPlot().plot(
figure,
context,
data_map,
pd.DataFrame(),
{},
)
mock_plot_histogram.assert_called_once_with(
ANY,
ANY,
ANY,
ANY,
ANY,
min_value,
max_value,
)

0 comments on commit 3b1587a

Please sign in to comment.