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:
dictClass representing a parameter.
A
Parameteris simply adictwhere each key is a string and each value is aNumPy array. We call an item of the dictionary a parameter component.A
Parameterdiffers from an ordinarydictin the following ways:It is ensured that each value is a
NumPy array.We overwrite
copyto ensure that not only thedictbut also theNumPy arraysare copied.The
allclosemethod allows to compareParametersfor equality in a mathematically meaningful way.Each
Parameterhas asidproperty providing a unique state id.We override
__str__to ensure alphanumerical ordering of the keys and pretty printing of the values.The
parameter_typeproperty can be used to obtain theParameterTypeof the parameter.Use
from_parameter_typeto construct aParameterfrom aParameterTypeand user supplied input.
Parameters
- v
Anything that
dictaccepts for the construction of a dictionary.
Methods
allclose,clear,copy,from_parameter_type,fromkeys,pop,popitem,updatedictget,items,keys,setdefault,values,__contains__,__getitem__,__new__,__sizeof__Attributes
-
parameter_type¶ The
ParameterTypeof theParameter.
-
allclose(mu)[source]¶ Compare two
Parametersusingfloat_cmp_all.Parameters
- mu
The
Parameterwith which to compare.
Returns
Trueif bothParametershave the sameParameterTypeand all parameter components are almost equal, elseFalse.
-
classmethod
from_parameter_type(mu, parameter_type=None)[source]¶ Takes a user input
muand interprets it as aParameteraccording to the givenParameterType.Depending on the
ParameterType,mucan 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
ParameterTypew.r.t. whichmuis to be interpreted.
Returns
The resulting
Parameter.Raises
- ValueError
Is raised if
mucannot be interpreted as aParameterofParameterTypeparameter_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.OrderedDictClass 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
ParameterTypeand an ordinarydictis, thatParameterTypeorders its keys alphabetically.Parameters
- t
If
tis an object with aparameter_typeattribute, a copy of thisParameterTypeis created. Otherwise,tcan be anything from which adictcan be constructed.
Methods
copy,fromkeysclear,items,keys,move_to_end,pop,popitem,setdefault,update,values,__reversed__dictget,__contains__,__getitem__,__new__Attributes
sid
-
class
pymor.parameters.base.Parametric[source]¶ Bases:
objectMixin class for objects representing mathematical entities depending on a
Parameter.Each such object has a
ParameterTypestored in theparameter_typeattribute, which should be set by the implementor during__init__using thebuild_parameter_typemethod. Methods expecting theParameter(typicallyevaluate,apply,solve, etc. ..) should accept an optional argumentmudefaulting toNone. This argumentmushould then be fed intoparse_parameterto obtain aParameterof correctParameterTypefrom the (user supplied) inputmu.Attributes
-
parameter_type¶ The
ParameterTypeof theParametersthe object expects.
-
parameter_space¶ ParameterSpacethe parameters are expected to lie in orNone.
-
parametric¶ Trueif the object really depends on a parameter, i.e.parameter_typeis not empty.
-
build_parameter_type(*args, provides=None, **kwargs)[source]¶ Builds the
ParameterTypeof the object. Should be called by__init__.The
ParameterTypeof aParametricobject 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
ParameterTypeof a givenParametricobject) with the same name are treated as identical and are thus required to have the same shapes. The object’sParameterTypeis then made up by the shapes of all parameter components appearing.Additionally components of the resulting
ParameterTypecan be removed by specifying them via theprovidesparameter. 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
Parametricobject whoseparameter_typeis added.- kwargs
Each keyword argument is interpreted as parameter component with corresponding shape.
- provides
Dictof 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
muas aParameter.If
muis not already aParameter,Parameter.from_parameter_typeis used, to makemua parameter of the correctParameterType. Ifmuis already aParameter, it is checked if itsParameterTypematches our own. (It is actually allowed that theParameterTypeofmuis a superset of our ownParameterTypein the obvious sense.)Parameters
- mu
The input to parse as a
Parameter.
-
strip_parameter(mu)[source]¶ Remove all components of the
Parametermuwhich are not part of the object’sParameterType.Otherwise
strip_parameterbehaves 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.ParameterFunctionalInterfaceConjugate of a given
ParameterFunctionalEvaluates a given
ParameterFunctionaland returns the complex conjugate of the value.Parameters
- functional
The
ParameterFunctionalof 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.ParameterFunctionalInterfaceParameterFunctionalreturning 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.GenericParameterFunctionalTurns a Python expression given as a string into a
ParameterFunctional.Some
NumPyarithmetic functions likesin,log,minare supported. For a full list see thefunctionsclass attribute.Warning
evalis 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
ParameterTypeof theParametersthe 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.ParameterFunctionalInterfaceA wrapper making an arbitrary Python function a
ParameterFunctionalNote that a GenericParameterFunctional can only be
pickledif the function it is wrapping can be pickled. For this reason, it is usually preferable to useExpressionParameterFunctionalinstead ofGenericParameterFunctional.Parameters
- mapping
The function to wrap. The function has signature
mapping(mu).- parameter_type
The
ParameterTypeof theParametersthe functional expects.- name
The name of the functional.
- derivative_mappings
A dict containing all partial derivatives of each component and index in the
ParameterTypewith the signaturederivative_mappings[component][index](mu)
Attributes
-
class
pymor.parameters.functionals.ProductParameterFunctional(factors, name=None)[source]¶ Bases:
pymor.parameters.interfaces.ParameterFunctionalInterfaceForms the product of a list of
ParameterFunctionalsor numbers.Parameters
- factors
A list of
ParameterFunctionalsor numbers.- name
Name of the functional.
Attributes
-
class
pymor.parameters.functionals.ProjectionParameterFunctional(component_name, component_shape, index=(), name=None)[source]¶ Bases:
pymor.parameters.interfaces.ParameterFunctionalInterfaceParameterFunctionalreturning 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.ParametricInterface for
Parameterfunctionals.A parameter functional is simply a function mapping a
Parameterto a number.Attributes
-
class
pymor.parameters.interfaces.ParameterSpaceInterface[source]¶ Bases:
pymor.core.interfaces.ImmutableInterfaceInterface for
Parameterspaces.Attributes
-
parameter_type¶ ParameterTypeof the space.
-
spaces module¶
-
class
pymor.parameters.spaces.CubicParameterSpace(parameter_type, minimum=None, maximum=None, ranges=None)[source]¶ Bases:
pymor.parameters.interfaces.ParameterSpaceInterfaceSimple
ParameterSpacewhere each summand is an n-cube.Parameters
- parameter_type
The
ParameterTypeof the space.- minimum
The minimum for each matrix entry of each
Parametercomponent. Must beNoneifrangesis specified.- maximum
The maximum for each matrix entry of each
Parametercomponent. Must beNoneifrangesis specified.- ranges
dict whose keys agree with
parameter_typeand whose values are tuples (min, max) specifying the minimum and maximum of each matrix entry of correspondingParametercomponent. Must beNoneifminimumandmaximumare specified.
Methods
contains,parse_parameter,sample_randomly,sample_uniformlydisable_logging,enable_logging,has_interface_name,implementor_names,implementorsAttributes
-
sample_randomly(count=None, random_state=None, seed=None)[source]¶ Randomly sample
Parametersfrom the space.Parameters
- count
Noneor number of random parameters (see below).- random_state
RandomStateto use for sampling. IfNone, a new random state is generated usingseedas random seed, or thedefaultrandom state is used.- seed
If not
None, a new radom state with this seed is used.
Returns
If
countisNone, an inexhaustible iterator returning randomParameters. Otherwise a list ofcountrandomParameters.
-
sample_uniformly(counts)[source]¶ Uniformly sample
Parametersfrom the space.