NDCube does not support arithmetic via numpy ufuncs

Open
#591 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
numpy, python
Domain
data

Research direction

Start with NDCube's existing add and related arithmetic methods, then read the NumPy NEP 13 recommendations linked in the issue. Implementing array_ufunc should cover the listed ufunc cases, route Python operators through NumPy ufuncs, and preserve NotImplemented behavior for operands with array_ufunc = None.

Written by the indexing model from the issue text.

Description

Describe the bug

Numpy ufuncs are things like np.add and np.equal, currently we support arithmetic methods via the standard Python operands like + (through __add__) but not the equivalent ufunc.

I discovered this issue because the following doesn't work:

cube = NDCube(...)
arr = np.array([cube], dtype=object)
np.equal(arr, cube)

In addition some things like np.maximum and np.isinf might be quite nice to have working without having to access .data, also if we implement __array_ufunc__ we can have those things return quantities.


See Recommendations for implementing binary operations in the NEP for how I think we need to go about this.

The overview is:

  • Implement all the arithmetic methods in __array_ufunc__.
  • Have the Python operator methods __add__ etc, call the numpy ufunc, i.e. __add__ calls np.add.
  • Ensure that our operator methods return NotImplemented if the other operand has __array_ufunc__ = None.
Dominant language
Python
Stars
49
Forks
56
Avg merge
5h 54m
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sunpy/ndcube

All issues in sunpy/ndcube

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.