modelparameters package¶
Submodules¶
modelparameters.codegeneration module¶
-
modelparameters.codegeneration.
latex_unit
(unit)[source]¶ Return sympified and LaTeX-formatted string describing given unit. E.g.: >>> LatexCodeGenerator.format_unit(“m/s**2”) ‘mathrm{frac{m}{s^{2}}}’
-
modelparameters.codegeneration.
ccode
(expr, assign_to=None, float_precision='double')[source]¶ Return a C-code representation of a sympy expression
-
modelparameters.codegeneration.
cppcode
(expr, assign_to=None, float_precision='double')[source]¶ Return a C++-code representation of a sympy expression
-
modelparameters.codegeneration.
pythoncode
(expr, assign_to=None, namespace='math')[source]¶ Return a Python-code representation of a sympy expression
-
modelparameters.codegeneration.
latex
(expr, **settings)[source]¶ Convert the given expression to LaTeX representation.
>>> from sympy import latex, pi, sin, asin, Integral, Matrix, Rational >>> from sympy.abc import x, y, mu, r, tau
>>> print(latex((2*tau)**Rational(7,2))) 8 \sqrt{2} \tau^{\frac{7}{2}}
Not using a print statement for printing, results in double backslashes for latex commands since that’s the way Python escapes backslashes in strings.
>>> latex((2*tau)**Rational(7,2)) '8 \\sqrt{2} \\tau^{\\frac{7}{2}}'
order: Any of the supported monomial orderings (currently “lex”, “grlex”, or “grevlex”), “old”, and “none”. This parameter does nothing for Mul objects. Setting order to “old” uses the compatibility ordering for Add defined in Printer. For very large expressions, set the ‘order’ keyword to ‘none’ if speed is a concern.
mode: Specifies how the generated code will be delimited. ‘mode’ can be one of ‘plain’, ‘inline’, ‘equation’ or ‘equation*’. If ‘mode’ is set to ‘plain’, then the resulting code will not be delimited at all (this is the default). If ‘mode’ is set to ‘inline’ then inline LaTeX $ $ will be used. If ‘mode’ is set to ‘equation’ or ‘equation*’, the resulting code will be enclosed in the ‘equation’ or ‘equation*’ environment (remember to import ‘amsmath’ for ‘equation*’), unless the ‘itex’ option is set. In the latter case, the
$$ $$
syntax is used.>>> print(latex((2*mu)**Rational(7,2), mode='plain')) 8 \sqrt{2} \mu^{\frac{7}{2}}
>>> print(latex((2*tau)**Rational(7,2), mode='inline')) $8 \sqrt{2} \tau^{7 / 2}$
>>> print(latex((2*mu)**Rational(7,2), mode='equation*')) \begin{equation*}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation*}
>>> print(latex((2*mu)**Rational(7,2), mode='equation')) \begin{equation}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation}
itex: Specifies if itex-specific syntax is used, including emitting
$$ $$
.>>> print(latex((2*mu)**Rational(7,2), mode='equation', itex=True)) $$8 \sqrt{2} \mu^{\frac{7}{2}}$$
fold_frac_powers: Emit “^{p/q}” instead of “^{frac{p}{q}}” for fractional powers.
>>> print(latex((2*tau)**Rational(7,2), fold_frac_powers=True)) 8 \sqrt{2} \tau^{7/2}
fold_func_brackets: Fold function brackets where applicable.
>>> print(latex((2*tau)**sin(Rational(7,2)))) \left(2 \tau\right)^{\sin{\left (\frac{7}{2} \right )}} >>> print(latex((2*tau)**sin(Rational(7,2)), fold_func_brackets = True)) \left(2 \tau\right)^{\sin {\frac{7}{2}}}
fold_short_frac: Emit “p / q” instead of “frac{p}{q}” when the denominator is simple enough (at most two terms and no powers). The default value is True for inline mode, False otherwise.
>>> print(latex(3*x**2/y)) \frac{3 x^{2}}{y} >>> print(latex(3*x**2/y, fold_short_frac=True)) 3 x^{2} / y
long_frac_ratio: The allowed ratio of the width of the numerator to the width of the denominator before we start breaking off long fractions. The default value is 2.
>>> print(latex(Integral(r, r)/2/pi, long_frac_ratio=2)) \frac{\int r\, dr}{2 \pi} >>> print(latex(Integral(r, r)/2/pi, long_frac_ratio=0)) \frac{1}{2 \pi} \int r\, dr
mul_symbol: The symbol to use for multiplication. Can be one of None, “ldot”, “dot”, or “times”.
>>> print(latex((2*tau)**sin(Rational(7,2)), mul_symbol="times")) \left(2 \times \tau\right)^{\sin{\left (\frac{7}{2} \right )}}
inv_trig_style: How inverse trig functions should be displayed. Can be one of “abbreviated”, “full”, or “power”. Defaults to “abbreviated”.
>>> print(latex(asin(Rational(7,2)))) \operatorname{asin}{\left (\frac{7}{2} \right )} >>> print(latex(asin(Rational(7,2)), inv_trig_style="full")) \arcsin{\left (\frac{7}{2} \right )} >>> print(latex(asin(Rational(7,2)), inv_trig_style="power")) \sin^{-1}{\left (\frac{7}{2} \right )}
mat_str: Which matrix environment string to emit. “smallmatrix”, “matrix”, “array”, etc. Defaults to “smallmatrix” for inline mode, “matrix” for matrices of no more than 10 columns, and “array” otherwise.
>>> print(latex(Matrix(2, 1, [x, y]))) \left[\begin{matrix}x\\y\end{matrix}\right]
>>> print(latex(Matrix(2, 1, [x, y]), mat_str = "array")) \left[\begin{array}{c}x\\y\end{array}\right]
mat_delim: The delimiter to wrap around matrices. Can be one of “[“, “(“, or the empty string. Defaults to “[“.
>>> print(latex(Matrix(2, 1, [x, y]), mat_delim="(")) \left(\begin{matrix}x\\y\end{matrix}\right)
symbol_names: Dictionary of symbols and the custom strings they should be emitted as.
>>> print(latex(x**2, symbol_names={x:'x_i'})) x_i^{2}
latex
also supports the builtin container types list, tuple, and dictionary.>>> print(latex([2/x, y], mode='inline')) $\left [ 2 / x, \quad y\right ]$
-
modelparameters.codegeneration.
octavecode
(expr, assign_to=None)¶
modelparameters.commands module¶
modelparameters.logger module¶
-
class
modelparameters.logger.
Logger
(name)[source]¶ Bases:
object
-
pop_log_level
()[source]¶ Pop log level from the level stack, reverting to before the last push_level.
-
modelparameters.parameterdict module¶
Contains the ParameterDict class, useful for defining recursive dictionaries of parameters and using attribute syntax for later access.
-
class
modelparameters.parameterdict.
Param
(value, name='', description='', **kwargs)[source]¶ Bases:
object
A simple type checking class for a single value
-
convert_to
(unit)[source]¶ Convert parameter to a different unit than the current one.
Parameters: unit (str) – The new unit Returns: Return the same prameter with the new unit Return type: Param Example
>>> p_s = ScalarParam(1.0, unit="s") >>> p_ms = p_s.convert_to('ms') >>> print('value = {}, unit = {}'.format(p_ms.value), p_ms.unit)) value = 1000.0, unit = 'milliseconds'
-
copy
(include_checkarg=True, include_name=True, include_description=True)[source]¶ Return a copy of the parameter
Parameters:
-
description
¶
-
format_data
(value=None, not_in=False, str_length=0)[source]¶ Print a nice formated version of the value and its range
Parameters:
-
name
¶
-
repr
(include_checkarg=True, include_name=True, include_description=True)[source]¶ Returns an executable version of the Param including optional arguments
Parameters:
-
value
¶ Return the value
-
-
class
modelparameters.parameterdict.
ScalarParam
(value, ge=None, le=None, gt=None, lt=None, unit='1', name='', description='')[source]¶ Bases:
modelparameters.parameters.Param
A simple type and range checking class for a scalar value
-
copy
(include_checkarg=True, include_name=True, include_description=True, include_unit=True)[source]¶ Return a copy of the parameter
Parameters:
-
name
¶
-
repr
(include_checkarg=True, include_name=True, include_description=True, include_unit=True)[source]¶ Returns an executable version of the Param including optional arguments
Parameters:
-
sym
¶
-
unit
¶ Return the unit
-
-
class
modelparameters.parameterdict.
OptionParam
(value, options, name='', description='')[source]¶ Bases:
modelparameters.parameters.Param
A simple type and options checking class for a single value
-
class
modelparameters.parameterdict.
ConstParam
(value, name='', description='')[source]¶ Bases:
modelparameters.parameters.Param
A Constant parameter which prevent any change of values
-
class
modelparameters.parameterdict.
ArrayParam
(value, size=None, ge=None, le=None, gt=None, lt=None, unit='1', name='', description='')[source]¶ Bases:
modelparameters.parameters.ScalarParam
A numpy Array based parameter
-
value
¶ Return the value
-
-
class
modelparameters.parameterdict.
SlaveParam
(expr, unit='1', name='', description='')[source]¶ Bases:
modelparameters.parameters.ScalarParam
A slave parameter defined by other parameters
-
expr
¶ Return the stored expression
-
format_data
(value=None, not_in=False, str_length=0)[source]¶ Print a nice formated version of the value and its range
-
value
¶ Return a computed value of the Parameters
-
-
class
modelparameters.parameterdict.
ParameterDict
(**params)[source]¶ Bases:
dict
A dictionary with attribute-style access, that maps attribute access to the real dictionary.
-
copy
(to_dict=False)[source]¶ Make a deep copy of self, including recursive copying of parameter subsets.
Parameters: to_dict (bool (optional)) – Return a dict with items representing the values of the Parameters
-
format_data
(indent=None)[source]¶ Make a recursive indented pretty-print string of self and parameter subsets.
-
iterparameterdicts
()[source]¶ Iterate over all ParameterDicts
Parameters: recurse (bool (optional)) – If True each encountered ParameterDict will also be entered
-
iterparams
(recurse=False)[source]¶ Iterate over all Param
Parameters: recurse (bool (optional)) – If True each encountered ParameterDict will be entered
-
optstr
()[source]¶ Return a string with option set
An option string can be sent to a script using a parameter dict to set its parameters from command line options
-
parse_args
(options=None, usage='')[source]¶ Parse a list of options. use sys.argv as default
Parameters: options (list of str (optional)) – List of options. By default sys.argv[1:] is used. This argument is mostly for debugging.
-
modelparameters.parameters module¶
-
modelparameters.parameters.
value_namespace
(expr, include_derivatives=False)[source]¶ Create a value name space for the included symbols in the expression
-
modelparameters.parameters.
symbols_from_expr
(expr, include_numbers=False, include_derivatives=False)[source]¶ Returns a set of all symbols of an expression
Parameters:
-
modelparameters.parameters.
symbol_to_param
(sym)[source]¶ Take a symbol or expression of symbols and returns the corresponding Parameters
-
modelparameters.parameters.
pythoncode
(expr, assign_to=None, namespace='math')[source]¶ Return a Python-code representation of a sympy expression
-
class
modelparameters.parameters.
Logger
(name)[source]¶ Bases:
object
-
pop_log_level
()[source]¶ Pop log level from the level stack, reverting to before the last push_level.
-
-
modelparameters.parameters.
check_arg
(arg, argtypes, num=-1, context=None, itemtypes=None, ge=None, le=None, gt=None, lt=None)[source]¶ Type check for positional arguments
Parameters: - arg (any) – The argument to be checked
- argtypes (type, tuple) – The type of which arg should be
- num (int (optional)) – The argument positional number
- context (type, function/method (optional)) – The context of the check. If context is a class the check is assumed to be during creation. If a function/method the contex is assumed to be a call to that function/method
- itemtypes (type (optional)) – If given argtypes must be a tuple or list and itemtypes forces each item to be a certain type
- ge (scalar (optional)) – Greater than or equal, range control of argument
- le (scalar (optional)) – Lesser than or equal, range control of argument
- gt (scalar (optional)) – Greater than, range control of argument
- lt (scalar (optional)) – Lesser than, range control of argument
-
modelparameters.parameters.
check_kwarg
(kwarg, name, argtypes, context=None, itemtypes=None, ge=None, le=None, gt=None, lt=None)[source]¶ Type check for keyword arguments
Parameters: - kwarg (any) – The keyword argument to be checked
- name (str) – The name of the keyword argument
- argtypes (type, tuple) – The type of which arg should be
- context (type, function/method (optional)) – The context of the check. If context is a class the check is assumed to be during creation. If a function/method the contex is assumed to be a call to that function/method
- itemtypes (type (optional)) – If given argtypes must be a tuple or list and itemtypes forces each item to be a certain type
- ge (scalar (optional)) – Greater than or equal, range control of argument
- le (scalar (optional)) – Lesser than or equal, range control of argument
- gt (scalar (optional)) – Greater than, range control of argument
- lt (scalar (optional)) – Lesser than, range control of argument
-
modelparameters.parameters.
value_formatter
(value, width=0)[source]¶ Return a formated string of a value
Parameters: - value (any) – The value which is formatted
- width (int) – A min str length value
-
class
modelparameters.parameters.
Range
(ge=None, le=None, gt=None, lt=None)[source]¶ Bases:
object
A simple class for helping checking a given value is within a certain range
-
format
(value, width=0)[source]¶ Return a formated range check of the value
Parameters: - value (scalar) – A value to be used in checking range
- width (int) – A min str length value
-
-
modelparameters.parameters.
tuplewrap
(arg)[source]¶ Wrap the argument to a tuple if it is not a tuple
-
class
modelparameters.parameters.
Param
(value, name='', description='', **kwargs)[source]¶ Bases:
object
A simple type checking class for a single value
-
convert_to
(unit)[source]¶ Convert parameter to a different unit than the current one.
Parameters: unit (str) – The new unit Returns: Return the same prameter with the new unit Return type: Param Example
>>> p_s = ScalarParam(1.0, unit="s") >>> p_ms = p_s.convert_to('ms') >>> print('value = {}, unit = {}'.format(p_ms.value), p_ms.unit)) value = 1000.0, unit = 'milliseconds'
-
copy
(include_checkarg=True, include_name=True, include_description=True)[source]¶ Return a copy of the parameter
Parameters:
-
description
¶
-
format_data
(value=None, not_in=False, str_length=0)[source]¶ Print a nice formated version of the value and its range
Parameters:
-
name
¶
-
repr
(include_checkarg=True, include_name=True, include_description=True)[source]¶ Returns an executable version of the Param including optional arguments
Parameters:
-
value
¶ Return the value
-
-
class
modelparameters.parameters.
OptionParam
(value, options, name='', description='')[source]¶ Bases:
modelparameters.parameters.Param
A simple type and options checking class for a single value
-
class
modelparameters.parameters.
ConstParam
(value, name='', description='')[source]¶ Bases:
modelparameters.parameters.Param
A Constant parameter which prevent any change of values
-
class
modelparameters.parameters.
TypelessParam
(value, name='', description='')[source]¶ Bases:
modelparameters.parameters.Param
A Typeless parameter allowing any change of value, including type changes
-
class
modelparameters.parameters.
ScalarParam
(value, ge=None, le=None, gt=None, lt=None, unit='1', name='', description='')[source]¶ Bases:
modelparameters.parameters.Param
A simple type and range checking class for a scalar value
-
copy
(include_checkarg=True, include_name=True, include_description=True, include_unit=True)[source]¶ Return a copy of the parameter
Parameters:
-
name
¶
-
repr
(include_checkarg=True, include_name=True, include_description=True, include_unit=True)[source]¶ Returns an executable version of the Param including optional arguments
Parameters:
-
sym
¶
-
unit
¶ Return the unit
-
-
class
modelparameters.parameters.
ArrayParam
(value, size=None, ge=None, le=None, gt=None, lt=None, unit='1', name='', description='')[source]¶ Bases:
modelparameters.parameters.ScalarParam
A numpy Array based parameter
-
value
¶ Return the value
-
-
class
modelparameters.parameters.
SlaveParam
(expr, unit='1', name='', description='')[source]¶ Bases:
modelparameters.parameters.ScalarParam
A slave parameter defined by other parameters
-
expr
¶ Return the stored expression
-
format_data
(value=None, not_in=False, str_length=0)[source]¶ Print a nice formated version of the value and its range
-
value
¶ Return a computed value of the Parameters
-
-
modelparameters.parameters.
eval_param_expr
(expr, param_ns=None, include_derivatives=False, ns=None)[source]¶ Eval an expression of symbols of ScalarParam
Parameters: - expr (expression of ParamSymbols) – The expression to be evaulated
- param_ns (dict (optional)) – A namespace containing the parameters for which the expr should be evaluated with.
- include_derivatives (bool (optional)) – If True not only symbols are evaulated but also derivatives
- ns (dict (optional)) – A namespace in which the expression will be evaluated in
modelparameters.sympytools module¶
-
modelparameters.sympytools.
check_arg
(arg, argtypes, num=-1, context=None, itemtypes=None, ge=None, le=None, gt=None, lt=None)[source]¶ Type check for positional arguments
Parameters: - arg (any) – The argument to be checked
- argtypes (type, tuple) – The type of which arg should be
- num (int (optional)) – The argument positional number
- context (type, function/method (optional)) – The context of the check. If context is a class the check is assumed to be during creation. If a function/method the contex is assumed to be a call to that function/method
- itemtypes (type (optional)) – If given argtypes must be a tuple or list and itemtypes forces each item to be a certain type
- ge (scalar (optional)) – Greater than or equal, range control of argument
- le (scalar (optional)) – Lesser than or equal, range control of argument
- gt (scalar (optional)) – Greater than, range control of argument
- lt (scalar (optional)) – Lesser than, range control of argument
-
modelparameters.sympytools.
deprecated
(func)[source]¶ This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.
-
modelparameters.sympytools.
Conditional
(cond, true_value, false_value)[source]¶ Declares a conditional
Parameters: - cond (A conditional) – The conditional which should be evaluated
- true_value (Any model expression) – Model expression for a true evaluation of the conditional
- false_value (Any model expression) – Model expression for a false evaluation of the conditional
-
modelparameters.sympytools.
ContinuousConditional
(cond, true_value, false_value, sigma=1.0)[source]¶ Declares a continuous conditional. Instead of a either or result the true and false values are weighted with a sigmoidal function which either evaluates to 0 or 1 instead of the true or false.
Parameters: - cond (An InEquality conditional) – An InEquality conditional which should be evaluated
- true_value (Any model expression) – Model expression for a true evaluation of the conditional
- false_value (Any model expression) – Model expression for a false evaluation of the conditional
- sigma (float (optional)) – Determines the sharpness of the sigmoidal function
-
modelparameters.sympytools.
symbol_to_param
(sym)[source]¶ Take a symbol or expression of symbols and returns the corresponding Parameters
-
modelparameters.sympytools.
symbols_from_expr
(expr, include_numbers=False, include_derivatives=False)[source]¶ Returns a set of all symbols of an expression
Parameters:
-
modelparameters.sympytools.
iter_symbol_params_from_expr
(expr)[source]¶ Return an iterator over sp.Symbols from expr
-
modelparameters.sympytools.
symbol_params_from_expr
(expr)[source]¶ Return a list of Symbols from expr
-
modelparameters.sympytools.
symbol_param_value_namespace
(expr)[source]¶ Create a value name space for the included symbols in the expression
modelparameters.utils module¶
-
modelparameters.utils.
ClassType
¶ alias of
builtins.type
-
class
modelparameters.utils.
Logger
(name)[source]¶ Bases:
object
-
pop_log_level
()[source]¶ Pop log level from the level stack, reverting to before the last push_level.
-
-
modelparameters.utils.
reduce
(function, sequence[, initial]) → value¶ Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.
-
modelparameters.utils.
VALUE_JUST
(s, *args, **kwargs)¶
-
modelparameters.utils.
value_formatter
(value, width=0)[source]¶ Return a formated string of a value
Parameters: - value (any) – The value which is formatted
- width (int) – A min str length value
-
class
modelparameters.utils.
Range
(ge=None, le=None, gt=None, lt=None)[source]¶ Bases:
object
A simple class for helping checking a given value is within a certain range
-
format
(value, width=0)[source]¶ Return a formated range check of the value
Parameters: - value (scalar) – A value to be used in checking range
- width (int) – A min str length value
-
-
modelparameters.utils.
format_time
(time)[source]¶ Return a formated version of the time argument
Parameters: time (float) – Time given in sections
-
modelparameters.utils.
is_iterable
(obj)[source]¶ Test for iterable
Parameters: obj (any) – Object which is beeing tested
-
modelparameters.utils.
check_arg
(arg, argtypes, num=-1, context=None, itemtypes=None, ge=None, le=None, gt=None, lt=None)[source]¶ Type check for positional arguments
Parameters: - arg (any) – The argument to be checked
- argtypes (type, tuple) – The type of which arg should be
- num (int (optional)) – The argument positional number
- context (type, function/method (optional)) – The context of the check. If context is a class the check is assumed to be during creation. If a function/method the contex is assumed to be a call to that function/method
- itemtypes (type (optional)) – If given argtypes must be a tuple or list and itemtypes forces each item to be a certain type
- ge (scalar (optional)) – Greater than or equal, range control of argument
- le (scalar (optional)) – Lesser than or equal, range control of argument
- gt (scalar (optional)) – Greater than, range control of argument
- lt (scalar (optional)) – Lesser than, range control of argument
-
modelparameters.utils.
check_kwarg
(kwarg, name, argtypes, context=None, itemtypes=None, ge=None, le=None, gt=None, lt=None)[source]¶ Type check for keyword arguments
Parameters: - kwarg (any) – The keyword argument to be checked
- name (str) – The name of the keyword argument
- argtypes (type, tuple) – The type of which arg should be
- context (type, function/method (optional)) – The context of the check. If context is a class the check is assumed to be during creation. If a function/method the contex is assumed to be a call to that function/method
- itemtypes (type (optional)) – If given argtypes must be a tuple or list and itemtypes forces each item to be a certain type
- ge (scalar (optional)) – Greater than or equal, range control of argument
- le (scalar (optional)) – Lesser than or equal, range control of argument
- gt (scalar (optional)) – Greater than, range control of argument
- lt (scalar (optional)) – Lesser than, range control of argument