causalexplain package#

Subpackages#

Submodules#

This module contains the GraphDiscovery class which is responsible for creating, fitting, and evaluating causal discovery experiments.

class GraphDiscovery(experiment_name=None, model_type='rex', csv_filename=None, true_dag_filename=None, verbose=False, seed=42)[source]#

Bases: object

Attributes:
model

Methods

combine_and_evaluate_dags([prior])

Retrieve the DAG from the Experiment objects.

create_experiments()

Create an Experiment object for each regressor.

export(output_file)

This method exports the DAG to a DOT file.

fit_experiments([hpo_iterations, ...])

Fit the Experiment objects.

load(model_path)

Load the model from a pickle file.

plot([show_metrics, show_node_fill, title, ...])

This method plots the DAG using networkx and matplotlib.

printout_results(graph, metrics)

This method prints the DAG to stdout in hierarchical order.

run([hpo_iterations, bootstrap_iterations, ...])

Run the experiment.

save(full_filename_path)

Save the model as an Experiment object.

__init__(experiment_name=None, model_type='rex', csv_filename=None, true_dag_filename=None, verbose=False, seed=42)[source]#

Initializes a new instance of the GraphDiscovery class.

Parameters:
  • experiment_name (str, optional) – The name of the experiment.

  • model_type (str, optional) – The type of model to use. Valid options are: ‘rex’, ‘pc’, ‘fci’, ‘ges’, ‘lingam’, ‘cam’, ‘notears’.

  • csv_filename (str, optional) – The filename of the CSV file containing the data.

  • true_dag_filename (str, optional) – The filename of the DOT file containing the true causal graph.

  • verbose (bool, optional) – Whether to print verbose output.

  • seed (int, optional) – The random seed for reproducibility.

create_experiments()[source]#

Create an Experiment object for each regressor.

Parameters:
  • dataset_name (str) – Name of the dataset

  • true_dag (str) – Path to the true DAG DOT file

  • regressors (list) – List of regressor types to create experiments for

  • dataset_path (str) – Path to the input dataset

  • output_path (str) – Path for output files

Returns:

A dictionary of Experiment objects

Return type:

dict

fit_experiments(hpo_iterations=None, bootstrap_iterations=None, prior=None, **kwargs)[source]#

Fit the Experiment objects.

Parameters:
  • trainer (dict) – A dictionary of Experiment objects

  • estimator (str) – The estimator to use (‘rex’ or other)

  • verbose (bool, optional) – Whether to print verbose output. Defaults to False.

  • hpo_iterations (int, optional) – Number of HPO trials for REX. Defaults to None.

  • bootstrap_iterations (int, optional) – Number of bootstrap trials for REX. Defaults to None.

combine_and_evaluate_dags(prior=None)[source]#

Retrieve the DAG from the Experiment objects.

Parameters:

prior (List[List[str]], optional) – The prior to use for ReX. Defaults to None.

Returns:

The experiment object with the final DAG

Return type:

Experiment

run(hpo_iterations=None, bootstrap_iterations=None, prior=None, **kwargs)[source]#

Run the experiment.

Parameters:
  • hpo_iterations (int, optional) – Number of HPO trials for REX. Defaults to None.

  • bootstrap_iterations (int, optional) – Number of bootstrap trials for REX. Defaults to None.

save(full_filename_path)[source]#

Save the model as an Experiment object.

Parameters:

full_filename_path (str) – A full path where to save the model, including the filename.

load(model_path)[source]#

Load the model from a pickle file.

Parameters:

model_path (str) – Path to the pickle file containing the model

Returns:

The loaded Experiment object

Return type:

Experiment

printout_results(graph, metrics)[source]#

This method prints the DAG to stdout in hierarchical order.

export(output_file)[source]#

This method exports the DAG to a DOT file.

plot(show_metrics=False, show_node_fill=True, title=None, ax=None, figsize=(5, 5), dpi=75, save_to_pdf=None, layout='dot', **kwargs)[source]#

This method plots the DAG using networkx and matplotlib.

property model#

Module contents#

CausalExplain: A Python package for causal discovery and inference.

This package provides tools for discovering and analyzing causal relationships in data using various methods and algorithms.

class GraphDiscovery(experiment_name=None, model_type='rex', csv_filename=None, true_dag_filename=None, verbose=False, seed=42)[source]#

Bases: object

Attributes:
model

Methods

combine_and_evaluate_dags([prior])

Retrieve the DAG from the Experiment objects.

create_experiments()

Create an Experiment object for each regressor.

export(output_file)

This method exports the DAG to a DOT file.

fit_experiments([hpo_iterations, ...])

Fit the Experiment objects.

load(model_path)

Load the model from a pickle file.

plot([show_metrics, show_node_fill, title, ...])

This method plots the DAG using networkx and matplotlib.

printout_results(graph, metrics)

This method prints the DAG to stdout in hierarchical order.

run([hpo_iterations, bootstrap_iterations, ...])

Run the experiment.

save(full_filename_path)

Save the model as an Experiment object.

__init__(experiment_name=None, model_type='rex', csv_filename=None, true_dag_filename=None, verbose=False, seed=42)[source]#

Initializes a new instance of the GraphDiscovery class.

Parameters:
  • experiment_name (str, optional) – The name of the experiment.

  • model_type (str, optional) – The type of model to use. Valid options are: ‘rex’, ‘pc’, ‘fci’, ‘ges’, ‘lingam’, ‘cam’, ‘notears’.

  • csv_filename (str, optional) – The filename of the CSV file containing the data.

  • true_dag_filename (str, optional) – The filename of the DOT file containing the true causal graph.

  • verbose (bool, optional) – Whether to print verbose output.

  • seed (int, optional) – The random seed for reproducibility.

create_experiments()[source]#

Create an Experiment object for each regressor.

Parameters:
  • dataset_name (str) – Name of the dataset

  • true_dag (str) – Path to the true DAG DOT file

  • regressors (list) – List of regressor types to create experiments for

  • dataset_path (str) – Path to the input dataset

  • output_path (str) – Path for output files

Returns:

A dictionary of Experiment objects

Return type:

dict

fit_experiments(hpo_iterations=None, bootstrap_iterations=None, prior=None, **kwargs)[source]#

Fit the Experiment objects.

Parameters:
  • trainer (dict) – A dictionary of Experiment objects

  • estimator (str) – The estimator to use (‘rex’ or other)

  • verbose (bool, optional) – Whether to print verbose output. Defaults to False.

  • hpo_iterations (int, optional) – Number of HPO trials for REX. Defaults to None.

  • bootstrap_iterations (int, optional) – Number of bootstrap trials for REX. Defaults to None.

combine_and_evaluate_dags(prior=None)[source]#

Retrieve the DAG from the Experiment objects.

Parameters:

prior (List[List[str]], optional) – The prior to use for ReX. Defaults to None.

Returns:

The experiment object with the final DAG

Return type:

Experiment

run(hpo_iterations=None, bootstrap_iterations=None, prior=None, **kwargs)[source]#

Run the experiment.

Parameters:
  • hpo_iterations (int, optional) – Number of HPO trials for REX. Defaults to None.

  • bootstrap_iterations (int, optional) – Number of bootstrap trials for REX. Defaults to None.

save(full_filename_path)[source]#

Save the model as an Experiment object.

Parameters:

full_filename_path (str) – A full path where to save the model, including the filename.

load(model_path)[source]#

Load the model from a pickle file.

Parameters:

model_path (str) – Path to the pickle file containing the model

Returns:

The loaded Experiment object

Return type:

Experiment

printout_results(graph, metrics)[source]#

This method prints the DAG to stdout in hierarchical order.

export(output_file)[source]#

This method exports the DAG to a DOT file.

plot(show_metrics=False, show_node_fill=True, title=None, ax=None, figsize=(5, 5), dpi=75, save_to_pdf=None, layout='dot', **kwargs)[source]#

This method plots the DAG using networkx and matplotlib.

property model#