Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do casting rules differ between computers in python?

I am running python 2.7 on my Mac, and I'm working on a group coding project with other people using Ubuntu. Every once and a while, code they write won't work on my computer due to casting rule errors:

    273     # Apply column averages to image
--> 274     img[:middle] *= (bg[0]/np.tile(topCol, (middle,1)))
    275     img[middle:] *= bg[1]/np.tile(botCol, (middle,1))
    276 

TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'

I don't think you need the specifics, since this happens in a few different places with different number types.

It works on all of their computers no problem. Everything I write works for them, but every so often what they write doesn't work for me.

Is there a reason why our machines don't agree and is there a way I can change things on my end?

Thanks!

like image 242
zachd1_618 Avatar asked Jan 10 '13 23:01

zachd1_618


1 Answers

This thread suggests that your numpy is newer than the version your colleagues are using (please check using numpy.version.version). In the 1.7.0 development branch, it seems they've changed the implicit casting rule to the more strict same_kind rule, which forbids (among other things) casts between floating-point and integer formats.

To work around this, I'd recommend using code like this:

img[:middle] *= (bg[0]/np.tile(topCol, (middle,1))).astype(img.dtype)
like image 175
nneonneo Avatar answered Nov 10 '22 15:11

nneonneo