### Install Pre-commit Hooks Source: https://dtaianomaly.readthedocs.io/en/latest/additional_information/contributing.html Install the pre-commit framework to automatically format code and run checks before commits. ```bash pre-commit install ``` -------------------------------- ### Start InTimeAD Demonstrator Programmatically Source: https://dtaianomaly.readthedocs.io/en/latest/api/in_time_ad.html Launch the InTimeAD demonstrator using Python. Import the in_time_ad module and call its run() method to start the application. This method allows for programmatic control and integration. ```python from dtaianomaly import in_time_ad in_time_ad.run() ``` -------------------------------- ### Install dtaianomaly from GitHub Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/getting_started/installation.rst.txt Install the latest development version of dtaianomaly directly from its GitHub repository. ```bash pip install git+https://github.com/ML-KULeuven/dtaianomaly ``` -------------------------------- ### Install pre-commit hooks Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/additional_information/contributing.rst.txt Install the pre-commit framework to automate checks before committing code. This helps maintain code quality and consistency. ```bash pre-commit install ``` -------------------------------- ### Install dtaianomaly from Local Source Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/getting_started/installation.rst.txt Install dtaianomaly after downloading and extracting the source code. Navigate to the repository's root directory in your terminal before running this command. ```bash pip install . ``` -------------------------------- ### Import and Load Demonstration Data Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.HybridKNearestNeighbors.html This snippet shows how to import the HybridKNearestNeighbors class and load a demonstration time series dataset for use in examples. ```python from dtaianomaly.anomaly_detection import HybridKNearestNeighbors from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() ``` -------------------------------- ### Install dtaianomaly with All Optional Dependencies Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/getting_started/installation.rst.txt Install dtaianomaly along with all its optional dependencies for full functionality. This includes dependencies for testing, progress bars, documentation generation, notebooks, and linting. ```bash pip install dtaianomaly[all] ``` -------------------------------- ### Run InTimeAD Demonstrator Programmatically Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/api/in_time_ad.rst.txt Start the InTimeAD demonstrator by importing and running the 'run' method from the 'dtaianomaly.in_time_ad' module in Python. ```python from dtaianomaly import in_time_ad in_time_ad.run() ``` -------------------------------- ### KNearestNeighbors Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.KNearestNeighbors.html Demonstrates how to initialize, fit, and compute anomaly scores using the KNearestNeighbors detector. Requires importing the class and demonstration data. ```python from dtaianomaly.anomaly_detection import KNearestNeighbors from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() knn = KNearestNeighbors(10).fit(x) knn.decision_function(x) ``` -------------------------------- ### KShapeAnomalyDetector Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.KShapeAnomalyDetector.html Demonstrates how to initialize, fit, and compute decision functions using KShapeAnomalyDetector with demonstration time series data. ```python from dtaianomaly.anomaly_detection import KShapeAnomalyDetector from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() kshape = KShapeAnomalyDetector(window_size=50).fit(x) kshape.decision_function(x) ``` -------------------------------- ### Install TimeMoE Dependency Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.TimeMoE.html To run Time-MoE, you need to install the optional dependency `time_moe`. This command installs dtaianomaly with the necessary extra. ```bash pip install dtaianomaly[time_moe] ``` -------------------------------- ### Install dtaianomaly with InTimeAD dependencies Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/api/in_time_ad.rst.txt Install the dtaianomaly library along with the optional 'demonstrator' and 'in_time_ad' dependencies using pip. ```bash pip install dtaianomaly[in_time_ad] ``` -------------------------------- ### Install dtaianomaly with InTimeAD Source: https://dtaianomaly.readthedocs.io/en/latest/api/in_time_ad.html Install the dtaianomaly library with the necessary dependencies for the InTimeAD module. This command ensures all required packages are available for running the demonstrator. ```bash pip install dtaianomaly[in_time_ad] ``` -------------------------------- ### Chronos Anomaly Detection Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.Chronos.html Demonstrates how to initialize, fit, and get anomaly decision scores using the Chronos model. Ensure the 'chronos' dependency is installed. ```python from dtaianomaly.anomaly_detection import Chronos from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() chronos = Chronos(10).fit(x) chronos.decision_function(x) array([0.00027719, 0.00027719, 0.00027719, ..., 0.00058781, 0.02628242, 0.00010728]...) ``` -------------------------------- ### Install Specific dtaianomaly Version from GitHub Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/getting_started/installation.rst.txt Install a specific version of dtaianomaly from GitHub by specifying the version tag after '@'. ```bash pip install git+https://github.com/ML-KULeuven/dtaianomaly@X.Y.Z ``` -------------------------------- ### MovingWindowVariance Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.MovingWindowVariance.html Demonstrates how to initialize, fit, and use the MovingWindowVariance detector with demonstration time series data to compute anomaly scores. ```python from dtaianomaly.anomaly_detection import MovingWindowVariance from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() baseline = MovingWindowVariance(16).fit(x) baseline.decision_function(x) ``` -------------------------------- ### Multivariate Detector Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.MultivariateDetector.html Demonstrates fitting a MultivariateDetector with an IsolationForest detector and calculating anomaly scores. This example shows how to initialize the detector with a specific aggregation method and then fit it to multivariate data. ```python import numpy as np from dtaianomaly.anomaly_detection import MultivariateDetector, IsolationForest x = np.array([[4, 8], [1, 2], [0, 1], [6, 5], [1, 4], [4, 3], [0, 9], [8, 2], [4, 5], [8, 3]]) detector = MultivariateDetector(IsolationForest(window_size=3, random_state=0), aggregation='mean').fit(x) print(detector.decision_function(x)) ``` -------------------------------- ### KMeansAnomalyDetector Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.KMeansAnomalyDetector.html Demonstrates how to initialize, fit, and compute decision functions using the KMeansAnomalyDetector. Requires importing the detector and demonstration data. ```python from dtaianomaly.anomaly_detection import KMeansAnomalyDetector from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() kmeans_ad = KMeansAnomalyDetector(10).fit(x) kmeans_ad.decision_function(x) ``` -------------------------------- ### Install Latest dtaianomaly from PyPI Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/getting_started/installation.rst.txt Use this command to install the most recent stable version of dtaianomaly from the Python Package Index. ```bash pip install dtaianomaly ``` -------------------------------- ### ConvolutionalNeuralNetwork Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.ConvolutionalNeuralNetwork.html Demonstrates how to initialize, fit, and use the ConvolutionalNeuralNetwork model for anomaly detection. Requires importing the model and demonstration data. ```python >>> from dtaianomaly.anomaly_detection import ConvolutionalNeuralNetwork >>> from dtaianomaly.data import demonstration_time_series >>> x, y = demonstration_time_series() >>> cnn = ConvolutionalNeuralNetwork(10, seed=0).fit(x) >>> cnn.decision_function(x) array([0.07708263, 0.07708263, 0.06242053, ..., 0.1827196 , 0.2396274 , 0.06390759]...) ``` -------------------------------- ### Install Editable Dependencies Source: https://dtaianomaly.readthedocs.io/en/latest/additional_information/contributing.html Install dtaianomaly and all its optional dependencies in editable mode. This ensures your local code changes are reflected immediately. ```bash pip install --editable .[all] ``` -------------------------------- ### Initialize and Fit MOMENT Model Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.MOMENT.html Demonstrates how to initialize the MOMENT anomaly detector with a specified window size and fit it to a time series dataset. This is a common starting point for using the MOMENT model. ```python from dtaianomaly.anomaly_detection import MOMENT from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() moment = MOMENT(10).fit(x) ``` -------------------------------- ### Kernel Principal Component Analysis Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.KernelPrincipalComponentAnalysis.html Demonstrates how to initialize, fit, and compute anomaly scores using Kernel Principal Component Analysis. Requires importing the necessary classes and data. ```python from dtaianomaly.anomaly_detection import KernelPrincipalComponentAnalysis from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() kpca = KernelPrincipalComponentAnalysis(10, n_components=2).fit(x) kpca.decision_function(x) ``` -------------------------------- ### ROCKAD Anomaly Detection Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.ROCKAD.html Demonstrates how to initialize, fit, and compute anomaly scores using the ROCKAD detector. Requires importing ROCKAD and demonstration data. ```python from dtaianomaly.anomaly_detection import ROCKAD from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() rockad = ROCKAD(64, seed=0).fit(x) rockad.decision_function(x) array([5.30759668, 5.25451016, 4.80149563, ..., 3.40483896, 3.72443581, 3.74599171]) ``` -------------------------------- ### MatrixProfileDetector Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.MatrixProfileDetector.html Demonstrates how to initialize and fit the MatrixProfileDetector with a specified window size and then compute anomaly scores for a given time series. ```python from dtaianomaly.anomaly_detection import MatrixProfileDetector from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() matrix_profile = MatrixProfileDetector(window_size=50).fit(x) matrix_profile.decision_function(x) ``` -------------------------------- ### Instantiate and Fit ClusterBasedLocalOutlierFactor Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.ClusterBasedLocalOutlierFactor.html This example demonstrates how to instantiate the ClusterBasedLocalOutlierFactor with a specified window size and then fit it to a time series dataset. It also shows how to compute the anomaly decision function on the fitted data. ```python from dtaianomaly.anomaly_detection import ClusterBasedLocalOutlierFactor from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() cblof = ClusterBasedLocalOutlierFactor(10).fit(x) cblof.decision_function(x) ``` -------------------------------- ### LocalPolynomialApproximation Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.LocalPolynomialApproximation.html Demonstrates how to initialize and use the LocalPolynomialApproximation class for anomaly detection. It fits the model to a demonstration time series and computes anomaly scores. ```python >>> from dtaianomaly.anomaly_detection import LocalPolynomialApproximation >>> from dtaianomaly.data import demonstration_time_series >>> x, y = demonstration_time_series() >>> poly = LocalPolynomialApproximation(neighborhood=50).fit(x) >>> poly.decision_function(x) array([0., 0., 0., ..., 0., 0., 0.]...) ``` -------------------------------- ### Run documentation doctests Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/additional_information/contributing.rst.txt Execute doctests embedded within the documentation to ensure code examples in the docs are accurate and functional. ```bash docs/make doctest ``` -------------------------------- ### AutoEncoder Initialization and Fitting Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.AutoEncoder.html Demonstrates how to initialize an AutoEncoder model with a specified window size and seed, and then fit it to demonstration time series data. This is a common starting point for using the AutoEncoder for anomaly detection. ```python >>> from dtaianomaly.anomaly_detection import AutoEncoder >>> from dtaianomaly.data import demonstration_time_series >>> x, y = demonstration_time_series() >>> auto_encoder = AutoEncoder(10, seed=0).fit(x) >>> auto_encoder.decision_function(x) array([0.59210092, 0.56707534, 0.56629006, ..., 0.58380051, 0.5808109 , 0.54450774]...) ``` -------------------------------- ### HistogramBasedOutlierScore Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.HistogramBasedOutlierScore.html Demonstrates how to initialize, fit, and compute anomaly scores using HistogramBasedOutlierScore. Requires importing the class and demonstration data. ```python from dtaianomaly.anomaly_detection import HistogramBasedOutlierScore from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() hbos = HistogramBasedOutlierScore(1).fit(x) hbos.decision_function(x) ``` -------------------------------- ### SquaredDifference Anomaly Detection Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.SquaredDifference.html Demonstrates how to initialize, fit, and use the SquaredDifference detector to compute anomaly scores on a time series. Requires importing the class and demonstration data. ```python from dtaianomaly.anomaly_detection import SquaredDifference from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() baseline = SquaredDifference().fit(x) baseline.decision_function(x) ``` -------------------------------- ### Run InTimeAD Demonstrator from Command Line Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/api/in_time_ad.rst.txt Execute the InTimeAD demonstrator directly from the command line. ```bash run-demonstrator ``` -------------------------------- ### Run InTimeAD Demonstrator via Command Line Source: https://dtaianomaly.readthedocs.io/en/latest/api/in_time_ad.html Execute the InTimeAD demonstrator directly from the command line. This is a quick way to launch the anomaly detection interface without writing any Python code. ```bash run-demonstrator ``` -------------------------------- ### Build documentation Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/additional_information/contributing.rst.txt Generate the HTML documentation for the project. This command ensures that the documentation builds correctly without errors or warnings. ```bash docs/make html ``` -------------------------------- ### Install Specific dtaianomaly Version from PyPI Source: https://dtaianomaly.readthedocs.io/en/latest/_sources/getting_started/installation.rst.txt Install a specific version of dtaianomaly by replacing 'X.Y.Z' with the desired version number. ```bash pip install dtaianomaly==X.Y.Z ``` -------------------------------- ### Principal Component Analysis Initialization and Fitting Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.PrincipalComponentAnalysis.html Demonstrates how to initialize and fit the PrincipalComponentAnalysis detector with a specified window size. The fitted detector can then be used to compute anomaly scores. ```python from dtaianomaly.anomaly_detection import PrincipalComponentAnalysis from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() pca = PrincipalComponentAnalysis(10).fit(x) pca.decision_function(x) ``` -------------------------------- ### Load Demonstration Time Series Data Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.data.DemonstrationTimeSeriesLoader.html Demonstrates how to instantiate the DemonstrationTimeSeriesLoader, load the dataset, and access the test data (features X and labels y). ```python from dtaianomaly.data import DemonstrationTimeSeriesLoader data_set = DemonstrationTimeSeriesLoader().load() X = data_set.X_test y = data_set.y_test ``` -------------------------------- ### Instantiate and Fit OneClassSupportVectorMachine Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.OneClassSupportVectorMachine.html Demonstrates how to instantiate the OneClassSupportVectorMachine with a specified window size and fit it to demonstration time series data. It also shows how to compute the decision function for the fitted data. ```python >>> from dtaianomaly.anomaly_detection import OneClassSupportVectorMachine >>> from dtaianomaly.data import demonstration_time_series >>> x, y = demonstration_time_series() >>> ocsvm = OneClassSupportVectorMachine(10).fit(x) >>> ocsvm.decision_function(x) array([-0.7442125 , -1.57019847, -1.86868112, ..., 13.33883568, 12.6492399 , 11.8761641 ]...) ``` -------------------------------- ### TimeMoE Anomaly Detection Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.TimeMoE.html This example demonstrates how to initialize, fit, and use the TimeMoE model for anomaly detection. It uses demonstration time series data and calculates anomaly scores. ```python from dtaianomaly.anomaly_detection import TimeMoE from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() time_moe = TimeMoE(10).fit(x) time_moe.decision_function(x) array([6.34949149e-05, 6.34949149e-05, 6.34949149e-05, ..., 6.34949149e-05, 6.34949149e-05, 6.34949149e-05]...) ``` -------------------------------- ### Generate Documentation Source: https://dtaianomaly.readthedocs.io/en/latest/additional_information/contributing.html Build the project documentation locally. This command generates HTML documentation and runs doctests to check for errors. ```bash docs/make html ``` ```bash docs/make doctest ``` -------------------------------- ### Robust PCA Anomaly Detection Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.RobustPrincipalComponentAnalysis.html Demonstrates how to initialize, fit, and compute anomaly scores using the RobustPrincipalComponentAnalysis class. This example uses demonstration time series data and a window size of 2. ```python from dtaianomaly.anomaly_detection import RobustPrincipalComponentAnalysis from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() rpca = RobustPrincipalComponentAnalysis(2).fit(x) rpca.decision_function(x) array([1.28436687, 1.29156655, 1.33793287, ..., 1.35563558, 1.25948662, 1.2923824 ]...) ``` -------------------------------- ### BaseNeuralDetector Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.BaseNeuralDetector.html Initializes the BaseNeuralDetector with various configuration parameters for training and model setup. ```APIDOC ## BaseNeuralDetector ### Description Base class for neural anomaly detectors, based on PyTorch. This class implements the main functionality for training a model and detecting anomalies, including building the data loader, building the optimizer, and implementing the main train and evaluation loops. Extensions of this class should also implement methods to build the data set, the neural architecture, and how to train and evaluate on a single batch. ### Parameters * **supervision** (Supervision) - The type of supervision this anomaly detector requires. * **window_size** (int or str) - The window size to use for extracting sliding windows from the time series. This value will be passed to `compute_window_size()`. * **stride** (int, default=1) - The stride, i.e., the step size for extracting sliding windows from the time series. * **standard_scaling** (bool, default=True) - Whether to standard scale each window independently, before feeding it to the network. * **batch_size** (int, default=32) - The size of the batches to feed to the network. * **data_loader_kwargs** (dictionary, default=None) - Additional kwargs to be passed to the data loader. For more information, see: https://docs.pytorch.org/docs/stable/data.html. * **optimizer** ({“adam”, “sgd”} or callable, default=”adam”) - The optimizer to use for learning the weights. If “adam” is given, then the torch.optim.Adam optimizer will be used. If “sgd” is given, then the torch.optim.SGD optimizer will be used. Otherwise, a callable should be given, which takes as input the network parameters, and then creates an optimizer. * **learning_rate** (float, default=1e-3) - The learning rate to use for training the network. Has no effect if optimize is a callable. * **compile_model** (bool, default=False) - Whether the network architecture should be compiled or not before training the weights. For more information, see: https://docs.pytorch.org/docs/stable/generated/torch.compile.html. * **compile_mode** ({“default”, “reduce-overhead”, “max-autotune”, “max-autotune-no-cudagraphs”}, default=”default”) - Method to compile the architecture. For more information, see: https://docs.pytorch.org/docs/stable/generated/torch.compile.html. * **n_epochs** (int, default=10) - The number of epochs for which the neural network should be trained. * **loss_function** ({“mse”, “l1”, “huber”} or torch.nn.Module, default=”mse”) - The loss function to use for updating the weights. Valid options are: 'mse': Use the Mean Squared Error loss. 'l1': Use the L1-loss or the mean absolute error. 'huber': Use the huber loss, which smoothly combines the MSE-loss with the L1-loss. `torch.nn.Module`: a custom torch module to use for the loss function. * **device** (str, default=”cpu”) - The device on which te neural network should be trained. For more information, see: https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch-device. * **seed** (int, default=None) - The seed used for training the model. This seed will update the torch and numpy seed at the beginning of the fit method. ### Attributes * **window_size_** (int) - The effectively used window size for this anomaly detector. * **optimizer_** (torch.optim.Optimizer) - The optimizer used for learning the weights of the network. * **neural_network_** (torch.nn.Module) - The PyTorch network architecture. ### See Also `BaseNeuralForecastingDetector` Use a neural network to forecast the time series, and detect anomalies by measuring the difference with the actual observations. `BaseNeuralReconstructionDetector` Use a neural network to reconstruct windows in the time series, and detect anomalies as windows that are incorrectly reconstructed. ``` -------------------------------- ### AffiliationPrecision Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.evaluation.AffiliationPrecision.html Demonstrates how to instantiate AffiliationPrecision and compute the precision score using ground truth and predicted anomaly labels. ```python from dtaianomaly.evaluation import AffiliationPrecision metric = AffiliationPrecision() y_true = [0, 0, 0, 1, 1, 0, 0, 0] y_pred = [1, 0, 0, 1, 1, 1, 0, 0] metric.compute(y_true, y_pred) ``` -------------------------------- ### Initialize and Fit RobustRandomCutForestAnomalyDetector Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.RobustRandomCutForestAnomalyDetector.html Demonstrates how to initialize the RobustRandomCutForestAnomalyDetector with a window size and random state, then fit it to demonstration time series data. ```python from dtaianomaly.anomaly_detection import RobustRandomCutForestAnomalyDetector from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() rrcf = RobustRandomCutForestAnomalyDetector(10, random_state=0).fit(x) ``` -------------------------------- ### AffiliationRecall Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.evaluation.AffiliationRecall.html Demonstrates how to instantiate and use the AffiliationRecall metric to compute the recall score between true and predicted anomaly labels. ```python from dtaianomaly.evaluation import AffiliationRecall metric = AffiliationRecall() y_true = [0, 0, 0, 1, 1, 0, 0, 0] y_pred = [1, 0, 0, 1, 1, 1, 0, 0] metric.compute(y_true, y_pred) ``` -------------------------------- ### Instantiate and Fit RandomDetector Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.RandomDetector.html Demonstrates how to instantiate the RandomDetector with a seed and fit it to demonstration time series data. This snippet is useful for setting up a baseline detector for testing purposes. ```python from dtaianomaly.anomaly_detection import RandomDetector from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() baseline = RandomDetector(seed=0).fit(x) baseline.decision_function(x) ``` -------------------------------- ### Instantiate and Load UCR Data Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.data.UCRLoader.html Demonstrates how to instantiate the UCRLoader with a file path and then load the dataset. Assumes the file path follows the expected naming convention for UCR datasets. ```python from dtaianomaly.data import UCRLoader path_to_ucr = "001_UCR_Anomaly_DISTORTED1sddb40_35000_52000_52620.txt" ucr_data_set = UCRLoader(path_to_ucr).load() ``` -------------------------------- ### Load Demonstration Time Series Source: https://dtaianomaly.readthedocs.io/en/latest/getting_started/data.html Loads the demonstration time series used throughout the dtaianomaly documentation. This is useful for quick testing and examples. ```python >>> from dtaianomaly.data import demonstration_time_series >>> X, y = demonstration_time_series() ``` -------------------------------- ### Initialize and Fit AlwaysNormal Detector Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.AlwaysNormal.html Demonstrates how to initialize the AlwaysNormal detector and fit it to demonstration time series data. This snippet shows the basic usage for setting up the detector. ```python >>> from dtaianomaly.anomaly_detection import AlwaysNormal >>> from dtaianomaly.data import demonstration_time_series >>> x, y = demonstration_time_series() >>> baseline = AlwaysNormal().fit(x) >>> baseline.decision_function(x) array([0., 0., 0., ..., 0., 0., 0.]...) ``` -------------------------------- ### Initialize and Fit MultilayerPerceptron Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.MultilayerPerceptron.html Demonstrates how to initialize the MultilayerPerceptron model with a specified seed and fit it to demonstration time series data. This snippet shows the basic usage for training the anomaly detector. ```python from dtaianomaly.anomaly_detection import MultilayerPerceptron from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() mlp = MultilayerPerceptron(10, seed=0).fit(x) mlp.decision_function(x) ``` -------------------------------- ### Instantiate and Fit CopulaBasedOutlierDetector Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.CopulaBasedOutlierDetector.html Demonstrates how to instantiate the CopulaBasedOutlierDetector with a specified window size and fit it to demonstration time series data. The decision function is then called to compute anomaly scores. ```python >>> from dtaianomaly.anomaly_detection import CopulaBasedOutlierDetector >>> from dtaianomaly.data import demonstration_time_series >>> x, y = demonstration_time_series() >>> copod = CopulaBasedOutlierDetector(10).fit(x) >>> copod.decision_function(x) array([ 9.90110663, 9.67868282, 9.51525285, ..., 25.00182389, 24.60594424, 24.30393026]...) ``` -------------------------------- ### LSTM Anomaly Detection Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.LongShortTermMemoryNetwork.html Demonstrates how to initialize, fit, and use the LongShortTermMemoryNetwork to detect anomalies in a time series. Requires importing the model and demonstration data. ```python >>> from dtaianomaly.anomaly_detection import LongShortTermMemoryNetwork >>> from dtaianomaly.data import demonstration_time_series >>> x, y = demonstration_time_series() >>> lstm = LongShortTermMemoryNetwork(10, seed=0).fit(x) >>> lstm.decision_function(x) array([0.354334 , 0.354334 , 0.28025536, ..., 0.61675562, 0.90525854, 0.39284754]...) ``` -------------------------------- ### DWT_MLEAD Anomaly Detection Example Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.DWT_MLEAD.html Demonstrates how to initialize and use the DWT_MLEAD detector to compute anomaly scores on a time series. No explicit fitting is required before calling decision_function. ```python >>> from dtaianomaly.anomaly_detection import DWT_MLEAD >>> from dtaianomaly.data import demonstration_time_series >>> x, y = demonstration_time_series() >>> dwt_mlead = DWT_MLEAD() # No fitting is necessary >>> dwt_mlead.decision_function(x) array([ 0., 0., 0., ..., 12., 12., 12.]...) ``` -------------------------------- ### Transformer Model Initialization and Fitting Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.Transformer.html Demonstrates how to initialize the Transformer model with a specified seed and fit it to demonstration time series data. This snippet shows the basic usage for training the anomaly detector. ```python from dtaianomaly.anomaly_detection import Transformer from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() transformer = Transformer(10, seed=0).fit(x) transformer.decision_function(x) ``` -------------------------------- ### Generate Demonstration Time Series Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.data.demonstration_time_series.html Imports the demonstration_time_series function and generates a sample time series dataset. This is useful for quick testing and visualization of anomaly detection algorithms. ```python from dtaianomaly.data import demonstration_time_series X, y = demonstration_time_series() ``` -------------------------------- ### Fit and Predict Anomaly Scores with LocalOutlierFactor Source: https://dtaianomaly.readthedocs.io/en/latest/api/auto_generated/dtaianomaly.anomaly_detection.LocalOutlierFactor.html Demonstrates how to initialize, fit, and compute anomaly scores using the LocalOutlierFactor detector. Ensure dtaianomaly is installed and demonstration data is available. ```python from dtaianomaly.anomaly_detection import LocalOutlierFactor from dtaianomaly.data import demonstration_time_series x, y = demonstration_time_series() local_outlier_factor = LocalOutlierFactor(10).fit(x) local_outlier_factor.decision_function(x) ```