@@ -193,3 +193,41 @@ def get_set_layering(H, collapse=True):
193193 levels [v ] = max (parent_levels ) + 1 if len (parent_levels ) else 0
194194
195195 return levels
196+
197+ def layout_with_radius (B , node_and_edge_radius = 1 , ** kwargs ):
198+ """
199+ Convenience function allowing the user to specify ideal radii for nodes and edges in the drawing
200+
201+ The Kamada-Kawai (networkx.kamada_kawai_layout) algorithm is used. The algorithm is passed
202+ a all pairs shorteset path matrix that is calculated from the bipartite graph input. The
203+ shortest path is determined
204+
205+
206+ Parameters
207+ ----------
208+ B: nx.Graph
209+ a bipartite graph representing the hypergraph
210+ node_and_edge_radius: float, int, dict, list, or function
211+ encoding of the radii of nodes in B, which are hyper-edges or hyper-nodes (0 by default)
212+ kwargs: dict
213+ Keyword arguments are passed through to the networkx.kamada_kawai_layout function
214+
215+ Returns
216+ -------
217+ dict
218+ mapping of node and edge positions to R^2
219+ """
220+
221+ # get radii encodings and convert to dictionary
222+ radius_dict = dict (zip (B , inflate (B , node_and_edge_radius )))
223+
224+ # edges weights are the sum of the radii of the edge endpoints
225+ for u , v , d in B .edges (data = True ):
226+ d ['weight' ] = radius_dict .get (u , 0 ) + radius_dict .get (v , 0 )
227+
228+ # compute all pairs shortest path (APSP)
229+ dist = dict (nx .all_pairs_dijkstra_path_length (B ))
230+
231+ # compute and return layout using above APSP; pass through arguments
232+ return nx .kamada_kawai_layout (B , dist = dist , ** kwargs )
233+
0 commit comments