Utilities¶
Overview:
- class pints.Loggable[source]¶
Interface for classes that can log to a
Logger
.- _log_init(logger)[source]¶
Adds this
Loggable's
fields to aLogger
.
- _log_write(logger)[source]¶
Logs data for each of the fields specified in
_log_init()
.
- class pints.Logger[source]¶
Logs numbers to screen and/or a file.
Example
log = pints.Logger() log.add_counter('id', width=2) log.add_float('Length') log.log(1, 1.23456) log.log(2, 7.8901)
- add_counter(name, width=5, max_value=None, file_only=False)[source]¶
Adds a field for positive integers.
Returns this
Logger
object.- Parameters:
name (str) – This field’s name. Will be displayed in the header.
width (int) – A hint for the width of this column. If numbers exceed this width layout will break, but no information will be lost.
max_value (int|None) – A hint for the maximum number this field will need to display.
file_only (boolean) – If set to
True
, this field will not be shown on screen.
- add_float(name, width=9, file_only=False)[source]¶
Adds a field for floating point number.
Returns this
Logger
object.- Parameters:
name (str) – This field’s name. Will be displayed in the header.
width (int) – A hint for the field’s width. The minimum width is 7.
file_only (boolean) – If set to
True
, this field will not be shown on screen.
- add_int(name, width=5, file_only=False)[source]¶
Adds a field for a (positive or negative) integer.
Returns this
Logger
object.- Parameters:
name (str) – This field’s name. Will be displayed in the header.
width (int) – A hint for the width of this column. If numbers exceed this width layout will break, but no information will be lost.
file_only (boolean) – If set to
True
, this field will not be shown on screen.
- add_long_float(name, file_only=False)[source]¶
Adds a field for a maximum precision floating point number.
Returns this
Logger
object.- Parameters:
name (str) – This field’s name. Will be displayed in the header.
file_only (boolean) – If set to
True
, this field will not be shown on screen.
- add_string(name, width, file_only=False)[source]¶
Adds a field showing (at most
width
characters of) string values.Returns this
Logger
object.- Parameters:
name (str) – This field’s name. Will be displayed in the header.
width (int) – The maximum width for strings to display.
file_only (boolean) – If set to
True
, this field will not be shown on screen.
- add_time(name, file_only=False)[source]¶
Adds a field showing a formatted time (given in seconds).
Returns this
Logger
object.- Parameters:
name (str) – This field’s name. Will be displayed in the header.
file_only (boolean) – If set to
True
, this field will not be shown on screen.
- class pints.Timer(output=None)[source]¶
Provides accurate timing.
Example
timer = pints.Timer() print(timer.format(timer.time()))
- pints.matrix2d(x)[source]¶
Copies
x
and returns a 2d read-only NumPy array of floats with shape(m, n)
.Raises a
ValueError
ifx
has an incompatible shape.
- pints.vector(x)[source]¶
Copies
x
and returns a 1d read-only NumPy array of floats with shape(n,)
.Raises a
ValueError
ifx
has an incompatible shape.
- pints.sample_initial_points(function, n_points, random_sampler=None, boundaries=None, max_tries=50, parallel=False, n_workers=None)[source]¶
Samples
n_points
parameter values to use as starting points in a sampling or optimisation routine on the givenfunction
.How the initial points are determined depends on the arguments supplied. In order of precedence:
If a method
random_sampler
is provided then this will be used to draw the random samples.If no sampler method is given but
function
is aLogPosterior
then the methodfunction.log_prior().sample()
will be used.If no sampler method is supplied and
function
is not aLogPosterior
and ifboundaries
are provided then the methodboundaries.sample()
will be used to draw samples.
A
ValueError
is raised if none of the above options are available.Each sample
x
is tested to ensure thatfunction(x)
returns a finite result withinboundaries
if these are supplied. If not, a new sample will be drawn. This is repeated at mostmax_tries
times, after which an error is raised.- Parameters:
function – A
pints.ErrorMeasure
or apints.LogPDF
that evaluates points in the parameter space. If the latter, it is optional thatfunction
be of typeLogPosterior
.n_points (int) – The number of initial values to generate.
random_sampler – A function that when called returns draws from a probability distribution of the same dimensionality as
function
. The only argument to this function should be an integer specifying the number of draws.boundaries – An optional set of boundaries on the parameter space of class
pints.Boundaries
.max_tries (int) – Number of attempts to find a finite initial value across all
n_points
. By default this is 50 per point.parallel (bool) – Whether to evaluate
function
in parallel (defaults to False).n_workers (int) – Number of workers on which to run parallel evaluation.