[BUG] Layouts fail for directed graphs
See original GitHub issueExpected Behavior
Layouts should work with directed graphs (IMO) Perhaps not a bug in the sense that I’m not sure we promise this works for directed, but still. If we decided to only support undirected maybe a clearer error is warranted.
Actual Behavior
NetworkXNotImplemented: not implemented for directed type
Example Code
import networkx as nx
from graspologic.layouts import layout_umap
g = nx.erdos_renyi_graph(10, 0.7, directed=True)
layout_umap(g)
Full Traceback
---------------------------------------------------------------------------
NetworkXNotImplemented Traceback (most recent call last)
~/JHU_code/maggot/maggot_connectome/scripts/what_is_flow_rank.py in
3
4 g = nx.erdos_renyi_graph(10, 0.7, directed=True)
----> 5 layout_umap(g)
~/miniconda3/envs/maggot-revamp/lib/python3.8/site-packages/graspologic/layouts/auto.py in layout_umap(graph, min_dist, n_neighbors, max_edges, random_seed)
173 """
174
--> 175 lcc_graph, tensors, labels = _node2vec_for_layout(graph, max_edges, random_seed)
176 points = umap.UMAP(
177 min_dist=min_dist, n_neighbors=n_neighbors, random_state=random_seed
~/miniconda3/envs/maggot-revamp/lib/python3.8/site-packages/graspologic/layouts/auto.py in _node2vec_for_layout(graph, max_edges, random_seed)
214 ) -> Tuple[nx.Graph, np.ndarray, np.ndarray]:
215 graph = _approximate_prune(graph, max_edges)
--> 216 graph = _largest_connected_component(graph)
217
218 start = time.time()
~/miniconda3/envs/maggot-revamp/lib/python3.8/site-packages/graspologic/layouts/auto.py in _largest_connected_component(graph)
182
183 def _largest_connected_component(graph: nx.Graph) -> nx.Graph:
--> 184 largest_component = max(nx.connected_components(graph), key=len)
185 return graph.subgraph(largest_component).copy()
186
in connected_components(G)
~/miniconda3/envs/maggot-revamp/lib/python3.8/site-packages/networkx/utils/decorators.py in _not_implemented_for(not_implement_for_func, *args, **kwargs)
74 if match:
75 msg = f"not implemented for {' '.join(graph_types)} type"
---> 76 raise nx.NetworkXNotImplemented(msg)
77 else:
78 return not_implement_for_func(*args, **kwargs)
NetworkXNotImplemented: not implemented for directed type
Your Environment
- Python version: 3.8
- graspologic version: dev
Additional Details
183 def _largest_connected_component(graph: nx.Graph) -> nx.Graph:
--> 184 largest_component = max(nx.connected_components(graph), key=len)
Just from these 2 lines on the traceback, I suspect this just needs to be adapted to check for weakly connected components. Or, use graspologic.utils.is_fully_connected from our own codebase. However in #112 we are talking about replacing the networkx-based check with a faster one from scipy. Not sure if the layouts code will always assume networkx input going forward. AFAIK there is nothing that inherently needs to be undirected for the layouts process, or am I missing something?
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (3 by maintainers)

Top Related StackOverflow Question
FYI @kareef928 has been working on the conversion stuff so we’ll keep him in the loop too!
Would be interesting to see if the time-to-convert an nx graph into a csr matrix and run that lcc check is faster than doing it all in networkx. I’ll keep you guys posted after I crunch some numbers 😃