Skip to content

util

Functions:

NameDescription
materializeEvaluate mask on every index pair, giving the dense boolean array of shape, such as (M, N) or (B, M, N).
padPad the np.ndarray in Xs to merge them into a single np.ndarray.
pairwise_distanceCompute the pairwise squared Euclidean distance between points in A (or between points in A and B).
topkComputes the top-k elements of the tensor along the specified dim.
pairwise_distance_npCompute the pairwise squared Euclidean distance between points in A (or between points in A and B).
dtwComputes the Dynamic Time Warping (DTW) distance between two sequences x and y.
autocorrelationComputes the autocorrelation of a given 1D numpy array at lags below a specified maximum lag.
materialize(mask: Mask, shape: Sequence[SupportsInt]) -> np.ndarray

Evaluate mask on every index pair, giving the dense boolean array of shape, such as (M, N) or (B, M, N).

pad(As: list[np.ndarray])

Pad the np.ndarray in Xs to merge them into a single np.ndarray.

Parameters:

NameTypeDescriptionDefault
Aslist[ndarray]List of arrays of shape (N, D_i).required

Returns:

TypeDescription
ndarraySingle array of shape (B, N, max(D)) where B is len(As).

Raises:

TypeDescription
ValueError- If any array in As is not 2D. - If the first dimension of all arrays in As are not equal.
pairwise_distance(A: Tensor, B: Tensor | None = None) -> Tensor

Compute the pairwise squared Euclidean distance between points in A (or between points in A and B).

Parameters:

NameTypeDescriptionDefault
ATensorShape (N, D) or (B, N, D). B is batch size, N is number of points, D is dimension of each point.required
BTensorShape (M, D) or (B, M, D). B is batch size, M is number of points, D is dimension of each point.None

Returns:

TypeDescription
TensorWhen A is of shape (N, D): shape (N, N) [or (N, M)] where the element at position (i, j) is the squared Euclidean distance between A[i] and A[j] [or between A[i] and B[j]]. When A is of shape (B, N, D): shape (B, N, N) [or (B, N, M)] where the element at position (b, i, j) is the squared Euclidean distance between A[b, i] and A[b, j].

Raises:

TypeDescription
ValueError- If A is not a 2D or 3D tensor. - If B is not None and A and B have different number of dimensions.
topk(A: Tensor, k: int, dim: int = -1, largest: bool = True, sorted_: bool = True) -> tuple[Tensor, Tensor]

Computes the top-k elements of the tensor along the specified dim.

Order of indices for equivalent elements is always preserved.

Parameters:

NameTypeDescriptionDefault
ATensorInput tensor.required
kintThe number of elements to take, at most A.shape[dim].required
dimintThe dimension along which to take the elements.-1
largestboolTake the largest elements if True, otherwise the smallest.True
sorted_boolMust be True; the elements are returned in order, as Tensor.topk requires.True

Returns:

NameTypeDescription
valuesTensorThe top-k elements, ordered from the most extreme, with A.shape[dim] replaced by k.
indicesTensorTheir indices along dim, of the same shape as values. Equal elements keep their order.

Raises:

TypeDescription
NotImplementedError- If sorted_ is False.
ValueError- If k exceeds A.shape[dim].
pairwise_distance_np(A: np.ndarray, B: np.ndarray | None = None) -> np.ndarray

Compute the pairwise squared Euclidean distance between points in A (or between points in A and B).

Parameters:

NameTypeDescriptionDefault
AndarrayShape (N, D) or (B, N, D). B is batch size, N is number of points, D is dimension of each point.required
BndarrayShape (M, D) or (B, M, D). B is batch size, M is number of points, D is dimension of each point.None

Returns:

TypeDescription
ndarrayWhen A is of shape (N, D): shape (N, N) [or (N, M)] where the element at position (i, j) is the squared Euclidean distance between A[i] and A[j] [or between A[i] and B[j]]. When A is of shape (B, N, D): shape (B, N, N) [or (B, N, M)] where the element at position (b, i, j) is the squared Euclidean distance between A[b, i] and A[b, j].

Raises:

TypeDescription
ValueError- If A is not a 2D or 3D array. - If B is not None and A and B have different number of dimensions.
dtw(A: np.ndarray, B: np.ndarray)

Computes the Dynamic Time Warping (DTW) distance between two sequences x and y.

Parameters:

NameTypeDescriptionDefault
AndarraySequence of shape (N, D).required
BndarraySequence of shape (M, D).required

Returns:

NameTypeDescription
distancefloatThe DTW distance between the two sequences.
autocorrelation(x: np.ndarray, max_lag: int, step: int = 1)

Computes the autocorrelation of a given 1D numpy array at lags below a specified maximum lag.

Parameters:

NameTypeDescriptionDefault
xndarrayThe input array for which to compute the autocorrelation.required
max_lagintExclusive upper bound of the lags, also capped at len(x).required
stepintThe step size for the lag. Default is 1.1

Returns:

TypeDescription
ndarrayAutocorrelation at lags range(0, min(max_lag, len(x)), step).