Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
22 changes: 12 additions & 10 deletions aeon/anomaly_detection/series/distance_based/_rockad.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
from sklearn.neighbors import NearestNeighbors
from sklearn.preprocessing import PowerTransformer
from sklearn.utils import resample
from sklearn.utils import check_random_state, resample

from aeon.anomaly_detection.series.base import BaseSeriesAnomalyDetector
from aeon.transformations.collection.convolution_based import Rocket
Expand Down Expand Up @@ -72,7 +72,7 @@ class ROCKAD(BaseSeriesAnomalyDetector):
Data Analysis XXI. IDA 2023. Lecture Notes in Computer Science,
vol 13876. Springer, Cham. https://doi.org/10.1007/978-3-031-30047-9_33

Examples
Examples
--------
>>> import numpy as np
>>> from aeon.anomaly_detection.series.distance_based import ROCKAD
Expand All @@ -83,11 +83,11 @@ class ROCKAD(BaseSeriesAnomalyDetector):
>>> detector = ROCKAD(window_size=15,n_estimators=10,n_kernels=10,n_neighbors=3)
>>> detector.fit(X_train)
ROCKAD(...)
>>> detector.predict(X_test)
array([0. , 0.00554713, 0.06990941, 0.22881059, 0.32382585,
0.43652154, 0.43652154, 0.43652154, 0.43652154, 0.43652154,
0.43652154, 0.43652154, 0.43652154, 0.43652154, 0.43652154,
0.52382585, 0.65200875, 0.80313368, 0.85194345, 1. ])
>>> np.round(detector.predict(X_test), 6)
array([0. , 0.290721, 0.498102, 0.623339, 0.714325, 0.748893, 0.748893,
0.748893, 0.748893, 0.748893, 0.748893, 0.748893, 0.748893,
0.748893, 0.748893, 0.898671, 0.977978, 0.999683, 1. ,
0.92173 ])
"""

_tags = {
Expand Down Expand Up @@ -164,11 +164,13 @@ def _check_params(self, X: np.ndarray) -> None:
def _inner_fit(self, X: np.ndarray) -> None:
self._n_jobs = check_n_jobs(self.n_jobs)

rng = check_random_state(self.random_state)

self.rocket_transformer_ = Rocket(
n_kernels=self.n_kernels,
normalise=self.normalise,
n_jobs=self._n_jobs,
random_state=self.random_state,
random_state=rng.randint(np.iinfo(np.int32).max),
)
# X: (n_windows, window_size)
Xt = self.rocket_transformer_.fit_transform(X)
Expand Down Expand Up @@ -196,7 +198,7 @@ def _inner_fit(self, X: np.ndarray) -> None:

self.list_baggers_ = []

for idx_estimator in range(self.n_estimators):
for _ in range(self.n_estimators):
# Initialize estimator
estimator = NearestNeighbors(
n_neighbors=self.n_neighbors,
Expand All @@ -209,7 +211,7 @@ def _inner_fit(self, X: np.ndarray) -> None:
Xtp,
replace=True,
n_samples=None,
random_state=self.random_state + idx_estimator,
random_state=rng.randint(np.iinfo(np.int32).max),
stratify=None,
)

Expand Down
6 changes: 4 additions & 2 deletions aeon/transformations/collection/convolution_based/_hydra.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
import pandas as pd
from sklearn.utils import check_random_state

from aeon.transformations.collection import BaseCollectionTransformer
from aeon.utils.validation import check_n_jobs
Expand Down Expand Up @@ -105,8 +106,9 @@ def _fit(self, X, y=None):

import torch

if isinstance(self.random_state, int):
torch.manual_seed(self.random_state)
rng = check_random_state(self.random_state)
seed = rng.randint(np.iinfo(np.int32).max)
torch.manual_seed(seed)

torch.set_num_threads(self._n_jobs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import numpy as np
from numba import get_num_threads, njit, prange, set_num_threads, vectorize
from sklearn.utils import check_random_state

from aeon.transformations.collection import BaseCollectionTransformer
from aeon.utils.validation import check_n_jobs
Expand Down Expand Up @@ -108,9 +109,9 @@ def _fit(self, X, y=None):
"""
self._n_jobs = check_n_jobs(self.n_jobs)

random_state = (
np.int32(self.random_state) if isinstance(self.random_state, int) else None
)
rng = check_random_state(self.random_state)
self._random_state = rng.randint(np.iinfo(np.int32).max)

_, n_channels, n_timepoints = X.shape
if n_timepoints < 9:
raise ValueError(
Expand All @@ -123,7 +124,7 @@ def _fit(self, X, y=None):
else:
self.n_kernels_ = self.n_kernels
self.parameters = _static_fit(
X, self.n_kernels_, self.max_dilations_per_kernel, random_state
X, self.n_kernels_, self.max_dilations_per_kernel, self._random_state
)
return self

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
from numba import get_num_threads, njit, prange, set_num_threads
from sklearn.utils import check_random_state

from aeon.transformations.collection import BaseCollectionTransformer
from aeon.utils.validation import check_n_jobs
Expand Down Expand Up @@ -117,11 +118,8 @@ def _fit(self, X, y=None):
"""
self._n_jobs = check_n_jobs(self.n_jobs)

self.random_state_ = (
np.int32(self.random_state) if isinstance(self.random_state, int) else None
)
if self.random_state_ is not None:
np.random.seed(self.random_state_)
rng = check_random_state(self.random_state)
self.random_state_ = rng.randint(np.iinfo(np.int32).max)

_, n_channels, n_timepoints = X.shape
if n_timepoints < 9:
Expand All @@ -140,9 +138,9 @@ def _fit(self, X, y=None):
_X1 = np.diff(X, 1)
self.parameter1 = self._fit_univariate(_X1)
else:
self.parameter = self._fit_multivariate(X)
self.parameter = self._fit_multivariate(X, rng)
_X1 = np.diff(X, 1)
self.parameter1 = self._fit_multivariate(_X1)
self.parameter1 = self._fit_multivariate(_X1, rng)

return self

Expand Down Expand Up @@ -226,7 +224,7 @@ def _fit_univariate(self, X):

return dilations, n_features_per_dilation, biases

def _fit_multivariate(self, X):
def _fit_multivariate(self, X, rng):
_, n_channels, input_length = X.shape

n_kernels = 84
Expand All @@ -246,7 +244,7 @@ def _fit_multivariate(self, X):
max_exponent = np.log2(max_n_channels + 1)

n_channels_per_combination = (
2 ** np.random.uniform(0, max_exponent, n_combinations)
2 ** rng.uniform(0, max_exponent, n_combinations)
).astype(np.int32)

channel_indices = np.zeros(n_channels_per_combination.sum(), dtype=np.int32)
Expand All @@ -255,7 +253,7 @@ def _fit_multivariate(self, X):
for combination_index in range(n_combinations):
n_channels_this_combination = n_channels_per_combination[combination_index]
n_channels_end = n_channels_start + n_channels_this_combination
channel_indices[n_channels_start:n_channels_end] = np.random.choice(
channel_indices[n_channels_start:n_channels_end] = rng.choice(
n_channels, n_channels_this_combination, replace=False
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
from numba import get_num_threads, njit, prange, set_num_threads
from sklearn.utils import check_random_state

from aeon.transformations.collection import BaseCollectionTransformer, Normalizer
from aeon.utils.validation import check_n_jobs
Expand Down Expand Up @@ -101,10 +102,9 @@ def _fit(self, X, y=None):
"""
self._n_jobs = check_n_jobs(self.n_jobs)

if isinstance(self.random_state, int):
self._random_state = self.random_state
else:
self._random_state = None
rng = check_random_state(self.random_state)
self._random_state = rng.randint(np.iinfo(np.int32).max)

n_channels = X[0].shape[0]

# The only use of n_timepoints is to set the maximum dilation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__all__ = ["ROCKETGPU"]

import numpy as np
from sklearn.utils import check_random_state

from aeon.transformations.collection.convolution_based.rocketGPU.base import (
BaseROCKETGPU,
Expand Down Expand Up @@ -73,7 +74,7 @@ def __init__(

def _define_parameters(self):
"""Define the parameters of ROCKET."""
rng = np.random.default_rng(self.random_state)
rng = np.random.default_rng(self._seed)

self._list_of_kernels = []
self._list_of_dilations = []
Expand All @@ -87,6 +88,8 @@ def _define_parameters(self):
axis=0, keepdims=True
)

_convolution_kernel = _convolution_kernel.astype(np.float32)

if self.use_dilation:
_dilation_rate = 2 ** rng.uniform(
0, np.log2((self.input_length - 1) / (_kernel_size - 1))
Expand All @@ -98,6 +101,7 @@ def _define_parameters(self):
assert _padding in ["SAME", "VALID"]

_bias = rng.uniform(self._bias_range[0], self._bias_range[1])
_bias = np.float32(_bias)

self._list_of_kernels.append(_convolution_kernel)
self._list_of_dilations.append(_dilation_rate)
Expand Down Expand Up @@ -129,6 +133,9 @@ def _fit(self, X, y=None):

assert self._bias_range[0] <= self._bias_range[1]

rng = check_random_state(self.random_state)
self._seed = rng.randint(np.iinfo(np.int32).max)

self._define_parameters()

def _generate_batch_indices(self, n):
Expand Down Expand Up @@ -180,7 +187,8 @@ def _transform(self, X, y=None):
"""
import tensorflow as tf

tf.random.set_seed(self.random_state)
X = X.astype(np.float32)
tf.random.set_seed(self._seed)

X = X.transpose(0, 2, 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@
)

expected_uni = {
"Rocket": [1.0, 1.3438051, 0.53333336],
"MiniRocket": [0.4, 1.0, 1.0],
"MultiRocket": [0.4, 1.0, 1.0],
"Rocket": [0.13333334, 1.622878, 0.8666667],
"MiniRocket": [0.4, 1.0, 0.93333334],
"MultiRocket": [0.4, 1.0, 0.93333334],
}
expected_features = {
"Rocket": 200,
"MiniRocket": 84,
"MultiRocket": 672,
}
expected_multi = {
"Rocket": [0.6363636, 0.74931127, 0.0],
"MiniRocket": [0.27272728, 0.33333334, 0.8181818],
"MultiRocket": [0.2727273, 0.3333333, 0.8181818],
"Rocket": [0.8181818, 5.735185, 0.6],
"MiniRocket": [0.36363637, 0.0, 0.90909094],
"MultiRocket": [0.36363637, 1.0, 0.90909094],
}


Expand All @@ -69,6 +69,7 @@ def test_rocket_on_univariate(transform):
np.testing.assert_almost_equal(
np.array(expected_uni[transform]),
np.array([X_trans[0][0], X_trans[1][5], X_trans[3][80]]),
decimal=6,
)
# Test fit_transform the same
X_trans2 = rocket.fit_transform(X)
Expand Down Expand Up @@ -121,21 +122,23 @@ def test_normalise_rocket():
expected_unit_test = {}
expected_unit_test["MiniRocket"] = np.array(
[
[0.5833333, 0.7083333, 0.16666667, 0.5833333, 0.8333333],
[0.5416667, 0.7083333, 0.16666667, 0.5416667, 0.7916667],
[0.5833333, 0.7083333, 0.20833333, 0.5833333, 0.7916667],
[0.45833334, 0.6666667, 0.16666667, 0.5, 0.75],
[0.5833333, 0.7083333, 0.1666667, 0.5833333, 0.8333333],
[0.5416667, 0.7083333, 0.1666667, 0.5416667, 0.7916667],
[0.5833333, 0.7083333, 0.2083333, 0.5833333, 0.7916667],
[0.4583333, 0.6666667, 0.1666667, 0.5, 0.75],
[0.5416667, 0.7916667, 0.125, 0.5416667, 0.8333333],
]
],
dtype=np.float32,
)
expected_unit_test["Rocket"] = np.array(
[
[0.54167, 3.04102, 0.41667, 2.21878, 0.66667],
[0.54167, 2.63263, 0.37500, 2.53897, 0.66667],
[0.58333, 2.79819, 0.41667, 2.53153, 0.58333],
[0.54167, 2.47356, 0.37500, 1.90805, 0.70833],
[0.54167, 2.54753, 0.41667, 2.16991, 0.66667],
]
[0.5, 2.507311, 0.416667, 7.098707, 0.875],
[0.5, 2.00701, 0.416667, 6.768808, 0.958333],
[0.5, 2.53585, 0.416667, 6.673825, 0.916667],
[0.3333333, 1.328932, 0.4583333, 6.315244, 0.8333333],
[0.5, 1.519787, 0.375, 6.620714, 0.875],
],
dtype=np.float32,
)
expected_unit_test["MultiRocket"] = np.array(
[
Expand All @@ -144,18 +147,20 @@ def test_normalise_rocket():
[0.58333, 0.70833, 0.20833, 0.58333, 0.79167],
[0.45833, 0.66667, 0.16667, 0.50000, 0.75000],
[0.54167, 0.79167, 0.12500, 0.54167, 0.83333],
]
],
dtype=np.float32,
)

expected_basic_motions = {}
expected_basic_motions["MiniRocket"] = np.array(
[
[0.38, 0.76, 0.15, 0.53, 0.91],
[0.43, 0.7, 0.09, 0.53, 0.95],
[0.46, 0.63, 0.25, 0.54, 0.87],
[0.3, 0.79, 0.04, 0.47, 1],
[0.45, 0.78, 0.02, 0.55, 1],
]
[0.02, 1, 0, 0.59, 1],
[0.04, 0.98, 0, 0.55, 0.99],
[0, 1, 0, 0.47, 1],
[0, 1, 0, 0.58, 1],
[0, 1, 0, 0.57, 1],
],
dtype=np.float32,
)
rockets = [
Rocket(n_kernels=100),
Expand Down
Loading