cai-causal-graph 0.5.4.dev0__tar.gz → 0.5.4.dev2__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.dev0 → cai_causal_graph-0.5.4.dev2}/PKG-INFO +1 -1
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/__init__.py +1 -1
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/causal_graph.py +72 -32
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/time_series_causal_graph.py +10 -1
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/pyproject.toml +1 -1
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/LICENSE +0 -0
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/README.md +0 -0
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/exceptions.py +0 -0
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/graph_components.py +0 -0
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/identify_utils.py +0 -0
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/interfaces.py +0 -0
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/metadata_handler.py +0 -0
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/type_definitions.py +0 -0
- {cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/utils.py +0 -0
{cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/causal_graph.py
RENAMED
|
@@ -233,6 +233,7 @@ class Skeleton(CanDictSerialize, CanDictDeserialize):
|
|
|
233
233
|
adjacency: numpy.ndarray,
|
|
234
234
|
node_names: Optional[List[Union[NodeLike, int]]] = None,
|
|
235
235
|
graph_class: Union[Type[CausalGraph], None] = None,
|
|
236
|
+
validate: bool = True,
|
|
236
237
|
) -> Skeleton:
|
|
237
238
|
"""
|
|
238
239
|
Instantiate a `cai_causal_graph.causal_graph.Skeleton` object from an adjacency matrix.
|
|
@@ -243,6 +244,7 @@ class Skeleton(CanDictSerialize, CanDictDeserialize):
|
|
|
243
244
|
:param graph_class: The appropriate subclass of `cai_causal_graph.causal_graph.CausalGraph` to use. If `None`,
|
|
244
245
|
which is the default, `cai_causal_graph.causal_graph.CausalGraph` will be used. This is to ensure the new
|
|
245
246
|
`cai_causal_graph.causal_graph.Skeleton` object has the correct node class type.
|
|
247
|
+
:param validate: Whether to perform validation against cycles. Default is `True`.
|
|
246
248
|
:return: A new `cai_causal_graph.causal_graph.Skeleton` based on the provided adjacency matrix.
|
|
247
249
|
"""
|
|
248
250
|
if graph_class is not None:
|
|
@@ -251,7 +253,9 @@ class Skeleton(CanDictSerialize, CanDictDeserialize):
|
|
|
251
253
|
), f'The provided graph_class is not a subclass of CausalGraph. Got type {type(graph_class)}.'
|
|
252
254
|
else:
|
|
253
255
|
graph_class = CausalGraph
|
|
254
|
-
graph: CausalGraph = graph_class.from_adjacency_matrix(
|
|
256
|
+
graph: CausalGraph = graph_class.from_adjacency_matrix(
|
|
257
|
+
adjacency=adjacency, node_names=node_names, validate=validate
|
|
258
|
+
)
|
|
255
259
|
return cls(graph=graph)
|
|
256
260
|
|
|
257
261
|
def to_dict(self, include_meta: bool = True) -> dict:
|
|
@@ -325,7 +329,9 @@ class Skeleton(CanDictSerialize, CanDictDeserialize):
|
|
|
325
329
|
return cls(graph=graph_class.from_dict(d))
|
|
326
330
|
|
|
327
331
|
@classmethod
|
|
328
|
-
def from_networkx(
|
|
332
|
+
def from_networkx(
|
|
333
|
+
cls, g: networkx.Graph, graph_class: Union[Type[CausalGraph], None] = None, validate: bool = True
|
|
334
|
+
) -> Skeleton:
|
|
329
335
|
"""
|
|
330
336
|
Instantiate a `cai_causal_graph.causal_graph.Skeleton` object from a `networkx.Graph`.
|
|
331
337
|
|
|
@@ -333,6 +339,7 @@ class Skeleton(CanDictSerialize, CanDictDeserialize):
|
|
|
333
339
|
:param graph_class: The appropriate subclass of `cai_causal_graph.causal_graph.CausalGraph` to use. If `None`,
|
|
334
340
|
which is the default, `cai_causal_graph.causal_graph.CausalGraph` will be used. This is to ensure the new
|
|
335
341
|
`cai_causal_graph.causal_graph.Skeleton` object has the correct node class type.
|
|
342
|
+
:param validate: Whether to perform validation against cycles. Default is `True`.
|
|
336
343
|
:return: A new `cai_causal_graph.causal_graph.Skeleton` based on the `networkx.Graph`.
|
|
337
344
|
"""
|
|
338
345
|
if graph_class is not None:
|
|
@@ -345,10 +352,12 @@ class Skeleton(CanDictSerialize, CanDictDeserialize):
|
|
|
345
352
|
node_names: List[str] = [
|
|
346
353
|
graph_class._NodeCls.identifier_from(graph_class.coerce_to_nodelike(node)) for node in g.nodes()
|
|
347
354
|
]
|
|
348
|
-
return cls.from_adjacency_matrix(networkx.to_numpy_array(g), node_names, graph_class) # type: ignore
|
|
355
|
+
return cls.from_adjacency_matrix(networkx.to_numpy_array(g), node_names, graph_class, validate=validate) # type: ignore
|
|
349
356
|
|
|
350
357
|
@classmethod
|
|
351
|
-
def from_gml_string(
|
|
358
|
+
def from_gml_string(
|
|
359
|
+
cls, gml: str, graph_class: Union[Type[CausalGraph], None] = None, validate: bool = True
|
|
360
|
+
) -> Skeleton:
|
|
352
361
|
"""
|
|
353
362
|
Instantiate a `cai_causal_graph.causal_graph.Skeleton` object from a Graph Modelling Language (GML) string.
|
|
354
363
|
|
|
@@ -356,10 +365,11 @@ class Skeleton(CanDictSerialize, CanDictDeserialize):
|
|
|
356
365
|
:param graph_class: The appropriate subclass of `cai_causal_graph.causal_graph.CausalGraph` to use. If `None`,
|
|
357
366
|
which is the default, `cai_causal_graph.causal_graph.CausalGraph` will be used. This is to ensure the new
|
|
358
367
|
`cai_causal_graph.causal_graph.Skeleton` object has the correct node class type.
|
|
368
|
+
:param validate: Whether to perform validation against cycles. Default is `True`.
|
|
359
369
|
:return: A new `cai_causal_graph.causal_graph.Skeleton` based on the provided GML string.
|
|
360
370
|
"""
|
|
361
371
|
g = networkx.parse_gml(gml)
|
|
362
|
-
return cls.from_networkx(g, graph_class)
|
|
372
|
+
return cls.from_networkx(g=g, graph_class=graph_class, validate=validate)
|
|
363
373
|
|
|
364
374
|
def copy(self) -> Skeleton:
|
|
365
375
|
"""Copy a `cai_causal_graph.causal_graph.Skeleton` instance."""
|
|
@@ -1569,6 +1579,24 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
|
|
|
1569
1579
|
|
|
1570
1580
|
return parents_graph
|
|
1571
1581
|
|
|
1582
|
+
def _get_subgraph(self, nodes: List[NodeLike]) -> CausalGraph:
|
|
1583
|
+
"""
|
|
1584
|
+
Get a sub-graph that only contains variables in `nodes` and edges between them.
|
|
1585
|
+
"""
|
|
1586
|
+
node_list: List[str] = [self._NodeCls.identifier_from(node) for node in nodes]
|
|
1587
|
+
filtered_edges = [
|
|
1588
|
+
edge
|
|
1589
|
+
for edge in self.edges
|
|
1590
|
+
if edge.source.identifier in node_list and edge.destination.identifier in node_list
|
|
1591
|
+
]
|
|
1592
|
+
filtered_graph = self.__class__(meta=deepcopy(self.meta))
|
|
1593
|
+
if len(filtered_edges) == 0:
|
|
1594
|
+
for node in nodes:
|
|
1595
|
+
filtered_graph.add_node(node=self.get_node(node))
|
|
1596
|
+
for edge in filtered_edges:
|
|
1597
|
+
filtered_graph.add_edge(edge=edge, validate=False)
|
|
1598
|
+
return filtered_graph
|
|
1599
|
+
|
|
1572
1600
|
def get_ancestors(self, node: NodeLike) -> Set[str]:
|
|
1573
1601
|
"""
|
|
1574
1602
|
Get all ancestors of a node.
|
|
@@ -1588,15 +1616,9 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
|
|
|
1588
1616
|
Get a sub causal graph that only includes the ancestors of a specific node and the node itself.
|
|
1589
1617
|
"""
|
|
1590
1618
|
identifier = self._NodeCls.identifier_from(node)
|
|
1591
|
-
ancestors: List[
|
|
1592
|
-
|
|
1593
|
-
ancestral_graph = self.copy()
|
|
1594
|
-
|
|
1595
|
-
for i in ancestral_graph.nodes:
|
|
1596
|
-
if i.identifier not in ancestors:
|
|
1597
|
-
ancestral_graph.delete_node(i.identifier)
|
|
1619
|
+
ancestors: List[NodeLike] = [*self.get_ancestors(identifier), identifier]
|
|
1598
1620
|
|
|
1599
|
-
return
|
|
1621
|
+
return self._get_subgraph(ancestors)
|
|
1600
1622
|
|
|
1601
1623
|
def get_descendants(self, node: NodeLike) -> Set[str]:
|
|
1602
1624
|
"""
|
|
@@ -1617,15 +1639,9 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
|
|
|
1617
1639
|
Get a sub causal graph that only includes the descendants of a specific node and the node itself.
|
|
1618
1640
|
"""
|
|
1619
1641
|
identifier = self._NodeCls.identifier_from(node)
|
|
1620
|
-
descendants: List[
|
|
1642
|
+
descendants: List[NodeLike] = [*self.get_descendants(node), identifier]
|
|
1621
1643
|
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
for i in descendant_graph.nodes:
|
|
1625
|
-
if i.identifier not in descendants:
|
|
1626
|
-
descendant_graph.delete_node(i.identifier)
|
|
1627
|
-
|
|
1628
|
-
return descendant_graph
|
|
1644
|
+
return self._get_subgraph(nodes=descendants)
|
|
1629
1645
|
|
|
1630
1646
|
def is_ancestor(
|
|
1631
1647
|
self, ancestor_node: NodeLike, descendant_node: Union[NodeLike, Set[NodeLike], List[NodeLike]]
|
|
@@ -2072,8 +2088,14 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
|
|
|
2072
2088
|
return graph
|
|
2073
2089
|
|
|
2074
2090
|
@classmethod
|
|
2075
|
-
def from_networkx(cls, g: networkx.Graph) -> CausalGraph:
|
|
2076
|
-
"""
|
|
2091
|
+
def from_networkx(cls, g: networkx.Graph, validate: bool = True) -> CausalGraph:
|
|
2092
|
+
"""
|
|
2093
|
+
Construct a `cai_causal_graph.causal_graph.CausalGraph` instance from a `networkx.Graph` instance.
|
|
2094
|
+
|
|
2095
|
+
:param g: A `networkx.Graph` object to build a graph from.
|
|
2096
|
+
:param validate: Whether to perform validation against cycles. Default is `True`.
|
|
2097
|
+
:return: A `cai_causal_graph.causal_graph.CausalGraph` object.
|
|
2098
|
+
"""
|
|
2077
2099
|
# Check graph type.
|
|
2078
2100
|
if isinstance(g, networkx.MultiGraph) or isinstance(g, networkx.MultiDiGraph):
|
|
2079
2101
|
raise CausalGraphErrors.InvalidNetworkXError(
|
|
@@ -2082,29 +2104,37 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
|
|
|
2082
2104
|
)
|
|
2083
2105
|
# Convert node names to strings.
|
|
2084
2106
|
node_names: List[str] = [cls._NodeCls.identifier_from(cls.coerce_to_nodelike(node)) for node in g.nodes()]
|
|
2085
|
-
return cls.from_adjacency_matrix(networkx.to_numpy_array(g), node_names) # type: ignore
|
|
2107
|
+
return cls.from_adjacency_matrix(networkx.to_numpy_array(g), node_names, validate=validate) # type: ignore
|
|
2086
2108
|
|
|
2087
2109
|
@classmethod
|
|
2088
|
-
def from_skeleton(cls, skeleton: Skeleton) -> CausalGraph:
|
|
2110
|
+
def from_skeleton(cls, skeleton: Skeleton, validate: bool = True) -> CausalGraph:
|
|
2089
2111
|
"""
|
|
2090
2112
|
Construct a `cai_causal_graph.causal_graph.CausalGraph` instance from a
|
|
2091
2113
|
`cai_causal_graph.causal_graph.Skeleton` instance.
|
|
2114
|
+
|
|
2115
|
+
:param skeleton: A `cai_causal_graph.causal_graph.Skeleton` object to build a graph from.
|
|
2116
|
+
:param validate: Whether to perform validation against cycles. Default is `True`.
|
|
2117
|
+
:return: A `cai_causal_graph.causal_graph.CausalGraph` object.
|
|
2092
2118
|
"""
|
|
2093
2119
|
assert isinstance(skeleton, Skeleton), f'Expected skeleton to be of type Skeleton, but got {type(skeleton)}.'
|
|
2094
|
-
return cls.from_networkx(skeleton.to_networkx())
|
|
2120
|
+
return cls.from_networkx(skeleton.to_networkx(), validate=validate)
|
|
2095
2121
|
|
|
2096
2122
|
@classmethod
|
|
2097
|
-
def from_gml_string(cls, gml: str) -> CausalGraph:
|
|
2123
|
+
def from_gml_string(cls, gml: str, validate: bool = True) -> CausalGraph:
|
|
2098
2124
|
"""
|
|
2099
2125
|
Return an instance of `cai_causal_graph.causal_graph.CausalGraph` constructed from the provided Graph Modelling
|
|
2100
2126
|
Language (GML) string.
|
|
2127
|
+
|
|
2128
|
+
:param gml: A GML string to build a graph from.
|
|
2129
|
+
:param validate: Whether to perform validation against cycles. Default is `True`.
|
|
2130
|
+
:return: A `cai_causal_graph.causal_graph.CausalGraph` object.
|
|
2101
2131
|
"""
|
|
2102
2132
|
g = networkx.parse_gml(gml)
|
|
2103
|
-
return cls.from_networkx(g)
|
|
2133
|
+
return cls.from_networkx(g, validate=validate)
|
|
2104
2134
|
|
|
2105
2135
|
@classmethod
|
|
2106
2136
|
def from_adjacency_matrix(
|
|
2107
|
-
cls, adjacency: numpy.ndarray, node_names: Optional[List[Union[NodeLike, int]]] = None
|
|
2137
|
+
cls, adjacency: numpy.ndarray, node_names: Optional[List[Union[NodeLike, int]]] = None, validate: bool = True
|
|
2108
2138
|
) -> CausalGraph:
|
|
2109
2139
|
"""
|
|
2110
2140
|
Construct a `cai_causal_graph.causal_graph.CausalGraph` instance from an adjacency matrix and optionally a list
|
|
@@ -2116,6 +2146,7 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
|
|
|
2116
2146
|
:param adjacency: A square binary numpy adjacency array.
|
|
2117
2147
|
:param node_names: A list of strings, `cai_causal_graph.interfaces.HasIdentifier`, and/or integers which can be
|
|
2118
2148
|
coerced to `cai_causal_graph.graph_components.Node`.
|
|
2149
|
+
:param validate: Whether to perform validation against cycles. Default is `True`.
|
|
2119
2150
|
:return: A `cai_causal_graph.causal_graph.CausalGraph` object.
|
|
2120
2151
|
"""
|
|
2121
2152
|
# check that adjacency matrix is a square matrix
|
|
@@ -2148,12 +2179,21 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
|
|
|
2148
2179
|
graph = cls()
|
|
2149
2180
|
graph.add_nodes_from(nodes)
|
|
2150
2181
|
for i, j in itertools.combinations(range(len(nodes)), 2):
|
|
2182
|
+
# Keep validate as False to not slow construction. We will look at validate param after graph is constructed.
|
|
2151
2183
|
if adjacency[i, j] != 0 and adjacency[j, i] == 0:
|
|
2152
|
-
graph.add_edge(nodes[i], nodes[j], edge_type=EdgeType.DIRECTED_EDGE)
|
|
2184
|
+
graph.add_edge(nodes[i], nodes[j], edge_type=EdgeType.DIRECTED_EDGE, validate=False)
|
|
2153
2185
|
elif adjacency[i, j] == 0 and adjacency[j, i] != 0:
|
|
2154
|
-
graph.add_edge(nodes[j], nodes[i], edge_type=EdgeType.DIRECTED_EDGE)
|
|
2186
|
+
graph.add_edge(nodes[j], nodes[i], edge_type=EdgeType.DIRECTED_EDGE, validate=False)
|
|
2155
2187
|
elif adjacency[i, j] != 0 and adjacency[j, i] != 0:
|
|
2156
|
-
graph.add_edge(nodes[i], nodes[j], edge_type=EdgeType.UNDIRECTED_EDGE)
|
|
2188
|
+
graph.add_edge(nodes[i], nodes[j], edge_type=EdgeType.UNDIRECTED_EDGE, validate=False)
|
|
2189
|
+
|
|
2190
|
+
# check that there are no cycles of directed edges
|
|
2191
|
+
if validate:
|
|
2192
|
+
try:
|
|
2193
|
+
for node in nodes:
|
|
2194
|
+
graph._assert_node_does_not_depend_on_itself(node)
|
|
2195
|
+
except AssertionError:
|
|
2196
|
+
raise CausalGraphErrors.CyclicConnectionError(f'Nodes {node} is on a cyclic path.')
|
|
2157
2197
|
|
|
2158
2198
|
return graph
|
|
2159
2199
|
|
|
@@ -391,6 +391,13 @@ class TimeSeriesCausalGraph(CausalGraph):
|
|
|
391
391
|
edge = self._EdgeCls(source=source, destination=destination, edge_type=edge.edge_type, meta=edge.meta)
|
|
392
392
|
summary_graph.add_edge(edge=edge)
|
|
393
393
|
|
|
394
|
+
# There could be floating nodes in the original graph that should be maintained in the summary graph.
|
|
395
|
+
# For example, X[t], Y[t-1]->Z[t], the summary graph should be X, Y->Z.
|
|
396
|
+
summary_var_names = summary_graph.get_node_names()
|
|
397
|
+
for var_name in self.get_all_variable_names():
|
|
398
|
+
if var_name not in summary_var_names:
|
|
399
|
+
summary_graph.add_node(var_name)
|
|
400
|
+
|
|
394
401
|
return summary_graph
|
|
395
402
|
|
|
396
403
|
def extend_graph(
|
|
@@ -833,6 +840,7 @@ class TimeSeriesCausalGraph(CausalGraph):
|
|
|
833
840
|
adjacency_matrices: Dict[int, numpy.ndarray],
|
|
834
841
|
variable_names: Optional[List[Union[NodeLike, int]]] = None,
|
|
835
842
|
construct_minimal: bool = True,
|
|
843
|
+
validate: bool = True,
|
|
836
844
|
) -> TimeSeriesCausalGraph:
|
|
837
845
|
"""
|
|
838
846
|
Instantiate a `cai_causal_graph.time_series_causal_graph.TimeSeriesCausalGraph` from a dictionary of
|
|
@@ -875,6 +883,7 @@ class TimeSeriesCausalGraph(CausalGraph):
|
|
|
875
883
|
:param variable_names: A list of variable names. If not provided, the variable names are integers starting
|
|
876
884
|
from 0. Node names must correspond to the variable names and must not contain the lag.
|
|
877
885
|
:param construct_minimal: Whether to return a minimal time series graph. Default is `True`.
|
|
886
|
+
:param validate: Whether to perform validation against cycles. Default is `True`.
|
|
878
887
|
:return: A time series causal graph.
|
|
879
888
|
"""
|
|
880
889
|
assert isinstance(adjacency_matrices, dict)
|
|
@@ -937,7 +946,7 @@ class TimeSeriesCausalGraph(CausalGraph):
|
|
|
937
946
|
time_delta_to_index[0] + (n_time_delta * column),
|
|
938
947
|
] = 1
|
|
939
948
|
|
|
940
|
-
graph = cls.from_adjacency_matrix(adjacency_matrix_full, node_names) # type: ignore
|
|
949
|
+
graph = cls.from_adjacency_matrix(adjacency_matrix_full, node_names, validate=validate) # type: ignore
|
|
941
950
|
|
|
942
951
|
if construct_minimal:
|
|
943
952
|
graph = graph.get_minimal_graph()
|
|
@@ -42,7 +42,7 @@ cache_dir = '/dev/null'
|
|
|
42
42
|
|
|
43
43
|
[tool.poetry]
|
|
44
44
|
name = "cai-causal-graph"
|
|
45
|
-
version = "0.5.4.
|
|
45
|
+
version = "0.5.4.dev2"
|
|
46
46
|
description = "A Causal AI package for causal graphs."
|
|
47
47
|
license = "Apache-2.0"
|
|
48
48
|
authors = ["causaLens <opensource@causalens.com>"]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/graph_components.py
RENAMED
|
File without changes
|
{cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/identify_utils.py
RENAMED
|
File without changes
|
|
File without changes
|
{cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/metadata_handler.py
RENAMED
|
File without changes
|
{cai_causal_graph-0.5.4.dev0 → cai_causal_graph-0.5.4.dev2}/cai_causal_graph/type_definitions.py
RENAMED
|
File without changes
|
|
File without changes
|