Skip to content

util

Functions:

NameDescription
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).
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 up to a specified maximum lag.
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.
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 up to a specified maximum lag.

Parameters:

NameTypeDescriptionDefault
xndarrayThe input array for which to compute the autocorrelation.required
max_lagintThe maximum lag up to which the autocorrelation is computed.required
stepintThe step size for the lag. Default is 1.1

Returns:

TypeDescription
ndarrayArray of shape (max_lag // step + 1,) containing the autocorrelation values.