def center_of_mass(input, labels=None, index=None):
 normalizer = sum(input, labels, index)
 grids = numpy.ogrid[[slice(0, i) for i in input.shape]]
 results = [(sum((input * grids[dir].astype(float)), labels, index) / normalizer) for dir in range(input.ndim)]
 if numpy.isscalar(results[0]):
  return tuple(results)
 return [tuple(v) for v in numpy.array(results).T]

def center_of_mass ( input , labels = None , index = None ) : normalizer = sum ( input , labels , index ) grids = numpy . ogrid [ [ slice ( 0 , i ) for i in input . shape ] ] results = [ ( sum ( ( input * grids [ dir ] . astype ( float ) ) , labels , index ) / normalizer ) for dir in range ( input . ndim ) ] if numpy . isscalar ( results [ 0 ] ) : return tuple ( results ) return [ tuple ( v ) for v in numpy . array ( results ) . T ]
calculate the center of mass of the values of an array at labels .