<s> import numpy as np <EOL> import skimage <EOL> import skimage . data as data <EOL> from skimage . filters . rank import median <EOL> from skimage . morphology import disk <EOL> from skimage . viewer import ImageViewer , has_qt <EOL> from skimage . viewer . plugins . base import Plugin <EOL> from skimage . viewer . widgets import Slider <EOL> from skimage . viewer . plugins import ( <EOL> LineProfile , Measure , CannyPlugin , LabelPainter , Crop , ColorHistogram , <EOL> PlotPlugin ) <EOL> from numpy . testing import assert_equal , assert_allclose , assert_almost_equal <EOL> from numpy . testing . decorators import skipif <EOL> from skimage . _shared . _warnings import expected_warnings <EOL> def setup_line_profile ( image , limits = '<STR_LIT:image>' ) : <EOL> viewer = ImageViewer ( skimage . img_as_float ( image ) ) <EOL> plugin = LineProfile ( limits = limits ) <EOL> viewer += plugin <EOL> return plugin <EOL> @ skipif ( not has_qt ) <EOL> def test_line_profile ( ) : <EOL> """<STR_LIT>""" <EOL> plugin = setup_line_profile ( data . camera ( ) ) <EOL> line_image , scan_data = plugin . output ( ) <EOL> for inp in [ line_image . nonzero ( ) [ <NUM_LIT:0> ] . size , <EOL> line_image . sum ( ) / line_image . max ( ) , <EOL> scan_data . size ] : <EOL> assert_equal ( inp , <NUM_LIT> ) <EOL> assert_equal ( line_image . shape , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> assert_allclose ( scan_data . max ( ) , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( scan_data . mean ( ) , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> @ skipif ( not has_qt ) <EOL> def test_line_profile_rgb ( ) : <EOL> """<STR_LIT>""" <EOL> plugin = setup_line_profile ( data . chelsea ( ) , limits = None ) <EOL> for i in range ( <NUM_LIT:6> ) : <EOL> plugin . line_tool . _thicken_scan_line ( ) <EOL> line_image , scan_data = plugin . output ( ) <EOL> assert_equal ( line_image [ line_image == <NUM_LIT> ] . size , <NUM_LIT> ) <EOL> assert_equal ( line_image [ line_image == <NUM_LIT:255> ] . size , <NUM_LIT> ) <EOL> assert_equal ( line_image . shape , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> assert_equal ( scan_data . shape , ( <NUM_LIT> , <NUM_LIT:3> ) ) <EOL> assert_allclose ( scan_data . max ( ) , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> assert_allclose ( scan_data . mean ( ) , <NUM_LIT> , rtol = <NUM_LIT> ) <EOL> @ skipif ( not has_qt ) <EOL> def test_line_profile_dynamic ( ) : <EOL> """<STR_LIT>""" <EOL> image = data . coins ( ) [ : - <NUM_LIT:50> , : ] <EOL> image = skimage . img_as_float ( image ) <EOL> viewer = ImageViewer ( image ) <EOL> lp = LineProfile ( limits = '<STR_LIT>' ) <EOL> viewer += lp <EOL> line = lp . get_profiles ( ) [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> assert line . size == <NUM_LIT> <EOL> assert_almost_equal ( np . std ( viewer . image ) , <NUM_LIT> , <NUM_LIT:3> ) <EOL> assert_almost_equal ( np . std ( line ) , <NUM_LIT> , <NUM_LIT:3> ) <EOL> assert_almost_equal ( np . max ( line ) - np . min ( line ) , <NUM_LIT> , <NUM_LIT:1> ) <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> viewer . image = skimage . img_as_float ( median ( image , <EOL> selem = disk ( radius = <NUM_LIT:3> ) ) ) <EOL> line = lp . get_profiles ( ) [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> assert_almost_equal ( np . std ( viewer . image ) , <NUM_LIT> , <NUM_LIT:3> ) <EOL> assert_almost_equal ( np . std ( line ) , <NUM_LIT> , <NUM_LIT:3> ) <EOL> assert_almost_equal ( np . max ( line ) - np . min ( line ) , <NUM_LIT> , <NUM_LIT:1> ) <EOL> @ skipif ( not has_qt ) <EOL> def test_measure ( ) : <EOL> image = data . camera ( ) <EOL> viewer = ImageViewer ( image ) <EOL> m = Measure ( ) <EOL> viewer += m <EOL> m . line_changed ( [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:10> , <NUM_LIT:10> ) ] ) <EOL> assert_equal ( str ( m . _length . text ) , '<STR_LIT>' ) <EOL> assert_equal ( str ( m . _angle . text [ : <NUM_LIT:5> ] ) , '<STR_LIT>' ) <EOL> @ skipif ( not has_qt ) <EOL> def test_canny ( ) : <EOL> image = data . camera ( ) <EOL> viewer = ImageViewer ( image ) <EOL> c = CannyPlugin ( ) <EOL> viewer += c <EOL> canny_edges = viewer . show ( False ) <EOL> viewer . close ( ) <EOL> edges = canny_edges [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert edges . sum ( ) == <NUM_LIT> <EOL> @ skipif ( not has_qt ) <EOL> def test_label_painter ( ) : <EOL> image = data . camera ( ) <EOL> moon = data . moon ( ) <EOL> viewer = ImageViewer ( image ) <EOL> lp = LabelPainter ( ) <EOL> viewer += lp <EOL> assert_equal ( lp . radius , <NUM_LIT:5> ) <EOL> lp . label = <NUM_LIT:1> <EOL> assert_equal ( str ( lp . label ) , '<STR_LIT:1>' ) <EOL> lp . label = <NUM_LIT:2> <EOL> assert_equal ( str ( lp . paint_tool . label ) , '<STR_LIT:2>' ) <EOL> assert_equal ( lp . paint_tool . radius , <NUM_LIT:5> ) <EOL> lp . _on_new_image ( moon ) <EOL> assert_equal ( lp . paint_tool . shape , moon . shape ) <EOL> @ skipif ( not has_qt ) <EOL> def test_crop ( ) : <EOL> image = data . camera ( ) <EOL> viewer = ImageViewer ( image ) <EOL> c = Crop ( ) <EOL> viewer += c <EOL> c . crop ( ( <NUM_LIT:0> , <NUM_LIT:100> , <NUM_LIT:0> , <NUM_LIT:100> ) ) <EOL> assert_equal ( viewer . image . shape , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ skipif ( not has_qt ) <EOL> def test_color_histogram ( ) : <EOL> image = skimage . img_as_float ( data . load ( '<STR_LIT>' ) ) <EOL> viewer = ImageViewer ( image ) <EOL> ch = ColorHistogram ( dock = '<STR_LIT:right>' ) <EOL> viewer += ch <EOL> assert_almost_equal ( viewer . image . std ( ) , <NUM_LIT> , <NUM_LIT:3> ) , <EOL> ch . ab_selected ( ( <NUM_LIT:0> , <NUM_LIT:100> , <NUM_LIT:0> , <NUM_LIT:100> ) ) , <EOL> assert_almost_equal ( viewer . image . std ( ) , <NUM_LIT> , <NUM_LIT:3> ) <EOL> @ skipif ( not has_qt ) <EOL> def test_plot_plugin ( ) : <EOL> viewer = ImageViewer ( data . moon ( ) ) <EOL> plugin = PlotPlugin ( image_filter = lambda x : x ) <EOL> viewer += plugin <EOL> assert_equal ( viewer . image , data . moon ( ) ) <EOL> plugin . _update_original_image ( data . coins ( ) ) <EOL> assert_equal ( viewer . image , data . coins ( ) ) <EOL> viewer . close ( ) <EOL> @ skipif ( not has_qt ) <EOL> def test_plugin ( ) : <EOL> img = skimage . img_as_float ( data . moon ( ) ) <EOL> viewer = ImageViewer ( img ) <EOL> def median_filter ( img , radius = <NUM_LIT:3> ) : <EOL> with expected_warnings ( [ '<STR_LIT>' ] ) : <EOL> return median ( img , selem = disk ( radius = radius ) ) <EOL> plugin = Plugin ( image_filter = median_filter ) <EOL> viewer += plugin <EOL> plugin += Slider ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> assert_almost_equal ( np . std ( viewer . image ) , <NUM_LIT> , <NUM_LIT:3> ) <EOL> plugin . filter_image ( ) <EOL> assert_almost_equal ( np . std ( viewer . image ) , <NUM_LIT> , <NUM_LIT:3> ) <EOL> plugin . show ( ) <EOL> plugin . close ( ) <EOL> plugin . clean_up ( ) <EOL> img , _ = plugin . output ( ) <EOL> assert_equal ( img , viewer . image ) </s>
