<s> from nose . tools import raises <EOL> import numpy as np <EOL> from mahotas . features import _lbp <EOL> import mahotas . thresholding <EOL> from mahotas . features import lbp <EOL> from mahotas . features . lbp import lbp_transform , lbp_names <EOL> from mahotas . tests . utils import luispedro_jpg <EOL> def test_shape ( ) : <EOL> A = np . arange ( <NUM_LIT:32> * <NUM_LIT:32> ) . reshape ( ( <NUM_LIT:32> , <NUM_LIT:32> ) ) <EOL> B = np . arange ( <NUM_LIT:64> * <NUM_LIT:64> ) . reshape ( ( <NUM_LIT:64> , <NUM_LIT:64> ) ) <EOL> features0 = lbp ( A , <NUM_LIT:3> , <NUM_LIT:12> ) <EOL> features1 = lbp ( B , <NUM_LIT:3> , <NUM_LIT:12> ) <EOL> assert features0 . shape == features1 . shape <EOL> def test_nonzero ( ) : <EOL> A = np . arange ( <NUM_LIT:32> * <NUM_LIT:32> ) . reshape ( ( <NUM_LIT:32> , <NUM_LIT:32> ) ) <EOL> features = lbp ( A , <NUM_LIT:3> , <NUM_LIT:12> ) <EOL> features_ignore_zeros = lbp ( A * ( A > <NUM_LIT> ) , <NUM_LIT:3> , <NUM_LIT:12> , ignore_zeros = True ) <EOL> assert features . sum ( ) > <NUM_LIT:0> <EOL> assert not np . all ( features == features_ignore_zeros ) <EOL> def test_histogram ( ) : <EOL> A = np . arange ( <NUM_LIT:32> * <NUM_LIT:32> ) . reshape ( ( <NUM_LIT:32> , <NUM_LIT:32> ) ) <EOL> for r in ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) : <EOL> assert lbp ( A , r , <NUM_LIT:8> ) . sum ( ) == A . size <EOL> def test_histogram_large ( ) : <EOL> A = np . arange ( <NUM_LIT:32> * <NUM_LIT:32> ) . reshape ( ( <NUM_LIT:32> , <NUM_LIT:32> ) ) <EOL> for r in ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) : <EOL> assert lbp ( A , r , <NUM_LIT:12> ) . sum ( ) == A . size <EOL> def test_map ( ) : <EOL> assert len ( set ( _lbp . map ( np . arange ( <NUM_LIT> , dtype = np . uint32 ) , <NUM_LIT:8> ) ) ) == <NUM_LIT> <EOL> def test_positives ( ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> f = np . random . random_sample ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> lbps = lbp ( f , <NUM_LIT:4> , <NUM_LIT:8> ) <EOL> assert len ( np . where ( lbps == <NUM_LIT:0> ) [ <NUM_LIT:0> ] ) < <NUM_LIT:2> <EOL> assert lbps . sum ( ) == f . size <EOL> def test_lbp_transform ( ) : <EOL> im = luispedro_jpg ( ) . max ( <NUM_LIT:2> ) <EOL> transformed = lbp_transform ( im , <NUM_LIT:8> , <NUM_LIT:4> , preserve_shape = True ) <EOL> assert transformed . shape == im . shape <EOL> assert transformed . min ( ) >= <NUM_LIT:0> <EOL> assert transformed . max ( ) < <NUM_LIT:2> ** <NUM_LIT:4> <EOL> transformed = lbp_transform ( im , <NUM_LIT:8> , <NUM_LIT:4> , preserve_shape = False ) <EOL> assert len ( transformed . shape ) == <NUM_LIT:1> <EOL> assert transformed . size == im . size <EOL> np . random . seed ( <NUM_LIT> ) <EOL> im *= np . random . random ( im . shape ) > <NUM_LIT> <EOL> transformed = lbp_transform ( im , <NUM_LIT:8> , <NUM_LIT:4> , preserve_shape = False , ignore_zeros = True ) <EOL> assert len ( transformed . shape ) == <NUM_LIT:1> <EOL> assert transformed . size == ( im . size - ( im == <NUM_LIT:0> ) . sum ( ) ) <EOL> def test_count_binary1s ( ) : <EOL> from mahotas . features . lbp import count_binary1s <EOL> assert np . all ( count_binary1s ( np . zeros ( ( <NUM_LIT> , <NUM_LIT> ) , np . uint8 ) ) == np . zeros ( ( <NUM_LIT> , <NUM_LIT> ) ) ) <EOL> np . random . seed ( <NUM_LIT> ) <EOL> arr = np . random . randint ( <NUM_LIT> , size = ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> c = count_binary1s ( arr ) <EOL> assert np . all ( ( c == <NUM_LIT:0> ) == ( arr == <NUM_LIT:0> ) ) <EOL> assert c . shape == arr . shape <EOL> assert np . all ( c [ arr == <NUM_LIT:5> ] == <NUM_LIT:2> ) <EOL> assert np . all ( c [ arr == <NUM_LIT:7> ] == <NUM_LIT:3> ) <EOL> assert np . all ( c [ arr == <NUM_LIT:8> ] == <NUM_LIT:1> ) <EOL> assert np . all ( c [ arr == <NUM_LIT:32> ] == <NUM_LIT:1> ) <EOL> assert np . all ( count_binary1s ( [ <NUM_LIT> ] ) == [ <NUM_LIT:1> ] ) <EOL> @ raises ( ValueError ) <EOL> def test_lbp_3d ( ) : <EOL> im = np . arange ( <NUM_LIT:10> * <NUM_LIT:20> * <NUM_LIT:3> ) . reshape ( ( <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:3> ) ) <EOL> lbp_transform ( im , <NUM_LIT:1> , <NUM_LIT:8> ) <EOL> def test_lbp_names ( ) : <EOL> f = np . random . random ( size = ( <NUM_LIT:64> , <NUM_LIT> ) ) <EOL> f *= <NUM_LIT:255> <EOL> f = f . astype ( np . uint8 ) <EOL> for radius , points in [ ( <NUM_LIT:8> , <NUM_LIT:6> ) , <EOL> ( <NUM_LIT:8> , <NUM_LIT:8> ) , ( <NUM_LIT:6> , <NUM_LIT:6> ) , ( <NUM_LIT:8> , <NUM_LIT:4> ) , ( <NUM_LIT:12> , <NUM_LIT:6> ) ] : <EOL> assert len ( lbp ( f , radius , points ) ) == len ( lbp_names ( radius , points ) ) </s>
