wepy.resampling.distances.distance module

Modular component for defining distance metrics usable within many different resamplers.

This module contains an abstract base class for Distance classes.

The suggested implementation method is to leave the ‘distance’ method as is, and override the ‘image’ and ‘image_distance’ methods instead. Because the default ‘distance’ method calls these transparently. The resamplers will use the ‘image’ and ‘image_distance’ calls because this allows performance optimizations.

For example in WExplore the images for some walkers end up being stored as the definitions of Voronoi regions, and if the whole walker state was stored it would not only use much more space in memory but require that common transformations be repeated every time a distance is to be calculated to that image (which is many times). In REVO all to all distances between walkers are computed which would also incur a high cost.

So use the ‘image’ to do precomputations on raw walker states and use the ‘image_distance’ to compute distances using only those images.

class wepy.resampling.distances.distance.Distance[source]

Bases: object

Abstract Base class for Distance classes.

Constructor for Distance class.

image(state)[source]

Compute the ‘image’ of a walker state which should be some transformation of the walker state that is more convenient. E.g. for precomputation of expensive operations or for saving as resampler state.

The abstract implementation is naive and just returns the state itself, thus it is the identity function.

Parameters

state (object implementing WalkerState) – The state which will be transformed to an image

Returns

image – The same state that was given as an argument.

Return type

object implementing WalkerState

image_distance(image_a, image_b)[source]

Compute the distance between two images of walker states.

Parameters
  • image_a (object produced by Distance.image) –

  • image_b (object produced by Distance.image) –

Returns

distance – The distance between the two images

Return type

float

:raises NotImplementedError : always because this is abstract:

distance(state_a, state_b)[source]

Compute the distance between two states.

Parameters
  • state_a (object implementing WalkerState) –

  • state_b (object implementing WalkerState) –

Returns

distance – The distance between the two walker states

Return type

float

class wepy.resampling.distances.distance.XYEuclideanDistance[source]

Bases: wepy.resampling.distances.distance.Distance

2 dimensional euclidean distance between points.

States have the attributes ‘x’ and ‘y’.

Constructor for Distance class.

image(state)[source]

Compute the ‘image’ of a walker state which should be some transformation of the walker state that is more convenient. E.g. for precomputation of expensive operations or for saving as resampler state.

The abstract implementation is naive and just returns the state itself, thus it is the identity function.

Parameters

state (object implementing WalkerState) – The state which will be transformed to an image

Returns

image – The same state that was given as an argument.

Return type

object implementing WalkerState

image_distance(image_a, image_b)[source]

Compute the distance between two images of walker states.

Parameters
  • image_a (object produced by Distance.image) –

  • image_b (object produced by Distance.image) –

Returns

distance – The distance between the two images

Return type

float

:raises NotImplementedError : always because this is abstract:

distance(state_a, state_b)

Compute the distance between two states.

Parameters
  • state_a (object implementing WalkerState) –

  • state_b (object implementing WalkerState) –

Returns

distance – The distance between the two walker states

Return type

float