Skip to content

Commit a79c5c8

Browse files
committed
Improve test coverage
1 parent ac612d0 commit a79c5c8

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

patsy/design_info.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,11 @@ def from_array(cls, array_like, default_column_prefix="column"):
774774

775775
__getstate__ = no_pickling
776776

777+
777778
def test_DesignInfo():
778779
from nose.tools import assert_raises
780+
from patsy.eval import EvalEnvironment
781+
779782
class _MockFactor(object):
780783
def __init__(self, name):
781784
self._name = name
@@ -821,6 +824,8 @@ def var_names(self, eval_env=0):
821824
repr(di)
822825

823826
assert di.var_names() == {'x_var', 'y_var'}
827+
eval_env = EvalEnvironment.capture(0)
828+
assert di.var_names(eval_env) == {'x_var', 'y_var'}
824829

825830
assert_no_pickling(di)
826831

@@ -844,6 +849,8 @@ def var_names(self, eval_env=0):
844849
assert di.slice("b") == slice(3, 4)
845850

846851
assert di.var_names() == {}
852+
eval_env = EvalEnvironment.capture(0)
853+
assert di.var_names(eval_env) == {}
847854

848855
# Check intercept handling in describe()
849856
assert DesignInfo(["Intercept", "a", "b"]).describe() == "1 + a + b"
@@ -1291,6 +1298,8 @@ def test_design_matrix():
12911298
def test_DesignInfo_partial():
12921299
from .highlevel import dmatrix
12931300
from numpy.testing import assert_allclose
1301+
from patsy.eval import EvalEnvironment
1302+
eval_env = EvalEnvironment.capture(0)
12941303
a = np.array(['a', 'b', 'a', 'b', 'a', 'a', 'b', 'a'])
12951304
b = np.array([1, 3, 2, 4, 1, 3, 1, 1])
12961305
c = np.array([4, 3, 2, 1, 6, 4, 2, 1])
@@ -1299,13 +1308,17 @@ def test_DesignInfo_partial():
12991308
x[1, 1] = 1
13001309
y = dm.design_info.partial({'a': ['a', 'b', 'a']})
13011310
assert_allclose(x, y)
1311+
y = dm.design_info.partial({'a': ['a', 'b', 'a']}, eval_env=eval_env)
1312+
assert_allclose(x, y)
13021313

13031314
x = np.zeros((2, 6))
13041315
x[1, 1] = 1
13051316
x[1, 5] = np.log(3)
13061317
p = OrderedDict([('a', ['a', 'b']), ('c', [1, 3])])
13071318
y = dm.design_info.partial(p)
13081319
assert_allclose(x, y)
1320+
y = dm.design_info.partial(p, eval_env=eval_env)
1321+
assert_allclose(x, y)
13091322

13101323
x = np.zeros((4, 6))
13111324
x[2, 1] = 1

patsy/eval.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,14 @@ class bar(object):
730730
bah = stateful_transform(lambda: "BAH-OBJ")
731731
eval_env = EvalEnvironment.capture(0)
732732
e = EvalFactor('foo(a) + bar.qux(b) + zed(bah(c))+ d')
733+
state = {}
734+
eval_env = EvalEnvironment.capture(0)
735+
passes = e.memorize_passes_needed(state, eval_env)
736+
print(passes)
737+
print(state)
738+
assert passes == 2
739+
for name in ["foo", "bah", "zed"]:
740+
assert state["eval_env"].namespace[name] is locals()[name]
733741
assert e.var_names(eval_env=eval_env) == {'a', 'b', 'c', 'd'}
734742

735743

patsy/test_build.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def assert_full_rank(m):
3131
u, s, v = np.linalg.svd(m)
3232
rank = np.sum(s > 1e-10)
3333
assert rank == m.shape[1]
34-
34+
3535
def test_assert_full_rank():
3636
assert_full_rank(np.eye(10))
3737
assert_full_rank([[1, 0], [1, 0], [1, 0], [1, 1]])
@@ -44,7 +44,7 @@ def test_assert_full_rank():
4444
# col1 + col2 = col3
4545
assert_raises(AssertionError,
4646
assert_full_rank, [[1, 2, 3], [1, 5, 6], [1, 6, 7]])
47-
47+
4848
def make_termlist(*entries):
4949
terms = []
5050
for entry in entries:
@@ -116,11 +116,11 @@ def test_simple():
116116
[1, 0, x1[1], 0],
117117
[0, 1, x1[2], x1[2]],
118118
[0, 1, x1[3], x1[3]]])
119-
119+
120120
m = make_matrix(data, 3, [["x1"], ["x2"], ["x2", "x1"]],
121121
column_names=["x1", "x2", "x2:x1"])
122122
assert np.allclose(m, np.column_stack((x1, x2, x1 * x2)))
123-
123+
124124
def test_R_bugs():
125125
data = balanced(a=2, b=2, c=2)
126126
data["x"] = np.linspace(0, 1, len(data["a"]))
@@ -253,7 +253,7 @@ def test_return_type():
253253
def iter_maker():
254254
yield data
255255
builder = design_matrix_builders([make_termlist("x")], iter_maker, 0)[0]
256-
256+
257257
# Check explicitly passing return_type="matrix" works
258258
mat = build_design_matrices([builder], data, return_type="matrix")[0]
259259
assert isinstance(mat, DesignMatrix)
@@ -298,7 +298,7 @@ def iter_maker():
298298
assert mat.shape == (2, 3)
299299
# According to this (and only this) function, NaN == NaN.
300300
np.testing.assert_array_equal(mat, [[1.0, 0.0, 10.0], [0.0, 1.0, np.nan]])
301-
301+
302302
# NA_action="raise"
303303
assert_raises(PatsyError,
304304
build_design_matrices,
@@ -596,7 +596,7 @@ def iter_maker():
596596
def test_contrast():
597597
from patsy.contrasts import ContrastMatrix, Sum
598598
values = ["a1", "a3", "a1", "a2"]
599-
599+
600600
# No intercept in model, full-rank coding of 'a'
601601
m = make_matrix({"a": C(values)}, 3, [["a"]],
602602
column_names=["a[a1]", "a[a2]", "a[a3]"])
@@ -605,7 +605,7 @@ def test_contrast():
605605
[0, 0, 1],
606606
[1, 0, 0],
607607
[0, 1, 0]])
608-
608+
609609
for s in (Sum, Sum()):
610610
m = make_matrix({"a": C(values, s)}, 3, [["a"]],
611611
column_names=["a[mean]", "a[S.a1]", "a[S.a2]"])
@@ -614,7 +614,7 @@ def test_contrast():
614614
[1,-1, -1],
615615
[1, 1, 0],
616616
[1, 0, 1]])
617-
617+
618618
m = make_matrix({"a": C(values, Sum(omit=0))}, 3, [["a"]],
619619
column_names=["a[mean]", "a[S.a2]", "a[S.a3]"])
620620
# Output from R
@@ -631,7 +631,7 @@ def test_contrast():
631631
[1, 0, 1],
632632
[1, 0, 0],
633633
[1, 1, 0]])
634-
634+
635635
for s in (Sum, Sum()):
636636
m = make_matrix({"a": C(values, s)}, 3, [[], ["a"]],
637637
column_names=["Intercept", "a[S.a1]", "a[S.a2]"])
@@ -640,7 +640,7 @@ def test_contrast():
640640
[1,-1, -1],
641641
[1, 1, 0],
642642
[1, 0, 1]])
643-
643+
644644
m = make_matrix({"a": C(values, Sum(omit=0))}, 3, [[], ["a"]],
645645
column_names=["Intercept", "a[S.a2]", "a[S.a3]"])
646646
# Output from R
@@ -747,9 +747,9 @@ def test_safe_data_maker():
747747
if not have_pandas:
748748
return
749749
from pandas.util.testing import assert_frame_equal
750-
data = pandas.DataFrame({'a': [1, 2, 3],
751-
'b': [4, 5, 6],
752-
'c': [7, 8, 9]})
750+
data = pandas.DataFrame({'a': [1, 2, 3, 4, 5, 6, 7, 8, 9],
751+
'b': [4, 5, 6, 7, 8, 9, 1, 2, 3],
752+
'c': [7, 8, 9, 1, 2, 3, 4, 5, 6]})
753753

754754
def iter_maker():
755755
for i in range(0, 3, 2):
@@ -758,7 +758,7 @@ def iter_maker():
758758
d2 = next(d)
759759
assert_frame_equal(d2, data.iloc[:2])
760760
d2 = next(d)
761-
assert_frame_equal(d2, data.iloc[2:])
761+
assert_frame_equal(d2, data.iloc[2: 4])
762762

763763
def iter_maker(var_names):
764764
for i in range(0, 3, 2):
@@ -767,4 +767,4 @@ def iter_maker(var_names):
767767
d2 = next(d)
768768
assert_frame_equal(d2, data[['a', 'b']].iloc[:2])
769769
d2 = next(d)
770-
assert_frame_equal(d2, data[['a', 'b']].iloc[2:])
770+
assert_frame_equal(d2, data[['a', 'b']].iloc[2: 4])

0 commit comments

Comments
 (0)