-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrn_properties.py
294 lines (268 loc) · 11.2 KB
/
grn_properties.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import json
import grn_endpoint.grn_info
import move_endpoint.movement
import numpy as np
import collections
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx
import pickle
command = "python3"
script = "user_secnario_producer.py"
UAV_to_UAV_threshold = 0
cell_size = 0
def get_UAV_graph(UAV_location):
"""
Function: get_UAV_graph\n
Parameters: UAV_location -> list of groun users placed along with their locations\n:
Returns: UAV graph at a particular point of time\n
"""
global UAV_to_UAV_threshold, cell_size
placed = [node for node, loc in UAV_location.items()]
UAV_G = nx.Graph()
for node in placed:
UAV_G.add_node(node)
for node1 in placed:
for node2 in placed:
if move_endpoint.movement.get_euc_dist(UAV_location[node1], UAV_location[node2]) <= int(UAV_to_UAV_threshold // cell_size) and node1 != node2:
UAV_G.add_edge(node1, node2)
return UAV_G
def get_triangles(dir_graph):
"""
Function: get_triangles\n
Parameter: dir_graph -> Grn Graph\n
Returns: Number of triangles in a directed graph\n
"""
triangles = 0
for u in dir_graph.nodes:
for v in dir_graph.nodes:
for w in dir_graph.nodes:
if dir_graph.has_edge(u, v) and dir_graph.has_edge(v, w) and dir_graph.has_edge(u, w):
triangles += 1
return triangles // 6
def init():
"""
Function: init\n
Parameter: None\n
Functionality: Initialiazes the GRN environment\n
"""
grn_endpoint.grn_info.init()
degree_data = get_degree_count()
motif_data = get_motif_count()
print(get_triangles(grn_endpoint.grn_info.get_grn_graph()))
return (degree_data, motif_data)
def get_degree_count():
"""
Function: get_degree_count\n
Parameter: None\n
Returns: dictionary containing key as degree and value as number of nodes having that degree\n
"""
degrees_val = sorted([degree for node,
degree in grn_endpoint.grn_info.get_grn_graph().degree()])
degrees_count = dict(collections.Counter(degrees_val))
return degrees_count
def get_motif_count():
"""
Function: get_motif_count\n
Parameter: None\n
Returns: dictionary containing key as motif and value as number of nodes having that motif\n
"""
motifs_val = sorted([motif for node, motif in dict(
grn_endpoint.grn_info.get_n_motif()).items()])
motifs_count = dict(collections.Counter(motifs_val))
return motifs_count
def write_to_json(data, file_path):
"""
Function: write_to_json\n
Parameters: data -> data to be written, file_path -> path of the file\n
Functionality: Writes the json data to the file\n
"""
with open(file_path, 'w') as file_pointer:
json.dump(data, file_pointer)
def get_filter_data(data):
"""
Function: get_filter_data\n
Parameters: data -> data to plot\n
Functionality: Filter the data and return the filtered data\n
"""
# print(data)
dividend = 10
filter_data = {}
for key, frequency in data.items():
new_key = key // dividend
if new_key not in filter_data:
filter_data[new_key] = [frequency]
else:
filter_data[new_key].append(frequency)
new_data = {}
for key, values in filter_data.items():
new_data[key] = sum(values)
data_x = [key for key, frequency in new_data.items()]
# summation = sum(data.values())
summation = 1
data_y = [frequency / summation for key, frequency in new_data.items()]
return (data_x, data_y)
def manage_all(degree_data, motif_data, degree_data_proposed, motif_data_proposed):
"""
Function: manage_all\n
Parameters: degree_data -> degree data for the GRN graph, motif_data -> motif data for the GRN graph, degree_data_proposed -> degree data for the Proposed graph, motif_data_proposed -> motif data for the Proposed graph\n
Functionality: Manages the simulation\n
"""
# Write data to file
file_path = os.path.join(
os.getcwd(), 'analysis_output_files', 'grn_properties_degree_plot.json')
write_to_json(degree_data, file_path)
file_path = os.path.join(
os.getcwd(), 'analysis_output_files', 'grn_properties_motif_plot.json')
write_to_json(motif_data, file_path)
file_path = os.path.join(
os.getcwd(), 'analysis_output_files', 'proposed_properties_degree_plot.json')
write_to_json(degree_data_proposed, file_path)
file_path = os.path.join(
os.getcwd(), 'analysis_output_files', 'proposed_properties_motif_plot.json')
write_to_json(motif_data_proposed, file_path)
# End writing
file_name = 'degree_distribution_grn_proposed.png'
file_path = os.path.join(os.getcwd(), 'analysis_output_files', file_name)
# Description of degree distribution plot
data_x, data_y = get_filter_data(degree_data)
data_x_proposed, data_y_proposed = get_filter_data(degree_data_proposed)
plt.scatter(data_x, data_y, color='b')
plt.scatter(data_x_proposed, data_y_proposed, color='r', marker='x')
plt.ylabel('Frequency of occurance',
fontsize=12, fontweight='bold')
plt.xlabel('Degree of nodes', fontsize=12, fontweight='bold')
# plt.title('Frequency Vs Degree of nodes',
# fontsize=17, fontweight='bold')
plt.xticks(fontsize=10, fontweight='bold')
plt.yticks(fontsize=10, fontweight='bold')
plt.savefig(file_path)
# Description of motif distribution plot
file_name = 'motif_distribution_grn_proposed.png'
file_path = os.path.join(os.getcwd(), 'analysis_output_files', file_name)
data_x, data_y = get_filter_data(motif_data)
data_x_proposed, data_y_proposed = get_filter_data(motif_data_proposed)
plt.scatter(data_x, data_y, color='b')
plt.scatter(data_x_proposed, data_y_proposed, color='r', marker='x')
plt.ylabel('Frequency of occurance',
fontsize=12, fontweight='bold')
plt.xlabel('Node motif centrality of nodes',
fontsize=12, fontweight='bold')
# plt.title('Frequency Vs Node motif centrality of nodes',
# fontsize=17, fontweight='bold')
plt.xticks(fontsize=10, fontweight='bold')
plt.yticks(fontsize=10, fontweight='bold')
plt.savefig(file_path)
def manage_TRN(degree_data, motif_data):
"""
Function: manage_TRN\n
Parameters: degree_data -> degree data for the GRN graph, motif_data -> motif data for the GRN graph\n
Functionality: Manages the simulation\n
"""
# Write data to file
file_path = os.path.join(
os.getcwd(), 'analysis_output_files', 'grn_properties_degree_plot.json')
write_to_json(degree_data, file_path)
file_path = os.path.join(
os.getcwd(), 'analysis_output_files', 'grn_properties_motif_plot.json')
write_to_json(motif_data, file_path)
# End writing
file_name = 'degree_distribution_grn_proposed.png'
file_path = os.path.join(os.getcwd(), 'analysis_output_files', file_name)
# Description of degree distribution plot
data_x, data_y = get_filter_data(degree_data)
# data_x_proposed, data_y_proposed = get_filter_data(degree_data_proposed)
plt.bar(data_x, data_y, color='b')
# plt.scatter(data_x_proposed, data_y_proposed, color='r', marker='x')
plt.ylabel('Frequency of occurance',
fontsize=12, fontweight='bold')
plt.xlabel('Degree of nodes', fontsize=12, fontweight='bold')
# plt.title('Frequency Vs Degree of nodes',
# fontsize=17, fontweight='bold')
plt.xticks(fontsize=10, fontweight='bold')
plt.yticks(fontsize=10, fontweight='bold')
# plt.savefig(file_path)
plt.yscale("log")
plt.show()
# Description of motif distribution plot
file_name = 'motif_distribution_grn_proposed.png'
file_path = os.path.join(os.getcwd(), 'analysis_output_files', file_name)
data_x, data_y = get_filter_data(motif_data)
# data_x_proposed, data_y_proposed = get_filter_data(motif_data_proposed)
plt.bar(data_x, data_y, color='b')
# plt.scatter(data_x_proposed, data_y_proposed, color='r', marker='x')
plt.ylabel('Frequency of occurance',
fontsize=12, fontweight='bold')
plt.xlabel('Node motif centrality of nodes',
fontsize=12, fontweight='bold')
# plt.title('Frequency Vs Node motif centrality of nodes',
# fontsize=17, fontweight='bold')
plt.xticks(fontsize=10, fontweight='bold')
plt.yticks(fontsize=10, fontweight='bold')
plt.yscale("log")
# plt.savefig(file_path)
plt.show()
def init_proposed():
"""
Function: init_proposed\n
Parameters: None\n
Functionality: initialiazes the environment for the proposed algorithm\n
"""
os.system('bash fresh_analysis.sh')
os.system(f'{command} {script}')
os.system(f'{command} main.py')
global UAV_to_UAV_threshold, cell_size
parent_dir = os.getcwd()
target_dir = 'input_files'
file_name = 'scenario_input.json'
file_path = os.path.join(parent_dir, target_dir, file_name)
with open(file_path, 'r') as file_pointer:
data = json.load(file_pointer)
UAV_to_UAV_threshold = data['UAV_to_UAV_threshold']
cell_size = data['cell_size']
parent_dir = os.getcwd()
target_dir = 'graph_output_files'
file_name = 'output_main0.json'
file_path = os.path.join(parent_dir, target_dir, file_name)
with open(file_path, 'r') as file_pointer:
data = json.load(file_pointer)
UAV_graph = get_UAV_graph(data)
degrees_val = sorted([degree for node,
degree in UAV_graph.degree()])
degrees_count = dict(collections.Counter(degrees_val))
motifs_val = sorted([motif for node, motif in dict(
grn_endpoint.grn_info.get_motif_dict(UAV_graph)).items()])
motifs_count = dict(collections.Counter(motifs_val))
return degrees_count, motifs_count
def properties_original():
grn_file_name = 'Ecoli.gml'
motif_file_name = 'Ecoli_centrality.p'
dir_name = 'grn_endpoint'
grn_file_path = os.path.join (os.getcwd(),dir_name, 'gml_files', grn_file_name)
motif_file_path = os.path.join (os.getcwd(), dir_name, 'gml_files', motif_file_name)
grn_graph = nx.read_gml(grn_file_path)
grn_graph = nx.convert_node_labels_to_integers(grn_graph, first_label=0)
print(f'Number of Edges: {len(grn_graph.edges())}')
print(f'Number of Nodes: {len(grn_graph.nodes())}')
degrees_val = sorted([degree for node,
degree in grn_graph.degree()])
degrees_count = dict(collections.Counter(degrees_val))
n_motif = pickle.load(open(motif_file_path, "rb"))
motifs_val = sorted([motif for node, motif in n_motif.items()])
motifs_count = dict(collections.Counter(motifs_val))
print(f'Minimum Degree: {min(degrees_count)}, Maximum Degree: {max(degrees_count)}')
print(f'Minimum Motif: {min(motifs_count)}, Maximum Motif: {max(motifs_count)}')
print(get_triangles(grn_graph))
return (degrees_count, motifs_count)
if __name__ == "__main__":
dir_path = os.path.join(os.getcwd(), 'analysis_output_files')
try:
os.mkdir(dir_path)
except OSError as error:
pass
# degree_data, motif_data = init()
# degree_data_proposed, motif_data_proposed = init_proposed()
# manage_TRN(degree_data, motif_data)
degree_data, motif_data = properties_original()
manage_TRN(degree_data, motif_data)