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

TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('uint8') with casting rule 'same_kind' #7225

Closed
dessskris opened this issue Feb 10, 2016 · 14 comments

Comments

@dessskris
Copy link

I'm using Gizeh library and I get the above error upon installing the latest version of Numpy. There was no error with Numpy version 1.08.

File "animation/target_animation.py", line 161, in draw
    fill = gizeh.ImagePattern(self.bg.data, self.bg.pos, filter='best')
  File "build/bdist.linux-x86_64/egg/gizeh/gizeh.py", line 295, in __init__
  File "build/bdist.linux-x86_64/egg/gizeh/gizeh.py", line 50, in from_image
TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'

Is there a workaround or would you fix this issue please?

@charris
Copy link
Member

charris commented Feb 10, 2016

You need to make the cast explicit using the casting argument in the np.add ufunc. The behavior Gizeh depends on has been deprecated since 1.7.

@charris
Copy link
Member

charris commented Feb 10, 2016

That is casting='unsafe'.

@vishalgolcha
Copy link

where should i change this @charris ?

@seberg
Copy link
Member

seberg commented Jun 2, 2016

Typically this is code such as a += b and you need to make it np.add(a, b, out=a, casting="unsafe") (if you are sure you want the unsafe cast behaviour. Or, unless b is huge and it is safe, maybe just cast b first.

@vishalgolcha
Copy link

I get that I was talking specifically for the changes to be made to gizeh/moviepy .

@mantielero
Copy link

In my case, I had a similar issue. It was solved as suggested by seberg. I replaced line 40 in gizeh.py:

arr += image.flatten()

with line:

arr = np.add(arr, image.flatten(), out=arr, casting="unsafe")

I hope this helps somebody else.

@FilipSavic95
Copy link

I had this issue when subtracting like this:
yn -= self.y_mean

My error went away when I did the subtraction via numpy.subtract, like so:
yn = np.subtract(yn, self.y_mean, out=yn, casting="unsafe")

Thanks a lot @seberg 👍

@eric-wieser
Copy link
Member

@FilipSavic95: Could also do yn -= self.y_mean.astype(yn.dtype)

@FilipSavic95
Copy link

@eric-wieser Yeah, I've just tried your solution and it works. Thanks for posting it. 👍

@seberg
Copy link
Member

seberg commented Apr 10, 2018

The question is which precision you want to use for the operation itself. the unsafe casting will do the operation in the larger (rhs) precision (or the combined safe dtype) the other option will do the cast and thus the operation in the lower precision.

@WarrenWeckesser
Copy link
Member

This is a downstream issue (see Zulko/gizeh#25), so closing.

@ocaleanam
Copy link

Hello, looking for this specific error with search engines usually leads to the solutions above, either on this issue here or on StackExchange.

However I've encountered it differently using np.insert

I had a massive array containing huge integers, as well as an array of indices and another of values to insert in the first.
That's why all three arrays had dtype=np.uint (uint64)

If you go by :

a = np.arange(100)
b = np.array([12, 25, 3, 0, 46, 87])
c = np.array([7, 99, 97, 85, 23, 45])

np.insert(a, b, c)

...there's no problem, values of c are inserted in a at positions given by b.

However, if the indices array has dtype=np.uint :
np.insert(a, b.astype(np.uint), c)

---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-4-bc3dd4e8f3bc> in <module>
----> 1 np.insert(a, b.astype(np.uint64), c)

<__array_function__ internals> in insert(*args, **kwargs)

~/.local/lib/python3.8/site-packages/numpy/lib/function_base.py in insert(arr, obj, values, axis)
   4738     numnew = len(indices)
   4739     order = indices.argsort(kind='mergesort')   # stable sort
-> 4740     indices[order] += np.arange(numnew)
   4741 
   4742     newshape[axis] += numnew

UFuncTypeError: Cannot cast ufunc 'add' output from dtype('float64') to dtype('uint64') with casting rule 'same_kind'

Cast indices array to int64 to solve :
np.insert(a, b.astype(np.int64), c)

I didn't understand why the error was mentioning float64 whereas everything was uint64. I was trying to look for uint64 overflow or some other weird implicit conversion before I realized indices must be signed int64.

Hope it'll help if someone else gets here for the same reason.

@seberg
Copy link
Member

seberg commented Dec 20, 2021

What you are seeing is that uint64 + int64 returns float64 in NumPy, which is somewhat weird.

@ocaleanam
Copy link

Indeed, I was so pissed when I realized I'd lost so much time for this

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

9 participants