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

Fixed Posterior plot errors with boolean array. #1707

Merged
merged 11 commits into from
Mar 23, 2022
21 changes: 13 additions & 8 deletions arviz/plots/backends/matplotlib/posteriorplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,23 @@ def format_axes():
rug=False,
show=False,
)
else:
elif values.dtype.kind == "i" or (values.dtype.kind == "f" and kind == "hist"):
if bins is None:
if values.dtype.kind == "i":
xmin = values.min()
xmax = values.max()
bins = get_bins(values)
ax.set_xlim(xmin - 0.5, xmax + 0.5)
else:
bins = "auto"
xmin = values.min()
xmax = values.max()
bins = get_bins(values)
ax.set_xlim(xmin - 0.5, xmax + 0.5)
kwargs.setdefault("align", "left")
kwargs.setdefault("color", "C0")
ax.hist(values, bins=bins, alpha=0.35, **kwargs)
elif values.dtype.kind == "b":
if bins is None:
bins = "auto"
kwargs.setdefault("color", "C0")
ax.bar(["False", "True"], [(~values).sum(), values.sum()], alpha=0.35, **kwargs)
hdi_prob = "hide"
utkarsh-maheshwari marked this conversation as resolved.
Show resolved Hide resolved
else:
raise TypeError("Values must be float, integer or boolean")
ahartikainen marked this conversation as resolved.
Show resolved Hide resolved

plot_height = ax.get_ylim()[1]

Expand Down
7 changes: 7 additions & 0 deletions arviz/tests/base_tests/test_plots_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,13 @@ def test_plot_posterior(models, kwargs):
assert axes.shape


def test_plot_posterior_boolean():
data = np.random.choice(a=[False, True], size=(4, 100))
axes = plot_posterior(data)
assert axes
ahartikainen marked this conversation as resolved.
Show resolved Hide resolved
assert axes.get_xticklabels()[0].get_text() != 0
Copy link
Contributor Author

@utkarsh-maheshwari utkarsh-maheshwari Jul 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is not the best way to see if xticklabels are correct. It should be like assert axes.get_xticklabels()[0].get_text() == 'False'.
When I tried it locally, tests work well only if I provide show=True to the plot function call but shows an assertion error when show=False (default).
AssertionError: assert ' ' == 'False'
Azure jobs fails with same error even if show=True is pushed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it could be solved using some non-interactive/directly writing to file backend? I don't reallly know what is happening, but it would not be surprising that Azure had some extra checks preventing guis, pop ups or other interactive objects

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already tried using plt.draw() in the matplotlib backend file, didn't work! I guess we need to call plt.draw() in the test function before assert or maybe leave it like that if its an "okayish" way to test if not good.



@pytest.mark.parametrize("kwargs", [{}, {"point_estimate": "mode"}, {"bins": None, "kind": "hist"}])
def test_plot_posterior_discrete(discrete_model, kwargs):
axes = plot_posterior(discrete_model, **kwargs)
Expand Down