"Random networks were generated using a network rewiring approach. Each random network was generated by repeatedly choosing two edges at random (e.g., A–B and C–D) and swapping them (yielding A–D and C–B, or A–C and B–D). This operation was iterated 100 × m times on each random network, where m is the number of edges. Therefore, each random network contains the same nodes, the same number of edges, and the same degree for each node as the original network. "
I can read in the graph that I want to randomise:
import networkx as nx
graph = nx.read_edgelist("ListOfGenesInNetwork.NonRedundant",delimiter="\t",nodetype=str)
--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to networkx-discuss@googlegroups.com.
Visit this group at https://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/d/optout.
import networkx as nx
import sys
graph = nx.read_edgelist(sys.argv[1],delimiter="\t",nodetype=str)
how_many_edges = int(len(graph.edges()))
how_many_times = int(len(graph.edges()))*100
for i in range(1000):
random_file = "random." + str(i) + ".txt"
count = 0
while count < how_many_times:
random_network = nx.double_edge_swap(graph)
count +=1
nx.write_edgelist(random_network,random_file,delimiter="\t",data=False)
You have already "generated" your network, so you should look in the "algorithms" section of the docs to rewire it. In particular, the section on edge swaps. In the top portion of those doc pages is a green link to the python code in case you need to do something close to, but not the same as what these routines do.
On Thu, May 11, 2017 at 6:32 AM, Tom <dr.aoife.ma...@gmail.com> wrote:
Hi all, I have a network (edge_list) and I want to randomise it using this method:"Random networks were generated using a network rewiring approach. Each random network was generated by repeatedly choosing two edges at random (e.g., A–B and C–D) and swapping them (yielding A–D and C–B, or A–C and B–D). This operation was iterated 100 × m times on each random network, where m is the number of edges. Therefore, each random network contains the same nodes, the same number of edges, and the same degree for each node as the original network. "
I can read in the graph that I want to randomise:
import networkx as nx
graph = nx.read_edgelist("ListOfGenesInNetwork.NonRedundant",delimiter="\t",nodetype=str)
But then I'm confused, is it possible to do the above network re-wiring approach using NetworkX? I have seen random_graph_generator function in NetworkX, but I'm not sure that this is what I want...I'm just a bit confused as to where to start with the randomisation via network re-wiring step, if someone could point me in the right direction I'd appreciate it.
--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To post to this group, send email to networkx...@googlegroups.com.
import networkx as nx
import sys
graph = nx.read_edgelist(sys.argv[1],delimiter="\t",nodetype=str)
how_many_times = int(len(graph.edges()))*100
for i in range(100):
random_file = "random." + str(i) + ".txt"
count = 0
while count < how_many_times:
random_network = nx.double_edge_swap(graph)
count +=1
nx.write_edgelist(random_network,random_file,delimiter="\t",data=False)
while count < how_many_times:
random_network = nx.double_edge_swap(graph)
count +=1
with this:
nx.double_edge_swap(graph, nswap=how_many_times)
That will be much more efficient, although I suspect that this will still take a very long time on a graph with 220,000 edges.
Best,
Raf
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to networkx-discuss@googlegroups.com.
The problem is the code is prohibitively slow. When I run this on a test network with say 10 nodes, it works fine. However, my actual network has 220,000 edges (it is a protein interaction network of the whole genome). But so far, it hasn't even written one random network to file, in 121 hours. I've triple checked, this code definitely works on smaller sections of the data, so it's to do with the size of the file. I'm wondering if anyone can suggest a way of speeding up this script?
import networkx as nx
import sys
graph = nx.read_edgelist(sys.argv[1],delimiter="\t",nodetype=str)
how_many_times = int(len(graph.edges()))*100
for i in range(100):
random_file = "random." + str(i) + ".txt"
random_network = nx.double_edge_swap(graph,nswap=how_many_times)
nx.write_edgelist(random_network,random_file,delimiter="\t",data=False)
I get the error:
File "RandomiseNetwork.py", line 9, in <module>
random_network = nx.double_edge_swap(graph,nswap=how_many_times)
File "/home/nis/tom/env/local/lib/python2.7/site-packages/networkx-1.11rc2-py2.7.egg/networkx/algorithms/swap.py", line 66, in double_edge_swap
raise nx.NetworkXError("Number of swaps > number of tries allowed.")
networkx.exception.NetworkXError: Number of swaps > number of tries allowed.
Thank you for your suggestion. I actually need to maintain the degree of each node in my random graphs (e.g. so if a node in the actual graph has 20 edges, it should have 20 edges in each random graph) (and the same number of nodes and edges), which I don't think the random configuration would give me?
1 2
1 4
5 6
7 8
9 10
import sys
import random
from datetime import datetime
startTime = datetime.now()
pair_lambda = lambda x: each_pair[x].split("_")
new_list_lambda = lambda x: pair1[x] + "_"+ pair2[x]
list_of_edges = [line.strip().split()[0] + "_" + line.strip().split()[1] for line in open(sys.argv[1])]
random.shuffle(list_of_edges)
for i in range(1000):
random.shuffle(list_of_edges)
paired_list = zip(list_of_edges,list_of_edges[1:])[::2]
new_list = []
for each_pair in paired_list:
pair1,pair2 = pair_lambda(0),pair_lambda(1)
new_list1,new_list2 = new_list_lambda(0),new_list_lambda(1)
new_list.append(new_list1)
new_list.append(new_list2)
list_of_edges = new_list
print list_of_edges
print datetime.now() - startTime