Skip to content

Commit ac7f477

Browse files
TinaJin0228MatthewMiddlehurstTonyBagnallinclinedadarshkevinzb56
authored
[ENH] add a difference transformer to series transformations (#2729)
* add difference transformer to series transformations * add myself to .all-contributorsrc * small modification * modify according to reviews * [ENH] add informational tags back to estimator docs (#2652) * feat: add new tags information for anomaly detection * feat: add tag information in given anomaly modles * fix: add new anomaly detector tags in mock anomaly detector class * fix: use anomaly-detector instead of estimator for new tags * update and add basic testing * fixes --------- Co-authored-by: MatthewMiddlehurst <pfm15hbu@gmail.com> * [DOC] Documentation improvement of BaseSeriesEstimator and BaseAeonEstimator (#2612) * Added the docs for base series and base classes * Updated documentation for base classes --------- Co-authored-by: Matthew Middlehurst <pfm15hbu@gmail.com> * [ENH] HydraTransformer Output changed (#2656) * output type added * hydra wflow modified * hydra test file added * pr_pytest reverted * soft dep check added * soft dep corrected * docstring corrected, enhanced test cases * [DOC] Improved Hidalgo Segmentation Notebook (#2616) * Improved Hidalgo Segmentation Notebook * Updated hidalgo notebook --------- Co-authored-by: Matthew Middlehurst <pfm15hbu@gmail.com> * new devs and workflow consolidation (#2797) * all branches (#2809) * Automated `pre-commit` hook update (#2820) Co-authored-by: TonyBagnall <9594042+TonyBagnall@users.noreply.github.com> * [MNT] Bump crs-k/stale-branches in the github-actions group (#2821) Bumps the github-actions group with 1 update: [crs-k/stale-branches](https://github.com/crs-k/stale-branches). Updates `crs-k/stale-branches` from 7.0.1 to 8.1.1 - [Release notes](https://github.com/crs-k/stale-branches/releases) - [Commits](crs-k/stale-branches@v7.0.1...v8.1.1) --- updated-dependencies: - dependency-name: crs-k/stale-branches dependency-version: 8.1.1 dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * fix: indexing in kdtw (#2826) * [ENH] Added test cases for feature based clustering (#2690) * Added test cases for feature based clustering * Automatic `pre-commit` fixes * added docstring * Automatic `pre-commit` fixes * added . * added . * updated tsfresh * Automatic `pre-commit` fixes * added pytest _check_soft_dependencies for tsfresh * Automatic `pre-commit` fixes * added more testing * Automatic `pre-commit` fixes * added docs * added docs * added pytest * Automatic `pre-commit` fixes * improved docs * Automatic `pre-commit` fixes * improved test cases * changes made as requested by moderators * changes made as requested by moderators * Automatic `pre-commit` fixes * changed "percentiles" to "quantiles" --------- Co-authored-by: Ramana-Raja <ramanarajakesavaraja@gamil.com> Co-authored-by: Matthew Middlehurst <pfm15hbu@gmail.com> Co-authored-by: Tony Bagnall <a.j.bagnall@soton.ac.uk> * [DOC] Fix inconsistent double backticks in updated similarity_search module docstrings (#2618) * delete some descriptions * modification to _maintainer_ --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Matthew Middlehurst <pfm15hbu@gmail.com> Co-authored-by: Tony Bagnall <a.j.bagnall@soton.ac.uk> Co-authored-by: Adarsh Dubey <dubeyadarshmain@gmail.com> Co-authored-by: Kevin Shah <161136814+kevinzb56@users.noreply.github.com> Co-authored-by: Balgopal Moharana <99070111+lucifer4073@users.noreply.github.com> Co-authored-by: Kavya Rambhia <161142013+kavya-r30@users.noreply.github.com> Co-authored-by: aeon-actions-bot[bot] <148872591+aeon-actions-bot[bot]@users.noreply.github.com> Co-authored-by: TonyBagnall <9594042+TonyBagnall@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Sebastian Schmidl <10573700+SebastianSchmidl@users.noreply.github.com> Co-authored-by: Ramana Raja <83065061+Ramana-Raja@users.noreply.github.com> Co-authored-by: Ramana-Raja <ramanarajakesavaraja@gamil.com> Co-authored-by: Ciph3r <142653450+adityagh006@users.noreply.github.com>
1 parent cf4bf29 commit ac7f477

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,16 @@
27102710
"contributions": [
27112711
"doc"
27122712
]
2713+
},
2714+
{
2715+
"login": "TinaJin0228",
2716+
"name": "Tina Jin",
2717+
"avatar_url": "https://avatars.githubusercontent.com/TinaJin0228",
2718+
"profile": "https://github.com/TinaJin0228",
2719+
"contributions": [
2720+
"code",
2721+
"doc"
2722+
]
27132723
}
27142724
],
27152725
"commitType": "docs"

aeon/transformations/series/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"SIVSeriesTransformer",
2222
"PCASeriesTransformer",
2323
"WarpingSeriesTransformer",
24+
"DifferenceTransformer",
2425
]
2526

2627
from aeon.transformations.series._acf import (
@@ -32,6 +33,7 @@
3233
from aeon.transformations.series._boxcox import BoxCoxTransformer
3334
from aeon.transformations.series._clasp import ClaSPTransformer
3435
from aeon.transformations.series._dft import DFTSeriesTransformer
36+
from aeon.transformations.series._diff import DifferenceTransformer
3537
from aeon.transformations.series._dobin import Dobin
3638
from aeon.transformations.series._exp_smoothing import ExpSmoothingSeriesTransformer
3739
from aeon.transformations.series._gauss import GaussSeriesTransformer

aeon/transformations/series/_diff.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import numpy as np
2+
3+
from aeon.transformations.series.base import BaseSeriesTransformer
4+
5+
__maintainer__ = ["TinaJin0228"]
6+
__all__ = ["DifferenceTransformer"]
7+
8+
9+
class DifferenceTransformer(BaseSeriesTransformer):
10+
"""
11+
Calculates the n-th order difference of a time series.
12+
13+
Transforms a time series X into a series Y representing the difference
14+
calculated `order` times.
15+
16+
The time series are supposed to be all in rows,
17+
with shape (n_channels, n_timepoints)
18+
19+
- Order 1: Y[t] = X[t] - X[t-1]
20+
- Order 2: Y[t] = (X[t] - X[t-1]) - (X[t-1] - X[t-2]) = X[t] - 2*X[t-1] + X[t-2]
21+
- ... and so on.
22+
23+
The transformed series will be shorter than the input series by `order`
24+
elements along the time axis.
25+
26+
Parameters
27+
----------
28+
order : int, default=1
29+
The order of differencing. Must be a positive integer.
30+
31+
Notes
32+
-----
33+
This transformer assumes the input series does not contain NaN values where
34+
the difference needs to be computed.
35+
36+
Examples
37+
--------
38+
>>> import numpy as np
39+
>>> from aeon.transformations.series._diff import DifferenceTransformer
40+
>>> X1 = np.array([[1, 3, 2, 5, 4, 7, 6, 9, 8, 10]]) # Shape (1, 10)
41+
>>> dt = DifferenceTransformer()
42+
>>> Xt1 = dt.fit_transform(X1)
43+
>>> print(Xt1) # Shape (1, 9)
44+
[[ 2 -1 3 -1 3 -1 3 -1 2]]
45+
46+
>>> X2 = np.array([[1, 3, 2, 5, 4, 7, 6, 9, 8, 10]]) # Shape (1, 10)
47+
>>> dt2 = DifferenceTransformer(order=2)
48+
>>> Xt2 = dt2.fit_transform(X2)
49+
>>> print(Xt2) # Shape (1, 8)
50+
[[-3 4 -4 4 -4 4 -4 3]]
51+
52+
>>> X3 = np.array([[1, 2, 3, 4, 5], [5, 4, 3, 2, 1]]) # Shape (2, 5)
53+
>>> dt = DifferenceTransformer()
54+
>>> Xt3 = dt.fit_transform(X3)
55+
>>> print(Xt3) # Shape (2, 4)
56+
[[ 1 1 1 1]
57+
[-1 -1 -1 -1]]
58+
"""
59+
60+
_tags = {
61+
"capability:multivariate": True,
62+
"X_inner_type": "np.ndarray",
63+
"fit_is_empty": True,
64+
}
65+
66+
def __init__(self, order=1):
67+
self.order = order
68+
super().__init__(axis=1)
69+
70+
def _transform(self, X, y=None):
71+
"""
72+
Perform the n-th order differencing transformation.
73+
74+
Parameters
75+
----------
76+
X : Time series to transform. With shape (n_channels, n_timepoints).
77+
y : ignored argument for interface compatibility
78+
79+
Returns
80+
-------
81+
Xt : np.ndarray
82+
"""
83+
if not isinstance(self.order, int) or self.order < 1:
84+
raise ValueError(
85+
f"`order` must be a positive integer, but got {self.order}"
86+
)
87+
88+
diff_X = np.diff(X, n=self.order, axis=1)
89+
90+
Xt = diff_X
91+
92+
return Xt
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Tests for Difference transformation."""
2+
3+
import numpy as np
4+
5+
from aeon.transformations.series._diff import DifferenceTransformer
6+
7+
8+
def test_diff():
9+
"""Tests basic first and second order differencing."""
10+
X = np.array([[1.0, 4.0, 9.0, 16.0, 25.0, 36.0]])
11+
12+
dt1 = DifferenceTransformer(order=1)
13+
Xt1 = dt1.fit_transform(X)
14+
expected1 = np.array([[3.0, 5.0, 7.0, 9.0, 11.0]])
15+
16+
np.testing.assert_allclose(
17+
Xt1, expected1, equal_nan=True, err_msg="Value mismatch for order 1"
18+
)
19+
20+
dt2 = DifferenceTransformer(order=2)
21+
Xt2 = dt2.fit_transform(X)
22+
expected2 = np.array([[2.0, 2.0, 2.0, 2.0]])
23+
24+
np.testing.assert_allclose(
25+
Xt2, expected2, equal_nan=True, err_msg="Value mismatch for order 2"
26+
)
27+
28+
Y = np.array([[1, 2, 3, 4], [5, 3, 1, 8]])
29+
30+
Yt1 = dt1.fit_transform(Y)
31+
expected3 = np.array([[1, 1, 1], [-2, -2, 7]])
32+
np.testing.assert_allclose(
33+
Yt1,
34+
expected3,
35+
equal_nan=True,
36+
err_msg="Value mismatch for order 1,multivariate",
37+
)

0 commit comments

Comments
 (0)