-
Notifications
You must be signed in to change notification settings - Fork 3
Added univariate streaming algorithms #9
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
121a4be
online without bonf
gloriadesideri 656c983
added online monitoring algorithms tests, fixed errors
gloriadesideri 9b4c82d
check for no columns in datasets, sdded tests
gloriadesideri daae074
fixed pr comments: added river as optional dependency
gloriadesideri 6e92d34
minor fixes
gloriadesideri e3e6628
added comment to clarify comparison size = 1 in online algorithm, fix…
gloriadesideri 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
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,77 @@ | ||
| from typing import Callable | ||
|
|
||
| import numpy as np | ||
| from ml3_drift.enums.monitoring import DataDimension, DataType, MonitoringType | ||
| from ml3_drift.models.monitoring import ( | ||
| DriftInfo, | ||
| MonitoringAlgorithmSpecs, | ||
| MonitoringOutput, | ||
| ) | ||
| from ml3_drift.monitoring.base.base_univariate import UnivariateMonitoringAlgorithm | ||
| from ml3_drift.monitoring.base.online_monitorning_algorithm import ( | ||
| OnlineMonitorningAlgorithm, | ||
| ) | ||
|
|
||
| RIVER = True | ||
| try: | ||
| from river.drift.adwin import ADWIN as RiverADWIN | ||
| except ModuleNotFoundError: | ||
| RIVER = False | ||
|
|
||
|
|
||
| class ADWIN(OnlineMonitorningAlgorithm, UnivariateMonitoringAlgorithm): | ||
| @classmethod | ||
| def specs(cls) -> MonitoringAlgorithmSpecs: | ||
| return MonitoringAlgorithmSpecs( | ||
| data_dimension=DataDimension.MULTIVARIATE, | ||
| data_type=DataType.MIX, | ||
| monitoring_type=MonitoringType.ONLINE, | ||
| ) | ||
|
|
||
| def __init__( | ||
| self, | ||
| callbacks: list[Callable[[DriftInfo | None], None]] | None = None, | ||
| p_value: float = 0.002, | ||
| clock: float = 32, | ||
| max_buckets: int = 5, | ||
| min_window_length: int = 5, | ||
| grace_period: int = 10, | ||
| *args, | ||
| **kwargs, | ||
| ) -> None: | ||
| if not RIVER: | ||
| raise ModuleNotFoundError( | ||
| "River library is required for ADWIN algorithm. Please install it using pip install/ uv add ml3-drift[river]" | ||
| ) | ||
| self.p_value = p_value | ||
| self.clock = clock | ||
| self.max_buckets = max_buckets | ||
| self.min_window_length = min_window_length | ||
| self.grace_period = grace_period | ||
| self._args = args | ||
| self._kwargs = kwargs | ||
| super().__init__( | ||
| comparison_size=1, callbacks=callbacks | ||
| ) # since we add only one sample per step and river handles building the window internally we set comparison_size to 1 | ||
|
|
||
| def _reset_internal_parameters(self): | ||
| self.drift_agent = RiverADWIN( | ||
| delta=self.p_value, | ||
| clock=self.clock, | ||
| max_buckets=self.max_buckets, | ||
| min_window_length=self.min_window_length, | ||
| grace_period=self.grace_period, | ||
| *self._args, | ||
| **self._kwargs, | ||
| ) | ||
|
|
||
| def _fit(self, X: np.ndarray): | ||
| """Fit the KSWIN algorithm to the data.""" | ||
| self._validate(X) | ||
| self.reset_internal_parameters() | ||
| self.is_fitted = True | ||
|
|
||
| def _detect(self): | ||
| self.drift_agent.update(self.comparison_data) | ||
| drift_detected = self.drift_agent.drift_detected | ||
| return MonitoringOutput(drift_detected=drift_detected, drift_info=None) |
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,74 @@ | ||
| from typing import Callable | ||
|
|
||
| import numpy as np | ||
| from ml3_drift.enums.monitoring import DataDimension, DataType, MonitoringType | ||
| from ml3_drift.models.monitoring import ( | ||
| DriftInfo, | ||
| MonitoringAlgorithmSpecs, | ||
| MonitoringOutput, | ||
| ) | ||
| from ml3_drift.monitoring.base.base_univariate import UnivariateMonitoringAlgorithm | ||
| from ml3_drift.monitoring.base.online_monitorning_algorithm import ( | ||
| OnlineMonitorningAlgorithm, | ||
| ) | ||
|
|
||
| RIVER = True | ||
| try: | ||
| from river.drift.kswin import KSWIN as RiverKSWIN | ||
| except ModuleNotFoundError: | ||
| RIVER = False | ||
|
|
||
|
|
||
| class KSWIN(OnlineMonitorningAlgorithm, UnivariateMonitoringAlgorithm): | ||
| @classmethod | ||
| def specs(cls) -> MonitoringAlgorithmSpecs: | ||
| return MonitoringAlgorithmSpecs( | ||
| data_dimension=DataDimension.MULTIVARIATE, | ||
| data_type=DataType.MIX, | ||
| monitoring_type=MonitoringType.ONLINE, | ||
| ) | ||
|
|
||
| def __init__( | ||
| self, | ||
| callbacks: list[Callable[[DriftInfo | None], None]] | None = None, | ||
| p_value: float = 0.00, | ||
| window_size: int = 100, | ||
| stat_size: int = 30, | ||
| seed: int | None = None, | ||
| *args, | ||
| **kwargs, | ||
| ) -> None: | ||
| if not RIVER: | ||
| raise ModuleNotFoundError( | ||
| "River library is required for KSWIN algorithm. Please install it using pip install/ uv add ml3-drift[river]" | ||
| ) | ||
| self.p_value = p_value | ||
| self.window_size = window_size | ||
| self.stat_size = stat_size | ||
| self.seed = seed | ||
| self._args = args | ||
| self._kwargs = kwargs | ||
| super().__init__( | ||
| comparison_size=1, callbacks=callbacks | ||
| ) # since we add only one sample per step and river handles building the window internally we set comparison_size to 1 | ||
|
|
||
| def _reset_internal_parameters(self): | ||
| self.drift_agent = RiverKSWIN( | ||
| alpha=self.p_value, | ||
| window_size=self.window_size, | ||
| stat_size=self.stat_size, | ||
| seed=self.seed, | ||
| *self._args, | ||
| **self._kwargs, | ||
| ) | ||
|
|
||
| def _fit(self, X: np.ndarray): | ||
| """Fit the KSWIN algorithm to the data.""" | ||
| self._validate(X) | ||
| self.reset_internal_parameters() | ||
| self.is_fitted = True | ||
|
|
||
| def _detect(self): | ||
| self.drift_agent.update(self.comparison_data) | ||
| drift_detected = self.drift_agent.drift_detected | ||
| return MonitoringOutput(drift_detected=drift_detected, drift_info=None) |
Oops, something went wrong.
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.