cai-causal-graph 0.5.4.dev2__tar.gz → 0.5.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/PKG-INFO +1 -1
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/__init__.py +1 -1
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/causal_graph.py +70 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/exceptions.py +3 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/pyproject.toml +1 -1
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/LICENSE +0 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/README.md +0 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/graph_components.py +0 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/identify_utils.py +0 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/interfaces.py +0 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/metadata_handler.py +0 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/time_series_causal_graph.py +0 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/type_definitions.py +0 -0
- {cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/utils.py +0 -0
|
@@ -741,6 +741,10 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
|
|
|
741
741
|
edge.destination
|
|
742
742
|
)
|
|
743
743
|
|
|
744
|
+
# raise an error if reverse edge from destination to source has already been defined
|
|
745
|
+
if self._edges_by_source[destination].get(source) is not None:
|
|
746
|
+
raise CausalGraphErrors.ReverseEdgeExistsError()
|
|
747
|
+
|
|
744
748
|
self._edges_by_source[source][destination] = edge
|
|
745
749
|
self._edges_by_destination[destination][source] = edge
|
|
746
750
|
|
|
@@ -1722,6 +1726,72 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
|
|
|
1722
1726
|
|
|
1723
1727
|
return self.get_descendants(node_1).intersection(self.get_descendants(node_2))
|
|
1724
1728
|
|
|
1729
|
+
def get_nodes_between(self, node_1: NodeLike, node_2: NodeLike) -> Set[Node]:
|
|
1730
|
+
"""
|
|
1731
|
+
Get the set of nodes that are on directed causal paths between two nodes.
|
|
1732
|
+
|
|
1733
|
+
The nodes themselves will be included in the final set.
|
|
1734
|
+
|
|
1735
|
+
If there are no causal paths between the nodes then an empty set will be returned.
|
|
1736
|
+
|
|
1737
|
+
This method will be faster and significantly more memory efficient than
|
|
1738
|
+
`cai_causal_graph.causal_graph.CausalGraph.get_all_causal_paths` for large graphs, but only returns the set of
|
|
1739
|
+
nodes of the causal paths, rather than each individual causal path. The worst case for this method is
|
|
1740
|
+
`O(n + e)` time complexity and `O(n)` memory complexity, where `n` and `e` are the number of nodes and
|
|
1741
|
+
edges in the graph respectively.
|
|
1742
|
+
|
|
1743
|
+
:param node_1: a single node-identifier coercible object, representing the source node.
|
|
1744
|
+
:param node_2: a single node-identifier coercible object, representing the destination node.
|
|
1745
|
+
:return: a set of nodes that are on causal paths between the two provided nodes.
|
|
1746
|
+
"""
|
|
1747
|
+
assert self.is_dag(), 'This method only works for DAGs but the current graph is not a DAG.'
|
|
1748
|
+
|
|
1749
|
+
# Set up cache: for each seen node, cache whether there is a causal path between it and the destination
|
|
1750
|
+
seen_nodes: dict[str, bool] = {}
|
|
1751
|
+
|
|
1752
|
+
start = self.get_node(node_1)
|
|
1753
|
+
end = self.get_node(node_2)
|
|
1754
|
+
|
|
1755
|
+
def _has_causal_path_inner(start_node_: Node, end_node_: Node) -> bool:
|
|
1756
|
+
"""
|
|
1757
|
+
Check if there is a causal path from the start node to the end node.
|
|
1758
|
+
|
|
1759
|
+
This is done recursively by checking:
|
|
1760
|
+
1. if the start node is the end node, return True
|
|
1761
|
+
2. if the start node is a sink node, return False
|
|
1762
|
+
3. else recursively check if any of the children of that start have a causal path to the end node
|
|
1763
|
+
|
|
1764
|
+
The return value for each node is cached in the `seen_nodes` dictionary to avoid repeat calculations. This
|
|
1765
|
+
means that for a full graph this will run in `O(e)` time, where e is the number of edges in the graph.
|
|
1766
|
+
"""
|
|
1767
|
+
if start_node_.identifier in seen_nodes:
|
|
1768
|
+
return seen_nodes[start_node_.identifier]
|
|
1769
|
+
if start_node_ == end_node_:
|
|
1770
|
+
seen_nodes[start_node_.identifier] = True
|
|
1771
|
+
return True
|
|
1772
|
+
if start_node_.is_sink_node():
|
|
1773
|
+
seen_nodes[start_node_.identifier] = False
|
|
1774
|
+
return False
|
|
1775
|
+
|
|
1776
|
+
start_children = [e.destination for e in start_node_._outbound_edges]
|
|
1777
|
+
|
|
1778
|
+
# Run recursively for all children, which itself will cache the results of all descendant nodes.
|
|
1779
|
+
children_have_causal_paths = [_has_causal_path_inner(child, end_node_) for child in start_children]
|
|
1780
|
+
|
|
1781
|
+
has_causal_path = any(children_have_causal_paths)
|
|
1782
|
+
seen_nodes[start_node_.identifier] = has_causal_path
|
|
1783
|
+
return has_causal_path
|
|
1784
|
+
|
|
1785
|
+
# Check recursively which of the start node of its descendants have a causal path to the end node
|
|
1786
|
+
causal_path = _has_causal_path_inner(start, end)
|
|
1787
|
+
|
|
1788
|
+
# Can return an empty set if there is no causal path from the start node to the end node
|
|
1789
|
+
if not causal_path:
|
|
1790
|
+
return set()
|
|
1791
|
+
|
|
1792
|
+
# Otherwise return all nodes that have a causal path to the destination (including the source and destination)
|
|
1793
|
+
return {self.get_node(node) for node, has_path in seen_nodes.items() if has_path}
|
|
1794
|
+
|
|
1725
1795
|
def get_d_separation_set(self, node_1: NodeLike, node_2: NodeLike) -> Set[str]:
|
|
1726
1796
|
"""
|
|
1727
1797
|
Return a minimal d-separation set between two nodes.
|
|
@@ -45,6 +45,9 @@ class CausalGraphErrors:
|
|
|
45
45
|
class EdgeExistsError(Exception):
|
|
46
46
|
"""Error raised when edge already exists in the graph."""
|
|
47
47
|
|
|
48
|
+
class ReverseEdgeExistsError(Exception):
|
|
49
|
+
"""Error raised when the reverse edge already exists in the graph."""
|
|
50
|
+
|
|
48
51
|
class EdgeDoesNotExistError(Exception):
|
|
49
52
|
"""Error raised when edge does not exist in the graph."""
|
|
50
53
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{cai_causal_graph-0.5.4.dev2 → cai_causal_graph-0.5.6}/cai_causal_graph/time_series_causal_graph.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|