<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import random <EOL> try : <EOL> from scipy . special import zeta as _zeta <EOL> def zeta ( x , q , tolerance ) : <EOL> return _zeta ( x , q ) <EOL> except ImportError : <EOL> def zeta ( x , q , tolerance ) : <EOL> """<STR_LIT>""" <EOL> z = <NUM_LIT:0> <EOL> z_prev = - float ( '<STR_LIT>' ) <EOL> k = <NUM_LIT:0> <EOL> while abs ( z - z_prev ) > tolerance : <EOL> z_prev = z <EOL> z += <NUM_LIT:1> / ( ( k + q ) ** x ) <EOL> k += <NUM_LIT:1> <EOL> return z <EOL> import networkx as nx <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def _zipf_rv_below ( gamma , xmin , threshold ) : <EOL> """<STR_LIT>""" <EOL> result = nx . utils . zipf_rv ( gamma , xmin ) <EOL> while result > threshold : <EOL> result = nx . utils . zipf_rv ( gamma , xmin ) <EOL> return result <EOL> def _powerlaw_sequence ( gamma , low , high , condition , length , max_iters ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( max_iters ) : <EOL> seq = [ ] <EOL> while not length ( seq ) : <EOL> seq . append ( _zipf_rv_below ( gamma , low , high ) ) <EOL> if condition ( seq ) : <EOL> return seq <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> def _generate_min_degree ( gamma , average_degree , max_degree , tolerance , <EOL> max_iters ) : <EOL> """<STR_LIT>""" <EOL> min_deg_top = max_degree <EOL> min_deg_bot = <NUM_LIT:1> <EOL> min_deg_mid = ( min_deg_top - min_deg_bot ) / <NUM_LIT:2> + min_deg_bot <EOL> itrs = <NUM_LIT:0> <EOL> mid_avg_deg = <NUM_LIT:0> <EOL> while abs ( mid_avg_deg - average_degree ) > tolerance : <EOL> if itrs > max_iters : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> mid_avg_deg = <NUM_LIT:0> <EOL> for x in range ( int ( min_deg_mid ) , max_degree + <NUM_LIT:1> ) : <EOL> mid_avg_deg += ( x ** ( - gamma + <NUM_LIT:1> ) ) / zeta ( gamma , min_deg_mid , <EOL> tolerance ) <EOL> if mid_avg_deg > average_degree : <EOL> min_deg_top = min_deg_mid <EOL> min_deg_mid = ( min_deg_top - min_deg_bot ) / <NUM_LIT:2> + min_deg_bot <EOL> else : <EOL> min_deg_bot = min_deg_mid <EOL> min_deg_mid = ( min_deg_top - min_deg_bot ) / <NUM_LIT:2> + min_deg_bot <EOL> itrs += <NUM_LIT:1> <EOL> return round ( min_deg_mid ) <EOL> def _generate_communities ( degree_sequence , community_sizes , mu , max_iters ) : <EOL> """<STR_LIT>""" <EOL> result = [ set ( ) for _ in community_sizes ] <EOL> n = len ( degree_sequence ) <EOL> free = list ( range ( n ) ) <EOL> for i in range ( max_iters ) : <EOL> v = free . pop ( ) <EOL> c = random . choice ( range ( len ( community_sizes ) ) ) <EOL> s = round ( degree_sequence [ v ] * ( <NUM_LIT:1> - mu ) ) <EOL> if s < community_sizes [ c ] : <EOL> result [ c ] . add ( v ) <EOL> else : <EOL> free . append ( v ) <EOL> if len ( result [ c ] ) > community_sizes [ c ] : <EOL> free . append ( result [ c ] . pop ( ) ) <EOL> if not free : <EOL> return result <EOL> raise nx . NetworkXError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def LFR_benchmark_graph ( n , tau1 , tau2 , mu , average_degree = None , <EOL> min_degree = None , max_degree = None , min_community = None , <EOL> max_community = None , tol = <NUM_LIT> , max_iters = <NUM_LIT> , <EOL> seed = None ) : <EOL> r"""<STR_LIT>""" <EOL> if seed is not None : <EOL> random . seed ( seed ) <EOL> if not tau1 > <NUM_LIT:1> : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if not tau2 > <NUM_LIT:1> : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if not <NUM_LIT:0> <= mu <= <NUM_LIT:1> : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if max_degree is None : <EOL> max_degree = n <EOL> elif not <NUM_LIT:0> < max_degree <= n : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> if not ( ( min_degree is None ) ^ ( average_degree is None ) ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if min_degree is None : <EOL> min_degree = _generate_min_degree ( tau1 , average_degree , max_degree , <EOL> tol , max_iters ) <EOL> low , high = min_degree , max_degree <EOL> condition = lambda seq : sum ( seq ) % <NUM_LIT:2> == <NUM_LIT:0> <EOL> length = lambda seq : len ( seq ) >= n <EOL> deg_seq = _powerlaw_sequence ( tau1 , low , high , condition , length , max_iters ) <EOL> if min_community is None : <EOL> min_community = min ( deg_seq ) <EOL> if max_community is None : <EOL> max_community = max ( deg_seq ) <EOL> low , high = min_community , max_community <EOL> condition = lambda seq : sum ( seq ) == n <EOL> length = lambda seq : sum ( seq ) >= n <EOL> comms = _powerlaw_sequence ( tau2 , low , high , condition , length , max_iters ) <EOL> max_iters *= <NUM_LIT:10> * n <EOL> communities = _generate_communities ( deg_seq , comms , mu , max_iters ) <EOL> G = nx . Graph ( ) <EOL> G . add_nodes_from ( range ( n ) ) <EOL> for c in communities : <EOL> for u in c : <EOL> while G . degree ( u ) < round ( deg_seq [ u ] * ( <NUM_LIT:1> - mu ) ) : <EOL> v = random . choice ( list ( c ) ) <EOL> G . add_edge ( u , v ) <EOL> while G . degree ( u ) < deg_seq [ u ] : <EOL> v = random . choice ( range ( n ) ) <EOL> if v not in c : <EOL> G . add_edge ( u , v ) <EOL> G . node [ u ] [ '<STR_LIT>' ] = c <EOL> return G </s>
