Utilities

Overview:

pints.strfloat(x)[source]

Converts a float to a string, with maximum precision.

class pints.Loggable[source]

Interface for classes that can log to a Logger.

_log_init(logger)[source]

Adds this Loggable's fields to a Logger.

_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.
log(*data)[source]

Logs a new row of data.

set_filename(filename=None, csv=False)[source]

Enables logging to a file if a filename is passed in. Logging to file can be disabled by passing filename=None.

Usually, file logging happens in the same format as logging to screen. To obtain csv logs instead, set csv=True

set_stream(stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]

Enables logging to screen if an output stream is passed in. Logging to screen can be disabled by passing stream=None.

class pints.Timer(output=None)[source]

Provides accurate timing.

Example

timer = pints.Timer()
print(timer.format(timer.time()))
format(time=None)[source]

Formats a (non-integer) number of seconds, returns a string like “5 weeks, 3 days, 1 hour, 4 minutes, 9 seconds”, or “0.0019 seconds”.

reset()[source]

Resets this timer’s start time.

time()[source]

Returns the time (in seconds) since this timer was created, or since meth:reset() was last called.

pints.matrix2d(x)[source]

Copies x and returns a 2d read-only NumPy array of floats with shape (m, n).

Raises a ValueError if x 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 if x 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 given function.

How the initial points are determined depends on the arguments supplied. In order of precedence:

  1. If a method random_sampler is provided then this will be used to draw the random samples.
  2. If no sampler method is given but function is a LogPosterior then the method function.log_prior().sample() will be used.
  3. If no sampler method is supplied and function is not a LogPosterior and if boundaries are provided then the method boundaries.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 that function(x) returns a finite result within boundaries if these are supplied. If not, a new sample will be drawn. This is repeated at most max_tries times, after which an error is raised.

Parameters:
  • function – A pints.ErrorMeasure or a pints.LogPDF that evaluates points in the parameter space. If the latter, it is optional that function be of type LogPosterior.
  • 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.