distancematrix.util

Module Contents

Functions

diag_length(h, w, diagonal=0)

Returns the number of elements on the specified diagonal of a matrix with dimensions (h, w).

diag_indices(h, w, diagonal=0)

Returns the indices of the elements on the specified diagonal of a matrix with dimensions (h, w).

diag_indices_of(array, diagonal=0)

Returns the indices of the elements on the specified diagonal of the given matrix.

cut_indices_of(array, cut)

Calculates the indices of the elements on the given cut for the given matrix.

shortest_path_distances(cost_array)

Creates a new array of the same shape, where each entry contains the lowest sum of elements on the path

shortest_path(cost_array)

Finds the shortest (= least summed cost) path from the top left of the array to the bottom right.

sliding_min(array, window_size)

sliding_max(array, window_size)

sliding_window_view(x, shape, step=None, subok=False, writeable=False)

Create sliding window views of the N dimensions array with the given window

distancematrix.util.diag_length(h, w, diagonal=0)

Returns the number of elements on the specified diagonal of a matrix with dimensions (h, w).

Parameters
  • h – int, height of the matrix

  • w – int, width of the matrix

  • diagonal – int, diagonal index of the matrix

Returns

a positive integer, zero if diagonal fall completely outside the matrix

distancematrix.util.diag_indices(h, w, diagonal=0)

Returns the indices of the elements on the specified diagonal of a matrix with dimensions (h, w).

Parameters
  • h – int, height of the matrix

  • w – int, width of the matrix

  • diagonal – int, diagonal index of the matrix

Returns

a tuple of ranges, serving as indices of the elements

distancematrix.util.diag_indices_of(array, diagonal=0)

Returns the indices of the elements on the specified diagonal of the given matrix.

Parameters
  • array – 2D array

  • diagonal – int, diagonal index of the matrix

Returns

a tuple of ranges, serving as indices of the elements

distancematrix.util.cut_indices_of(array, cut)

Calculates the indices of the elements on the given cut for the given matrix. Where a diagonal runs from top left to bottom right, a cut runs from bottom left to top right.

Parameters
  • array – 2D array

  • cut – index of the cut (cut 0 is the single element of the top left)

Returns

the indices to retrieve the cut

distancematrix.util.shortest_path_distances(cost_array)

Creates a new array of the same shape, where each entry contains the lowest sum of elements on the path from (0, 0) to that entry. Steps in the path can go horizontal, vertical and diagonal.

Parameters

cost_array – 2D array containing only positives

Returns

a new array

distancematrix.util.shortest_path(cost_array)

Finds the shortest (= least summed cost) path from the top left of the array to the bottom right.

Parameters

cost_array – 2D array containing only positives

Returns

array of indices, starting from the top left (index: [0, 0])

distancematrix.util.sliding_min(array, window_size)
distancematrix.util.sliding_max(array, window_size)
distancematrix.util.sliding_window_view(x, shape, step=None, subok=False, writeable=False)

Create sliding window views of the N dimensions array with the given window shape. Window slides across each dimension of x and provides subsets of x at any window position.

sliding_window_view create sliding window views of the N dimensions array with the given window shape and its implementation based on as_strided. Please note that if writeable set to False, the return is views, not copies of array. In this case, write operations could be unpredictable, so the return views is readonly. Bear in mind, return copies (writeable=True), could possibly take memory multiple amount of origin array, due to overlapping windows.

For some cases, there may be more efficient approaches

Parameters
  • x – ndarray Array to create sliding window views.

  • shape – sequence of int The shape of the window. Must have same length as number of input array dimensions.

  • step – sequence of int, optional The steps of window shifts for each dimension on input array at a time. If given, must have same length as number of input array dimensions. Defaults to 1 on all dimensions.

  • subok – bool, optional If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).

  • writeable – bool, optional If set to False, the returned array will always be readonly view. Otherwise it will return writable copies(see Notes).

Returns

ndarray Sliding window views (or copies) of x. view.shape = (x.shape - shape) // step + 1