Skip to content

Commit 64c0341

Browse files
committed
simplify community graph construction
1 parent 3024ee3 commit 64c0341

File tree

1 file changed

+20
-39
lines changed

1 file changed

+20
-39
lines changed

dadapy/causal_graph.py

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -786,51 +786,32 @@ def community_graph_visualization(
786786
communities_orders[order_idx].append(
787787
community_dictionary[community_idx, order_idx]
788788
)
789+
789790
# draw edges
790791
for community_effect_idx, order_idx in keys:
791792
if order_idx > 0:
792793
# for each putative effect community at order >=1...
793794
for community_effect in communities_orders[order_idx]:
794795
community_name_effect = community_names[tuple(community_effect)]
795-
# ...loop over all putative causal communities at order -1, -2, ...
796-
for previous_order in range(1, order_idx + 1):
797-
for community_cause in communities_orders[
798-
order_idx - previous_order
799-
]:
800-
community_name_cause = community_names[
801-
tuple(community_cause)
802-
]
803-
effect_ancestors_names = set(
804-
nx.ancestors(G, community_name_effect)
805-
)
806-
effect_ancestors_set = [
807-
from_names_to_communities[effect_ancestor_name]
808-
for effect_ancestor_name in effect_ancestors_names
809-
]
810-
effect_ancestors_set = set().union(
811-
*effect_ancestors_set
812-
)
813-
community_cause_not_already_ancestor = set(
814-
community_cause
815-
).difference(effect_ancestors_set)
816-
# ...loop over all variables in each putative causal community,
817-
# avoiding communities that are already ancestors...
818-
for (
819-
variable_cause
820-
) in community_cause_not_already_ancestor:
821-
# ...and draw an edge if at least a link is found
822-
if adj_matrix[
823-
variable_cause, community_effect
824-
].any():
825-
G.add_edges_from(
826-
[
827-
(
828-
str(community_name_cause),
829-
str(community_name_effect),
830-
)
831-
]
832-
)
833-
break
796+
# ...loop over all putative causal communities at order -1
797+
previous_order = order_idx - 1
798+
for community_cause in communities_orders[previous_order]:
799+
community_name_cause = community_names[
800+
tuple(community_cause)
801+
]
802+
# ...loop over all variables in each putative causal community
803+
for variable_cause in community_cause:
804+
# ...and draw an edge if at least a link is found
805+
if adj_matrix[variable_cause, community_effect].any():
806+
G.add_edges_from(
807+
[
808+
(
809+
str(community_name_cause),
810+
str(community_name_effect),
811+
)
812+
]
813+
)
814+
break
834815
# show graph
835816
options = {
836817
"node_color": "gray",

0 commit comments

Comments
 (0)