55# http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm
66# http://www.ats.ucla.edu/stat/sas/webbooks/reg/chapter5/sasreg5.htm
77
8+ from __future__ import print_function
9+
810# These are made available in the patsy.* namespace
911__all__ = ["ContrastMatrix" , "Treatment" , "Poly" , "Sum" , "Helmert" , "Diff" ]
1012
1113import sys
14+ import six
1215import numpy as np
1316from patsy import PatsyError
1417from patsy .compat import triu_indices , tril_indices , diag_indices
@@ -37,7 +40,7 @@ def __init__(self, matrix, column_suffixes):
3740 self .matrix = np .asarray (matrix )
3841 self .column_suffixes = column_suffixes
3942 if self .matrix .shape [1 ] != len (column_suffixes ):
40- raise PatsyError , "matrix and column_suffixes don't conform"
43+ raise PatsyError ( "matrix and column_suffixes don't conform" )
4144
4245 __repr__ = repr_pretty_delegate
4346 def _repr_pretty_ (self , p , cycle ):
@@ -82,19 +85,16 @@ def t(obj, expected):
8285 t (1 , "1" )
8386 t (1.0 , "1.0" )
8487 t ("asdf" , "asdf" )
85- t (u "asdf" , "asdf" )
88+ t (six . u ( "asdf" ) , "asdf" )
8689 if sys .version_info >= (3 ,):
87- # a utf-8 encoded euro-sign comes out as a real euro sign. We have to
88- # use u""-style strings here, even though this is a py3-only block,
89- # because otherwise 2to3 may be clever enough to realize that in py2
90- # "\u20ac" produces a literal \, and double it for us when
91- # converting.
92- t (u"\u20ac " .encode ("utf-8" ), u"\u20ac " )
90+ # we can use "foo".encode here b/c this is python 3!
91+ # a utf-8 encoded euro-sign comes out as a real euro sign.
92+ t ("\u20ac " .encode ("utf-8" ), six .u ("\u20ac " ))
9393 # but a iso-8859-15 euro sign can't be decoded, and we fall back on
9494 # repr()
95- t (u "\u20ac " .encode ("iso-8859-15" ), "b'\\ xa4'" )
95+ t ("\u20ac " .encode ("iso-8859-15" ), "b'\\ xa4'" )
9696 else :
97- t (u "\u20ac " , "u'\\ u20ac'" )
97+ t (six . u ( "\u20ac " ) , "u'\\ u20ac'" )
9898
9999def _name_levels (prefix , levels ):
100100 return ["[%s%s]" % (prefix , _obj_to_readable_str (level )) for level in levels ]
@@ -115,7 +115,7 @@ def _get_level(levels, level_ref):
115115 raise PatsyError ("specified level %r is out of range"
116116 % (level_ref ,))
117117 return level_ref
118- raise PatsyError , "specified level %r not found" % (level_ref ,)
118+ raise PatsyError ( "specified level %r not found" % (level_ref ,) )
119119
120120def test__get_level ():
121121 assert _get_level (["a" , "b" , "c" ], 0 ) == 0
@@ -257,7 +257,7 @@ def _code_either(self, intercept, levels):
257257 # The constant term is always all 1's -- we don't normalize it.
258258 q [:, 0 ] = 1
259259 names = [".Constant" , ".Linear" , ".Quadratic" , ".Cubic" ]
260- names += ["^%s" % (i ,) for i in xrange (4 , n )]
260+ names += ["^%s" % (i ,) for i in range (4 , n )]
261261 names = names [:n ]
262262 if intercept :
263263 return ContrastMatrix (q , names )
@@ -280,12 +280,12 @@ def test_Poly():
280280 expected = [[1 , - 7.07106781186548e-01 , 0.408248290463863 ],
281281 [1 , 0 , - 0.816496580927726 ],
282282 [1 , 7.07106781186547e-01 , 0.408248290463863 ]]
283- print matrix .matrix
283+ print ( matrix .matrix )
284284 assert np .allclose (matrix .matrix , expected )
285285 matrix = t1 .code_without_intercept (["a" , "b" , "c" ])
286286 assert matrix .column_suffixes == [".Linear" , ".Quadratic" ]
287287 # Values from R 'options(digits=15); contr.poly(3)'
288- print matrix .matrix
288+ print ( matrix .matrix )
289289 assert np .allclose (matrix .matrix ,
290290 [[- 7.07106781186548e-01 , 0.408248290463863 ],
291291 [0 , - 0.816496580927726 ],
@@ -294,7 +294,7 @@ def test_Poly():
294294 matrix = Poly (scores = [0 , 10 , 11 ]).code_with_intercept (["a" , "b" , "c" ])
295295 assert matrix .column_suffixes == [".Constant" , ".Linear" , ".Quadratic" ]
296296 # Values from R 'options(digits=15); contr.poly(3, scores=c(0, 10, 11))'
297- print matrix .matrix
297+ print ( matrix .matrix )
298298 assert np .allclose (matrix .matrix ,
299299 [[1 , - 0.813733471206735 , 0.0671156055214024 ],
300300 [1 , 0.348742916231458 , - 0.7382716607354268 ],
@@ -305,13 +305,13 @@ def test_Poly():
305305 matrix = Poly (scores = [0 , 10 , 12 ]).code_with_intercept (["a" , "b" , "c" ])
306306 assert matrix .column_suffixes == [".Constant" , ".Linear" , ".Quadratic" ]
307307 # Values from R 'options(digits=15); contr.poly(3, scores=c(0, 10, 12))'
308- print matrix .matrix
308+ print ( matrix .matrix )
309309 assert np .allclose (matrix .matrix ,
310310 [[1 , - 0.806559132617443 , 0.127000127000191 ],
311311 [1 , 0.293294230042706 , - 0.762000762001143 ],
312312 [1 , 0.513264902574736 , 0.635000635000952 ]])
313313
314- matrix = t1 .code_with_intercept (range (6 ))
314+ matrix = t1 .code_with_intercept (list ( range (6 ) ))
315315 assert matrix .column_suffixes == [".Constant" , ".Linear" , ".Quadratic" ,
316316 ".Cubic" , "^4" , "^5" ]
317317
0 commit comments