spiketools.spatial.distance.get_closest_position

spiketools.spatial.distance.get_closest_position(position, location, threshold=None)[source]

Get the index of the closest position value to a specified location.

Parameters:
position1d or 2d array

Position values.

locationlist of float

The position values of the two positions to calculate distance between. Can be 1d (a single value per position) or 2d (x and y values per position).

thresholdfloat, optional

The threshold that the closest value must be within to be returned. If the distance is greater than the threshold, output is -1.

Returns:
min_indint

The index of the closest position value to the specified location.

Notes

If there are multiple positions that are equivalently close to the location, this function will return the index of first one.

Examples

Get the index of the closest 1d position to a specified location:

>>> position = np.array([1., 2., 3., 4., 5.])
>>> location = [3.]
>>> get_closest_position(position, location)
2

Get the index of the closest 2d position to a specified location:

>>> position = np.array([[1, 2, 2, 3],
...                      [1, 1, 2, 3]])
>>> location = [2, 2]
>>> get_closest_position(position, location)
2