Metadata-Version: 2.4
Name: dots-causal
Version: 0.2.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

Pass your time series as it is (one column per variable, rows in time order) and choose how many past time steps to consider:

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

data = pd.read_csv("data/samples.csv")                # columns: X1, X2, X3
graph = DOTS(max_lag=1, random_state=0).fit(data)
```

`graph` is a pandas DataFrame over the lagged variables (`X1_lag0, X2_lag0, X3_lag0, X1_lag1, ...`,
where `lag0` is the current time step and `lag1` the previous one).
`graph.loc[cause, effect] == 1` means an estimated edge, e.g. `graph.loc["X1_lag1", "X2_lag0"]`
is an effect of `X1` on `X2` one time step later. NumPy arrays work too (variables are then named `X1, X2, ...`).
`random_state` is optional and makes runs reproducible.

### Options

| Parameter | Default | Meaning |
|---|---|---|
| `max_lag` | `1` | Number of past time steps considered as causes. |
| `voting_threshold` | `0.0` | DOTS combines several causal orderings by voting. A candidate edge must agree with more than this fraction of them (before pruning). `0.0` = any ordering (as in the paper); higher = sparser. |
| `n_orderings` | `10` | How many diffusion time steps are used to compute orderings (`n_orderings + 1` orderings with the defaults). |
| `normalize` | `True` | Standardise each variable to zero mean and unit variance before training. |
| `allow_instantaneous` | `False` | Also allow edges within the same time step. |
| `pruning_cutoff` | `0.001` | Significance level of the final pruning step: a candidate parent is kept if its p-value is below this. Higher = more edges. |
| `random_state` | `None` | Seed for reproducible results. |

Training settings (`epochs`, `batch_size`, `learning_rate`, ...) are listed in `help(DOTS)`.
To see the lagged data DOTS works with, use `dots_causal.make_lagged(data, max_lag)`.

### 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, graph))
```

## 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}}
```
