Function evaluation

The Evaluator classes provide an abstraction layer that makes it easier to implement sequential and/or parallel evaluation of functions.

Example:

f = pints.SumOfSquaresError(problem)
e = pints.ParallelEvaluator(f)
x = [[1, 2],
     [3, 4],
     [5, 6],
     [7, 8],
    ]
fx = e.evaluate(x)

Overview:

pints.evaluate(f, x, parallel=False, args=None)[source]

Evaluates the function f on every value present in x and returns a sequence of evaluations f(x[i]).

It is possible for the evaluation of f to involve the generation of random numbers (using numpy). In this case, the results from calling evaluate can be made reproducible by first seeding numpy’s generator with a fixed number. However, a call with parallel=True will use a different (but consistent) sequence of random numbers than a call with parallel=False.

Parameters:
  • f (callable) – The function to evaluate, called as f(x[i], *args).
  • x – A list of values to evaluate f with
  • parallel (boolean) – Run in parallel or not. If set to True, the evaluations will happen in parallel using a number of worker processes equal to the detected cpu core count. The number of workers can be set explicitly by setting parallel to an integer greater than 0. Parallelisation can be disabled by setting parallel to 0 or False.
  • args (sequence) – Optional extra arguments to pass into f.
class pints.Evaluator(function, args=None)[source]

Abstract base class for classes that take a function (or callable object) f(x) and evaluate it for list of input values x.

This interface is shared by a parallel and a sequential implementation, allowing easy switching between parallel or sequential implementations of the same algorithm.

It is possible for the evaluation of f to involve the generation of random numbers (using numpy). In this case, the results from calling evaluate can be made reproducible by first seeding numpy’s generator with a fixed number. However, different Evaluator implementations may use a different random sequence. In other words, each Evaluator can be made to return consistent results, but the results returned by different Evaluators may vary.

Parameters:
  • function (callable) – A function or other callable object f that takes a value x and returns an evaluation f(x).
  • args (sequence) – An optional sequence of extra arguments to f. If args is specified, f will be called as f(x, *args).
evaluate(positions)[source]

Evaluate the function for every value in the sequence positions.

Returns a list with the returned evaluations.

class pints.ParallelEvaluator(function, n_workers=None, max_tasks_per_worker=500, n_numpy_threads=1, args=None)[source]

Evaluates a single-valued function object for any set of input values given, using all available cores.

Shares an interface with the SequentialEvaluator, allowing parallelism to be switched on and off with minimal hassle. Parallelism takes a little time to be set up, so as a general rule of thumb it’s only useful for if the total run-time is at least ten seconds (anno 2015).

By default, the number of processes (“workers”) used to evaluate the function is set equal to the number of CPU cores reported by python’s multiprocessing module. To override the number of workers used, set n_workers to some integer greater than 0.

There are two important caveats for using multiprocessing to evaluate functions:

  1. Processes don’t share memory. This means the function to be evaluated will be duplicated (via pickling) for each process (see Avoid shared state for details).
  2. On windows systems your code should be within an if __name__ == '__main__': block (see Windows for details).

The evaluator will keep it’s subprocesses alive and running until it is tidied up by garbage collection.

Note that while this class uses multiprocessing, it is not thread/process safe itself: It should not be used by more than a single thread/process at a time.

Extends Evaluator.

Parameters:
  • function – The function to evaluate
  • n_workers – The number of worker processes to use. If left at the default value n_workers=None the number of workers will equal the number of CPU cores in the machine this is run on. In many cases this will provide good performance.
  • max_tasks_per_worker – Python garbage collection does not seem to be optimized for multi-process function evaluation. In many cases, some time can be saved by refreshing the worker processes after every max_tasks_per_worker evaluations. This number can be tweaked for best performance on a given task / system.
  • n_numpy_threads – Numpy and other scientific libraries may make use of threading in C or C++ based BLAS libraries, which can interfere with PINTS multiprocessing and cause slower execution. To prevent this, the number of threads to use will be limited to 1 by default, using the threadpoolctl module. To use the current numpy default instead, set n_numpy_threads to None, to use the BLAS/OpenMP etc. defaults, set n_numpy_threads to 0, or to use a specific number of threads pass in any integer greater than 1.
  • args – An optional sequence of extra arguments to f. If args is specified, f will be called as f(x, *args).
static cpu_count()[source]

Uses the multiprocessing module to guess the number of available cores.

For machines with simultaneous multithreading (“hyperthreading”) this will return the number of virtual cores.

evaluate(positions)

Evaluate the function for every value in the sequence positions.

Returns a list with the returned evaluations.

class pints.SequentialEvaluator(function, args=None)[source]

Evaluates a function (or callable object) for a list of input values, and returns a list containing the calculated function evaluations.

Runs sequentially, but shares an interface with the ParallelEvaluator, allowing parallelism to be switched on/off.

Extends Evaluator.

Parameters:
  • function (callable) – The function to evaluate.
  • args (sequence) – An optional tuple containing extra arguments to f. If args is specified, f will be called as f(x, *args).
evaluate(positions)

Evaluate the function for every value in the sequence positions.

Returns a list with the returned evaluations.