<s> from __future__ import absolute_import , division , print_function <EOL> import pytest <EOL> import itertools <EOL> import numpy as np <EOL> import pandas as pd <EOL> from datetime import datetime , date <EOL> from blaze . compute . core import compute , compute_up <EOL> from blaze . expr import symbol , by , exp , summary , Broadcast , join , concat <EOL> from blaze . expr import greatest , least , coalesce <EOL> from blaze import sin <EOL> import blaze <EOL> from odo import into <EOL> from datashape import discover , to_numpy , dshape <EOL> x = np . array ( [ ( <NUM_LIT:1> , '<STR_LIT>' , <NUM_LIT:100> ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , - <NUM_LIT:200> ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' , - <NUM_LIT> ) ] , <EOL> dtype = [ ( '<STR_LIT:id>' , '<STR_LIT>' ) , ( '<STR_LIT:name>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> t = symbol ( '<STR_LIT:t>' , discover ( x ) ) <EOL> def eq ( a , b ) : <EOL> c = a == b <EOL> if isinstance ( c , np . ndarray ) : <EOL> return c . all ( ) <EOL> return c <EOL> def test_symbol ( ) : <EOL> assert eq ( compute ( t , x ) , x ) <EOL> def test_eq ( ) : <EOL> assert eq ( compute ( t [ '<STR_LIT>' ] == <NUM_LIT:100> , x ) , <EOL> x [ '<STR_LIT>' ] == <NUM_LIT:100> ) <EOL> def test_selection ( ) : <EOL> assert eq ( compute ( t [ t [ '<STR_LIT>' ] == <NUM_LIT:100> ] , x ) , x [ x [ '<STR_LIT>' ] == <NUM_LIT:0> ] ) <EOL> assert eq ( compute ( t [ t [ '<STR_LIT>' ] < <NUM_LIT:0> ] , x ) , x [ x [ '<STR_LIT>' ] < <NUM_LIT:0> ] ) <EOL> def test_arithmetic ( ) : <EOL> assert eq ( compute ( t [ '<STR_LIT>' ] + t [ '<STR_LIT:id>' ] , x ) , <EOL> x [ '<STR_LIT>' ] + x [ '<STR_LIT:id>' ] ) <EOL> assert eq ( compute ( t [ '<STR_LIT>' ] * t [ '<STR_LIT:id>' ] , x ) , <EOL> x [ '<STR_LIT>' ] * x [ '<STR_LIT:id>' ] ) <EOL> assert eq ( compute ( t [ '<STR_LIT>' ] % t [ '<STR_LIT:id>' ] , x ) , <EOL> x [ '<STR_LIT>' ] % x [ '<STR_LIT:id>' ] ) <EOL> def test_UnaryOp ( ) : <EOL> assert eq ( compute ( exp ( t [ '<STR_LIT>' ] ) , x ) , <EOL> np . exp ( x [ '<STR_LIT>' ] ) ) <EOL> assert eq ( compute ( abs ( - t [ '<STR_LIT>' ] ) , x ) , <EOL> abs ( - x [ '<STR_LIT>' ] ) ) <EOL> def test_Neg ( ) : <EOL> assert eq ( compute ( - t [ '<STR_LIT>' ] , x ) , <EOL> - x [ '<STR_LIT>' ] ) <EOL> def test_invert_not ( ) : <EOL> assert eq ( compute ( ~ ( t . amount > <NUM_LIT:0> ) , x ) , <EOL> ~ ( x [ '<STR_LIT>' ] > <NUM_LIT:0> ) ) <EOL> def test_Reductions ( ) : <EOL> assert compute ( t [ '<STR_LIT>' ] . mean ( ) , x ) == x [ '<STR_LIT>' ] . mean ( ) <EOL> assert compute ( t [ '<STR_LIT>' ] . count ( ) , x ) == len ( x [ '<STR_LIT>' ] ) <EOL> assert compute ( t [ '<STR_LIT>' ] . sum ( ) , x ) == x [ '<STR_LIT>' ] . sum ( ) <EOL> assert compute ( t [ '<STR_LIT>' ] . min ( ) , x ) == x [ '<STR_LIT>' ] . min ( ) <EOL> assert compute ( t [ '<STR_LIT>' ] . max ( ) , x ) == x [ '<STR_LIT>' ] . max ( ) <EOL> assert compute ( t [ '<STR_LIT>' ] . nunique ( ) , x ) == len ( np . unique ( x [ '<STR_LIT>' ] ) ) <EOL> assert compute ( t [ '<STR_LIT>' ] . var ( ) , x ) == x [ '<STR_LIT>' ] . var ( ) <EOL> assert compute ( t [ '<STR_LIT>' ] . std ( ) , x ) == x [ '<STR_LIT>' ] . std ( ) <EOL> assert compute ( t [ '<STR_LIT>' ] . var ( unbiased = True ) , x ) == x [ '<STR_LIT>' ] . var ( ddof = <NUM_LIT:1> ) <EOL> assert compute ( t [ '<STR_LIT>' ] . std ( unbiased = True ) , x ) == x [ '<STR_LIT>' ] . std ( ddof = <NUM_LIT:1> ) <EOL> assert compute ( ( t [ '<STR_LIT>' ] > <NUM_LIT> ) . any ( ) , x ) == True <EOL> assert compute ( ( t [ '<STR_LIT>' ] > <NUM_LIT> ) . all ( ) , x ) == False <EOL> assert compute ( t [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , x ) == x [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> assert compute ( t [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] , x ) == x [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] <EOL> def test_count_string ( ) : <EOL> s = symbol ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> x = np . array ( [ '<STR_LIT>' , np . nan , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , dtype = '<STR_LIT:object>' ) <EOL> assert compute ( s . count ( ) , x ) == <NUM_LIT:4> <EOL> def test_reductions_on_recarray ( ) : <EOL> assert compute ( t . count ( ) , x ) == len ( x ) <EOL> def test_count_nan ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> x = np . array ( [ <NUM_LIT:1.0> , np . nan , <NUM_LIT> ] ) <EOL> assert compute ( t . count ( ) , x ) == <NUM_LIT:2> <EOL> def test_distinct ( ) : <EOL> x = np . array ( [ ( '<STR_LIT>' , <NUM_LIT:100> ) , <EOL> ( '<STR_LIT>' , - <NUM_LIT:200> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:100> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:100> ) ] , <EOL> dtype = [ ( '<STR_LIT:name>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert eq ( compute ( t [ '<STR_LIT:name>' ] . distinct ( ) , x ) , <EOL> np . unique ( x [ '<STR_LIT:name>' ] ) ) <EOL> assert eq ( compute ( t . distinct ( ) , x ) , <EOL> np . unique ( x ) ) <EOL> def test_distinct_on_recarray ( ) : <EOL> rec = pd . DataFrame ( <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> ] ] , <EOL> columns = ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , <EOL> ) . to_records ( index = False ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( rec ) ) <EOL> assert ( <EOL> compute ( s . distinct ( '<STR_LIT:a>' ) , rec ) == <EOL> pd . DataFrame ( <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> ] ] , <EOL> columns = ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , <EOL> ) . to_records ( index = False ) <EOL> ) . all ( ) <EOL> def test_distinct_on_structured_array ( ) : <EOL> arr = np . array ( <EOL> [ ( <NUM_LIT:0.> , <NUM_LIT:1.> ) , <EOL> ( <NUM_LIT:0.> , <NUM_LIT> ) , <EOL> ( <NUM_LIT:1.> , <NUM_LIT:1.> ) , <EOL> ( <NUM_LIT:1.> , <NUM_LIT> ) ] , <EOL> dtype = [ ( '<STR_LIT:a>' , '<STR_LIT>' ) , ( '<STR_LIT:b>' , '<STR_LIT>' ) ] , <EOL> ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( arr ) ) <EOL> assert ( <EOL> compute ( s . distinct ( '<STR_LIT:a>' ) , arr ) == <EOL> np . array ( [ ( <NUM_LIT:0.> , <NUM_LIT:1.> ) , ( <NUM_LIT:1.> , <NUM_LIT:1.> ) ] , dtype = arr . dtype ) <EOL> ) . all ( ) <EOL> def test_distinct_on_str ( ) : <EOL> rec = pd . DataFrame ( <EOL> [ [ '<STR_LIT:a>' , '<STR_LIT:a>' ] , <EOL> [ '<STR_LIT:a>' , '<STR_LIT:b>' ] , <EOL> [ '<STR_LIT:b>' , '<STR_LIT:a>' ] , <EOL> [ '<STR_LIT:b>' , '<STR_LIT:b>' ] ] , <EOL> columns = ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , <EOL> ) . to_records ( index = False ) . astype ( [ ( '<STR_LIT:a>' , '<STR_LIT>' ) , ( '<STR_LIT:b>' , '<STR_LIT>' ) ] ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( rec ) ) <EOL> assert ( <EOL> compute ( s . distinct ( '<STR_LIT:a>' ) , rec ) == <EOL> pd . DataFrame ( <EOL> [ [ '<STR_LIT:a>' , '<STR_LIT:a>' ] , <EOL> [ '<STR_LIT:b>' , '<STR_LIT:a>' ] ] , <EOL> columns = ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , <EOL> ) . to_records ( index = False ) . astype ( [ ( '<STR_LIT:a>' , '<STR_LIT>' ) , ( '<STR_LIT:b>' , '<STR_LIT>' ) ] ) <EOL> ) . all ( ) <EOL> def test_sort ( ) : <EOL> assert eq ( compute ( t . sort ( '<STR_LIT>' ) , x ) , <EOL> np . sort ( x , order = '<STR_LIT>' ) ) <EOL> assert eq ( compute ( t . sort ( '<STR_LIT>' , ascending = False ) , x ) , <EOL> np . sort ( x , order = '<STR_LIT>' ) [ : : - <NUM_LIT:1> ] ) <EOL> assert eq ( compute ( t . sort ( [ '<STR_LIT>' , '<STR_LIT:id>' ] ) , x ) , <EOL> np . sort ( x , order = [ '<STR_LIT>' , '<STR_LIT:id>' ] ) ) <EOL> assert eq ( compute ( t . amount . sort ( ) , x ) , <EOL> np . sort ( x [ '<STR_LIT>' ] ) ) <EOL> def test_head ( ) : <EOL> assert eq ( compute ( t . head ( <NUM_LIT:2> ) , x ) , <EOL> x [ : <NUM_LIT:2> ] ) <EOL> def test_tail ( ) : <EOL> assert eq ( compute ( t . tail ( <NUM_LIT:2> ) , x ) , <EOL> x [ - <NUM_LIT:2> : ] ) <EOL> def test_label ( ) : <EOL> expected = x [ '<STR_LIT>' ] * <NUM_LIT:10> <EOL> expected = np . array ( expected , dtype = [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) ] ) <EOL> assert eq ( compute ( ( t [ '<STR_LIT>' ] * <NUM_LIT:10> ) . label ( '<STR_LIT:foo>' ) , x ) , <EOL> expected ) <EOL> def test_relabel ( ) : <EOL> expected = np . array ( x , dtype = [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> result = compute ( t . relabel ( { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:id>' : '<STR_LIT>' } ) , x ) <EOL> assert result . dtype . names == expected . dtype . names <EOL> assert eq ( result , expected ) <EOL> def test_by ( ) : <EOL> expr = by ( t . amount > <NUM_LIT:0> , count = t . id . count ( ) ) <EOL> result = compute ( expr , x ) <EOL> assert set ( map ( tuple , into ( list , result ) ) ) == set ( [ ( False , <NUM_LIT:2> ) , ( True , <NUM_LIT:3> ) ] ) <EOL> def test_compute_up_field ( ) : <EOL> assert eq ( compute ( t [ '<STR_LIT:name>' ] , x ) , x [ '<STR_LIT:name>' ] ) <EOL> def test_compute_up_projection ( ) : <EOL> assert eq ( compute_up ( t [ [ '<STR_LIT:name>' , '<STR_LIT>' ] ] , x ) , x [ [ '<STR_LIT:name>' , '<STR_LIT>' ] ] ) <EOL> ax = np . arange ( <NUM_LIT:30> , dtype = '<STR_LIT>' ) . reshape ( ( <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> a = symbol ( '<STR_LIT:a>' , discover ( ax ) ) <EOL> def test_slice ( ) : <EOL> inds = [ <NUM_LIT:0> , slice ( <NUM_LIT:2> ) , slice ( <NUM_LIT:1> , <NUM_LIT:3> ) , slice ( None , None , <NUM_LIT:2> ) , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:0> , slice ( <NUM_LIT:1> , <NUM_LIT:3> ) ) , ( slice ( <NUM_LIT:0> , <NUM_LIT:3> ) , slice ( <NUM_LIT:3> , <NUM_LIT:1> , - <NUM_LIT:1> ) ) , <EOL> ( <NUM_LIT:0> , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ] <EOL> for s in inds : <EOL> assert ( compute ( a [ s ] , ax ) == ax [ s ] ) . all ( ) <EOL> def test_array_reductions ( ) : <EOL> for axis in [ None , <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:1> ) ] : <EOL> assert eq ( compute ( a . sum ( axis = axis ) , ax ) , ax . sum ( axis = axis ) ) <EOL> assert eq ( compute ( a . std ( axis = axis ) , ax ) , ax . std ( axis = axis ) ) <EOL> def test_array_reductions_with_keepdims ( ) : <EOL> for axis in [ None , <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:1> ) ] : <EOL> assert eq ( compute ( a . sum ( axis = axis , keepdims = True ) , ax ) , <EOL> ax . sum ( axis = axis , keepdims = True ) ) <EOL> def test_summary_on_ndarray ( ) : <EOL> assert compute ( summary ( total = a . sum ( ) , min = a . min ( ) ) , ax ) == ( ax . min ( ) , ax . sum ( ) ) <EOL> result = compute ( summary ( total = a . sum ( ) , min = a . min ( ) , keepdims = True ) , ax ) <EOL> expected = np . array ( [ ( ax . min ( ) , ax . sum ( ) ) ] , <EOL> dtype = [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> assert result . ndim == ax . ndim <EOL> assert eq ( expected , result ) <EOL> def test_summary_on_ndarray_with_axis ( ) : <EOL> for axis in [ <NUM_LIT:0> , <NUM_LIT:1> , ( <NUM_LIT:1> , <NUM_LIT:0> ) ] : <EOL> expr = summary ( total = a . sum ( ) , min = a . min ( ) , axis = axis ) <EOL> result = compute ( expr , ax ) <EOL> shape , dtype = to_numpy ( expr . dshape ) <EOL> expected = np . empty ( shape = shape , dtype = dtype ) <EOL> expected [ '<STR_LIT>' ] = ax . sum ( axis = axis ) <EOL> expected [ '<STR_LIT>' ] = ax . min ( axis = axis ) <EOL> assert eq ( result , expected ) <EOL> def test_utcfromtimestamp ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> data = np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> expected = np . array ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> dtype = '<STR_LIT>' ) <EOL> assert eq ( compute ( t . utcfromtimestamp , data ) , expected ) <EOL> def test_nelements_structured_array ( ) : <EOL> assert compute ( t . nelements ( ) , x ) == len ( x ) <EOL> assert compute ( t . nelements ( keepdims = True ) , x ) == ( len ( x ) , ) <EOL> def test_nelements_array ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> x = np . random . randn ( * t . shape ) <EOL> result = compute ( t . nelements ( axis = ( <NUM_LIT:0> , <NUM_LIT:1> ) ) , x ) <EOL> np . testing . assert_array_equal ( result , np . array ( [ <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT:20> ] ) ) <EOL> result = compute ( t . nelements ( axis = <NUM_LIT:1> ) , x ) <EOL> np . testing . assert_array_equal ( result , <NUM_LIT:4> * np . ones ( ( <NUM_LIT:5> , <NUM_LIT:3> ) ) ) <EOL> def test_nrows ( ) : <EOL> assert compute ( t . nrows , x ) == len ( x ) <EOL> dts = np . array ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> dtype = '<STR_LIT>' ) <EOL> s = symbol ( '<STR_LIT:s>' , '<STR_LIT>' ) <EOL> def test_datetime_truncation ( ) : <EOL> assert eq ( compute ( s . truncate ( <NUM_LIT:1> , '<STR_LIT>' ) , dts ) , <EOL> dts . astype ( '<STR_LIT>' ) ) <EOL> assert eq ( compute ( s . truncate ( <NUM_LIT:2> , '<STR_LIT>' ) , dts ) , <EOL> np . array ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> dtype = '<STR_LIT>' ) ) <EOL> assert eq ( compute ( s . truncate ( <NUM_LIT:2> , '<STR_LIT>' ) , dts ) , <EOL> np . array ( [ '<STR_LIT>' , '<STR_LIT>' ] , dtype = '<STR_LIT>' ) ) <EOL> assert into ( list , compute ( s . truncate ( <NUM_LIT:1> , '<STR_LIT>' ) , dts ) ) [ <NUM_LIT:0> ] . isoweekday ( ) == <NUM_LIT:7> <EOL> def test_hour ( ) : <EOL> dts = [ datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:20> , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:20> , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:20> , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:20> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT> ) ] <EOL> dts = into ( np . ndarray , dts ) <EOL> assert eq ( compute ( s . truncate ( <NUM_LIT:1> , '<STR_LIT>' ) , dts ) , <EOL> into ( np . ndarray , [ datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:20> , <NUM_LIT:1> , <NUM_LIT:0> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:20> , <NUM_LIT:12> , <NUM_LIT:0> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:20> , <NUM_LIT:12> , <NUM_LIT:0> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:20> , <NUM_LIT:11> , <NUM_LIT:0> ) ] ) ) <EOL> def test_month ( ) : <EOL> dts = [ datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:1> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:30> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:1> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT> ) ] <EOL> dts = into ( np . ndarray , dts ) <EOL> assert eq ( compute ( s . truncate ( <NUM_LIT:1> , '<STR_LIT>' ) , dts ) , <EOL> into ( np . ndarray , [ date ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:1> ) , <EOL> date ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:1> ) , <EOL> date ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:1> ) , <EOL> date ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:1> ) ] ) ) <EOL> def test_truncate_on_np_datetime64_scalar ( ) : <EOL> s = symbol ( '<STR_LIT:s>' , '<STR_LIT>' ) <EOL> data = np . datetime64 ( '<STR_LIT>' ) <EOL> assert compute ( s . truncate ( <NUM_LIT:1> , '<STR_LIT>' ) , data ) == data . astype ( '<STR_LIT>' ) <EOL> def test_numpy_and_python_datetime_truncate_agree_on_start_of_week ( ) : <EOL> s = symbol ( '<STR_LIT:s>' , '<STR_LIT>' ) <EOL> n = np . datetime64 ( '<STR_LIT>' ) <EOL> p = datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:11> ) <EOL> expr = s . truncate ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> assert compute ( expr , n ) == compute ( expr , p ) <EOL> def test_add_multiple_ndarrays ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> x = np . arange ( <NUM_LIT:9> , dtype = '<STR_LIT>' ) . reshape ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> y = ( x + <NUM_LIT:1> ) . astype ( '<STR_LIT>' ) <EOL> expr = sin ( a ) + <NUM_LIT:2> * b <EOL> scope = { a : x , b : y } <EOL> expected = sin ( x ) + <NUM_LIT:2> * y <EOL> assert expr . dshape == dshape ( '<STR_LIT>' ) <EOL> np . testing . assert_array_equal ( compute ( expr , scope ) , expected ) <EOL> np . testing . assert_array_equal ( compute ( expr , scope , optimize = False ) , <EOL> expected ) <EOL> nA = np . arange ( <NUM_LIT:30> , dtype = '<STR_LIT>' ) . reshape ( ( <NUM_LIT:5> , <NUM_LIT:6> ) ) <EOL> ny = np . arange ( <NUM_LIT:6> , dtype = '<STR_LIT>' ) <EOL> A = symbol ( '<STR_LIT:A>' , discover ( nA ) ) <EOL> y = symbol ( '<STR_LIT:y>' , discover ( ny ) ) <EOL> def test_transpose ( ) : <EOL> assert eq ( compute ( A . T , nA ) , nA . T ) <EOL> assert eq ( compute ( A . transpose ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) , nA ) , nA ) <EOL> def test_dot ( ) : <EOL> assert eq ( compute ( y . dot ( y ) , { y : ny } ) , np . dot ( ny , ny ) ) <EOL> assert eq ( compute ( A . dot ( y ) , { A : nA , y : ny } ) , np . dot ( nA , ny ) ) <EOL> def test_subexpr_datetime ( ) : <EOL> data = pd . date_range ( start = '<STR_LIT>' , end = '<STR_LIT>' , freq = '<STR_LIT:D>' ) . values <EOL> s = symbol ( '<STR_LIT:s>' , discover ( data ) ) <EOL> result = compute ( s . truncate ( days = <NUM_LIT:2> ) . day , data ) <EOL> expected = np . array ( [ <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:4> ] ) <EOL> np . testing . assert_array_equal ( result , expected ) <EOL> def test_mixed_types ( ) : <EOL> x = np . array ( [ [ ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) , ( <NUM_LIT:4> , <NUM_LIT> ) ] ] , <EOL> dtype = [ ( '<STR_LIT:count>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> aggregate = symbol ( '<STR_LIT>' , discover ( x ) ) <EOL> result = compute ( aggregate . total . sum ( axis = ( <NUM_LIT:0> , ) ) / <EOL> aggregate [ '<STR_LIT:count>' ] . sum ( axis = ( <NUM_LIT:0> , ) ) , x ) <EOL> expected = ( x [ '<STR_LIT>' ] . sum ( axis = <NUM_LIT:0> , keepdims = True ) / <EOL> x [ '<STR_LIT:count>' ] . sum ( axis = <NUM_LIT:0> , keepdims = True ) ) . squeeze ( ) <EOL> np . testing . assert_array_equal ( result , expected ) <EOL> def test_broadcast_compute_against_numbers_and_arrays ( ) : <EOL> A = symbol ( '<STR_LIT:A>' , '<STR_LIT>' ) <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> x = np . arange ( <NUM_LIT:5> , dtype = '<STR_LIT>' ) <EOL> expr = Broadcast ( ( A , b ) , ( a , b ) , a + b ) <EOL> result = compute ( expr , { A : x , b : <NUM_LIT:10> } ) <EOL> assert eq ( result , x + <NUM_LIT:10> ) <EOL> def test_map ( ) : <EOL> pytest . importorskip ( '<STR_LIT>' ) <EOL> a = np . arange ( <NUM_LIT> ) <EOL> f = lambda x : np . sin ( x ) + <NUM_LIT> * np . cos ( x ) ** <NUM_LIT:2> <EOL> x = symbol ( '<STR_LIT:x>' , discover ( a ) ) <EOL> expr = x . map ( f , '<STR_LIT>' ) <EOL> result = compute ( expr , a ) <EOL> expected = f ( a ) <EOL> assert type ( result ) == np . ndarray <EOL> assert type ( result ) == type ( expected ) <EOL> np . testing . assert_array_equal ( result , expected ) <EOL> def test_vector_norm ( ) : <EOL> x = np . arange ( <NUM_LIT:30> ) . reshape ( ( <NUM_LIT:5> , <NUM_LIT:6> ) ) <EOL> s = symbol ( '<STR_LIT:x>' , discover ( x ) ) <EOL> assert eq ( compute ( s . vnorm ( ) , x ) , <EOL> np . linalg . norm ( x ) ) <EOL> assert eq ( compute ( s . vnorm ( ord = <NUM_LIT:1> ) , x ) , <EOL> np . linalg . norm ( x . flatten ( ) , ord = <NUM_LIT:1> ) ) <EOL> assert eq ( compute ( s . vnorm ( ord = <NUM_LIT:4> , axis = <NUM_LIT:0> ) , x ) , <EOL> np . linalg . norm ( x , ord = <NUM_LIT:4> , axis = <NUM_LIT:0> ) ) <EOL> expr = s . vnorm ( ord = <NUM_LIT:4> , axis = <NUM_LIT:0> , keepdims = True ) <EOL> assert expr . shape == compute ( expr , x ) . shape <EOL> def test_join ( ) : <EOL> cities = np . array ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> dtype = [ ( '<STR_LIT:name>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT:O>' ) ] ) <EOL> c = symbol ( '<STR_LIT>' , discover ( cities ) ) <EOL> expr = join ( t , c , '<STR_LIT:name>' ) <EOL> result = compute ( expr , { t : x , c : cities } ) <EOL> assert ( b'<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:100> , '<STR_LIT>' ) in into ( list , result ) <EOL> def test_query_with_strings ( ) : <EOL> b = np . array ( [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) , ( '<STR_LIT:c>' , <NUM_LIT:3> ) ] , <EOL> dtype = [ ( '<STR_LIT:x>' , '<STR_LIT>' ) , ( '<STR_LIT:y>' , '<STR_LIT>' ) ] ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( b ) ) <EOL> assert compute ( s [ s . x == b'<STR_LIT:b>' ] , b ) . tolist ( ) == [ ( b'<STR_LIT:b>' , <NUM_LIT:2> ) ] <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ [ '<STR_LIT:a>' ] , list ( '<STR_LIT>' ) ] ) <EOL> def test_isin ( keys ) : <EOL> b = np . array ( [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) , ( '<STR_LIT:c>' , <NUM_LIT:3> ) , ( '<STR_LIT:a>' , <NUM_LIT:4> ) , ( '<STR_LIT:c>' , <NUM_LIT:5> ) , ( '<STR_LIT:b>' , <NUM_LIT:6> ) ] , <EOL> dtype = [ ( '<STR_LIT:x>' , '<STR_LIT>' ) , ( '<STR_LIT:y>' , '<STR_LIT>' ) ] ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( b ) ) <EOL> result = compute ( s . x . isin ( keys ) , b ) <EOL> expected = np . in1d ( b [ '<STR_LIT:x>' ] , keys ) <EOL> np . testing . assert_array_equal ( result , expected ) <EOL> def test_nunique_recarray ( ) : <EOL> b = np . array ( [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) , ( '<STR_LIT:c>' , <NUM_LIT:3> ) , ( '<STR_LIT:a>' , <NUM_LIT:4> ) , ( '<STR_LIT:c>' , <NUM_LIT:5> ) , ( '<STR_LIT:b>' , <NUM_LIT:6> ) , <EOL> ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) ] , <EOL> dtype = [ ( '<STR_LIT:x>' , '<STR_LIT>' ) , ( '<STR_LIT:y>' , '<STR_LIT>' ) ] ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( b ) ) <EOL> expr = s . nunique ( ) <EOL> assert compute ( expr , b ) == len ( np . unique ( b ) ) <EOL> def test_str_repeat ( ) : <EOL> a = np . array ( ( '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ) ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( a ) ) <EOL> expr = s . repeat ( <NUM_LIT:3> ) <EOL> assert all ( compute ( expr , a ) == np . char . multiply ( a , <NUM_LIT:3> ) ) <EOL> def test_str_interp ( ) : <EOL> a = np . array ( ( '<STR_LIT:%s>' , '<STR_LIT:%s>' , '<STR_LIT:%s>' ) ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( a ) ) <EOL> expr = s . interp ( <NUM_LIT:1> ) <EOL> assert all ( compute ( expr , a ) == np . char . mod ( a , <NUM_LIT:1> ) ) <EOL> def test_timedelta_arith ( ) : <EOL> dates = np . arange ( '<STR_LIT>' , '<STR_LIT>' , dtype = '<STR_LIT>' ) <EOL> delta = np . timedelta64 ( <NUM_LIT:1> , '<STR_LIT:D>' ) <EOL> sym = symbol ( '<STR_LIT:s>' , discover ( dates ) ) <EOL> assert ( compute ( sym + delta , dates ) == dates + delta ) . all ( ) <EOL> assert ( compute ( sym - delta , dates ) == dates - delta ) . all ( ) <EOL> assert ( <EOL> compute ( sym - ( sym - delta ) , dates ) == <EOL> dates - ( dates - delta ) <EOL> ) . all ( ) <EOL> def test_coerce ( ) : <EOL> x = np . arange ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( x ) ) <EOL> np . testing . assert_array_equal ( compute ( s . coerce ( '<STR_LIT>' ) , x ) , <EOL> np . arange ( <NUM_LIT:1.0> , <NUM_LIT> ) ) <EOL> def test_concat_arr ( ) : <EOL> s_data = np . arange ( <NUM_LIT:15> ) <EOL> t_data = np . arange ( <NUM_LIT:15> , <NUM_LIT:30> ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( s_data ) ) <EOL> t = symbol ( '<STR_LIT:t>' , discover ( t_data ) ) <EOL> assert ( <EOL> compute ( concat ( s , t ) , { s : s_data , t : t_data } ) == <EOL> np . arange ( <NUM_LIT:30> ) <EOL> ) . all ( ) <EOL> def test_concat_mat ( ) : <EOL> s_data = np . arange ( <NUM_LIT:15> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> t_data = np . arange ( <NUM_LIT:15> , <NUM_LIT:30> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( s_data ) ) <EOL> t = symbol ( '<STR_LIT:t>' , discover ( t_data ) ) <EOL> assert ( <EOL> compute ( concat ( s , t ) , { s : s_data , t : t_data } ) == <EOL> np . arange ( <NUM_LIT:30> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:3> ) <EOL> ) . all ( ) <EOL> assert ( <EOL> compute ( concat ( s , t , axis = <NUM_LIT:1> ) , { s : s_data , t : t_data } ) == <EOL> np . concatenate ( ( s_data , t_data ) , axis = <NUM_LIT:1> ) <EOL> ) . all ( ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_least ( dtype ) : <EOL> s_data = np . arange ( <NUM_LIT:15> , dtype = dtype ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> t_data = np . arange ( <NUM_LIT:15> , <NUM_LIT:30> , dtype = dtype ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( s_data ) ) <EOL> t = symbol ( '<STR_LIT:t>' , discover ( t_data ) ) <EOL> expr = least ( s , t ) <EOL> result = compute ( expr , { s : s_data , t : t_data } ) <EOL> expected = np . minimum ( s_data , t_data ) <EOL> assert np . all ( result == expected ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_least_mixed ( dtype ) : <EOL> s_data = np . array ( [ <NUM_LIT:2> , <NUM_LIT:1> ] , dtype = dtype ) <EOL> t_data = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , dtype = dtype ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( s_data ) ) <EOL> t = symbol ( '<STR_LIT:t>' , discover ( t_data ) ) <EOL> expr = least ( s , t ) <EOL> result = compute ( expr , { s : s_data , t : t_data } ) <EOL> expected = np . minimum ( s_data , t_data ) <EOL> assert np . all ( result == expected ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_greatest ( dtype ) : <EOL> s_data = np . arange ( <NUM_LIT:15> , dtype = dtype ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> t_data = np . arange ( <NUM_LIT:15> , <NUM_LIT:30> , dtype = dtype ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( s_data ) ) <EOL> t = symbol ( '<STR_LIT:t>' , discover ( t_data ) ) <EOL> expr = greatest ( s , t ) <EOL> result = compute ( expr , { s : s_data , t : t_data } ) <EOL> expected = np . maximum ( s_data , t_data ) <EOL> assert np . all ( result == expected ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_greatest_mixed ( dtype ) : <EOL> s_data = np . array ( [ <NUM_LIT:2> , <NUM_LIT:1> ] , dtype = dtype ) <EOL> t_data = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , dtype = dtype ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( s_data ) ) <EOL> t = symbol ( '<STR_LIT:t>' , discover ( t_data ) ) <EOL> expr = greatest ( s , t ) <EOL> result = compute ( expr , { s : s_data , t : t_data } ) <EOL> expected = np . maximum ( s_data , t_data ) <EOL> assert np . all ( result == expected ) <EOL> binary_name_map = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> @ pytest . mark . parametrize ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> itertools . product ( [ '<STR_LIT>' , '<STR_LIT>' ] , [ dict ( optimize = False ) , dict ( ) ] ) <EOL> ) <EOL> def test_binary_math ( func , kwargs ) : <EOL> s_data = np . arange ( <NUM_LIT:15> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> t_data = np . arange ( <NUM_LIT:15> , <NUM_LIT:30> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( s_data ) ) <EOL> t = symbol ( '<STR_LIT:t>' , discover ( t_data ) ) <EOL> scope = { s : s_data , t : t_data } <EOL> result = compute ( getattr ( blaze , func ) ( s , t ) , scope , ** kwargs ) <EOL> expected = getattr ( np , binary_name_map . get ( func , func ) ) ( s_data , t_data ) <EOL> np . testing . assert_equal ( result , expected ) <EOL> @ pytest . mark . parametrize ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> itertools . product ( [ '<STR_LIT>' , '<STR_LIT>' ] , [ dict ( optimize = False ) , dict ( ) ] ) <EOL> ) <EOL> def test_floating_binary_math ( func , kwargs ) : <EOL> s_data = np . arange ( <NUM_LIT:15> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> t_data = np . arange ( <NUM_LIT:15> , <NUM_LIT:30> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> s = symbol ( '<STR_LIT:s>' , discover ( s_data ) ) <EOL> t = symbol ( '<STR_LIT:t>' , discover ( t_data ) ) <EOL> scope = { s : s_data , t : t_data } <EOL> result = compute ( getattr ( blaze , func ) ( s , t ) , scope , ** kwargs ) <EOL> expected = getattr ( np , binary_name_map . get ( func , func ) ) ( s_data , t_data ) <EOL> np . testing . assert_allclose ( result , expected ) <EOL> def test_selection_inner_inputs ( ) : <EOL> s_data = np . arange ( <NUM_LIT:5> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:1> ) <EOL> t_data = np . arange ( <NUM_LIT:5> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:1> ) <EOL> s = symbol ( '<STR_LIT:s>' , '<STR_LIT>' ) <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert ( <EOL> compute ( s [ s . a == t . a ] , { s : s_data , t : t_data } ) == <EOL> s_data <EOL> ) . all ( ) <EOL> def test_coalesce ( ) : <EOL> data = np . array ( [ <NUM_LIT:0> , None , <NUM_LIT:1> , None , <NUM_LIT:2> , None ] ) <EOL> s = symbol ( '<STR_LIT:s>' , '<STR_LIT>' ) <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT:int>' ) <EOL> u = symbol ( '<STR_LIT:u>' , '<STR_LIT>' ) <EOL> v = symbol ( '<STR_LIT:v>' , '<STR_LIT>' ) <EOL> w = symbol ( '<STR_LIT:w>' , '<STR_LIT>' ) <EOL> np . testing . assert_array_equal ( <EOL> compute ( coalesce ( s , t ) , { s : data , t : - <NUM_LIT:1> } ) , <EOL> np . array ( [ <NUM_LIT:0> , - <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:2> , - <NUM_LIT:1> ] ) , <EOL> ) <EOL> np . testing . assert_array_equal ( <EOL> compute ( coalesce ( s , u ) , { s : data , u : None } ) , <EOL> np . array ( [ <NUM_LIT:0> , None , <NUM_LIT:1> , None , <NUM_LIT:2> , None ] , dtype = object ) <EOL> ) <EOL> np . testing . assert_array_equal ( <EOL> compute ( coalesce ( s , v ) , { <EOL> s : data , v : np . array ( [ - <NUM_LIT:1> , - <NUM_LIT:2> , - <NUM_LIT:3> , - <NUM_LIT:4> , - <NUM_LIT:5> , - <NUM_LIT:6> ] ) , <EOL> } ) , <EOL> np . array ( [ <NUM_LIT:0> , - <NUM_LIT:2> , <NUM_LIT:1> , - <NUM_LIT:4> , <NUM_LIT:2> , - <NUM_LIT:6> ] ) <EOL> ) <EOL> np . testing . assert_array_equal ( <EOL> compute ( coalesce ( s , w ) , { <EOL> s : data , w : np . array ( [ - <NUM_LIT:1> , None , - <NUM_LIT:3> , - <NUM_LIT:4> , - <NUM_LIT:5> , - <NUM_LIT:6> ] ) , <EOL> } ) , <EOL> np . array ( [ <NUM_LIT:0> , None , <NUM_LIT:1> , - <NUM_LIT:4> , <NUM_LIT:2> , - <NUM_LIT:6> ] ) , <EOL> ) </s>
