<s> from __future__ import absolute_import , division , print_function <EOL> import warnings <EOL> import operator <EOL> import pytest <EOL> import numpy as np <EOL> from mock import MagicMock <EOL> from glue . external import six <EOL> from glue import core <EOL> from glue . tests . helpers import requires_astropy <EOL> from . . component import ( Component , DerivedComponent , CoordinateComponent , <EOL> CategoricalComponent ) <EOL> from . . component_id import ComponentID <EOL> from . . data import Data <EOL> VIEWS = ( np . s_ [ : ] , np . s_ [ <NUM_LIT:1> ] , np . s_ [ : : - <NUM_LIT:1> ] , np . s_ [ <NUM_LIT:0> , : ] ) <EOL> class TestComponent ( object ) : <EOL> def setup_method ( self , method ) : <EOL> self . data = MagicMock ( ) <EOL> self . data . shape = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> self . component = Component ( self . data ) <EOL> def test_data ( self ) : <EOL> assert self . component . data is self . data <EOL> def test_shape ( self ) : <EOL> assert self . component . shape is self . data . shape <EOL> def test_ndim ( self ) : <EOL> assert self . component . ndim is len ( self . data . shape ) <EOL> class TestComponentID ( object ) : <EOL> def setup_method ( self , method ) : <EOL> self . cid = ComponentID ( '<STR_LIT:test>' ) <EOL> def test_label ( self ) : <EOL> assert self . cid . label == '<STR_LIT:test>' <EOL> def test_str ( self ) : <EOL> """<STR_LIT>""" <EOL> str ( self . cid ) <EOL> def test_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> repr ( self . cid ) <EOL> class TestDerivedComponent ( object ) : <EOL> def setup_method ( self , method ) : <EOL> data = MagicMock ( ) <EOL> link = MagicMock ( ) <EOL> self . cid = DerivedComponent ( data , link ) <EOL> self . link = link <EOL> self . data = data <EOL> def test_data ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cid . data <EOL> self . link . compute . assert_called_once_with ( self . data ) <EOL> def test_link ( self ) : <EOL> assert self . cid . link == self . link <EOL> class TestCategoricalComponent ( object ) : <EOL> def setup_method ( self , method ) : <EOL> self . list_data = [ '<STR_LIT:a>' , '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:b>' ] <EOL> self . array_data = np . array ( self . list_data ) <EOL> def test_autodetection ( self ) : <EOL> assert Component . autotyped ( self . array_data ) . categorical <EOL> assert Component . autotyped ( self . list_data ) . categorical <EOL> x = np . array ( [ True , False , True , False ] ) <EOL> assert not Component . autotyped ( x ) . categorical <EOL> x = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert not Component . autotyped ( x ) . categorical <EOL> x = np . array ( [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' , '<STR_LIT:4>' ] ) <EOL> assert not Component . autotyped ( x ) . categorical <EOL> d = Data ( x = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> assert d . get_component ( '<STR_LIT:x>' ) . categorical <EOL> def test_basic_properties ( self ) : <EOL> data = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:b>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:a>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] <EOL> cat_comp = CategoricalComponent ( data ) <EOL> np . testing . assert_equal ( cat_comp . labels , data ) <EOL> np . testing . assert_equal ( cat_comp . codes , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> np . testing . assert_equal ( cat_comp . categories , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> cat_comp . data <EOL> assert len ( w ) == <NUM_LIT:1> <EOL> assert str ( w [ <NUM_LIT:0> ] . message ) == ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_accepts_numpy ( self ) : <EOL> cat_comp = CategoricalComponent ( self . array_data ) <EOL> assert cat_comp . _categorical_data . shape == ( <NUM_LIT:4> , ) <EOL> def test_accepts_list ( self ) : <EOL> """<STR_LIT>""" <EOL> cat_comp = CategoricalComponent ( self . list_data ) <EOL> np . testing . assert_equal ( cat_comp . _categorical_data , self . array_data ) <EOL> def test_multi_nans ( self ) : <EOL> cat_comp = CategoricalComponent ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT>' ] ) <EOL> np . testing . assert_equal ( cat_comp . codes , <EOL> np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) ) <EOL> np . testing . assert_equal ( cat_comp . categories , <EOL> np . asarray ( [ '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT>' ] ) ) <EOL> def test_calculate_grouping ( self ) : <EOL> cat_comp = CategoricalComponent ( self . array_data ) <EOL> np . testing . assert_equal ( cat_comp . categories , np . asarray ( [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) ) <EOL> np . testing . assert_equal ( cat_comp . codes , np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ) ) <EOL> assert cat_comp . codes . dtype == np . float <EOL> def test_accepts_provided_grouping ( self ) : <EOL> ncategories = [ '<STR_LIT:b>' , '<STR_LIT:c>' ] <EOL> cat_data = list ( '<STR_LIT>' ) <EOL> cat_comp = CategoricalComponent ( cat_data , categories = ncategories ) <EOL> assert cat_comp . categories == ncategories <EOL> assert np . all ( np . isnan ( cat_comp . codes [ : <NUM_LIT:3> ] ) ) <EOL> assert np . all ( cat_comp . codes [ <NUM_LIT:3> : <NUM_LIT:6> ] == <NUM_LIT:0> ) <EOL> assert np . all ( cat_comp . codes [ <NUM_LIT:6> : <NUM_LIT:9> ] == <NUM_LIT:1> ) <EOL> assert np . all ( np . isnan ( cat_comp . codes [ <NUM_LIT:9> : ] ) ) <EOL> def test_uniform_jitter ( self ) : <EOL> cat_comp = CategoricalComponent ( self . array_data ) <EOL> second_comp = CategoricalComponent ( self . array_data ) <EOL> cat_comp . jitter ( method = '<STR_LIT>' ) <EOL> assert np . all ( cat_comp . codes != second_comp . codes ) , "<STR_LIT>" <EOL> second_comp . jitter ( method = '<STR_LIT>' ) <EOL> np . testing . assert_equal ( cat_comp . codes , <EOL> second_comp . codes , <EOL> "<STR_LIT>" ) <EOL> assert cat_comp . _jitter_method == '<STR_LIT>' <EOL> def test_no_double_jitter ( self ) : <EOL> cat_comp = CategoricalComponent ( self . array_data ) <EOL> second_comp = CategoricalComponent ( self . array_data ) <EOL> cat_comp . jitter ( method = '<STR_LIT>' ) <EOL> delta = np . abs ( cat_comp . codes - second_comp . codes ) . sum ( ) <EOL> assert delta > <NUM_LIT:0> <EOL> second_comp . jitter ( method = '<STR_LIT>' ) <EOL> second_comp . jitter ( method = '<STR_LIT>' ) <EOL> np . testing . assert_equal ( cat_comp . codes , <EOL> second_comp . codes , <EOL> "<STR_LIT>" ) <EOL> def test_unjitter_data ( self ) : <EOL> cat_comp = CategoricalComponent ( self . array_data ) <EOL> second_comp = CategoricalComponent ( self . array_data ) <EOL> cat_comp . jitter ( method = '<STR_LIT>' ) <EOL> delta = np . abs ( cat_comp . codes - second_comp . codes ) . sum ( ) <EOL> assert delta > <NUM_LIT:0> <EOL> cat_comp . jitter ( method = None ) <EOL> np . testing . assert_equal ( cat_comp . codes , <EOL> second_comp . codes , <EOL> "<STR_LIT>" ) <EOL> def test_jitter_on_init ( self ) : <EOL> cat_comp = CategoricalComponent ( self . array_data , jitter = '<STR_LIT>' ) <EOL> second_comp = CategoricalComponent ( self . array_data ) <EOL> second_comp . jitter ( method = '<STR_LIT>' ) <EOL> delta = np . abs ( cat_comp . codes - second_comp . codes ) . sum ( ) <EOL> assert delta == <NUM_LIT:0> <EOL> def test_object_dtype ( self ) : <EOL> d = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:1> , '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:a>' ] , dtype = object ) <EOL> c = CategoricalComponent ( d ) <EOL> np . testing . assert_array_equal ( c . categories , <EOL> np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , '<STR_LIT:a>' , '<STR_LIT:b>' ] , dtype = object ) ) <EOL> np . testing . assert_array_equal ( c . codes , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> ] ) <EOL> def test_valueerror_on_bad_jitter ( self ) : <EOL> with pytest . raises ( ValueError ) : <EOL> cat_comp = CategoricalComponent ( self . array_data ) <EOL> cat_comp . jitter ( method = '<STR_LIT>' ) <EOL> class TestCoordinateComponent ( object ) : <EOL> def setup_method ( self , method ) : <EOL> class TestCoords ( object ) : <EOL> def pixel2world ( self , * args ) : <EOL> return [ a * ( i + <NUM_LIT:1> ) for i , a in enumerate ( args ) ] <EOL> data = core . Data ( ) <EOL> data . add_component ( Component ( np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) ) ) , '<STR_LIT:test>' ) <EOL> data . coords = TestCoords ( ) <EOL> self . data = data <EOL> self . px = CoordinateComponent ( data , <NUM_LIT:2> ) <EOL> self . py = CoordinateComponent ( data , <NUM_LIT:1> ) <EOL> self . pz = CoordinateComponent ( data , <NUM_LIT:0> ) <EOL> self . wx = CoordinateComponent ( data , <NUM_LIT:2> , world = True ) <EOL> self . wy = CoordinateComponent ( data , <NUM_LIT:1> , world = True ) <EOL> self . wz = CoordinateComponent ( data , <NUM_LIT:0> , world = True ) <EOL> def test_data ( self ) : <EOL> z , y , x = np . mgrid [ <NUM_LIT:0> : <NUM_LIT:3> , <NUM_LIT:0> : <NUM_LIT:3> , <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> np . testing . assert_array_equal ( self . px . data , x ) <EOL> np . testing . assert_array_equal ( self . py . data , y ) <EOL> np . testing . assert_array_equal ( self . pz . data , z ) <EOL> np . testing . assert_array_equal ( self . wx . data , x * <NUM_LIT:1> ) <EOL> np . testing . assert_array_equal ( self . wy . data , y * <NUM_LIT:2> ) <EOL> np . testing . assert_array_equal ( self . wz . data , z * <NUM_LIT:3> ) <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT>' ) , VIEWS ) <EOL> def test_view ( self , view ) : <EOL> z , y , x = np . mgrid [ <NUM_LIT:0> : <NUM_LIT:3> , <NUM_LIT:0> : <NUM_LIT:3> , <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> np . testing . assert_array_equal ( self . px [ view ] , x [ view ] ) <EOL> np . testing . assert_array_equal ( self . py [ view ] , y [ view ] ) <EOL> np . testing . assert_array_equal ( self . pz [ view ] , z [ view ] ) <EOL> np . testing . assert_array_equal ( self . wx [ view ] , x [ view ] * <NUM_LIT:1> ) <EOL> np . testing . assert_array_equal ( self . wy [ view ] , y [ view ] * <NUM_LIT:2> ) <EOL> np . testing . assert_array_equal ( self . wz [ view ] , z [ view ] * <NUM_LIT:3> ) <EOL> def check_binary ( result , left , right , op ) : <EOL> assert isinstance ( result , core . subset . InequalitySubsetState ) <EOL> assert result . left is left <EOL> assert result . right is right <EOL> assert result . operator is op <EOL> def check_link ( result , left , right ) : <EOL> assert isinstance ( result , core . component_link . ComponentLink ) <EOL> if isinstance ( left , ComponentID ) : <EOL> assert left in result . get_from_ids ( ) <EOL> if isinstance ( right , ComponentID ) : <EOL> assert right in result . get_from_ids ( ) <EOL> COMPARE_OPS = ( operator . gt , operator . ge , operator . lt , operator . le ) <EOL> NUMBER_OPS = ( operator . add , operator . mul , operator . truediv , operator . sub ) <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT>' ) , COMPARE_OPS ) <EOL> def test_inequality_scalar ( op ) : <EOL> cid = ComponentID ( '<STR_LIT:test>' ) <EOL> result = op ( cid , <NUM_LIT:3> ) <EOL> check_binary ( result , cid , <NUM_LIT:3> , op ) <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT>' ) , COMPARE_OPS ) <EOL> def test_inequality_id ( op ) : <EOL> cid = ComponentID ( '<STR_LIT:test>' ) <EOL> cid2 = ComponentID ( '<STR_LIT>' ) <EOL> result = op ( cid , cid2 ) <EOL> check_binary ( result , cid , cid2 , op ) <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT>' ) , NUMBER_OPS ) <EOL> def test_arithmetic_scalar ( op ) : <EOL> cid = ComponentID ( '<STR_LIT:test>' ) <EOL> result = op ( cid , <NUM_LIT:3> ) <EOL> check_link ( result , cid , <NUM_LIT:3> ) <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT>' ) , NUMBER_OPS ) <EOL> def test_arithmetic_scalar_right ( op ) : <EOL> cid = ComponentID ( '<STR_LIT:test>' ) <EOL> result = op ( <NUM_LIT:3> , cid ) <EOL> check_link ( result , <NUM_LIT:3> , cid ) <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT>' ) , NUMBER_OPS ) <EOL> def test_arithmetic_cid ( op ) : <EOL> cid = ComponentID ( '<STR_LIT:test>' ) <EOL> cid2 = ComponentID ( '<STR_LIT>' ) <EOL> result = op ( cid , cid2 ) <EOL> check_link ( result , cid , cid2 ) <EOL> def test_pow_scalar ( ) : <EOL> cid = ComponentID ( '<STR_LIT:test>' ) <EOL> result = cid ** <NUM_LIT:3> <EOL> check_link ( result , cid , <NUM_LIT:3> ) <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT>' ) , VIEWS ) <EOL> def test_view ( view ) : <EOL> comp = Component ( np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ] ) ) <EOL> np . testing . assert_array_equal ( comp [ view ] , comp . data [ view ] ) <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT>' ) , VIEWS ) <EOL> def test_view_derived ( view ) : <EOL> comp = Component ( np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ] ) ) <EOL> d = core . Data ( ) <EOL> cid = d . add_component ( comp , '<STR_LIT>' ) <EOL> cid2 = ComponentID ( "<STR_LIT>" ) <EOL> link = core . ComponentLink ( [ cid ] , cid2 , using = lambda x : x * <NUM_LIT:3> ) <EOL> dc = DerivedComponent ( d , link ) <EOL> np . testing . assert_array_equal ( dc [ view ] , comp . data [ view ] * <NUM_LIT:3> ) <EOL> @ requires_astropy <EOL> def test_units ( ) : <EOL> from astropy import units as u <EOL> comp = Component ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , units = '<STR_LIT:m>' ) <EOL> assert comp . units == '<STR_LIT:m>' <EOL> assert isinstance ( comp . units , six . string_types ) <EOL> comp = Component ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , units = u . m ) <EOL> assert comp . units == '<STR_LIT:m>' <EOL> assert isinstance ( comp . units , six . string_types ) </s>
