Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_termlist(*entries):
terms = []
for entry in entries:
terms.append(Term([LookupFactor(name) for name in entry]))
return terms
__repr__ = repr_pretty_delegate
def _repr_pretty_(self, p, cycle):
assert not cycle
repr_pretty_impl(p, self, [list(self.factors)])
def name(self):
"""Return a human-readable name for this term."""
if self.factors:
return ":".join([f.name() for f in self.factors])
else:
return "Intercept"
__getstate__ = no_pickling
INTERCEPT = Term([])
class _MockFactor(object):
def __init__(self, name):
self._name = name
def name(self):
return self._name
def test_Term():
assert Term([1, 2, 1]).factors == (1, 2)
assert Term([1, 2]) == Term([2, 1])
assert hash(Term([1, 2])) == hash(Term([2, 1]))
f1 = _MockFactor("a")
f2 = _MockFactor("b")
assert Term([f1, f2]).name() == "a:b"
assert Term([f2, f1]).name() == "b:a"
def _eval_python_expr(evaluator, tree):
factor = EvalFactor(tree.token.extra, origin=tree.origin)
return IntermediateExpr(False, None, False, [Term([factor])])
def _interaction(left_expr, right_expr):
for expr in (left_expr, right_expr):
_check_interactable(expr)
terms = []
for l_term in left_expr.terms:
for r_term in right_expr.terms:
terms.append(Term(l_term.factors + r_term.factors))
return IntermediateExpr(False, None, False, terms)
factor_codings_a = {f_a:
FactorInfo(f_a, "categorical", {},
categories=["a1", "a2"])}
term_codings_a_bad_rows = OrderedDict([
(t_a,
[SubtermInfo([f_a],
{f_a: ContrastMatrix(np.ones((3, 2)),
["[1]", "[2]"])},
2)])])
assert_raises(ValueError, DesignInfo,
["a[1]", "a[2]"],
factor_codings_a,
term_codings_a_bad_rows)
# have a contrast matrix for a non-categorical factor
t_ax = Term([f_a, f_x])
factor_codings_ax = {f_a:
FactorInfo(f_a, "categorical", {},
categories=["a1", "a2"]),
f_x:
FactorInfo(f_x, "numerical", {},
num_columns=2)}
term_codings_ax_extra_cm = OrderedDict([
(t_ax,
[SubtermInfo([f_a, f_x],
{f_a: ContrastMatrix(np.ones((2, 2)), ["[1]", "[2]"]),
f_x: ContrastMatrix(np.ones((2, 2)), ["[1]", "[2]"])},
4)])])
assert_raises(ValueError, DesignInfo,
["a[1]:x[1]", "a[2]:x[1]", "a[1]:x[2]", "a[2]:x[2]"],
factor_codings_ax,
term_codings_ax_extra_cm)
def test_ModelDesc_from_formula():
for input in ("y ~ x", parse_formula("y ~ x")):
md = ModelDesc.from_formula(input)
assert md.lhs_termlist == [Term([EvalFactor("y")]),]
assert md.rhs_termlist == [INTERCEPT, Term([EvalFactor("x")])]
def test_Term():
assert Term([1, 2, 1]).factors == (1, 2)
assert Term([1, 2]) == Term([2, 1])
assert hash(Term([1, 2])) == hash(Term([2, 1]))
f1 = _MockFactor("a")
f2 = _MockFactor("b")
assert Term([f1, f2]).name() == "a:b"
assert Term([f2, f1]).name() == "b:a"
assert Term([]).name() == "Intercept"
assert_no_pickling(Term([]))
def test_pick_contrasts_for_term():
from patsy.desc import Term
used = set()
codings = pick_contrasts_for_term(Term([]), set(), used)
assert codings == [{}]
codings = pick_contrasts_for_term(Term(["a", "x"]), set(["x"]), used)
assert codings == [{"a": False}]
codings = pick_contrasts_for_term(Term(["a", "b"]), set(), used)
assert codings == [{"a": True, "b": False}]
used_snapshot = set(used)
codings = pick_contrasts_for_term(Term(["c", "d"]), set(), used)
assert codings == [{"d": False}, {"c": False, "d": True}]
# Do it again backwards, to make sure we're deterministic with respect to
# order:
codings = pick_contrasts_for_term(Term(["d", "c"]), set(), used_snapshot)
assert codings == [{"c": False}, {"c": True, "d": False}]