本文整理汇总了Python中patsy.eval.EvalEnvironment类的典型用法代码示例。如果您正苦于以下问题:Python EvalEnvironment类的具体用法?Python EvalEnvironment怎么用?Python EvalEnvironment使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EvalEnvironment类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dmatrix
def dmatrix(formula_like, data={}, eval_env=0, return_type="matrix"):
"""Construct a single design matrix given a formula_like and data.
:arg formula_like: An object that can be used to construct a design
matrix. See below.
:arg data: A dict-like object that can be used to look up variables
referenced in `formula_like`.
:arg eval_env: Either a :class:`EvalEnvironment` which will be used to
look up any variables referenced in `formula_like` that cannot be
found in `data`, or else a depth represented as an
integer which will be passed to :meth:`EvalEnvironment.capture`.
``eval_env=0`` means to use the context of the function calling
:func:`dmatrix` for lookups. If calling this function from a library,
you probably want ``eval_env=1``, which means that variables should be
resolved in *your* caller's namespace.
:arg return_type: Either ``"matrix"`` or ``"dataframe"``. See below.
The `formula_like` can take a variety of forms:
* A formula string like "x1 + x2" (for :func:`dmatrix`) or "y ~ x1 + x2"
(for :func:`dmatrices`). For details see :ref:`formulas`.
* A :class:`ModelDesc`, which is a Python object representation of a
formula. See :ref:`formulas` and :ref:`expert-model-specification` for
details.
* A :class:`DesignMatrixBuilder`.
* An object that has a method called :meth:`__patsy_get_model_desc__`.
For details see :ref:`expert-model-specification`.
* A numpy array_like (for :func:`dmatrix`) or a tuple
(array_like, array_like) (for :func:`dmatrices`). These will have
metadata added, representation normalized, and then be returned
directly. In this case `data` and `eval_env` are
ignored. There is special handling for two cases:
* :class:`DesignMatrix` objects will have their :class:`DesignInfo`
preserved. This allows you to set up custom column names and term
information even if you aren't using the rest of the patsy
machinery.
* :class:`pandas.DataFrame` or :class:`pandas.Series` objects will have
their (row) indexes checked. If two are passed in, their indexes must
be aligned. If ``return_type="dataframe"``, then their indexes will be
preserved on the output.
Regardless of the input, the return type is always either:
* A :class:`DesignMatrix`, if ``return_type="matrix"`` (the default)
* A :class:`pandas.DataFrame`, if ``return_type="dataframe"``.
The actual contents of the design matrix is identical in both cases, and
in both cases a :class:`DesignInfo` will be available in a
``.design_info`` attribute on the return value. However, for
``return_type="dataframe"``, any pandas indexes on the input (either in
`data` or directly passed through `formula_like`) will be
preserved, which may be useful for e.g. time-series models.
"""
eval_env = EvalEnvironment.capture(eval_env, reference=1)
(lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env, return_type)
if lhs.shape[1] != 0:
raise PatsyError("encountered outcome variables for a model "
"that does not expect them")
return rhs
示例2: test_formula_factor_origin
def test_formula_factor_origin():
from patsy.origin import Origin
desc = ModelDesc.from_formula("a + b", EvalEnvironment.capture(0))
assert (desc.rhs_termlist[1].factors[0].origin
== Origin("a + b", 0, 1))
assert (desc.rhs_termlist[2].factors[0].origin
== Origin("a + b", 4, 5))
示例3: __init__
def __init__(self, mapping=None, data=None, environment=None):
# Allow some sloppiness
if (isinstance(mapping, pd.DataFrame) and
(data is None or isinstance(data, aes))):
mapping, data = data, mapping
if mapping is None:
mapping = aes()
if (data is not None and
not isinstance(data, pd.DataFrame)):
raise PlotnineError(
'data must be a dataframe or None if each '
'layer will have separate data.')
# Recognize plydata groups
if hasattr(data, 'group_indices') and 'group' not in mapping:
mapping = mapping.copy()
mapping['group'] = data.group_indices()
self.data = data
self.mapping = mapping
self.facet = facet_null()
self.labels = make_labels(mapping)
self.layers = Layers()
self.guides = guides()
self.scales = Scales()
self.theme = None
self.coordinates = coord_cartesian()
self.environment = environment or EvalEnvironment.capture(1)
self.layout = None
self.figure = None
self.watermarks = []
self.axs = None
示例4: __init__
def __init__(self, *args, **kwargs):
if args:
self.data = dict(zip(self.DEFAULT_ARGS, args))
else:
self.data = {}
if kwargs:
self.data.update(kwargs)
if 'colour' in self.data:
self.data['color'] = self.data['colour']
del self.data['colour']
self.__eval_env__ = EvalEnvironment.capture(1)
示例5: __init__
def __init__(self, *args, **kwargs):
if args:
self.data = dict(zip(self.DEFAULT_ARGS, args))
else:
self.data = {}
if kwargs:
self.data.update(kwargs)
if "colour" in self.data:
self.data["color"] = self.data["colour"]
del self.data["colour"]
if "linetype" in self.data:
self.data["linestyle"] = self.data["linetype"]
del self.data["linetype"]
self.__eval_env__ = EvalEnvironment.capture(1)
示例6: incr_dbuilders
def incr_dbuilders(formula_like, data_iter_maker, eval_env=0):
"""Construct two design matrix builders incrementally from a large data
set.
:func:`incr_dbuilders` is to :func:`incr_dbuilder` as :func:`dmatrices` is
to :func:`dmatrix`. See :func:`incr_dbuilder` for details.
"""
eval_env = EvalEnvironment.capture(eval_env, reference=1)
builders = _try_incr_builders(formula_like, data_iter_maker, eval_env)
if builders is None:
raise PatsyError("bad formula-like object")
if len(builders[0].design_info.column_names) == 0:
raise PatsyError("model is missing required outcome variables")
return builders
示例7: test_evalfactor_reraise
def test_evalfactor_reraise():
# From issue #11:
env = EvalEnvironment.capture()
data = {"X" : [0,1,2,3], "Y" : [1,2,3,4]}
formula = "C(X) + Y"
new_data = {"X" : [0,0,1,2,3,3,4], "Y" : [1,2,3,4,5,6,7]}
info = dmatrix(formula, data)
# This will produce a PatsyError, which is originally raised within the
# call to C() (which has no way to know where it is being called
# from). But EvalFactor should notice this, and add a useful origin:
try:
build_design_matrices([info.design_info.builder], new_data)
except PatsyError, e:
assert e.origin == Origin(formula, 0, 4)
示例8: _do_eval_formula_tests
def _do_eval_formula_tests(tests): # pragma: no cover
for code, result in six.iteritems(tests):
if len(result) == 2:
result = (False, []) + result
eval_env = EvalEnvironment.capture(0)
model_desc = ModelDesc.from_formula(code, eval_env)
print(repr(code))
print(result)
print(model_desc)
lhs_intercept, lhs_termlist, rhs_intercept, rhs_termlist = result
_assert_terms_match(model_desc.lhs_termlist,
lhs_intercept, lhs_termlist)
_assert_terms_match(model_desc.rhs_termlist,
rhs_intercept, rhs_termlist)
示例9: dmatrices
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)
示例10: _apply_transforms
def _apply_transforms(data, aes):
"""Adds columns from the aes included transformations
Possible transformations are "factor(<col>)" and
expressions which can be used with eval.
Parameters
----------
data : DataFrame
the original dataframe
aes : aesthetics
the aesthetic
Returns
-------
data : DateFrame
Transformed DataFrame
"""
data = data.copy()
for ae, name in aes.items():
if (isinstance(name, six.string_types) and (name not in data)):
# here we assume that it is a transformation
# if the mapping is to a single value (color="red"), this will be handled by pandas and
# assigned to the whole index. See also the last case in mapping building in get_layer!
from patsy.eval import EvalEnvironment
def factor(s, levels=None, labels=None):
# TODO: This factor implementation needs improvements...
# probably only gonna happen after https://github.com/pydata/pandas/issues/5313 is
# implemented in pandas ...
if levels or labels:
print("factor levels or labels are not yet implemented.")
return s.apply(str)
# use either the captured eval_env from aes or use the env one steps up
env = EvalEnvironment.capture(eval_env=(aes.__eval_env__ or 1))
# add factor as a special case
env.add_outer_namespace({"factor":factor})
try:
new_val = env.eval(name, inner_namespace=data)
except Exception as e:
msg = "Could not evaluate the '%s' mapping: '%s' (original error: %s)"
raise Exception(msg % (ae, name, str(e)))
try:
data[name] = new_val
except Exception as e:
msg = """The '%s' mapping: '%s' produced a value of type '%s', but only single items
and lists/arrays can be used. (original error: %s)"""
raise Exception(msg % (ae, name, str(type(new_val)), str(e)))
return data
示例11: _evaluate_aes_expressions
def _evaluate_aes_expressions(self):
"""
Evaluates patsy expressions within the aesthetics. For example, 'x + 1'
, 'factor(x)', or 'pd.cut(price, bins=10)')
"""
for key, item in self._aes.items():
if item not in self.data:
def factor(s, levels=None, labels=None):
return s.apply(str)
env = EvalEnvironment.capture(eval_env=(self._aes.__eval_env__ or 1)).with_outer_namespace({ "factor": factor, "pd": pd, "np": np })
try:
new_val = env.eval(item, inner_namespace=self.data)
self.data[item] = new_val
except:
pass
示例12: incr_dbuilder
def incr_dbuilder(formula_like, data_iter_maker, eval_env=0, NA_action="drop"):
"""Construct a design matrix builder incrementally from a large data set.
:arg formula_like: Similar to :func:`dmatrix`, except that explicit
matrices are not allowed. Must be a formula string, a
:class:`ModelDesc`, a :class:`DesignInfo`, or an object with a
``__patsy_get_model_desc__`` method.
:arg data_iter_maker: A zero-argument callable which returns an iterator
over dict-like data objects. This must be a callable rather than a
simple iterator because sufficiently complex formulas may require
multiple passes over the data (e.g. if there are nested stateful
transforms).
:arg eval_env: Either a :class:`EvalEnvironment` which will be used to
look up any variables referenced in `formula_like` that cannot be
found in `data`, or else a depth represented as an
integer which will be passed to :meth:`EvalEnvironment.capture`.
``eval_env=0`` means to use the context of the function calling
:func:`incr_dbuilder` for lookups. If calling this function from a
library, you probably want ``eval_env=1``, which means that variables
should be resolved in *your* caller's namespace.
:arg NA_action: An :class:`NAAction` object or string, used to determine
what values count as 'missing' for purposes of determining the levels of
categorical factors.
:returns: A :class:`DesignInfo`
Tip: for `data_iter_maker`, write a generator like::
def iter_maker():
for data_chunk in my_data_store:
yield data_chunk
and pass `iter_maker` (*not* `iter_maker()`).
.. versionadded:: 0.2.0
The ``NA_action`` argument.
"""
eval_env = EvalEnvironment.capture(eval_env, reference=1)
design_infos = _try_incr_builders(formula_like, data_iter_maker, eval_env,
NA_action)
if design_infos is None:
raise PatsyError("bad formula-like object")
if len(design_infos[0].column_names) > 0:
raise PatsyError("encountered outcome variables for a model "
"that does not expect them")
return design_infos[1]
示例13: Q
def Q(name):
"""A way to 'quote' variable names, especially ones that do not otherwise
meet Python's variable name rules.
If ``x`` is a variable, ``Q("x")`` returns the value of ``x``. (Note that
``Q`` takes the *string* ``"x"``, not the value of ``x`` itself.) This
works even if instead of ``x``, we have a variable name that would not
otherwise be legal in Python.
For example, if you have a column of data named `weight.in.kg`, then you
can't write::
y ~ weight.in.kg
because Python will try to find a variable named ``weight``, that has an
attribute named ``in``, that has an attribute named ``kg``. (And worse
yet, ``in`` is a reserved word, which makes this example doubly broken.)
Instead, write::
y ~ Q("weight.in.kg")
and all will be well. Note, though, that this requires embedding a Python
string inside your formula, which may require some care with your quote
marks. Some standard options include::
my_fit_function("y ~ Q('weight.in.kg')", ...)
my_fit_function('y ~ Q("weight.in.kg")', ...)
my_fit_function("y ~ Q(\\"weight.in.kg\\")", ...)
Note also that ``Q`` is an ordinary Python function, which means that you
can use it in more complex expressions. For example, this is a legal
formula::
y ~ np.sqrt(Q("weight.in.kg"))
"""
from patsy.eval import EvalEnvironment
env = EvalEnvironment.capture(1)
try:
return env.namespace[name]
except KeyError:
raise NameError, "no data named %r found" % (name,)
示例14: _evaluate_expressions
def _evaluate_expressions(self, data):
"""
Evaluates patsy expressions within the aesthetics. For example, 'x + 1'
, 'factor(x)', or 'pd.cut(price, bins=10)')
"""
for key, item in self.data.items():
if item not in data:
def factor(s, levels=None, labels=None):
return s.apply(str)
env = EvalEnvironment.capture(eval_env=(self.__eval_env__ or 1)).with_outer_namespace({ "factor": factor, "pd": pd, "np": np })
try:
new_val = env.eval(item, inner_namespace=data)
data[item] = new_val
except:
msg = "Invalid column: '%s'" % str(item)
matches = difflib.get_close_matches(item, data.columns)
msg += "\ndid you mean one of the following:\n"
for match in matches:
msg += " - %s\n" % match
raise Exception(msg)
return data
示例15: _get_env
def _get_env(eval_env):
if isinstance(eval_env, int):
# Here eval_env=0 refers to our caller's caller.
return EvalEnvironment.capture(eval_env + 2)
return eval_env