Skip to content
Snippets Groups Projects
Commit a5b0923d authored by NGUYEN Do Duc Anh's avatar NGUYEN Do Duc Anh
Browse files

update plot result

parent 4d6fac25
No related branches found
No related tags found
No related merge requests found
import random
import matplotlib.pyplot as plt
import ast, sys, os, json
import numpy as np
import scipy.stats
NUM_FILE = 1
def draw_bar(number_pc_measured, included_string):
start_name = "measurement-" + number_pc_measured + "-"
directory = os.getcwd()
y_values = []
# Iterate over files in the directory
for filename in os.listdir(directory):
# Check if the file starts with "measure" and ends with ".txt"
if filename.startswith(start_name) and filename.endswith(".txt") and included_string in filename:
with open(filename, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
# Now you can work with the array as needed
if len(data) != int(number_pc_measured):
print(f"Array from {filename}:")
else:
y_values.append(data[-1][0] - data[0][0])
x_values = list(map(int, range(1, len(y_values) + 1)))
plt.bar(x_values, y_values)
# Add labels and title
plt.xlabel('Run time')
plt.xticks(x_values)
# plt.yscale('log')
plt.ylim(0)
plt.ylabel('Time (s)')
plt.title('Infected PC Time each run')
def draw_bar_special(number_pc_measured, included_string):
start_name = "measurement-" + number_pc_measured + "-"
directory = os.getcwd()
y_values = []
y_second_values = []
# Iterate over files in the directory
for filename in os.listdir(directory):
# Check if the file starts with "measure" and ends with ".txt"
if filename.startswith(start_name) and filename.endswith(".txt") and included_string in filename:
with open(filename, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
# Now you can work with the array as needed
if len(data) != int(number_pc_measured):
print(f"Array from {filename}:")
else:
y_values.append(data[-1][0] - data[0][0])
y_second_values.append(data[1][0] - data[0][0])
x_values = list(map(int, range(1, len(y_values) + 1)))
# Plot the blue bars on top of the red bars
plt.bar(x_values, y_values, label='Time for ' + number_pc_measured + 'th PC infection')
# Plot the red bars
if number_pc_measured != '2':
plt.bar(x_values, y_second_values, color='red', label='Time for 2nd PC infection')
# Add labels and title
plt.xlabel('Run time', fontsize=18)
plt.xticks(x_values, fontsize=18)
plt.yticks(fontsize=18)
# plt.yscale('log')
plt.ylim(0)
plt.ylabel('Time (s)', fontsize=18)
# plt.title('Infected PC Time each run')
plt.legend(fontsize=18)
def plot_graph(file_path):
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
# Extract x and y values from the data
ip_values = [entry[1] for entry in data]
x_values = [entry[0] for entry in data]
first_x = x_values[0]
x_values = [i - first_x for i in x_values]
y_values = list(map(int, range(1, len(data) + 1)))
if True:
plt.xlim(min(x_values), max(x_values) + 50)
# plt.xticks(x_values)
plt.xticks(list(plt.xticks()[0]) + [x_values[-1]], list(plt.xticks()[0]) + [round(x_values[-1])], fontsize=18)
# plt.xticks(rotation=45)
else:
plt.xscale('log')
x_values = x_values[1:]
y_values = y_values[1:]
ip_values = ip_values[1:]
# Plot the graph with lines connecting the points
plt.ylabel('Infected PC number', fontsize=18)
plt.xlabel('Time (s)', fontsize=18)
# plt.title('Infected PC over time')
plt.plot(x_values, y_values, marker='o', markersize=5, label='IP address')
last_x = x_values[-1]
last_y = y_values[-1]
plt.axvline(x=last_x, linestyle='--', color='gray')
# plt.axhline(y=last_y, linestyle='--', color='gray')
for i in range(len(x_values)):
plt.text(x_values[i], y_values[i], f'{ip_values[i]}', ha='right', va='bottom', fontsize=14)
plt.subplots_adjust(left=0.045, right=0.975, top=0.995, bottom=0.070)
plt.yticks(fontsize=18)
# plt.gca().yaxis.set_major_locator(MultipleLocator(10)) # Set major ticks every 10 units
plt.legend(loc='upper left', fontsize=18)
def plot_graph_with_infector(file_path):
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
temp_path = file_path.split("-")
infector_path = "infector" + "-" + temp_path[1] + "-" + temp_path[2] + "-" + temp_path[3]
with open(infector_path, 'r') as file:
contents = file.read()
infector_data = ast.literal_eval(contents)
# Extract x and y values from the data
infector_ips = [entry.split(",") for entry in infector_data]
map_point = {}
y_values_infector = [0]
# Extract x and y values from the data
ip_values = [entry[1] for entry in data]
x_values = [entry[0] for entry in data]
first_x = x_values[0]
x_values = [i - first_x for i in x_values]
y_values = list(map(int, range(1, len(data) + 1)))
for ip in ip_values:
is_exist = False
temp = y_values_infector[-1]
for infector_ip in infector_ips:
if ip == infector_ip[0]:
if infector_ip[1] in map_point:
y_values_infector.append(temp)
else:
y_values_infector.append(temp + 1)
map_point[infector_ip[1]] = 1
is_exist = True
break
if (not is_exist) and (ip != '10.0.0.1'):
print(ip)
if '10.0.0.1' in map_point:
y_values_infector.append(temp)
else:
y_values_infector.append(temp + 1)
map_point['10.0.0.1'] = 1
# print(y_values_infector)
if True:
plt.xlim(min(x_values), max(x_values) + 50)
# plt.xticks(x_values)
# plt.xticks(list(plt.xticks()[0]) + [x_values[-1]], list(plt.xticks()[0]) + [round(x_values[-1])], fontsize=18)
# plt.xticks(rotation=45)
else:
plt.xscale('log')
x_values = x_values[1:]
y_values = y_values[1:]
ip_values = ip_values[1:]
# Plot the graph with lines connecting the points
plt.ylabel('Number of PCs', fontsize=18)
plt.xlabel('Time (s)', fontsize=18)
# plt.title('Infected PC over time')
plt.plot(x_values, y_values, marker='o', markersize=5, label='Infected PC')
y_values_safe = [(len(y_values) - item) for item in y_values]
plt.plot(x_values, y_values_safe, marker='o', markersize=5, label='Safe PC')
plt.plot(x_values, y_values_infector, marker='o', markersize=5, label='Infector PC')
last_x = x_values[-1]
last_y = y_values[-1]
plt.axvline(x=last_x, linestyle='--', color='gray')
# plt.axhline(y=last_y, linestyle='--', color='gray')
for i in range(len(x_values)):
plt.text(x_values[i], y_values[i], f'{ip_values[i]}', ha='right', va='bottom', fontsize=14)
plt.subplots_adjust(left=0.045, right=0.975, top=0.995, bottom=0.070)
plt.ylim(0)
plt.yticks(fontsize=18)
# plt.gca().yaxis.set_major_locator(MultipleLocator(10)) # Set major ticks every 10 units
plt.legend(loc='upper left', fontsize=18)
def plot_graph_with_infector_average(number_pc):
y_values_infector_total = []
x_values_total = []
x_values_min = []
x_values_max = []
standard_deviation = []
for i in range(11, 14):
file_path = "measurement-" + number_pc + "-" + str(i) + "-SUB.txt"
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
infector_path = "infector" + "-" + number_pc + "-" + str(i) + "-SUB.txt"
with open(infector_path, 'r') as file:
contents = file.read()
infector_data = ast.literal_eval(contents)
# Extract x and y values from the data
infector_ips = [entry.split(",") for entry in infector_data]
map_point = {}
y_values_infector = [0]
# Extract x and y values from the data
ip_values = [entry[1] for entry in data]
x_values = [entry[0] for entry in data]
first_x = x_values[0]
x_values = [i - first_x for i in x_values]
y_values = list(map(int, range(1, len(data) + 1)))
for ip in ip_values:
is_exist = False
temp = y_values_infector[-1]
for infector_ip in infector_ips:
if ip == infector_ip[0]:
if infector_ip[1] in map_point:
y_values_infector.append(temp)
else:
y_values_infector.append(temp + 1)
map_point[infector_ip[1]] = 1
is_exist = True
break
if (not is_exist) and (ip != '10.0.0.1'):
print(ip)
if '10.0.0.1' in map_point:
y_values_infector.append(temp)
else:
y_values_infector.append(temp + 1)
map_point['10.0.0.1'] = 1
if len(x_values_total) != len(x_values):
for i in range(len(x_values)):
x_values_min.append(10000)
x_values_max.append(-1)
x_values_total.append(0)
for i in range(len(x_values)):
x_values_total[i] += x_values[i]
if x_values_min[i] > x_values[i]:
x_values_min[i] = x_values[i]
if x_values_max[i] < x_values[i]:
x_values_max[i] = x_values[i]
# if len(y_values_infector_total) != len(y_values_infector):
# for i in range(len(y_values_infector)):
# y_values_infector_total[i] = 0
# for i in range(len(y_values_infector)):
# y_values_infector_total[i] += y_values_infector[i]
# print(y_values_infector)
for i in range(len(x_values_total)):
x_values_total[i] = x_values_total[i] / 3.0
if True:
plt.xlim(min(x_values), max(x_values) + 50)
# plt.xticks(x_values)
# plt.xticks(list(plt.xticks()[0]) + [x_values[-1]], list(plt.xticks()[0]) + [round(x_values[-1])], fontsize=18)
# plt.xticks(rotation=45)
else:
plt.xscale('log')
x_values = x_values[1:]
y_values = y_values[1:]
ip_values = ip_values[1:]
# Plot the graph with lines connecting the points
plt.ylabel('Number of PCs', fontsize=18)
plt.xlabel('Time (s)', fontsize=18)
# plt.title('Infected PC over time')
# plt.plot(x_values, y_values, marker='o', markersize=5, label='Infected PC')
y_values_safe = [(len(y_values) - item) for item in y_values]
# plt.plot(x_values_total, y_values_safe, marker='o', markersize=5, label='Average Time Safe PC')
plt.plot(x_values_total, y_values_infector, marker='o', markersize=5, label='Infector PC')
plt.plot(x_values_total, y_values, marker='o', markersize=5, label='Average Time Infected PC')
plt.plot(x_values_max, y_values, linestyle='dashed', markersize=5, label='Upper Time Infected PC')
plt.plot(x_values_min, y_values, linestyle='dashed', markersize=5, label='Lower Time Infected PC')
# last_x = x_values[-1]
# last_y = y_values[-1]
a = [200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800]
for i in a:
plt.axvline(x=i, linestyle='--', color='gray')
# plt.axhline(y=last_y, linestyle='--', color='gray')
# for i in range(len(x_values)):
# plt.text(x_values[i], y_values[i], f'{ip_values[i]}', ha='right', va='bottom', fontsize=14)
plt.subplots_adjust(left=0.045, right=0.975, top=0.995, bottom=0.070)
plt.ylim(0)
plt.yticks(fontsize=18)
# plt.gca().yaxis.set_major_locator(MultipleLocator(10)) # Set major ticks every 10 units
plt.legend(loc='upper left', fontsize=18)
def draw_plot(number_pc_measured, malware_name):
global NUM_FILE
# pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
pc_number_values = [1] + list(range(10, int(number_pc_measured) + 1, 10))
# print(pc_number_values)
x_values = [[] for _ in range(int(number_pc_measured))]
for i in range(1, NUM_FILE + 1):
file_path = "measurement-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
ip_values = [entry[1] for entry in data]
time_values = [entry[0] for entry in data]
first_time = time_values[0]
time_values = [i - first_time for i in time_values]
for i in range(len(time_values)):
x_values[i].append(time_values[i])
# pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
print(x_values)
# boxplot = plt.boxplot(x_values, vert=False, showfliers=False)
lower_bounds = []
upper_bounds = []
average = []
for x in x_values:
average.append(np.mean(x))
lower_bounds.append(min(x))
upper_bounds.append(max(x))
# plt.plot(lower_bounds, [i for i in range(int(number_pc_measured))], linestyle='dashed', label='Lower Bound')
# plt.plot(upper_bounds, [i for i in range(int(number_pc_measured))], linestyle='dashed', label='Upper Bound')
# plt.plot(average, [i for i in range(int(number_pc_measured))], label='Average')
# plt.legend(fontsize=18)
# plt.fill_betweenx(range(len(average)), lower_bounds, upper_bounds, color='gray', alpha=0.3)
plt.errorbar(average, [i for i in range(int(number_pc_measured))],
xerr=[[average[i] - lower_bounds[i] for i in range(len(lower_bounds))], [upper_bounds[i] - average[i] for i in range(len(upper_bounds))]], color='orange',
capsize=7, linestyle='', fmt='o', ecolor='black', label='Average')
printMean(x_values[-1])
# print(f"Confidence interval: {mean} and {margin_of_error}")
plt.ylabel('Number of PCs', fontsize=18)
plt.xlabel('Time (s)', fontsize=18)
plt.subplots_adjust(left=0.045, right=0.995, top=0.995, bottom=0.070)
# plt.ylim(1)
plt.xticks(fontsize=18)
plt.yticks(pc_number_values, [str(i) for i in pc_number_values], fontsize=18)
def draw_whisker_plot(number_pc_measured, malware_name):
global NUM_FILE
# pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
pc_number_values = [1] + list(range(10, int(number_pc_measured) + 1, 10))
# print(pc_number_values)
x_values = [[] for _ in range(int(number_pc_measured))]
for i in range(1, NUM_FILE + 1):
file_path = "measurement-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
ip_values = [entry[1] for entry in data]
time_values = [entry[0] for entry in data]
first_time = time_values[0]
time_values = [i - first_time for i in time_values]
for i in range(len(time_values)):
x_values[i].append(time_values[i])
# pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
print(x_values)
boxplot = plt.boxplot(x_values, vert=False, showfliers=False)
printMean(x_values[-1])
# print(f"Confidence interval: {mean} and {margin_of_error}")
plt.ylabel('Number of PCs', fontsize=18)
plt.xlabel('Time (s)', fontsize=18)
plt.subplots_adjust(left=0.045, right=0.995, top=0.995, bottom=0.070)
# plt.ylim(1)
plt.xticks(fontsize=18)
plt.yticks(pc_number_values, [str(i) for i in pc_number_values], fontsize=18)
def mean_confidence_interval(data, confidence=0.95):
a = 1.0 * np.array(data)
n = len(a)
m, se = np.mean(a), scipy.stats.sem(a)
h = se * scipy.stats.t.ppf((1 + confidence) / 2., n - 1)
return m, m - h, m + h, h
def draw_confidence_inter_time(number_pc_measured, malware_name):
global NUM_FILE
num_time_period = 100
Num = int(2500 / num_time_period)
# pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
period_number_values = list(range(num_time_period, num_time_period * Num + num_time_period, num_time_period))
# print(pc_number_values)
x_values = [[] for _ in range(int(Num))]
for i in range(1, NUM_FILE + 1):
file_path = "measurement-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
ip_values = [entry[1] for entry in data]
time_values = [entry[0] for entry in data]
first_time = time_values[0]
time_values = [i - first_time for i in time_values]
temp_values = [0 for _ in range(int(Num))]
for each_time in time_values:
period_time = int(each_time // num_time_period)
print(period_time, each_time, num_time_period)
temp_values[period_time] += 1
for i in range(Num):
x_values[i].append(temp_values[i])
# pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
print(x_values)
means = []
lower_bounds = []
upper_bounds = []
errors = []
for x in x_values:
mean, lower_bound, upper_bound, error = mean_confidence_interval(x)
errors.append(error)
means.append(mean)
lower_bounds.append(lower_bound)
upper_bounds.append(upper_bound)
temp_value = [i for i in range(1, Num + 1)]
plt.plot(temp_value, means, marker='o', label='Average')
plt.errorbar(temp_value, means, yerr=errors, fmt='o', color='k', capsize=10)
# plt.plot(temp_value, lower_bounds, linestyle='dashed', label='Lower Bound')
# plt.plot(temp_value, upper_bounds, linestyle='dashed', label='Upper Bound')
# plt.fill_between(range(len(x_values)), lower_bounds, upper_bounds, color='gray', alpha=0.3)
# plt.boxplot(x_values, showfliers=False)
plt.ylabel('Number of PCs', fontsize=18)
plt.xlabel('Each ' + str(num_time_period) + '-second Period', fontsize=18)
plt.subplots_adjust(left=0.045, right=0.995, top=0.995, bottom=0.070)
# plt.ylim(1)
plt.yticks(fontsize=18)
plt.xticks([i for i in range(1, Num + 1)], [str(i) for i in period_number_values], fontsize=15)
plt.legend(fontsize=18)
def draw_whisker_plot_inter_time(number_pc_measured, malware_name):
global NUM_FILE
num_time_period = 50
Num = int(1800 / num_time_period)
# pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
period_number_values = list(range(num_time_period, num_time_period * Num + num_time_period, num_time_period))
# print(pc_number_values)
x_values = [[] for _ in range(int(Num))]
for i in range(1, NUM_FILE + 1):
file_path = "measurement-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
ip_values = [entry[1] for entry in data]
time_values = [entry[0] for entry in data]
first_time = time_values[0]
time_values = [i - first_time for i in time_values]
temp_values = [0 for _ in range(int(Num))]
for each_time in time_values:
period_time = int(each_time // num_time_period)
print(period_time, each_time, num_time_period)
temp_values[period_time] += 1
for i in range(Num):
x_values[i].append(temp_values[i])
# pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
print(x_values)
plt.boxplot(x_values, showfliers=False)
plt.ylabel('Number of PCs', fontsize=18)
plt.xlabel('Each ' + number_pc_measured + '-second Period', fontsize=18)
plt.subplots_adjust(left=0.045, right=0.995, top=0.995, bottom=0.070)
# plt.ylim(1)
plt.yticks(fontsize=18)
plt.xticks([i for i in range(1, Num + 1)], [str(i) for i in period_number_values], fontsize=15)
def draw_confidence_ip_distance(number_pc_measured, malware_name):
global NUM_FILE
x_values = [[] for _ in range(1, int(number_pc_measured))]
for i in range(1, NUM_FILE + 1):
map_distance = {}
infector_path = "infector" + "-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
with open(infector_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
victim_infector_ips = [entry.split(",") for entry in data]
for ips in victim_infector_ips:
distance = abs(int(ips[0].split(".")[3]) - int(ips[1].split(".")[3]))
if distance in map_distance:
map_distance[distance] += 1
else:
map_distance[distance] = 1
for i in range(len(x_values)):
if (i + 1) in map_distance:
x_values[i].append(map_distance[i + 1])
else:
x_values[i].append(0)
y_values = [str(i) for i in range(1, int(number_pc_measured))]
print(y_values)
print(x_values)
means = []
lower_bounds = []
upper_bounds = []
errors = []
totalx = [[0] for i in range(int(number_pc_measured) - 1)]
for i in range(len(x_values)):
totalx[i] = sum(x_values[i])
# mean, lower_bound, upper_bound, error = mean_confidence_interval(x)
# print(mean, lower_bound, upper_bound, error)
# errors.append(error)
# means.append(mean)
# lower_bounds.append(lower_bound)
# upper_bounds.append(upper_bound)
# plt.plot(y_values, means, marker='o', label='Average')
# plt.bar(y_values, means)
# plt.errorbar(y_values, means, yerr=errors, fmt = 'o', color = 'k', capsize=10)
plt.bar(y_values, totalx)
# plt.plot(y_values, lower_bounds, linestyle='dashed', label='Lower Bound')
# plt.plot(y_values, upper_bounds, linestyle='dashed', label='Upper Bound')
# plt.fill_between(range(len(x_values)), lower_bounds, upper_bounds, color='gray', alpha=0.3)
plt.ylabel('Occurences', fontsize=18)
plt.xlabel('Distance', fontsize=18)
plt.subplots_adjust(left=0.03, right=0.995, top=0.985, bottom=0.070)
locs, labels = plt.yticks()
yint = [int(each) for each in locs]
plt.yticks(yint, fontsize=18)
plt.xticks(fontsize=18)
# plt.legend(fontsize=18)
def draw_whisker_plot_ip_distance(number_pc_measured, malware_name):
global NUM_FILE
x_values = [[] for _ in range(1, int(number_pc_measured))]
for i in range(1, NUM_FILE + 1):
map_distance = {}
infector_path = "infector" + "-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
with open(infector_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
victim_infector_ips = [entry.split(",") for entry in data]
for ips in victim_infector_ips:
distance = abs(int(ips[0].split(".")[3]) - int(ips[1].split(".")[3]))
if distance in map_distance:
map_distance[distance] += 1
else:
map_distance[distance] = 1
for i in range(len(x_values)):
if (i + 1) in map_distance:
x_values[i].append(map_distance[i + 1])
else:
x_values[i].append(0)
y_values = [i for i in range(1, int(number_pc_measured))]
print(y_values)
print(x_values)
plt.boxplot(x_values, showfliers=False)
plt.ylabel('Occurences', fontsize=18)
plt.xlabel('Distance', fontsize=18)
plt.subplots_adjust(left=0.03, right=0.995, top=0.995, bottom=0.070)
locs, labels = plt.yticks()
yint = [int(each) for each in locs]
plt.yticks(yint, fontsize=18)
plt.xticks(y_values, fontsize=18)
def draw_confidence_infector(number_pc_measured, malware_name):
global NUM_FILE
pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
x_values = [[] for _ in range(int(number_pc_measured))]
x_infector_values = [[] for _ in range(int(number_pc_measured))]
for i in range(1, NUM_FILE + 1):
infector_map = {}
infecting_map = {}
file_path = "infecting" + "-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
with open(file_path, 'r') as file:
infecting_map = json.load(file)
file_path = "infector" + "-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
for entry in data:
victim_infector_ip = entry.split(",")
if victim_infector_ip[0] not in infector_map:
infector_map[victim_infector_ip[0]] = victim_infector_ip[1]
else:
print(victim_infector_ip[0])
print("error")
input()
file_path = "measurement-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
ip_values = [entry[1] for entry in data]
time_values = [entry[0] for entry in data]
first_time = time_values[0]
time_values = [i - first_time for i in time_values]
map_point_infector = {}
number_infector = 0
for i in range(len(ip_values)):
ip = ip_values[i]
if ip in infecting_map:
number_infecting = len(infecting_map[ip])
else:
if ip != "10.0.0.1":
print("1", ip)
number_infecting = 0
if ip in infector_map:
if infector_map[ip] not in map_point_infector:
map_point_infector[infector_map[ip]] = 1
number_infector += 1
if infector_map[ip] in infecting_map[ip]:
number_infecting -= 1
else:
pass
# print("3",ip)
elif ip != "10.0.0.1":
print("2", ip)
x_values[i].append(number_infecting)
x_infector_values[i].append(number_infector)
print(x_values)
print(x_infector_values)
means = []
lower_bounds = []
upper_bounds = []
errors = []
for x in x_infector_values:
mean, lower_bound, upper_bound, error = mean_confidence_interval(x)
errors.append(error)
means.append(mean)
lower_bounds.append(lower_bound)
upper_bounds.append(upper_bound)
plt.plot(pc_number_values, means, marker='o', label='Average Number of Effective Infectors')
plt.errorbar(pc_number_values, means, yerr=errors, fmt='o', color='black', capsize=7)
means = []
lower_bounds = []
upper_bounds = []
errors = []
for x in x_values:
mean, lower_bound, upper_bound, error = mean_confidence_interval(x)
errors.append(error)
means.append(mean)
lower_bounds.append(lower_bound)
upper_bounds.append(upper_bound)
plt.plot(pc_number_values, means, marker='o', label='Average Number of Attempted Infectors')
plt.errorbar(pc_number_values, means, yerr=errors, fmt='o', color='green', capsize=5)
# plt.boxplot(x_values, positions=[x - 0.25 for x in pc_number_values],widths=0.4,patch_artist=True, showfliers=False, boxprops=dict(facecolor="skyblue"),medianprops=dict(color="red"))
# plt.boxplot(x_infector_values, positions=[x + 0.25 for x in pc_number_values],widths=0.4, patch_artist=True,showfliers=False, boxprops=dict(facecolor="lightgreen"))
plt.ylabel('Number of Infectors', fontsize=18)
plt.xlabel('Infection Number', fontsize=18)
plt.subplots_adjust(left=0.04, right=0.995, top=0.995, bottom=0.070)
# plt.ylim(1)
plt.yticks(fontsize=18)
plt.xticks(pc_number_values, [str(i) for i in pc_number_values], fontsize=18)
# legend_handles = [plt.Rectangle((0,0),1,1,fc="skyblue", edgecolor = 'black'), plt.Rectangle((0,0),1,1,fc="lightgreen", edgecolor = 'black')]
# legend_labels = ['Number of Attempted Infectors', 'Number of Effective Infectors']
plt.legend(fontsize=18)
def draw_whisker_plot_infector(number_pc_measured, malware_name):
global NUM_FILE
pc_number_values = list(map(int, range(1, int(number_pc_measured) + 1)))
x_values = [[] for _ in range(int(number_pc_measured))]
x_infector_values = [[] for _ in range(int(number_pc_measured))]
for i in range(1, NUM_FILE + 1):
infector_map = {}
infecting_map = {}
file_path = "infecting" + "-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
with open(file_path, 'r') as file:
infecting_map = json.load(file)
file_path = "infector" + "-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
for entry in data:
victim_infector_ip = entry.split(",")
if victim_infector_ip[0] not in infector_map:
infector_map[victim_infector_ip[0]] = victim_infector_ip[1]
else:
print(victim_infector_ip[0])
print("error")
input()
file_path = "measurement-" + number_pc_measured + "-" + malware_name + "-" + str(i) + ".txt"
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
ip_values = [entry[1] for entry in data]
time_values = [entry[0] for entry in data]
first_time = time_values[0]
time_values = [i - first_time for i in time_values]
map_point_infector = {}
number_infector = 0
for i in range(len(ip_values)):
ip = ip_values[i]
if ip in infecting_map:
number_infecting = len(infecting_map[ip])
else:
if ip != "10.0.0.1":
print("1", ip)
number_infecting = 0
if ip in infector_map:
if infector_map[ip] not in map_point_infector:
map_point_infector[infector_map[ip]] = 1
number_infector += 1
if infector_map[ip] in infecting_map[ip]:
number_infecting -= 1
else:
pass
# print("3",ip)
elif ip != "10.0.0.1":
print("2", ip)
x_values[i].append(number_infecting)
x_infector_values[i].append(number_infector)
print(x_values)
print(x_infector_values)
plt.boxplot(x_values, positions=[x - 0.25 for x in pc_number_values], widths=0.4, patch_artist=True, showfliers=False, boxprops=dict(facecolor="skyblue"),
medianprops=dict(color="red"))
plt.boxplot(x_infector_values, positions=[x + 0.25 for x in pc_number_values], widths=0.4, patch_artist=True, showfliers=False, boxprops=dict(facecolor="lightgreen"))
plt.ylabel('Number of Infectors', fontsize=18)
plt.xlabel('Infection Number', fontsize=18)
plt.subplots_adjust(left=0.04, right=0.995, top=0.995, bottom=0.070)
# plt.ylim(1)
plt.yticks(fontsize=18)
plt.xticks(pc_number_values, [str(i) for i in pc_number_values], fontsize=18)
legend_handles = [plt.Rectangle((0, 0), 1, 1, fc="skyblue", edgecolor='black'), plt.Rectangle((0, 0), 1, 1, fc="lightgreen", edgecolor='black')]
legend_labels = ['Number of Attempted Infectors', 'Number of Effective Infectors']
plt.legend(legend_handles, legend_labels, fontsize=18)
def printMean(x):
mean, lower_bound, upper_bound, error = mean_confidence_interval(x)
print("Mean:", mean)
print("Lower Bound:", lower_bound)
print("Upper Bound:", upper_bound)
print("Error:", error)
def read_data():
array_def_infect = []
array_def_notif = []
array_nodef_infect = []
for i in range(1, 2):
file_path = "measurement-" + str(i) + ".txt"
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
array_def_infect.append(data)
print(data)
file_path = "deploy_notif-" + str(i) + ".txt"
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
array_def_notif.append(data)
print(data)
file_path = "measurement-infect-" + str(i) + ".txt"
# Read the contents of the file
with open(file_path, 'r') as file:
contents = file.read()
data = ast.literal_eval(contents)
array_nodef_infect.append(data)
print(data)
return array_def_infect, array_def_notif, array_nodef_infect
def plot(number_pc):
array_def_infect, array_def_notif, array_nodef_infect = read_data()
pc_number_values = [1] + list(range(10, int(number_pc) + 1, 10))
x_values = [[] for _ in range(int(number_pc))]
for item in array_nodef_infect:
ip_values = [entry[1] for entry in item]
time_values = [entry[0] for entry in item]
first_time = time_values[0]
time_values = [i - first_time for i in time_values]
for i in range(len(time_values)):
x_values[i].append(time_values[i])
# print(x_values)
lower_bounds = []
upper_bounds = []
average = []
for x in x_values:
average.append(np.mean(x))
lower_bounds.append(min(x))
upper_bounds.append(max(x))
# plt.plot(lower_bounds, [i for i in range(int(number_pc))], linestyle='dashed', label='Lower Bound')
# plt.plot(upper_bounds, [i for i in range(int(number_pc))], linestyle='dashed', label='Upper Bound')
plt.plot(average, [i for i in range(int(number_pc))])
# plt.fill_betweenx(range(len(average)), lower_bounds, upper_bounds, color='gray', alpha=0.3)
plt.errorbar(average, [i for i in range(int(number_pc))],
xerr=[[average[i] - lower_bounds[i] for i in range(len(lower_bounds))], [upper_bounds[i] - average[i] for i in range(len(upper_bounds))]], color='orange',
capsize=7, linestyle='', fmt='o', ecolor='black', label='Average')
printMean(x_values[-1])
# print(f"Confidence interval: {mean} and {margin_of_error}")
plt.ylabel('Number of PCs', fontsize=18)
plt.xlabel('Time (s)', fontsize=18)
plt.subplots_adjust(left=0.045, right=0.995, top=0.995, bottom=0.070)
# plt.ylim(1)
plt.xticks(fontsize=18)
plt.yticks(pc_number_values, [str(i) for i in pc_number_values], fontsize=18)
attack_stop_time = array_def_notif[0][0][2] - array_def_infect[0][0][0]
plt.axvline(x=attack_stop_time, color='red', linestyle='--', label="Attack Stopped")
plt.legend(fontsize=18)
# file_path = 'measurement-1.txt' # Update this with your file path
# if len(sys.argv) > 2:
# func_id = sys.argv[1]
# if func_id == "1":
# file_path = sys.argv[2]
# plot_graph(file_path)
# elif func_id == "2":
# number_pc_measured = sys.argv[2]
# malware_name = sys.argv[3]
# # draw_whisker_plot(number_pc_measured, malware_name)
# draw_plot(number_pc_measured, malware_name)
# elif func_id == "3":
# number_pc_measured = sys.argv[2]
# malware_name = sys.argv[3]
# # draw_whisker_plot_infector(number_pc_measured, malware_name)
# draw_confidence_infector(number_pc_measured, malware_name)
# elif func_id == "4":
# number_pc_measured = sys.argv[2]
# malware_name = sys.argv[3]
# # draw_whisker_plot_inter_time(number_pc_measured, malware_name)
# draw_confidence_inter_time(number_pc_measured, malware_name)
# elif func_id == "5":
# number_pc_measured = sys.argv[2]
# malware_name = sys.argv[3]
# # draw_whisker_plot_ip_distance(number_pc_measured, malware_name)
# draw_confidence_ip_distance(number_pc_measured, malware_name)
# draw_bar(number_pc_measured, included_string)
# draw_bar_special(number_pc_measured, included_string)
# elif func_id == "3":
# file_path = sys.argv[2]
# plot_graph_with_infector(file_path)
# elif func_id == "4":
# number_pc = sys.argv[2]
# plot_graph_with_infector_average(number_pc)
if __name__ == "__main__":
plot(12)
plt.get_current_fig_manager().window.showMaximized()
plt.show()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment