Skip to content

Commit 8f47181

Browse files
author
Jaime Céspedes Sisniega
authored
Merge pull request #258 from IFCA/feature-doc-api-remaining-examples
Add remaining examples in documentation API
2 parents 91135e3 + 5114057 commit 8f47181

File tree

13 files changed

+191
-465
lines changed

13 files changed

+191
-465
lines changed

docs/source/api_reference/callbacks/streaming.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Streaming
22

3+
The {mod}`frouros.callbacks.streaming` module contains streaming callbacks.
4+
5+
36
```{eval-rst}
47
.. automodule:: frouros.callbacks.streaming
58
:no-members:
@@ -12,6 +15,4 @@
1215
:template: class.md
1316
1417
HistoryConceptDrift
15-
mSPRT
16-
WarningSamplesBuffer
1718
```

frouros/callbacks/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
"""Callbacks init."""
22

33
from .batch import PermutationTestDistanceBased, ResetStatisticalTest
4-
from .streaming import HistoryConceptDrift, mSPRT, WarningSamplesBuffer
4+
from .streaming import HistoryConceptDrift
55

66
__all__ = [
77
"HistoryConceptDrift",
8-
"mSPRT",
98
"PermutationTestDistanceBased",
109
"ResetStatisticalTest",
11-
"WarningSamplesBuffer",
1210
]

frouros/callbacks/batch/permutation_test.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,49 @@
1010

1111

1212
class PermutationTestDistanceBased(BaseCallbackBatch):
13-
"""Permutation test on distance based batch callback class."""
14-
15-
def __init__(
13+
"""Permutation test callback class that can be applied to :mod:`data_drift.batch.distance_based <frouros.detectors.data_drift.batch.distance_based>` detectors.
14+
15+
:param num_permutations: number of permutations to obtain the p-value
16+
:type num_permutations: int
17+
:param num_jobs: number of jobs, defaults to -1
18+
:type num_jobs: int
19+
:param verbose: verbose flag, defaults to False
20+
:type verbose: bool
21+
:param name: name value, defaults to None. If None, the name will be set to `PermutationTestDistanceBased`.
22+
:type name: Optional[str]
23+
24+
:Note:
25+
Callbacks logs are updated with the following variables:
26+
27+
- `observed_statistic`: observed statistic obtained from the distance-based detector. Same distance value returned by the `compare` method
28+
- `permutation_statistic`: list of statistics obtained from the permutations
29+
- `p_value`: p-value obtained from the permutation test
30+
31+
:Example:
32+
33+
>>> from frouros.callbacks import PermutationTestDistanceBased
34+
>>> from frouros.detectors.data_drift import MMD
35+
>>> import numpy as np
36+
>>> np.random.seed(seed=31)
37+
>>> X = np.random.multivariate_normal(mean=[1, 1], cov=[[2, 0], [0, 2]], size=100)
38+
>>> Y = np.random.multivariate_normal(mean=[0, 0], cov=[[2, 1], [1, 2]], size=100)
39+
>>> detector = MMD(callbacks=PermutationTestDistanceBased(num_permutations=1000, random_state=31))
40+
>>> _ = detector.fit(X=X)
41+
>>> distance, callbacks_log = detector.compare(X=Y)
42+
>>> distance
43+
DistanceResult(distance=0.05643613752975596)
44+
>>> callbacks_log["PermutationTestDistanceBased"]["p_value"]
45+
0.0
46+
""" # noqa: E501 # pylint: disable=line-too-long
47+
48+
def __init__( # noqa: D107
1649
self,
1750
num_permutations: int,
1851
num_jobs: int = -1,
19-
name: Optional[str] = None,
2052
verbose: bool = False,
53+
name: Optional[str] = None,
2154
**kwargs,
2255
) -> None:
23-
"""Init method.
24-
25-
:param num_permutations: number of permutations
26-
:type num_permutations: int
27-
:param num_jobs: number of jobs
28-
:type num_jobs: int
29-
:param name: name to be use
30-
:type name: Optional[str]
31-
"""
3256
super().__init__(name=name)
3357
self.num_permutations = num_permutations
3458
self.num_jobs = num_jobs

frouros/callbacks/batch/reset.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,33 @@
77

88

99
class ResetStatisticalTest(BaseCallbackBatch):
10-
"""Reset on statistical test batch callback class."""
10+
"""Reset callback class that can be applied to :mod:`data_drift.batch.statistical_test <frouros.detectors.data_drift.batch.statistical_test>` detectors.
1111
12-
def __init__(self, alpha: float, name: Optional[str] = None) -> None:
13-
"""Init method.
12+
:param alpha: significance value
13+
:type alpha: float
14+
:param name: name value, defaults to None. If None, the name will be set to `ResetStatisticalTest`.
15+
:type name: Optional[str]
1416
15-
:param alpha: significance value
16-
:type alpha: float
17-
:param name: name to be use
18-
:type name: Optional[str]
19-
"""
17+
:Example:
18+
19+
>>> from frouros.callbacks import ResetStatisticalTest
20+
>>> from frouros.detectors.data_drift import KSTest
21+
>>> import numpy as np
22+
>>> np.random.seed(seed=31)
23+
>>> X = np.random.normal(loc=0, scale=1, size=100)
24+
>>> Y = np.random.normal(loc=1, scale=1, size=100)
25+
>>> detector = KSTest(callbacks=ResetStatisticalTest(alpha=0.01))
26+
>>> _ = detector.fit(X=X)
27+
>>> detector.compare(X=Y)[0]
28+
INFO:frouros:Drift detected. Resetting detector...
29+
StatisticalResult(statistic=0.55, p_value=3.0406585087050305e-14)
30+
""" # noqa: E501 # pylint: disable=line-too-long
31+
32+
def __init__( # noqa: D107
33+
self,
34+
alpha: float,
35+
name: Optional[str] = None,
36+
) -> None:
2037
super().__init__(name=name)
2138
self.alpha = alpha
2239

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
"""Streaming callbacks init."""
22

33
from .history import HistoryConceptDrift
4-
from .msprt import mSPRT
5-
from .warning_samples import WarningSamplesBuffer
64

75
__all__ = [
86
"HistoryConceptDrift",
9-
"mSPRT",
10-
"WarningSamplesBuffer",
117
]

frouros/callbacks/streaming/history.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,44 @@
77

88

99
class HistoryConceptDrift(BaseCallbackStreaming):
10-
"""HistoryConceptDrift callback class."""
10+
"""HistoryConceptDrift callback class that can be applied to :mod:`concept_drift.streaming <frouros.detectors.concept_drift.streaming>` detectors.
1111
12-
def __init__(self, name: Optional[str] = None) -> None:
13-
"""Init method.
12+
:param name: name value, defaults to None. If None, the name will be set to `HistoryConceptDrift`.
13+
:type name: Optional[str]
1414
15-
:param name: name value
16-
:type name: Optional[str]
17-
"""
15+
:Note:
16+
By default the following variables are stored:
17+
18+
- `value`: list of values received by the detector
19+
- `drift`: list of drift flags
20+
- `num_instances`: list of number of instances received by the detector
21+
Each detector may store additional variables if they are defined in an `additional_vars` dictionary in the detectors `__init__` method.
22+
The user can add additional variables by calling the :func:`add_additional_vars` method.
23+
24+
:Example:
25+
26+
>>> from frouros.callbacks import HistoryConceptDrift
27+
>>> from frouros.detectors.concept_drift import ADWIN
28+
>>> import numpy as np
29+
>>> np.random.seed(seed=31)
30+
>>> dist_a = np.random.normal(loc=0.2, scale=0.01, size=1000)
31+
>>> dist_b = np.random.normal(loc=0.8, scale=0.04, size=1000)
32+
>>> stream = np.concatenate((dist_a, dist_b))
33+
>>> detector = ADWIN(callbacks=[HistoryConceptDrift(name="history")])
34+
>>> for i, value in enumerate(stream):
35+
... callbacks_log = detector.update(value=value)
36+
... if detector.drift:
37+
... print(f"Change detected at step {i}")
38+
... break
39+
Change detected at step 1055
40+
>>> callbacks_log["history"]["drift"]
41+
[False, False, ..., True]
42+
""" # noqa: E501 # pylint: disable=line-too-long
43+
44+
def __init__( # noqa: D107
45+
self,
46+
name: Optional[str] = None,
47+
) -> None:
1848
super().__init__(name=name)
1949
self.additional_vars: List[str] = []
2050
self.history: Dict[str, List[Any]] = {
@@ -24,7 +54,7 @@ def __init__(self, name: Optional[str] = None) -> None:
2454
}
2555

2656
def add_additional_vars(self, vars_: List[str]) -> None:
27-
"""Add addtional variables to track.
57+
"""Add additional variables to track.
2858
2959
:param vars_: list of variables
3060
:type vars_: List[str]

0 commit comments

Comments
 (0)