This repository provides the implementation of the RoCA: Robust Contrastive One-class Time Series Anomaly Detection with Contaminated Data method, called RoCA below.
The increasing volume of time-series signals and the scarcity of labels make time-series anomaly detection a natural fit for self-supervised deep learning. However, existing normality-based approaches face two key limitations: (1) relying on a single assumption often fails to capture the whole normal patterns, leading to biased representations; and (2) they typically presume clean training data, which is unrealistic in practice and undermines model robustness. In this paper, we propose RoCA, a unified and robust anomaly detection framework that simultaneously addresses assumption incompleteness and data contamination. The key insight is that normal samples tend to satisfy multiple normality assumptions, whereas anomalous or contaminated samples should violate at least one. RoCA employs a composite loss function consisting of a multi-normality alignment term, a dynamic abnormality-aware term, and a variance regularization term to maintain training stability. This design enables RoCA to dynamically discover and isolate latent anomalies during training, without requiring clean supervision. Extensive experiments on both univariate and multivariate time-series benchmarks demonstrate that RoCA consistently outperforms state-of-the-art methods, achieving up to 7.3% improvement under real-world contamination. Our theoretical analysis further reveals the intrinsic synergy between contrastive learning and one-class classification under the RoCA framework.
Link to our paper xxx. If you use this code for your research, please cite our paper:
We will add citation information later.
This code is based on Python 3.8, all requirements are written in requirements.txt. Additionally, we should install saleforce-merlion v1.1.1 and ts_dataset as Merlion suggested.
pip install salesforce-merlion==1.1.1
pip install -r requirements.txt
We acknowledge the dataset's contributors, including AIOps, UCR, SWaT, and WADI.
This repository already includes Merlion's data loading package ts_datasets.
- AIOps Link: https://github.com/NetManAIOps/KPI-Anomaly-Detection
- UCR Link: https://wu.renjie.im/research/anomaly-benchmarks-are-flawed/ and https://www.cs.ucr.edu/~eamonn/time_series_data_2018/UCR_TimeSeriesAnomalyDatasets2021.zip
- Download and unzip the data in
data/iops_competitionanddata/ucrrespectively. e.g. For AIOps, downloadphase2.zipand unzip thedata/iops_competition/phase2.zipbefore running the program.
- For SWaT and WADI, you need to apply by their official tutorial. Link: https://itrust.sutd.edu.sg/itrust-labs_datasets/dataset_info/
- Because multiple versions of these two datasets exist,
we used their newer versions:
SWaT.SWaT.A2_Dec2015, version 0andWADI.A2_19Nov2019. - Download and unzip the data in
data/swatanddata/wadirespectively. Then run theswat_preprocessing()andwadi_preprocessing()functions indataloader/data_preprocessing.pyfor preprocessing.
This directory contains experiment parameters for all models on AIOps (as IOpsCompetition in the code), UCR, SWaT, and WADI datasets.
Source code of the RoCA model.
Processed datasets. Such as data/UCR, data/WADI.
Directory where the experiment result is saved.
# RoCA Method (dataset_name: IOpsCompetition, UCR, SWaT, WADI)
python roca.py --selected_dataset <dataset_name> --device cuda --seed 2
Anomaly Transformer(AnoTrans, AOT), AOC, RandomScore(RAS), NCAD, LSTMED, OC_SVM, IF, SR, RRCF, SVDD, DAMP, TS_AD(TCC)
We reiterate that in addition to our method, the source code of other baselines is based on the GitHub source code provided by their papers. For reproducibility, we changed the source code of their models as little as possible. We are grateful for the work on these papers.
We consult the GitHub source code of the paper corresponding to the baseline and then reproduce it. For baselines that use the same datasets as ours, we use their own recommended hyperparameters. For different datasets, we use the same hyperparameter optimization method Grid Search as our model to find the optimal hyperparameters.
Part of the code, especially the baseline code, is based on the following source code.
- Anomaly Transformer(AOT)
- AOC
- Deep-SVDD-PyTorch
- TS-TCC
- DAMP and DAMP-python
- LSTM_ED, SR, and IF are reproduced based on saleforce-merlion
- RRCF
- Metrics:affiliation-metrics