-
Notifications
You must be signed in to change notification settings - Fork 222
[ENH] Make transformers inherit TransformerMixin and add CollectionToSeriesWrapper
#2812
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
MatthewMiddlehurst
wants to merge
19
commits into
main
Choose a base branch
from
mm/transformer-mixin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
01eb337
transformer mixin, composables and pipeline fix
MatthewMiddlehurst 494e3c6
transform changes
MatthewMiddlehurst cda1312
Merge remote-tracking branch 'origin/main' into mm/transformer-mixin
MatthewMiddlehurst 8aaafe5
fixing tests
MatthewMiddlehurst 12e268f
Merge remote-tracking branch 'origin/main' into mm/transformer-mixin
MatthewMiddlehurst 8c7e5f6
Merge remote-tracking branch 'origin/main' into mm/transformer-mixin
MatthewMiddlehurst 7e3ce1a
Merge remote-tracking branch 'origin/main' into mm/transformer-mixin
MatthewMiddlehurst 1a3e821
Merge remote-tracking branch 'origin/main' into mm/transformer-mixin
MatthewMiddlehurst daf56f2
skip test
MatthewMiddlehurst 91513c0
no mixin
MatthewMiddlehurst 5e434de
yes mixin
MatthewMiddlehurst 931eee6
Merge branch 'main' into mm/transformer-mixin
MatthewMiddlehurst 171689a
Merge remote-tracking branch 'origin/main' into mm/transformer-mixin
MatthewMiddlehurst f1c1ec6
Merge remote-tracking branch 'origin/mm/transformer-mixin' into mm/tr…
MatthewMiddlehurst de1d3d0
docstring
MatthewMiddlehurst 112b62e
Merge remote-tracking branch 'origin/main' into mm/transformer-mixin
MatthewMiddlehurst 126bbda
import
MatthewMiddlehurst 10240de
Merge remote-tracking branch 'origin/main' into mm/transformer-mixin
MatthewMiddlehurst 3818d0f
doctest
MatthewMiddlehurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """Class to wrap a collection transformer for single series.""" | ||
|
|
||
| __maintainer__ = ["MatthewMiddlehurst"] | ||
| __all__ = ["CollectionToSeriesWrapper"] | ||
|
|
||
|
|
||
| from aeon.transformations.collection.base import BaseCollectionTransformer | ||
| from aeon.transformations.series.base import BaseSeriesTransformer | ||
|
|
||
|
|
||
| class CollectionToSeriesWrapper(BaseSeriesTransformer): | ||
| """Wrap a ``BaseCollectionTransformer`` to run on single series datatypes. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| transformer : BaseCollectionTransformer | ||
| The collection transformer to wrap. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> from aeon.transformations.series import CollectionToSeriesWrapper | ||
| >>> from aeon.transformations.collection.unequal_length import Resizer | ||
| >>> import numpy as np | ||
| >>> X = np.random.rand(1, 10) | ||
| >>> transformer = Resizer(resized_length=5) | ||
| >>> wrapper = CollectionToSeriesWrapper(transformer) | ||
| >>> X_t = wrapper.fit_transform(X) | ||
| """ | ||
|
|
||
| # These tags are not set from the collection transformer. | ||
| _tags = { | ||
| "input_data_type": "Series", | ||
| "output_data_type": "Series", | ||
| "capability:inverse_transform": True, | ||
| "X_inner_type": "np.ndarray", | ||
| } | ||
|
|
||
| def __init__( | ||
| self, | ||
| transformer: BaseCollectionTransformer, | ||
| ) -> None: | ||
| self.transformer = transformer | ||
|
|
||
| super().__init__(axis=1) | ||
|
|
||
| # Setting tags before __init__() causes them to be overwritten. | ||
| tags_to_keep = CollectionToSeriesWrapper._tags | ||
| tags_to_add = transformer.get_tags() | ||
| for key in tags_to_keep: | ||
| tags_to_add.pop(key, None) | ||
| for key in ["capability:unequal_length", "removes_unequal_length"]: | ||
| tags_to_add.pop(key, None) | ||
| self.set_tags(**tags_to_add) | ||
|
|
||
| def _fit(self, X, y=None): | ||
| X = X.reshape(1, X.shape[0], X.shape[1]) | ||
| self.collection_transformer_ = self.transformer.clone() | ||
| self.collection_transformer_.fit(X, y) | ||
|
|
||
| def _transform(self, X, y=None): | ||
| X = X.reshape(1, X.shape[0], X.shape[1]) | ||
|
|
||
| t = self.transformer | ||
| if not self.get_tag("fit_is_empty"): | ||
| t = self.collection_transformer_ | ||
|
|
||
| return t.transform(X, y) | ||
|
|
||
| def _fit_transform(self, X, y=None): | ||
| X = X.reshape(1, X.shape[0], X.shape[1]) | ||
| self.collection_transformer_ = self.transformer.clone() | ||
| return self.collection_transformer_.fit_transform(X, y) | ||
|
|
||
| def _inverse_transform(self, X, y=None): | ||
| X = X.reshape(1, X.shape[0], X.shape[1]) | ||
|
|
||
| t = self.transformer | ||
| if not self.get_tag("fit_is_empty"): | ||
| t = self.collection_transformer_ | ||
|
|
||
| return t.inverse_transform(X, y) | ||
|
|
||
| @classmethod | ||
| def _get_test_params(cls, parameter_set="default"): | ||
| """Return testing parameter settings for the estimator. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| parameter_set : str, default="default" | ||
| Name of the set of test parameters to return, for use in tests. If no | ||
| special parameters are defined for a value, will return `"default"` set. | ||
|
|
||
| Returns | ||
| ------- | ||
| params : dict or list of dict, default={} | ||
| Parameters to create testing instances of the class. | ||
| Each dict are parameters to construct an "interesting" test instance, i.e., | ||
| `MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance. | ||
| """ | ||
| from aeon.testing.mock_estimators._mock_collection_transformers import ( | ||
| MockCollectionTransformer, | ||
| ) | ||
|
|
||
| return {"transformer": MockCollectionTransformer()} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| """Tests for SeriesToCollectionBroadcaster transformer.""" | ||
|
|
||
| from aeon.testing.mock_estimators import MockCollectionTransformer | ||
| from aeon.transformations.series import CollectionToSeriesWrapper | ||
|
|
||
|
|
||
| def test_broadcaster_tag_inheritance(): | ||
| """Test the ability to inherit tags from the BaseCollectionTransformer. | ||
|
|
||
| The broadcaster should always keep some tags related to single series | ||
| """ | ||
| trans = MockCollectionTransformer() | ||
| class_tags = CollectionToSeriesWrapper._tags | ||
|
|
||
| bc = CollectionToSeriesWrapper(trans) | ||
|
|
||
| post_constructor_tags = bc.get_tags() | ||
| mock_tags = trans.get_tags() | ||
| # constructor_tags should match class_tags or, if not present, tags in transformer | ||
| for key in post_constructor_tags: | ||
| if key in class_tags: | ||
| assert post_constructor_tags[key] == class_tags[key] | ||
| elif key in mock_tags: | ||
| assert post_constructor_tags[key] == mock_tags[key] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.