cai-causal-graph 0.4.7.dev0__tar.gz → 0.4.8.dev1__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cai-causal-graph
3
- Version: 0.4.7.dev0
3
+ Version: 0.4.8.dev1
4
4
  Summary: A Causal AI package for causal graphs.
5
5
  Home-page: https://causalgraph.causalens.com/
6
6
  License: Apache-2.0
@@ -15,7 +15,8 @@ Classifier: Programming Language :: Python :: 3.10
15
15
  Classifier: Programming Language :: Python :: 3.11
16
16
  Classifier: Programming Language :: Python :: 3.12
17
17
  Requires-Dist: mypy-extensions (>=1.0.0,<2.0.0)
18
- Requires-Dist: networkx (>=3.0.0,<4.0.0)
18
+ Requires-Dist: networkx (>=3.0.0,<3.2.0) ; python_full_version >= "3.8.0" and python_full_version < "3.9.0"
19
+ Requires-Dist: networkx (>=3.0.0,<3.3.0) ; python_full_version >= "3.9.0" and python_full_version < "3.13.0"
19
20
  Requires-Dist: numpy (>=1.20.0,<1.25.0) ; python_full_version >= "3.8.0" and python_full_version < "3.9.0"
20
21
  Requires-Dist: numpy (>=1.20.0,<2.0.0) ; python_full_version >= "3.9.0" and python_full_version < "3.13.0"
21
22
  Requires-Dist: pandas (>=1.0.0,<3.0.0)
@@ -31,7 +31,7 @@ __all__ = [
31
31
  'VARIABLE_NAME',
32
32
  ]
33
33
 
34
- __version__ = '0.4.7.dev0'
34
+ __version__ = '0.4.8.dev1'
35
35
 
36
36
  from cai_causal_graph.causal_graph import CausalGraph, Skeleton
37
37
  from cai_causal_graph.time_series_causal_graph import TimeSeriesCausalGraph
@@ -1797,6 +1797,12 @@ class CausalGraph(HasIdentifier, HasMetadata, CanDictSerialize, CanDictDeseriali
1797
1797
 
1798
1798
  assert all(node in self.get_node_names() for node in [source, destination])
1799
1799
 
1800
+ # In networkx 3.3 this will return a path of length 1 with just that node in it. We don't want to consider this
1801
+ # particularly as there is no self loop from that node to itself. One could make an edge from source to source
1802
+ # but then it would no longer be a DAG as we confirm in the first step of this method.
1803
+ if source == destination:
1804
+ return []
1805
+
1800
1806
  return list(networkx.all_simple_paths(self.to_networkx(), source, destination))
1801
1807
 
1802
1808
  def directed_path_exists(self, source: NodeLike, destination: NodeLike) -> bool:
@@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  """
16
+ from itertools import combinations
16
17
  from typing import List, Optional, Set, Tuple, Union
17
18
 
18
19
  import networkx
@@ -20,7 +21,7 @@ import networkx
20
21
  from cai_causal_graph import CausalGraph, Skeleton
21
22
  from cai_causal_graph.exceptions import CausalGraphErrors
22
23
  from cai_causal_graph.graph_components import Node
23
- from cai_causal_graph.type_definitions import NodeLike
24
+ from cai_causal_graph.type_definitions import EdgeType, NodeLike
24
25
 
25
26
 
26
27
  def _verify_identify_inputs(
@@ -387,3 +388,54 @@ def identify_markov_boundary(graph: Union[CausalGraph, Skeleton], node: NodeLike
387
388
  mb = graph.get_neighbors(node_id)
388
389
 
389
390
  return mb
391
+
392
+
393
+ def identify_colliders(graph: CausalGraph, unshielded_only: bool = False) -> List[str]:
394
+ """
395
+ Identify all the collider nodes in the provided `graph`.
396
+
397
+ :param graph: The graph given by a `cai_causal_graph.causal_graph.CausalGraph` instance.
398
+ :param unshielded_only: If `True`, only unshielded colliders are returned. If `False`, all colliders are returned.
399
+ Default is `False`. A collider is unshielded if there is no edge between any of its parents.
400
+ :return: A list of all node identifiers for the collider nodes in the graph.
401
+ """
402
+ colliders = set()
403
+
404
+ bidirected_edges = [(edge.source.identifier, edge.destination.identifier) for edge in graph.get_bidirected_edges()]
405
+
406
+ # go through every node in the graph
407
+ for node in graph.get_node_names():
408
+ # the node z is collider if x <> z <> y, x -> z <- y, x <> z <- y, or x -> z <> y
409
+ neighbors = graph.get_neighbors(node)
410
+
411
+ potential_parents = set()
412
+
413
+ for neighbor_node in neighbors:
414
+ # we need to check if the directed edge (neigh_node, node) exists
415
+ directed_exists = (
416
+ graph.edge_exists(neighbor_node, node)
417
+ and graph.get_edge(neighbor_node, node).edge_type == EdgeType.DIRECTED_EDGE
418
+ )
419
+ # we need to check if the bi-directed edge (neigh_node, node) or (node, neigh_node) exists
420
+ bidirected_exists = (neighbor_node, node) in bidirected_edges or (node, neighbor_node) in bidirected_edges
421
+
422
+ condition = directed_exists or bidirected_exists
423
+
424
+ if condition:
425
+ potential_parents.add(neighbor_node)
426
+
427
+ if len(potential_parents) >= 2:
428
+ # check if the collider is unshielded. A collider is unshielded if there is no edge between any of its
429
+ # parents
430
+ is_unshielded = True
431
+ if unshielded_only:
432
+ # test all the possible combinations of the potential parents
433
+ for parent_1, parent_2 in combinations(potential_parents, 2):
434
+ if graph.edge_exists(parent_1, parent_2) or graph.edge_exists(parent_2, parent_1):
435
+ is_unshielded = False
436
+ break
437
+
438
+ if not unshielded_only or is_unshielded:
439
+ colliders.add(node)
440
+
441
+ return list(colliders)
@@ -42,7 +42,7 @@ cache_dir = '/dev/null'
42
42
 
43
43
  [tool.poetry]
44
44
  name = "cai-causal-graph"
45
- version = "0.4.7.dev0"
45
+ version = "0.4.8.dev1"
46
46
  description = "A Causal AI package for causal graphs."
47
47
  license = "Apache-2.0"
48
48
  authors = ["causaLens <opensource@causalens.com>"]
@@ -54,7 +54,12 @@ packages = [{include = "cai_causal_graph"}]
54
54
 
55
55
  [tool.poetry.dependencies]
56
56
  mypy-extensions = "^1.0.0"
57
- networkx = ">=3.0.0, <4.0.0"
57
+ # networkx 3.3 broke how get_all_simple_paths works so want to bound for now.
58
+ # networkx 3.1 is the last one to support 3.8. We will drop 3.8 on next minor.
59
+ networkx = [
60
+ {version = ">=3.0.0, <3.3.0", python = ">=3.9.0, <3.13.0"},
61
+ {version = ">=3.0.0, <3.2.0", python = ">=3.8.0, <3.9.0"} # 3.1 is the last version to support 3.8.
62
+ ]
58
63
  # Newest numpy is >= 3.9 so need to have different deps while we still support 3.8 but will drop 3.8 on next minor.
59
64
  # 1.26 is the first version to support 3.12, but want to allow other versions to support earlier numpy.
60
65
  numpy = [