-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
191 lines (143 loc) · 5.03 KB
/
analysis.py
File metadata and controls
191 lines (143 loc) · 5.03 KB
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
import os
import re
import math
import argparse
import numpy as np
import matplotlib.pyplot as plt
from sys import exit
from pathlib import Path
from matplotlib.backends.backend_pdf import PdfPages
import csv
def read_data(file):
file_data = {"mean" : 0.0, "var": 0.0, "data": [], "name": 0}
print("Reading file: " + file)
# Get second result because first is the level
base = Path(file).stem
file_data["name"] = float(re.findall(r'\d+\.?\d*', base)[-1])
with open(file, "r") as f:
for i, line in enumerate(f.read().splitlines()):
if i == 0:
file_data["mean"] = float(line)
elif i == 1:
file_data["var"] = float(line)
else:
file_data["data"].append(float(line))
return file_data
def get_histogram(hist_data, axis):
# Plot histogram
n, bins, patches = axis.hist(x=hist_data, bins=10, color='#0504aa', alpha=0.7, rwidth=0.85)
maxfreq = n.max()
axis.set_ylim(ymax=np.ceil(maxfreq / 10) * 10 if maxfreq % 10 else maxfreq + 10)
m = sum(hist_data) / len(hist_data)
v = sum([(x - m) ** 2 for x in hist_data]) / len(hist_data)
d = math.sqrt(v)
axis.grid(axis='y', alpha=0.75)
axis.set_xlabel('Value')
axis.set_ylabel('Frequency')
axis.set_title('Histogram')
axis.axvline(x=m, color='r', linestyle='-')
axis.axvline(x=m + d, color='g', linestyle='-')
axis.axvline(x=m - d, color='g', linestyle='-')
axis.legend(["Mean: {:.2f}".format(m), "Deviation: {:.2f}".format(d)])
def get_plot(x, y, axis, x_label):
axis.plot(x, y)
axis.grid(alpha=0.5)
axis.set_xlabel(x_label)
axis.set_ylabel('Q value')
axis.set_title('{} vs Q value'.format(x_label))
def get_heatmap(dir):
index = 0
bins = {
"L11": (8, 12),
"L12": (8, 18),
"L2": (16, 31),
"L3": (28, 40)
}
pdf = PdfPages(dir + "heatmap.pdf")
for file in os.listdir(dir):
if not file.endswith(".csv"):
continue
x = []
y = []
base = Path(file).stem
level = base.split('_')[0]
bin = bins[level]
with open(dir + file, newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
x.append(int(row[0]) / 32)
y.append(int(row[1]) / 32)
print("There are {} points in {}".format(len(x), level))
heatmap, xedges, yedges = np.histogram2d(x, y, bins=bin)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
fig = plt.figure(index, figsize=(10, 10))
axis = fig.add_subplot(1,1,1)
index += 1
axis.set_title('{}'.format(base))
axis.imshow(heatmap.T, extent=extent, origin='lower')
axis.invert_yaxis()
pdf.savefig(figure=fig)
pdf.close()
parser = argparse.ArgumentParser(
prog='IntrusionGame',
description="""Simulates a specified intrusion game based on the \"Physical Intrusion
Games—Optimizing Surveillance by Simulation and Game Theory\" paper""",
epilog='For research purpose only, see: https://github.com/FelipeACXavier/IntrusionGame')
parser.add_argument('-l', dest="level", default=0, help="Specify the level", type=str)
parser.add_argument('-t', dest="title", default=None, help="Title, i.e. parameter used", type=str)
parser.add_argument('-d', dest="dir", default=None, help="Folder where data is located", type=str)
parser.add_argument('--heatmap', dest="heatmap", default=False, action='store_true', help="Generate heatmap from csv files in dir")
args = parser.parse_args()
if not args.dir:
print("No folder given")
exit()
if args.heatmap:
get_heatmap(args.dir)
exit()
if args.level not in ["11", "12", "2", "3"]:
print("Invalid level selected")
exit()
if not args.title:
print("No title given")
exit()
level = args.level
subtitle = args.title
data_folder = args.dir
name = "level_" + str(level)
# =====================================================================
# Get data from all files
data = []
for file in os.listdir(data_folder):
if file.endswith(".txt") and name in file:
data.append(read_data(data_folder + file))
data = sorted(data, key=lambda d : d["name"])
# =====================================================================
# Generate graphs
x = []
y = []
full_data = []
mean = 0.0
variance = 0.0
pdf_name = data_folder + subtitle.replace(" ", "_") + str(level) + ".pdf"
pdf = PdfPages(pdf_name)
for i, d in enumerate(data):
fig = plt.figure(i, figsize=(10, 10))
axis = fig.add_subplot(111)
plt.suptitle("Level {} - {}={}".format(level, subtitle, d["name"]), fontsize=24)
get_histogram(d["data"], axis)
pdf.savefig(figure=fig)
x.append(float(d["name"]))
y.append(d["mean"])
full_data.extend(d["data"])
# This plots the complete histogram and change for each parameter
if len(data) > 1:
fig = plt.figure(10, figsize=(20, 10))
plt.suptitle("Level {} - {}".format(level, subtitle), fontsize=32)
axis_hist = fig.add_subplot(121)
axis_line = fig.add_subplot(122)
# Plot histogram
get_histogram(full_data, axis_hist)
# Plot how the parameter affects the mean
get_plot(x, y, axis_line, subtitle)
pdf.savefig(figure=fig)
pdf.close()