From a1454c5a98eb50d0a1dcbedc65ef3708a392be95 Mon Sep 17 00:00:00 2001 From: Manos Date: Wed, 13 Oct 2021 09:03:36 +0200 Subject: [PATCH] code: Several fixes --- code/expt/copenhagen-sel.py | 6 ++---- code/expt/hue-sel.py | 9 ++++----- code/expt/lmdk_sel_cmp.py | 19 ++++--------------- code/expt/t-drive-sel.py | 8 ++++---- code/lib/lmdk_lib.py | 4 +++- code/lib/lmdk_sel.py | 4 ++-- 6 files changed, 19 insertions(+), 31 deletions(-) diff --git a/code/expt/copenhagen-sel.py b/code/expt/copenhagen-sel.py index ab17d31..6620097 100644 --- a/code/expt/copenhagen-sel.py +++ b/code/expt/copenhagen-sel.py @@ -71,13 +71,11 @@ def main(args): lmdks_sel, eps_out = lmdk_sel.find_lmdks(seq, lmdks, epsilon) # Skip - rls_data_s, bgts_s = lmdk_bgt.skip_cont(seq, lmdks_sel, eps_out) - # lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_s) + rls_data_s, _ = lmdk_bgt.skip_cont(seq, lmdks_sel, eps_out) mae_s[i] += (lmdk_bgt.mae_cont(rls_data_s)/args.iter)*100 # Uniform - rls_data_u, bgts_u = lmdk_bgt.uniform_cont(seq, lmdks_sel, eps_out) - # lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_u) + rls_data_u, _ = lmdk_bgt.uniform_cont(seq, lmdks_sel, eps_out) mae_u[i] += (lmdk_bgt.mae_cont(rls_data_u)/args.iter)*100 # Adaptive diff --git a/code/expt/hue-sel.py b/code/expt/hue-sel.py index 930bd54..5a0ce9b 100644 --- a/code/expt/hue-sel.py +++ b/code/expt/hue-sel.py @@ -64,19 +64,18 @@ def main(args): for _ in range(args.iter): - lmdks, eps_out = lmdk_sel.find_lmdks(seq, lmdks, epsilon) + lmdks_sel, eps_out = lmdk_sel.find_lmdks(seq, lmdks, epsilon) # Skip - rls_data_s, bgts_s = lmdk_bgt.skip_cons(seq, lmdks, eps_out) - # lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_s) + rls_data_s, _ = lmdk_bgt.skip_cons(seq, lmdks_sel, eps_out) mae_s[i] += lmdk_bgt.mae_cons(seq, rls_data_s)/args.iter # Uniform - rls_data_u, bgts_u = lmdk_bgt.uniform_cons(seq, lmdks, eps_out) + rls_data_u, _ = lmdk_bgt.uniform_cons(seq, lmdks_sel, eps_out) mae_u[i] += lmdk_bgt.mae_cons(seq, rls_data_u)/args.iter # Adaptive - rls_data_a, _, _ = lmdk_bgt.adaptive_cons(seq, lmdks, eps_out, .5, .5) + rls_data_a, _, _ = lmdk_bgt.adaptive_cons(seq, lmdks_sel, eps_out, .5, .5) mae_a[i] += lmdk_bgt.mae_cons(seq, rls_data_a)/args.iter # Calculate once diff --git a/code/expt/lmdk_sel_cmp.py b/code/expt/lmdk_sel_cmp.py index ce466bc..ea109f1 100644 --- a/code/expt/lmdk_sel_cmp.py +++ b/code/expt/lmdk_sel_cmp.py @@ -57,8 +57,6 @@ def main(args): print('(%d/%d) %s... ' %(d_i + 1, len(dist_type), title), end='', flush=True) mae = np.zeros(len(lmdk_n)) for n_i, n in enumerate(lmdk_n): - if n == lmdk_n[-1]: - break for r in range(args.iter): lmdks = lmdk_lib.get_lmdks(seq, n, d) hist, h = lmdk_lib.get_hist(seq, lmdks) @@ -67,20 +65,11 @@ def main(args): res, _ = exp_mech.exponential(hist, opts, exp_mech.score, delta, epsilon) mae[n_i] += lmdk_lib.get_norm(hist, res)/args.iter # Euclidean # mae[n_i] += lmdk_lib.get_emd(hist, res)/args.iter # Wasserstein - mae = mae/21 # Euclidean - # mae = mae/11.75 # Wasserstein + # Rescaling (min-max normalization) + # https://en.wikipedia.org/wiki/Feature_scaling#Rescaling_(min-max_normalization) + mae = (mae - mae.min())/(mae.max() - mae.min()) print('[OK]', flush=True) - # # Plot bar for current distribution - # plt.bar( - # x_i + x_offset, - # mae, - # bar_width, - # label=label, - # linewidth=lmdk_lib.line_width - # ) - # # Change offset for next bar - # x_offset += bar_width - # Plot line + # Plot bar for current distribution plt.plot( x_i, mae, diff --git a/code/expt/t-drive-sel.py b/code/expt/t-drive-sel.py index a9ebdcf..b91b411 100644 --- a/code/expt/t-drive-sel.py +++ b/code/expt/t-drive-sel.py @@ -87,18 +87,18 @@ def main(args): for bgt in bgt_conf: for _ in range(args.iter): - lmdks, eps_out = lmdk_sel.find_lmdks(seq, lmdks, bgt['epsilon']) + lmdks_sel, eps_out = lmdk_sel.find_lmdks(seq, lmdks, bgt['epsilon']) # Skip - rls_data_s, _ = lmdk_bgt.skip(seq, lmdks, eps_out) + rls_data_s, _ = lmdk_bgt.skip(seq, lmdks_sel, eps_out) mae_s[i] += lmdk_bgt.mae(seq, rls_data_s)/args.iter # Uniform - rls_data_u, _ = lmdk_bgt.uniform_r(seq, lmdks, eps_out) + rls_data_u, _ = lmdk_bgt.uniform_r(seq, lmdks_sel, eps_out) mae_u[i] += lmdk_bgt.mae(seq, rls_data_u)/args.iter # Adaptive - rls_data_a, _, _ = lmdk_bgt.adaptive(seq, lmdks, eps_out, .5, .5) + rls_data_a, _, _ = lmdk_bgt.adaptive(seq, lmdks_sel, eps_out, .5, .5) mae_a[i] += lmdk_bgt.mae(seq, rls_data_a)/args.iter # Calculate once diff --git a/code/lib/lmdk_lib.py b/code/lib/lmdk_lib.py index e85ff95..80af98a 100644 --- a/code/lib/lmdk_lib.py +++ b/code/lib/lmdk_lib.py @@ -189,8 +189,10 @@ def get_hist(seq, lmdks): # lmdks_rel = np.append(lmdks_rel, end) # Dealing with zeros. - if len(seq) == 0 or len(lmdks) == 0: + if len(seq) == 0: return np.zeros(math.ceil(max(seq))), 1 + elif len(lmdks) == 0: + return np.zeros(1), len(seq) # Interquartile range (IQR) is a measure of statistical dispersion, being equal to the difference between 75th and 25th percentiles, or between upper and lower quartiles. # https://en.wikipedia.org/wiki/Interquartile_range diff --git a/code/lib/lmdk_sel.py b/code/lib/lmdk_sel.py index 73cbc10..65c938e 100644 --- a/code/lib/lmdk_sel.py +++ b/code/lib/lmdk_sel.py @@ -406,7 +406,7 @@ def find_lmdks_eps(seq, lmdks, epsilon): ''' # The new landmarks lmdks_new = lmdks - if len(lmdks) > 0 and len(seq) != len(lmdks): + if len(seq) != len(lmdks): # Get landmarks timestamps in sequence lmdks_seq = find_lmdks_seq(seq, lmdks) # Turn landmarks to histogram @@ -426,7 +426,7 @@ def find_lmdks_eps(seq, lmdks, epsilon): # Already landmarks lmdks_seq_pt = lmdks_seq[(lmdks_seq >= pt[0]) & (lmdks_seq <= pt[1])] # Sample randomly from the rest of the sequence - size = hist_new[i] - len(lmdks_seq_pt) + size = int(hist_new[i] - len(lmdks_seq_pt)) rglr = np.setdiff1d(np.arange(pt[0], pt[1] + 1), lmdks_seq_pt) # Add already landmarks lmdks_seq_new = np.concatenate([lmdks_seq_new, lmdks_seq_pt])