Using the networkx module, I do some network analysis in Python 3.2, where I need to project a bipartite graph (of prisoners associated with their cell: input graph B in the code below) onto a subgraph (linking cellmates to each other if both have overlapping spells in one and in the same cell: using the input of the given nodes defining the nodal nodes of graph B, generating the output graph G). I do not need a special algorithm that is suitable for any or optimal match, I just need to collect all the links that satisfy certain conditions. Thus, the other SO posts I found do not really apply. But:
My current code explodes (RAM-, swap- and CPU-wise) as I give it more and more data. Please let me know if you see ways to arrange the code below using 5 layers of loops. I'm not sure if knowledge of networkx is necessary or the details of my edge attribute labeling are relevant. Thanks!
def time_overlap_projected_graph_parallel(B, nodes): G=nx.MultiGraph() G.add_nodes_from((n,B.node[n]) for n in nodes) for u in nodes: unbrs = set(B[u]) nbrs2 = set((n for nbr in unbrs for n in B[nbr])) - set([u]) for v in nbrs2: for mutual_cell in set(B[u]) & set(B[v]): for uspell in B.get_edge_data(u,mutual_cell).values(): ustart = uspell[1] uend = uspell[2] for vspell in B.get_edge_data(v,mutual_cell).values(): vstart = vspell[1] vend = vspell[2] if uend > vstart and vend > ustart: ostart = max(ustart,vstart) oend = min(uend,vend) olen = (oend-ostart+1)/86400 ocell = mutual_cell if (v not in G[u] or ostart not in [ edict[1] for edict in G[u][v].values() ]): G.add_edges_from([(u,v,{0: olen,1: ostart,2: oend,3: ocell})]) return G
source share