<s> from __future__ import absolute_import , division , print_function <EOL> import pytest <EOL> import numpy as np <EOL> from numpy . testing import assert_array_equal <EOL> from . . component_link import ComponentLink , BinaryComponentLink <EOL> from . . data import ComponentID , Data , Component <EOL> from . . data_collection import DataCollection <EOL> from . . link_helpers import LinkSame <EOL> from . . subset import InequalitySubsetState <EOL> class TestComponentLink ( object ) : <EOL> def toy_data ( self ) : <EOL> data = Data ( ) <EOL> from_comp = Component ( np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ) <EOL> to_comp = Component ( np . array ( [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ) <EOL> return data , from_comp , to_comp <EOL> def test_valid_init ( self ) : <EOL> ComponentLink ( [ ComponentID ( '<STR_LIT>' ) ] , ComponentID ( '<STR_LIT:to>' ) ) <EOL> def test_valid_init_using ( self ) : <EOL> data , from_ , to_ = self . toy_data ( ) <EOL> using = lambda x : x <EOL> ComponentLink ( [ ComponentID ( '<STR_LIT>' ) ] , ComponentID ( '<STR_LIT:to>' ) , using ) <EOL> def test_invalid_init_multi_from_no_using ( self ) : <EOL> with pytest . raises ( TypeError ) as exc : <EOL> ComponentLink ( [ ComponentID ( '<STR_LIT:a>' ) , ComponentID ( '<STR_LIT:b>' ) ] , <EOL> ComponentID ( '<STR_LIT:c>' ) ) <EOL> assert exc . value . args [ <NUM_LIT:0> ] == ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_invalid_init_scalar_from ( self ) : <EOL> with pytest . raises ( TypeError ) as exc : <EOL> ComponentLink ( ComponentID ( '<STR_LIT>' ) , ComponentID ( '<STR_LIT:to>' ) ) <EOL> assert exc . value . args [ <NUM_LIT:0> ] . startswith ( "<STR_LIT>" ) <EOL> def test_compute_direct ( self ) : <EOL> data , from_ , to_ = self . toy_data ( ) <EOL> from_id = data . add_component ( from_ , '<STR_LIT>' ) <EOL> to_id = ComponentID ( '<STR_LIT>' ) <EOL> link = ComponentLink ( [ from_id ] , to_id ) <EOL> result = link . compute ( data ) <EOL> expected = from_ . data <EOL> assert_array_equal ( result , expected ) <EOL> def test_compute_using ( self ) : <EOL> data , from_ , to_ = self . toy_data ( ) <EOL> from_id = data . add_component ( from_ , '<STR_LIT>' ) <EOL> to_id = ComponentID ( '<STR_LIT>' ) <EOL> using = lambda x : <NUM_LIT:3> * x <EOL> link = ComponentLink ( [ from_id ] , to_id , using ) <EOL> result = link . compute ( data ) <EOL> expected = from_ . data * <NUM_LIT:3> <EOL> assert_array_equal ( result , expected ) <EOL> def test_getters ( self ) : <EOL> data , from_ , to_ = self . toy_data ( ) <EOL> from_id = data . add_component ( from_ , '<STR_LIT>' ) <EOL> to_id = ComponentID ( '<STR_LIT>' ) <EOL> using = lambda x : <NUM_LIT:3> * x <EOL> link = ComponentLink ( [ from_id ] , to_id , using ) <EOL> assert link . get_from_ids ( ) [ <NUM_LIT:0> ] is from_id <EOL> assert link . get_to_id ( ) is to_id <EOL> assert link . get_using ( ) is using <EOL> def test_str ( self ) : <EOL> """<STR_LIT>""" <EOL> data , from_ , to_ = self . toy_data ( ) <EOL> from_id = data . add_component ( from_ , '<STR_LIT>' ) <EOL> to_id = ComponentID ( '<STR_LIT>' ) <EOL> link = ComponentLink ( [ from_id ] , to_id ) <EOL> str ( link ) <EOL> link = ComponentLink ( [ from_id ] , to_id , using = lambda x : <NUM_LIT:3> * x ) <EOL> str ( link ) <EOL> def test_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> data , from_ , to_ = self . toy_data ( ) <EOL> from_id = data . add_component ( from_ , '<STR_LIT>' ) <EOL> to_id = ComponentID ( '<STR_LIT>' ) <EOL> link = ComponentLink ( [ from_id ] , to_id ) <EOL> repr ( link ) <EOL> def test_type_check ( self ) : <EOL> """<STR_LIT>""" <EOL> cid = ComponentID ( '<STR_LIT:test>' ) <EOL> with pytest . raises ( TypeError ) as exc : <EOL> ComponentLink ( [ None ] , cid ) <EOL> assert exc . value . args [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> with pytest . raises ( TypeError ) as exc : <EOL> ComponentLink ( [ cid ] , None ) <EOL> assert exc . value . args [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) <EOL> with pytest . raises ( TypeError ) as exc : <EOL> ComponentLink ( [ cid , None ] , None , using = lambda x , y : None ) <EOL> assert exc . value . args [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> l = ComponentLink ( [ ComponentID ( '<STR_LIT:a>' ) ] , ComponentID ( '<STR_LIT:b>' ) ) <EOL> cid = ComponentID ( '<STR_LIT:a>' ) <EOL> scalar = <NUM_LIT:3> <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , [ ( l , l ) , ( l , cid ) , ( l , scalar ) ] ) <EOL> def test_arithmetic_overload ( a , b ) : <EOL> for x in [ a + b , a - b , a * b , a / b , a ** b ] : <EOL> assert isinstance ( x , BinaryComponentLink ) <EOL> for x in [ b + a , b - a , b * a , b / a , b ** a ] : <EOL> assert isinstance ( x , BinaryComponentLink ) <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , [ ( l , l ) , ( l , cid ) , ( l , scalar ) ] ) <EOL> def test_inequality_overload ( a , b ) : <EOL> for x in [ a < b , a <= b , a > b , a >= b ] : <EOL> assert isinstance ( x , InequalitySubsetState ) <EOL> for x in [ b < a , b <= a , b > a , b >= a ] : <EOL> assert isinstance ( x , InequalitySubsetState ) <EOL> def test_link_bad_input ( ) : <EOL> with pytest . raises ( TypeError ) as exc : <EOL> BinaryComponentLink ( ComponentID ( '<STR_LIT:x>' ) , None , None ) <EOL> assert exc . value . args [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> with pytest . raises ( TypeError ) as exc : <EOL> BinaryComponentLink ( None , ComponentID ( '<STR_LIT:x>' ) , None ) <EOL> assert exc . value . args [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> def test_arithmetic_id_scalar ( ) : <EOL> d = Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , y = [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:10> , <NUM_LIT:20> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] + <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] - <NUM_LIT:3> ] , [ - <NUM_LIT:2> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] * <NUM_LIT:3> ] , [ <NUM_LIT:3> , <NUM_LIT:6> , <NUM_LIT:9> , <NUM_LIT:12> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:y>' ] / <NUM_LIT:10> ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] ** <NUM_LIT:2> ] , [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:9> , <NUM_LIT:16> ] ) <EOL> assert_array_equal ( d [ <NUM_LIT:3> + d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ] ) <EOL> assert_array_equal ( d [ <NUM_LIT:3> - d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ] ) <EOL> assert_array_equal ( d [ <NUM_LIT:3> * d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:3> , <NUM_LIT:6> , <NUM_LIT:9> , <NUM_LIT:12> ] ) <EOL> assert_array_equal ( d [ <NUM_LIT> / d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:8> , <NUM_LIT:6> ] ) <EOL> assert_array_equal ( d [ <NUM_LIT:2> ** d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:8> , <NUM_LIT:16> ] ) <EOL> def test_arithmetic_id_id ( ) : <EOL> d = Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , y = [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:10> , <NUM_LIT:20> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] + d . id [ '<STR_LIT:y>' ] ] , [ <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] - d . id [ '<STR_LIT:y>' ] ] , [ - <NUM_LIT:9> , - <NUM_LIT> , - <NUM_LIT:7> , - <NUM_LIT:16> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] * d . id [ '<STR_LIT:y>' ] ] , [ <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:30> , <NUM_LIT> ] ) <EOL> assert_array_equal ( <EOL> d [ d . id [ '<STR_LIT:y>' ] / d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> / <NUM_LIT:3> , <NUM_LIT:5> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:y>' ] ** d . id [ '<STR_LIT:x>' ] ] , <EOL> [ <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:1000> , <NUM_LIT:20> ** <NUM_LIT:4> ] ) <EOL> def test_arithmetic_id_link ( ) : <EOL> d = Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , y = [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:10> , <NUM_LIT:20> ] ) <EOL> y10 = d . id [ '<STR_LIT:y>' ] / <NUM_LIT:10> <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] + y10 ] , [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:6> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] - y10 ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] * y10 ] , [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:8> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] / y10 ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:2> ] ) <EOL> assert_array_equal ( d [ d . id [ '<STR_LIT:x>' ] ** y10 ] , [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:16> ] ) <EOL> assert_array_equal ( d [ y10 + d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:6> ] ) <EOL> assert_array_equal ( d [ y10 - d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:2> , - <NUM_LIT:2> ] ) <EOL> assert_array_equal ( d [ y10 * d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:8> ] ) <EOL> assert_array_equal ( d [ y10 / d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> / <NUM_LIT> , <NUM_LIT:1> / <NUM_LIT> ] ) <EOL> assert_array_equal ( d [ y10 ** d . id [ '<STR_LIT:x>' ] ] , [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:16> ] ) <EOL> def test_arithmetic_link_link ( ) : <EOL> d = Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , y = [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:10> , <NUM_LIT:20> ] ) <EOL> x = d [ d . id [ '<STR_LIT:x>' ] ] <EOL> y = d [ d . id [ '<STR_LIT:y>' ] ] <EOL> xpy = d . id [ '<STR_LIT:x>' ] + d . id [ '<STR_LIT:y>' ] <EOL> xt3 = d . id [ '<STR_LIT:x>' ] * <NUM_LIT:3> <EOL> assert_array_equal ( d [ xpy + xt3 ] , x + y + x * <NUM_LIT:3> ) <EOL> assert_array_equal ( d [ xpy - xt3 ] , x + y - x * <NUM_LIT:3> ) <EOL> assert_array_equal ( d [ xpy * xt3 ] , ( x + y ) * x * <NUM_LIT:3> ) <EOL> assert_array_equal ( d [ xpy / xt3 ] , ( x + y ) / ( x * <NUM_LIT:3> ) ) <EOL> assert_array_equal ( d [ xpy ** xt3 ] , ( x + y ) ** ( x * <NUM_LIT:3> ) ) <EOL> def test_inequality ( ) : <EOL> d = Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , y = [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:10> , <NUM_LIT:20> ] ) <EOL> s = d . new_subset ( ) <EOL> xpy = d . id [ '<STR_LIT:x>' ] + d . id [ '<STR_LIT:y>' ] <EOL> twentytwo = xpy * <NUM_LIT:0> + <NUM_LIT> <EOL> x = d [ d . id [ '<STR_LIT:x>' ] ] <EOL> y = d [ d . id [ '<STR_LIT:y>' ] ] <EOL> s . subset_state = xpy < <NUM_LIT> <EOL> assert_array_equal ( s . to_mask ( ) , ( x + y ) < <NUM_LIT> ) <EOL> s . subset_state = xpy <= <NUM_LIT> <EOL> assert_array_equal ( s . to_mask ( ) , ( x + y ) <= <NUM_LIT> ) <EOL> s . subset_state = xpy >= <NUM_LIT> <EOL> assert_array_equal ( s . to_mask ( ) , ( x + y ) >= <NUM_LIT> ) <EOL> s . subset_state = xpy > <NUM_LIT> <EOL> assert_array_equal ( s . to_mask ( ) , ( x + y ) > <NUM_LIT> ) <EOL> s . subset_state = <NUM_LIT> < xpy <EOL> assert_array_equal ( s . to_mask ( ) , <NUM_LIT> < ( x + y ) ) <EOL> s . subset_state = <NUM_LIT> <= xpy <EOL> assert_array_equal ( s . to_mask ( ) , <NUM_LIT> <= ( x + y ) ) <EOL> s . subset_state = <NUM_LIT> > xpy <EOL> assert_array_equal ( s . to_mask ( ) , <NUM_LIT> > ( x + y ) ) <EOL> s . subset_state = <NUM_LIT> >= xpy <EOL> assert_array_equal ( s . to_mask ( ) , <NUM_LIT> >= ( x + y ) ) <EOL> s . subset_state = twentytwo < xpy <EOL> assert_array_equal ( s . to_mask ( ) , <NUM_LIT> < ( x + y ) ) <EOL> s . subset_state = twentytwo <= xpy <EOL> assert_array_equal ( s . to_mask ( ) , <NUM_LIT> <= ( x + y ) ) <EOL> s . subset_state = twentytwo > xpy <EOL> assert_array_equal ( s . to_mask ( ) , <NUM_LIT> > ( x + y ) ) <EOL> s . subset_state = twentytwo >= xpy <EOL> assert_array_equal ( s . to_mask ( ) , <NUM_LIT> >= ( x + y ) ) <EOL> def test_link_fixes_shape ( ) : <EOL> def double ( x ) : <EOL> return ( x * <NUM_LIT:2> ) . reshape ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> d = Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> y = ComponentID ( '<STR_LIT:y>' ) <EOL> link = ComponentLink ( [ d . id [ '<STR_LIT:x>' ] ] , y , using = double ) <EOL> assert_array_equal ( d [ link ] , [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> ] ) <EOL> def test_link_str ( ) : <EOL> """<STR_LIT>""" <EOL> d = Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , y = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> x = d . id [ '<STR_LIT:x>' ] <EOL> y = d . id [ '<STR_LIT:y>' ] <EOL> assert str ( x + y ) == ( '<STR_LIT>' ) <EOL> assert str ( x - y ) == ( '<STR_LIT>' ) <EOL> assert str ( x * y ) == ( '<STR_LIT>' ) <EOL> assert str ( x / y ) == ( '<STR_LIT>' ) <EOL> assert str ( x ** y ) == ( '<STR_LIT>' ) <EOL> assert str ( x ** <NUM_LIT:3> ) == ( '<STR_LIT>' ) <EOL> assert str ( <NUM_LIT:3> + x * y ) == ( '<STR_LIT>' ) <EOL> assert str ( x + x + y ) == ( '<STR_LIT>' ) <EOL> assert repr ( x + y ) == '<STR_LIT>' <EOL> def test_duplicated_links_remove_first_input ( ) : <EOL> """<STR_LIT>""" <EOL> d1 = Data ( x = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> d2 = Data ( y = [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> ] ) <EOL> x = d1 . id [ '<STR_LIT:x>' ] <EOL> y = d2 . id [ '<STR_LIT:y>' ] <EOL> dc = DataCollection ( [ d1 , d2 ] ) <EOL> dc . add_link ( LinkSame ( x , y ) ) <EOL> assert y not in d2 . components <EOL> assert y not in d1 . components <EOL> assert x in d2 . components <EOL> assert x in d2 . components </s>
