<s> import mahotas as mh <EOL> import numpy as np <EOL> from mahotas . morph import get_structuring_elem , subm , tophat_open , tophat_close <EOL> from nose . tools import raises <EOL> def test_get_structuring_elem ( ) : <EOL> A = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , np . bool ) <EOL> Bc = np . ones ( ( <NUM_LIT:4> , <NUM_LIT:4> ) , dtype = np . bool ) <EOL> Bc [ <NUM_LIT:0> , <NUM_LIT:2> ] = <NUM_LIT:0> <EOL> assert np . all ( get_structuring_elem ( A , None ) == [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> assert np . all ( get_structuring_elem ( A , <NUM_LIT:4> ) == [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> assert np . all ( get_structuring_elem ( A , <NUM_LIT:4> ) == get_structuring_elem ( A , <NUM_LIT:1> ) ) <EOL> assert np . all ( get_structuring_elem ( A , <NUM_LIT:8> ) == get_structuring_elem ( A , <NUM_LIT:2> ) ) <EOL> assert np . all ( get_structuring_elem ( A , <NUM_LIT:8> ) == np . ones ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , dtype = np . bool ) ) <EOL> assert np . all ( get_structuring_elem ( A , Bc ) == Bc ) <EOL> assert np . all ( get_structuring_elem ( A , Bc . T ) == Bc . T ) <EOL> assert get_structuring_elem ( A , Bc . T ) . flags [ '<STR_LIT>' ] <EOL> assert np . all ( get_structuring_elem ( A , Bc . astype ( np . float ) . T ) . flags [ '<STR_LIT>' ] ) <EOL> assert np . all ( get_structuring_elem ( A , Bc . astype ( np . float ) . T ) == Bc . T ) <EOL> @ raises ( ValueError ) <EOL> def bad_dims ( ) : <EOL> Bc = np . ones ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) , dtype = np . bool ) <EOL> get_structuring_elem ( A , Bc ) <EOL> bad_dims ( ) <EOL> def test_open ( ) : <EOL> from mahotas . morph import open <EOL> np . random . seed ( <NUM_LIT> ) <EOL> A = np . random . random_sample ( ( <NUM_LIT:16> , <NUM_LIT:16> ) ) > <NUM_LIT> <EOL> assert open ( A ) . shape == ( <NUM_LIT:16> , <NUM_LIT:16> ) <EOL> def test_close ( ) : <EOL> from mahotas . morph import close <EOL> np . random . seed ( <NUM_LIT> ) <EOL> A = np . random . random_sample ( ( <NUM_LIT:16> , <NUM_LIT:16> ) ) > <NUM_LIT> <EOL> assert close ( A ) . shape == ( <NUM_LIT:16> , <NUM_LIT:16> ) <EOL> def slow_reg ( A , agg ) : <EOL> def get ( i , j ) : <EOL> vals = [ ] <EOL> def try_this ( i , j ) : <EOL> if <NUM_LIT:0> <= i < A . shape [ <NUM_LIT:0> ] and <NUM_LIT:0> <= j < A . shape [ <NUM_LIT:1> ] : <EOL> vals . append ( A [ i , j ] ) <EOL> try_this ( i , j ) <EOL> try_this ( i - <NUM_LIT:1> , j ) <EOL> try_this ( i + <NUM_LIT:1> , j ) <EOL> try_this ( i , j - <NUM_LIT:1> ) <EOL> try_this ( i , j + <NUM_LIT:1> ) <EOL> return vals <EOL> res = np . zeros ( A . shape , bool ) <EOL> for i in range ( A . shape [ <NUM_LIT:0> ] ) : <EOL> for j in range ( A . shape [ <NUM_LIT:0> ] ) : <EOL> res [ i , j ] = ( A [ i , j ] == agg ( get ( i , j ) ) ) <EOL> return res <EOL> def test_locmin_max ( ) : <EOL> from mahotas . morph import locmax , locmin <EOL> np . random . seed ( <NUM_LIT> ) <EOL> for i in range ( <NUM_LIT:8> ) : <EOL> A = np . random . random_sample ( ( <NUM_LIT:64> , <NUM_LIT:64> ) ) <EOL> A *= <NUM_LIT:255> <EOL> if ( i % <NUM_LIT:2> ) == <NUM_LIT:0> : <EOL> A = A . astype ( np . uint8 ) <EOL> fast = locmax ( A ) <EOL> assert np . all ( fast == slow_reg ( A , max ) ) <EOL> fast = locmin ( A ) <EOL> assert np . all ( fast == slow_reg ( A , min ) ) <EOL> def test_regmax_min ( ) : <EOL> from mahotas . morph import locmax , locmin , regmax , regmin <EOL> np . random . seed ( <NUM_LIT> ) <EOL> for i in range ( <NUM_LIT:8> ) : <EOL> A = np . random . random_sample ( ( <NUM_LIT:64> , <NUM_LIT:64> ) ) <EOL> A *= <NUM_LIT:255> <EOL> if ( i % <NUM_LIT:2> ) == <NUM_LIT:1> : <EOL> A = A . astype ( np . uint8 ) <EOL> loc = locmax ( A ) <EOL> reg = regmax ( A ) <EOL> assert not np . any ( reg & ~ loc ) <EOL> loc = locmin ( A ) <EOL> reg = regmin ( A ) <EOL> assert not np . any ( reg & ~ loc ) <EOL> def test_dilate_crash ( ) : <EOL> from mahotas . morph import dilate <EOL> large = np . random . random_sample ( ( <NUM_LIT> , <NUM_LIT> ) ) > <NUM_LIT> <EOL> small = large [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] <EOL> dilate ( small ) <EOL> def slow_subm_uint8 ( a , b ) : <EOL> a = a . astype ( np . int64 ) <EOL> b = b . astype ( np . int64 ) <EOL> c = a - b <EOL> return np . clip ( c , <NUM_LIT:0> , <NUM_LIT:255> ) . astype ( np . uint8 ) <EOL> def slow_subm_uint16 ( a , b ) : <EOL> a = a . astype ( np . int64 ) <EOL> b = b . astype ( np . int64 ) <EOL> c = a - b <EOL> return np . clip ( c , <NUM_LIT:0> , <NUM_LIT:2> ** <NUM_LIT:16> - <NUM_LIT:1> ) . astype ( np . uint16 ) <EOL> def slow_subm_int16 ( a , b ) : <EOL> a = a . astype ( np . int64 ) <EOL> b = b . astype ( np . int64 ) <EOL> c = a - b <EOL> return np . clip ( c , - <NUM_LIT:2> ** <NUM_LIT:15> , <NUM_LIT:2> ** <NUM_LIT:15> - <NUM_LIT:1> ) . astype ( np . int16 ) <EOL> def test_subm ( ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> for j in range ( <NUM_LIT:8> ) : <EOL> s = ( <NUM_LIT> , <NUM_LIT> ) <EOL> a = np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = s ) <EOL> b = np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = s ) <EOL> a = a . astype ( np . uint8 ) <EOL> b = b . astype ( np . uint8 ) <EOL> assert np . all ( slow_subm_uint8 ( a , b ) == subm ( a , b ) ) <EOL> a = <NUM_LIT> * np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = s ) <EOL> b = <NUM_LIT> * np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = s ) <EOL> a = a . astype ( np . uint16 ) <EOL> b = b . astype ( np . uint16 ) <EOL> assert np . all ( slow_subm_uint16 ( a , b ) == subm ( a , b ) ) <EOL> a2 = <NUM_LIT> * np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = s ) <EOL> b2 = <NUM_LIT> * np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = s ) <EOL> a = a . astype ( np . int16 ) <EOL> b = b . astype ( np . int16 ) <EOL> a -= a2 <EOL> b -= b2 <EOL> assert np . all ( slow_subm_int16 ( a , b ) == subm ( a , b ) ) <EOL> def test_subm_out ( ) : <EOL> np . random . seed ( <NUM_LIT:32> ) <EOL> for j in range ( <NUM_LIT:8> ) : <EOL> s = ( <NUM_LIT> , <NUM_LIT> ) <EOL> a = np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = s ) <EOL> b = np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = s ) <EOL> c = subm ( a , b ) <EOL> assert c is not a <EOL> assert c is not b <EOL> assert not np . all ( c == a ) <EOL> c = subm ( a , b , out = a ) <EOL> assert c is a <EOL> assert c is not b <EOL> assert np . all ( c == a ) <EOL> def test_tophat ( ) : <EOL> np . random . seed ( <NUM_LIT:32> ) <EOL> for j in range ( <NUM_LIT:8> ) : <EOL> s = ( <NUM_LIT> , <NUM_LIT> ) <EOL> f = np . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , size = s ) <EOL> f = f . astype ( np . uint8 ) <EOL> g = tophat_close ( f ) <EOL> assert f . shape == g . shape <EOL> g = tophat_open ( f ) <EOL> assert f . shape == g . shape <EOL> def test_circle_se ( ) : <EOL> from mahotas . morph import circle_se <EOL> for r in ( <NUM_LIT:4> , <NUM_LIT:5> ) : <EOL> c = circle_se ( r ) <EOL> assert len ( c ) == ( <NUM_LIT:2> * r + <NUM_LIT:1> ) <EOL> assert len ( c ) == len ( c . T ) <EOL> assert not c . all ( ) <EOL> assert c . any ( ) <EOL> @ raises ( ValueError ) <EOL> def circle_1 ( ) : <EOL> circle_se ( - <NUM_LIT:1> ) <EOL> circle_1 ( ) <EOL> def test_distance_multi ( ) : <EOL> import mahotas . _morph <EOL> np . random . seed ( <NUM_LIT:20> ) <EOL> binim = np . random . random ( ( <NUM_LIT:12> , <NUM_LIT> ) ) > <NUM_LIT> <EOL> f = ( binim * <NUM_LIT:0> + <NUM_LIT> * <NUM_LIT> ) . astype ( float ) <EOL> Bc = np . ones ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , bool ) <EOL> mahotas . _morph . distance_multi ( f , binim , Bc ) <EOL> f2 = mahotas . distance ( binim ) <EOL> assert np . all ( f == f2 ) <EOL> def test_disk ( ) : <EOL> from mahotas . morph import disk <EOL> D2 = disk ( <NUM_LIT:2> ) <EOL> assert D2 . shape [ <NUM_LIT:0> ] == D2 . shape [ <NUM_LIT:1> ] <EOL> assert D2 . shape == ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> assert not D2 [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> assert len ( D2 . shape ) == <NUM_LIT:2> <EOL> D3 = disk ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> assert np . all ( D3 [ <NUM_LIT:2> ] == D2 ) <EOL> D3 = disk ( <NUM_LIT:4> , <NUM_LIT:3> ) <EOL> assert len ( D3 . shape ) == <NUM_LIT:3> <EOL> assert D3 . shape [ <NUM_LIT:0> ] == D3 . shape [ <NUM_LIT:1> ] <EOL> assert D3 . shape [ <NUM_LIT:0> ] == D3 . shape [ <NUM_LIT:2> ] <EOL> D = disk ( <NUM_LIT:32> , <NUM_LIT:2> ) <EOL> assert D [ <NUM_LIT:32> , <NUM_LIT:2> ] <EOL> @ raises ( ValueError ) <EOL> def test_negative_dim ( dim ) : <EOL> disk ( <NUM_LIT:3> , dim ) <EOL> test_negative_dim ( - <NUM_LIT:2> ) <EOL> test_negative_dim ( - <NUM_LIT:1> ) <EOL> test_negative_dim ( <NUM_LIT:0> ) <EOL> @ raises ( ValueError ) <EOL> def test_close_holes_3d ( ) : <EOL> '<STR_LIT>' <EOL> f = np . random . rand ( <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:3> ) > <NUM_LIT> <EOL> mh . close_holes ( f ) </s>
