the-last-thing/code/expt/copenhagen.py

175 lines
4.4 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
import sys
sys.path.insert(1, '../lib')
import argparse
2021-09-29 03:12:24 +02:00
import ast
from datetime import datetime
from geopy.distance import distance
import lmdk_bgt
import lmdk_lib
2021-09-29 03:12:24 +02:00
import math
import numpy as np
from matplotlib import pyplot as plt
import time
def main(args):
res_file = '/home/manos/Cloud/Data/Copenhagen/Results.zip'
# Contacts for all users
cont_data = lmdk_lib.load_data(args, 'cont')
# Contacts for landmark's percentages for all users
2021-10-01 21:30:32 +02:00
lmdk_data = lmdk_lib.load_data(args, 'usrs_data')
# The name of the dataset
d = 'Copenhagen'
# The user's id
2021-10-01 21:30:32 +02:00
uid = '449'
# The landmarks percentages
lmdks_pct = [0, 20, 40, 60, 80, 100]
# The privacy budget
2021-10-07 12:54:25 +02:00
epsilon = 1.0
# Number of methods
2021-09-29 03:12:24 +02:00
n = 3
# Width of bars
bar_width = 1/(n + 1)
# The x axis
x_i = np.arange(len(lmdks_pct))
x_margin = bar_width*(n/2 + 1)
print('\n##############################', d, '\n')
# Get user's contacts sequence
2021-10-01 21:30:32 +02:00
seq = cont_data[cont_data[:, 1] == float(uid)][:1000]
# Initialize plot
lmdk_lib.plot_init()
# The x axis
plt.xticks(x_i, np.array(lmdks_pct, int))
2021-10-01 21:57:14 +02:00
plt.xlabel('Landmarks (%)') # Set x axis label.
plt.xlim(x_i.min() - x_margin, x_i.max() + x_margin)
# The y axis
2021-10-01 21:57:14 +02:00
plt.ylabel('Mean absolute error (%)') # Set y axis label.
2021-09-29 03:12:24 +02:00
# plt.yscale('log')
2021-10-01 21:57:14 +02:00
plt.ylim(0, 100)
# Bar offset
x_offset = -(bar_width/2)*(n - 1)
mae_u = np.zeros(len(lmdks_pct))
mae_s = np.zeros(len(lmdks_pct))
mae_a = np.zeros(len(lmdks_pct))
2021-10-01 21:30:32 +02:00
mae_evt = 0
mae_usr = 0
2021-09-29 12:58:40 +02:00
for i, pct in enumerate(lmdks_pct):
# Find landmarks
lmdks = lmdk_lib.find_lmdks_cont(lmdk_data, seq, uid, pct)
2021-09-29 03:12:24 +02:00
for _ in range(args.iter):
# Skip
2021-09-29 12:58:40 +02:00
rls_data_s, bgts_s = lmdk_bgt.skip_cont(seq, lmdks, epsilon)
# lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_s)
2021-10-07 12:32:46 +02:00
mae_s[i] += (lmdk_bgt.mae_cont(rls_data_s)/args.iter)*100
2021-09-29 03:12:24 +02:00
# Uniform
2021-09-29 12:58:40 +02:00
rls_data_u, bgts_u = lmdk_bgt.uniform_cont(seq, lmdks, epsilon)
# lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_u)
2021-10-07 12:32:46 +02:00
mae_u[i] += (lmdk_bgt.mae_cont(rls_data_u)/args.iter)*100
2021-09-29 03:12:24 +02:00
# Adaptive
rls_data_a, _, _ = lmdk_bgt.adaptive_cont(seq, lmdks, epsilon, .5, .5)
2021-10-07 12:32:46 +02:00
mae_a[i] += (lmdk_bgt.mae_cont(rls_data_a)/args.iter)*100
2021-09-29 03:12:24 +02:00
2021-09-29 12:58:40 +02:00
# Calculate once
if pct == lmdks_pct[0]:
2021-10-01 21:30:32 +02:00
# Event
rls_data_evt, _ = lmdk_bgt.uniform_cont(seq, lmdks, epsilon)
2021-10-07 12:32:46 +02:00
mae_evt += (lmdk_bgt.mae_cont(rls_data_evt)/args.iter)*100
elif pct == lmdks_pct[-1]:
2021-10-01 21:30:32 +02:00
# User
rls_data_usr, _ = lmdk_bgt.uniform_cont(seq, lmdks, epsilon)
2021-10-07 12:32:46 +02:00
mae_usr += (lmdk_bgt.mae_cont(rls_data_usr)/args.iter)*100
2021-10-07 12:54:25 +02:00
2021-10-01 21:30:32 +02:00
plt.axhline(
y = mae_evt,
color = '#212121',
2021-09-29 03:12:24 +02:00
linewidth=lmdk_lib.line_width
)
2021-10-01 21:57:14 +02:00
plt.text(x_i[-1] + x_i[-1]*.14, mae_evt - mae_evt*.05, 'event')
2021-09-29 03:12:24 +02:00
2021-10-01 21:30:32 +02:00
plt.axhline(
y = mae_usr,
color = '#616161',
2021-09-29 03:12:24 +02:00
linewidth=lmdk_lib.line_width
)
2021-10-01 21:57:14 +02:00
plt.text(x_i[-1] + x_i[-1]*.14, mae_usr - mae_usr*.05, 'user')
2021-09-29 03:12:24 +02:00
plt.bar(
x_i + x_offset,
mae_s,
bar_width,
label='Skip',
linewidth=lmdk_lib.line_width
)
x_offset += bar_width
plt.bar(
x_i + x_offset,
mae_u,
bar_width,
label='Uniform',
linewidth=lmdk_lib.line_width
)
x_offset += bar_width
plt.bar(
x_i + x_offset,
mae_a,
bar_width,
label='Adaptive',
linewidth=lmdk_lib.line_width
)
x_offset += bar_width
2021-09-29 12:58:40 +02:00
path = str('../../rslt/bgt_cmp/' + d)
2021-09-29 03:12:24 +02:00
# Plot legend
lmdk_lib.plot_legend()
# # Show plot
2021-09-29 03:12:24 +02:00
# plt.show()
2021-09-29 03:23:37 +02:00
# Save plot
lmdk_lib.save_plot(path + '.pdf')
print('[OK]', flush=True)
def parse_args():
'''
Parse arguments.
Optional:
res - The results archive file.
iter - The total iterations.
'''
# Create argument parser.
parser = argparse.ArgumentParser()
# Mandatory arguments.
# Optional arguments.
parser.add_argument('-r', '--res', help='The results archive file.', type=str, default='/home/manos/Cloud/Data/Copenhagen/Results.zip')
parser.add_argument('-i', '--iter', help='The total iterations.', type=int, default=1)
# Parse arguments.
args = parser.parse_args()
return args
if __name__ == '__main__':
try:
start_time = time.time()
main(parse_args())
end_time = time.time()
print('##############################')
2021-10-07 12:32:46 +02:00
print('Time elapsed: %s' % (time.strftime('%H:%M:%S', time.gmtime(end_time - start_time))))
print('##############################')
except KeyboardInterrupt:
print('Interrupted by user.')
exit()