<s> from __future__ import print_function <EOL> import six <EOL> import numpy as np <EOL> from nose . tools import assert_raises <EOL> from patsy import PatsyError <EOL> from patsy . util import ( atleast_2d_column_default , <EOL> have_pandas , have_pandas_categorical ) <EOL> from patsy . desc import Term , INTERCEPT <EOL> from patsy . build import * <EOL> from patsy . categorical import C <EOL> from patsy . user_util import balanced , LookupFactor <EOL> from patsy . design_info import DesignMatrix , DesignInfo <EOL> if have_pandas : <EOL> import pandas <EOL> def assert_full_rank ( m ) : <EOL> m = atleast_2d_column_default ( m ) <EOL> if m . shape [ <NUM_LIT:1> ] == <NUM_LIT:0> : <EOL> return True <EOL> u , s , v = np . linalg . svd ( m ) <EOL> rank = np . sum ( s > <NUM_LIT> ) <EOL> assert rank == m . shape [ <NUM_LIT:1> ] <EOL> def test_assert_full_rank ( ) : <EOL> assert_full_rank ( np . eye ( <NUM_LIT:10> ) ) <EOL> assert_full_rank ( [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> assert_raises ( AssertionError , <EOL> assert_full_rank , [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:2> , <NUM_LIT:0> ] ] ) <EOL> assert_raises ( AssertionError , <EOL> assert_full_rank , [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:2> , <NUM_LIT:4> ] ] ) <EOL> assert_raises ( AssertionError , <EOL> assert_full_rank , [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:1> , <NUM_LIT:10> , <NUM_LIT:100> ] ] ) <EOL> assert_raises ( AssertionError , <EOL> assert_full_rank , [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:1> , <NUM_LIT:6> , <NUM_LIT:7> ] ] ) <EOL> def make_termlist ( * entries ) : <EOL> terms = [ ] <EOL> for entry in entries : <EOL> terms . append ( Term ( [ LookupFactor ( name ) for name in entry ] ) ) <EOL> return terms <EOL> def check_design_matrix ( mm , expected_rank , termlist , column_names = None ) : <EOL> assert_full_rank ( mm ) <EOL> assert set ( mm . design_info . terms ) == set ( termlist ) <EOL> if column_names is not None : <EOL> assert mm . design_info . column_names == column_names <EOL> assert mm . ndim == <NUM_LIT:2> <EOL> assert mm . shape [ <NUM_LIT:1> ] == expected_rank <EOL> def make_matrix ( data , expected_rank , entries , column_names = None ) : <EOL> termlist = make_termlist ( * entries ) <EOL> def iter_maker ( ) : <EOL> yield data <EOL> design_infos = design_matrix_builders ( [ termlist ] , iter_maker , eval_env = <NUM_LIT:0> ) <EOL> matrices = build_design_matrices ( design_infos , data ) <EOL> matrix = matrices [ <NUM_LIT:0> ] <EOL> assert ( design_infos [ <NUM_LIT:0> ] . term_slices <EOL> == matrix . design_info . term_slices ) <EOL> assert ( design_infos [ <NUM_LIT:0> ] . column_names <EOL> == matrix . design_info . column_names ) <EOL> assert matrix . design_info is design_infos [ <NUM_LIT:0> ] <EOL> check_design_matrix ( matrix , expected_rank , termlist , <EOL> column_names = column_names ) <EOL> return matrix <EOL> def test_simple ( ) : <EOL> data = balanced ( a = <NUM_LIT:2> , b = <NUM_LIT:2> ) <EOL> x1 = data [ "<STR_LIT>" ] = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , len ( data [ "<STR_LIT:a>" ] ) ) <EOL> x2 = data [ "<STR_LIT>" ] = data [ "<STR_LIT>" ] ** <NUM_LIT:2> <EOL> m = make_matrix ( data , <NUM_LIT:2> , [ [ "<STR_LIT:a>" ] ] , column_names = [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> m = make_matrix ( data , <NUM_LIT:2> , [ [ ] , [ "<STR_LIT:a>" ] ] , column_names = [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> m = make_matrix ( data , <NUM_LIT:4> , [ [ "<STR_LIT:a>" , "<STR_LIT:b>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> m = make_matrix ( data , <NUM_LIT:4> , [ [ ] , [ "<STR_LIT:a>" ] , [ "<STR_LIT:b>" ] , [ "<STR_LIT:a>" , "<STR_LIT:b>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> m = make_matrix ( data , <NUM_LIT:4> , [ [ ] , [ "<STR_LIT:b>" ] , [ "<STR_LIT:a>" ] , [ "<STR_LIT:b>" , "<STR_LIT:a>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> m = make_matrix ( data , <NUM_LIT:4> , [ [ "<STR_LIT:a>" ] , [ "<STR_LIT>" ] , [ "<STR_LIT:a>" , "<STR_LIT>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:0> , x1 [ <NUM_LIT:0> ] , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , x1 [ <NUM_LIT:1> ] , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , x1 [ <NUM_LIT:2> ] , x1 [ <NUM_LIT:2> ] ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , x1 [ <NUM_LIT:3> ] , x1 [ <NUM_LIT:3> ] ] ] ) <EOL> m = make_matrix ( data , <NUM_LIT:3> , [ [ "<STR_LIT>" ] , [ "<STR_LIT>" ] , [ "<STR_LIT>" , "<STR_LIT>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , np . column_stack ( ( x1 , x2 , x1 * x2 ) ) ) <EOL> def test_R_bugs ( ) : <EOL> data = balanced ( a = <NUM_LIT:2> , b = <NUM_LIT:2> , c = <NUM_LIT:2> ) <EOL> data [ "<STR_LIT:x>" ] = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , len ( data [ "<STR_LIT:a>" ] ) ) <EOL> make_matrix ( data , <NUM_LIT:4> , [ [ ] , [ "<STR_LIT:a>" , "<STR_LIT:b>" ] ] ) <EOL> make_matrix ( data , <NUM_LIT:6> , [ [ "<STR_LIT:a>" , "<STR_LIT:x>" ] , [ "<STR_LIT:a>" , "<STR_LIT:b>" ] ] ) <EOL> make_matrix ( data , <NUM_LIT:6> , [ [ "<STR_LIT:a>" , "<STR_LIT:c>" ] , [ "<STR_LIT:a>" , "<STR_LIT:b>" ] ] ) <EOL> def test_redundancy_thoroughly ( ) : <EOL> data = balanced ( a = <NUM_LIT:2> , b = <NUM_LIT:2> , repeat = <NUM_LIT:5> ) <EOL> data [ "<STR_LIT>" ] = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , len ( data [ "<STR_LIT:a>" ] ) ) <EOL> data [ "<STR_LIT>" ] = data [ "<STR_LIT>" ] ** <NUM_LIT:2> <EOL> def all_subsets ( l ) : <EOL> if not l : <EOL> yield tuple ( ) <EOL> else : <EOL> obj = l [ <NUM_LIT:0> ] <EOL> for subset in all_subsets ( l [ <NUM_LIT:1> : ] ) : <EOL> yield tuple ( sorted ( subset ) ) <EOL> yield tuple ( sorted ( ( obj , ) + subset ) ) <EOL> all_terms = list ( all_subsets ( ( "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT>" , "<STR_LIT>" ) ) ) <EOL> all_termlist_templates = list ( all_subsets ( all_terms ) ) <EOL> print ( len ( all_termlist_templates ) ) <EOL> redundant = [ [ ( "<STR_LIT:b>" , ) , ( "<STR_LIT:a>" , ) ] , <EOL> [ ( "<STR_LIT>" , ) , ( "<STR_LIT>" , ) ] , <EOL> [ ( "<STR_LIT:b>" , "<STR_LIT>" ) , ( "<STR_LIT:a>" , "<STR_LIT>" ) ] , <EOL> [ ( "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT>" ) , ( "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT>" ) ] , <EOL> [ ( "<STR_LIT:b>" , "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT:a>" , "<STR_LIT>" , "<STR_LIT>" ) ] ] <EOL> count = <NUM_LIT:0> <EOL> import time <EOL> start = time . time ( ) <EOL> for termlist_template in all_termlist_templates : <EOL> termlist_set = set ( termlist_template ) <EOL> for dispreferred , preferred in redundant : <EOL> if dispreferred in termlist_set and preferred not in termlist_set : <EOL> break <EOL> else : <EOL> expanded_terms = set ( ) <EOL> for term_template in termlist_template : <EOL> numeric = tuple ( [ t for t in term_template if t . startswith ( "<STR_LIT:x>" ) ] ) <EOL> rest = [ t for t in term_template if not t . startswith ( "<STR_LIT:x>" ) ] <EOL> for subset_rest in all_subsets ( rest ) : <EOL> expanded_terms . add ( frozenset ( subset_rest + numeric ) ) <EOL> expected_rank = len ( expanded_terms ) <EOL> if termlist_template in [ ( ) , ( ( ) , ) ] : <EOL> assert_raises ( PatsyError , <EOL> make_matrix , <EOL> data , expected_rank , termlist_template ) <EOL> else : <EOL> make_matrix ( data , expected_rank , termlist_template ) <EOL> count += <NUM_LIT:1> <EOL> if count % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" , count ) <EOL> print ( "<STR_LIT>" % ( time . time ( ) - start , ) ) <EOL> test_redundancy_thoroughly . slow = <NUM_LIT:1> <EOL> def test_data_types ( ) : <EOL> basic_dict = { "<STR_LIT:a>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] } <EOL> basic_dict_bytes = dict ( basic_dict ) <EOL> basic_dict_bytes [ "<STR_LIT:a>" ] = [ s . encode ( "<STR_LIT:ascii>" ) for s in basic_dict_bytes [ "<STR_LIT:a>" ] ] <EOL> basic_dict_unicode = { "<STR_LIT:a>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] } <EOL> basic_dict_unicode = dict ( basic_dict ) <EOL> basic_dict_unicode [ "<STR_LIT:a>" ] = [ six . text_type ( s ) for s in basic_dict_unicode [ "<STR_LIT:a>" ] ] <EOL> structured_array_bytes = np . array ( list ( zip ( basic_dict [ "<STR_LIT:a>" ] , <EOL> basic_dict [ "<STR_LIT:x>" ] ) ) , <EOL> dtype = [ ( "<STR_LIT:a>" , "<STR_LIT>" ) , ( "<STR_LIT:x>" , int ) ] ) <EOL> structured_array_unicode = np . array ( list ( zip ( basic_dict [ "<STR_LIT:a>" ] , <EOL> basic_dict [ "<STR_LIT:x>" ] ) ) , <EOL> dtype = [ ( "<STR_LIT:a>" , "<STR_LIT>" ) , ( "<STR_LIT:x>" , int ) ] ) <EOL> recarray_bytes = structured_array_bytes . view ( np . recarray ) <EOL> recarray_unicode = structured_array_unicode . view ( np . recarray ) <EOL> datas = [ basic_dict , structured_array_bytes , structured_array_unicode , <EOL> recarray_bytes , recarray_unicode ] <EOL> if have_pandas : <EOL> df_bytes = pandas . DataFrame ( basic_dict_bytes ) <EOL> datas . append ( df_bytes ) <EOL> df_unicode = pandas . DataFrame ( basic_dict_unicode ) <EOL> datas . append ( df_unicode ) <EOL> for data in datas : <EOL> m = make_matrix ( data , <NUM_LIT:4> , [ [ "<STR_LIT:a>" ] , [ "<STR_LIT:a>" , "<STR_LIT:x>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:4> ] ] ) <EOL> def test_build_design_matrices_dtype ( ) : <EOL> data = { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } <EOL> def iter_maker ( ) : <EOL> yield data <EOL> builder = design_matrix_builders ( [ make_termlist ( "<STR_LIT:x>" ) ] , iter_maker , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> mat = build_design_matrices ( [ builder ] , data ) [ <NUM_LIT:0> ] <EOL> assert mat . dtype == np . dtype ( np . float64 ) <EOL> mat = build_design_matrices ( [ builder ] , data , dtype = np . float32 ) [ <NUM_LIT:0> ] <EOL> assert mat . dtype == np . dtype ( np . float32 ) <EOL> if hasattr ( np , "<STR_LIT>" ) : <EOL> mat = build_design_matrices ( [ builder ] , data , dtype = np . float128 ) [ <NUM_LIT:0> ] <EOL> assert mat . dtype == np . dtype ( np . float128 ) <EOL> def test_return_type ( ) : <EOL> data = { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } <EOL> def iter_maker ( ) : <EOL> yield data <EOL> builder = design_matrix_builders ( [ make_termlist ( "<STR_LIT:x>" ) ] , iter_maker , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> mat = build_design_matrices ( [ builder ] , data , return_type = "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> assert isinstance ( mat , DesignMatrix ) <EOL> assert_raises ( PatsyError , <EOL> build_design_matrices , [ builder ] , data , <EOL> return_type = "<STR_LIT>" ) <EOL> def test_NA_action ( ) : <EOL> initial_data = { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , "<STR_LIT:c>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] } <EOL> def iter_maker ( ) : <EOL> yield initial_data <EOL> builder = design_matrix_builders ( [ make_termlist ( "<STR_LIT:x>" , "<STR_LIT:c>" ) ] , iter_maker , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> mat = build_design_matrices ( [ builder ] , <EOL> { "<STR_LIT:x>" : [ <NUM_LIT> , np . nan , <NUM_LIT> ] , <EOL> "<STR_LIT:c>" : np . asarray ( [ "<STR_LIT>" , "<STR_LIT>" , None ] , <EOL> dtype = object ) } ) [ <NUM_LIT:0> ] <EOL> assert mat . shape == ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> assert np . array_equal ( mat , [ [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ] ) <EOL> mat = build_design_matrices ( [ builder ] , <EOL> { "<STR_LIT:x>" : [ <NUM_LIT> , np . nan , <NUM_LIT> ] , <EOL> "<STR_LIT:c>" : np . asarray ( [ "<STR_LIT>" , "<STR_LIT>" , None ] , <EOL> dtype = object ) } , <EOL> NA_action = "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> assert mat . shape == ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> assert np . array_equal ( mat , [ [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ] ) <EOL> from patsy . missing import NAAction <EOL> NA_action = NAAction ( NA_types = [ ] ) <EOL> mat = build_design_matrices ( [ builder ] , <EOL> { "<STR_LIT:x>" : [ <NUM_LIT> , np . nan ] , <EOL> "<STR_LIT:c>" : np . asarray ( [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> dtype = object ) } , <EOL> NA_action = NA_action ) [ <NUM_LIT:0> ] <EOL> assert mat . shape == ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> np . testing . assert_array_equal ( mat , [ [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT> ] , [ <NUM_LIT:0.0> , <NUM_LIT:1.0> , np . nan ] ] ) <EOL> assert_raises ( PatsyError , <EOL> build_design_matrices , <EOL> [ builder ] , <EOL> { "<STR_LIT:x>" : [ <NUM_LIT> , np . nan , <NUM_LIT> ] , <EOL> "<STR_LIT:c>" : np . asarray ( [ "<STR_LIT>" , "<STR_LIT>" , None ] , <EOL> dtype = object ) } , <EOL> NA_action = "<STR_LIT>" ) <EOL> def test_NA_drop_preserves_levels ( ) : <EOL> data = { "<STR_LIT:x>" : [ <NUM_LIT:1.0> , np . nan , <NUM_LIT> ] , "<STR_LIT:c>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] } <EOL> def iter_maker ( ) : <EOL> yield data <EOL> design_info = design_matrix_builders ( [ make_termlist ( "<STR_LIT:x>" , "<STR_LIT:c>" ) ] , iter_maker , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> assert design_info . column_names == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:x>" ] <EOL> mat , = build_design_matrices ( [ design_info ] , data ) <EOL> assert mat . shape == ( <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> assert np . array_equal ( mat , [ [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:1.0> ] , <EOL> [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> ] ] ) <EOL> def test_return_type_pandas ( ) : <EOL> if not have_pandas : <EOL> return <EOL> data = pandas . DataFrame ( { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> "<STR_LIT:y>" : [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , <EOL> "<STR_LIT:a>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] } , <EOL> index = [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) <EOL> def iter_maker ( ) : <EOL> yield data <EOL> int_builder , = design_matrix_builders ( [ make_termlist ( [ ] ) ] , iter_maker , <NUM_LIT:0> ) <EOL> ( y_builder , x_builder ) = design_matrix_builders ( [ make_termlist ( "<STR_LIT:y>" ) , <EOL> make_termlist ( "<STR_LIT:x>" ) ] , <EOL> iter_maker , <EOL> eval_env = <NUM_LIT:0> ) <EOL> ( x_a_builder , ) = design_matrix_builders ( [ make_termlist ( "<STR_LIT:x>" , "<STR_LIT:a>" ) ] , <EOL> iter_maker , <EOL> eval_env = <NUM_LIT:0> ) <EOL> ( x_y_builder , ) = design_matrix_builders ( [ make_termlist ( "<STR_LIT:x>" , "<STR_LIT:y>" ) ] , <EOL> iter_maker , <EOL> eval_env = <NUM_LIT:0> ) <EOL> assert_raises ( PatsyError , <EOL> build_design_matrices , <EOL> [ x_a_builder ] , { "<STR_LIT:x>" : data [ "<STR_LIT:x>" ] , "<STR_LIT:a>" : data [ "<STR_LIT:a>" ] [ : : - <NUM_LIT:1> ] } ) <EOL> assert_raises ( PatsyError , <EOL> build_design_matrices , <EOL> [ y_builder , x_builder ] , <EOL> { "<STR_LIT:x>" : data [ "<STR_LIT:x>" ] , "<STR_LIT:y>" : data [ "<STR_LIT:y>" ] [ : : - <NUM_LIT:1> ] } ) <EOL> class CheatingDataFrame ( pandas . DataFrame ) : <EOL> def __getitem__ ( self , key ) : <EOL> if key == "<STR_LIT:x>" : <EOL> return pandas . DataFrame . __getitem__ ( self , key ) [ : : - <NUM_LIT:1> ] <EOL> else : <EOL> return pandas . DataFrame . __getitem__ ( self , key ) <EOL> assert_raises ( PatsyError , <EOL> build_design_matrices , <EOL> [ x_builder ] , <EOL> CheatingDataFrame ( data ) ) <EOL> ( mat , ) = build_design_matrices ( [ x_y_builder ] , <EOL> { "<STR_LIT:x>" : data [ "<STR_LIT:x>" ] , "<STR_LIT:y>" : [ <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT> ] } ) <EOL> assert np . allclose ( mat , [ [ <NUM_LIT:1> , <NUM_LIT> ] , [ <NUM_LIT:2> , <NUM_LIT:50> ] , [ <NUM_LIT:3> , <NUM_LIT> ] ] ) <EOL> y_df , x_df = build_design_matrices ( [ y_builder , x_builder ] , data , <EOL> return_type = "<STR_LIT>" ) <EOL> assert isinstance ( y_df , pandas . DataFrame ) <EOL> assert isinstance ( x_df , pandas . DataFrame ) <EOL> assert np . array_equal ( y_df , [ [ <NUM_LIT:4> ] , [ <NUM_LIT:5> ] , [ <NUM_LIT:6> ] ] ) <EOL> assert np . array_equal ( x_df , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] ) <EOL> assert np . array_equal ( y_df . index , [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) <EOL> assert np . array_equal ( x_df . index , [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) <EOL> assert np . array_equal ( y_df . columns , [ "<STR_LIT:y>" ] ) <EOL> assert np . array_equal ( x_df . columns , [ "<STR_LIT:x>" ] ) <EOL> assert y_df . design_info . column_names == [ "<STR_LIT:y>" ] <EOL> assert x_df . design_info . column_names == [ "<STR_LIT:x>" ] <EOL> assert y_df . design_info . term_names == [ "<STR_LIT:y>" ] <EOL> assert x_df . design_info . term_names == [ "<STR_LIT:x>" ] <EOL> y_df , x_df = build_design_matrices ( [ y_builder , x_builder ] , <EOL> { "<STR_LIT:y>" : [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] , "<STR_LIT:x>" : data [ "<STR_LIT:x>" ] } , <EOL> return_type = "<STR_LIT>" ) <EOL> assert isinstance ( y_df , pandas . DataFrame ) <EOL> assert isinstance ( x_df , pandas . DataFrame ) <EOL> assert np . array_equal ( y_df , [ [ <NUM_LIT:7> ] , [ <NUM_LIT:8> ] , [ <NUM_LIT:9> ] ] ) <EOL> assert np . array_equal ( x_df , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] ) <EOL> assert np . array_equal ( y_df . index , [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) <EOL> assert np . array_equal ( x_df . index , [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) <EOL> assert np . array_equal ( y_df . columns , [ "<STR_LIT:y>" ] ) <EOL> assert np . array_equal ( x_df . columns , [ "<STR_LIT:x>" ] ) <EOL> assert y_df . design_info . column_names == [ "<STR_LIT:y>" ] <EOL> assert x_df . design_info . column_names == [ "<STR_LIT:x>" ] <EOL> assert y_df . design_info . term_names == [ "<STR_LIT:y>" ] <EOL> assert x_df . design_info . term_names == [ "<STR_LIT:x>" ] <EOL> ( x_a_df , ) = build_design_matrices ( [ x_a_builder ] , <EOL> { "<STR_LIT:x>" : [ - <NUM_LIT:1> , - <NUM_LIT:2> , - <NUM_LIT:3> ] , "<STR_LIT:a>" : data [ "<STR_LIT:a>" ] } , <EOL> return_type = "<STR_LIT>" ) <EOL> assert isinstance ( x_a_df , pandas . DataFrame ) <EOL> assert np . array_equal ( x_a_df , [ [ <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:1> , - <NUM_LIT:2> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:3> ] ] ) <EOL> assert np . array_equal ( x_a_df . index , [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) <EOL> ( x_y_df , ) = build_design_matrices ( [ x_y_builder ] , <EOL> { "<STR_LIT:y>" : [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] , "<STR_LIT:x>" : [ <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:12> ] } , <EOL> return_type = "<STR_LIT>" ) <EOL> assert isinstance ( x_y_df , pandas . DataFrame ) <EOL> assert np . array_equal ( x_y_df , [ [ <NUM_LIT:10> , <NUM_LIT:7> ] , [ <NUM_LIT:11> , <NUM_LIT:8> ] , [ <NUM_LIT:12> , <NUM_LIT:9> ] ] ) <EOL> assert np . array_equal ( x_y_df . index , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> ( int_df , ) = build_design_matrices ( [ int_builder ] , data , <EOL> return_type = "<STR_LIT>" ) <EOL> assert isinstance ( int_df , pandas . DataFrame ) <EOL> assert np . array_equal ( int_df , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] ] ) <EOL> assert int_df . index . equals ( [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) <EOL> import patsy . build <EOL> had_pandas = patsy . build . have_pandas <EOL> try : <EOL> patsy . build . have_pandas = False <EOL> assert_raises ( PatsyError , <EOL> build_design_matrices , <EOL> [ x_builder ] , { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , return_type = "<STR_LIT>" ) <EOL> finally : <EOL> patsy . build . have_pandas = had_pandas <EOL> x_df , = build_design_matrices ( [ x_a_builder ] , <EOL> { "<STR_LIT:x>" : [ <NUM_LIT:1.0> , np . nan , <NUM_LIT> ] , <EOL> "<STR_LIT:a>" : np . asarray ( [ None , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> dtype = object ) } , <EOL> NA_action = "<STR_LIT>" , <EOL> return_type = "<STR_LIT>" ) <EOL> assert x_df . index . equals ( [ <NUM_LIT:2> ] ) <EOL> def test_data_mismatch ( ) : <EOL> test_cases_twoway = [ <EOL> ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ True , False , True ] ) , <EOL> ( C ( [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] , levels = [ "<STR_LIT:c>" , "<STR_LIT:b>" , "<STR_LIT:a>" ] ) , <EOL> C ( [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] , levels = [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] ) ) , <EOL> ( [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] , [ [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:3> ] ] ) , <EOL> ( [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ] ] , [ [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:3> ] ] ) , <EOL> ] <EOL> test_cases_oneway = [ <EOL> ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] ) , <EOL> ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , C ( [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] ) ) , <EOL> ( [ True , False , True ] , C ( [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] ) ) , <EOL> ( [ True , False , True ] , [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] ) , <EOL> ] <EOL> setup_predict_only = [ <EOL> ( [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] , [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:d>" ] ) , <EOL> ] <EOL> termlist = make_termlist ( [ "<STR_LIT:x>" ] ) <EOL> def t_incremental ( data1 , data2 ) : <EOL> def iter_maker ( ) : <EOL> yield { "<STR_LIT:x>" : data1 } <EOL> yield { "<STR_LIT:x>" : data2 } <EOL> try : <EOL> builders = design_matrix_builders ( [ termlist ] , iter_maker , <NUM_LIT:0> ) <EOL> build_design_matrices ( builders , { "<STR_LIT:x>" : data1 } ) <EOL> build_design_matrices ( builders , { "<STR_LIT:x>" : data2 } ) <EOL> except PatsyError : <EOL> pass <EOL> else : <EOL> raise AssertionError <EOL> def t_setup_predict ( data1 , data2 ) : <EOL> def iter_maker ( ) : <EOL> yield { "<STR_LIT:x>" : data1 } <EOL> builders = design_matrix_builders ( [ termlist ] , iter_maker , <NUM_LIT:0> ) <EOL> assert_raises ( PatsyError , <EOL> build_design_matrices , builders , { "<STR_LIT:x>" : data2 } ) <EOL> for ( a , b ) in test_cases_twoway : <EOL> t_incremental ( a , b ) <EOL> t_incremental ( b , a ) <EOL> t_setup_predict ( a , b ) <EOL> t_setup_predict ( b , a ) <EOL> for ( a , b ) in test_cases_oneway : <EOL> t_incremental ( a , b ) <EOL> t_setup_predict ( a , b ) <EOL> for ( a , b ) in setup_predict_only : <EOL> t_setup_predict ( a , b ) <EOL> t_setup_predict ( b , a ) <EOL> assert_raises ( PatsyError , <EOL> make_matrix , { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , "<STR_LIT:y>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] } , <EOL> <NUM_LIT:2> , [ [ "<STR_LIT:x>" ] , [ "<STR_LIT:y>" ] ] ) <EOL> def test_data_independent_builder ( ) : <EOL> data = { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } <EOL> def iter_maker ( ) : <EOL> yield data <EOL> null_builder = design_matrix_builders ( [ make_termlist ( ) ] , iter_maker , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> assert_raises ( PatsyError , build_design_matrices , [ null_builder ] , data ) <EOL> intercept_builder = design_matrix_builders ( [ make_termlist ( [ ] ) ] , <EOL> iter_maker , <EOL> eval_env = <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> assert_raises ( PatsyError , build_design_matrices , [ intercept_builder ] , data ) <EOL> assert_raises ( PatsyError , <EOL> build_design_matrices , <EOL> [ null_builder , intercept_builder ] , data ) <EOL> if have_pandas : <EOL> int_m , null_m = build_design_matrices ( [ intercept_builder , <EOL> null_builder ] , <EOL> pandas . DataFrame ( data ) ) <EOL> assert np . allclose ( int_m , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] ] ) <EOL> assert null_m . shape == ( <NUM_LIT:3> , <NUM_LIT:0> ) <EOL> x_termlist = make_termlist ( [ "<STR_LIT:x>" ] ) <EOL> builders = design_matrix_builders ( [ x_termlist , make_termlist ( ) ] , <EOL> iter_maker , <EOL> eval_env = <NUM_LIT:0> ) <EOL> x_m , null_m = build_design_matrices ( builders , data ) <EOL> assert np . allclose ( x_m , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] ) <EOL> assert null_m . shape == ( <NUM_LIT:3> , <NUM_LIT:0> ) <EOL> builders = design_matrix_builders ( [ x_termlist , make_termlist ( [ ] ) ] , <EOL> iter_maker , <EOL> eval_env = <NUM_LIT:0> ) <EOL> x_m , null_m = build_design_matrices ( builders , data ) <EOL> x_m , intercept_m = build_design_matrices ( builders , data ) <EOL> assert np . allclose ( x_m , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] ) <EOL> assert np . allclose ( intercept_m , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] ] ) <EOL> def test_same_factor_in_two_matrices ( ) : <EOL> data = { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , "<STR_LIT:a>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] } <EOL> def iter_maker ( ) : <EOL> yield data <EOL> t1 = make_termlist ( [ "<STR_LIT:x>" ] ) <EOL> t2 = make_termlist ( [ "<STR_LIT:x>" , "<STR_LIT:a>" ] ) <EOL> builders = design_matrix_builders ( [ t1 , t2 ] , iter_maker , eval_env = <NUM_LIT:0> ) <EOL> m1 , m2 = build_design_matrices ( builders , data ) <EOL> check_design_matrix ( m1 , <NUM_LIT:1> , t1 , column_names = [ "<STR_LIT:x>" ] ) <EOL> assert np . allclose ( m1 , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] ) <EOL> check_design_matrix ( m2 , <NUM_LIT:2> , t2 , column_names = [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m2 , [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:0> ] ] ) <EOL> def test_eval_env_type_builder ( ) : <EOL> data = { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } <EOL> def iter_maker ( ) : <EOL> yield data <EOL> assert_raises ( TypeError , design_matrix_builders , [ make_termlist ( "<STR_LIT:x>" ) ] , iter_maker , "<STR_LIT:foo>" ) <EOL> def test_categorical ( ) : <EOL> data_strings = { "<STR_LIT:a>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] } <EOL> data_categ = { "<STR_LIT:a>" : C ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) } <EOL> datas = [ data_strings , data_categ ] <EOL> if have_pandas_categorical : <EOL> data_pandas = { "<STR_LIT:a>" : pandas . Categorical . from_array ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) } <EOL> datas . append ( data_pandas ) <EOL> def t ( data1 , data2 ) : <EOL> def iter_maker ( ) : <EOL> yield data1 <EOL> builders = design_matrix_builders ( [ make_termlist ( [ "<STR_LIT:a>" ] ) ] , <EOL> iter_maker , <EOL> eval_env = <NUM_LIT:0> ) <EOL> build_design_matrices ( builders , data2 ) <EOL> for data1 in datas : <EOL> for data2 in datas : <EOL> t ( data1 , data2 ) <EOL> def test_contrast ( ) : <EOL> from patsy . contrasts import ContrastMatrix , Sum <EOL> values = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> m = make_matrix ( { "<STR_LIT:a>" : C ( values ) } , <NUM_LIT:3> , [ [ "<STR_LIT:a>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> for s in ( Sum , Sum ( ) ) : <EOL> m = make_matrix ( { "<STR_LIT:a>" : C ( values , s ) } , <NUM_LIT:3> , [ [ "<STR_LIT:a>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> m = make_matrix ( { "<STR_LIT:a>" : C ( values , Sum ( omit = <NUM_LIT:0> ) ) } , <NUM_LIT:3> , [ [ "<STR_LIT:a>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> m = make_matrix ( { "<STR_LIT:a>" : C ( values ) } , <NUM_LIT:3> , [ [ ] , [ "<STR_LIT:a>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> for s in ( Sum , Sum ( ) ) : <EOL> m = make_matrix ( { "<STR_LIT:a>" : C ( values , s ) } , <NUM_LIT:3> , [ [ ] , [ "<STR_LIT:a>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> m = make_matrix ( { "<STR_LIT:a>" : C ( values , Sum ( omit = <NUM_LIT:0> ) ) } , <NUM_LIT:3> , [ [ ] , [ "<STR_LIT:a>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> m = make_matrix ( { "<STR_LIT:a>" : C ( values , [ [ <NUM_LIT:7> , <NUM_LIT:12> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:8> , - <NUM_LIT:1> ] ] ) } , <EOL> <NUM_LIT:2> , [ [ "<STR_LIT:a>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:7> , <NUM_LIT:12> ] , <EOL> [ <NUM_LIT:8> , - <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:7> , <NUM_LIT:12> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT> ] ] ) <EOL> m = make_matrix ( { "<STR_LIT:a>" : C ( values , ContrastMatrix ( [ [ <NUM_LIT:7> , <NUM_LIT:12> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:8> , - <NUM_LIT:1> ] ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) ) } , <EOL> <NUM_LIT:2> , [ [ "<STR_LIT:a>" ] ] , <EOL> column_names = [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert np . allclose ( m , [ [ <NUM_LIT:7> , <NUM_LIT:12> ] , <EOL> [ <NUM_LIT:8> , - <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:7> , <NUM_LIT:12> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT> ] ] ) <EOL> def test_DesignInfo_subset ( ) : <EOL> all_data = { "<STR_LIT:x>" : [ <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> "<STR_LIT:y>" : [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] ] , <EOL> "<STR_LIT:z>" : [ <NUM_LIT:5> , <NUM_LIT:6> ] } <EOL> all_terms = make_termlist ( "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" ) <EOL> def iter_maker ( ) : <EOL> yield all_data <EOL> all_builder = design_matrix_builders ( [ all_terms ] , iter_maker , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> full_matrix = build_design_matrices ( [ all_builder ] , all_data ) [ <NUM_LIT:0> ] <EOL> def t ( which_terms , variables , columns ) : <EOL> sub_design_info = all_builder . subset ( which_terms ) <EOL> sub_data = { } <EOL> for variable in variables : <EOL> sub_data [ variable ] = all_data [ variable ] <EOL> sub_matrix = build_design_matrices ( [ sub_design_info ] , sub_data ) [ <NUM_LIT:0> ] <EOL> sub_full_matrix = full_matrix [ : , columns ] <EOL> if not isinstance ( which_terms , six . string_types ) : <EOL> assert len ( which_terms ) == len ( sub_design_info . terms ) <EOL> assert np . array_equal ( sub_matrix , sub_full_matrix ) <EOL> t ( "<STR_LIT>" , [ "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" ] , slice ( None ) ) <EOL> t ( [ "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" ] , [ "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" ] , slice ( None ) ) <EOL> if not six . PY3 : <EOL> t ( [ unicode ( "<STR_LIT:x>" ) , unicode ( "<STR_LIT:y>" ) , unicode ( "<STR_LIT:z>" ) ] , <EOL> [ "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" ] , slice ( None ) ) <EOL> t ( all_terms , [ "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" ] , slice ( None ) ) <EOL> t ( [ all_terms [ <NUM_LIT:0> ] , "<STR_LIT:y>" , all_terms [ <NUM_LIT:2> ] ] , [ "<STR_LIT:x>" , "<STR_LIT:y>" , "<STR_LIT:z>" ] , slice ( None ) ) <EOL> t ( "<STR_LIT>" , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:0> , <NUM_LIT:3> ] ) <EOL> t ( [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:0> , <NUM_LIT:3> ] ) <EOL> if not six . PY3 : <EOL> t ( [ unicode ( "<STR_LIT:x>" ) , unicode ( "<STR_LIT:z>" ) ] , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:0> , <NUM_LIT:3> ] ) <EOL> t ( [ all_terms [ <NUM_LIT:0> ] , all_terms [ <NUM_LIT:2> ] ] , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:0> , <NUM_LIT:3> ] ) <EOL> t ( [ all_terms [ <NUM_LIT:0> ] , "<STR_LIT:z>" ] , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:0> , <NUM_LIT:3> ] ) <EOL> t ( "<STR_LIT>" , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:3> , <NUM_LIT:0> ] ) <EOL> t ( [ "<STR_LIT:z>" , "<STR_LIT:x>" ] , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:3> , <NUM_LIT:0> ] ) <EOL> t ( [ six . text_type ( "<STR_LIT:z>" ) , six . text_type ( "<STR_LIT:x>" ) ] , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:3> , <NUM_LIT:0> ] ) <EOL> t ( [ all_terms [ <NUM_LIT:2> ] , all_terms [ <NUM_LIT:0> ] ] , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:3> , <NUM_LIT:0> ] ) <EOL> t ( [ all_terms [ <NUM_LIT:2> ] , "<STR_LIT:x>" ] , [ "<STR_LIT:x>" , "<STR_LIT:z>" ] , [ <NUM_LIT:3> , <NUM_LIT:0> ] ) <EOL> t ( "<STR_LIT>" , [ "<STR_LIT:y>" ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> t ( [ "<STR_LIT:y>" ] , [ "<STR_LIT:y>" ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> t ( [ six . text_type ( "<STR_LIT:y>" ) ] , [ "<STR_LIT:y>" ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> t ( [ all_terms [ <NUM_LIT:1> ] ] , [ "<STR_LIT:y>" ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> assert_raises ( PatsyError , all_builder . subset , "<STR_LIT>" ) <EOL> assert_raises ( KeyError , all_builder . subset , "<STR_LIT>" ) <EOL> assert_raises ( KeyError , all_builder . subset , [ "<STR_LIT>" ] ) <EOL> assert_raises ( KeyError , <EOL> all_builder . subset , [ Term ( [ "<STR_LIT>" ] ) ] ) <EOL> min_di = DesignInfo ( [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] ) <EOL> min_di_subset = min_di . subset ( [ "<STR_LIT:c>" , "<STR_LIT:a>" ] ) <EOL> assert min_di_subset . column_names == [ "<STR_LIT:c>" , "<STR_LIT:a>" ] <EOL> assert min_di_subset . terms is None </s>
