<s> import numpy as np <EOL> from scipy import ndimage <EOL> import mahotas . center_of_mass <EOL> np . random . seed ( <NUM_LIT> ) <EOL> def _mean_out ( img , axis ) : <EOL> if len ( img . shape ) == <NUM_LIT:2> : return img . mean ( <NUM_LIT:1> - axis ) <EOL> if axis == <NUM_LIT:0> : <EOL> return _mean_out ( img . mean ( <NUM_LIT:1> ) , <NUM_LIT:0> ) <EOL> return _mean_out ( img . mean ( <NUM_LIT:0> ) , axis - <NUM_LIT:1> ) <EOL> def slow_center_of_mass ( img ) : <EOL> '''<STR_LIT>''' <EOL> xs = [ ] <EOL> for axis , si in enumerate ( img . shape ) : <EOL> xs . append ( np . mean ( _mean_out ( img , axis ) * np . arange ( si ) ) ) <EOL> xs = np . array ( xs ) <EOL> xs /= img . mean ( ) <EOL> return xs <EOL> def test_cmp_ndimage ( ) : <EOL> R = ( <NUM_LIT:255> * np . random . rand ( <NUM_LIT> , <NUM_LIT> ) ) . astype ( np . uint16 ) <EOL> R += np . arange ( <NUM_LIT> , dtype = np . uint16 ) <EOL> m0 , m1 = mahotas . center_of_mass ( R ) <EOL> n0 , n1 = ndimage . center_of_mass ( R ) <EOL> assert np . abs ( n0 - m0 ) < <NUM_LIT:1.> <EOL> assert np . abs ( n1 - m1 ) < <NUM_LIT:1.> <EOL> def test_cmp_ndimage3 ( ) : <EOL> R = ( <NUM_LIT:255> * np . random . rand ( <NUM_LIT:32> , <NUM_LIT> , <NUM_LIT:8> , <NUM_LIT:16> ) ) . astype ( np . uint16 ) <EOL> R += np . arange ( <NUM_LIT:16> , dtype = np . uint16 ) <EOL> m = mahotas . center_of_mass ( R ) <EOL> n = ndimage . center_of_mass ( R ) <EOL> p = slow_center_of_mass ( R ) <EOL> assert np . abs ( n - m ) . max ( ) < <NUM_LIT:1.> <EOL> assert np . abs ( p - m ) . max ( ) < <NUM_LIT:1.> <EOL> def test_simple ( ) : <EOL> R = ( <NUM_LIT:255> * np . random . rand ( <NUM_LIT> , <NUM_LIT> ) ) . astype ( np . uint16 ) <EOL> R += np . arange ( <NUM_LIT> , dtype = np . uint16 ) <EOL> m0 , m1 = mahotas . center_of_mass ( R ) <EOL> assert <NUM_LIT:0> < m0 < <NUM_LIT> <EOL> assert <NUM_LIT:0> < m1 < <NUM_LIT> <EOL> def test_labels ( ) : <EOL> R = ( <NUM_LIT:255> * np . random . rand ( <NUM_LIT> , <NUM_LIT> ) ) . astype ( np . uint16 ) <EOL> labels = np . zeros ( R . shape , np . intc ) <EOL> labels [ <NUM_LIT:100> : , : ] += <NUM_LIT:1> <EOL> labels [ <NUM_LIT:100> : , <NUM_LIT:100> : ] += <NUM_LIT:1> <EOL> centres = mahotas . center_of_mass ( R , labels ) <EOL> for label , cm in enumerate ( centres ) : <EOL> assert np . all ( cm == mahotas . center_of_mass ( R * ( labels == label ) ) ) <EOL> def test_labels_not_intc ( ) : <EOL> img = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:16> , <NUM_LIT:16> ) ) <EOL> labels = img . copy ( ) <EOL> labels %= <NUM_LIT:3> <EOL> cm = mahotas . center_of_mass ( img , labels ) <EOL> assert cm . shape == ( <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> labels = labels . T . copy ( ) <EOL> cm = mahotas . center_of_mass ( img , labels . T ) <EOL> assert cm . shape == ( <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> labels = labels . T . copy ( ) <EOL> labels = labels . astype ( np . uint16 ) <EOL> cm = mahotas . center_of_mass ( img , labels ) <EOL> assert cm . shape == ( <NUM_LIT:3> , <NUM_LIT:2> ) </s>
