本文整理汇总了Python中patsy.PatsyError方法的典型用法代码示例。如果您正苦于以下问题:Python patsy.PatsyError方法的具体用法?Python patsy.PatsyError怎么用?Python patsy.PatsyError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类patsy
的用法示例。
在下文中一共展示了patsy.PatsyError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __setstate__
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def __setstate__(self, d):
if "restore_design_info" in d:
# NOTE: there may be a more performant way to do this
from patsy import dmatrices, PatsyError
exc = []
try:
data = d['frame']
except KeyError:
data = d['orig_endog'].join(d['orig_exog'])
for depth in [2, 3, 1, 0, 4]: # sequence is a guess where to likely find it
try:
_, design = dmatrices(d['formula'], data, eval_env=depth,
return_type='dataframe')
break
except (NameError, PatsyError) as e:
print('not in depth %d' % depth)
exc.append(e) # why do I need a reference from outside except block
pass
else:
raise exc[-1]
self.design_info = design.design_info
del d["restore_design_info"]
self.__dict__.update(d)
示例2: test_formula_missing_cat
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def test_formula_missing_cat():
# gh-805
import statsmodels.api as sm
from statsmodels.formula.api import ols
from patsy import PatsyError
dta = sm.datasets.grunfeld.load_pandas().data
dta.loc[dta.index[0], 'firm'] = np.nan
mod = ols(formula='value ~ invest + capital + firm + year',
data=dta.dropna())
res = mod.fit()
mod2 = ols(formula='value ~ invest + capital + firm + year',
data=dta)
res2 = mod2.fit()
assert_almost_equal(res.params.values, res2.params.values)
assert_raises(PatsyError, ols, 'value ~ invest + capital + firm + year',
data=dta, missing='raise')
示例3: eval
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def eval(self, tree, constraint=False):
key = (tree.type, len(tree.args))
assert key in self._dispatch
val = self._dispatch[key](tree)
if constraint:
# Force it to be a constraint
if isinstance(val, LinearConstraint):
return val
else:
assert val.size == self._N + 1
if np.all(val[:self._N] == 0):
raise PatsyError("term is constant, with no variables",
tree)
return LinearConstraint(self._variable_names,
val[:self._N],
-val[-1])
else:
# Force it to *not* be a constraint
if isinstance(val, LinearConstraint):
raise PatsyError("unexpected constraint object", tree)
return val
示例4: test_demo_data
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def test_demo_data():
d1 = demo_data("a", "b", "x")
assert sorted(d1.keys()) == ["a", "b", "x"]
assert d1["a"] == ["a1", "a1", "a2", "a2", "a1", "a1", "a2", "a2"]
assert d1["b"] == ["b1", "b2", "b1", "b2", "b1", "b2", "b1", "b2"]
assert d1["x"].dtype == np.dtype(float)
assert d1["x"].shape == (8,)
d2 = demo_data("x", "y")
assert sorted(d2.keys()) == ["x", "y"]
assert len(d2["x"]) == len(d2["y"]) == 5
assert len(demo_data("x", min_rows=10)["x"]) == 10
assert len(demo_data("a", "b", "x", min_rows=10)["x"]) == 12
assert len(demo_data("a", "b", "x", min_rows=10, nlevels=3)["x"]) == 18
from nose.tools import assert_raises
assert_raises(PatsyError, demo_data, "a", "b", "__123")
assert_raises(TypeError, demo_data, "a", "b", asdfasdf=123)
示例5: _eval_binary_power
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def _eval_binary_power(evaluator, tree):
left_expr = evaluator.eval(tree.args[0])
_check_interactable(left_expr)
power = -1
if tree.args[1].type in ("ONE", "NUMBER"):
expr = tree.args[1].token.extra
try:
power = int(expr)
except ValueError:
pass
if power < 1:
raise PatsyError("'**' requires a positive integer", tree.args[1])
all_terms = left_expr.terms
big_expr = left_expr
# Small optimization: (a + b)**100 is just the same as (a + b)**2.
power = min(len(left_expr.terms), power)
for i in range(1, power):
big_expr = _interaction(left_expr, big_expr)
all_terms = all_terms + big_expr.terms
return IntermediateExpr(False, None, False, all_terms)
示例6: eval
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def eval(self, tree, require_evalexpr=True):
result = None
assert isinstance(tree, ParseNode)
key = (tree.type, len(tree.args))
if key not in self._evaluators:
raise PatsyError("I don't know how to evaluate this "
"'%s' operator" % (tree.type,),
tree.token)
result = self._evaluators[key](self, tree)
if require_evalexpr and not isinstance(result, IntermediateExpr):
if isinstance(result, ModelDesc):
raise PatsyError("~ can only be used once, and "
"only at the top level",
tree)
else:
raise PatsyError("custom operator returned an "
"object that I don't know how to "
"handle", tree)
return result
#############
示例7: ast_names
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def ast_names(code):
"""Iterator that yields all the (ast) names in a Python expression.
:arg code: A string containing a Python expression.
"""
# Syntax that allows new name bindings to be introduced is tricky to
# handle here, so we just refuse to do so.
disallowed_ast_nodes = (ast.Lambda, ast.ListComp, ast.GeneratorExp)
if sys.version_info >= (2, 7):
disallowed_ast_nodes += (ast.DictComp, ast.SetComp)
for node in ast.walk(ast.parse(code)):
if isinstance(node, disallowed_ast_nodes):
raise PatsyError("Lambda, list/dict/set comprehension, generator "
"expression in patsy formula not currently supported.")
if isinstance(node, ast.Name):
yield node.id
示例8: call_and_wrap_exc
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def call_and_wrap_exc(msg, origin, f, *args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
if sys.version_info[0] >= 3:
new_exc = PatsyError("%s: %s: %s"
% (msg, e.__class__.__name__, e),
origin)
# Use 'exec' to hide this syntax from the Python 2 parser:
exec("raise new_exc from e")
else:
# In python 2, we just let the original exception escape -- better
# than destroying the traceback. But if it's a PatsyError, we can
# at least set the origin properly.
if isinstance(e, PatsyError):
e.set_origin(origin)
raise
示例9: test_ContrastMatrix
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def test_ContrastMatrix():
cm = ContrastMatrix([[1, 0], [0, 1]], ["a", "b"])
assert np.array_equal(cm.matrix, np.eye(2))
assert cm.column_suffixes == ["a", "b"]
# smoke test
repr(cm)
from nose.tools import assert_raises
assert_raises(PatsyError, ContrastMatrix, [[1], [0]], ["a", "b"])
assert_no_pickling(cm)
# This always produces an object of the type that Python calls 'str' (whether
# that be a Python 2 string-of-bytes or a Python 3 string-of-unicode). It does
# *not* make any particular guarantees about being reversible or having other
# such useful programmatic properties -- it just produces something that will
# be nice for users to look at.
示例10: _build_subterm
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def _build_subterm(subterm, factor_infos, factor_values, out):
assert subterm.num_columns == out.shape[1]
out[...] = 1
for i, column_idxs in enumerate(
_subterm_column_combinations(factor_infos, subterm)):
for factor, column_idx in zip(subterm.factors, column_idxs):
if factor_infos[factor].type == "categorical":
contrast = subterm.contrast_matrices[factor]
if np.any(factor_values[factor] < 0):
raise PatsyError("can't build a design matrix "
"containing missing values", factor)
out[:, i] *= contrast.matrix[factor_values[factor],
column_idx]
else:
assert factor_infos[factor].type == "numerical"
assert (factor_values[factor].shape[1]
== factor_infos[factor].num_columns)
out[:, i] *= factor_values[factor][:, column_idx]
示例11: dmatrices
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def dmatrices(formula_like, data={}, eval_env=0,
NA_action="drop", return_type="matrix"):
"""Construct two design matrices given a formula_like and data.
This function is identical to :func:`dmatrix`, except that it requires
(and returns) two matrices instead of one. By convention, the first matrix
is the "outcome" or "y" data, and the second is the "predictor" or "x"
data.
See :func:`dmatrix` for details.
"""
eval_env = EvalEnvironment.capture(eval_env, reference=1)
(lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
NA_action, return_type)
if lhs.shape[1] == 0:
raise PatsyError("model is missing required outcome variables")
return (lhs, rhs)
示例12: _parsing_error_test
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def _parsing_error_test(parse_fn, error_descs): # pragma: no cover
for error_desc in error_descs:
letters = []
start = None
end = None
for letter in error_desc:
if letter == "<":
start = len(letters)
elif letter == ">":
end = len(letters)
else:
letters.append(letter)
bad_code = "".join(letters)
assert start is not None and end is not None
print(error_desc)
print(repr(bad_code), start, end)
try:
parse_fn(bad_code)
except PatsyError as e:
print(e)
assert e.origin.code == bad_code
assert e.origin.start == start
assert e.origin.end == end
else:
assert False, "parser failed to report an error!"
示例13: _eval_binary_eq
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def _eval_binary_eq(self, tree):
# Handle "a1 = a2 = a3", which is parsed as "(a1 = a2) = a3"
args = list(tree.args)
constraints = []
for i, arg in enumerate(args):
if arg.type == "=":
constraints.append(self.eval(arg, constraint=True))
# make our left argument be their right argument, or
# vice-versa
args[i] = arg.args[1 - i]
left = self.eval(args[0])
right = self.eval(args[1])
coefs = left[:self._N] - right[:self._N]
if np.all(coefs == 0):
raise PatsyError("no variables appear in constraint", tree)
constant = -left[-1] + right[-1]
constraint = LinearConstraint(self._variable_names, coefs, constant)
constraints.append(constraint)
return LinearConstraint.combine(constraints)
示例14: _read_noun_context
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def _read_noun_context(token, c):
if token.type == Token.LPAREN:
if c.trace:
print("Pushing open-paren")
c.op_stack.append(_StackOperator(_open_paren, token))
return True
elif token.type in c.unary_ops:
if c.trace:
print("Pushing unary op %r" % (token.type,))
c.op_stack.append(_StackOperator(c.unary_ops[token.type], token))
return True
elif token.type in c.atomic_types:
if c.trace:
print("Pushing noun %r (%r)" % (token.type, token.extra))
c.noun_stack.append(ParseNode(token.type, token, [],
token.origin))
return False
else:
raise PatsyError("expected a noun, not '%s'"
% (token.origin.relevant_code(),),
token)
示例15: handle_NA
# 需要导入模块: import patsy [as 别名]
# 或者: from patsy import PatsyError [as 别名]
def handle_NA(self, values, is_NAs, origins):
"""Takes a set of factor values that may have NAs, and handles them
appropriately.
:arg values: A list of `ndarray` objects representing the data.
These may be 1- or 2-dimensional, and may be of varying dtype. All
will have the same number of rows (or entries, for 1-d arrays).
:arg is_NAs: A list with the same number of entries as `values`,
containing boolean `ndarray` objects that indicate which rows
contain NAs in the corresponding entry in `values`.
:arg origins: A list with the same number of entries as
`values`, containing information on the origin of each
value. If we encounter a problem with some particular value, we use
the corresponding entry in `origins` as the origin argument when
raising a :class:`PatsyError`.
:returns: A list of new values (which may have a differing number of
rows.)
"""
assert len(values) == len(is_NAs) == len(origins)
if len(values) == 0:
return values
if self.on_NA == "raise":
return self._handle_NA_raise(values, is_NAs, origins)
elif self.on_NA == "drop":
return self._handle_NA_drop(values, is_NAs, origins)
else: # pragma: no cover
assert False