<s> """<STR_LIT>""" <EOL> import networkx as nx <EOL> __author__ = """<STR_LIT:\n>""" . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def projected_graph ( B , nodes , multigraph = False ) : <EOL> r"""<STR_LIT>""" <EOL> if B . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if B . is_directed ( ) : <EOL> directed = True <EOL> if multigraph : <EOL> G = nx . MultiDiGraph ( ) <EOL> else : <EOL> G = nx . DiGraph ( ) <EOL> else : <EOL> directed = False <EOL> if multigraph : <EOL> G = nx . MultiGraph ( ) <EOL> else : <EOL> G = nx . Graph ( ) <EOL> G . graph . update ( B . graph ) <EOL> G . add_nodes_from ( ( n , B . node [ n ] ) for n in nodes ) <EOL> for u in nodes : <EOL> nbrs2 = set ( ( v for nbr in B [ u ] for v in B [ nbr ] ) ) - set ( [ u ] ) <EOL> if multigraph : <EOL> for n in nbrs2 : <EOL> if directed : <EOL> links = set ( B [ u ] ) & set ( B . pred [ n ] ) <EOL> else : <EOL> links = set ( B [ u ] ) & set ( B [ n ] ) <EOL> for l in links : <EOL> if not G . has_edge ( u , n , l ) : <EOL> G . add_edge ( u , n , key = l ) <EOL> else : <EOL> G . add_edges_from ( ( u , n ) for n in nbrs2 ) <EOL> return G <EOL> def weighted_projected_graph ( B , nodes , ratio = False ) : <EOL> r"""<STR_LIT>""" <EOL> if B . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if B . is_directed ( ) : <EOL> pred = B . pred <EOL> G = nx . DiGraph ( ) <EOL> else : <EOL> pred = B . adj <EOL> G = nx . Graph ( ) <EOL> G . graph . update ( B . graph ) <EOL> G . add_nodes_from ( ( n , B . node [ n ] ) for n in nodes ) <EOL> n_top = float ( len ( B ) - len ( nodes ) ) <EOL> for u in nodes : <EOL> unbrs = set ( B [ u ] ) <EOL> nbrs2 = set ( ( n for nbr in unbrs for n in B [ nbr ] ) ) - set ( [ u ] ) <EOL> for v in nbrs2 : <EOL> vnbrs = set ( pred [ v ] ) <EOL> common = unbrs & vnbrs <EOL> if not ratio : <EOL> weight = len ( common ) <EOL> else : <EOL> weight = len ( common ) / n_top <EOL> G . add_edge ( u , v , weight = weight ) <EOL> return G <EOL> def collaboration_weighted_projected_graph ( B , nodes ) : <EOL> r"""<STR_LIT>""" <EOL> if B . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if B . is_directed ( ) : <EOL> pred = B . pred <EOL> G = nx . DiGraph ( ) <EOL> else : <EOL> pred = B . adj <EOL> G = nx . Graph ( ) <EOL> G . graph . update ( B . graph ) <EOL> G . add_nodes_from ( ( n , B . node [ n ] ) for n in nodes ) <EOL> for u in nodes : <EOL> unbrs = set ( B [ u ] ) <EOL> nbrs2 = set ( ( n for nbr in unbrs for n in B [ nbr ] ) ) - set ( [ u ] ) <EOL> for v in nbrs2 : <EOL> vnbrs = set ( pred [ v ] ) <EOL> common = unbrs & vnbrs <EOL> weight = sum ( [ <NUM_LIT:1.0> / ( len ( B [ n ] ) - <NUM_LIT:1> ) for n in common if len ( B [ n ] ) > <NUM_LIT:1> ] ) <EOL> G . add_edge ( u , v , weight = weight ) <EOL> return G <EOL> def overlap_weighted_projected_graph ( B , nodes , jaccard = True ) : <EOL> r"""<STR_LIT>""" <EOL> if B . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if B . is_directed ( ) : <EOL> pred = B . pred <EOL> G = nx . DiGraph ( ) <EOL> else : <EOL> pred = B . adj <EOL> G = nx . Graph ( ) <EOL> G . graph . update ( B . graph ) <EOL> G . add_nodes_from ( ( n , B . node [ n ] ) for n in nodes ) <EOL> for u in nodes : <EOL> unbrs = set ( B [ u ] ) <EOL> nbrs2 = set ( ( n for nbr in unbrs for n in B [ nbr ] ) ) - set ( [ u ] ) <EOL> for v in nbrs2 : <EOL> vnbrs = set ( pred [ v ] ) <EOL> if jaccard : <EOL> weight = float ( len ( unbrs & vnbrs ) ) / len ( unbrs | vnbrs ) <EOL> else : <EOL> weight = float ( len ( unbrs & vnbrs ) ) / min ( len ( unbrs ) , len ( vnbrs ) ) <EOL> G . add_edge ( u , v , weight = weight ) <EOL> return G <EOL> def generic_weighted_projected_graph ( B , nodes , weight_function = None ) : <EOL> r"""<STR_LIT>""" <EOL> if B . is_multigraph ( ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if B . is_directed ( ) : <EOL> pred = B . pred <EOL> G = nx . DiGraph ( ) <EOL> else : <EOL> pred = B . adj <EOL> G = nx . Graph ( ) <EOL> if weight_function is None : <EOL> def weight_function ( G , u , v ) : <EOL> return len ( set ( G [ u ] ) & set ( pred [ v ] ) ) <EOL> G . graph . update ( B . graph ) <EOL> G . add_nodes_from ( ( n , B . node [ n ] ) for n in nodes ) <EOL> for u in nodes : <EOL> nbrs2 = set ( ( n for nbr in set ( B [ u ] ) for n in B [ nbr ] ) ) - set ( [ u ] ) <EOL> for v in nbrs2 : <EOL> weight = weight_function ( B , u , v ) <EOL> G . add_edge ( u , v , weight = weight ) <EOL> return G <EOL> def project ( B , nodes , create_using = None ) : <EOL> return projected_graph ( B , nodes ) </s>
