Skip to content

edge_replace

Signature/Parameters

def edge_replace(self, remove, add)

Replace an existing edge with a new one in a single operation.

Parameters:

Name Type Description Default
remove tuple[str, str] or tuple[tuple[str, str], tuple[str, str]] or set[str]

Edge specification to be removed. Formats follow the accepted edge types for the graph and support undirected and bidirected symmetry.

required
add tuple[str, str] or tuple[tuple[str, str], tuple[str, str]] or set[str]

Edge specification to be added after removal.

required

Returns:

Type Description
DAG

A DAG instance reflecting the requested change. If the removal fails because the edge does not exist, the method still returns the result of attempting to add the new edge.

Examples:

>>> G = DAG(graph="X -> Y")
>>> G = G.edge_replace(("X", "Y"), ("X", "Z"))
>>> ("X", "Y") in G.directed, ("X", "Z") in G.directed
(False, True)
Source code in causalinf/gcm.py
def edge_replace(self, remove, add):
    """
    Replace an existing edge with a new one in a single operation.

    Parameters
    ----------
    remove : tuple[str, str] or tuple[tuple[str, str], tuple[str, str]] or set[str]
        Edge specification to be removed. Formats follow the accepted edge
        types for the graph and support undirected and bidirected symmetry.

    add : tuple[str, str] or tuple[tuple[str, str], tuple[str, str]] or set[str]
        Edge specification to be added after removal.

    Returns
    -------
    DAG
        A `DAG` instance reflecting the requested change. If the removal
        fails because the edge does not exist, the method still returns the
        result of attempting to add the new edge.

    Examples
    --------
    >>> G = DAG(graph="X -> Y")
    >>> G = G.edge_replace(("X", "Y"), ("X", "Z"))
    >>> ("X", "Y") in G.directed, ("X", "Z") in G.directed
    (False, True)
    """
    res = self.edge_remove(remove)
    res = res.edge_add(add)
    return res