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

188 lines
4.6 KiB
Python

#!/usr/bin/env python3
import sys
sys.path.insert(1, '../lib')
import argparse
from datetime import datetime
from geopy.distance import distance
import lmdk_bgt
import lmdk_lib
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
lmdk_data = lmdk_lib.load_data(args, 'usrs_expt')
# The name of the dataset
d = 'Copenhagen'
# The user's id
uid = '623'
# The landmarks percentages
lmdks_pct = [0, 20, 40, 60, 80, 100]
# The privacy budget
epsilon = 1.0
# Number of methods
n = 6
# 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
seq = cont_data[cont_data[:, 1] == float(uid)]
# Initialize plot
lmdk_lib.plot_init()
# The x axis
plt.xticks(x_i, np.array(lmdks_pct, int))
plt.xlabel('Landmarks percentage') # Set x axis label.
plt.xlim(x_i.min() - x_margin, x_i.max() + x_margin)
# The y axis
plt.ylabel('Mean absolute error (m)') # Set y axis label.
plt.yscale('log')
plt.ylim(1, 100000000)
# 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))
mae_r = np.zeros(len(lmdks_pct))
mae_d = np.zeros(len(lmdks_pct))
mae_i = np.zeros(len(lmdks_pct))
for i, pct in enumerate(lmdks_pct):
# Find landmarks
lmdks = lmdk_lib.find_lmdks_cont(lmdk_data, seq, uid, pct)
print(pct, np.shape(lmdks)[0]/np.shape(seq)[0])
# for _ in range(args.iter):
# # Skip
# rls_data_s, _ = lmdk_bgt.skip(seq, lmdks, epsilon)
# mae_s[i] += lmdk_bgt.mae(seq, rls_data_s)/args.iter
# # Uniform
# rls_data_u, _ = lmdk_bgt.uniform_r(seq, lmdks, epsilon)
# mae_u[i] += lmdk_bgt.mae(seq, rls_data_u)/args.iter
# # Adaptive
# rls_data_a, _, _ = lmdk_bgt.adaptive(seq, lmdks, epsilon, .5, .5)
# mae_a[i] += lmdk_bgt.mae(seq, rls_data_a)/args.iter
# # Sample
# rls_data_r, _, _ = lmdk_bgt.sample(seq, lmdks, epsilon)
# mae_r[i] += lmdk_bgt.mae(seq, rls_data_r)/args.iter
# # Discount
# rls_data_d, _, _ = lmdk_bgt.discount(seq, lmdks, epsilon)
# mae_d[i] += lmdk_bgt.mae(seq, rls_data_d)/args.iter
# # Incremental
# rls_data_i, _, _ = lmdk_bgt.incremental(seq, lmdks, epsilon, .5)
# mae_i[i] += lmdk_bgt.mae(seq, rls_data_i)/args.iter
# plt.bar(
# x_i + x_offset,
# mae_s,
# bar_width,
# label='Skip',
# linewidth=lmdk_lib.line_width
# )
# x_offset += bar_width
# # Plot bars
# 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
# plt.bar(
# x_i + x_offset,
# mae_r,
# bar_width,
# label='Sample',
# linewidth=lmdk_lib.line_width
# )
# x_offset += bar_width
# plt.bar(
# x_i + x_offset,
# mae_d,
# bar_width,
# label='Discount',
# linewidth=lmdk_lib.line_width
# )
# x_offset += bar_width
# plt.bar(
# x_i + x_offset,
# mae_i,
# bar_width,
# label='Incremental',
# linewidth=lmdk_lib.line_width
# )
# x_offset += bar_width
# path = str('rslt/bgt_cmp/' + d)
# # Plot legend
# lmdk_lib.plot_legend()
# # Show plot
# # plt.show()
# # 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('##############################')
print('Time : %.4fs' % (end_time - start_time))
print('##############################')
except KeyboardInterrupt:
print('Interrupted by user.')
exit()