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, 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 = 2.3025850929940455
method(directional_derivative: DirectionalDerivative) None
only_at_completion: bool = True
transform(value: ndarray[Any, dtype[float64]], scale: str, position: float64)

fiddy.constants module

class fiddy.constants.MethodId(value, names=None, *, 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[Any, dtype[float64]]

DERIVATIVE

alias of ndarray[Any, dtype[ndarray[Any, dtype[float64]]]]

DERIVATIVE_FUNCTION

alias of Callable[[ndarray[Any, dtype[float64]]], ndarray[Any, dtype[ndarray[Any, dtype[float64]]]]]

DIRECTION

alias of ndarray[Any, dtype[float64]]

DIRECTIONAL_DERIVATIVE

alias of ndarray[Any, dtype[float64]]

DIRECTIONAL_DERIVATIVE_FUNCTION

alias of Callable[[ndarray[Any, dtype[float64]]], ndarray[Any, dtype[float64]]]

FUNCTION

alias of Callable[[ndarray[Any, dtype[float64]]], ndarray[Any, dtype[float64]]]

FUNCTION_OUTPUT

alias of ndarray[Any, dtype[float64]]

POINT

alias of ndarray[Any, dtype[float64]]

SCALAR

alias of float64

SIZE

alias of float64

SUCCESS_CHECKER

alias of Callable[[Derivative], Union[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[Any, dtype[float64]]], ndarray[Any, dtype[float64]]], point: ndarray[Any, dtype[float64]], sizes: List[float64], method_ids: List[str | MethodId], success_checker: Success, *args, analysis_classes: List[Analysis] = None, relative_sizes: bool = False, directions: List[ndarray[Any, dtype[float64]]] | Dict[str, ndarray[Any, dtype[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[Any, dtype[ndarray[Any, dtype[float64]]]], point: ndarray[Any, dtype[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: numpy.ndarray[Any, numpy.dtype[numpy.ndarray[Any, numpy.dtype[numpy.float64]]]], expectation: numpy.ndarray[Any, numpy.dtype[numpy.ndarray[Any, numpy.dtype[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[Any, dtype[ndarray[Any, dtype[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[Any, dtype[ndarray[Any, dtype[float64]]]]

The value that was tested.

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

Bases: object

direction_id: str

The direction.

expectation: ndarray[Any, dtype[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[Any, dtype[float64]]

The value that was tested.

class fiddy.derivative_check.NumpyIsCloseDerivativeCheck(derivative: Derivative, expectation: ndarray[Any, dtype[ndarray[Any, dtype[float64]]]], point: ndarray[Any, dtype[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: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], direction: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], size: numpy.float64, method: Union[Callable[[numpy.ndarray[Any, numpy.dtype[numpy.float64]]], numpy.ndarray[Any, numpy.dtype[numpy.float64]]], fiddy.constants.MethodId], function: Callable[[numpy.ndarray[Any, numpy.dtype[numpy.float64]]], numpy.ndarray[Any, numpy.dtype[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[Any, dtype[float64]]
function: Callable[[ndarray[Any, dtype[float64]]], ndarray[Any, dtype[float64]]]
get_size()
method: Callable[[ndarray[Any, dtype[float64]]], ndarray[Any, dtype[float64]]] | MethodId
point: ndarray[Any, dtype[float64]]
relative_size: bool = False
results: List[ComputerResult]
size: float64
class fiddy.directional_derivative.ComputerResult(method_id: str, value: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], metadata: Dict[str, Any] = <factory>)

Bases: object

metadata: Dict[str, Any]
method_id: str
value: ndarray[Any, dtype[float64]]
class fiddy.directional_derivative.DefaultBackward(function: Callable[[ndarray[Any, dtype[float64]]], ndarray[Any, dtype[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[Any, dtype[float64]]], ndarray[Any, dtype[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[Any, dtype[float64]]], ndarray[Any, dtype[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[Any, dtype[float64]], size: float64, direction: ndarray[Any, dtype[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: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], pending_computers: List[fiddy.directional_derivative.Computer], computers: List[fiddy.directional_derivative.Computer], analyses: List[Callable[[ForwardRef('DirectionalDerivative')], Any]], success_checker: Callable[[ForwardRef('DirectionalDerivative')], bool] = <function DirectionalDerivative.<lambda> at 0x7fcab8521da0>, expected_result: fiddy.directional_derivative.ExpectedDirectionalDerivative = None, success: bool = False, value: numpy.float64 = None, 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[Any, dtype[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 = None
class fiddy.directional_derivative.DirectionalDerivativeBase(function: Callable[[ndarray[Any, dtype[float64]]], ndarray[Any, dtype[float64]]])

Bases: ABC

Base class for default implementations of directional derivatives.

function

The function.

abstract compute(points: List[ndarray[Any, dtype[float64]]])

Compute the directional derivative.

Parameters:

use. (The points to) –

Returns:

The directional derivative.

abstract get_points(point: ndarray[Any, dtype[float64]], direction: ndarray[Any, dtype[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: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], direction: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], size: numpy.float64, method: Union[Callable[[numpy.ndarray[Any, numpy.dtype[numpy.float64]]], numpy.ndarray[Any, numpy.dtype[numpy.float64]]], fiddy.constants.MethodId], function: Callable[[numpy.ndarray[Any, numpy.dtype[numpy.float64]]], numpy.ndarray[Any, numpy.dtype[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[Any, dtype[float64]]], ndarray[Any, dtype[float64]]] = None
method: Callable[[ndarray[Any, dtype[float64]]], ndarray[Any, dtype[float64]]] | MethodId = None
size: float64 = None
class fiddy.directional_derivative.TwoPointSlopeDirectionalDirection(function: Callable[[ndarray[Any, dtype[float64]]], ndarray[Any, dtype[float64]]])

Bases: DirectionalDerivativeBase

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

compute(points: List[ndarray[Any, dtype[float64]]], size: float64, **kwargs)

Compute the directional derivative.

Parameters:

use. (The points to) –

Returns:

The directional derivative.

fiddy.directional_derivative.get_directions(point: ndarray[Any, dtype[float64]] = None, directions: List[ndarray[Any, dtype[float64]]] | Dict[str, ndarray[Any, dtype[float64]]] = None, ids: List[str] = None, indices: List[int] = None) Tuple[str, ndarray[Any, dtype[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[Any, dtype[float64]]) List[ndarray[Any, dtype[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[Any, dtype[float64]]], ndarray[Any, dtype[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[Any, dtype[float64]]], ndarray[Any, dtype[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[Any, dtype[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) [<class 'bool'>, <class '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

fiddy.version module

Module contents