<s> import numpy as np <EOL> from skimage . segmentation import random_walker <EOL> from skimage . transform import resize <EOL> from skimage . _shared . _warnings import expected_warnings <EOL> PYAMG_EXPECTED_WARNING = '<STR_LIT>' <EOL> def make_2d_syntheticdata ( lx , ly = None ) : <EOL> if ly is None : <EOL> ly = lx <EOL> np . random . seed ( <NUM_LIT> ) <EOL> data = np . zeros ( ( lx , ly ) ) + <NUM_LIT:0.1> * np . random . randn ( lx , ly ) <EOL> small_l = int ( lx // <NUM_LIT:5> ) <EOL> data [ lx // <NUM_LIT:2> - small_l : lx // <NUM_LIT:2> + small_l , <EOL> ly // <NUM_LIT:2> - small_l : ly // <NUM_LIT:2> + small_l ] = <NUM_LIT:1> <EOL> data [ lx // <NUM_LIT:2> - small_l + <NUM_LIT:1> : lx // <NUM_LIT:2> + small_l - <NUM_LIT:1> , <EOL> ly // <NUM_LIT:2> - small_l + <NUM_LIT:1> : ly // <NUM_LIT:2> + small_l - <NUM_LIT:1> ] = ( <EOL> <NUM_LIT:0.1> * np . random . randn ( <NUM_LIT:2> * small_l - <NUM_LIT:2> , <NUM_LIT:2> * small_l - <NUM_LIT:2> ) ) <EOL> data [ lx // <NUM_LIT:2> - small_l , ly // <NUM_LIT:2> - small_l // <NUM_LIT:8> : ly // <NUM_LIT:2> + small_l // <NUM_LIT:8> ] = <NUM_LIT:0> <EOL> seeds = np . zeros_like ( data ) <EOL> seeds [ lx // <NUM_LIT:5> , ly // <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> seeds [ lx // <NUM_LIT:2> + small_l // <NUM_LIT:4> , ly // <NUM_LIT:2> - small_l // <NUM_LIT:4> ] = <NUM_LIT:2> <EOL> return data , seeds <EOL> def make_3d_syntheticdata ( lx , ly = None , lz = None ) : <EOL> if ly is None : <EOL> ly = lx <EOL> if lz is None : <EOL> lz = lx <EOL> np . random . seed ( <NUM_LIT> ) <EOL> data = np . zeros ( ( lx , ly , lz ) ) + <NUM_LIT:0.1> * np . random . randn ( lx , ly , lz ) <EOL> small_l = int ( lx // <NUM_LIT:5> ) <EOL> data [ lx // <NUM_LIT:2> - small_l : lx // <NUM_LIT:2> + small_l , <EOL> ly // <NUM_LIT:2> - small_l : ly // <NUM_LIT:2> + small_l , <EOL> lz // <NUM_LIT:2> - small_l : lz // <NUM_LIT:2> + small_l ] = <NUM_LIT:1> <EOL> data [ lx // <NUM_LIT:2> - small_l + <NUM_LIT:1> : lx // <NUM_LIT:2> + small_l - <NUM_LIT:1> , <EOL> ly // <NUM_LIT:2> - small_l + <NUM_LIT:1> : ly // <NUM_LIT:2> + small_l - <NUM_LIT:1> , <EOL> lz // <NUM_LIT:2> - small_l + <NUM_LIT:1> : lz // <NUM_LIT:2> + small_l - <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> hole_size = np . max ( [ <NUM_LIT:1> , small_l // <NUM_LIT:8> ] ) <EOL> data [ lx // <NUM_LIT:2> - small_l , <EOL> ly // <NUM_LIT:2> - hole_size : ly // <NUM_LIT:2> + hole_size , <EOL> lz // <NUM_LIT:2> - hole_size : lz // <NUM_LIT:2> + hole_size ] = <NUM_LIT:0> <EOL> seeds = np . zeros_like ( data ) <EOL> seeds [ lx // <NUM_LIT:5> , ly // <NUM_LIT:5> , lz // <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> seeds [ lx // <NUM_LIT:2> + small_l // <NUM_LIT:4> , <EOL> ly // <NUM_LIT:2> - small_l // <NUM_LIT:4> , <EOL> lz // <NUM_LIT:2> - small_l // <NUM_LIT:4> ] = <NUM_LIT:2> <EOL> return data , seeds <EOL> def test_2d_bf ( ) : <EOL> lx = <NUM_LIT> <EOL> ly = <NUM_LIT:100> <EOL> data , labels = make_2d_syntheticdata ( lx , ly ) <EOL> labels_bf = random_walker ( data , labels , beta = <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> assert ( labels_bf [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> full_prob_bf = random_walker ( data , labels , beta = <NUM_LIT> , mode = '<STR_LIT>' , <EOL> return_full_prob = True ) <EOL> assert ( full_prob_bf [ <NUM_LIT:1> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] >= <EOL> full_prob_bf [ <NUM_LIT:0> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> labels [ <NUM_LIT> , <NUM_LIT> ] = <NUM_LIT:3> <EOL> full_prob_bf = random_walker ( data , labels , beta = <NUM_LIT> , mode = '<STR_LIT>' , <EOL> return_full_prob = True ) <EOL> assert ( full_prob_bf [ <NUM_LIT:1> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] >= <EOL> full_prob_bf [ <NUM_LIT:0> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] ) . all ( ) <EOL> assert len ( full_prob_bf ) == <NUM_LIT:3> <EOL> assert data . shape == labels . shape <EOL> def test_2d_cg ( ) : <EOL> lx = <NUM_LIT> <EOL> ly = <NUM_LIT:100> <EOL> data , labels = make_2d_syntheticdata ( lx , ly ) <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> labels_cg = random_walker ( data , labels , beta = <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> assert ( labels_cg [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> full_prob = random_walker ( data , labels , beta = <NUM_LIT> , mode = '<STR_LIT>' , <EOL> return_full_prob = True ) <EOL> assert ( full_prob [ <NUM_LIT:1> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] >= <EOL> full_prob [ <NUM_LIT:0> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> return data , labels_cg <EOL> def test_2d_cg_mg ( ) : <EOL> lx = <NUM_LIT> <EOL> ly = <NUM_LIT:100> <EOL> data , labels = make_2d_syntheticdata ( lx , ly ) <EOL> expected = '<STR_LIT>' % PYAMG_EXPECTED_WARNING <EOL> with expected_warnings ( [ expected ] ) : <EOL> labels_cg_mg = random_walker ( data , labels , beta = <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> assert ( labels_cg_mg [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> with expected_warnings ( [ expected ] ) : <EOL> full_prob = random_walker ( data , labels , beta = <NUM_LIT> , mode = '<STR_LIT>' , <EOL> return_full_prob = True ) <EOL> assert ( full_prob [ <NUM_LIT:1> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] >= <EOL> full_prob [ <NUM_LIT:0> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> return data , labels_cg_mg <EOL> def test_types ( ) : <EOL> lx = <NUM_LIT> <EOL> ly = <NUM_LIT:100> <EOL> data , labels = make_2d_syntheticdata ( lx , ly ) <EOL> data = <NUM_LIT:255> * ( data - data . min ( ) ) // ( data . max ( ) - data . min ( ) ) <EOL> data = data . astype ( np . uint8 ) <EOL> with expected_warnings ( [ PYAMG_EXPECTED_WARNING ] ) : <EOL> labels_cg_mg = random_walker ( data , labels , beta = <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> assert ( labels_cg_mg [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> return data , labels_cg_mg <EOL> def test_reorder_labels ( ) : <EOL> lx = <NUM_LIT> <EOL> ly = <NUM_LIT:100> <EOL> data , labels = make_2d_syntheticdata ( lx , ly ) <EOL> labels [ labels == <NUM_LIT:2> ] = <NUM_LIT:4> <EOL> labels_bf = random_walker ( data , labels , beta = <NUM_LIT> , mode = '<STR_LIT>' ) <EOL> assert ( labels_bf [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> return data , labels_bf <EOL> def test_2d_inactive ( ) : <EOL> lx = <NUM_LIT> <EOL> ly = <NUM_LIT:100> <EOL> data , labels = make_2d_syntheticdata ( lx , ly ) <EOL> labels [ <NUM_LIT:10> : <NUM_LIT:20> , <NUM_LIT:10> : <NUM_LIT:20> ] = - <NUM_LIT:1> <EOL> labels [ <NUM_LIT> : <NUM_LIT:50> , <NUM_LIT> : <NUM_LIT> ] = - <NUM_LIT:2> <EOL> labels = random_walker ( data , labels , beta = <NUM_LIT> ) <EOL> assert ( labels . reshape ( ( lx , ly ) ) [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> return data , labels <EOL> def test_3d ( ) : <EOL> n = <NUM_LIT:30> <EOL> lx , ly , lz = n , n , n <EOL> data , labels = make_3d_syntheticdata ( lx , ly , lz ) <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> labels = random_walker ( data , labels , mode = '<STR_LIT>' ) <EOL> assert ( labels . reshape ( data . shape ) [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> return data , labels <EOL> def test_3d_inactive ( ) : <EOL> n = <NUM_LIT:30> <EOL> lx , ly , lz = n , n , n <EOL> data , labels = make_3d_syntheticdata ( lx , ly , lz ) <EOL> old_labels = np . copy ( labels ) <EOL> labels [ <NUM_LIT:5> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] = - <NUM_LIT:1> <EOL> after_labels = np . copy ( labels ) <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> labels = random_walker ( data , labels , mode = '<STR_LIT>' ) <EOL> assert ( labels . reshape ( data . shape ) [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data . shape == labels . shape <EOL> return data , labels , old_labels , after_labels <EOL> def test_multispectral_2d ( ) : <EOL> lx , ly = <NUM_LIT> , <NUM_LIT:100> <EOL> data , labels = make_2d_syntheticdata ( lx , ly ) <EOL> data = data [ ... , np . newaxis ] . repeat ( <NUM_LIT:2> , axis = - <NUM_LIT:1> ) <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> multi_labels = random_walker ( data , labels , mode = '<STR_LIT>' , <EOL> multichannel = True ) <EOL> assert data [ ... , <NUM_LIT:0> ] . shape == labels . shape <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> single_labels = random_walker ( data [ ... , <NUM_LIT:0> ] , labels , mode = '<STR_LIT>' ) <EOL> assert ( multi_labels . reshape ( labels . shape ) [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data [ ... , <NUM_LIT:0> ] . shape == labels . shape <EOL> return data , multi_labels , single_labels , labels <EOL> def test_multispectral_3d ( ) : <EOL> n = <NUM_LIT:30> <EOL> lx , ly , lz = n , n , n <EOL> data , labels = make_3d_syntheticdata ( lx , ly , lz ) <EOL> data = data [ ... , np . newaxis ] . repeat ( <NUM_LIT:2> , axis = - <NUM_LIT:1> ) <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> multi_labels = random_walker ( data , labels , mode = '<STR_LIT>' , <EOL> multichannel = True ) <EOL> assert data [ ... , <NUM_LIT:0> ] . shape == labels . shape <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> single_labels = random_walker ( data [ ... , <NUM_LIT:0> ] , labels , mode = '<STR_LIT>' ) <EOL> assert ( multi_labels . reshape ( labels . shape ) [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert ( single_labels . reshape ( labels . shape ) [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> assert data [ ... , <NUM_LIT:0> ] . shape == labels . shape <EOL> return data , multi_labels , single_labels , labels <EOL> def test_spacing_0 ( ) : <EOL> n = <NUM_LIT:30> <EOL> lx , ly , lz = n , n , n <EOL> data , _ = make_3d_syntheticdata ( lx , ly , lz ) <EOL> data_aniso = np . zeros ( ( n , n , n // <NUM_LIT:2> ) ) <EOL> for i , yz in enumerate ( data ) : <EOL> data_aniso [ i , : , : ] = resize ( yz , ( n , n // <NUM_LIT:2> ) ) <EOL> small_l = int ( lx // <NUM_LIT:5> ) <EOL> labels_aniso = np . zeros_like ( data_aniso ) <EOL> labels_aniso [ lx // <NUM_LIT:5> , ly // <NUM_LIT:5> , lz // <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> labels_aniso [ lx // <NUM_LIT:2> + small_l // <NUM_LIT:4> , <EOL> ly // <NUM_LIT:2> - small_l // <NUM_LIT:4> , <EOL> lz // <NUM_LIT:4> - small_l // <NUM_LIT:8> ] = <NUM_LIT:2> <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> labels_aniso = random_walker ( data_aniso , labels_aniso , mode = '<STR_LIT>' , <EOL> spacing = ( <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:0.5> ) ) <EOL> assert ( labels_aniso [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT:7> : <NUM_LIT:9> ] == <NUM_LIT:2> ) . all ( ) <EOL> def test_spacing_1 ( ) : <EOL> n = <NUM_LIT:30> <EOL> lx , ly , lz = n , n , n <EOL> data , _ = make_3d_syntheticdata ( lx , ly , lz ) <EOL> data_aniso = np . zeros ( ( n , n * <NUM_LIT:2> , n ) ) <EOL> for i , yz in enumerate ( data ) : <EOL> data_aniso [ i , : , : ] = resize ( yz , ( n * <NUM_LIT:2> , n ) ) <EOL> small_l = int ( lx // <NUM_LIT:5> ) <EOL> labels_aniso = np . zeros_like ( data_aniso ) <EOL> labels_aniso [ lx // <NUM_LIT:5> , ly // <NUM_LIT:5> , lz // <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> labels_aniso [ lx // <NUM_LIT:2> + small_l // <NUM_LIT:4> , <EOL> ly - small_l // <NUM_LIT:2> , <EOL> lz // <NUM_LIT:2> - small_l // <NUM_LIT:4> ] = <NUM_LIT:2> <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> labels_aniso = random_walker ( data_aniso , labels_aniso , mode = '<STR_LIT>' , <EOL> spacing = ( <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT:1.> ) ) <EOL> assert ( labels_aniso [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> data_aniso = np . zeros ( ( n , n * <NUM_LIT:2> , n ) ) <EOL> for i in range ( data . shape [ <NUM_LIT:1> ] ) : <EOL> data_aniso [ i , : , : ] = resize ( data [ : , <NUM_LIT:1> , : ] , ( n * <NUM_LIT:2> , n ) ) <EOL> small_l = int ( lx // <NUM_LIT:5> ) <EOL> labels_aniso2 = np . zeros_like ( data_aniso ) <EOL> labels_aniso2 [ lx // <NUM_LIT:5> , ly // <NUM_LIT:5> , lz // <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> labels_aniso2 [ lx - small_l // <NUM_LIT:2> , <EOL> ly // <NUM_LIT:2> + small_l // <NUM_LIT:4> , <EOL> lz // <NUM_LIT:2> - small_l // <NUM_LIT:4> ] = <NUM_LIT:2> <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> labels_aniso2 = random_walker ( data_aniso , <EOL> labels_aniso2 , <EOL> mode = '<STR_LIT>' , spacing = ( <NUM_LIT> , <NUM_LIT:1.> , <NUM_LIT:1.> ) ) <EOL> assert ( labels_aniso2 [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> ] == <NUM_LIT:2> ) . all ( ) <EOL> def test_trivial_cases ( ) : <EOL> img = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> labels = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> with expected_warnings ( [ "<STR_LIT>" ] ) : <EOL> pass_through = random_walker ( img , labels ) <EOL> np . testing . assert_array_equal ( pass_through , labels ) <EOL> labels [ : , : <NUM_LIT:5> ] = <NUM_LIT:3> <EOL> expected = np . concatenate ( ( ( labels == <NUM_LIT:1> ) [ ... , np . newaxis ] , <EOL> ( labels == <NUM_LIT:3> ) [ ... , np . newaxis ] ) , axis = <NUM_LIT:2> ) <EOL> with expected_warnings ( [ "<STR_LIT>" ] ) : <EOL> test = random_walker ( img , labels , return_full_prob = True ) <EOL> np . testing . assert_array_equal ( test , expected ) <EOL> def test_length2_spacing ( ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> img = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) + <NUM_LIT> * np . random . normal ( size = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> labels = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , dtype = np . uint8 ) <EOL> labels [ <NUM_LIT:2> , <NUM_LIT:4> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:6> , <NUM_LIT:8> ] = <NUM_LIT:4> <EOL> random_walker ( img , labels , spacing = ( <NUM_LIT:1.> , <NUM_LIT> ) ) <EOL> def test_bad_inputs ( ) : <EOL> img = np . ones ( <NUM_LIT:10> ) <EOL> labels = np . arange ( <NUM_LIT:10> ) <EOL> np . testing . assert_raises ( ValueError , random_walker , img , labels ) <EOL> np . testing . assert_raises ( ValueError , <EOL> random_walker , img , labels , multichannel = True ) <EOL> np . random . seed ( <NUM_LIT> ) <EOL> img = np . random . normal ( size = ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> labels = np . arange ( <NUM_LIT:3> ** <NUM_LIT:5> ) . reshape ( img . shape ) <EOL> np . testing . assert_raises ( ValueError , random_walker , img , labels ) <EOL> np . testing . assert_raises ( ValueError , <EOL> random_walker , img , labels , multichannel = True ) <EOL> img = np . random . normal ( size = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> labels = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> labels [ <NUM_LIT:2> , <NUM_LIT:4> ] = <NUM_LIT:2> <EOL> labels [ <NUM_LIT:6> , <NUM_LIT:8> ] = <NUM_LIT:5> <EOL> np . testing . assert_raises ( ValueError , <EOL> random_walker , img , labels , spacing = ( <NUM_LIT:1> , ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> np . testing . run_module_suite ( ) </s>
