parameters - encapsulates model parameters

Module for encapsulating model parameters.

Instances of the Parameters class represents a set of parameters, typically program input data.

Parameters object

The Parameters object can be used to represent the input parameters to your model. It works like a dict with attribute access, but has several additional features:

  • stores metadata about itself and the parameters, like:
    • the parameter object entity id
    • a description this parameter object
    • parameter types
    • parameter units
    • parameter descriptions
    • parameter grouping
    • parameter ordering
  • nesting - Parameters object values might themselves be Parameters objects interpreating dots in parameter names correctly

  • can be read from/written to file - support for new file formats can be added by subclassing ParametersFormat

  • has protection against overwriting existing values or setting invalid parameters through the mode argument

  • templating - by setting mode to “c”, a Parameters object will work as a template where values can be changed, but new parameters cannot be added/removed and existing types cannot be changed

  • automatic conversion between different template versions, through registration of the entityid and defining an EntityConverter

  • can be viewed and edited through an autogenerated ParEdit gui

For nesting and attribute access to work, parameter names should be valid Python identifiers not starting with underscore.

A basic example

An empty parameters object can be created with:

>>> from pario import parameters
>>> p = parameters.Parameters()

and later populated by hand by like:

>>> p._set('length', 4.0, 'm', doc='The length')

or from a file with the _update_fromstring() and_update_fromfile() methods, like:

>>> p._update_fromstring('5.0  m  height  # The height')

However, typically it will be created by reading a parameter file:

p = parameters.read('mydatafile')

The parameter values can later be accessed as attributes or indexing:

>>> p.length
4.0
>>> p['height']
5.0

If you e.g. want the length in cm you can do:

>>> p._get('length', 'cm')
400.0

Content

pario.parameters.Parameters([entityid, ...]) A dict-like class for encapsulating model parameters.
pario.parameters.read(fname[, fmt, ...]) Returns a new Parameters object from file.
pario.parameters.fromstring(string[, fmt, ...]) Returns a new Parameters object from string.
pario.parameters.getpar(fname, name[, fmt]) Returns the value of a given parameter from a parameter file.
pario.parameters.setpar(filename, name, value) Sets the value of a parameter in a parameter file.
pario.parameters.NotPermittedError Raised when trying to add or change a parameter not permitted by
class pario.parameters.Parameters(entityid=None, description='', mode='ac', **options)[source]

A dict-like class for encapsulating model parameters.

All parameters are available either as attributes (only those that are Python identifiers) or through standard key indexing. In order to avoid name conflicts, all methods are prefixed with underscore.

Parameters :

entityid : string | tuple

Identifies the parameter entity, i.e. what the parameters in this instance describes. Consists of a name and version. Examples: “MyProgram-1.0” or (‘MyProgram’, ‘1.0’).

description : string

Description of the parameter entity.

mode : string

Determines wheter parameters can be changed or added. Is a combination of ‘a’ (allow adding new parameters), ‘c’ (allow changing existing parameters) and ‘t’ (allow changing the type when assigning to an existing parameter).

options : keywords

Options. Common options includes:

default_group:The default group assigned to new parameters: string
default_format:Default format to use when writing: string

The options are also passed to the formatters. The default TextFormat uses e.g. the following options:

linesep:Line separator: “auto” | “LF” | “CRLF”
show_heading:Whether to show heading: bool
show_group_comments:
 Whether to show group comments: bool
converters:A sequence of type converters, or a comma-separated string with the names of the type converters

Options recognized by other modules:

casename:Set by pario.parcases.ParCases: string

Examples

>>> s = '''
... 2         -     model   model flag
... 1.52e10   m^-2  rho_i   dislocation density
... # a comment
... 0.3e-6    m     delta   subgrain size
... '''
>>> p = fromstring(s, fmt='txt', entityid='mymodel-0.1')
>>> print(p)
2                 -      model              model flag
15200000000.0     m^-2   rho_i              dislocation density
3e-07             m      delta              subgrain size
>>> p._names
['model', 'rho_i', 'delta']
>>> [p[name] for name in p._names]
[2, 15200000000.0, 3e-07]

Attributes

_entityid (parameter entity id)
_description (description of parameter entity)
_options (dict of options, set from keyword arguments)
_names (list of parameter names)
_values (dict mapping parameter names to their value)
_units (dict mapping parameter names to their unit)
_groups (dict mapping parameter names to group name)
_doc (dict mapping parameter names to documentation strings)
_mode (default mode for adding and changing parameters)

Methods

_set_mode(mode) Sets the current mode.
_get_mode() Returns the current mode.
_set_option(self, key, value): Sets option key to value.
_get_option(self, key, default=None) Returns value of option key. If no such option exists, default is returned.
_has_option(self, key) Returns whether an option with the given name exists.
_remove_option(self, key): Deletes the given option.
_get_names(group=None) Returns all parameter names for the given group.
_get_groups() Returns all groups.
_copy(names=None) Returns a copy of self.
_remove(name) Removes a parameter.
_set(name, value, unit, doc=None, group=None, mode=’ac’) Sets a parameter, adding it if it does not exists (and mode allows).
_get(name, unit=None) Returns a parameter value in the given unit.
_get_quantity(name, unit=None) Returns parameter name as a pint Qiantity in given unit.
_update(par, mode=None) Update parameters from other Parameters object.
_update_fromstring(self, string, fmt=’text’, mode=None, options) Update parameters from string.
_update_fromfile(self, fnaeme, fmt=None, mode=None, options) Update parameters from file.
_tostring(fmt, names=None, linesep=None) Returns a string representation of self in given format.
_write(fname, fmt=None, names=None, linesep=None) Write parameters to file.
_edit(names=None, group=None, title=’Parameters’, callback=None) Opens a graphical window for editing stored parameters.
_connect(callback, name, action) Register callback to be called when a parameter is changed.
_disconnect(callback, name, action) Unregister a callback.
_emit(name, action) Calls all callbacks connected to name and action.
_get_mode()[source]

Returns the current mode.

_set_mode(mode)[source]

Set current mode for allowing adding and changing parameters. It is a combination of ‘a’ (allow adding new parameters) and ‘c’ (allow changing existing parameters).

_get_names(group=None)[source]

Returns a list of parameter names. If group is None, all parameter names are returned. Else, if group is the empty string, the name of all ungrouped parameters is returned. Otherwise only the names in the given group is returned.

_copy(names=None, copy_callbacks=False)[source]

Returns a copy of self. If names is provided, only the given parameters are included in the copy.

_set(name, value, unit=None, ptype=None, group=None, doc=None, mode='ac')[source]

Add or set the value of a parameter.

Parameters :

name: string

Name of parameter to set.

value: bool | int | float | string | array

New value of the parameter.

unit: None | string

Parameter unit. Use None or “-” if not applicaple or dimensionless.

ptype : None | type | string

Explicitly set value to this type, where ptype is a type object or Python type name. If it is a string, it will be looked up in the local, global and buildin namespaces, in this order.

group: None | string

Parameter group. If None, the default group is determined from the “default_group” option.

doc: None | string

A description of the parameter.

mode : string

Determines wheter parameters can be changed or added. Is a combination of ‘a’ (allow adding new parameters), ‘c’ (allow changing existing parameters) and ‘t’ (allow changing the type when assigning to an existing parameter).

_get(name, unit=None)[source]

Returns the value of parameter name. If unit is provided, convert the returned value to the given unit.

_get_quantity(name, unit=None)

Returns parameter name as a pint Quantity. If unit is provided, convert the returned quantity to the given unit.

_update(par, mode=None)[source]

Update parameters. par can be another Parameters object, a mapping or a sequence of (key, value) tuples. If mode is not None, it overwrites the default mode.

_update_fromstring(string, fmt='text', mode=None, **options)[source]

Updates parameters from string.

fmt is the data format mode, if not None, overwrites the default mode options is passed to the format parser

_update_fromfile(fname, fmt=None, mode=None, **options)[source]

Updates parameters from file fname.

fmt is the data format mode, if not None, overwrites the default mode options is passed to the format parser

_tostring(fmt='text', names=None, **kw)[source]

Returns a string representation of self in given format.

The default is to return all names, but if names is given, then only the given names are returned.

The keyword arguments are passed to the tostring() method of the actual ParametersFormat subclass.

_write(fname, fmt=None, names=None, **kw)[source]

Writes self to file fname using given format.

The default is to return all names, but if names is given, then only the given names are returned.

The keyword arguments are passed to the write() method of the actual ParametersFormat subclass.

_edit(names=None, group=None, title='Parameters', header=True, callback=None, wait=True)[source]

Popup a graphical window for editing stored parameters.

See pario.paredit.paredit() for a description of the arguments and return value.

pario.parameters.read(fname, fmt=None, entityid=None, description=None, mode='c', **options)[source]

Returns a new Parameters object from file.

Parameters :

fname : string | file_like

File name or object to read from.

fmt : None | string

File format. If none it might be deduced from fname.

entityid : None | string | tuple

Identifies the parameter entity, i.e. what the parameters in this instance describes. Consists of a name and version. If None it might be derived from fname. Examples: “MyProgram-1.0” or (‘MyProgram’, ‘1.0’).

description : string

Description of the parameter entity.

mode : string

Determines wheter parameters can be changed or added. Is a combination of ‘a’ (allow adding new parameters), ‘c’ (allow changing existing parameters) and ‘t’ (allow changing the type when assigning to an existing parameter).

options : keywords

Additional keyword options. See the Parameters class docstring for details.

pario.parameters.fromstring(string, fmt='text', entityid=None, description=None, mode='c', **options)[source]

Returns a new Parameters object from string.

See read() for a description of the parameters.

pario.parameters.getpar(fname, name, fmt=None)[source]

Returns the value of a given parameter from a parameter file.

Parameters :

fname : string | file-like

The file to read the parameter from.

name : string

Name of the parameter to return.

fmt : None | string

File format. If none it might be deduced from fname.

pario.parameters.setpar(filename, name, value, fmt=None, **options)[source]

Sets the value of a parameter in a parameter file.

Parameters :

filename : string

The file to modify.

name : string

Name of the parameter to set.

value : correct type for this parameter

New value.

fmt : None | string

File format. If none it might be deduced from fname.

options : keywords

Keywords passed to the writer for the given format.

class pario.parameters.NotPermittedError[source]

Raised when trying to add or change a parameter not permitted by the mode.

EntityConverter object

Since Parameters objects knows so much about themselves, it is possible to convert between different versions of your input file or make rules for generating input to another program based on some input files and the output from your program. This functionality is supported through the pario.parameters.EntityConverter class.

See the class documentation below for an example.

If the only changes between two versions of an input file is one of:

  • reordering of the parameters
  • change of units
  • adding a new parameter with a default value

you can use the pario.parameters.simple_converter() to generate a converter. Other you have to subclass pario.parameters.EntityConverter .

Content

pario.parameters.EntityConverter(ereg) Class for converting between parameter entities.
pario.parameters.ConversionError Error in convertion between parameter entities.
pario.parameters.simple_converter(name, ...) A factory function that returns a new converter class that
class pario.parameters.EntityConverter(ereg)[source]

Class for converting between parameter entities.

Parameters :

ereg : EntityRegistry intance

Registry with all known parameter entities.

Notes

Subclasses are expected to:
  • implement a converter() method. It should take a set of Parameter instances as input and return a new Parameter instance of the entity type given by the target attribute.
  • define the sources attribute. It should be a list of entityid’s corresponding to the arguments of the converter() method
  • define the target attribute. It should be an entityid corresponding to return value of the converter() method.

Examples

# Define some converters

>>> class A2fromA1Converter(EntityConverter):
...     sources = [('A', '1'), ]
...     target = ('A', '2')
...     def converter(self, a1):
...         self.check_arguments(a1)
...         return self.updated_copy(self.target, a1)
>>> class B2fromB1_A2Converter(EntityConverter):
...     sources = [('B', '1'), ('A', '2')]
...     target = ('B', '2')
...     def converter(self, b1, a2):
...         self.check_arguments(b1, a2)
...         b2 = self.updated_copy(self.target, b1)
...         b2.area = a2.length * a2.height
...         return b2

# Define some parameter templates

>>> a1 = Parameters('A-1')
>>> a1._set('length', 1.0, 'm')
>>> a1._set('height', 2.0, 'cm')
>>> a2 = Parameters('A-2')
>>> a2._set('length', 1.0, 'm')
>>> a2._set('height', 0.03, 'm')
>>> b1 = Parameters('B-1')
>>> b1._set('time', 5.0, 's')
>>> b2 = Parameters('B-2')
>>> b2._set('time', 4.0, 's')
>>> b2._set('area', 0.04, 'm^2')

# Register the templates

>>> ereg = EntityRegistry()
>>> ereg.register_parameters(a1, a2, b1, b2)

# Use the a1 and b1 templates as input to generate a new Parameters # instance of entity B-2. Note the implicit conversion of unis # when a1 is converted to entity A-2.

>>> econv = EntityConverter(ereg)
>>> b2_new = econv.convert(('B', '2'), a1, b1)
>>> print(b2_new)
5.0               s      time               None
0.02              m^2    area               None
check_arguments(*sources)[source]

Checks that *sources confirms to the EntityConverter subclass sources attribute. Raises an exception on error.

convert(target, *sources)[source]

Returns a Parameters instance of entity target using sources as input.

Parameters :

target : string | tuple

Target entity.

*sources : Parameters instances

Source parameter instances.

Raises :

ConversionError if the conversion fails.

converter(*sources)[source]

Converter implemented by subclasses.

It should return an Parameters instance of the subclass target entity, using the *sources Parameters instances as input.

You can call check_sources() to check that *sources confirms in number and entity types to the subclass sources attribute.

updated_copy(src, pars=(), mode='c')[source]

Returns a copy of src with values updated from pars.

src is either a Parameters instance or an entity identifier. In the latter case a template Parameters object from the registry is used.

pars is either a Parameters instances or a sequence of zero or more instances. These will update src in the given order, which means that the later arguments will overwrite the firsts.

mode is a combination of ‘a’ (allow adding new parameters) and ‘c’ (allow changing existing parameters).

class pario.parameters.ConversionError[source]

Error in convertion between parameter entities.

pario.parameters.simple_converter(name, sources, target, mode='c')[source]

A factory function that returns a new converter class that converts Parameters objects of entity sources to target.

mode determines wheter parameters can be changed or added. Is a combination of ‘a’ (allow adding new parameters) and ‘c’ (allow changing existing parameters).

Examples

>>> A2fromA1Converter = simple_converter(
...     'A2fromA1Converter', sources=['A-1'], target='A-2')
>>> C1fromA1_B2Converter = simple_converter(
...     'C1fromA1_B2Converter', sources=['A-1', 'B-2'], target='C-1')

EntityRegistry object

In order for the parameter entity conversions to work, the converter needs to know a definition of the parameter entities. This is simply done by creating an entity register

>>> ereg = EntityRegistry()

and registering a default input file as a Parameters objects

>>> ereg.register_file('my_default_inputfile.txt')

The ereg object is then be passed to the EntityConverter constructor.

class pario.parameters.EntityRegistry[source]

Entity registry class.

Examples

>>> ereg = EntityRegistry()
>>> ereg.register_template_folder('templates')  
get(entity)[source]

Returns a parameters instance for given entity.

lookup(names, require_unique=True)

Returns the entityid(s) of templates that matches names.

If require_unique is true and there is one match its entityid is returned, otherwise an exception is raised.

If require_unique if false, a list with entityid’s for all the matches is returned.

register_file(fname, fmt=None, entityid=None, description=None, **options)[source]

Adds template file fname to registry. See read() for a arguments.

register_parameters(*pars)[source]

Adds Parameters objects to registry.

register_template_folder(path, pattern=None, recurs=True)[source]

Add all templates in folder path to the registry. If pattern is given, only files matching this shell-style wildcard pattern will be added. If recurs is true, sub-folders are also searched.

remove_template(entity)[source]

Removes template for given entity from registry.

ParametersFormat subclasses

New file formats can be added by subclassing ParametersFormat. Predefined includes:

Content

pario.parameters.ParametersFormat Base class for parameter formats.
pario.parameters.TextFormat Text format.
pario.parameters.SettingsFormat A subclass of TextFormat where each input line has the
pario.parameters.InputError Raised on error when reading input.
class pario.parameters.ParametersFormat[source]

Base class for parameter formats.

Subclasses should define the name and extension attributes as well as overriding the fromstring() and tostring() methods. If it is more natural to only support files, the read() and write() methods could be overridden instead.

Attributes

name (string) Name of the format.
extension (string) Name of file name extension associated with this format.
classmethod fromstring(string, entityid=None, description='', mode='c', **options)[source]

Returns a Parameters object read from string.

Should be implemented by the subclass.

static get_format(name=None, extension=None)[source]

Returns the subclass with given format name or extension.

classmethod read(fname, entityid=None, description='', mode='c', **options)[source]

Reads a fname and returns a Parameters object.

classmethod tostring(par, **options)[source]

Returns a string representation of Parameters object par.

Should be implemented by the subclass.

classmethod write(fname, par, **options)[source]

Writes Parameters object par to file fname.

class pario.parameters.TextFormat[source]

Text format.

classmethod fromstring(string, entityid=None, description='', mode='c', converters=None, **options)[source]

Returns a Parameters object read from string.

Parameters :

string : str

Sting to read.

entityid : string | tuple

Identifies the parameter entity, i.e. what the parameters in this instance describes. Consists of a name and version. If None it might be derived from fname. Examples: “MyProgram-1.0” or (‘MyProgram’, ‘1.0’).

description : string

Description of the parameter entity.

mode : string

Determines wheter parameters can be changed or added. Is a combination of ‘a’ (allow adding new parameters), ‘c’ (allow changing existing parameters) and ‘t’ (allow changing the type when assigning to an existing parameter).

converters : None | sequence_of_types | string

Type converters that converts the value as a string into a value of the correct type. It may be a sequence of type converters, or a comma-separated string with the name of the type converters, e.g. “int,float”.

options : keywords

Additional keyword options. See the Parameters class docstring for details.

classmethod tostring(par, names=None, linesep='auto', show_heading=False, show_group_comments=True, **kw)[source]

Returns parameters as a string.

Parameters :

par : Parameters instance

Input parameters instance.

names : None | sequence

If not None, only include the given parameters.

linesep : string

The line separator. Three special values “auto” (os default), “LF” and “CRLF” are interpreated.

show_heading : bool

Whether to write a heading.

show_group_comments : bool

Whether to write group comments.

kw : keywords

Ignored.

class pario.parameters.SettingsFormat[source]

A subclass of TextFormat where each input line has the following syntax:

name = value # doc

Examples

>>> s = '''
... model = 2         # model flag
... rho_i = 1.52e10   # dislocation density, m^-2
... 
... # subgrain size, m
... delta = 3e-7
... '''
>>> settings = fromstring(s, fmt='settings')
>>> print(settings._tostring(fmt='settings'))
model        = 2                 # model flag
rho_i        = 15200000000.0     # dislocation density, m^-2
delta        = 3e-07             # subgrain size, m
class pario.parameters.InputError[source]

Raised on error when reading input.

Other utilities functions

pario.parameters.entity_to_tuple(entity)[source]

Returns entity as an (entityname, version) tuple. entity may be either the wanted tuple or a string of the form:

entityname-version[.ext]

where the extension .ext is optional.

Examples

>>> entity_to_tuple('myprogram-0.1')
('myprogram', '0')
>>> entity_to_tuple('myprogram-3.txt')
('myprogram', '3')
pario.parameters.entity_to_string(entity)[source]

Returns entity as a string on the form entityname-version.

Examples

>>> entity_to_string(('myprogram', '0.1'))
'myprogram-0.1'
pario.parameters.get_subclasses(cls, recurs=True)[source]

Returns all list with all subclasses of cls. If recurs is true, nested subclasses are also included.

pario.parameters.typeconvert(value, converters=None)

Tries to convert value to another type using the converters in the sequence converters. converters may also be a comma-separated string of converter names (looked up in the local, global or builtin scopes).

The new value after the first successful convertsion is returned.

If value is a sequence, a list is returned with all elements converted using the first conversion that works for all of them.

If types is None, it defaults to (int, float, ast.literal_eval, string2bool, str).

Examples

>>> typeconvert('3.4')
3.4
>>> typeconvert('3.4a')
'3.4a'
>>> typeconvert('yes')
True
>>> typeconvert('yes', converters='int,float,string2bool')
True
>>> typeconvert(['3', '1', '2.2'])
[3.0, 1.0, 2.2]
>>> typeconvert([3, 1, 2.2])    # Note that 2.2 is truncated!
[3, 1, 2]
>>> typeconvert(['a', 1, 2.2])
['a', '1', '2.2']