[SciPy-user] easy to fix bug in ndimage.center_of_mass

Stéfan van der Walt stefan@sun.ac...
Thu Jun 5 07:11:40 CDT 2008


2008/6/5 Sebastian Haase <haase@msg.ucsf.edu>:
> Hi,
> I found this code in measurements.py
> def center_of_mass(input, labels = None, index = None):
>    """Calculate the center of mass of of the array.
>
>    The index parameter is a single label number or a sequence of
>    label numbers of the objects to be measured. If index is None, all
>    values are used where labels is larger than zero.
>    """
>    input = numpy.asarray(input)
>    if numpy.iscomplexobj(input):
>        raise TypeError, 'Complex type not supported'
>    if labels != None:
>        labels = numpy.asarray(labels)
>        labels = _broadcast(labels, input.shape)
>
>        if labels.shape != input.shape:
>            raise RuntimeError, 'input and labels shape are not equal'
>    return _nd_image.center_of_mass(input, labels, index)
>
>
> but the if part:
> if labels.shape != input.shape:
>
> is probably indented one level too much !?

That looks right.  You don't want to access labels.shape if labels is
None.  The "!=" is wrong, though.  It should be "is not"

Regards
Stéfan


More information about the SciPy-user mailing list