def generate_scale_free_power_law_graph(num, exp, seed): ''' this function generates a scale free with power law graph and write it into a file with .net format ''' sequence = create_degree_sequence(num, powerlaw_sequence, exponent=exp) graph = nx.configuration_model(sequence, seed=seed) loops = graph.selfloop_edges() json_str = json_graph.dumps(graph) dict_graph = json.loads(json_str) output_file = open('scale_free_power_law.net', 'w') # write nodes total_node_num = len(dict_graph['nodes']) output_file.write('*Vertices ' + str(total_node_num) + '\n') count = 1 for item in dict_graph['nodes']: # +1 coz id starts with 0, it should start from 1 output_file.write(' ' + str(count) + ' ' + str(item['id'] + 1) + '\n') count = count + 1 # write edges, links output_file.write('*Edges' + '\n') for item in dict_graph['links']: # +1 coz source and target starts with 0, it should start from 1 output_file.write( str(item['source'] + 1) + ' ' + str(item['target'] + 1) + ' 1' + '\n') output_file.close()
def pogiantc(n, c): mean = 0 for i in range(0, 1): z = utils.create_degree_sequence(100, powerlaw_sequence) print(z) G = nx.configuration_model(z) components = nx.connected_components(G) gene_components = sorted(components, key=len, reverse=True) mean = mean + (1.0 * len(gene_components[0])) / n i = i + 1 print(i) return (mean)
def create_star(self,node): sequence = create_degree_sequence(node, powerlaw_sequence, exponent=2.5) graph = nx.configuration_model(sequence) loops = graph.selfloop_edges() graph = nx.Graph(graph) graph.remove_edges_from(loops) components = sorted(nx.connected_components(graph), key=len, reverse=True) lcc = graph.subgraph(components[0]) #pos = nx.spring_layout(lcc) #nx.draw_networkx(lcc, pos) graph = list(nx.generate_edgelist(lcc)) edges = lcc.edges() #print(edges) flat = list(sum(edges, ())) return edges, max(flat,key=flat.count)
def test_degree_sequences(): seq = create_degree_sequence(10, uniform_sequence) assert_equal(len(seq), 10) seq = create_degree_sequence(10, powerlaw_sequence) assert_equal(len(seq), 10)
print components print "in: infect" for i in infect: print "link: infect infect@node%d" % i ports = [0 for i in range(0,len(nodes))] for edge in CM.edges(): origin = edge[0] target = edge[1] print "link: out%d@node%d in%d@node%d" % (ports[origin],origin,ports[target],target) print "link: out%d@node%d in%d@node%d" % (ports[target],target,ports[origin],origin) ports[origin] += 1 ports[target] += 1 if (len(sys.argv) < 2): print "arg[0] is population size, arg[1] is gamma alpha (default 10), arg[2] is beta (default 1) and arg[3] is infected size (default 1%)" sys.exit(0); n = int(sys.argv[1]) seq=create_degree_sequence(n,lambda x: [rnd.gammavariate(alpha=10.0 if len(sys.argv) < 3 else float(sys.argv[2]) ,beta=1.0 if len(sys.argv) < 4 else float(sys.argv[3])) for i in range(x)]) G=nx.configuration_model(seq) G.remove_edges_from(G.selfloop_edges()) G=nx.Graph(G) degree_sequence=sorted(nx.degree(G).values(),reverse=True) # degree sequence print "%Degree sequence is ", degree_sequence dmax=max(degree_sequence) print "%%Max degree is: %d"%dmax printma(G,infect=[rnd.randint(0,n-1) for i in range(n/100 if len(sys.argv) < 5 else int(sys.argv[4]))])
cp = 0.02 cdr = args.cell_death_rate #0.25 bcr = args.b_cell_rate #0.05 Variant.creact = True T = args.T fasta = SeqIO.parse(args.input, 'fasta') initial = fasta.next() print initial # Generate network # noinspection PyDeprecation sequence = create_degree_sequence(num, powerlaw_sequence, exponent=exp) graph = nx.configuration_model(sequence, seed=seed) loops = graph.selfloop_edges() # remove parallel edges and self-loops graph = nx.Graph(graph) graph.remove_edges_from(loops) # get largest connected component # unfortunately, the iterator over the components is not guaranteed to be sorted by size component = max(nx.connected_components(graph), key=len) lcc = graph.subgraph(component) nd.write_dot(lcc, out_dir_f % "scale_free.dot") print "Number of nodes in generated scale free graph: %i" % len(lcc) transmission = nx.DiGraph()
def test_degree_sequences(): seq=create_degree_sequence(10,uniform_sequence) assert_equal(len(seq), 10) seq=create_degree_sequence(10,powerlaw_sequence) assert_equal(len(seq), 10)