Metadata-Version: 2.4
Name: dots-causal
Version: 0.1.0
Summary: DOTS (Diffusion Ordered Temporal Structure): causal structure learning from time series.
Author: Pedro Sanchez, Damian Machlanski, Steven McDonagh, Sotirios A. Tsaftaris
License-Expression: MIT
Project-URL: Homepage, https://github.com/chai-uk/dots
Project-URL: Paper, https://openreview.net/forum?id=hWuTzqggSd
Project-URL: Benchmark, https://chai-uk.github.io/tscd-benchmark/
Keywords: causal discovery,time series,diffusion models,structure learning
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.0
Requires-Dist: numpy
Requires-Dist: pandas
Requires-Dist: scikit-learn
Requires-Dist: networkx
Requires-Dist: tqdm
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# Diffusion Ordered Temporal Structure (DOTS)

This repository accompanies the paper "Causal Ordering for Structure Learning from Time Series" published in Transactions on Machine Learning Research ([link to the paper](https://openreview.net/forum?id=hWuTzqggSd)).

**See also:** [Time Series Causal Discovery Benchmark](https://chai-uk.github.io/tscd-benchmark/) (used to run experiments).

> **Reproducing the paper?** Use the [`tmlr-2025`](https://github.com/chai-uk/dots/tree/tmlr-2025) release.
> It contains the exact code and conda environment used for the paper's results.
> The `main` branch holds the actively maintained `dots-causal` Python package, which may change over time.

## Installation

```bash
pip install dots-causal
```

or, from a clone of this repository:

```bash
pip install -e .
```

### Requirement: R with `mgcv`

The final pruning step of DOTS (CAM pruning) runs in R. You need:

1. R installed, with `Rscript` on your `PATH` ([CRAN](https://cran.r-project.org/)).
2. The `mgcv` R package:

   ```bash
   Rscript -e 'install.packages("mgcv", repos = "https://cloud.r-project.org")'
   ```

If R or `mgcv` is missing, `DOTS.fit` raises `dots_causal.RNotFoundError` with instructions.

Alternatively, conda can install Python, R and `mgcv` together. From the repository root:

```bash
conda env create -f environment.yml
conda activate dots
```

A GPU is used automatically when PyTorch detects CUDA; otherwise DOTS runs on CPU.

## Quick start

DOTS takes *lagged* data: one column per variable per lag
(e.g. `X1_lag0, X2_lag0, X1_lag1, X2_lag1`, ...), with variables ordered within each lag.

```python
import pandas as pd
from dots_causal import DOTS

data_lagged = pd.read_csv("data/samples_lagged.csv")  # columns: X1_lag0, X2_lag0, X3_lag0, X1_lag1, ...
nb_vars = 3
nb_nodes = data_lagged.shape[1]
nb_lags = nb_nodes // nb_vars

model = DOTS(nb_nodes)
adj = model.fit(data_lagged.to_numpy(), nb_timesteps=nb_lags, nb_variables=nb_vars)

adj_df = pd.DataFrame(adj, columns=data_lagged.columns, index=data_lagged.columns)
```

`adj[i, j] == 1` means an estimated edge from node `i` to node `j`.

### Evaluating against a known graph

```python
from dots_causal import perform_eval, read_json

graphs_true = read_json("data/graphs_true.json")
print(perform_eval(graphs_true, adj_df, ["X1", "X2", "X3"]))
```

## Demo

From the repository root, run `sh setup-run.sh` to create the conda environment and run the demo in one step, or after installing:

```bash
python demo.py
```

There is also an interactive [notebook demo](https://github.com/chai-uk/dots/blob/main/demo.ipynb).

## Replicating Experiments

You can use the [TSCD Benchmark](https://chai-uk.github.io/tscd-benchmark/) to fully replicate our results in an end-to-end manner.

## Cite Us

```
@article{sanchez2025causal,
title={Causal Ordering for Structure Learning from Time Series},
author={Pedro Sanchez and Damian Machlanski and Steven McDonagh and Sotirios A. Tsaftaris},
journal={Transactions on Machine Learning Research},
issn={2835-8856},
year={2025},
url={https://openreview.net/forum?id=hWuTzqggSd}}
```
