Skip to content

Commit 9df3873

Browse files
committed
Merge branch 'master' into splines
2 parents 9d2666e + 559d3c7 commit 9df3873

File tree

5 files changed

+94
-65
lines changed

5 files changed

+94
-65
lines changed

patsy/builtins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def Q(name):
6767
6868
my_fit_function("y ~ Q('weight.in.kg')", ...)
6969
my_fit_function('y ~ Q("weight.in.kg")', ...)
70-
my_fit_function("y ~ Q(\"weight.in.kg\")", ...)
70+
my_fit_function("y ~ Q(\\"weight.in.kg\\")", ...)
7171
7272
Note also that ``Q`` is an ordinary Python function, which means that you
7373
can use it in more complex expressions. For example, this is a legal

patsy/desc.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from patsy.util import repr_pretty_delegate, repr_pretty_impl
1414

1515
# These are made available in the patsy.* namespace
16-
__all__ = ["Term", "ModelDesc", "INTERCEPT", "LookupFactor"]
16+
__all__ = ["Term", "ModelDesc", "INTERCEPT"]
1717

1818
# One might think it would make more sense for 'factors' to be a set, rather
1919
# than a tuple-with-guaranteed-unique-entries-that-compares-like-a-set. The
@@ -63,64 +63,6 @@ def name(self):
6363

6464
INTERCEPT = Term([])
6565

66-
class LookupFactor(object):
67-
"""A simple factor class that simply looks up a named entry in the given
68-
data.
69-
70-
Useful for programatically constructing formulas, and as a simple example
71-
of the factor protocol. For details see
72-
:ref:`expert-model-specification`.
73-
74-
Example::
75-
76-
dmatrix(ModelDesc([], [Term([LookupFactor("x")])]), {"x": [1, 2, 3]})
77-
"""
78-
def __init__(self, varname, origin=None):
79-
self._varname = varname
80-
self.origin = origin
81-
82-
def name(self):
83-
return self._varname
84-
85-
def __repr__(self):
86-
return "%s(%r)" % (self.__class__.__name__, self._varname)
87-
88-
def __eq__(self, other):
89-
return (isinstance(other, LookupFactor)
90-
and self._varname == other._varname)
91-
92-
def __ne__(self, other):
93-
return not self == other
94-
95-
def __hash__(self):
96-
return hash((LookupFactor, self._varname))
97-
98-
def memorize_passes_needed(self, state):
99-
return 0
100-
101-
def memorize_chunk(self, state, which_pass, env): # pragma: no cover
102-
assert False
103-
104-
def memorize_finish(self, state, which_pass): # pragma: no cover
105-
assert False
106-
107-
def eval(self, memorize_state, data):
108-
return data[self._varname]
109-
110-
def test_LookupFactor():
111-
l_a = LookupFactor("a")
112-
assert l_a.name() == "a"
113-
assert l_a == LookupFactor("a")
114-
assert l_a != LookupFactor("b")
115-
assert hash(l_a) == hash(LookupFactor("a"))
116-
assert hash(l_a) != hash(LookupFactor("b"))
117-
assert l_a.eval({}, {"a": 1}) == 1
118-
assert l_a.eval({}, {"a": 2}) == 2
119-
assert repr(l_a) == "LookupFactor('a')"
120-
assert l_a.origin is None
121-
l_with_origin = LookupFactor("b", origin="asdf")
122-
assert l_with_origin.origin == "asdf"
123-
12466
class _MockFactor(object):
12567
def __init__(self, name):
12668
self._name = name

patsy/test_build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
from patsy.util import (atleast_2d_column_default,
1414
have_pandas, have_pandas_categorical)
1515
from patsy.compat import itertools_product
16-
from patsy.desc import Term, INTERCEPT, LookupFactor
16+
from patsy.desc import Term, INTERCEPT
1717
from patsy.build import *
1818
from patsy.categorical import C
19-
from patsy.user_util import balanced
19+
from patsy.user_util import balanced, LookupFactor
2020
from patsy.design_info import DesignMatrix
2121

2222
if have_pandas:

patsy/test_highlevel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
from patsy import PatsyError
1212
from patsy.design_info import DesignMatrix
1313
from patsy.eval import EvalEnvironment
14-
from patsy.desc import ModelDesc, Term, LookupFactor, INTERCEPT
14+
from patsy.desc import ModelDesc, Term, INTERCEPT
1515
from patsy.categorical import C
1616
from patsy.contrasts import Helmert
17-
from patsy.user_util import balanced
17+
from patsy.user_util import balanced, LookupFactor
1818
from patsy.build import (design_matrix_builders,
1919
build_design_matrices,
2020
DesignMatrixBuilder)

patsy/user_util.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
# patsy.util, which is misc. utilities useful for implementing patsy).
77

88
# These are made available in the patsy.* namespace
9-
__all__ = ["balanced", "demo_data"]
9+
__all__ = ["balanced", "demo_data", "LookupFactor"]
1010

1111
import numpy as np
1212
from patsy import PatsyError
1313
from patsy.compat import itertools_product
14+
from patsy.categorical import C
1415

1516
def balanced(**kwargs):
1617
"""balanced(factor_name=num_levels, [factor_name=num_levels, ..., repeat=1])
@@ -136,3 +137,89 @@ def test_demo_data():
136137
from nose.tools import assert_raises
137138
assert_raises(PatsyError, demo_data, "a", "b", "__123")
138139
assert_raises(TypeError, demo_data, "a", "b", asdfasdf=123)
140+
141+
class LookupFactor(object):
142+
"""A simple factor class that simply looks up a named entry in the given
143+
data.
144+
145+
Useful for programatically constructing formulas, and as a simple example
146+
of the factor protocol. For details see
147+
:ref:`expert-model-specification`.
148+
149+
Example::
150+
151+
dmatrix(ModelDesc([], [Term([LookupFactor("x")])]), {"x": [1, 2, 3]})
152+
"""
153+
def __init__(self, varname,
154+
force_categorical=False, contrast=None, levels=None,
155+
origin=None):
156+
self._varname = varname
157+
self._force_categorical = force_categorical
158+
self._contrast = contrast
159+
self._levels = levels
160+
self.origin = origin
161+
if not self._force_categorical:
162+
if contrast is not None:
163+
raise ValueError("contrast= requires force_categorical=True")
164+
if levels is not None:
165+
raise ValueError("levels= requires force_categorical=True")
166+
167+
def name(self):
168+
return self._varname
169+
170+
def __repr__(self):
171+
return "%s(%r)" % (self.__class__.__name__, self._varname)
172+
173+
def __eq__(self, other):
174+
return (isinstance(other, LookupFactor)
175+
and self._varname == other._varname
176+
and self._force_categorical == other._force_categorical
177+
and self._contrast == other._contrast
178+
and self._levels == other._levels)
179+
180+
def __ne__(self, other):
181+
return not self == other
182+
183+
def __hash__(self):
184+
return hash((LookupFactor, self._varname,
185+
self._force_categorical, self._contrast, self._levels))
186+
187+
def memorize_passes_needed(self, state):
188+
return 0
189+
190+
def memorize_chunk(self, state, which_pass, env): # pragma: no cover
191+
assert False
192+
193+
def memorize_finish(self, state, which_pass): # pragma: no cover
194+
assert False
195+
196+
def eval(self, memorize_state, data):
197+
value = data[self._varname]
198+
if self._force_categorical:
199+
value = C(value, contrast=self._contrast, levels=self._levels)
200+
return value
201+
202+
def test_LookupFactor():
203+
l_a = LookupFactor("a")
204+
assert l_a.name() == "a"
205+
assert l_a == LookupFactor("a")
206+
assert l_a != LookupFactor("b")
207+
assert hash(l_a) == hash(LookupFactor("a"))
208+
assert hash(l_a) != hash(LookupFactor("b"))
209+
assert l_a.eval({}, {"a": 1}) == 1
210+
assert l_a.eval({}, {"a": 2}) == 2
211+
assert repr(l_a) == "LookupFactor('a')"
212+
assert l_a.origin is None
213+
l_with_origin = LookupFactor("b", origin="asdf")
214+
assert l_with_origin.origin == "asdf"
215+
216+
l_c = LookupFactor("c", force_categorical=True,
217+
contrast="CONTRAST", levels=(1, 2))
218+
box = l_c.eval({}, {"c": [1, 1, 2]})
219+
assert box.data == [1, 1, 2]
220+
assert box.contrast == "CONTRAST"
221+
assert box.levels == (1, 2)
222+
223+
from nose.tools import assert_raises
224+
assert_raises(ValueError, LookupFactor, "nc", contrast="CONTRAST")
225+
assert_raises(ValueError, LookupFactor, "nc", levels=(1, 2))

0 commit comments

Comments
 (0)