Function approximation¶
The module myokit.lib.approx
contains a number of function approximation
algorithms.
Some functions in this module require a recent version of scipy to be installed.

class
myokit.lib.approx.
FittingError
¶ Error raised when a fitting procedure fails.

class
myokit.lib.approx.
FittedFunction
(abserr=None, relerr=None)¶ Represents a function that was fitted to some data or another function.
The used fitting procedure may have resulted in an absolute error
abserr
and a relative errorrelerr
. If so, this can be indicated by setting these properties to values other thanNone
.
abserr
()¶ Returns the maximum absolute error found when fitting this function.

relerr
()¶ Returns the maximum relative error found when fitting this function.

set_abserr
(abserr)¶ Sets the maximum absolute error found when fitting this function.

set_relerr
(relerr)¶ Sets the maximum relative error found when fitting this function.

Simple polynomials¶

class
myokit.lib.approx.
Polynomial
(c)¶ Represents a polynomial function.
A polynomial is created from a list of coefficients
c
and is defined as:p(x) = c[0] + c[1]*x + c[2]*x**2 + ... + c[n]*x**n
or, equivalently:
P(x) = c[0] + x * (c[1] + x * (c[2] + ... + x * (c[2] + x * c[1])))
A polynomial can be evaluated:
>>> from myokit.lib.approx import Polynomial >>> f = Polynomial([1, 2, 3]) >>> print(f(0), f(1), f(2)) (1, 6, 9)
Polynomials work with numpy input:
>>> import numpy as np >>> x = np.array([0, 1, 2]) >>> print(f(x)) [1 6 9]
A Polynomial is iterable, which provides (readonly) access to its coefficients:
>>> print(', '.join(str(x) for x in f)) 1.0, 2.0, 3.0

myokit_form
(lhs)¶ Returns this piecewise polynomial as a myokit expression with the indepent variable expressed by lhs.


myokit.lib.approx.
fit_remez_polynomial
(f, i, n, stop_tol=1e06, max_iter=100)¶ Calculates an approximating polynomial of degree
n
to the functionf
on the intervali=[a,b]
. The result is returned as aPolynomial
.This function uses the Remez exchange algorithm to iteratively calculate a polynomial that minimises the supremum of
f  g
over the interval[a, b]
. The function will stop when the difference between succesive errors is less thanstop_tol
ormax_iter
is reached.

myokit.lib.approx.
fit_lagrange_polynomial
(f, i, n=None, x=None)¶ Calculates the Lagrange polynomial of degree
n
to the functionf
on the intervali=[a,b]
. The result is returned as aPolynomial
.Instead of (or in addition to) giving the degree
n
, a list ofn+1
points on the intervali
can be passed in. In these points, the approximation will match the function exactly.
Piecewise polynomials¶

class
myokit.lib.approx.
PiecewisePolynomial
(p, k=None)¶ Represents a piecewise polynomial function.
A piecewise polynomial is created from a list of
n
polynomials
and a sorted list ofn  1
splitting points.The resulting spline is calculated as:
x < k[0] > s(x) = p[0] k[0] <= x < k[1] > s(x) = p[1] : : k[n3] <= x < k[n2] > s(x) = p[n2] k[n2] <= x > s(x) = p[n1]
where
p
is the list of polynomial functions andk
is the list of splitting points.A piecewise polynomial can be evaluated:
>>> from myokit.lib.approx import Polynomial, PiecewisePolynomial >>> f = PiecewisePolynomial( ... [Polynomial((1, 2, 3)), ... Polynomial((2, 3,4)), ... Polynomial((3,4,5))], ... [1.5, 2.5]) >>> print(f(0), f(1), f(2), f(3), f(4)) (1, 6, 24, 60, 99)
Piecewise polynomials work with numpy input:
>>> import numpy as np >>> x = np.array([0, 1, 2, 3, 4]) >>> print(f(x)) [ 1 6 24 60 99]
Instead of passing in Polynomial objects, a list of coefficientlists can also be given.

myokit_form
(lhs)¶ Returns this piecewise polynomial as a myokit expression with the indepent variable expressed by lhs.


class
myokit.lib.approx.
Spline
(p, k=None)¶ Extends:
PiecewisePolynomial
A spline is created from a list of
n
polynomials
and a sorted list ofn + 1
points, including the start and end point of the domain the spline is defined on.Outside the domain boundaries, the values of the
The resulting spline is calculated as:
x < k[0] > s(x) = p[1] k[0] <= x < k[1] > s(x) = p[2] : : k[n3] <= x < k[n2] > s(x) = p[n2] k[n2] <= x > s(x) = p[n1]
The spline’s knots can be retrieved using
knots()
.Using the
Spline
class instead ofPiecewisePolynomial
indicates that the function is firstorder differentiable at the knots, although this is not checked internally.Splines can be cloned using the syntax
s_clone = Spline(s)
.
knots
()¶ Returns this piecewise function’s knots.

number_of_pieces
()¶ Returns the number of pieces in this spline.

pieces
()¶ Returns the individual polynomial pieces this spline is composed of.


myokit.lib.approx.
fit_cubic_spline
(f, i, reltol=0.001, abstol=None, min_pieces=2, max_pieces=50, regular_spacing=False)¶ Creates a cubic spline approximation to
f
on the given intervali=[a,b]
.The number of pieces is started at
min_pieces
and increased until the specified absolute toleranceabstol
and relative tolerancereltol
are met. If the tolerance can’t be met with at mostmax_pieces
, thenNone
is returned.The result is returned as a
Spline
.The routine starts with regularly spaced knots, and then adds a new knot at the position with the highest error. To disable this behaviour, and use regularly spaced intervals throughout the routine, set
regular_spacing
toTrue
.

myokit.lib.approx.
solve_cubic_spline
(f, p)¶ Finds the coefficients for a cubic spline matching function
f
on the knots given byp
and returns the resultingSpline
.The list of knots must include the left and rightmost points of the spline, so that the (number of knots) = 1 + (number of pieces).
The argument
f
must be the handle to a functionf(x)
. The arrayp
must containn+1
increasing xvalues (the “knots”). The approximating splineg
matchesf
exactly at every knot. At the outermost knots the first derivative ofg
matchesf
‘s first derivative. At all inner knots, the first derivatives of the two touching pieces will be equal.
Model optimizers¶

myokit.lib.approx.
list_fitters
()¶ Returns a dict mapping fitter names to classes.

myokit.lib.approx.
suitable_expressions
(model, var, blacklist=None, require_exp=False, exclude_conditionals=False)¶ Extracts the RHS expressions from model suitable for approximation. “Suitable” functions are singlevariabled functions dependent on variable var.
Arguments:
model
 The model to search in
var
 The variable all expressions must be a univariate function of
blacklist=None
 A list of variables (or variable qnames) whose RHS to exclude.
require_exp=False
 With this set to
True
, only expressions containing anexp()
function are considered. exclude_conditionals=False
 With this set to
True
, any functions containing a conditional statement will be excluded.
The returned value is a list of tuples
(lhs, rhs)
, whererhs
is the expressions defininglhs
, andrhs
is deemed “suitable”.

class
myokit.lib.approx.
Fitter
¶ Attempts to update a myokit model by approximating functions of a single variable with a known range (typically the membrane potential).

changed_variables
()¶ Returns a list containing the names of the variables that were updated during the last run.

fit
(model, var, rng, report=None)¶ Attempts to approximate suitable functions in the given model.
A variable
var
must be specified, as well as the rangerng
that this variable is likely to take. A typical example would be the variablemembrane.V
and the range(100, 50)
.The variable can be specified as a name (string), an object representing a variable or an LhsExpression.
The range can be specified as any sequence type of length 2.
If the variable
report
is set, a small HTML based report will be generated and stored in the directory indicated byreport
.


class
myokit.lib.approx.
CubicSplineFitter
(abstol=None, reltol=0.001, min_pieces=1, max_pieces=200, blacklist=None)¶ Attempts to approximating functions of a single variable with a known range (typically the membrane potential) using a cubic spline.
Arguments:
abstol
 The absolute tolerance to fit to.
reltol
 The relative tolerance to fit to.
min_pieces
 The minimum number of pieces in the spline.
max_pieces
 The maximum number of pieces in the spline.
blacklist
 A list of variables not to approximate.

class
myokit.lib.approx.
PolynomialFitter
¶ Attempts to update a myokit model by approximating functions of a single variable with a known range (typically the membrane potential) with a single polynomial