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

histplot() with histtype="errorbar" crashes when data contains inf or nan #390

Closed
mreininghaus opened this issue Jun 23, 2022 · 3 comments · Fixed by #391
Closed

histplot() with histtype="errorbar" crashes when data contains inf or nan #390

mreininghaus opened this issue Jun 23, 2022 · 3 comments · Fixed by #391

Comments

@mreininghaus
Copy link

When I make ratio plots of two histograms, the resulting histogram bins often contain inf or nan values. If I try to plot it with mplhep.histplot(), I get an error when I add the histtype="errorbar" option (no crash when using the default `histtype="step"). I'm using version 0.3.25. A few months back, this used to work without issues.

Code to reproduce the error:

import boost_histogram as bh
import numpy as np
import mplhep
import matplotlib.pyplot as plt

hist_ref = bh.Histogram(bh.axis.Regular(50, 0, 30))
hist_ref.fill(np.random.exponential(size=100000))

r = np.random.exponential(size=100000)
hist = bh.Histogram(bh.axis.Regular(50, 0, 30))
hist.fill(r)

fig, (ax,ax2) = plt.subplots(2,1, sharex=True, dpi=200)
mplhep.histplot(hist, ax=ax, yerr=False, histtype='errorbar', marker=None, linestyle="-")
mplhep.histplot(hist_ref, ax=ax, yerr=False, histtype='errorbar', marker=None, linestyle="-")

# this crashes:
mplhep.histplot((hist / hist_ref), ax=ax2, yerr=False, histtype='errorbar')

traceback:

TypeError                                 Traceback (most recent call last)
Input In [1], in <cell line: 16>()
     14 mplhep.histplot(hist, ax=ax, yerr=False, histtype='errorbar', marker=None, linestyle="-")
     15 mplhep.histplot(hist_ref, ax=ax, yerr=False, histtype='errorbar', marker=None, linestyle="-")
---> 16 mplhep.histplot((hist / hist_ref), ax=ax2, yerr=False, histtype='errorbar')

File ~/.local/lib/python3.9/site-packages/mplhep/plot.py:384, in histplot(H, bins, yerr, w2, w2method, stack, density, binwnorm, histtype, xerr, label, sort, edges, binticks, ax, **kwargs)
    382         _plot_info["yerr"] = None
    383     _plot_info["xerr"] = _xerr
--> 384     _e = ax.errorbar(
    385         **_plot_info,
    386         label=_labels[i],
    387         **soft_update_kwargs(_chunked_kwargs[i], err_defaults),
    388     )
    389     return_artists.append(ErrorBarArtists(_e))
    391 _artist = _e[0]

File ~/.local/lib/python3.9/site-packages/matplotlib/__init__.py:1412, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1409 @functools.wraps(func)
   1410 def inner(ax, *args, data=None, **kwargs):
   1411     if data is None:
-> 1412         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1414     bound = new_sig.bind(ax, *args, **kwargs)
   1415     auto_label = (bound.arguments.get(label_namer)
   1416                   or bound.kwargs.get(label_namer))

File ~/.local/lib/python3.9/site-packages/matplotlib/axes/_axes.py:3460, in Axes.errorbar(self, x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, **kwargs)
   3454 # This is like
   3455 #     elow, ehigh = np.broadcast_to(...)
   3456 #     return dep - elow * ~lolims, dep + ehigh * ~uplims
   3457 # except that broadcast_to would strip units.
   3458 low, high = dep + np.row_stack([-(1 - lolims), 1 - uplims]) * err
-> 3460 barcols.append(lines_func(
   3461     *apply_mask([indep, low, high], everymask), **eb_lines_style))
   3462 # Normal errorbars for points without upper/lower limits.
   3463 nolims = ~(lolims | uplims)

File ~/.local/lib/python3.9/site-packages/matplotlib/__init__.py:1412, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1409 @functools.wraps(func)
   1410 def inner(ax, *args, data=None, **kwargs):
   1411     if data is None:
-> 1412         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1414     bound = new_sig.bind(ax, *args, **kwargs)
   1415     auto_label = (bound.arguments.get(label_namer)
   1416                   or bound.kwargs.get(label_namer))

File ~/.local/lib/python3.9/site-packages/matplotlib/axes/_axes.py:1063, in Axes.hlines(self, y, xmin, xmax, colors, linestyles, label, **kwargs)
   1060 lines.update(kwargs)
   1062 if len(y) > 0:
-> 1063     minx = min(xmin.min(), xmax.min())
   1064     maxx = max(xmin.max(), xmax.max())
   1065     miny = y.min()

File ~/miniconda3/lib/python3.9/site-packages/numpy/ma/core.py:5748, in MaskedArray.min(self, axis, out, fill_value, keepdims)
   5746 # No explicit output
   5747 if out is None:
-> 5748     result = self.filled(fill_value).min(
   5749         axis=axis, out=out, **kwargs).view(type(self))
   5750     if result.ndim:
   5751         # Set the mask
   5752         result.__setmask__(newmask)

File ~/miniconda3/lib/python3.9/site-packages/numpy/core/_methods.py:44, in _amin(a, axis, out, keepdims, initial, where)
     42 def _amin(a, axis=None, out=None, keepdims=False,
     43           initial=_NoValue, where=True):
---> 44     return umr_minimum(a, axis, None, out, keepdims, initial, where)

TypeError: '<=' not supported between instances of 'float' and 'str'
@mreininghaus
Copy link
Author

The latest release that still works with the example code is 0.3.15. I start seeing the error with 0.3.17.

@andrzejnovak
Copy link
Member

Thanks for the report @mreininghaus. Indeed the tags seem right, as that's where the underlying logic was reorganized a bit. It's interesting the error only happens down the line in mpl but it apparently worked previously. I'll take a look.

@andrzejnovak
Copy link
Member

Alright, thanks @mreininghaus, this should be fixed in https://github.com/scikit-hep/mplhep/releases/tag/v0.3.26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants