pymor.parameters package¶
Submodules¶
base module¶
This module contains the implementation of pyMOR’s parameter handling facilities.
A Parameter
in pyMOR is basically a dict
of NumPy arrays
. Each item of the
dict is called a parameter component. The ParameterType
of a Parameter
is the dict
of the shapes of the parameter components, i.e.
mu.parameter_type['component'] == mu['component'].shape
Classes which represent mathematical objects depending on parameters, e.g. Functions
,
Operators
, Models
derive from the Parametric
mixin. Each Parametric
object has a parameter_type
attribute holding the ParameterType
of the Parameters
the object’s evaluate
, apply
, solve
, etc. methods expect.
Note that the ParameterType
of the given Parameter
is allowed to be a
superset of the object’s ParameterType
.
The ParameterType
of a Parametric
object is determined in its __init__
method by calling build_parameter_type
which computes the
ParameterType
as the union of the ParameterTypes
of the objects given to the
method. This way, e.g., an Operator
can inherit the ParameterTypes
of the
data functions it depends upon.
A Parametric
object can have a ParameterSpace
assigned to it by setting the
parameter_space
attribute (the ParameterType
of the space
has to agree with the ParameterType
of the object). The
parse_parameter
method parses a user input according to
the object’s ParameterType
to make it a Parameter
(e.g. if the ParameterType
consists of a single one-dimensional component, the user can simply supply a list
of numbers of the right length). Moreover, when given a Parameter
,
parse_parameter
ensures the Parameter
has an appropriate
ParameterType
.
-
class
pymor.parameters.base.
Parameter
(v)[source]¶ Bases:
dict
Class representing a parameter.
A
Parameter
is simply adict
where each key is a string and each value is aNumPy array
. We call an item of the dictionary a parameter component.A
Parameter
differs from an ordinarydict
in the following ways:It is ensured that each value is a
NumPy array
.We overwrite
copy
to ensure that not only thedict
but also theNumPy arrays
are copied.The
allclose
method allows to compareParameters
for equality in a mathematically meaningful way.Each
Parameter
has asid
property providing a unique state id.We override
__str__
to ensure alphanumerical ordering of the keys and pretty printing of the values.The
parameter_type
property can be used to obtain theParameterType
of the parameter.Use
from_parameter_type
to construct aParameter
from aParameterType
and user supplied input.
Parameters
- v
Anything that
dict
accepts for the construction of a dictionary.
Methods
allclose
,clear
,copy
,from_parameter_type
,fromkeys
,pop
,popitem
,update
dict
get
,items
,keys
,setdefault
,values
,__contains__
,__getitem__
,__new__
,__sizeof__
Attributes
-
parameter_type
¶ The
ParameterType
of theParameter
.
-
allclose
(mu)[source]¶ Compare two
Parameters
usingfloat_cmp_all
.Parameters
- mu
The
Parameter
with which to compare.
Returns
True
if bothParameters
have the sameParameterType
and all parameter components are almost equal, elseFalse
.
-
classmethod
from_parameter_type
(mu, parameter_type=None)[source]¶ Takes a user input
mu
and interprets it as aParameter
according to the givenParameterType
.Depending on the
ParameterType
,mu
can be given as aParameter
, dict, tuple, list, array or scalar.Parameters
- mu
The user input which shall be interpreted as a
Parameter
.- parameter_type
The
ParameterType
w.r.t. whichmu
is to be interpreted.
Returns
The resulting
Parameter
.Raises
- ValueError
Is raised if
mu
cannot be interpreted as aParameter
ofParameterType
parameter_type
.
-
fromkeys
(S, v=None)[source]¶ Create a new dictionary with keys from iterable and values set to value.
-
pop
(k[, d]) → v, remove specified key and return the corresponding value.[source]¶ If key is not found, d is returned if given, otherwise KeyError is raised
-
class
pymor.parameters.base.
ParameterType
(t)[source]¶ Bases:
collections.OrderedDict
Class representing a parameter type.
A parameter type is simply a dictionary with strings as keys and tuples of natural numbers as values. The keys are the names of the parameter components and the tuples their expected shape (compare
Parameter
).Apart from checking the correct format of its values, the only difference between a
ParameterType
and an ordinarydict
is, thatParameterType
orders its keys alphabetically.Parameters
- t
If
t
is an object with aparameter_type
attribute, a copy of thisParameterType
is created. Otherwise,t
can be anything from which adict
can be constructed.
Methods
copy
,fromkeys
clear
,items
,keys
,move_to_end
,pop
,popitem
,setdefault
,update
,values
,__reversed__
dict
get
,__contains__
,__getitem__
,__new__
Attributes
sid
-
class
pymor.parameters.base.
Parametric
[source]¶ Bases:
object
Mixin class for objects representing mathematical entities depending on a
Parameter
.Each such object has a
ParameterType
stored in theparameter_type
attribute, which should be set by the implementor during__init__
using thebuild_parameter_type
method. Methods expecting theParameter
(typicallyevaluate
,apply
,solve
, etc. ..) should accept an optional argumentmu
defaulting toNone
. This argumentmu
should then be fed intoparse_parameter
to obtain aParameter
of correctParameterType
from the (user supplied) inputmu
.Attributes
-
parameter_type
¶ The
ParameterType
of theParameters
the object expects.
-
parameter_space
¶ ParameterSpace
the parameters are expected to lie in orNone
.
-
parametric
¶ True
if the object really depends on a parameter, i.e.parameter_type
is not empty.
-
build_parameter_type
(*args, provides=None, **kwargs)[source]¶ Builds the
ParameterType
of the object. Should be called by__init__
.The
ParameterType
of aParametric
object is determined by the parameter components the object itself requires for evaluation, and by the parameter components required by the objects the object depends upon for evaluation.All parameter components (directly specified or inherited by the
ParameterType
of a givenParametric
object) with the same name are treated as identical and are thus required to have the same shapes. The object’sParameterType
is then made up by the shapes of all parameter components appearing.Additionally components of the resulting
ParameterType
can be removed by specifying them via theprovides
parameter. The idea is that the object itself may provide parameter components to the inherited objects which thus should not become part of the object’s own parameter type. (A typical application would beInstationaryModel
, which provides a time parameter component to its (time-dependent) operators during time-stepping.)Parameters
- args
Each positional argument must either be a dict of parameter components and shapes or a
Parametric
object whoseparameter_type
is added.- kwargs
Each keyword argument is interpreted as parameter component with corresponding shape.
- provides
Dict
of parameter component names and shapes which are provided by the object itself. The parameter components listed here will not become part of the object’sParameterType
.
-
parse_parameter
(mu)[source]¶ Interpret a user supplied parameter
mu
as aParameter
.If
mu
is not already aParameter
,Parameter.from_parameter_type
is used, to makemu
a parameter of the correctParameterType
. Ifmu
is already aParameter
, it is checked if itsParameterType
matches our own. (It is actually allowed that theParameterType
ofmu
is a superset of our ownParameterType
in the obvious sense.)Parameters
- mu
The input to parse as a
Parameter
.
-
strip_parameter
(mu)[source]¶ Remove all components of the
Parameter
mu
which are not part of the object’sParameterType
.Otherwise
strip_parameter
behaves likeparse_parameter
.This method is mainly useful for caching where the key should only contain the relevant parameter components.
-
functionals module¶
-
class
pymor.parameters.functionals.
ConjugateParameterFunctional
(functional, name=None)[source]¶ Bases:
pymor.parameters.interfaces.ParameterFunctionalInterface
Conjugate of a given
ParameterFunctional
Evaluates a given
ParameterFunctional
and returns the complex conjugate of the value.Parameters
- functional
The
ParameterFunctional
of which the complex conjuate is taken.- name
Name of the functional.
Attributes
-
class
pymor.parameters.functionals.
ConstantParameterFunctional
(constant_value, name=None)[source]¶ Bases:
pymor.parameters.interfaces.ParameterFunctionalInterface
ParameterFunctional
returning a constant value for each parameter.Parameters
- constant_value
value of the functional
- name
Name of the functional.
Attributes
-
class
pymor.parameters.functionals.
ExpressionParameterFunctional
(expression, parameter_type, name=None, derivative_expressions=None)[source]¶ Bases:
pymor.parameters.functionals.GenericParameterFunctional
Turns a Python expression given as a string into a
ParameterFunctional
.Some
NumPy
arithmetic functions likesin
,log
,min
are supported. For a full list see thefunctions
class attribute.Warning
eval
is used to evaluate the given expression. Using this class with expression strings from untrusted sources will cause mayhem and destruction!Parameters
- expression
A Python expression in the parameter components of the given
parameter_type
.- parameter_type
The
ParameterType
of theParameters
the functional expects.- name
The name of the functional.
- derivative_expressions
A dict containing a Python expression for the partial derivatives of each parameter component.
Attributes
functions
-
class
pymor.parameters.functionals.
GenericParameterFunctional
(mapping, parameter_type, name=None, derivative_mappings=None)[source]¶ Bases:
pymor.parameters.interfaces.ParameterFunctionalInterface
A wrapper making an arbitrary Python function a
ParameterFunctional
Note that a GenericParameterFunctional can only be
pickled
if the function it is wrapping can be pickled. For this reason, it is usually preferable to useExpressionParameterFunctional
instead ofGenericParameterFunctional
.Parameters
- mapping
The function to wrap. The function has signature
mapping(mu)
.- parameter_type
The
ParameterType
of theParameters
the functional expects.- name
The name of the functional.
- derivative_mappings
A dict containing all partial derivatives of each component and index in the
ParameterType
with the signaturederivative_mappings[component][index](mu)
Attributes
-
class
pymor.parameters.functionals.
ProductParameterFunctional
(factors, name=None)[source]¶ Bases:
pymor.parameters.interfaces.ParameterFunctionalInterface
Forms the product of a list of
ParameterFunctionals
or numbers.Parameters
- factors
A list of
ParameterFunctionals
or numbers.- name
Name of the functional.
Attributes
-
class
pymor.parameters.functionals.
ProjectionParameterFunctional
(component_name, component_shape, index=(), name=None)[source]¶ Bases:
pymor.parameters.interfaces.ParameterFunctionalInterface
ParameterFunctional
returning a component of the given parameter.For given parameter
mu
, this functional evaluates tomu[component_name][index]
Parameters
- component_name
The name of the parameter component to return.
- component_shape
The shape of the parameter component.
- index
See above.
- name
Name of the functional.
Attributes
interfaces module¶
-
class
pymor.parameters.interfaces.
ParameterFunctionalInterface
[source]¶ Bases:
pymor.core.interfaces.ImmutableInterface
,pymor.parameters.base.Parametric
Interface for
Parameter
functionals.A parameter functional is simply a function mapping a
Parameter
to a number.Attributes
-
class
pymor.parameters.interfaces.
ParameterSpaceInterface
[source]¶ Bases:
pymor.core.interfaces.ImmutableInterface
Interface for
Parameter
spaces.Attributes
-
parameter_type
¶ ParameterType
of the space.
-
spaces module¶
-
class
pymor.parameters.spaces.
CubicParameterSpace
(parameter_type, minimum=None, maximum=None, ranges=None)[source]¶ Bases:
pymor.parameters.interfaces.ParameterSpaceInterface
Simple
ParameterSpace
where each summand is an n-cube.Parameters
- parameter_type
The
ParameterType
of the space.- minimum
The minimum for each matrix entry of each
Parameter
component. Must beNone
ifranges
is specified.- maximum
The maximum for each matrix entry of each
Parameter
component. Must beNone
ifranges
is specified.- ranges
dict whose keys agree with
parameter_type
and whose values are tuples (min, max) specifying the minimum and maximum of each matrix entry of correspondingParameter
component. Must beNone
ifminimum
andmaximum
are specified.
Methods
contains
,parse_parameter
,sample_randomly
,sample_uniformly
disable_logging
,enable_logging
,has_interface_name
,implementor_names
,implementors
Attributes
-
sample_randomly
(count=None, random_state=None, seed=None)[source]¶ Randomly sample
Parameters
from the space.Parameters
- count
None
or number of random parameters (see below).- random_state
RandomState
to use for sampling. IfNone
, a new random state is generated usingseed
as random seed, or thedefault
random state is used.- seed
If not
None
, a new radom state with this seed is used.
Returns
If
count
isNone
, an inexhaustible iterator returning randomParameters
. Otherwise a list ofcount
randomParameters
.
-
sample_uniformly
(counts)[source]¶ Uniformly sample
Parameters
from the space.