-
Notifications
You must be signed in to change notification settings - Fork 0
/
case_analysis_plot.py
373 lines (346 loc) · 14.7 KB
/
case_analysis_plot.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import os
import os.path
import matplotlib.pyplot as plt
import json
import time
import pandas as pd
# Global Declarations
# Dictionary containing graph value
# Key is the point in x axis and value is a dictionary containing list of y values for different similarity thresholds
graph_data = {}
# The value dictionary of graph_data
helper_dict = {}
# list for similarity thresholds
sim_thld_lst = []
# Plot title
plot_title = ''
# X label
x_label = ''
# Y label
y_label = ''
# Maximum Iteration
max_iter = 5
# Running command
command = 'python3'
user_generation_script = 'user_secnario_producer.py'
def set_to_defaults():
"""
Function: set_to_defaults\n
Parameters: None\n
Functionality: Sets the scenario_input.json and user_location.json file to default values.\n
"""
parent_dir = os.getcwd()
dir_name = 'input_files'
file_path = os.path.join(parent_dir, dir_name)
default_scenario = {}
file_name = 'default_scenario.json'
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
default_scenario = json.load(file_pointer)
number_users = default_scenario['number_users']
N = default_scenario['N']
M = default_scenario['M']
default_scenario.pop('number_users')
file_name = 'scenario_input.json'
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(default_scenario, file_pointer)
user_loc = {"N": N, "M": M, "Number of User": number_users}
file_name = 'user_location.json'
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(user_loc, file_pointer)
def take_input():
"""
Function: take_input\n
Parameters: None\n
Functionality: decide which graph to plot according to plot_input.json file\n
"""
global sim_thld_lst, plot_title, x_label, y_label
set_to_defaults()
parent_dir = os.getcwd()
dir_name = 'input_files'
file_path = os.path.join(parent_dir, dir_name)
plot_input_data = {}
file_name = 'plot_input.json'
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
plot_input_data = json.load(file_pointer)
sim_thld_lst = plot_input_data['Similarity Threshold']
plot_title = plot_input_data['plot_name']
x_label = plot_input_data['x_axis']
y_label = plot_input_data['y_axis']
lst_to_iterate = plot_input_data[x_label]
determine(lst_to_iterate)
def determine(lst_to_iterate):
"""
Function: determine\n
Parameters: lst_to_iterate -> list over which x axis value will depend\n
Functionality: Determine x axis iterator\n
"""
global x_label, sim_thld_lst
if 'coverage' in x_label.lower():
plot('coverage_threshold', lst_to_iterate)
elif 'users' in x_label.lower():
plot('Number of User', lst_to_iterate)
elif 'area' in x_label.lower():
plot('NM', lst_to_iterate)
elif 'radius' in x_label.lower():
plot('radius_UAV', lst_to_iterate)
def plot(type, lst_to_iterate):
"""
Function: plot\n
Parameter: type -> which plot to plot (^_^), lst_to_iterate -> list of data which will be changing \n
Functionality: Create the plot of desiered type\n
"""
global sim_thld_lst, command, user_generation_script, helper_dict
parent_dir = os.getcwd()
dir_name = 'input_files'
file_path = os.path.join(parent_dir, dir_name)
if type == 'Number of User':
for iter in range(max_iter):
for i in range(len(lst_to_iterate)):
value = lst_to_iterate[i]
diff = 0
file_name = 'user_location.json'
user_loc = {}
user_input = {}
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
user_loc = json.load(file_pointer)
if i != 0:
diff = value - lst_to_iterate[i - 1]
user_loc[type] = diff
file_name = 'user_input.json'
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
user_input = json.load(file_pointer)
prev_user_pos = user_input['Position of Ground users']
file_name = 'user_location.json'
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(user_loc, file_pointer)
os.system(f'{command} {user_generation_script}')
file_name = 'user_input.json'
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
user_input = json.load(file_pointer)
new_user_pos = user_input['Position of Ground users']
user_input['Number of Ground users'] = value
user_input['Position of Ground users'] = prev_user_pos + \
new_user_pos
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(user_input, file_pointer)
else:
user_loc[type] = value
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(user_loc, file_pointer)
os.system(f'{command} {user_generation_script}')
file_name = 'user_location.json'
user_loc[type] = value
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(user_loc, file_pointer)
for similarity_threshold in sim_thld_lst:
file_name = 'scenario_input.json'
scenario_data = {}
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
scenario_data = json.load(file_pointer)
scenario_data['similarity_threshold'] = similarity_threshold
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(scenario_data, file_pointer)
os.system(f'{command} main.py')
os.system(f'{command} analysis.py')
update_file(similarity_threshold,
value, get_number_UAV())
os.system('bash fresh_analysis.sh')
plot_graph(True)
elif type == 'NM':
for value in lst_to_iterate:
helper_dict = {}
N, M = map(int, value.split(' '))
file_name = 'user_location.json'
user_loc = {}
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
user_loc = json.load(file_pointer)
user_loc['N'] = N
user_loc['M'] = M
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(user_loc, file_pointer)
for iter in range(max_iter):
os.system(f'{command} {user_generation_script}')
for similarity_threshold in sim_thld_lst:
file_name = 'scenario_input.json'
scenario_data = {}
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
scenario_data = json.load(file_pointer)
scenario_data['similarity_threshold'] = similarity_threshold
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(scenario_data, file_pointer)
os.system(f'{command} main.py')
os.system(f'{command} analysis.py')
update_file(similarity_threshold,
value, get_number_UAV())
os.system('bash fresh_analysis.sh')
plot_graph(False)
else:
for value in lst_to_iterate:
helper_dict = {}
for iter in range(max_iter):
os.system(f'{command} {user_generation_script}')
for similarity_threshold in sim_thld_lst:
file_name = 'scenario_input.json'
scenario_data = {}
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
scenario_data = json.load(file_pointer)
scenario_data['similarity_threshold'] = similarity_threshold
scenario_data[type] = value
with open(os.path.join(file_path, file_name), 'w') as file_pointer:
json.dump(scenario_data, file_pointer)
os.system(f'{command} main.py')
os.system(f'{command} analysis.py')
update_file(similarity_threshold,
value, get_number_UAV())
os.system('bash fresh_analysis.sh')
plot_graph(True)
def update_file(similarity_threshold, x_data, y_data):
"""
Function update_file\n
Parameters: similarity_threshold, x_data -> data point on x axis, y_data -> data point on y axis\n
Functionality: Updates the file with required Parameters\n
"""
parent_dir = os.getcwd()
dir_name = 'analysis_output_files'
file_name = 'graph_data.log'
line_to_write = f'{similarity_threshold} {x_data} {y_data}\n'
with open(os.path.join(parent_dir, dir_name, file_name), 'a') as file_pointer:
file_pointer.writelines(line_to_write)
def get_number_UAV():
"""
Function: get_number_UAV\n
Parameters: None\n
Returns: Number of UAVs required to reach that target\n
"""
parent_dir = os.getcwd()
dir_name = 'input_files'
file_path = os.path.join(parent_dir, dir_name)
scenario_input = {}
file_name = 'scenario_input.json'
with open(os.path.join(file_path, file_name), 'r') as file_pointer:
scenario_input = json.load(file_pointer)
epsilon = scenario_input['epsilon']
learning_rate = scenario_input['learning_rate']
decay_factor = scenario_input['decay_factor']
parent_dir = os.path.join(os.getcwd(), 'output_files')
curr_dir = str(epsilon) + "_" + str(learning_rate) + \
"_" + str(decay_factor)
dir_path = os.path.join(parent_dir, curr_dir)
file_name = 'analysis.log'
file_data = []
with open(os.path.join(dir_path, file_name), 'r') as file_pointer:
file_data = file_pointer.readlines()
line_number = 13
return int(float(file_data[line_number].split(':')[1]))
def process_graph_data():
"""
Function: process_graph_data\n
Parameter: None\n
Functionality: Process the graph_data to make it ready for plots\n
Returns: dictionary where key is similarity_threshold and value is a list where each list item contains a tuple of x_point and a list of y_point where list items are 75%, std, mean, min, max, median\n
"""
global graph_data
processed_data = {}
parent_dir = os.getcwd()
dir_name = 'analysis_output_files'
file_name = 'graph_data.log'
data = []
with open(os.path.join(parent_dir, dir_name, file_name), 'r') as file_pointer:
data = file_pointer.readlines()
data = data[1:]
for line in data:
lst_temp = line.split(' ')
if len(lst_temp) == 4:
sim_th = round(float(lst_temp[0]), 2)
N = round(float(lst_temp[1]), 2)
M = round(float(lst_temp[2]), 2)
x_data = (N, M)
y_data = round(float(lst_temp[3]), 2)
else:
sim_th = round(float(lst_temp[0]), 2)
x_data = round(float(lst_temp[1]), 2)
y_data = float(lst_temp[2])
key = (sim_th, x_data)
if key not in processed_data:
processed_data[key] = [y_data]
else:
processed_data[key].append(y_data)
for key, y_lst in processed_data.items():
temp_df = pd.DataFrame(y_lst)
des_dict = temp_df.describe()[0]
output_lst = [round(float(des_dict['75%']), 2), round(float(des_dict['std']), 2), round(float(des_dict['mean']), 2), round(float(des_dict['min']), 2), round(float(des_dict['max']), 2), round(float(des_dict['50%']), 2)]
sim_th, x_data = key
if sim_th not in graph_data:
graph_data[sim_th] = [(x_data, output_lst)]
else:
graph_data[sim_th].append((x_data, output_lst))
return graph_data
def plot_graph(flag):
"""
Function: plot_graph\n
Parameter: flag -> bool variable to indicate the type of plot\n
Functionality: Generate the plot\n
"""
global graph_data, plot_title, x_label, y_label
graph_data = process_graph_data()
parent_dir = os.getcwd()
dir_name = 'analysis_output_files'
file_name = 'graph_data.json'
with open(os.path.join(parent_dir, dir_name, file_name), 'w') as file_pointer:
json.dump(graph_data, file_pointer)
if flag:
for sim_thld, points in graph_data.items():
x = []
y = []
for point in points:
a, b = point
x.append(a)
y.append(b[0])
plt.scatter(x, y)
plt.plot(x, y, label=f'{sim_thld}')
else:
parent_dir = os.getcwd()
dir_name = 'input_files'
file_name = 'scenario_input.json'
scenario_data = {}
with open(os.path.join(parent_dir, dir_name, file_name), 'r') as file_pointer:
scenario_data = json.load(file_pointer)
cell_size = scenario_data['cell_size']
unit_mul = scenario_data['unit_multiplier']
cell_size *= unit_mul
cell_size = unit_mul / cell_size
for sim_thld, points in graph_data.items():
x = []
y = []
for point in points:
x_data, y_data = point
N, M = map(int, x_data)
x.append(f'{N // cell_size} X {M // cell_size}')
y.append(y_data[0])
plt.scatter(x, y)
plt.plot(x, y, label=f'{sim_thld}')
plt.title(
plot_title, fontweight="bold")
plt.xlabel(x_label, fontweight='bold')
plt.ylabel(y_label, fontweight='bold')
plt.legend()
file_name = f'{plot_title}'
parent_dir = os.getcwd()
dir_name = 'analysis_output_files'
plt.savefig(os.path.join(parent_dir, dir_name, file_name))
if __name__ == "__main__":
dir_path = os.path.join(os.getcwd(), 'analysis_output_files')
try:
os.mkdir(dir_path)
except OSError as error:
pass
parent_dir = os.getcwd()
dir_name = 'analysis_output_files'
file_name = 'graph_data.log'
line_to_write = f'+-----------------------------Data-----------------------------+\n'
with open (os.path.join (parent_dir, dir_name, file_name), 'w') as file_pointer:
file_pointer.writelines(line_to_write)
os.system('bash fresh_analysis.sh')
print(f'Relax!! we have taken the charge. (-_-)')
os.system('bash fresh_analysis.sh')
take_input()