fiddy package

Subpackages

Submodules

fiddy.analysis module

class fiddy.analysis.Analysis(method: Callable[[DirectionalDerivative], Any] = None)

Bases: object

abstract method(directional_derivative: DirectionalDerivative) Any
only_at_completion: bool = False
class fiddy.analysis.AnalysisResult(method_id: str, value: Any, metadata: dict[str, typing.Any] = <factory>)

Bases: object

metadata: dict[str, Any]
method_id: str
value: Any
class fiddy.analysis.ApproximateCentral

Bases: Analysis

Uses the first valid forward and backward directional derivative computers.

id = 'approximate_central'
method(directional_derivative: DirectionalDerivative) None
only_at_completion: bool = True
class fiddy.analysis.TransformByDirectionScale(scales: dict[str, str])

Bases: Analysis

Transform derivatives by applying a transformation to their directions.

LOG_E_10 = np.float64(2.302585092994046)
method(directional_derivative: DirectionalDerivative) None
only_at_completion: bool = True
transform(value: NDArray[float64], scale: str, position: float64)

fiddy.constants module

class fiddy.constants.MethodId(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: str, Enum

Finite different method IDs.

BACKWARD = 'backward'
CENTRAL = 'central'
FORWARD = 'forward'
RICHARDSON = 'richardson'
class fiddy.constants.Type

Bases: object

Type annotation variables.

ANALYSIS_METHOD

alias of Callable[DirectionalDerivative, Any]

ARRAY

alias of NDArray[float64]

DERIVATIVE

alias of NDArray[NDArray[float64]]

DERIVATIVE_FUNCTION

alias of Callable[NDArray[float64], NDArray[NDArray[float64]]]

DIRECTION

alias of NDArray[float64]

DIRECTIONAL_DERIVATIVE

alias of NDArray[float64]

DIRECTIONAL_DERIVATIVE_FUNCTION

alias of Callable[NDArray[float64], NDArray[float64]]

FUNCTION

alias of Callable[NDArray[float64], NDArray[float64]]

FUNCTION_OUTPUT

alias of NDArray[float64]

POINT

alias of NDArray[float64]

SCALAR

alias of float64

SIZE

alias of float64

SUCCESS_CHECKER

alias of Callable[Derivative, bool | Any]

fiddy.derivative module

class fiddy.derivative.Derivative(directional_derivatives: list[DirectionalDerivative], autorun: bool = True)

Bases: object

Handle all aspects of derivative computation.

The general sequence is: 1. define derivatives to be computed 2. compute derivatives (possibly with multiple methods) 3. analyze derivatives (e.g. compute “consistency” between multiple methods) 4. check derivatives (e.g. ensure sufficient “consistency”)

directional_derivatives

A list of directional derivative objects.

expected_derivative

The expected derivative.

analysis_results

A list of analysis result objects.

success_checker

The method to determine whether the derivative was successfully computed.

success

Whether the derivative was successfully computed.

property df
property df_full
property dict
hide_columns = ['pending_computers', 'computers', 'analyses', 'success_checker', 'expected_result', 'fast', 'autorun']
property series
property value
fiddy.derivative.get_derivative(function: Callable[[NDArray[float64]], NDArray[float64]], point: NDArray[float64], sizes: list[float64], method_ids: list[str | MethodId], success_checker: Success, *args, analysis_classes: list[type[Analysis]] = None, relative_sizes: bool = False, directions: list[NDArray[float64]] | dict[str, NDArray[float64]] = None, direction_ids: list[str] = None, direction_indices: list[int] = None, custom_methods: dict[str, Callable] = None, expected_result: list[float64] = None, **kwargs)

Get a derivative.

Parameters:
  • sizes – The step sizes.

  • direction_ids – The IDs of the directions.

  • directions – List: The directions to step along. Dictionary: keys are direction IDs, values are directions.

  • relative_sizes – If True, sizes are scaled by the point, otherwise not.

fiddy.derivative_check module

class fiddy.derivative_check.DerivativeCheck(derivative: Derivative, expectation: NDArray[NDArray[float64]], point: NDArray[float64])

Bases: ABC

Check whether a derivative is correct.

Parameters:
  • derivative – The test derivative.

  • expectation – The expected derivative.

  • point – The point where the test derivative was computed.

  • output_indices – The derivative can be a multi-dimensional object that has dimensions associated with the multiple outputs of a function, and dimensions associated with the derivative of these multiple outputs with respect to multiple directions.

abstract method(*args, **kwargs)
method_id: str

The name of the derivative check method.

class fiddy.derivative_check.DerivativeCheckResult(method_id: str, directional_derivative_check_results: list[fiddy.derivative_check.DirectionalDerivativeCheckResult], test: NDArray[NDArray[numpy.float64]], expectation: NDArray[NDArray[numpy.float64]], success: bool, output: dict[str, Any] = None)

Bases: object

property df
directional_derivative_check_results: list[DirectionalDerivativeCheckResult]

The results from checking individual directions.

expectation: NDArray[NDArray[float64]]

The expected value.

method_id: str

The method that determined whether the directional derivative is correct.

output: dict[str, Any] = None

Miscellaneous output from the method.

success: bool

Whether the check passed.

test: NDArray[NDArray[float64]]

The value that was tested.

class fiddy.derivative_check.DirectionalDerivativeCheckResult(direction_id: str, method_id: str, test: NDArray[numpy.float64], expectation: NDArray[numpy.float64], success: bool, output: dict[str, Any] = None)

Bases: object

direction_id: str

The direction.

expectation: NDArray[float64]

The expected value.

method_id: str

The method that determined whether the directional derivative is correct.

output: dict[str, Any] = None

Miscellaneous output from the method.

success: bool

Whether the check passed.

test: NDArray[float64]

The value that was tested.

class fiddy.derivative_check.NumpyIsCloseDerivativeCheck(derivative: Derivative, expectation: NDArray[NDArray[float64]], point: NDArray[float64])

Bases: DerivativeCheck

method(*args, **kwargs)
method_id: str = 'np.isclose'

The name of the derivative check method.

fiddy.directional_derivative module

class fiddy.directional_derivative.Computer(point: NDArray[numpy.float64], direction: NDArray[numpy.float64], size: numpy.float64, method: collections.abc.Callable[[NDArray[numpy.float64]], NDArray[numpy.float64]] | fiddy.constants.MethodId, function: collections.abc.Callable[[NDArray[numpy.float64]], NDArray[numpy.float64]], autorun: bool = True, completed: bool = False, results: list[fiddy.directional_derivative.ComputerResult] = <factory>, relative_size: bool = False)

Bases: object

autorun: bool = True
completed: bool = False
direction: NDArray[float64]
function: Callable[[NDArray[float64]], NDArray[float64]]
get_size()
method: Callable[[NDArray[float64]], NDArray[float64]] | MethodId
point: NDArray[float64]
relative_size: bool = False
results: list[ComputerResult]
size: float64
class fiddy.directional_derivative.ComputerResult(method_id: str, value: NDArray[numpy.float64], metadata: dict[str, typing.Any] = <factory>)

Bases: object

metadata: dict[str, Any]
method_id: str
value: NDArray[float64]
class fiddy.directional_derivative.DefaultBackward(function: Callable[[NDArray[float64]], NDArray[float64]])

Bases: TwoPointSlopeDirectionalDirection

The backward difference derivative.

get_points(point, direction, size)

Compute the two points used by the method.

:param See __call__().:

Returns:

The points at which the function will be evaluated.

id: MethodId = 'backward'
class fiddy.directional_derivative.DefaultCentral(function: Callable[[NDArray[float64]], NDArray[float64]])

Bases: TwoPointSlopeDirectionalDirection

The central difference derivative.

get_points(point, direction, size)

Compute the two points used by the method.

:param See __call__().:

Returns:

The points at which the function will be evaluated.

id: MethodId = 'central'
class fiddy.directional_derivative.DefaultForward(function: Callable[[NDArray[float64]], NDArray[float64]])

Bases: TwoPointSlopeDirectionalDirection

The forward difference derivative.

get_points(point, direction, size)

Compute the two points used by the method.

:param See __call__().:

Returns:

The points at which the function will be evaluated.

id: MethodId = 'forward'
class fiddy.directional_derivative.DefaultRichardson(*args, **kwargs)

Bases: DirectionalDerivativeBase

The Richardson extrapolation method.

Based on https://doi.org/10.48550/arXiv.2110.04335

Given some step size h and some order n, terms are computed as

\[A_{i,j} = \mathrm{Central\,Difference}\left(\mathrm{step\,size}= \frac{h}{2^{i-1}} \right)\]

if j = 1, and

\[A_{i,j} = \frac{4^{j-1} A_{i,j-1} - A_{i-1,j-1}}{4^{j-1} - 1}\]

otherwise.

The derivative is given by A at i=n, j=n.

Some basic caching is used, which is reset when a new derivative is requested.

compute(points: NDArray[float64], size: float64, direction: NDArray[float64])

Compute the directional derivative.

Parameters:

use. (The points to)

Returns:

The directional derivative.

get_points(point, direction, size)

Compute the two points used by the method.

:param See __call__().:

Returns:

The points at which the function will be evaluated.

get_term(i, j, **kwargs)
id: MethodId = 'richardson'
order = 4
reset_cache()
class fiddy.directional_derivative.DirectionalDerivative(id: str, direction: NDArray[numpy.float64], pending_computers: list[fiddy.directional_derivative.Computer], computers: list[fiddy.directional_derivative.Computer], analyses: list[collections.abc.Callable[['DirectionalDerivative'], typing.Any]], success_checker: collections.abc.Callable[['DirectionalDerivative'], bool] = <function DirectionalDerivative.<lambda> at 0x7969f68dfa60>, expected_result: fiddy.directional_derivative.ExpectedDirectionalDerivative = None, success: bool = False, value: numpy.float64 = nan, completed: bool = False, fast: bool = False, autorun: bool = True)

Bases: object

analyses: list[Callable[[DirectionalDerivative], Any]]
autorun: bool = True
check_success()
completed: bool = False
computers: list[Computer]
direction: NDArray[float64]
expected_result: ExpectedDirectionalDerivative = None
fast: bool = False
get_analysis_results()
get_computer_results()
id: str
iterate()
pending_computers: list[Computer]
run_analyses()
run_next_computer()
success: bool = False
success_checker()
value: float64 = nan
class fiddy.directional_derivative.DirectionalDerivativeBase(function: Callable[[NDArray[float64]], NDArray[float64]])

Bases: ABC

Base class for default implementations of directional derivatives.

function

The function.

abstract compute(points: list[NDArray[float64]])

Compute the directional derivative.

Parameters:

use. (The points to)

Returns:

The directional derivative.

abstract get_points(point: NDArray[float64], direction: NDArray[float64], size: float64)

Compute the two points used by the method.

:param See __call__().:

Returns:

The points at which the function will be evaluated.

id: MethodId
class fiddy.directional_derivative.ExpectedDirectionalDerivative(point: NDArray[numpy.float64], direction: NDArray[numpy.float64], size: numpy.float64, method: collections.abc.Callable[[NDArray[numpy.float64]], NDArray[numpy.float64]] | fiddy.constants.MethodId, function: collections.abc.Callable[[NDArray[numpy.float64]], NDArray[numpy.float64]], autorun: bool = True, completed: bool = False, results: list[fiddy.directional_derivative.ComputerResult] = <factory>, relative_size: bool = False)

Bases: Computer

autorun: bool = False
function: Callable[[NDArray[float64]], NDArray[float64]] = None
method: Callable[[NDArray[float64]], NDArray[float64]] | MethodId = None
size: float64 = None
class fiddy.directional_derivative.TwoPointSlopeDirectionalDirection(function: Callable[[NDArray[float64]], NDArray[float64]])

Bases: DirectionalDerivativeBase

Derivatives that are similar to a simple (y1-y0)/h slope function.

compute(points: list[NDArray[float64]], size: float64, **kwargs)

Compute the directional derivative.

Parameters:

use. (The points to)

Returns:

The directional derivative.

fiddy.directional_derivative.get_directions(point: NDArray[float64] = None, directions: list[NDArray[float64]] | dict[str, NDArray[float64]] = None, ids: list[str] = None, indices: list[int] = None) tuple[str, NDArray[float64]]

Get directions from minimal information.

Parameters:
  • point – The standard basis of this point may be used as directions.

  • directions – The direction vectors.

  • ids – The direction IDs.

  • indices – The indices of the standard basis to use as directions.

Returns:

  1. Direction IDs, and (2) directions.

fiddy.directional_derivative.standard_basis(point: NDArray[float64]) list[NDArray[float64]]

Get standard basis (Cartesian/one-hot) vectors.

Parameters:

point – The space of this point is used as the space for the standard basis.

Returns:

A

fiddy.function module

class fiddy.function.CachedFunction(function: Callable[[NDArray[float64]], NDArray[float64]], ram_cache: bool = False, **kwargs)

Bases: Function

Wrapper for functions to enable caching.

Cached data may persist, but can be removed by calling CachedFunction.delete_cache().

function

The function.

ram_cache_path

The path to the RAM cache, if requested.

delete_cache()
class fiddy.function.Function(function: Callable[[NDArray[float64]], NDArray[float64]])

Bases: object

Wrapper for functions.

fiddy.numpy module

fiddy.numpy.fiddy_array(a)

fiddy.step module

Methods related to moving along a finite difference direction.

fiddy.step.step(direction: NDArray[float64], size: float64)

Get a step.

Parameters:
  • direction – The direction to step in.

  • size – The size of the step.

Returns:

The step.

fiddy.success module

class fiddy.success.Consistency(computer_parser: Callable[[Computer], float | None] = None, analysis_parser: Callable[[Analysis], float | None] = None, rtol: float = 0.2, atol: float = 1e-15, equal_nan: bool = True)

Bases: Success

id: str = 'consistency'
method(directional_derivative: DirectionalDerivative) tuple[bool, float]
only_at_completion: bool = True
class fiddy.success.Success(method: Callable[[Derivative], bool | Any] = None)

Bases: object

id: str = None
abstract method(directional_derivative: DirectionalDerivative) Any

Module contents