Skip to content

Graph Conversion Functions

The convert module provides functions for transforming between different graph representations used in causal discovery algorithms and utilities for working with adjacency matrices.

Functions

dag_to_pdag(dag)

Convert a DAG to its corresponding PDAG representation (equivalence class).

Parameters:

  • dag (DAG): The directed acyclic graph to convert

Returns:

  • PDAG: PDAG representing the Markov equivalence class

Usage:

from causaliq_core.graph import DAG, dag_to_pdag

# Create a DAG
dag = DAG(['X', 'Y', 'Z'], [('X', '->', 'Y'), ('Y', '->', 'Z')])

# Convert to PDAG equivalence class
pdag = dag_to_pdag(dag)

pdag_to_cpdag(pdag)

Convert a PDAG to its completed PDAG (CPDAG) form.

Parameters:

  • pdag (PDAG): The partially directed graph to complete

Returns:

  • PDAG or None: Completed PDAG, or None if completion fails

Usage:

from causaliq_core.graph import PDAG, pdag_to_cpdag

# Create a PDAG
pdag = PDAG(['A', 'B', 'C'], [('A', '--', 'B'), ('B', '->', 'C')])

# Complete to CPDAG
cpdag = pdag_to_cpdag(pdag)

dict_to_adjmat(adjmat_dict, labels=None)

Convert a dictionary representation of an adjacency matrix to a pandas DataFrame.

Parameters:

  • adjmat_dict (Dict): Dictionary with (row, col) tuple keys and numeric values
  • labels (List[str], optional): Variable labels. If None, creates labels A, B, C, etc.

Returns:

  • DataFrame: Adjacency matrix as a pandas DataFrame

Raises:

  • TypeError: If adjmat_dict is not a dictionary or labels is not a list
  • ValueError: If keys are not tuples of length 2 or contain non-integer indices

Usage:

from causaliq_core.graph.convert import dict_to_adjmat

# Create adjacency matrix from dictionary
adjmat_dict = {(0, 1): 1.0, (1, 2): 1.0}
labels = ['X', 'Y', 'Z']
df = dict_to_adjmat(adjmat_dict, labels)

extend_pdag(pdag)

Extend a PDAG to a consistent DAG by orienting undirected edges.

Parameters:

  • pdag (PDAG): The partially directed graph to extend

Returns:

  • DAG: A DAG extension of the PDAG

Usage:

from causaliq_core.graph import PDAG, extend_pdag

# Create a PDAG
pdag = PDAG(['X', 'Y'], [('X', '--', 'Y')])

# Extend to DAG
dag = extend_pdag(pdag)

is_cpdag(pdag)

Check whether a PDAG is a completed PDAG (CPDAG).

Parameters:

  • pdag (PDAG): The graph to check

Returns:

  • bool: True if the PDAG is completed, False otherwise

Usage:

from causaliq_core.graph import PDAG, is_cpdag

# Check if PDAG is complete
pdag = PDAG(['A', 'B'], [('A', '->', 'B')])
is_complete = is_cpdag(pdag)  # True

Reference

Functions:

  • dag_to_pdag

    Generates PDAG representing equivalence class DAG belongs to.

  • pdag_to_cpdag

    Generates a completed PDAG (CPDAG) from supplied PDAG

  • is_cpdag

    Whether the PDAG is a Completed PDAG (CPDAG)

  • extend_pdag

    Generates a DAG which extends a PDAG (i.e. is a member of the

  • dict_to_adjmat

    Create an adjacency matrix with specified entries.

Classes

Functions

dag_to_pdag

dag_to_pdag(dag: DAG) -> PDAG

Generates PDAG representing equivalence class DAG belongs to.

Uses the algorithm in "A Transformational Characterization of Equivalent Bayesian Network Structures", Chickering, 1995. Step numbers in comments refer to algorithm step numbers in paper.

Parameters:

  • dag
    (DAG) –

    DAG whose PDAG is required.

Raises:

  • TypeError

    if dag is not of type DAG

Returns:

  • PDAG

    PDAG for equivalence class that dag belongs to

pdag_to_cpdag

pdag_to_cpdag(pdag: PDAG) -> Union[PDAG, None]

Generates a completed PDAG (CPDAG) from supplied PDAG

:param PDAG pdag: PDAG to be completed

:raises TypeError: if pdag is not of type PDAG :raises ValueError: if pdag is non-extendable

:returns PDAG/None: CPDAG corresponding to pdag

is_cpdag

is_cpdag(pdag: PDAG) -> bool

Whether the PDAG is a Completed PDAG (CPDAG)

:param PDAG pdag: PDAG to check

:raises ValueError: if PDAG is not extendable

:returns bool: True if CPDAG, otherwise False

extend_pdag

extend_pdag(pdag: PDAG) -> DAG

Generates a DAG which extends a PDAG (i.e. is a member of the equivalence class the PDAG represents)

Uses the algorithm in "A simple algorithm to construct a consistent extension of a partially oriented graph", Dor and Tarsi, 1992

:param PDAG pdag: PDAG from which DAG derived

:raises TypeError: if pdag is not of type PDAG :raises ValueError: if pdag is not extendable (example is an undirected square PDAG)

:returns DAG: extension of pdag

dict_to_adjmat

dict_to_adjmat(columns: Optional[Dict[str, List[int]]] = None) -> DataFrame

Create an adjacency matrix with specified entries.

:param dict columns: data for matrix specified by column

:raises TypeError: if arg types incorrect :raises ValueError: if values specified are invalid

:returns DataFrame: the adjacency matrix