<s> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> import itertools <EOL> import six <EOL> import numpy as np <EOL> from patsy import PatsyError <EOL> from patsy . categorical import ( guess_categorical , <EOL> CategoricalSniffer , <EOL> categorical_to_int ) <EOL> from patsy . util import ( atleast_2d_column_default , <EOL> have_pandas , asarray_or_pandas , <EOL> safe_issubdtype ) <EOL> from patsy . design_info import ( DesignMatrix , DesignInfo , <EOL> FactorInfo , SubtermInfo ) <EOL> from patsy . redundancy import pick_contrasts_for_term <EOL> from patsy . eval import EvalEnvironment <EOL> from patsy . contrasts import code_contrast_matrix , Treatment <EOL> from patsy . compat import OrderedDict <EOL> from patsy . missing import NAAction <EOL> if have_pandas : <EOL> import pandas <EOL> class _MockFactor ( object ) : <EOL> def __init__ ( self , name = "<STR_LIT>" ) : <EOL> self . _name = name <EOL> def eval ( self , state , env ) : <EOL> return env [ "<STR_LIT>" ] <EOL> def name ( self ) : <EOL> return self . _name <EOL> def _max_allowed_dim ( dim , arr , factor ) : <EOL> if arr . ndim > dim : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( factor . name ( ) , arr . ndim , dim ) ) <EOL> raise PatsyError ( msg , factor ) <EOL> def test__max_allowed_dim ( ) : <EOL> from nose . tools import assert_raises <EOL> f = _MockFactor ( ) <EOL> _max_allowed_dim ( <NUM_LIT:1> , np . array ( <NUM_LIT:1> ) , f ) <EOL> _max_allowed_dim ( <NUM_LIT:1> , np . array ( [ <NUM_LIT:1> ] ) , f ) <EOL> assert_raises ( PatsyError , _max_allowed_dim , <NUM_LIT:1> , np . array ( [ [ <NUM_LIT:1> ] ] ) , f ) <EOL> assert_raises ( PatsyError , _max_allowed_dim , <NUM_LIT:1> , np . array ( [ [ [ <NUM_LIT:1> ] ] ] ) , f ) <EOL> _max_allowed_dim ( <NUM_LIT:2> , np . array ( <NUM_LIT:1> ) , f ) <EOL> _max_allowed_dim ( <NUM_LIT:2> , np . array ( [ <NUM_LIT:1> ] ) , f ) <EOL> _max_allowed_dim ( <NUM_LIT:2> , np . array ( [ [ <NUM_LIT:1> ] ] ) , f ) <EOL> assert_raises ( PatsyError , _max_allowed_dim , <NUM_LIT:2> , np . array ( [ [ [ <NUM_LIT:1> ] ] ] ) , f ) <EOL> def _eval_factor ( factor_info , data , NA_action ) : <EOL> factor = factor_info . factor <EOL> result = factor . eval ( factor_info . state , data ) <EOL> if factor_info . type == "<STR_LIT>" : <EOL> result = atleast_2d_column_default ( result , preserve_pandas = True ) <EOL> _max_allowed_dim ( <NUM_LIT:2> , result , factor ) <EOL> if result . shape [ <NUM_LIT:1> ] != factor_info . num_columns : <EOL> raise PatsyError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( factor . name ( ) , <EOL> factor_info . num_columns , <EOL> result . shape [ <NUM_LIT:1> ] ) , <EOL> factor ) <EOL> if not safe_issubdtype ( np . asarray ( result ) . dtype , np . number ) : <EOL> raise PatsyError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( factor . name ( ) , result . dtype ) , <EOL> factor ) <EOL> return result , NA_action . is_numerical_NA ( result ) <EOL> else : <EOL> assert factor_info . type == "<STR_LIT>" <EOL> result = categorical_to_int ( result , factor_info . categories , NA_action , <EOL> origin = factor_info . factor ) <EOL> assert result . ndim == <NUM_LIT:1> <EOL> return result , np . asarray ( result == - <NUM_LIT:1> ) <EOL> def test__eval_factor_numerical ( ) : <EOL> from nose . tools import assert_raises <EOL> naa = NAAction ( ) <EOL> f = _MockFactor ( ) <EOL> fi1 = FactorInfo ( f , "<STR_LIT>" , { } , num_columns = <NUM_LIT:1> , categories = None ) <EOL> assert fi1 . factor is f <EOL> eval123 , is_NA = _eval_factor ( fi1 , { "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , naa ) <EOL> assert eval123 . shape == ( <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> assert np . all ( eval123 == [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] ) <EOL> assert is_NA . shape == ( <NUM_LIT:3> , ) <EOL> assert np . all ( ~ is_NA ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , { "<STR_LIT>" : [ [ [ <NUM_LIT:1> ] ] ] } , naa ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , { "<STR_LIT>" : [ [ <NUM_LIT:1> , <NUM_LIT:2> ] ] } , naa ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , { "<STR_LIT>" : [ "<STR_LIT:a>" , "<STR_LIT:b>" ] } , naa ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , { "<STR_LIT>" : [ True , False ] } , naa ) <EOL> fi2 = FactorInfo ( _MockFactor ( ) , "<STR_LIT>" , <EOL> { } , num_columns = <NUM_LIT:2> , categories = None ) <EOL> eval123321 , is_NA = _eval_factor ( fi2 , <EOL> { "<STR_LIT>" : [ [ <NUM_LIT:1> , <NUM_LIT:3> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:1> ] ] } , <EOL> naa ) <EOL> assert eval123321 . shape == ( <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> assert np . all ( eval123321 == [ [ <NUM_LIT:1> , <NUM_LIT:3> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:1> ] ] ) <EOL> assert is_NA . shape == ( <NUM_LIT:3> , ) <EOL> assert np . all ( ~ is_NA ) <EOL> assert_raises ( PatsyError , _eval_factor , fi2 , { "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , naa ) <EOL> assert_raises ( PatsyError , _eval_factor , fi2 , { "<STR_LIT>" : [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] } , naa ) <EOL> ev_nan , is_NA = _eval_factor ( fi1 , { "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> , np . nan ] } , <EOL> NAAction ( NA_types = [ "<STR_LIT>" ] ) ) <EOL> assert np . array_equal ( is_NA , [ False , False , True ] ) <EOL> ev_nan , is_NA = _eval_factor ( fi1 , { "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> , np . nan ] } , <EOL> NAAction ( NA_types = [ ] ) ) <EOL> assert np . array_equal ( is_NA , [ False , False , False ] ) <EOL> if have_pandas : <EOL> eval_ser , _ = _eval_factor ( fi1 , <EOL> { "<STR_LIT>" : <EOL> pandas . Series ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> index = [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) } , <EOL> naa ) <EOL> assert isinstance ( eval_ser , pandas . DataFrame ) <EOL> assert np . array_equal ( eval_ser , [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] ) <EOL> assert np . array_equal ( eval_ser . index , [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) <EOL> eval_df1 , _ = _eval_factor ( fi1 , <EOL> { "<STR_LIT>" : <EOL> pandas . DataFrame ( [ [ <NUM_LIT:2> ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:3> ] ] , <EOL> index = [ <NUM_LIT:20> , <NUM_LIT:10> , <NUM_LIT:30> ] ) } , <EOL> naa ) <EOL> assert isinstance ( eval_df1 , pandas . DataFrame ) <EOL> assert np . array_equal ( eval_df1 , [ [ <NUM_LIT:2> ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:3> ] ] ) <EOL> assert np . array_equal ( eval_df1 . index , [ <NUM_LIT:20> , <NUM_LIT:10> , <NUM_LIT:30> ] ) <EOL> eval_df2 , _ = _eval_factor ( fi2 , <EOL> { "<STR_LIT>" : <EOL> pandas . DataFrame ( [ [ <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:1> , <NUM_LIT:4> ] , [ <NUM_LIT:3> , - <NUM_LIT:1> ] ] , <EOL> index = [ <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT:10> ] ) } , <EOL> naa ) <EOL> assert isinstance ( eval_df2 , pandas . DataFrame ) <EOL> assert np . array_equal ( eval_df2 , [ [ <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:1> , <NUM_LIT:4> ] , [ <NUM_LIT:3> , - <NUM_LIT:1> ] ] ) <EOL> assert np . array_equal ( eval_df2 . index , [ <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT:10> ] ) <EOL> assert_raises ( PatsyError , <EOL> _eval_factor , fi2 , <EOL> { "<STR_LIT>" : pandas . Series ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , index = [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ) } , <EOL> naa ) <EOL> assert_raises ( PatsyError , <EOL> _eval_factor , fi1 , <EOL> { "<STR_LIT>" : <EOL> pandas . DataFrame ( [ [ <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:1> , <NUM_LIT:4> ] , [ <NUM_LIT:3> , - <NUM_LIT:1> ] ] , <EOL> index = [ <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT:10> ] ) } , <EOL> naa ) <EOL> def test__eval_factor_categorical ( ) : <EOL> from nose . tools import assert_raises <EOL> from patsy . categorical import C <EOL> naa = NAAction ( ) <EOL> f = _MockFactor ( ) <EOL> fi1 = FactorInfo ( f , "<STR_LIT>" , <EOL> { } , num_columns = None , categories = ( "<STR_LIT:a>" , "<STR_LIT:b>" ) ) <EOL> assert fi1 . factor is f <EOL> cat1 , _ = _eval_factor ( fi1 , { "<STR_LIT>" : [ "<STR_LIT:b>" , "<STR_LIT:a>" , "<STR_LIT:b>" ] } , naa ) <EOL> assert cat1 . shape == ( <NUM_LIT:3> , ) <EOL> assert np . all ( cat1 == [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , { "<STR_LIT>" : [ "<STR_LIT:c>" ] } , naa ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , { "<STR_LIT>" : C ( [ "<STR_LIT:a>" , "<STR_LIT:c>" ] ) } , naa ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , <EOL> { "<STR_LIT>" : C ( [ "<STR_LIT:a>" , "<STR_LIT:b>" ] , levels = [ "<STR_LIT:b>" , "<STR_LIT:a>" ] ) } , naa ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , { "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] } , naa ) <EOL> bad_cat = np . asarray ( [ "<STR_LIT:b>" , "<STR_LIT:a>" , "<STR_LIT:a>" , "<STR_LIT:b>" ] ) <EOL> bad_cat . resize ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , { "<STR_LIT>" : bad_cat } , naa ) <EOL> cat1_NA , is_NA = _eval_factor ( fi1 , { "<STR_LIT>" : [ "<STR_LIT:a>" , None , "<STR_LIT:b>" ] } , <EOL> NAAction ( NA_types = [ "<STR_LIT:None>" ] ) ) <EOL> assert np . array_equal ( is_NA , [ False , True , False ] ) <EOL> assert np . array_equal ( cat1_NA , [ <NUM_LIT:0> , - <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> assert_raises ( PatsyError , _eval_factor , fi1 , <EOL> { "<STR_LIT>" : [ "<STR_LIT:a>" , None , "<STR_LIT:b>" ] } , NAAction ( NA_types = [ ] ) ) <EOL> fi2 = FactorInfo ( _MockFactor ( ) , "<STR_LIT>" , { } , <EOL> num_columns = None , categories = [ False , True ] ) <EOL> cat2 , _ = _eval_factor ( fi2 , { "<STR_LIT>" : [ True , False , False , True ] } , naa ) <EOL> assert cat2 . shape == ( <NUM_LIT:4> , ) <EOL> assert np . all ( cat2 == [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> if have_pandas : <EOL> s = pandas . Series ( [ "<STR_LIT:b>" , "<STR_LIT:a>" ] , index = [ <NUM_LIT:10> , <NUM_LIT:20> ] ) <EOL> cat_s , _ = _eval_factor ( fi1 , { "<STR_LIT>" : s } , naa ) <EOL> assert isinstance ( cat_s , pandas . Series ) <EOL> assert np . array_equal ( cat_s , [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> assert np . array_equal ( cat_s . index , [ <NUM_LIT:10> , <NUM_LIT:20> ] ) <EOL> sbool = pandas . Series ( [ True , False ] , index = [ <NUM_LIT:11> , <NUM_LIT> ] ) <EOL> cat_sbool , _ = _eval_factor ( fi2 , { "<STR_LIT>" : sbool } , naa ) <EOL> assert isinstance ( cat_sbool , pandas . Series ) <EOL> assert np . array_equal ( cat_sbool , [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> assert np . array_equal ( cat_sbool . index , [ <NUM_LIT:11> , <NUM_LIT> ] ) <EOL> def _column_combinations ( columns_per_factor ) : <EOL> iterators = [ range ( n ) for n in reversed ( columns_per_factor ) ] <EOL> for reversed_combo in itertools . product ( * iterators ) : <EOL> yield reversed_combo [ : : - <NUM_LIT:1> ] <EOL> def test__column_combinations ( ) : <EOL> assert list ( _column_combinations ( [ <NUM_LIT:2> , <NUM_LIT:3> ] ) ) == [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:2> ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> assert list ( _column_combinations ( [ <NUM_LIT:3> ] ) ) == [ ( <NUM_LIT:0> , ) , ( <NUM_LIT:1> , ) , ( <NUM_LIT:2> , ) ] <EOL> assert list ( _column_combinations ( [ ] ) ) == [ ( ) ] <EOL> def _subterm_column_combinations ( factor_infos , subterm ) : <EOL> columns_per_factor = [ ] <EOL> for factor in subterm . factors : <EOL> if factor in subterm . contrast_matrices : <EOL> columns = subterm . contrast_matrices [ factor ] . matrix . shape [ <NUM_LIT:1> ] <EOL> else : <EOL> columns = factor_infos [ factor ] . num_columns <EOL> columns_per_factor . append ( columns ) <EOL> return _column_combinations ( columns_per_factor ) <EOL> def _subterm_column_names_iter ( factor_infos , subterm ) : <EOL> total = <NUM_LIT:0> <EOL> for i , column_idxs in enumerate ( <EOL> _subterm_column_combinations ( factor_infos , subterm ) ) : <EOL> name_pieces = [ ] <EOL> for factor , column_idx in zip ( subterm . factors , column_idxs ) : <EOL> fi = factor_infos [ factor ] <EOL> if fi . type == "<STR_LIT>" : <EOL> if fi . num_columns > <NUM_LIT:1> : <EOL> name_pieces . append ( "<STR_LIT>" <EOL> % ( factor . name ( ) , column_idx ) ) <EOL> else : <EOL> assert column_idx == <NUM_LIT:0> <EOL> name_pieces . append ( factor . name ( ) ) <EOL> else : <EOL> assert fi . type == "<STR_LIT>" <EOL> contrast = subterm . contrast_matrices [ factor ] <EOL> suffix = contrast . column_suffixes [ column_idx ] <EOL> name_pieces . append ( "<STR_LIT>" % ( factor . name ( ) , suffix ) ) <EOL> if not name_pieces : <EOL> yield "<STR_LIT>" <EOL> else : <EOL> yield "<STR_LIT::>" . join ( name_pieces ) <EOL> total += <NUM_LIT:1> <EOL> assert total == subterm . num_columns <EOL> def _build_subterm ( subterm , factor_infos , factor_values , out ) : <EOL> assert subterm . num_columns == out . shape [ <NUM_LIT:1> ] <EOL> out [ ... ] = <NUM_LIT:1> <EOL> for i , column_idxs in enumerate ( <EOL> _subterm_column_combinations ( factor_infos , subterm ) ) : <EOL> for factor , column_idx in zip ( subterm . factors , column_idxs ) : <EOL> if factor_infos [ factor ] . type == "<STR_LIT>" : <EOL> contrast = subterm . contrast_matrices [ factor ] <EOL> if np . any ( factor_values [ factor ] < <NUM_LIT:0> ) : <EOL> raise PatsyError ( "<STR_LIT>" <EOL> "<STR_LIT>" , factor ) <EOL> out [ : , i ] *= contrast . matrix [ factor_values [ factor ] , <EOL> column_idx ] <EOL> else : <EOL> assert factor_infos [ factor ] . type == "<STR_LIT>" <EOL> assert ( factor_values [ factor ] . shape [ <NUM_LIT:1> ] <EOL> == factor_infos [ factor ] . num_columns ) <EOL> out [ : , i ] *= factor_values [ factor ] [ : , column_idx ] <EOL> def test__subterm_column_names_iter_and__build_subterm ( ) : <EOL> from nose . tools import assert_raises <EOL> from patsy . contrasts import ContrastMatrix <EOL> from patsy . categorical import C <EOL> f1 = _MockFactor ( "<STR_LIT>" ) <EOL> f2 = _MockFactor ( "<STR_LIT>" ) <EOL> f3 = _MockFactor ( "<STR_LIT>" ) <EOL> contrast = ContrastMatrix ( np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:0.5> ] , <EOL> [ <NUM_LIT:3> , <NUM_LIT:0> ] ] ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> factor_infos1 = { f1 : FactorInfo ( f1 , "<STR_LIT>" , { } , <EOL> num_columns = <NUM_LIT:1> , categories = None ) , <EOL> f2 : FactorInfo ( f2 , "<STR_LIT>" , { } , <EOL> num_columns = None , categories = [ "<STR_LIT:a>" , "<STR_LIT:b>" ] ) , <EOL> f3 : FactorInfo ( f3 , "<STR_LIT>" , { } , <EOL> num_columns = <NUM_LIT:1> , categories = None ) , <EOL> } <EOL> contrast_matrices = { f2 : contrast } <EOL> subterm1 = SubtermInfo ( [ f1 , f2 , f3 ] , contrast_matrices , <NUM_LIT:2> ) <EOL> assert ( list ( _subterm_column_names_iter ( factor_infos1 , subterm1 ) ) <EOL> == [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> mat = np . empty ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> _build_subterm ( subterm1 , factor_infos1 , <EOL> { f1 : atleast_2d_column_default ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) , <EOL> f2 : np . asarray ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ) , <EOL> f3 : atleast_2d_column_default ( [ <NUM_LIT> , <NUM_LIT:2> , - <NUM_LIT:12> ] ) } , <EOL> mat ) <EOL> assert np . allclose ( mat , [ [ <NUM_LIT:0> , <NUM_LIT:0.5> * <NUM_LIT:1> * <NUM_LIT> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0.5> * <NUM_LIT:2> * <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:3> * <NUM_LIT:3> * - <NUM_LIT:12> , <NUM_LIT:0> ] ] ) <EOL> assert_raises ( PatsyError , _build_subterm , subterm1 , factor_infos1 , <EOL> { f1 : atleast_2d_column_default ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) , <EOL> f2 : np . asarray ( [ <NUM_LIT:0> , - <NUM_LIT:1> , <NUM_LIT:1> ] ) , <EOL> f3 : atleast_2d_column_default ( [ <NUM_LIT> , <NUM_LIT:2> , - <NUM_LIT:12> ] ) } , <EOL> mat ) <EOL> factor_infos2 = dict ( factor_infos1 ) <EOL> factor_infos2 [ f1 ] = FactorInfo ( f1 , "<STR_LIT>" , { } , <EOL> num_columns = <NUM_LIT:2> , categories = None ) <EOL> subterm2 = SubtermInfo ( [ f1 , f2 , f3 ] , contrast_matrices , <NUM_LIT:4> ) <EOL> assert ( list ( _subterm_column_names_iter ( factor_infos2 , subterm2 ) ) <EOL> == [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> mat2 = np . empty ( ( <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> _build_subterm ( subterm2 , factor_infos2 , <EOL> { f1 : atleast_2d_column_default ( [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:6> ] ] ) , <EOL> f2 : np . asarray ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ) , <EOL> f3 : atleast_2d_column_default ( [ <NUM_LIT> , <NUM_LIT:2> , - <NUM_LIT:12> ] ) } , <EOL> mat2 ) <EOL> assert np . allclose ( mat2 , [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0.5> * <NUM_LIT:1> * <NUM_LIT> , <NUM_LIT:0.5> * <NUM_LIT:2> * <NUM_LIT> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0.5> * <NUM_LIT:3> * <NUM_LIT:2> , <NUM_LIT:0.5> * <NUM_LIT:4> * <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:3> * <NUM_LIT:5> * - <NUM_LIT:12> , <NUM_LIT:3> * <NUM_LIT:6> * - <NUM_LIT:12> , <NUM_LIT:0> , <NUM_LIT:0> ] ] ) <EOL> subterm_int = SubtermInfo ( [ ] , { } , <NUM_LIT:1> ) <EOL> assert list ( _subterm_column_names_iter ( { } , subterm_int ) ) == [ "<STR_LIT>" ] <EOL> mat3 = np . empty ( ( <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> _build_subterm ( subterm_int , { } , <EOL> { f1 : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , f2 : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , f3 : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , <EOL> mat3 ) <EOL> assert np . allclose ( mat3 , <NUM_LIT:1> ) <EOL> def _factors_memorize ( factors , data_iter_maker , eval_env ) : <EOL> factor_states = { } <EOL> passes_needed = { } <EOL> for factor in factors : <EOL> state = { } <EOL> which_pass = factor . memorize_passes_needed ( state , eval_env ) <EOL> factor_states [ factor ] = state <EOL> passes_needed [ factor ] = which_pass <EOL> memorize_needed = set ( ) <EOL> for factor , passes in six . iteritems ( passes_needed ) : <EOL> if passes > <NUM_LIT:0> : <EOL> memorize_needed . add ( factor ) <EOL> which_pass = <NUM_LIT:0> <EOL> while memorize_needed : <EOL> for data in data_iter_maker ( ) : <EOL> for factor in memorize_needed : <EOL> state = factor_states [ factor ] <EOL> factor . memorize_chunk ( state , which_pass , data ) <EOL> for factor in list ( memorize_needed ) : <EOL> factor . memorize_finish ( factor_states [ factor ] , which_pass ) <EOL> if which_pass == passes_needed [ factor ] - <NUM_LIT:1> : <EOL> memorize_needed . remove ( factor ) <EOL> which_pass += <NUM_LIT:1> <EOL> return factor_states <EOL> def test__factors_memorize ( ) : <EOL> class MockFactor ( object ) : <EOL> def __init__ ( self , requested_passes , token ) : <EOL> self . _requested_passes = requested_passes <EOL> self . _token = token <EOL> self . _chunk_in_pass = <NUM_LIT:0> <EOL> self . _seen_passes = <NUM_LIT:0> <EOL> def memorize_passes_needed ( self , state , eval_env ) : <EOL> state [ "<STR_LIT>" ] = [ ] <EOL> state [ "<STR_LIT>" ] = self . _token <EOL> return self . _requested_passes <EOL> def memorize_chunk ( self , state , which_pass , data ) : <EOL> state [ "<STR_LIT>" ] . append ( ( "<STR_LIT>" , which_pass ) ) <EOL> assert data [ "<STR_LIT>" ] == self . _chunk_in_pass <EOL> self . _chunk_in_pass += <NUM_LIT:1> <EOL> def memorize_finish ( self , state , which_pass ) : <EOL> state [ "<STR_LIT>" ] . append ( ( "<STR_LIT>" , which_pass ) ) <EOL> self . _chunk_in_pass = <NUM_LIT:0> <EOL> class Data ( object ) : <EOL> CHUNKS = <NUM_LIT:3> <EOL> def __init__ ( self ) : <EOL> self . calls = <NUM_LIT:0> <EOL> self . data = [ { "<STR_LIT>" : i } for i in range ( self . CHUNKS ) ] <EOL> def __call__ ( self ) : <EOL> self . calls += <NUM_LIT:1> <EOL> return iter ( self . data ) <EOL> data = Data ( ) <EOL> f0 = MockFactor ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> f1 = MockFactor ( <NUM_LIT:1> , "<STR_LIT>" ) <EOL> f2a = MockFactor ( <NUM_LIT:2> , "<STR_LIT>" ) <EOL> f2b = MockFactor ( <NUM_LIT:2> , "<STR_LIT>" ) <EOL> factor_states = _factors_memorize ( set ( [ f0 , f1 , f2a , f2b ] ) , data , { } ) <EOL> assert data . calls == <NUM_LIT:2> <EOL> mem_chunks0 = [ ( "<STR_LIT>" , <NUM_LIT:0> ) ] * data . CHUNKS <EOL> mem_chunks1 = [ ( "<STR_LIT>" , <NUM_LIT:1> ) ] * data . CHUNKS <EOL> expected = { <EOL> f0 : { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> f1 : { <EOL> "<STR_LIT>" : mem_chunks0 + [ ( "<STR_LIT>" , <NUM_LIT:0> ) ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> f2a : { <EOL> "<STR_LIT>" : mem_chunks0 + [ ( "<STR_LIT>" , <NUM_LIT:0> ) ] <EOL> + mem_chunks1 + [ ( "<STR_LIT>" , <NUM_LIT:1> ) ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> f2b : { <EOL> "<STR_LIT>" : mem_chunks0 + [ ( "<STR_LIT>" , <NUM_LIT:0> ) ] <EOL> + mem_chunks1 + [ ( "<STR_LIT>" , <NUM_LIT:1> ) ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> } <EOL> assert factor_states == expected <EOL> def _examine_factor_types ( factors , factor_states , data_iter_maker , NA_action ) : <EOL> num_column_counts = { } <EOL> cat_sniffers = { } <EOL> examine_needed = set ( factors ) <EOL> for data in data_iter_maker ( ) : <EOL> for factor in list ( examine_needed ) : <EOL> value = factor . eval ( factor_states [ factor ] , data ) <EOL> if factor in cat_sniffers or guess_categorical ( value ) : <EOL> if factor not in cat_sniffers : <EOL> cat_sniffers [ factor ] = CategoricalSniffer ( NA_action , <EOL> factor . origin ) <EOL> done = cat_sniffers [ factor ] . sniff ( value ) <EOL> if done : <EOL> examine_needed . remove ( factor ) <EOL> else : <EOL> value = atleast_2d_column_default ( value ) <EOL> _max_allowed_dim ( <NUM_LIT:2> , value , factor ) <EOL> column_count = value . shape [ <NUM_LIT:1> ] <EOL> num_column_counts [ factor ] = column_count <EOL> examine_needed . remove ( factor ) <EOL> if not examine_needed : <EOL> break <EOL> cat_levels_contrasts = { } <EOL> for factor , sniffer in six . iteritems ( cat_sniffers ) : <EOL> cat_levels_contrasts [ factor ] = sniffer . levels_contrast ( ) <EOL> return ( num_column_counts , cat_levels_contrasts ) <EOL> def test__examine_factor_types ( ) : <EOL> from patsy . categorical import C <EOL> class MockFactor ( object ) : <EOL> def __init__ ( self ) : <EOL> from patsy . origin import Origin <EOL> self . origin = Origin ( "<STR_LIT>" , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> def eval ( self , state , data ) : <EOL> return state [ data ] <EOL> def name ( self ) : <EOL> return "<STR_LIT>" <EOL> class DataIterMaker ( object ) : <EOL> def __init__ ( self ) : <EOL> self . i = - <NUM_LIT:1> <EOL> def __call__ ( self ) : <EOL> return self <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> self . i += <NUM_LIT:1> <EOL> if self . i > <NUM_LIT:1> : <EOL> raise StopIteration <EOL> return self . i <EOL> __next__ = next <EOL> num_1dim = MockFactor ( ) <EOL> num_1col = MockFactor ( ) <EOL> num_4col = MockFactor ( ) <EOL> categ_1col = MockFactor ( ) <EOL> bool_1col = MockFactor ( ) <EOL> string_1col = MockFactor ( ) <EOL> object_1col = MockFactor ( ) <EOL> object_levels = ( object ( ) , object ( ) , object ( ) ) <EOL> factor_states = { <EOL> num_1dim : ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) , <EOL> num_1col : ( [ [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> ] ] , [ [ <NUM_LIT:4> ] , [ <NUM_LIT:5> ] , [ <NUM_LIT:6> ] ] ) , <EOL> num_4col : ( np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:4> ) ) , np . ones ( ( <NUM_LIT:3> , <NUM_LIT:4> ) ) ) , <EOL> categ_1col : ( C ( [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] , levels = ( "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ) , <EOL> contrast = "<STR_LIT>" ) , <EOL> C ( [ "<STR_LIT:c>" , "<STR_LIT:b>" , "<STR_LIT:a>" ] , levels = ( "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ) , <EOL> contrast = "<STR_LIT>" ) ) , <EOL> bool_1col : ( [ True , True , False ] , [ False , True , True ] ) , <EOL> string_1col : ( [ "<STR_LIT:a>" , "<STR_LIT:a>" , "<STR_LIT:a>" ] , [ "<STR_LIT:c>" , "<STR_LIT:b>" , "<STR_LIT:a>" ] ) , <EOL> object_1col : ( [ object_levels [ <NUM_LIT:0> ] ] * <NUM_LIT:3> , object_levels ) , <EOL> } <EOL> it = DataIterMaker ( ) <EOL> ( num_column_counts , cat_levels_contrasts , <EOL> ) = _examine_factor_types ( factor_states . keys ( ) , factor_states , it , <EOL> NAAction ( ) ) <EOL> assert it . i == <NUM_LIT:2> <EOL> iterations = <NUM_LIT:0> <EOL> assert num_column_counts == { num_1dim : <NUM_LIT:1> , num_1col : <NUM_LIT:1> , num_4col : <NUM_LIT:4> } <EOL> assert cat_levels_contrasts == { <EOL> categ_1col : ( ( "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ) , "<STR_LIT>" ) , <EOL> bool_1col : ( ( False , True ) , None ) , <EOL> string_1col : ( ( "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ) , None ) , <EOL> object_1col : ( tuple ( sorted ( object_levels , key = id ) ) , None ) , <EOL> } <EOL> it = DataIterMaker ( ) <EOL> no_read_necessary = [ num_1dim , num_1col , num_4col , categ_1col , bool_1col ] <EOL> ( num_column_counts , cat_levels_contrasts , <EOL> ) = _examine_factor_types ( no_read_necessary , factor_states , it , <EOL> NAAction ( ) ) <EOL> assert it . i == <NUM_LIT:0> <EOL> assert num_column_counts == { num_1dim : <NUM_LIT:1> , num_1col : <NUM_LIT:1> , num_4col : <NUM_LIT:4> } <EOL> assert cat_levels_contrasts == { <EOL> categ_1col : ( ( "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ) , "<STR_LIT>" ) , <EOL> bool_1col : ( ( False , True ) , None ) , <EOL> } <EOL> bool_3col = MockFactor ( ) <EOL> num_3dim = MockFactor ( ) <EOL> string_3col = MockFactor ( ) <EOL> object_3col = MockFactor ( ) <EOL> illegal_factor_states = { <EOL> num_3dim : ( np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) ) , np . ones ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) ) ) , <EOL> string_3col : ( [ [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] ] , [ [ "<STR_LIT:b>" , "<STR_LIT:c>" , "<STR_LIT:a>" ] ] ) , <EOL> object_3col : ( [ [ [ object ( ) ] ] ] , [ [ [ object ( ) ] ] ] ) , <EOL> } <EOL> from nose . tools import assert_raises <EOL> for illegal_factor in illegal_factor_states : <EOL> it = DataIterMaker ( ) <EOL> try : <EOL> _examine_factor_types ( [ illegal_factor ] , illegal_factor_states , it , <EOL> NAAction ( ) ) <EOL> except PatsyError as e : <EOL> assert e . origin is illegal_factor . origin <EOL> else : <EOL> assert False <EOL> def _make_subterm_infos ( terms , <EOL> num_column_counts , <EOL> cat_levels_contrasts ) : <EOL> term_buckets = OrderedDict ( ) <EOL> bucket_ordering = [ ] <EOL> for term in terms : <EOL> num_factors = [ ] <EOL> for factor in term . factors : <EOL> if factor in num_column_counts : <EOL> num_factors . append ( factor ) <EOL> bucket = frozenset ( num_factors ) <EOL> if bucket not in term_buckets : <EOL> bucket_ordering . append ( bucket ) <EOL> term_buckets . setdefault ( bucket , [ ] ) . append ( term ) <EOL> if frozenset ( ) in term_buckets : <EOL> bucket_ordering . remove ( frozenset ( ) ) <EOL> bucket_ordering . insert ( <NUM_LIT:0> , frozenset ( ) ) <EOL> term_to_subterm_infos = OrderedDict ( ) <EOL> new_term_order = [ ] <EOL> for bucket in bucket_ordering : <EOL> bucket_terms = term_buckets [ bucket ] <EOL> bucket_terms . sort ( key = lambda t : len ( t . factors ) ) <EOL> new_term_order += bucket_terms <EOL> used_subterms = set ( ) <EOL> for term in bucket_terms : <EOL> subterm_infos = [ ] <EOL> factor_codings = pick_contrasts_for_term ( term , <EOL> num_column_counts , <EOL> used_subterms ) <EOL> for factor_coding in factor_codings : <EOL> subterm_factors = [ ] <EOL> contrast_matrices = { } <EOL> subterm_columns = <NUM_LIT:1> <EOL> for factor in term . factors : <EOL> if factor in num_column_counts : <EOL> subterm_factors . append ( factor ) <EOL> subterm_columns *= num_column_counts [ factor ] <EOL> elif factor in factor_coding : <EOL> subterm_factors . append ( factor ) <EOL> levels , contrast = cat_levels_contrasts [ factor ] <EOL> coded = code_contrast_matrix ( factor_coding [ factor ] , <EOL> levels , contrast , <EOL> default = Treatment ) <EOL> contrast_matrices [ factor ] = coded <EOL> subterm_columns *= coded . matrix . shape [ <NUM_LIT:1> ] <EOL> subterm_infos . append ( SubtermInfo ( subterm_factors , <EOL> contrast_matrices , <EOL> subterm_columns ) ) <EOL> term_to_subterm_infos [ term ] = subterm_infos <EOL> assert new_term_order == list ( term_to_subterm_infos ) <EOL> return term_to_subterm_infos <EOL> def design_matrix_builders ( termlists , data_iter_maker , eval_env , <EOL> NA_action = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> eval_env = EvalEnvironment . capture ( eval_env , reference = <NUM_LIT:1> ) <EOL> if isinstance ( NA_action , str ) : <EOL> NA_action = NAAction ( NA_action ) <EOL> all_factors = set ( ) <EOL> for termlist in termlists : <EOL> for term in termlist : <EOL> all_factors . update ( term . factors ) <EOL> factor_states = _factors_memorize ( all_factors , data_iter_maker , eval_env ) <EOL> ( num_column_counts , <EOL> cat_levels_contrasts ) = _examine_factor_types ( all_factors , <EOL> factor_states , <EOL> data_iter_maker , <EOL> NA_action ) <EOL> factor_infos = { } <EOL> for factor in all_factors : <EOL> if factor in num_column_counts : <EOL> fi = FactorInfo ( factor , <EOL> "<STR_LIT>" , <EOL> factor_states [ factor ] , <EOL> num_columns = num_column_counts [ factor ] , <EOL> categories = None ) <EOL> else : <EOL> assert factor in cat_levels_contrasts <EOL> categories = cat_levels_contrasts [ factor ] [ <NUM_LIT:0> ] <EOL> fi = FactorInfo ( factor , <EOL> "<STR_LIT>" , <EOL> factor_states [ factor ] , <EOL> num_columns = None , <EOL> categories = categories ) <EOL> factor_infos [ factor ] = fi <EOL> design_infos = [ ] <EOL> for termlist in termlists : <EOL> term_to_subterm_infos = _make_subterm_infos ( termlist , <EOL> num_column_counts , <EOL> cat_levels_contrasts ) <EOL> assert isinstance ( term_to_subterm_infos , OrderedDict ) <EOL> assert frozenset ( term_to_subterm_infos ) == frozenset ( termlist ) <EOL> this_design_factor_infos = { } <EOL> for term in termlist : <EOL> for factor in term . factors : <EOL> this_design_factor_infos [ factor ] = factor_infos [ factor ] <EOL> column_names = [ ] <EOL> for subterms in six . itervalues ( term_to_subterm_infos ) : <EOL> for subterm in subterms : <EOL> for column_name in _subterm_column_names_iter ( <EOL> factor_infos , subterm ) : <EOL> column_names . append ( column_name ) <EOL> design_infos . append ( DesignInfo ( column_names , <EOL> factor_infos = this_design_factor_infos , <EOL> term_codings = term_to_subterm_infos ) ) <EOL> return design_infos <EOL> def _build_design_matrix ( design_info , factor_info_to_values , dtype ) : <EOL> factor_to_values = { } <EOL> need_reshape = False <EOL> num_rows = None <EOL> for factor_info , value in six . iteritems ( factor_info_to_values ) : <EOL> if design_info . factor_infos . get ( factor_info . factor ) is not factor_info : <EOL> continue <EOL> factor_to_values [ factor_info . factor ] = value <EOL> if num_rows is not None : <EOL> assert num_rows == value . shape [ <NUM_LIT:0> ] <EOL> else : <EOL> num_rows = value . shape [ <NUM_LIT:0> ] <EOL> if num_rows is None : <EOL> num_rows = <NUM_LIT:1> <EOL> need_reshape = True <EOL> shape = ( num_rows , len ( design_info . column_names ) ) <EOL> m = DesignMatrix ( np . empty ( shape , dtype = dtype ) , design_info ) <EOL> start_column = <NUM_LIT:0> <EOL> for term , subterms in six . iteritems ( design_info . term_codings ) : <EOL> for subterm in subterms : <EOL> end_column = start_column + subterm . num_columns <EOL> m_slice = m [ : , start_column : end_column ] <EOL> _build_subterm ( subterm , design_info . factor_infos , <EOL> factor_to_values , m_slice ) <EOL> start_column = end_column <EOL> assert start_column == m . shape [ <NUM_LIT:1> ] <EOL> return need_reshape , m <EOL> class _CheckMatch ( object ) : <EOL> def __init__ ( self , name , eq_fn ) : <EOL> self . _name = name <EOL> self . _eq_fn = eq_fn <EOL> self . value = None <EOL> self . _value_desc = None <EOL> self . _value_origin = None <EOL> def check ( self , seen_value , desc , origin ) : <EOL> if self . value is None : <EOL> self . value = seen_value <EOL> self . _value_desc = desc <EOL> self . _value_origin = origin <EOL> else : <EOL> if not self . _eq_fn ( self . value , seen_value ) : <EOL> msg = ( "<STR_LIT>" <EOL> % ( self . _name , self . _value_desc , desc ) ) <EOL> if isinstance ( self . value , int ) : <EOL> msg += "<STR_LIT>" % ( self . value , seen_value ) <EOL> raise PatsyError ( msg , origin ) <EOL> def build_design_matrices ( design_infos , data , <EOL> NA_action = "<STR_LIT>" , <EOL> return_type = "<STR_LIT>" , <EOL> dtype = np . dtype ( float ) ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( NA_action , str ) : <EOL> NA_action = NAAction ( NA_action ) <EOL> if return_type == "<STR_LIT>" and not have_pandas : <EOL> raise PatsyError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if return_type not in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> raise PatsyError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( return_type , ) ) <EOL> factor_info_to_values = { } <EOL> factor_info_to_isNAs = { } <EOL> rows_checker = _CheckMatch ( "<STR_LIT>" , lambda a , b : a == b ) <EOL> index_checker = _CheckMatch ( "<STR_LIT>" , lambda a , b : a . equals ( b ) ) <EOL> if have_pandas and isinstance ( data , pandas . DataFrame ) : <EOL> index_checker . check ( data . index , "<STR_LIT>" , None ) <EOL> rows_checker . check ( data . shape [ <NUM_LIT:0> ] , "<STR_LIT>" , None ) <EOL> for design_info in design_infos : <EOL> for factor_info in six . itervalues ( design_info . factor_infos ) : <EOL> if factor_info not in factor_info_to_values : <EOL> value , is_NA = _eval_factor ( factor_info , data , NA_action ) <EOL> factor_info_to_isNAs [ factor_info ] = is_NA <EOL> name = factor_info . factor . name ( ) <EOL> origin = factor_info . factor . origin <EOL> rows_checker . check ( value . shape [ <NUM_LIT:0> ] , name , origin ) <EOL> if ( have_pandas <EOL> and isinstance ( value , ( pandas . Series , pandas . DataFrame ) ) ) : <EOL> index_checker . check ( value . index , name , origin ) <EOL> value = np . asarray ( value ) <EOL> factor_info_to_values [ factor_info ] = value <EOL> values = list ( factor_info_to_values . values ( ) ) <EOL> is_NAs = list ( factor_info_to_isNAs . values ( ) ) <EOL> origins = [ factor_info . factor . origin <EOL> for factor_info in factor_info_to_values ] <EOL> pandas_index = index_checker . value <EOL> num_rows = rows_checker . value <EOL> if return_type == "<STR_LIT>" and num_rows is not None : <EOL> if pandas_index is None : <EOL> pandas_index = np . arange ( num_rows ) <EOL> values . append ( pandas_index ) <EOL> is_NAs . append ( np . zeros ( len ( pandas_index ) , dtype = bool ) ) <EOL> origins . append ( None ) <EOL> new_values = NA_action . handle_NA ( values , is_NAs , origins ) <EOL> if new_values : <EOL> num_rows = new_values [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] <EOL> if return_type == "<STR_LIT>" and num_rows is not None : <EOL> pandas_index = new_values . pop ( ) <EOL> factor_info_to_values = dict ( zip ( factor_info_to_values , new_values ) ) <EOL> results = [ ] <EOL> for design_info in design_infos : <EOL> results . append ( _build_design_matrix ( design_info , <EOL> factor_info_to_values , <EOL> dtype ) ) <EOL> matrices = [ ] <EOL> for need_reshape , matrix in results : <EOL> if need_reshape : <EOL> assert matrix . shape [ <NUM_LIT:0> ] == <NUM_LIT:1> <EOL> if num_rows is not None : <EOL> matrix = DesignMatrix ( np . repeat ( matrix , num_rows , axis = <NUM_LIT:0> ) , <EOL> matrix . design_info ) <EOL> else : <EOL> raise PatsyError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> matrices . append ( matrix ) <EOL> if return_type == "<STR_LIT>" : <EOL> assert have_pandas <EOL> for i , matrix in enumerate ( matrices ) : <EOL> di = matrix . design_info <EOL> matrices [ i ] = pandas . DataFrame ( matrix , <EOL> columns = di . column_names , <EOL> index = pandas_index ) <EOL> matrices [ i ] . design_info = di <EOL> return matrices </s>
