-
Notifications
You must be signed in to change notification settings - Fork 4
/
1.create_networks.py
125 lines (92 loc) · 3.78 KB
/
1.create_networks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
- Creates the network csv files used in the evaluation
- Designed to be called from the command line with keyword arguments described below in the CommandLine() class.
- Output is a csv file for each network
"""
import random, argparse, os
import pandas as pd
from igraph import *
import networkx as nx
# ---------------
# Define functions to create graphs
# ---------------
def gen_ER_random(nodes, p, name):
# Generate Graph
G = Graph.Erdos_Renyi(n=nodes,p=p,directed=True)
# Transform into dataframe of edges
source, target = [], []
for edge in G.es:
source.append(edge.source)
target.append(edge.target)
d = pd.DataFrame({'source': source,'target': target})
# Save to file
d.to_csv('./network_data/ER/' + name +'.csv', index=False)
def gen_WS(nodes, K, beta, name):
# Generate Graph
G = Graph.Watts_Strogatz(1,nodes,K,beta)
# Transform into dataframe of edges
source, target = [], []
for edge in G.es:
source.append(edge.source)
target.append(edge.target)
d = pd.DataFrame({'source': source,'target': target})
# Save to file
d.to_csv('./network_data/WS/' + name +'.csv', index=False)
def gen_SF(nodes, gam, name):
# Generate Graph
s = nx.utils.powerlaw_sequence(nodes, gam)
s = [6*i for i in s]
G = nx.expected_degree_graph(s, selfloops=False)
# Transform into dataframe of edges
source, target = [], []
for edge in G.edges():
source.append(edge[0])
target.append(edge[1])
d = pd.DataFrame({'source': source,'target': target})
# Save to file
d.to_csv('./network_data/SF/' + name +'.csv', index=False)
# ---------------
# Define command line argument class
# ---------------
class CommandLine:
def __init__(self):
parser = argparse.ArgumentParser(description = "The keyword arguments are:")
parser.add_argument("-n", help = "Integer size of network (default=100)", required = False, default = 100)
parser.add_argument("-vers", help = "Integer number of graphs of each type (default=10)", required = False, default = 10)
argument = parser.parse_args()
if argument.n:
print("You have used '-n' with argument: {0}".format(argument.n))
if argument.vers:
print("You have used '-vers' with argument: {0}".format(argument.vers))
self.n, self.vers = argument.n, argument.vers
# ---------------
# Generate Network Files
# ---------------
if __name__ == '__main__':
# Read in keyword arguments
app = CommandLine()
n, vers = int(app.n), int(app.vers)
# Create folders if necessary
if not os.path.exists("./network_data"):
os.mkdir("./network_data")
if not os.path.exists("./network_data/ER"):
os.mkdir("./network_data/ER")
if not os.path.exists("./network_data/WS"):
os.mkdir("./network_data/WS")
if not os.path.exists("./network_data/SF"):
os.mkdir("./network_data/SF")
# Generate Erdos-Renyi Network
for p in [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]:
for j in range(vers):
random.seed(p * j)
gen_ER_random(n, p, 'nodes' + str(n) + '_p' + str(p) + '_v' + str(j+1))
# Generate Watts-Strogatz Networks
for beta in [0,0.05,0.1,0.15,0.2,0.25,0.3,0.4,0.5,0.6,0.7,0.8,0.9]:
for j in range(vers):
random.seed(beta * j)
gen_WS(n, 8, beta, 'nodes' + str(n) + '_beta' + str(beta) + '_v' + str(j+1))
# Create SF Networks
for gamma in [1.5,2,2.25,2.5,3,3.5,4]:
for j in range(vers):
random.seed(gamma * j)
gen_SF(n, gamma, 'nodes' + str(n) + '_gamma' + str(gamma) + '_v' + str(j+1))