Skip to content

[ENH] Add Dynamic Alphabet Sizes for SFA #2844

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 17 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
53 changes: 53 additions & 0 deletions aeon/distances/tests/test_symbolic_mindist.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,56 @@ def test_sfa_whole_mindist():
assert mindist_sfa <= ed
assert mindist_dft_sfa >= mindist_sfa # a tighter lower bound
assert mindist_dft_sfa <= ed


def test_dynamic_alphabet_allocation():
"""Test the SFA Min-Distance function."""
n_segments = 16
alphabet_size = 64

X_train, _ = load_unit_test("TRAIN")
X_test, _ = load_unit_test("TEST")

X_train = zscore(X_train.squeeze(), axis=1)
X_test = zscore(X_test.squeeze(), axis=1)
histogram_type = "equi-width"

# print("Testing")
Copy link
Member

Choose a reason for hiding this comment

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

Left over comment

for alphabet_allocation_method in {
"linear_scale",
"log_scale",
"sqrt_scale",
}:
sfa = SFAWhole(
word_length=n_segments,
alphabet_size=alphabet_size,
binning_method=histogram_type,
alphabet_allocation_method=alphabet_allocation_method,
variance=True, # True gives a tighter lower bound
norm=True,
)

X_train_words, X_train_dfts = sfa.fit_transform(X_train)
X_test_words, _ = sfa.transform(X_test)

for i in range(min(X_train.shape[0], X_test.shape[0])):
X = X_train[i].reshape(1, -1)
Y = X_test[i].reshape(1, -1)

# SFA Min-Distance
mindist_sfa = mindist_sfa_distance(
X_train_words[i], X_test_words[i], sfa.breakpoints
)

# DFT-SFA Min-Distance
mindist_dft_sfa = mindist_dft_sfa_distance(
X_train_dfts[i], X_test_words[i], sfa.breakpoints
)

# Euclidean Distance
ed = np.linalg.norm(X[0] - Y[0])

assert np.mean(np.log2(sfa.alphabet_sizes)) == np.log2(alphabet_size)
assert mindist_sfa <= ed
assert mindist_dft_sfa >= mindist_sfa # a tighter lower bound
assert mindist_dft_sfa <= ed
Loading