Skip to content

numpy apply_along_axis drops values after casting incorrectly #5193

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

Open
jlippi opened this issue Oct 16, 2014 · 1 comment
Open

numpy apply_along_axis drops values after casting incorrectly #5193

jlippi opened this issue Oct 16, 2014 · 1 comment

Comments

@jlippi
Copy link

jlippi commented Oct 16, 2014

Hello,
I have found that when I make the following call using apply_along_axis and return an int rather than a float, the field with the incorrect type is cast to the right type and put into the array properly, but the value of the subsequent field is set to zero:

def f0(m,l,val):
    return np.apply_along_axis(f1,0,m,m,l,val)

def f1(v,m,l,val):
    return np.apply_along_axis(f2,0,m,v,l,val)

def f2(v1,v2,l,val):
    l[0] += 1
    if l[0] <= 2:
        return 1.
    if l[0] == 3:
        return val
    if l[0] == 4:
        return .34151
print f0(np.array(np.ones((1,2))),list([0]),0) <-- int
print f0(np.array(np.ones((1,2))),list([0]),0.) <-- float
print f0(np.array(np.ones((1,2))),list([0]),1) <-- int
print f0(np.array(np.ones((1,2))),list([0]),1.) <-- float
print type(f0(np.array(np.ones((1,2))),list([0]),1)[0][1])
print numpy.__version__

output:

[[ 1.  0.] <-- correctly zero
 [ 1.  0.]] <-- value dropped
[[ 1.       0.     ] <-- correctly zero
 [ 1.       0.34151]] <-- value correct
[[ 1.  1.] <-- correctly 1
 [ 1.  0.]] <-- value dropped
[[ 1.       1.     ] <-- correctly 1
 [ 1.       0.34151]] <-- value correct
<type 'numpy.float64'> <-- type is float64, this was correctly cast
1.8.2 <-- numpy version

discovered with @asna1005

@eric-wieser
Copy link
Member

apply_along_axis looks at the return type of the first function call, and then casts all future calls to that same type. This is basically the same issue as #8352

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

No branches or pull requests

2 participants