<s> from __future__ import absolute_import , division , print_function <EOL> from distutils . version import LooseVersion <EOL> import pytest <EOL> from mock import patch <EOL> from matplotlib import __version__ as mpl_version <EOL> from glue import core <EOL> from glue . core . tests . util import simple_session <EOL> from glue . viewers . common . qt . mpl_widget import MplCanvas <EOL> from . . viewer_widget import ScatterWidget <EOL> class TestScatterWidget ( object ) : <EOL> def setup_method ( self , method ) : <EOL> s = simple_session ( ) <EOL> self . hub = s . hub <EOL> self . d1 = core . Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , y = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , <EOL> z = [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , w = [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> self . d1 . label = '<STR_LIT>' <EOL> self . d2 = core . Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , y = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , <EOL> z = [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , w = [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> self . d2 . label = '<STR_LIT>' <EOL> self . data = [ self . d1 , self . d2 ] <EOL> self . collect = s . data_collection <EOL> self . collect . append ( self . data ) <EOL> self . widget = ScatterWidget ( s ) <EOL> self . session = s <EOL> self . connect_to_hub ( ) <EOL> def teardown_method ( self , method ) : <EOL> self . assert_widget_synced ( ) <EOL> def assert_widget_synced ( self ) : <EOL> cl = self . widget . client <EOL> w = self . widget <EOL> assert abs ( w . xmin - cl . xmin ) < <NUM_LIT> <EOL> assert abs ( w . xmax - cl . xmax ) < <NUM_LIT> <EOL> assert w . xlog == cl . xlog <EOL> assert w . ylog == cl . ylog <EOL> assert w . xflip == cl . xflip <EOL> assert w . yflip == cl . yflip <EOL> assert abs ( w . ymin - cl . ymin ) < <NUM_LIT> <EOL> assert abs ( w . ymax - cl . ymax ) < <NUM_LIT> <EOL> def connect_to_hub ( self ) : <EOL> self . widget . register_to_hub ( self . hub ) <EOL> self . collect . register_to_hub ( self . hub ) <EOL> def add_layer_via_hub ( self ) : <EOL> layer = self . data [ <NUM_LIT:0> ] <EOL> layer . label = '<STR_LIT>' <EOL> self . collect . append ( layer ) <EOL> return layer <EOL> def add_layer_via_method ( self , index = <NUM_LIT:0> ) : <EOL> layer = self . data [ index ] <EOL> self . widget . add_data ( layer ) <EOL> return layer <EOL> def plot_data ( self , layer ) : <EOL> """<STR_LIT>""" <EOL> client = self . widget . client <EOL> x , y = client . artists [ layer ] [ <NUM_LIT:0> ] . get_data ( ) <EOL> assert x . size > <NUM_LIT:0> <EOL> assert y . size > <NUM_LIT:0> <EOL> xmin = x . min ( ) <EOL> xmax = x . max ( ) <EOL> ymin = y . min ( ) <EOL> ymax = y . max ( ) <EOL> return [ xmin , xmax ] , [ ymin , ymax ] <EOL> def plot_limits ( self ) : <EOL> """<STR_LIT>""" <EOL> ax = self . widget . client . axes <EOL> xlim = ax . get_xlim ( ) <EOL> ylim = ax . get_ylim ( ) <EOL> return xlim , ylim <EOL> def assert_layer_inside_limits ( self , layer ) : <EOL> """<STR_LIT>""" <EOL> xydata = self . plot_data ( layer ) <EOL> xylimits = self . plot_limits ( ) <EOL> assert xydata [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] >= xylimits [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert xydata [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] >= xylimits [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> assert xydata [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <= xylimits [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> assert xydata [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <= xylimits [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> def is_layer_present ( self , layer ) : <EOL> return self . widget . client . is_layer_present ( layer ) <EOL> def is_layer_visible ( self , layer ) : <EOL> return self . widget . client . is_visible ( layer ) <EOL> def test_rescaled_on_init ( self ) : <EOL> layer = self . add_layer_via_method ( ) <EOL> self . assert_layer_inside_limits ( layer ) <EOL> def test_hub_data_add_is_ignored ( self ) : <EOL> layer = self . add_layer_via_hub ( ) <EOL> assert not self . widget . client . is_layer_present ( layer ) <EOL> def test_valid_add_data_via_method ( self ) : <EOL> layer = self . add_layer_via_method ( ) <EOL> assert self . is_layer_present ( layer ) <EOL> def test_add_first_data_updates_combos ( self ) : <EOL> self . add_layer_via_method ( ) <EOL> xatt = str ( self . widget . ui . xAxisComboBox . currentText ( ) ) <EOL> yatt = str ( self . widget . ui . yAxisComboBox . currentText ( ) ) <EOL> assert xatt is not None <EOL> assert yatt is not None <EOL> def test_flip_x ( self ) : <EOL> self . add_layer_via_method ( ) <EOL> self . widget . xflip = True <EOL> assert self . widget . client . xflip <EOL> self . widget . xflip = False <EOL> assert not self . widget . client . xflip <EOL> def test_flip_y ( self ) : <EOL> self . add_layer_via_method ( ) <EOL> self . widget . yflip = True <EOL> assert self . widget . client . yflip <EOL> self . widget . yflip = False <EOL> assert not self . widget . client . yflip <EOL> def test_log_x ( self ) : <EOL> self . add_layer_via_method ( ) <EOL> self . widget . xlog = True <EOL> assert self . widget . client . xlog <EOL> self . widget . xlog = False <EOL> assert not self . widget . client . xlog <EOL> def test_log_y ( self ) : <EOL> self . widget . ylog = True <EOL> assert self . widget . client . ylog <EOL> self . widget . ylog = False <EOL> assert not self . widget . client . ylog <EOL> def test_double_add_ignored ( self ) : <EOL> self . add_layer_via_method ( ) <EOL> nobj = self . widget . ui . xAxisComboBox . count ( ) <EOL> self . add_layer_via_method ( ) <EOL> assert self . widget . ui . xAxisComboBox . count ( ) == nobj <EOL> def test_subsets_dont_duplicate_fields ( self ) : <EOL> layer = self . add_layer_via_method ( ) <EOL> nobj = self . widget . ui . xAxisComboBox . count ( ) <EOL> subset = layer . new_subset ( ) <EOL> subset . register ( ) <EOL> assert self . widget . ui . xAxisComboBox . count ( ) == nobj <EOL> def test_correct_title_single_data ( self ) : <EOL> ct = self . widget . client . layer_count <EOL> assert ct == <NUM_LIT:0> <EOL> layer = self . add_layer_via_method ( ) <EOL> ct = self . widget . client . layer_count <EOL> assert ct == <NUM_LIT:1> <EOL> assert len ( layer . label ) > <NUM_LIT:0> <EOL> assert self . widget . windowTitle ( ) == layer . label <EOL> def test_title_updates_with_label_change ( self ) : <EOL> layer = self . add_layer_via_method ( ) <EOL> assert layer . hub is self . hub <EOL> layer . label = "<STR_LIT>" <EOL> assert self . widget . windowTitle ( ) == layer . label <EOL> def test_title_updates_with_second_data ( self ) : <EOL> l1 = self . add_layer_via_method ( <NUM_LIT:0> ) <EOL> l2 = self . add_layer_via_method ( <NUM_LIT:1> ) <EOL> expected = '<STR_LIT>' % ( l1 . label , l2 . label ) <EOL> self . widget . windowTitle ( ) == expected <EOL> def test_second_data_add_preserves_plot_variables ( self ) : <EOL> self . add_layer_via_method ( <NUM_LIT:0> ) <EOL> self . widget . ui . xAxisComboBox . setCurrentIndex ( <NUM_LIT:3> ) <EOL> self . widget . ui . yAxisComboBox . setCurrentIndex ( <NUM_LIT:2> ) <EOL> self . add_layer_via_method ( <NUM_LIT:1> ) <EOL> assert self . widget . ui . xAxisComboBox . currentIndex ( ) == <NUM_LIT:3> <EOL> assert self . widget . ui . yAxisComboBox . currentIndex ( ) == <NUM_LIT:2> <EOL> def test_set_limits ( self ) : <EOL> self . add_layer_via_method ( <NUM_LIT:0> ) <EOL> w = self . widget <EOL> c = self . widget . client <EOL> ax = self . widget . client . axes <EOL> print ( w . xmin , w . xmax , w . ymin , w . ymax ) <EOL> print ( c . xmin , c . xmax , c . ymin , c . ymax ) <EOL> print ( ax . get_xlim ( ) , ax . get_ylim ( ) ) <EOL> self . widget . xmax = <NUM_LIT:20> <EOL> print ( w . xmin , w . xmax , w . ymin , w . ymax ) <EOL> print ( c . xmin , c . xmax , c . ymin , c . ymax ) <EOL> print ( ax . get_xlim ( ) , ax . get_ylim ( ) ) <EOL> self . widget . xmin = <NUM_LIT:10> <EOL> print ( w . xmin , w . xmax , w . ymin , w . ymax ) <EOL> print ( c . xmin , c . xmax , c . ymin , c . ymax ) <EOL> print ( ax . get_xlim ( ) , ax . get_ylim ( ) ) <EOL> self . widget . ymax = <NUM_LIT> <EOL> print ( w . xmin , w . xmax , w . ymin , w . ymax ) <EOL> print ( c . xmin , c . xmax , c . ymin , c . ymax ) <EOL> print ( ax . get_xlim ( ) , ax . get_ylim ( ) ) <EOL> self . widget . ymin = <NUM_LIT:30> <EOL> print ( w . xmin , w . xmax , w . ymin , w . ymax ) <EOL> print ( c . xmin , c . xmax , c . ymin , c . ymax ) <EOL> print ( ax . get_xlim ( ) , ax . get_ylim ( ) ) <EOL> assert self . widget . client . axes . get_xlim ( ) == ( <NUM_LIT:10> , <NUM_LIT:20> ) <EOL> assert self . widget . client . axes . get_ylim ( ) == ( <NUM_LIT:30> , <NUM_LIT> ) <EOL> assert float ( self . widget . ui . xmin . text ( ) ) == <NUM_LIT:10> <EOL> assert float ( self . widget . ui . xmax . text ( ) ) == <NUM_LIT:20> <EOL> assert float ( self . widget . ui . ymin . text ( ) ) == <NUM_LIT:30> <EOL> assert float ( self . widget . ui . ymax . text ( ) ) == <NUM_LIT> <EOL> def test_widget_props_synced_with_client ( self ) : <EOL> self . widget . client . xmax = <NUM_LIT:100> <EOL> assert self . widget . xmax == <NUM_LIT:100> <EOL> self . widget . client . ymax = <NUM_LIT:200> <EOL> assert self . widget . ymax == <NUM_LIT:200> <EOL> self . widget . client . xmin = <NUM_LIT:10> <EOL> assert self . widget . xmin == <NUM_LIT:10> <EOL> self . widget . client . ymin = <NUM_LIT:30> <EOL> assert self . widget . ymin == <NUM_LIT:30> <EOL> @ pytest . mark . xfail ( "<STR_LIT>" ) <EOL> def test_labels_sync_with_plot_limits ( self ) : <EOL> """<STR_LIT>""" <EOL> self . add_layer_via_method ( <NUM_LIT:0> ) <EOL> self . widget . client . axes . set_xlim ( ( <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> self . widget . client . axes . set_ylim ( ( <NUM_LIT:5> , <NUM_LIT:6> ) ) <EOL> super ( MplCanvas , self . widget . client . axes . figure . canvas ) . draw ( ) <EOL> assert float ( self . widget . ui . xmin . text ( ) ) == <NUM_LIT:3> <EOL> assert float ( self . widget . ui . xmax . text ( ) ) == <NUM_LIT:4> <EOL> assert float ( self . widget . ui . ymin . text ( ) ) == <NUM_LIT:5> <EOL> assert float ( self . widget . ui . ymax . text ( ) ) == <NUM_LIT:6> <EOL> def assert_component_present ( self , label ) : <EOL> ui = self . widget . ui <EOL> for combo in [ ui . xAxisComboBox , ui . yAxisComboBox ] : <EOL> atts = [ combo . itemText ( i ) for i in range ( combo . count ( ) ) ] <EOL> assert label in atts <EOL> def test_component_change_syncs_with_combo ( self ) : <EOL> l1 = self . add_layer_via_method ( ) <EOL> l1 . add_component ( l1 [ l1 . components [ <NUM_LIT:0> ] ] , '<STR_LIT>' ) <EOL> self . assert_component_present ( '<STR_LIT>' ) <EOL> def test_swap_axes ( self ) : <EOL> self . add_layer_via_method ( ) <EOL> cl = self . widget . client <EOL> cl . xlog , cl . xflip = True , True <EOL> cl . ylog , cl . yflip = False , False <EOL> x , y = cl . xatt , cl . yatt <EOL> self . widget . ui . swapAxes . click ( ) <EOL> assert ( cl . xlog , cl . xflip ) == ( False , False ) <EOL> assert ( cl . ylog , cl . yflip ) == ( True , True ) <EOL> assert ( cl . xatt , cl . yatt ) == ( y , x ) <EOL> def test_hidden ( self ) : <EOL> self . add_layer_via_method ( ) <EOL> xcombo = self . widget . ui . xAxisComboBox <EOL> self . widget . hidden = False <EOL> assert xcombo . count ( ) == <NUM_LIT:4> <EOL> self . widget . hidden = True <EOL> assert xcombo . count ( ) == <NUM_LIT:6> <EOL> self . widget . hidden = False <EOL> assert xcombo . count ( ) == <NUM_LIT:4> <EOL> def test_add_subset_preserves_plot_variables ( self ) : <EOL> self . add_layer_via_method ( <NUM_LIT:0> ) <EOL> print ( self . widget . client . layer_count ) <EOL> self . widget . ui . xAxisComboBox . setCurrentIndex ( <NUM_LIT:3> ) <EOL> self . widget . ui . yAxisComboBox . setCurrentIndex ( <NUM_LIT:2> ) <EOL> assert self . widget . ui . xAxisComboBox . currentIndex ( ) == <NUM_LIT:3> <EOL> assert self . widget . ui . yAxisComboBox . currentIndex ( ) == <NUM_LIT:2> <EOL> s = self . data [ <NUM_LIT:1> ] . new_subset ( label = '<STR_LIT>' ) <EOL> self . widget . add_subset ( s ) <EOL> assert self . widget . ui . xAxisComboBox . currentIndex ( ) == <NUM_LIT:3> <EOL> assert self . widget . ui . yAxisComboBox . currentIndex ( ) == <NUM_LIT:2> <EOL> def test_title_synced_if_data_removed ( self ) : <EOL> n0 = self . widget . windowTitle ( ) <EOL> self . add_layer_via_method ( <NUM_LIT:0> ) <EOL> n1 = self . widget . windowTitle ( ) <EOL> assert n1 != n0 <EOL> l2 = self . add_layer_via_method ( <NUM_LIT:1> ) <EOL> n2 = self . widget . windowTitle ( ) <EOL> assert n2 != n1 <EOL> self . widget . remove_layer ( l2 ) <EOL> assert self . widget . windowTitle ( ) == n1 <EOL> class TestDrawCount ( TestScatterWidget ) : <EOL> def patch_draw ( self ) : <EOL> return patch ( '<STR_LIT>' ) <EOL> def test_xatt_redraws_once ( self ) : <EOL> self . add_layer_via_method ( ) <EOL> with self . patch_draw ( ) as draw : <EOL> self . widget . yatt = self . widget . xatt <EOL> assert draw . call_count == <NUM_LIT:1> <EOL> def test_swap_redraws_once ( self ) : <EOL> self . add_layer_via_method ( ) <EOL> with self . patch_draw ( ) as draw : <EOL> self . widget . swap_axes ( ) <EOL> assert draw . call_count == <NUM_LIT:1> </s>
