Summing numpy.ndarray fails when passed to a thread
<p>I'm writing a Python script that interfaces to a frame grabber via a call to ctypes. It gets an image from the frame grabber as a pointer to a byte array. I then want to sum the array along one of the dimensions to plot a profile. This works fine when I do this in the same thread. But I want to hand off to a different thread for processing (so I can grab images as fast as possible). When I do this, the numpy.sum method seems to be silently overflowing internally, because the profiles I get back have negative numbers in them. Any idea what might be happening here? Here's a code fragment:</p>

<pre><code>self.grablib.IC_SnapImage(self.hGrabber, 100) # 100 ms timeout
imgPtr = self.grablib.IC_GetImagePtr(self.hGrabber)
#imgPtr is returned as a char pointer, which will mess things up
imgPtr2 = ctypes.cast(imgPtr, ctypes.POINTER(ctypes.c_byte))
imgArray = np.ctypeslib.as_array(imgPtr2, (572, 768))
imgTime = datetime.now()
args = (imgArray, imgTime)
Thread(target=self.processImage, args=args).start()
</code></pre>

<p>And here's the offending code inside the processing thread:</p>

<pre><code># Another thread to actually do the processing, while the previous one gets another image
def processImage(self, imgArray, imgTime):
    iy = np.sum(imgArray[range(0, 572, 2)], 1, dtype=np.float)
</code></pre>

<p>It doesn't seem to matter what I put as the <code>dtype</code> argument in <code>sum</code>, the overflowing still happens. It's as if numpy's accumulator is defaulting to the wrong type, and not getting overruled by the <code>dtype</code> argument.</p>