TSLib: Time Series Library

repository·main·Indexed 11 days ago

https://github.com/thuml/time-series-library

An open-source library for deep time series analysis providing a standardized codebase for forecasting, imputation, anomaly detection, and classification. It includes a wide range of baseline models such as PatchTST, iTransformer, and DLinear, as well as support for zero-shot evaluation of Large Time Series Models (LTSMs) like Chronos and Moirai. As of April 2026, the library is in maintenance-only mode.

Tokens
10.8K
Snippets
21
Records
38
Agent score
94%

What's inside TSLib

  1. Overview of TSLib (Time Series Library)

    main

    TSLib is an open-source library designed for deep learning researchers specializing in deep time series analysis. It provides a clean codebase for evaluating state-of-the-art deep time series models or developing custom models. The library covers five major mainstream tasks:

    1. Long-term forecasting
    2. Short-term forecasting
    3. Imputation
    4. Anomaly detection
    5. Classification
  2. Overview of Time Series Library (TSLib)

    main

    TSLib is an open-source library designed for deep learning researchers focusing on deep time series analysis. It provides a unified codebase for evaluating advanced models or developing new ones. The library supports five mainstream time series tasks:

    • Long-term forecasting
    • Short-term forecasting
    • Imputation
    • Anomaly detection
    • Classification

    Note: As of April 2026, the library is in a maintenance-only mode. While baseline implementations remain correct, many benchmarks may no longer be sufficient for evaluating state-of-the-art progress, and users are encouraged to seek newer benchmarks.

  3. View the Time Series Analysis Leaderboard

    main

    The TSLib repository maintains a leaderboard for various time series analysis tasks, including Long-term Forecasting (with different look-back windows), Short-term Forecasting, Imputation, Classification, and Anomaly Detection. The leaderboard tracks state-of-the-art models and provides links to their respective papers and implementations within the library.

    Key Tasks Tracked:

    • Long-term Forecasting (Look-Back-96): Models optimized for long-range prediction.
    • Long-term Forecasting (Look-Back-Searching): Models using searching mechanisms for look-back windows.
    • Short-term Forecasting: Models for immediate future prediction.
    • Imputation: Models for filling missing values.
    • Classification: Models for categorizing time series data.
    • Anomaly Detection: Models for identifying outliers or unusual patterns.
  4. Understand the TSLib project architecture

    main

    The project follows a structured pipeline for time series analysis:

    • Execution Flow: scripts/*.sh (parameters) $\rightarrow$ run.py (entry point/dispatcher) $\rightarrow$ exp/* (task-specific experiment logic) $\rightarrow$ data_provider (data loading) & models (network instantiation) $\rightarrow$ results saved to ./checkpoints.
    • Experiment Layer (exp/): Exp_Basic serves as the base class for all tasks, handling model registration and device management. Subclasses (e.g., exp_long_term_forecasting.py) implement task-specific train, test, and val logic.
    • Model & Layer Layer (models/ & layers/): models/ contains the network architectures. Common building blocks like attention, convolutions, and frequency-domain units are stored in layers/ to promote reuse.
    • Data Layer (data_provider/): data_factory.py selects the appropriate DataLoader based on the task. data_loader.py handles sequence slicing, sliding windows, and masking strategies.
    • Utility Layer (utils/): Contains evaluation metrics (metrics.py), training tools like EarlyStopping (tools.py), and task-specific augmentation or masking logic.
  5. Understand the Time-Series-Library architecture

    main

    The project follows a layered architecture designed for end-to-end (E2E) time-series analysis:

    • Entry Point (run.py): The unified interface that parses arguments and dispatches tasks to the appropriate experiment class based on --task_name.
    • Experiment Layer (exp/): Contains task-specific logic. Exp_Basic serves as the base class for registering models and devices, while subclasses (e.g., exp_imputation.py, exp_classification.py) implement the specific train and test flows.
    • Model & Layer Layer (models/ and layers/): models/ contains the high-level architectures (e.g., TimesNet.py), while layers/ contains reusable primitives like attention blocks, convolutional units, or embedding layers.
    • Data Layer (data_provider/): Uses a data_factory.py to return the correct DataLoader for a given task. The data_loader.py handles windowing, masking, and sampling logic.
    • Utility Layer (utils/): Provides shared tools for metrics, augmentation, masking, and training helpers like EarlyStopping.
    • Script Layer (scripts/): Contains bash recipes that capture specific hyperparameters and configurations for reproducibility.

    E2E Data Flow: scripts/*.sh $\rightarrow$ run.py $\rightarrow$ exp/* $\rightarrow$ data_provider & models $\rightarrow$ utils $\rightarrow$ results written to ./checkpoints.

  6. Explore implemented baseline and foundation models

    main

    TSLib includes implementations for a wide range of time series models, categorized into baseline models and large-scale foundation models (LTSM). Many of these models are available for zero-shot evaluation.

    Baseline Models include:

    • Transformers/Attention-based: iTransformer, PatchTST, TimesNet, Autoformer, Informer, Crossformer, TFT.
    • MLP-based: TSMixer, DLinear, LightTS, FreTS.
    • State Space/Mamba-based: Mamba, MambaSL.
    • Other architectures: FEDformer, Pyraformer, SegRNN, Koopa, TiDE, SCINet.

    Large Time Series Models (Foundation Models) include:

    • Chronos2
    • TiRex
    • Sundial
    • Time-MoE
    • Chronos
    • Moirai
    • TimesFM
  7. How to develop your own model

    main

    To extend the library with a new model architecture:

    1. Add the model file: Create a new Python file in the ./models directory. Use ./models/Transformer.py as a template for the required structure.
    2. Create experiment scripts: Add corresponding bash scripts in the ./scripts directory to define how your model should be trained and evaluated on specific datasets.
  8. Reproduce benchmark experiments using scripts

    main

    The ./scripts/ directory contains bash scripts for reproducing all benchmark experiments. Note that some scripts may have CUDA_VISIBLE_DEVICES hardcoded; modify or remove this setting to match your GPU configuration.

    Examples:

    • Long-term forecasting: bash ./scripts/long_term_forecast/ETT_script/TimesNet_ETTh1.sh
    • Short-term forecasting: bash ./scripts/short_term_forecast/TimesNet_M4.sh
    • Imputation: bash ./scripts/imputation/ETT_script/TimesNet_ETTh1.sh
    • Anomaly detection: bash ./scripts/anomaly_detection/PSM/TimesNet.sh
    • Classification: bash ./scripts/classification/TimesNet.sh
    bash ./scripts/long_term_forecast/ETT_script/TimesNet_ETTh1.sh
  9. Apply Augmentation via Shell Script

    main

    You can perform data augmentation by passing specific augmentation type labels as command-line arguments to run.py.

    To control the frequency of augmentation, use the --augmentation_ratio parameter. This parameter represents how many times the augmentation method is performed. For example:

    • Setting --augmentation_ratio 1 followed by --jitter performs the jitter augmentation once.
    • Setting --augmentation_ratio 3 followed by three different augmentation labels will perform those three augmentations three times each (or according to the specific implementation logic of the ratio).

    Note: The augmentation code follows the standard Time-Series-Library prototype. All other training parameters (like --batch_size, --learning_rate, etc.) are configurable via run.py.

    export CUDA_VISIBLE_DEVICES=0
    
    model_name=PatchTST
    
    for aug in jitter scaling permutation magwarp timewarp windowslice windowwarp rotation spawner dtwwarp shapedtwwarp wdba discdtw discsdtw
    do
    echo using augmentation: ${aug}
    
    python -u run.py \
      --task_name classification \
      --is_training 1 \
      --root_path ./dataset/EthanolConcentration/ \
      --model_id EthanolConcentration \
      --model $model_name \
      --data UEA \
      --e_layers 3 \
      --batch_size 16 \
      --d_model 128 \
      --d_ff 256 \
      --top_k 3 \
      --des 'Exp' \
      --itr 1 \
      --learning_rate 0.001 \
      --train_epochs 100 \
      --patience 10 \
      --augmentation_ratio 1 \
      --${aug}
     done
  10. Contribute new models to the leaderboard

    main

    If you have developed advanced models for time series analysis, you can contribute them to the repository by:

    1. Sending a link to your paper or code.
    2. Raising a Pull Request to add the model and update the leaderboard.
  11. Install Time-Series-Library via Conda and Pip

    main

    Follow these steps to set up a local development environment:

    1. Clone the repository:

      git clone https://github.com/thuml/Time-Series-Library.git
      cd Time-Series-Library
    2. Create and activate a Conda environment:

      conda create -n tslib python=3.11
      conda activate tslib
    3. Install Core Dependencies: Ensure you install a torch version that matches your local CUDA version. Recommended: torch==2.5.1.

      pip install torch==2.5.1 --index-url https://download.pytorch.org/whl/cu121
      pip install -r requirements.txt
    4. Install Mamba Model dependencies (Linux only, required for models/Mamba.py): The Mamba wheel must match your CUDA version. Example for CUDA 12:

      pip install https://github.com/state-spaces/mamba/releases/download/v2.2.6.post3/mamba_ssm-2.2.6.post3+cu12torch2.5cxx11abiFALSE-cp311-cp311-linux_x86_64.whl
    5. Install Moirai Model dependencies (Required for models/Moirai.py):

      pip install uni2ts --no-deps
  12. Train and Evaluate using experiment scripts

    main

    Reproducible experiment results are provided as bash scripts in the ./scripts/ directory.

    Note: Some scripts contain CUDA_VISIBLE_DEVICES settings. You must modify or remove these to match your actual GPU configuration.

    Available script categories:

    • long_term_forecast/
    • short_term_forecast/
    • imputation/
    • anomaly_detection/
    • classification/
    • exogenous_forecast/
    # long-term forecast
    bash ./scripts/long_term_forecast/ETT_script/TimesNet_ETTh1.sh
    # short-term forecast
    bash ./scripts/short_term_forecast/TimesNet_M4.sh
    # imputation
    bash ./scripts/imputation/ETT_script/TimesNet_ETTh1.sh
    # anomaly detection
    bash ./scripts/anomaly_detection/PSM/TimesNet.sh
    # classification
    bash ./scripts/classification/TimesNet.sh