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

__mul__ and __add__ compatible with user classes #3577

Closed
nr7s opened this issue Mar 25, 2019 · 3 comments · Fixed by #5073
Closed

__mul__ and __add__ compatible with user classes #3577

nr7s opened this issue Mar 25, 2019 · 3 comments · Fixed by #5073

Comments

@nr7s
Copy link

nr7s commented Mar 25, 2019

I have a custom class that holds the plot and extra data. I wanted to be able to keep using the holoviews operators in the same way with my class.
I implemented both __add__ and __radd__ on my class, while __add__ worked just fine, __radd__ was never triggered because holoviews wasn't raising a NotImplementedError on the __mul__ and holoviews shouldn't be expecting my class. Is it possible to add a check for unknown objects in the operators or that would break other parts of the code?

Here's the traceback I got when trying to do something like hvplot * myclass.

Traceback
/usr/local/anaconda3/envs/pkg/lib/python3.6/site-packages/holoviews/core/overlay.py in __mul__(self, other)
     41             return NotImplemented
     42 
---> 43         return Overlay([self, other])
     44 
     45 

/usr/local/anaconda3/envs/pkg/lib/python3.6/site-packages/holoviews/core/overlay.py in __init__(self, items, group, label, **params)
    141         self.__dict__['_group'] = group
    142         self.__dict__['_label'] = label
--> 143         super(Overlay, self).__init__(items, **params)
    144 
    145     def __getitem__(self, key):

/usr/local/anaconda3/envs/pkg/lib/python3.6/site-packages/holoviews/core/dimension.py in __init__(self, items, identifier, parent, **kwargs)
   1475         params = {p: kwargs.pop(p) for p in list(self.params().keys())+['id', 'plot_id'] if p in kwargs}
   1476 
-> 1477         AttrTree.__init__(self, items, identifier, parent, **kwargs)
   1478         Dimensioned.__init__(self, self.data, **params)
   1479 

/usr/local/anaconda3/envs/pkg/lib/python3.6/site-packages/holoviews/core/tree.py in __init__(self, items, identifier, parent, dir_mode)
     67         items = list(items) if items else items
     68         items = [] if not items else items
---> 69         for path, item in items:
     70             self.set_path(path, item)
     71 

/usr/local/anaconda3/envs/pkg/lib/python3.6/site-packages/holoviews/core/element.py in __iter__(self)
     89     def __iter__(self):
     90         "Disable iterator interface."
---> 91         raise NotImplementedError('Iteration on Elements is not supported.')
     92 
     93 

NotImplementedError: Iteration on Elements is not supported.
@philippjfr
Copy link
Member

Sorry I never replied, adding a check for other types of objects seems very reasonable.

@douglas-raillard-arm
Copy link
Contributor

Holoviews should actually return NotImplemented rather than raising NotImplementedError in order to fall back on the reflected operation on the 2nd operand:
https://docs.python.org/3/library/constants.html#NotImplemented

I ran into the same issue while trying to wrap a holoviews plot such that it is displayed inside a panel:
https://discourse.holoviz.org/t/replace-holoviews-notebook-rendering-with-a-panel/2519/12

douglas-raillard-arm added a commit to douglas-raillard-arm/holoviews that referenced this issue Sep 2, 2021
* Add layout.Layoutable as a mirror of overlay.Overlayable
* Remove a good deal of duplicated code
* Remove broken calls to super().__radd__ where the super class does not
  implement __radd__
* Return NotImplemented when Layout([x,y]) raises NotImplementedError.
  This allows correct interoperability with external classes that could
  themselves define __radd__ as stated by:
  https://docs.python.org/3/library/constants.html#NotImplemented

Fixes holoviz#3577
douglas-raillard-arm added a commit to douglas-raillard-arm/holoviews that referenced this issue Sep 2, 2021
* Return NotImplemented when appropriate
* Deduplicate code between 2 non-trivial and almost identical
  implementations of __mul__
* Fix non-inheritance-friendly type checking with a local import to
  avoid cyclic dependency

Fixes holoviz#3577
philippjfr pushed a commit that referenced this issue Nov 16, 2021
* layout: Fix __add__ and __radd__ implementation

* Add layout.Layoutable as a mirror of overlay.Overlayable
* Remove a good deal of duplicated code
* Remove broken calls to super().__radd__ where the super class does not
  implement __radd__
* Return NotImplemented when Layout([x,y]) raises NotImplementedError.
  This allows correct interoperability with external classes that could
  themselves define __radd__ as stated by:
  https://docs.python.org/3/library/constants.html#NotImplemented

Fixes #3577

* overlay: deduplicate and fix __mul__

* Return NotImplemented when appropriate
* Deduplicate code between 2 non-trivial and almost identical
  implementations of __mul__
* Fix non-inheritance-friendly type checking with a local import to
  avoid cyclic dependency

Fixes #3577
philippjfr pushed a commit that referenced this issue Nov 16, 2021
* layout: Fix __add__ and __radd__ implementation

* Add layout.Layoutable as a mirror of overlay.Overlayable
* Remove a good deal of duplicated code
* Remove broken calls to super().__radd__ where the super class does not
  implement __radd__
* Return NotImplemented when Layout([x,y]) raises NotImplementedError.
  This allows correct interoperability with external classes that could
  themselves define __radd__ as stated by:
  https://docs.python.org/3/library/constants.html#NotImplemented

Fixes #3577

* overlay: deduplicate and fix __mul__

* Return NotImplemented when appropriate
* Deduplicate code between 2 non-trivial and almost identical
  implementations of __mul__
* Fix non-inheritance-friendly type checking with a local import to
  avoid cyclic dependency

Fixes #3577
maximlt added a commit that referenced this issue Dec 16, 2021
* Fix Violin matplotlib rendering with non-finite values (#5135)

* Handle the empty string as a group name (#5131)

* Fix unhandled numpy.round overflow return value in core/util.py:bound_range(...) (#5095)

* Core/Util: Fix unhandled np.round overflow return

The function numpy.round return for the input
np.float64(2.6558061446181644e+295) the output
numpy.inf. This should lead to
density = full_precision_density.

* Core/Util: Supress numpy.round overflow error

The function numpy.round produces for the input
numpy.float64(2.6558061446181644e+295) an
FloatingPointError with the message
"overflow encountered in multiply". This error
should be suppressed because its an internal
computation by holoview.

* Use context manager

Co-authored-by: christoph.weiss@gtd-gmbh.de <christoph.weiss@gtd-gmbh.de>
Co-authored-by: Philipp Rudiger <prudiger@anaconda.com>

* Update Plotting_with_Matplotlib.ipynb (#4983)

Ordering of the fig_bounds tuple in the documentation was different from that in e.g. https://github.com/holoviz/holoviews/blob/40977c515dd9837019aaa0e5708773e78809fbe1/holoviews/plotting/mpl/plot.py#L69

* Notimplemented binop (#5073)

* layout: Fix __add__ and __radd__ implementation

* Add layout.Layoutable as a mirror of overlay.Overlayable
* Remove a good deal of duplicated code
* Remove broken calls to super().__radd__ where the super class does not
  implement __radd__
* Return NotImplemented when Layout([x,y]) raises NotImplementedError.
  This allows correct interoperability with external classes that could
  themselves define __radd__ as stated by:
  https://docs.python.org/3/library/constants.html#NotImplemented

Fixes #3577

* overlay: deduplicate and fix __mul__

* Return NotImplemented when appropriate
* Deduplicate code between 2 non-trivial and almost identical
  implementations of __mul__
* Fix non-inheritance-friendly type checking with a local import to
  avoid cyclic dependency

Fixes #3577

* Fix matplotlib colorbar labeling for dim expressions (#5137)

* Ensure FreehandDraw renders when styles set (#5139)

* Fix datetime clipping on RangeXY stream (#5138)

* Fix Bars legend error when overlaid with annotation (#5142)

* Do not raise deprecated .opts warning for empty groups (#5144)

* Fix plotly Bar plots containing nans (#5143)

* Fix plotly Bar plots containing nans

* Update tests

* Preserve cols when overlaying on layout (#5141)

* Do not merge partially overlapping Stream callbacks (#5133)

* Do not merge partially overlapping Stream callbacks

* Add tests

* Remove print

* Add bounds to the cache_size Parameter (#5105)

* Fix broken link in Gridded user guide (#5098)

* Pin freetype on Windows due to matplotlib error (#5109)

* Fixed typo

* import bokeh's version from an internal util module (#5103)

* Add current_key property to DynamicMap (#5106)

* Validate dimensionality of xarray interface data (#5140)

* Support xyzservices.TileProvider as hv.Tiles input (#5062)

* implementation

* docstring, plotly test

* add bokeh tests

* CI: Fix before release (#5151)

* delay projection comparison to optimize geoviews (#5152)

* Test suite maintenance (#5157)

Fixed or suppressed warnings issued while running the tests

* fix cherry-pick and compatibility with holoviews 1.14

* fix linting

* Handle unsigned integer dtype in datashader aggregate operation (#5149)

* Fix docs CI build (#5088)

* Remove conda-forge from the build channels, post-install awscli and pin jupyter_client

* remove pytest usage

* add missing comma and fix test

* CI: improvements to the docs build (#5161)

* Fix for Contours consistent of empty and nonempty paths (#5162)

* Switch to the PyData sphinx theme (#5163)

* use pydata sphinx theme to build the site

* add pooch to the docs dependencies

* Add release notes for 1.14.6 and 1.14.7 (#5160)

* attempt to fix pip build

* micro fixes to the release notes

Co-authored-by: James A. Bednar <jbednar@users.noreply.github.com>
Co-authored-by: w31t1 <Christoph.Weiss1@gmx.de>
Co-authored-by: christoph.weiss@gtd-gmbh.de <christoph.weiss@gtd-gmbh.de>
Co-authored-by: jenssss <37403847+jenssss@users.noreply.github.com>
Co-authored-by: Douglas Raillard <douglas.raillard@arm.com>
Co-authored-by: Maxime Liquet <35924738+maximlt@users.noreply.github.com>
Co-authored-by: James A. Bednar <jbednar@continuum.io>
Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
Co-authored-by: maximlt <mliquet@anaconda.com>
Co-authored-by: Simon Høxbro Hansen <simon.hansen@me.com>
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants