Skip to main content

A package to extract the causal graph from continuous tabular data.

Project description

logo

License Python Platform PyPI version Build Status codecov Documentation

CausalExplain - A library to infer causal-effect relationships from tabular data

'CausalExplain' is a library that implements methods to extract the causal graph, from tabular data, specifically the ReX method, and other compared methods like GES, PC, FCI, LiNGAM, CAM, and NOTEARS.

This repository contains the implementation of ReX and all necessary tools to reproduce the results presented in our accompanying paper. ReX supports diverse data generation processes, including non-linear and additive noise models, and has demonstrated robust performance on synthetic and real-world datasets.

About ReX

ReX is a causal discovery method that leverages machine learning (ML) models coupled with explainability techniques, specifically Shapley values, to identify and interpret significant causal relationships among variables. Comparative evaluations on synthetic datasets comprising tabular data reveal that ReX outperforms state-of-the-art causal discovery methods across diverse data generation processes, including non-linear and additive noise models. Moreover, ReX was tested on the Sachs single-cell protein-signaling dataset, achieving a precision of 0.952 and recovering key causal relationships with no incorrect edges. Taking together, these results showcase ReX's effectiveness in accurately recovering true causal structures while minimizing false positive predictions, its robustness across diverse datasets, and its applicability to real-world problems. By combining ML and explainability techniques with causal discovery, ReX bridges the gap between predictive modeling and causal inference, offering an effective tool for understanding complex causal structures.

ReX Schema

Our experimental results, conducted on five families of synthetic datasets with varying complexity, demonstrate that REX consistently recovers true causal relationships with high precision while minimizing false positives and orientation errors, comparing favorably to existing methods. Additionally, REX was tested on the Sachs single-cell protein-signaling dataset (Sachs et al., 2005), achieving a competitive performance with no false positives and recovering important causal relationships. This further validates the applicability of REX to real-world datasets, highlighting its robustness across different types of data.

Prerequisites without Docker

  • Operating System: Linux or macOS
  • Environment Manager: PyEnv or Conda
  • Programming Language: Python 3.10.12 or higher
  • Hardware: CPU

Installation

The project can be installed using pip:

$ pip install causalexplain

Data

The datasets used in the paper and the examples can be generated using the generators module, which is also part of this library. In case you want to reproduce results from the articles that we used as reference, you can find the datasets in the data folder.

Executing causalexplain

Option 1: Command Line

To run causalexplain on your data, you can use the causalexplain command:

$ python -m causalexplain
   ___                      _                 _       _
  / __\__ _ _   _ ___  __ _| | _____  ___ __ | | __ _(_)_ __
 / /  / _` | | | / __|/ _` | |/ _ \ \/ / '_ \| |/ _` | | '_ \
/ /__| (_| | |_| \__ \ (_| | |  __/>  <| |_) | | (_| | | | | |
\____/\__,_|\__,_|___/\__,_|_|\___/_/\_\ .__/|_|\__,_|_|_| |_|
                                       |_|
usage: causalexplain [-h] -d DATASET [-m {rex,pc,fci,ges,lingam,cam,notears}]
                   [-t TRUE_DAG] [-l LOAD_MODEL] [-T THRESHOLD] [-u UNION]
                   [-i ITERATIONS] [-b BOOTSTRAP] [-r REGRESSOR] [-S SEED]
                   [-s [SAVE_MODEL]] [-n] [-v] [-q] [-o OUTPUT]

that will present you with a menu to choose the dataset you want to use, the method you want to use to infer the causal graph, and the hyperparameters you want to use.

The minimum required to run causalexplain is a dataset file in CSV format, with the first row containing the names of the variables, and the rest of the rows containing the values of the variables. The method selected by default is ReX, but you can also choose between PC, FCI, GES, LiNGAM, CAM, NOTEARS. At the end of the execution, the edges of the plausible causal graph will be displayed along with the metrics obtained, if the true dag is provided (argument -t).

Option 2: Notebook

In case you want to run causalexplain from your code in a notebook, you can use the GraphDiscovery class. The following example shows how to use the GraphDiscovery class to train a model on a dataset using ReX method:

from causalexplain import GraphDiscovery

experiment = GraphDiscovery(
   experiment_name='my_experiment',
   model_type='rex',
   csv_filename='data.csv',
   true_dag_filename='true_graph.dot')

# Run the experiments
experiment.run()

To load a model from a file, you can use the load method of the GraphDiscovery class:

from causalexplain import GraphDiscovery

experiment = GraphDiscovery()
experiment.load("/path/to/model.pkl")

This can be useful if you want to train a model on a dataset and then use it to predict causal graphs on other datasets, or train a model on different batches.

Once a model has been trained or loaded, you can plot the resulting DAG, save the trained model to a file, or export the predicted causal graph to a DOT file.

# Plot the resulting DAG
experiment.plot()

# Save the trained model to a file
experiment.save("/path/to/model.pkl")

To export the predicted causal graph to a DOT file, you can use the export method of the GraphDiscovery class:

experiment.export("/path/to/my_predicted_graph.dot")

Output

The output of causalexplain is typically a graph with the edges of the plausible causal graph and the metrics obtained from the evaluation of the causal graph against the true DAG. These results are printed to the console, unless the '-o' option is specified, in which case the DAG is saved to a file in DOT format. Metrics are printed only if the true DAG is provided.

Example CLI commands

The following command illustrates how to run causalexplain on the toy dataset using the ReX method:

$ python -m causalexplain -d /path/to/toy_dataset.csv -t /path/to/toy_dataset.dot

The same command can be used to run causalexplain on the toy dataset using the CAM method:

$ python -m causalexplain -d /path/to/toy_dataset.csv -m cam -t /path/to/toy_dataset.dot

For more information on command line options, run causalexplain -h or go to the Quickstart section in the documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

causalexplain-0.6.1b0.tar.gz (217.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

causalexplain-0.6.1b0-py3-none-any.whl (260.1 kB view details)

Uploaded Python 3

File details

Details for the file causalexplain-0.6.1b0.tar.gz.

File metadata

  • Download URL: causalexplain-0.6.1b0.tar.gz
  • Upload date:
  • Size: 217.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for causalexplain-0.6.1b0.tar.gz
Algorithm Hash digest
SHA256 c762564488f815465fd93f01399d18dc599353c938ce9ec5f3e6726eeba2f2f7
MD5 8051b61b8e9a2d0f34c9a14a55fc506d
BLAKE2b-256 cbaeb0e77ada7d8898ffb1aa88a9dee32b4199f746eb84bdc0b1a18adb795ac6

See more details on using hashes here.

File details

Details for the file causalexplain-0.6.1b0-py3-none-any.whl.

File metadata

  • Download URL: causalexplain-0.6.1b0-py3-none-any.whl
  • Upload date:
  • Size: 260.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for causalexplain-0.6.1b0-py3-none-any.whl
Algorithm Hash digest
SHA256 2662ec6f6b853625920d19a09b32f47cd02f2df6f2420a7bf746fde113d6b864
MD5 4a2539a33b2c5492301e78920af046bb
BLAKE2b-256 ed274d97e8c4c3a448ff473e982453c11c7a592f345eb99d983878cc6128361f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page