You can fix it by replacing the line:
CM = ndimage.measurements.center_of_mass(roi_data)
with the lines:
import numpy # Unnecessary if you've already done this.
CM = ndimage.measurements.center_of_mass(numpy.array(roi_data))
Explanation: from your comments, roi_data
is a NumPy memory-mapped array. The ndimage.measurements.center_of_mass
function expects a regular NumPy array; that is, an instance of ndarray
. In theory, since a memory-mapped array has type memmap
, and memmap
is a subclass of ndarray
, your original code should work; in practice it fails (as you've discovered), and the workaround is to explicitly convert the memory-mapped array to a normal NumPy array. The fact that your code doesn't work represents a violation of the Liskov substitution principle, and indicates a bug in either NumPy or SciPy (most likely the former).
Looking at the ndimage
source, I tracked the difference in behaviour down to the fact that for a memory-mapped array x
, the result of x.sum()
is another (zero-dimensional) array, while for a regular NumPy ndarray
x
, the result of x.sum()
is a scalar (an instance of numpy.float64
, for example). This NumPy bug report looks relevant.