<s> import numpy as np <EOL> from numpy . testing import run_module_suite , assert_raises , assert_equal <EOL> from skimage import restoration , data , color , img_as_float , measure <EOL> from skimage . _shared . _warnings import expected_warnings <EOL> np . random . seed ( <NUM_LIT> ) <EOL> astro = img_as_float ( data . astronaut ( ) [ : <NUM_LIT> , : <NUM_LIT> ] ) <EOL> astro_gray = color . rgb2gray ( astro ) <EOL> checkerboard_gray = img_as_float ( data . checkerboard ( ) ) <EOL> checkerboard = color . gray2rgb ( checkerboard_gray ) <EOL> def test_denoise_tv_chambolle_2d ( ) : <EOL> img = astro_gray . copy ( ) <EOL> img += <NUM_LIT:0.5> * img . std ( ) * np . random . rand ( * img . shape ) <EOL> img = np . clip ( img , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> denoised_astro = restoration . denoise_tv_chambolle ( img , weight = <NUM_LIT:0.1> ) <EOL> assert denoised_astro . dtype in [ np . float , np . float32 , np . float64 ] <EOL> from scipy import ndimage as ndi <EOL> grad = ndi . morphological_gradient ( img , size = ( ( <NUM_LIT:3> , <NUM_LIT:3> ) ) ) <EOL> grad_denoised = ndi . morphological_gradient ( denoised_astro , size = ( ( <NUM_LIT:3> , <NUM_LIT:3> ) ) ) <EOL> assert grad_denoised . dtype == np . float <EOL> assert ( np . sqrt ( ( grad_denoised ** <NUM_LIT:2> ) . sum ( ) ) <EOL> < np . sqrt ( ( grad ** <NUM_LIT:2> ) . sum ( ) ) ) <EOL> def test_denoise_tv_chambolle_multichannel ( ) : <EOL> denoised0 = restoration . denoise_tv_chambolle ( astro [ ... , <NUM_LIT:0> ] , weight = <NUM_LIT:0.1> ) <EOL> denoised = restoration . denoise_tv_chambolle ( astro , weight = <NUM_LIT:0.1> , <EOL> multichannel = True ) <EOL> assert_equal ( denoised [ ... , <NUM_LIT:0> ] , denoised0 ) <EOL> astro3 = np . tile ( astro [ : <NUM_LIT:64> , : <NUM_LIT:64> , np . newaxis , : ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ] ) <EOL> astro3 [ : , : , <NUM_LIT:0> , : ] = <NUM_LIT:2> * astro3 [ : , : , <NUM_LIT:0> , : ] <EOL> denoised0 = restoration . denoise_tv_chambolle ( astro3 [ ... , <NUM_LIT:0> ] , weight = <NUM_LIT:0.1> ) <EOL> denoised = restoration . denoise_tv_chambolle ( astro3 , weight = <NUM_LIT:0.1> , <EOL> multichannel = True ) <EOL> assert_equal ( denoised [ ... , <NUM_LIT:0> ] , denoised0 ) <EOL> def test_denoise_tv_chambolle_float_result_range ( ) : <EOL> img = astro_gray <EOL> int_astro = np . multiply ( img , <NUM_LIT:255> ) . astype ( np . uint8 ) <EOL> assert np . max ( int_astro ) > <NUM_LIT:1> <EOL> denoised_int_astro = restoration . denoise_tv_chambolle ( int_astro , <EOL> weight = <NUM_LIT:0.1> ) <EOL> assert denoised_int_astro . dtype == np . float <EOL> assert np . max ( denoised_int_astro ) <= <NUM_LIT:1.0> <EOL> assert np . min ( denoised_int_astro ) >= <NUM_LIT:0.0> <EOL> def test_denoise_tv_chambolle_3d ( ) : <EOL> """<STR_LIT>""" <EOL> x , y , z = np . ogrid [ <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:0> : <NUM_LIT> ] <EOL> mask = ( x - <NUM_LIT> ) ** <NUM_LIT:2> + ( y - <NUM_LIT:20> ) ** <NUM_LIT:2> + ( z - <NUM_LIT> ) ** <NUM_LIT:2> < <NUM_LIT:8> ** <NUM_LIT:2> <EOL> mask = <NUM_LIT:100> * mask . astype ( np . float ) <EOL> mask += <NUM_LIT> <EOL> mask += <NUM_LIT:20> * np . random . rand ( * mask . shape ) <EOL> mask [ mask < <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> mask [ mask > <NUM_LIT:255> ] = <NUM_LIT:255> <EOL> res = restoration . denoise_tv_chambolle ( mask . astype ( np . uint8 ) , weight = <NUM_LIT:0.1> ) <EOL> assert res . dtype == np . float <EOL> assert res . std ( ) * <NUM_LIT:255> < mask . std ( ) <EOL> def test_denoise_tv_chambolle_1d ( ) : <EOL> """<STR_LIT>""" <EOL> x = <NUM_LIT> + <NUM_LIT:100> * np . sin ( np . linspace ( <NUM_LIT:0> , <NUM_LIT:8> * np . pi , <NUM_LIT:1000> ) ) <EOL> x += <NUM_LIT:20> * np . random . rand ( x . size ) <EOL> x = np . clip ( x , <NUM_LIT:0> , <NUM_LIT:255> ) <EOL> res = restoration . denoise_tv_chambolle ( x . astype ( np . uint8 ) , weight = <NUM_LIT:0.1> ) <EOL> assert res . dtype == np . float <EOL> assert res . std ( ) * <NUM_LIT:255> < x . std ( ) <EOL> def test_denoise_tv_chambolle_4d ( ) : <EOL> """<STR_LIT>""" <EOL> im = <NUM_LIT:255> * np . random . rand ( <NUM_LIT:8> , <NUM_LIT:8> , <NUM_LIT:8> , <NUM_LIT:8> ) <EOL> res = restoration . denoise_tv_chambolle ( im . astype ( np . uint8 ) , weight = <NUM_LIT:0.1> ) <EOL> assert res . dtype == np . float <EOL> assert res . std ( ) * <NUM_LIT:255> < im . std ( ) <EOL> def test_denoise_tv_chambolle_weighting ( ) : <EOL> rstate = np . random . RandomState ( <NUM_LIT> ) <EOL> img2d = astro_gray . copy ( ) <EOL> img2d += <NUM_LIT> * rstate . standard_normal ( img2d . shape ) <EOL> img2d = np . clip ( img2d , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> img4d = np . tile ( img2d [ ... , None , None ] , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> w = <NUM_LIT> <EOL> denoised_2d = restoration . denoise_tv_chambolle ( img2d , weight = w ) <EOL> denoised_4d = restoration . denoise_tv_chambolle ( img4d , weight = w ) <EOL> assert measure . compare_ssim ( denoised_2d , <EOL> denoised_4d [ : , : , <NUM_LIT:0> , <NUM_LIT:0> ] ) > <NUM_LIT> <EOL> def test_denoise_tv_bregman_2d ( ) : <EOL> img = checkerboard_gray . copy ( ) <EOL> img += <NUM_LIT:0.5> * img . std ( ) * np . random . rand ( * img . shape ) <EOL> img = np . clip ( img , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> out1 = restoration . denoise_tv_bregman ( img , weight = <NUM_LIT:10> ) <EOL> out2 = restoration . denoise_tv_bregman ( img , weight = <NUM_LIT:5> ) <EOL> assert img [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) > out1 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) <EOL> assert out1 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) > out2 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) <EOL> def test_denoise_tv_bregman_float_result_range ( ) : <EOL> img = astro_gray . copy ( ) <EOL> int_astro = np . multiply ( img , <NUM_LIT:255> ) . astype ( np . uint8 ) <EOL> assert np . max ( int_astro ) > <NUM_LIT:1> <EOL> denoised_int_astro = restoration . denoise_tv_bregman ( int_astro , weight = <NUM_LIT> ) <EOL> assert denoised_int_astro . dtype == np . float <EOL> assert np . max ( denoised_int_astro ) <= <NUM_LIT:1.0> <EOL> assert np . min ( denoised_int_astro ) >= <NUM_LIT:0.0> <EOL> def test_denoise_tv_bregman_3d ( ) : <EOL> img = checkerboard . copy ( ) <EOL> img += <NUM_LIT:0.5> * img . std ( ) * np . random . rand ( * img . shape ) <EOL> img = np . clip ( img , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> out1 = restoration . denoise_tv_bregman ( img , weight = <NUM_LIT:10> ) <EOL> out2 = restoration . denoise_tv_bregman ( img , weight = <NUM_LIT:5> ) <EOL> assert img [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) > out1 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) <EOL> assert out1 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) > out2 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) <EOL> def test_denoise_bilateral_2d ( ) : <EOL> img = checkerboard_gray . copy ( ) <EOL> img += <NUM_LIT:0.5> * img . std ( ) * np . random . rand ( * img . shape ) <EOL> img = np . clip ( img , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> out1 = restoration . denoise_bilateral ( img , sigma_color = <NUM_LIT:0.1> , <EOL> sigma_spatial = <NUM_LIT:20> , multichannel = False ) <EOL> out2 = restoration . denoise_bilateral ( img , sigma_color = <NUM_LIT> , <EOL> sigma_spatial = <NUM_LIT:30> , multichannel = False ) <EOL> assert img [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) > out1 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) <EOL> assert out1 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) > out2 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) <EOL> def test_denoise_bilateral_color ( ) : <EOL> img = checkerboard . copy ( ) <EOL> img += <NUM_LIT:0.5> * img . std ( ) * np . random . rand ( * img . shape ) <EOL> img = np . clip ( img , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> out1 = restoration . denoise_bilateral ( img , sigma_color = <NUM_LIT:0.1> , sigma_spatial = <NUM_LIT:20> ) <EOL> out2 = restoration . denoise_bilateral ( img , sigma_color = <NUM_LIT> , sigma_spatial = <NUM_LIT:30> ) <EOL> assert img [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) > out1 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) <EOL> assert out1 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) > out2 [ <NUM_LIT:30> : <NUM_LIT> , <NUM_LIT:5> : <NUM_LIT:15> ] . std ( ) <EOL> def test_denoise_bilateral_3d_grayscale ( ) : <EOL> img = np . ones ( ( <NUM_LIT:50> , <NUM_LIT:50> , <NUM_LIT:3> ) ) <EOL> assert_raises ( ValueError , restoration . denoise_bilateral , img , <EOL> multichannel = False ) <EOL> def test_denoise_bilateral_3d_multichannel ( ) : <EOL> img = np . ones ( ( <NUM_LIT:50> , <NUM_LIT:50> , <NUM_LIT:50> ) ) <EOL> with expected_warnings ( [ "<STR_LIT>" ] ) : <EOL> result = restoration . denoise_bilateral ( img ) <EOL> expected = np . empty_like ( img ) <EOL> expected . fill ( np . nan ) <EOL> assert_equal ( result , expected ) <EOL> def test_denoise_bilateral_multidimensional ( ) : <EOL> img = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> assert_raises ( ValueError , restoration . denoise_bilateral , img ) <EOL> assert_raises ( ValueError , restoration . denoise_bilateral , img , <EOL> multichannel = True ) <EOL> def test_denoise_bilateral_nan ( ) : <EOL> img = np . NaN + np . empty ( ( <NUM_LIT:50> , <NUM_LIT:50> ) ) <EOL> out = restoration . denoise_bilateral ( img , multichannel = False ) <EOL> assert_equal ( img , out ) <EOL> def test_denoise_sigma_range ( ) : <EOL> img = checkerboard_gray . copy ( ) <EOL> img += <NUM_LIT:0.5> * img . std ( ) * np . random . rand ( * img . shape ) <EOL> img = np . clip ( img , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> out1 = restoration . denoise_bilateral ( img , sigma_color = <NUM_LIT:0.1> , <EOL> sigma_spatial = <NUM_LIT:20> , multichannel = False ) <EOL> with expected_warnings ( '<STR_LIT>' <EOL> '<STR_LIT>' ) : <EOL> out2 = restoration . denoise_bilateral ( img , sigma_range = <NUM_LIT:0.1> , <EOL> sigma_spatial = <NUM_LIT:20> , multichannel = False ) <EOL> assert_equal ( out1 , out2 ) <EOL> def test_denoise_sigma_range_and_sigma_color ( ) : <EOL> img = checkerboard_gray . copy ( ) <EOL> img += <NUM_LIT:0.5> * img . std ( ) * np . random . rand ( * img . shape ) <EOL> img = np . clip ( img , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> out1 = restoration . denoise_bilateral ( img , sigma_color = <NUM_LIT:0.1> , <EOL> sigma_spatial = <NUM_LIT:20> , multichannel = False ) <EOL> with expected_warnings ( '<STR_LIT>' <EOL> '<STR_LIT>' ) : <EOL> out2 = restoration . denoise_bilateral ( img , sigma_color = <NUM_LIT> , sigma_range = <NUM_LIT:0.1> , <EOL> sigma_spatial = <NUM_LIT:20> , multichannel = False ) <EOL> assert_equal ( out1 , out2 ) <EOL> def test_nl_means_denoising_2d ( ) : <EOL> img = np . zeros ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> img [ <NUM_LIT:10> : - <NUM_LIT:10> , <NUM_LIT:10> : - <NUM_LIT:10> ] = <NUM_LIT:1.> <EOL> img += <NUM_LIT> * np . random . randn ( * img . shape ) <EOL> denoised = restoration . denoise_nl_means ( img , <NUM_LIT:7> , <NUM_LIT:5> , <NUM_LIT> , fast_mode = True ) <EOL> assert img . std ( ) > denoised . std ( ) <EOL> denoised = restoration . denoise_nl_means ( img , <NUM_LIT:7> , <NUM_LIT:5> , <NUM_LIT> , fast_mode = False ) <EOL> assert img . std ( ) > denoised . std ( ) <EOL> def test_denoise_nl_means_2drgb ( ) : <EOL> img = np . copy ( astro [ : <NUM_LIT:50> , : <NUM_LIT:50> ] ) <EOL> img += <NUM_LIT:0.5> * img . std ( ) * np . random . random ( img . shape ) <EOL> img = np . clip ( img , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> denoised = restoration . denoise_nl_means ( img , <NUM_LIT:7> , <NUM_LIT:9> , <NUM_LIT> , fast_mode = True ) <EOL> assert img . std ( ) > denoised . std ( ) <EOL> denoised = restoration . denoise_nl_means ( img , <NUM_LIT:7> , <NUM_LIT:9> , <NUM_LIT> , fast_mode = False ) <EOL> assert img . std ( ) > denoised . std ( ) <EOL> def test_denoise_nl_means_3d ( ) : <EOL> img = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT:10> ) ) <EOL> img [ <NUM_LIT:5> : - <NUM_LIT:5> , <NUM_LIT:5> : - <NUM_LIT:5> , <NUM_LIT:3> : - <NUM_LIT:3> ] = <NUM_LIT:1.> <EOL> img += <NUM_LIT> * np . random . randn ( * img . shape ) <EOL> denoised = restoration . denoise_nl_means ( img , <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT> , fast_mode = True , <EOL> multichannel = False ) <EOL> assert img . std ( ) > denoised . std ( ) <EOL> denoised = restoration . denoise_nl_means ( img , <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT> , fast_mode = False , <EOL> multichannel = False ) <EOL> assert img . std ( ) > denoised . std ( ) <EOL> def test_denoise_nl_means_multichannel ( ) : <EOL> img = np . zeros ( ( <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT:10> ) ) <EOL> img [ <NUM_LIT:10> , <NUM_LIT:9> : <NUM_LIT:11> , <NUM_LIT:2> : - <NUM_LIT:2> ] = <NUM_LIT:1.> <EOL> img += <NUM_LIT> * np . random . randn ( * img . shape ) <EOL> denoised_wrong_multichannel = restoration . denoise_nl_means ( img , <EOL> <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT:0.1> , fast_mode = True , multichannel = True ) <EOL> denoised_ok_multichannel = restoration . denoise_nl_means ( img , <EOL> <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT:0.1> , fast_mode = True , multichannel = False ) <EOL> snr_wrong = <NUM_LIT:10> * np . log10 ( <NUM_LIT:1.> / <EOL> ( ( denoised_wrong_multichannel - img ) ** <NUM_LIT:2> ) . mean ( ) ) <EOL> snr_ok = <NUM_LIT:10> * np . log10 ( <NUM_LIT:1.> / <EOL> ( ( denoised_ok_multichannel - img ) ** <NUM_LIT:2> ) . mean ( ) ) <EOL> assert snr_ok > snr_wrong <EOL> def test_denoise_nl_means_wrong_dimension ( ) : <EOL> img = np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> assert_raises ( NotImplementedError , restoration . denoise_nl_means , img ) <EOL> def test_no_denoising_for_small_h ( ) : <EOL> img = np . zeros ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> img [ <NUM_LIT:10> : - <NUM_LIT:10> , <NUM_LIT:10> : - <NUM_LIT:10> ] = <NUM_LIT:1.> <EOL> img += <NUM_LIT> * np . random . randn ( * img . shape ) <EOL> denoised = restoration . denoise_nl_means ( img , <NUM_LIT:7> , <NUM_LIT:5> , <NUM_LIT> , fast_mode = True ) <EOL> assert np . allclose ( denoised , img ) <EOL> denoised = restoration . denoise_nl_means ( img , <NUM_LIT:7> , <NUM_LIT:5> , <NUM_LIT> , fast_mode = False ) <EOL> assert np . allclose ( denoised , img ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_module_suite ( ) </s>
