Skip to content

[BUG] Fixes sbd distances for multivariate case #2742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aeon/distances/_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ class DistanceType(Enum):
"pairwise_distance": sbd_pairwise_distance,
"type": DistanceType.CROSS_CORRELATION,
"symmetric": True,
"unequal_support": True,
"unequal_support": False,
},
{
"name": "shift_scale",
Expand Down
25 changes: 19 additions & 6 deletions aeon/distances/_sbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ def sbd_distance(x: np.ndarray, y: np.ndarray, standardize: bool = True) -> floa
return _univariate_sbd_distance(_x, _y, standardize)
else:
# independent (time series should have the same number of channels!)
nchannels = min(x.shape[0], y.shape[0])
distance = 0.0
if x.shape[0] != y.shape[0]:
raise ValueError("x and y must have the same number of channels ")
nchannels = x.shape[0] # both x and y have the same number of channels
norm = np.linalg.norm(x.astype(np.float64)) * np.linalg.norm(
y.astype(np.float64)
)
distance = np.zeros((2 * x.shape[1] - 1,))
for i in range(nchannels):
distance += _univariate_sbd_distance(x[i], y[i], standardize)
return distance / nchannels
distance += _helper_sbd(x[i], y[i], standardize)
return np.abs(1 - np.max(distance) / norm)
Comment on lines +107 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do other distances have this in a separate function like the univariate one?


raise ValueError("x and y must be 1D or 2D")

Expand Down Expand Up @@ -240,8 +245,16 @@ def _univariate_sbd_distance(x: np.ndarray, y: np.ndarray, standardize: bool) ->
x = (x - np.mean(x)) / np.std(x)
y = (y - np.mean(y)) / np.std(y)

with objmode(a="float64[:]"):
a = correlate(x, y, method="fft")
a = _helper_sbd(x, y, standardize)

b = np.sqrt(np.dot(x, x) * np.dot(y, y))
return np.abs(1.0 - np.max(a / b))


@njit(cache=True, fastmath=True)
def _helper_sbd(x, y, standardize):

with objmode(a="float64[:]"):
a = correlate(x, y, method="fft")
Comment on lines +257 to +258
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really a fan of this but it is the current functionality. Maybe worth an issue.


return a
4 changes: 2 additions & 2 deletions aeon/testing/expected_results/expected_distance_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
0.6617308353925114,
0.6617308353925114,
0.5750093257763462,
0.5263609881742105,
None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this None? The comment in the file does not seem to line up with the values seen.

0.0,
],
"shift_scale": [
Expand Down Expand Up @@ -185,7 +185,7 @@
[8.602610210695161, 8.645028399102344],
[1.750534284134988, 12.516745017325773],
],
"sbd": [[0.2435580798173309, 0.18613277150939772]],
"sbd": [[0.2435580798173309, 0.21430477859140418]],
"shift_scale": [
[0.8103639073457298, 5.535457073146429],
[0.6519267432870345, 5.491208968546096],
Expand Down