Repository: aeon-toolkit/aeon
Pull request #2897 (2025-06-11 13:33:36)

[BUG] Fixes a bug with SFAFast throwing an error when calling transform after fit
This PR fixes a bug with SFAFast throwing an error when calling `transform()` after `fit()`

As reported by @baraline SFAFast had the following error:

```python
from aeon.datasets import load_classification
from aeon.transformations.collection.dictionary_based import SFAFast
X_train, y_train = load_classification("SmoothSubspace", split="train")
SFAFast().fit(X_train, y_train).transform(X_train, y_train)
```

results in

```
File ~\Documents\aeon\aeon\transformations\collection\dictionary_based\_sfa_fast.py:1245, in create_bag_transform(X_index, feature_count, feature_selection, relevant_features, sfa_words, remove_repeat_words)
   1241         all_win_words[j, :] = np.bincount(
   1242             sfa_words[j][masked], minlength=feature_count
   1243         )
   1244     else:
-> 1245         all_win_words[j, :] = np.bincount(sfa_words[j], minlength=feature_count)
   1246 else:
   1247     if remove_repeat_words:

ValueError: could not broadcast input array from shape (20324,) into shape (0,)
```


This PR fixes the above problem and also adds a test case.

aeon-actions-bot[bot]: 
## Thank you for contributing to `aeon`


I have added the following labels to this PR based on the changes made: [ **transformations** ]. Feel free to change these if they do not properly represent the PR.

The [Checks](https://github.com/aeon-toolkit/aeon/pull/2897/checks) tab will show the status of our automated tests. You can click on individual test runs in the tab or "Details" in the panel below to see more information if there is a failure.

If our `pre-commit` code quality check fails, any trivial fixes will automatically be pushed to your PR unless it is a draft.

Don't hesitate to ask questions on the `aeon` [Slack](https://join.slack.com/t/aeon-toolkit/shared_invite/zt-36dlmbouu-vajTShUYAHopSXUUVtHGzw) channel if you have any.

<details><summary>PR CI actions</summary>
<p>

These checkboxes will add labels to enable/disable CI functionality for this PR. This may not take effect immediately, and a new commit may be required to run the new configuration.

- [ ] Run `pre-commit` checks for all files
- [ ] Run `mypy` typecheck tests
- [ ] Run all `pytest` tests and configurations
- [ ] Run all notebook example tests
- [ ] Run numba-disabled `codecov` tests
- [ ] Stop automatic `pre-commit` fixes (always disabled for drafts)
- [ ] Disable numba cache loading
- [ ] Push an empty commit to re-run CI checks

</p>
</details>
    

Patch:
diff --git a/aeon/transformations/collection/dictionary_based/_sfa_fast.py b/aeon/transformations/collection/dictionary_based/_sfa_fast.py
index 42c1b5d4..c118ce38 100644
--- a/aeon/transformations/collection/dictionary_based/_sfa_fast.py
+++ b/aeon/transformations/collection/dictionary_based/_sfa_fast.py
@@ -329,7 +329,7 @@ class SFAFast(BaseCollectionTransformer):
         self: object
         """
         # with parallel_backend("loky", inner_max_num_threads=n_jobs):
-        self._fit_transform(X, y, return_bag_of_words=False)
+        self._fit_transform(X, y, return_bag_of_words=self.return_sparse)
         return self
 
     def _transform(self, X, y=None):


Test patch:
diff --git a/aeon/transformations/collection/dictionary_based/tests/test_sfa.py b/aeon/transformations/collection/dictionary_based/tests/test_sfa.py
index 7304f38e..1ec2aba8 100644
--- a/aeon/transformations/collection/dictionary_based/tests/test_sfa.py
+++ b/aeon/transformations/collection/dictionary_based/tests/test_sfa.py
@@ -5,7 +5,8 @@ import sys
 import numpy as np
 import pytest
 
-from aeon.transformations.collection.dictionary_based._sfa import SFA
+from aeon.datasets import load_unit_test
+from aeon.transformations.collection.dictionary_based import SFA, SFAFast
 
 
 @pytest.mark.parametrize(
@@ -224,3 +225,27 @@ def test_typed_dict():
     word_list2 = p2.bag_to_string(p2.transform(X, y)[0][0])
 
     assert word_list == word_list2
+
+
+def test_sfa_fast_transform_after_fit():
+    """Test transform called after fit returns the same result as fit_transform()."""
+    X_train, y_train = load_unit_test(split="train")
+
+    # Fit, then transform
+    sfa = SFAFast()
+    sfa.fit(X_train, y_train)
+    x = sfa.transform(X_train, y_train)
+
+    # Fit_transform, then transform
+    sfa = SFAFast()
+    sfa.fit_transform(X_train, y_train)
+    y = sfa.transform(X_train, y_train)
+
+    # Assert that the two csr_matrix are equal
+    assert (
+        x.shape == y.shape
+        and x.dtype == y.dtype
+        and np.all(x.indices == y.indices)
+        and np.all(x.indptr == y.indptr)
+        and np.allclose(x.data, y.data)
+    )