本文整理汇总了Python中funcsigs.Parameter方法的典型用法代码示例。如果您正苦于以下问题:Python funcsigs.Parameter方法的具体用法?Python funcsigs.Parameter怎么用?Python funcsigs.Parameter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类funcsigs
的用法示例。
在下文中一共展示了funcsigs.Parameter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: vars_as_functions
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def vars_as_functions(self):
"""
:return: Turn the keys of this model into
:class:`~sympy.core.function.Function`
objects. This is done recursively so the chain rule can be applied
correctly. This is done on the basis of `connectivity_mapping`.
Example: for ``{y: a * x, z: y**2 + a}`` this returns
``{y: y(x, a), z: z(y(x, a), a)}``.
"""
vars2functions = {}
key = lambda arg: [isinstance(arg, Parameter), str(arg)]
# Iterate over all symbols in this model in topological order, turning
# each one into a function object recursively.
for symbol in self.ordered_symbols:
if symbol in self.connectivity_mapping:
dependencies = self.connectivity_mapping[symbol]
# Replace the dependency by it's function if possible
dependencies = [vars2functions.get(dependency, dependency)
for dependency in dependencies]
# sort by vars first, then params, and alphabetically within
# each group
dependencies = sorted(dependencies, key=key)
vars2functions[symbol] = sympy.Function(symbol.name)(*dependencies)
return vars2functions
示例2: __str__
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def __str__(self):
"""
Printable representation of a Mapping model.
:return: str
"""
template = "{}({}; {}) = {}"
parts = []
for var, expr in self.items():
# Print every component as a function of only the dependencies it
# has. We can deduce these from the connectivity mapping.
params_sorted = sorted((x for x in self.connectivity_mapping[var]
if isinstance(x, Parameter)),
key=lambda x: x.name)
vars_sorted = sorted((x for x in self.connectivity_mapping[var]
if x not in params_sorted),
key=lambda x: x.name)
parts.append(template.format(
var,
', '.join([x.name for x in vars_sorted]),
', '.join([x.name for x in params_sorted]),
expr
)
)
return '[{}]'.format(",\n ".join(parts))
示例3: numerical_components
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def numerical_components(self):
"""
:return: lambda functions of each of the analytical components in
model_dict, to be used in numerical calculation.
"""
# All components must feature the independent vars and params, that's
# the API convention. But for those components which also contain
# interdependence, we add those vars
components = []
for var, expr in self.items():
dependencies = self.connectivity_mapping[var]
# vars first, then params, and alphabetically within each group
key = lambda arg: [isinstance(arg, Parameter), str(arg)]
ordered = sorted(dependencies, key=key)
components.append(sympy_to_py(expr, ordered))
return ModelOutput(self.keys(), components)
示例4: jacobian
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def jacobian(self):
"""
:return: Jacobian filled with the symbolic expressions for all the
partial derivatives. Partial derivatives are of the components of
the function with respect to the Parameter's, not the independent
Variable's. The return shape is a list over the models components,
filled with tha symbolical jacobian for that component, as a list.
"""
jac = []
for var, expr in self.items():
jac_row = []
for param in self.params:
partial_dv = D(var, param)
jac_row.append(self.jacobian_model[partial_dv])
jac.append(jac_row)
return jac
示例5: _make_parameters
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def _make_parameters(model, none_allowed=None):
"""
Based on a model, return the inspect.Parameter objects
needed to satisfy all the variables of this model.
:param model: instance of model
:param none_allowed: If provided, this has to be a sequence of
:class:`symfit.core.argument.Variable` whose values are set to
``None`` by default. If not provided, this will be set to sigma
variables only.
:return: list of :class:`inspect.Parameter` corresponding to all the
external variables of the model.
"""
if none_allowed is None:
none_allowed = model.sigmas.values()
parameters = [
inspect_sig.Parameter(
var.name,
kind=inspect_sig.Parameter.POSITIONAL_OR_KEYWORD,
default=None if var in none_allowed else inspect_sig.Parameter.empty
)
for var in model.vars
]
return parameters
示例6: get_callable_args
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def get_callable_args(function, required_only=False):
"""Get names of callable arguments.
Special arguments (like ``*args`` and ``**kwargs``) are not included into
output.
If required_only is True, optional arguments (with default values)
are not included into output.
"""
sig = get_signature(function)
function_args = list(six.iterkeys(sig.parameters))
for param_name, p in six.iteritems(sig.parameters):
if (p.kind in (Parameter.VAR_POSITIONAL, Parameter.VAR_KEYWORD) or
(required_only and p.default is not Parameter.empty)):
function_args.remove(param_name)
return function_args
示例7: _np_signature
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def _np_signature(f):
"""An enhanced funcsigs.signature that can handle numpy.ufunc."""
if not isinstance(f, np.ufunc):
return funcsigs.signature(f)
def names_from_num(prefix, n):
if n <= 0:
return []
elif n == 1:
return [prefix]
else:
return [prefix + str(i + 1) for i in range(n)]
input_names = names_from_num('x', f.nin)
output_names = names_from_num('out', f.nout)
keyword_only_params = [
('where', True),
('casting', 'same_kind'),
('order', 'K'),
('dtype', None),
('subok', True),
('signature', None),
('extobj', None)]
params = []
params += [funcsigs.Parameter(name, funcsigs.Parameter.POSITIONAL_ONLY)
for name in input_names]
if f.nout > 1:
params += [funcsigs.Parameter(name, funcsigs.Parameter.POSITIONAL_ONLY,
default=None)
for name in output_names]
params += [funcsigs.Parameter(
'out', funcsigs.Parameter.POSITIONAL_OR_KEYWORD,
default=None if f.nout == 1 else (None,) * f.nout)]
params += [funcsigs.Parameter(name, funcsigs.Parameter.KEYWORD_ONLY,
default=default)
for name, default in keyword_only_params]
return funcsigs.Signature(params)
# Python 2 doesn't allow keyword-only argument. Python prior to 3.8 doesn't
# allow positional-only argument. So we conflate positional-only, keyword-only
# and positional-or-keyword arguments here.
示例8: _is_compatible_param_kind
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def _is_compatible_param_kind(a, b):
def relax(k):
if k in (funcsigs.Parameter.POSITIONAL_ONLY,
funcsigs.Parameter.KEYWORD_ONLY):
return funcsigs.Parameter.POSITIONAL_OR_KEYWORD
return k
return relax(a) == relax(b)
示例9: __signature__
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def __signature__(self):
if hasattr(self, '__customsig'):
return self.__customsig
kwargs = []
for param in self.PARAMETERS:
kwargs.append(funcsigs.Parameter(param.name,
default=param.default,
kind=funcsigs.Parameter.POSITIONAL_OR_KEYWORD))
self.__customsig = funcsigs.Signature(kwargs, __validate_parameters__=True)
return self.__customsig
示例10: __init__
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def __init__(self, **kwonly_arguments):
self.kwonly_arguments = kwonly_arguments
# Mark which are required
self.required_keywords = {
kw for kw, value in kwonly_arguments.items() if value is RequiredKeyword
}
# Transform all into keywordonly inspect.Parameter objects.
self.keywordonly_parameters = OrderedDict(
(kw, inspect_sig.Parameter(kw,
kind=inspect_sig.Parameter.KEYWORD_ONLY,
default=value)
)
for kw, value in kwonly_arguments.items()
)
示例11: _init_from_dict
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def _init_from_dict(self, model_dict):
"""
Initiate self from a model_dict to make sure attributes such as vars, params are available.
Creates lists of alphabetically sorted independent vars, dependent vars, sigma vars, and parameters.
Finally it creates a signature for this model so it can be called nicely. This signature only contains
independent vars and params, as one would expect.
:param model_dict: dict of (dependent_var, expression) pairs.
"""
sort_func = lambda symbol: symbol.name
self.model_dict = OrderedDict(sorted(model_dict.items(),
key=lambda i: sort_func(i[0])))
# Everything at the bottom of the toposort is independent, at the top
# dependent, and the rest interdependent.
ordered = list(toposort(self.connectivity_mapping))
independent = sorted(ordered.pop(0), key=sort_func)
self.dependent_vars = sorted(ordered.pop(-1), key=sort_func)
self.interdependent_vars = sorted(
[item for items in ordered for item in items],
key=sort_func
)
# `independent` contains both params and vars, needs to be separated
self.independent_vars = [s for s in independent if
not isinstance(s, Parameter) and not s in self]
self.params = [s for s in independent if isinstance(s, Parameter)]
try:
assert not any(isinstance(var, Parameter)
for var in self.dependent_vars)
assert not any(isinstance(var, Parameter)
for var in self.interdependent_vars)
except AssertionError:
raise ModelError('`Parameter`\'s can not feature in the role '
'of `Variable`')
# Make Variable object corresponding to each depedent var.
self.sigmas = {var: Variable(name='sigma_{}'.format(var.name))
for var in self.dependent_vars}
示例12: hessian
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def hessian(self):
"""
:return: Hessian filled with the symbolic expressions for all the
second order partial derivatives. Partial derivatives are taken with
respect to the Parameter's, not the independent Variable's.
"""
return [[[sympy.diff(partial_dv, param) for param in self.params]
for partial_dv in comp] for comp in self.jacobian]
示例13: seperate_symbols
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def seperate_symbols(func):
"""
Seperate the symbols in symbolic function func. Return them in alphabetical
order.
:param func: scipy symbolic function.
:return: (vars, params), a tuple of all variables and parameters, each
sorted in alphabetical order.
:raises TypeError: only symfit Variable and Parameter are allowed, not sympy
Symbols.
"""
params = []
vars = []
for symbol in func.free_symbols:
if not isidentifier(str(symbol)):
continue # E.g. Indexed objects might print to A[i, j]
if isinstance(symbol, Parameter):
params.append(symbol)
elif isinstance(symbol, Idx):
# Idx objects are not seen as parameters or vars.
pass
elif isinstance(symbol, (MatrixExpr, Expr)):
vars.append(symbol)
else:
raise TypeError('model contains an unknown symbol type, {}'.format(type(symbol)))
for der in func.atoms(sympy.Derivative):
# Used by jacobians and hessians, where derivatives are treated as
# Variables. This way of writing it is purposefully discriminatory
# against derivatives wrt variables, since such derivatives should be
# performed explicitly in the case of jacs/hess, and are treated
# differently in the case of ODEModels.
if der.expr in vars and all(isinstance(s, Parameter) for s in der.variables):
vars.append(der)
params.sort(key=lambda symbol: symbol.name)
vars.sort(key=lambda symbol: symbol.name)
return vars, params
示例14: __init__
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def __init__(self, objective, parameters):
"""
:param objective: Objective function to be used.
:param parameters: List of :class:`~symfit.core.argument.Parameter` instances
"""
self.parameters = parameters
self._fixed_params = [p for p in parameters if p.fixed]
self.objective = self._baseobjective_from_callable(objective)
# Mapping which we use to track the original, to be used upon pickling
self._pickle_kwargs = {'parameters': parameters, 'objective': objective}
self.params = [p for p in parameters if not p.fixed]
示例15: accepts_kwargs
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import Parameter [as 别名]
def accepts_kwargs(function):
"""Returns ``True`` if function accepts kwargs otherwise ``False``."""
sig = get_signature(function)
return any(p.kind == Parameter.VAR_KEYWORD
for p in six.itervalues(sig.parameters))