sporco.common

Common functions and classes iterative solver classes

Functions

_fix_nested_class_lookup(cls, nstnm) Fix name lookup problem that prevents pickling of classes with nested class definitions.
_fix_dynamic_class_lookup(cls, pstfx) Fix name lookup problem that prevents pickling of dynamically defined classes.
solve_status_str(hdrlbl[, fmtmap, fwdth0, …]) Construct header and format details for status display of an iterative solver.

Classes

_IterSolver_Meta(*args) Metaclass for iterative solver classes that handles intialisation of IterationStats namedtuple and applies _fix_nested_class_lookup to class definitions to fix problems with lookup of nested class definitions when using pickle.
IterativeSolver Base class for iterative solver classes, providing some common infrastructure.

Function Descriptions

sporco.common._fix_nested_class_lookup(cls, nstnm)[source]

Fix name lookup problem that prevents pickling of classes with nested class definitions. The approach is loosely based on that implemented at https://git.io/viGqU , simplified and modified to work in both Python 2.7 and Python 3.x.

Parameters:
cls : class

Outer class to which fix is to be applied

nstnm : string

Name of nested (inner) class to be renamed

sporco.common._fix_dynamic_class_lookup(cls, pstfx)[source]

Fix name lookup problem that prevents pickling of dynamically defined classes.

Parameters:
cls : class

Dynamically generated class to which fix is to be applied

pstfx : string

Postfix that can be used to identify dynamically generated classes that are equivalent by construction

sporco.common.solve_status_str(hdrlbl, fmtmap=None, fwdth0=4, fwdthdlt=6, fprec=2)[source]

Construct header and format details for status display of an iterative solver.

Parameters:
hdrlbl : tuple of strings

Tuple of field header strings

fmtmap : dict or None, optional (default None)

A dict providing a mapping from field header strings to print format strings, providing a mechanism for fields with print formats that depart from the standard format

fwdth0 : int, optional (default 4)

Number of characters in first field formatted for integers

fwdthdlt : int, optional (default 6)

The width of fields formatted for floats is the sum of the value of this parameter and the field precision

fprec : int, optional (default 2)

Precision of fields formatted for floats

Returns:
hdrstr : string

Complete header string

fmtstr : string

Complete print formatting string for numeric values

nsep : integer

Number of characters in separator string


Class Descriptions

class sporco.common._IterSolver_Meta(*args)[source]

Bases: type

Metaclass for iterative solver classes that handles intialisation of IterationStats namedtuple and applies _fix_nested_class_lookup to class definitions to fix problems with lookup of nested class definitions when using pickle. It is also responsible for stopping the object initialisation timer at the end of initialisation.

class sporco.common.IterativeSolver[source]

Bases: object

Base class for iterative solver classes, providing some common infrastructure.

itstat_fields_objfn = ()

Fields in IterationStats associated with the objective function

itstat_fields_alg = ()

Fields in IterationStats associated with the specific solver algorithm, e.g. ADMM or FISTA

itstat_fields_extra = ()

Non-standard fields in IterationStats

classmethod itstat_fields()[source]

Construct tuple of field names used to initialise IterationStats named tuple.

set_dtype(opt, dtype)[source]

Set the dtype attribute. If opt[‘DataType’] has a value other than None, it overrides the dtype parameter of this method. No changes are made if the dtype attribute already exists and has a value other than ‘None’.

Parameters:
opt : cdict.ConstrainedDict object

Algorithm options

dtype : data-type

Data type for working variables (overridden by ‘DataType’ option)

set_attr(name, val, dval=None, dtype=None, reset=False)[source]

Set an object attribute by its name. The attribute value can be specified as a primary value val, and as default value ‘dval` that will be used if the primary value is None. This arrangement allows an attribute to be set from an entry in an options object, passed as val, while specifying a default value to use, passed as dval in the event that the options entry is None. Unless reset is True, the attribute is only set if it doesn’t exist, or if it exists with value None. This arrangement allows for attributes to be set in both base and derived class initialisers, with the derived class value taking preference.

Parameters:
name : string

Attribute name

val : any

Primary attribute value

dval : any

Default attribute value in case val is None

dtype : data-type, optional (default None)

If the dtype parameter is not None, the attribute name is set to val (which is assumed to be of numeric type) after conversion to the specified type.

reset : bool, optional (default False)

Flag indicating whether attribute assignment should be conditional on the attribute not existing or having value None. If False, an attribute value other than None will not be overwritten.