weave.distance#

Calculate the distance between points.

Notes

In general, distance functions \(d(x, y)\) should satisfy the following properties [1]:

  1. \(d(x, y)\) is real-valued, finite, and nonnegative

  2. \(d(x, y) = 0\) if and only if \(x = y\)

  3. \(d(x, y) = d(y, x)\) (symmetry)

  4. \(d(x, y) \leq d(x, z) + d(z, y)\) (triangle inequality)

References

weave.distance.euclidean(x, y)[source]#

Get Euclidean distance between x and y.

Points x and y are specified as vectors of coordinate values.

Parameters:
  • x (1D numpy.ndarray) – Current point.

  • y (1D numpy.ndarray) – Nearby point.

Returns:

Euclidean distance between x and y.

Return type:

nonnegative numpy.float32

Notes

For a pair of points with n coordinates, this function computes the n-dimensional Euclidean distance [2]:

\[d(x, y) = \sqrt{(x_1 - y_1)^2 + (x_2 - y_2)^2 + \dots + (x_n - y_n)^2}\]

If \(n = 1\), this is equivalent to the absolute value of the difference between points:

\[d(x, y) = |x - y|\]

References

Examples

Get Euclidean distances between points.

>>> import numpy as np
>>> from weave.distance import euclidean
>>> euclidean(np.array([0]), np.array([1]))
1.0
>>> euclidean(np.array([0, 0]), np.array([1, 2]))
2.236068
>>> euclidean(np.array([0, 0, 0]), np.array([1, 2, 3]))
3.7416575
weave.distance.tree(x, y)[source]#

Get tree distance between x and y.

Points x and y are specified as vectors of IDs corresponding to nodes in a tree, starting with the root node and ending at the leaf node. The distance between two points is defined as the number of edges between the leaves and their nearest common ancestor.

If x and y have different roots (i.e., points are not from the same tree, then the length of the points is returned.

Parameters:
  • x (1D numpy.ndarray) – Current point.

  • y (1D numpy.ndarray) – Nearby point.

Returns:

Tree distance between x and y.

Return type:

nonnegative numpy.float32

Examples

Get tree distances between leaf nodes from the following tree.

../_images/tree.png
>>> import numpy as np
>>> from weave.distance import tree
>>> tree(np.array([1, 2, 4]), np.array([1, 2, 4]))
0.0
>>> tree(np.array([1, 2, 4]), np.array([1, 2, 5]))
1.0
>>> tree(np.array([1, 2, 4]), np.array([1, 3, 6]))
2.0
>>> tree(np.array([1, 2, 4]), np.array([7, 8, 9]))
3.0