FunctionLogger#

class pybads.function_logger.FunctionLogger(fun, D: int, noise_flag: bool, uncertainty_handling_level: int, cache_size: int = 500, variable_transformer: VariableTransformer = None)[source]#

Class that evaluates a function and caches its values.

Parameters:
funcallable

The function to be logged. fun must take a vector input and return a scalar value, or, when uncertainty_handling_level is 2, a tuple of the value and its (estimated) SD.

Dint

The number of dimensions of the points that the logger takes, in the transformed space. The function takes as many, unless the variable_transformer has fixed variables (fixed_values): it then takes points of all the variables of the original space, as X_orig holds them.

noise_flagbool

Whether the logger holds the standard deviations of the noise that the function returns (S).

uncertainty_handling_level{0, 1, 2}

The uncertainty handling level which can be one of (0: none; 1: unknown noise level; 2: user-provided noise).

cache_sizeint, optional

The initial size of caching table (default 500), number of stored function values.

variable_transformerVariableTransformer, optional

A VariableTransformer is required for linear and non-linear transformation of the input space. By default None.

__call__(x: ndarray, record_duplicate_data: bool = True)[source]#

Evaluates the function self.fun at x and caches values.

Parameters:
xnp.ndarray

The point at which the function will be evaluated, in the transformed space (u), of shape (1, D) or (D,). The logger maps it back to the original space with its variable_transformer, if it has one, and evaluates the function there; without one, the two spaces are the same.

record_duplicate_databool, optional (default True)

Whether the evaluation is recorded in the log. One that is not leaves the log as it is: it adds only its time to total_fun_eval_time, and counts in func_count.

Returns:
fvalfloat

The result of the evaluation, merged by precision weighting with earlier observations at the point when recording it in a logger that holds SDs. An evaluation that is not recorded returns its own value.

SDfloat or None

The (estimated) SD that the function returned, None when the logger takes none (uncertainty_handling_level below 2). Describes this evaluation before any merge.

idxint or None

The index of the last updated entry, None when an evaluation that is not recorded (record_duplicate_data=False) is of a point that the log does not hold.

Raises:
ValueError

Raise if the function value is not a finite real-valued scalar, as a tuple (f, sd) is not when uncertainty_handling_level is below 2.

ValueError

Raise if the (estimated) SD (second function output) is not a finite, positive real-valued scalar, or if the function does not return a tuple (f, sd) when uncertainty_handling_level is 2.

Exception

Any exception that the function raises, as it was raised, with a note of the point on Python 3.11 and later.

add(x: ndarray, fval_orig: float, fsd: float = None, fun_eval_time=nan)[source]#

Add an evaluation of the function made elsewhere to the log, without counting it in func_count: BADS adds the evaluations made before its run (precomputed_evaluations) this way.

The evaluation is recorded as one of the logger’s own: at a point that the log holds, a logger that holds SDs merges it into the point’s row by precision weighting, and one that does not adds a row.

Parameters:
xnp.ndarray

The point at which the function has been evaluated, in the transformed space (u), of shape (1, D) or (D,). The logger maps it back to the original space with its variable_transformer, if it has one, and records both; without one, the two spaces are the same.

fval_origfloat

The result of the evaluation of the function, a finite real scalar, or an array or a list of one element.

fsdfloat, optional

The (estimated) SD of the result, a finite positive real scalar, or an array or a list of one element. A logger that holds SDs (noise_flag) requires it at uncertainty handling level 2, where the target returns them, and records a missing one as 1 below it; a logger that does not hold SDs ignores it. By default None.

fun_eval_timefloat

The duration of the time it took to evaluate the function, by default np.nan.

Returns:
fvalfloat

The value that the log holds at the point: fval_orig, merged with the earlier evaluations at the point for a logger that holds SDs.

SDfloat or None

The SD recorded for this evaluation: fsd, or 1 for a missing one, and None when the logger holds no SDs.

idxint

The index of the last updated entry.

Raises:
ValueError

Raise if the function value is not a finite real-valued scalar.

ValueError

Raise if the logger holds SDs and fsd is missing at uncertainty handling level 2, or is not a finite, positive real-valued scalar.

finalize()[source]#

Remove unused caching entries.

reset_fun_eval_time()[source]#

Set every entry of fun_eval_time to NaN, keeping its length that of the other arrays.