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(*args, **kwargs)

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:
clsclass

Outer class to which fix is to be applied

nstnmstring

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:
clsclass

Dynamically generated class to which fix is to be applied

pstfxstring

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:
hdrlbltuple of strings

Tuple of field header strings

fmtmapdict 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

fwdth0int, optional (default 4)

Number of characters in first field formatted for integers

fwdthdltint, optional (default 6)

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

fprecint, optional (default 2)

Precision of fields formatted for floats

Returns:
hdrstrstring

Complete header string

fmtstrstring

Complete print formatting string for numeric values

nsepinteger

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(*args, **kwargs)[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 PGM

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’.

Note that the dtype attribute is expected to have type numpy.dtype rather than type, e.g. for float32 values, it should be np.dtype(np.float32) rather than np.float32.

Parameters:
optcdict.ConstrainedDict object

Algorithm options

dtypenumpy.dtype

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:
namestring

Attribute name

valany

Primary attribute value

dvalany

Default attribute value in case val is None

dtypedata-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.

resetbool, 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.