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

np.median doesn't support read-only arrays #3070

Open
EmptySamurai opened this issue Jun 30, 2018 · 2 comments
Open

np.median doesn't support read-only arrays #3070

EmptySamurai opened this issue Jun 30, 2018 · 2 comments
Projects

Comments

@EmptySamurai
Copy link

np.median implementation in Numba doesn't support read-only arrays, while non-jitted version does. When I launch the snippet below

import numpy as np
from numba import jit

@jit(nopython=True)
def median(arr):
    return np.median(arr)

x = np.random.random(100)
x.setflags(write=False)

median(x)

I got an error

TypeError: Cannot modify value of type readonly array(float64, 1d, C)

My setup:

  • Python 3.5.2 (Anaconda)
  • numba==0.38.1
  • numpy==1.14.2
@EmptySamurai
Copy link
Author

EmptySamurai commented Jun 30, 2018

It seems that problem is with .flatten() method, that is used by np.median.

First of all the following snippet produces the same error

import numpy as np
from numba import jit

@jit(nopython=True)
def flatten_and_change(arr):
    result = arr.flatten()
    result[0]=1
    return result

x = np.random.random(100)
x.setflags(write=False)
flatten_and_change(x)
numba.errors.InternalError: Cannot modify value of type readonly array(float64, 1d, C)

And second, I just can't return flattened array (maybe this should be considered as separate bug)

import numpy as np
from numba import jit

@jit(nopython=True)
def flatten(arr):
    return arr.flatten()

x = np.random.random(100)
x.setflags(write=False)
flatten(x)
.......
.......
~/anaconda3/lib/python3.6/site-packages/numba/targets/arrayobj.py in array_to_array(context, builder, fromty, toty, val)
   4435 def array_to_array(context, builder, fromty, toty, val):
   4436     # Type inference should have prevented illegal array casting.
-> 4437     assert toty.layout == 'A'
   4438     return val
   4439 

LoweringError: Failed at nopython (nopython mode backend)
Failed at nopython (nopython mode backend)

File "../../../../../../../../home/emptysamurai/anaconda3/lib/python3.6/site-packages/numba/targets/arrayobj.py", line 1593
[1] During: lowering "$0.8 = cast(value=$0.7)" at /home/emptysamurai/anaconda3/lib/python3.6/site-packages/numba/targets/arrayobj.py (1593)
[2] During: lowering "$0.3 = call $0.2(func=$0.2, args=[], kws=(), vararg=None)" at <ipython-input-40-8e9bf9ef1f40> (6)

@stuartarchibald
Copy link
Contributor

Thanks for the report. I think #3070 (comment) is an oversight and a bug. .flatten() should return a mutable copy.

For the second example (returning flattened array), I can't reproduce on 0.39rc1, I think 24df695 (#2668) fixed it?

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

No branches or pull requests

3 participants