5

I occasionally use the where clause in numpy's ufuncs. For example, the following:

import numpy as np
a = np.linspace(-1, 1, 10)
np.sqrt(a, where=a>0) * (a>0)

In Numpy 1.12 and earlier, this used to give me square root values where possible and zero otherwise.

Recently, though, I upgraded to numpy 1.13. The code above now gives me the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Automatic allocation was requested for an iterator operand, and it was flagged as readable, but buffering  without delayed allocation was enabled

I thought that this was exactly how the where clause was supposed to be used, but perhaps I was wrong. So I have two questions: first, what's wrong with this code; and second, what is the recommended way of achieving my goal?

Kessel
  • 136
  • 6
  • This `where` is documented in the general `ufunc` optional keywords section: https://docs.scipy.org/doc/numpy-1.12.0/reference/ufuncs.html#optional-keyword-arguments – hpaulj Jun 23 '17 at 16:13
  • 1
    The error looks like something at `nditer` would issue. I'd look on github for changes or issues related to that. – hpaulj Jun 23 '17 at 16:19
  • This message is issued by line 1004 in numpy/numpy/core/src/multiarray/nditer_constr.c, the `nditer` source code. – hpaulj Jun 23 '17 at 16:33
  • There tests for the `where` parameter in `numpy/core/tests/test_ufunc.py` all include an `out` parameter. Does including an `out` array make a change in this 1.13 error? – hpaulj Jun 23 '17 at 17:34
  • @hpaulj Yeah, I found the same bit of source code by googling the error message, but really don't have a clue where to go next. I have never looked at numpy's internals before. I was hoping that some of you gurus might know :) – Kessel Jun 23 '17 at 17:36
  • @hpaulj That seems to fix it. Any idea what's going on? – Kessel Jun 23 '17 at 17:38
  • I assume it's a side effect of some other change, and these tests aren't comprehensive enough to catch it. – hpaulj Jun 23 '17 at 17:54
  • thank u .. it works –  Jun 23 '17 at 19:00

1 Answers1

3

For future reference: this turned out to be a bug in numpy. It has been fixed for the next numpy release, presumably version 1.13.1.

A workaround fix for 1.13.0 is to explicitly provide an out parameter to the ufunc. In the example above, np.sqrt(a, where=a>0, out=np.zeros(a.shape)) works.

Kessel
  • 136
  • 6
  • A discussion of this weird behavoiur can be found in [here](https://stackoverflow.com/questions/45544189/what-additional-work-is-done-by-np-power). Essentially because you don't give an out array a np.empty array is used as out array – Jürg W. Spaak Aug 07 '17 at 12:33