hgrecco / pint

Operate and manipulate physical quantities in Python

Home Page:http://pint.readthedocs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Plotting to matplotlib axis with `units=None` leads to ConversionError in 0.23, but not in 0.22

matrss opened this issue · comments

When upgrading pint to 0.23 we encountered this issue in MSS:

Traceback (most recent call last):
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/matplotlib/axis.py", line 1770, in convert_units
    ret = self.converter.convert(x, self.units, self)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/pint/matplotlib.py", line 39, in convert
    return value.to(unit).magnitude
           ^^^^^^^^^^^^^^
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 528, in to
    magnitude = self._convert_magnitude_not_inplace(other, *contexts, **ctx_kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/pint/facets/plain/quantity.py", line 472, in _convert_magnitude_not_inplace
    return self._REGISTRY.convert(self._magnitude, self._units, other)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/pint/facets/plain/registry.py", line 1028, in convert
    return self._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/pint/facets/context/registry.py", line 404, in _convert
    return super()._convert(value, src, dst, inplace)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/pint/facets/nonmultiplicative/registry.py", line 247, in _convert
    dst_offset_unit = self._validate_and_extract(dst)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/pint/facets/nonmultiplicative/registry.py", line 167, in _validate_and_extract
    (u, e) for u, e in units.items() if not self._is_multiplicative(u)
                       ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'items'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/icg149/Projects/MSS/mslib/msui/msui_mainwindow.py", line 899, in create_view_handler
    self.create_view(_type, self.mscolab.waypoints_model)
  File "/home/icg149/Projects/MSS/mslib/msui/msui_mainwindow.py", line 922, in create_view
    view_window = sideview.MSUISideViewWindow(model=model)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/Projects/MSS/mslib/msui/sideview.py", line 260, in __init__
    self.setupUi(self)
  File "/home/icg149/Projects/MSS/mslib/msui/qt5/ui_sideview_window.py", line 19, in setupUi
    self.mpl = MplSideViewWidget(self.centralwidget)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/Projects/MSS/mslib/msui/mpl_qtwidget.py", line 1360, in __init__
    sideview=True, parent=parent, canvas=MplSideViewCanvas())
                                         ^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/Projects/MSS/mslib/msui/mpl_qtwidget.py", line 1159, in __init__
    self.plotter.setup_side_view()
  File "/home/icg149/Projects/MSS/mslib/msui/mpl_qtwidget.py", line 452, in setup_side_view
    self.redraw_yaxis()
  File "/home/icg149/Projects/MSS/mslib/msui/mpl_qtwidget.py", line 481, in redraw_yaxis
    ax.set_yticks(minor_ticks, minor=True)
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 74, in wrapper
    return get_method(self)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/matplotlib/axis.py", line 2123, in set_ticks
    result = self._set_tick_locations(ticks, minor=minor)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/matplotlib/axis.py", line 2063, in _set_tick_locations
    ticks = self.convert_units(ticks)
            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/icg149/mambaforge/envs/mssdev-311/lib/python3.11/site-packages/matplotlib/axis.py", line 1772, in convert_units
    raise munits.ConversionError('Failed to convert value(s) to axis '
matplotlib.units.ConversionError: Failed to convert value(s) to axis units: <Quantity([], 'pascal')>

I've tracked this issue down to be caused by the changes in #1819, i.e. if I reverse that change locally the issue is gone.

I think this is caused by the matplotlib axis units not being set, i.e. being None, which sounds like an issue on our (MSS) end, but I am not sure yet why it worked previously and what it did then.

We have an issue for this on our end as well, in Open-MSS/MSS#2162.