code: Several fixes

This commit is contained in:
Manos Katsomallos 2021-10-13 09:03:36 +02:00
parent b8aa9dfc3d
commit a1454c5a98
6 changed files with 19 additions and 31 deletions

View File

@ -71,13 +71,11 @@ def main(args):
lmdks_sel, eps_out = lmdk_sel.find_lmdks(seq, lmdks, epsilon) lmdks_sel, eps_out = lmdk_sel.find_lmdks(seq, lmdks, epsilon)
# Skip # Skip
rls_data_s, bgts_s = lmdk_bgt.skip_cont(seq, lmdks_sel, eps_out) rls_data_s, _ = lmdk_bgt.skip_cont(seq, lmdks_sel, eps_out)
# lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_s)
mae_s[i] += (lmdk_bgt.mae_cont(rls_data_s)/args.iter)*100 mae_s[i] += (lmdk_bgt.mae_cont(rls_data_s)/args.iter)*100
# Uniform # Uniform
rls_data_u, bgts_u = lmdk_bgt.uniform_cont(seq, lmdks_sel, eps_out) rls_data_u, _ = lmdk_bgt.uniform_cont(seq, lmdks_sel, eps_out)
# lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_u)
mae_u[i] += (lmdk_bgt.mae_cont(rls_data_u)/args.iter)*100 mae_u[i] += (lmdk_bgt.mae_cont(rls_data_u)/args.iter)*100
# Adaptive # Adaptive

View File

@ -64,19 +64,18 @@ def main(args):
for _ in range(args.iter): 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 # Skip
rls_data_s, bgts_s = lmdk_bgt.skip_cons(seq, lmdks, eps_out) rls_data_s, _ = lmdk_bgt.skip_cons(seq, lmdks_sel, eps_out)
# lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_s)
mae_s[i] += lmdk_bgt.mae_cons(seq, rls_data_s)/args.iter mae_s[i] += lmdk_bgt.mae_cons(seq, rls_data_s)/args.iter
# Uniform # 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 mae_u[i] += lmdk_bgt.mae_cons(seq, rls_data_u)/args.iter
# Adaptive # 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 mae_a[i] += lmdk_bgt.mae_cons(seq, rls_data_a)/args.iter
# Calculate once # Calculate once

View File

@ -57,8 +57,6 @@ def main(args):
print('(%d/%d) %s... ' %(d_i + 1, len(dist_type), title), end='', flush=True) print('(%d/%d) %s... ' %(d_i + 1, len(dist_type), title), end='', flush=True)
mae = np.zeros(len(lmdk_n)) mae = np.zeros(len(lmdk_n))
for n_i, n in enumerate(lmdk_n): for n_i, n in enumerate(lmdk_n):
if n == lmdk_n[-1]:
break
for r in range(args.iter): for r in range(args.iter):
lmdks = lmdk_lib.get_lmdks(seq, n, d) lmdks = lmdk_lib.get_lmdks(seq, n, d)
hist, h = lmdk_lib.get_hist(seq, lmdks) 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) 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_norm(hist, res)/args.iter # Euclidean
# mae[n_i] += lmdk_lib.get_emd(hist, res)/args.iter # Wasserstein # mae[n_i] += lmdk_lib.get_emd(hist, res)/args.iter # Wasserstein
mae = mae/21 # Euclidean # Rescaling (min-max normalization)
# mae = mae/11.75 # Wasserstein # https://en.wikipedia.org/wiki/Feature_scaling#Rescaling_(min-max_normalization)
mae = (mae - mae.min())/(mae.max() - mae.min())
print('[OK]', flush=True) print('[OK]', flush=True)
# # Plot bar for current distribution # 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
plt.plot( plt.plot(
x_i, x_i,
mae, mae,

View File

@ -87,18 +87,18 @@ def main(args):
for bgt in bgt_conf: for bgt in bgt_conf:
for _ in range(args.iter): 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 # 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 mae_s[i] += lmdk_bgt.mae(seq, rls_data_s)/args.iter
# Uniform # 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 mae_u[i] += lmdk_bgt.mae(seq, rls_data_u)/args.iter
# Adaptive # 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 mae_a[i] += lmdk_bgt.mae(seq, rls_data_a)/args.iter
# Calculate once # Calculate once

View File

@ -189,8 +189,10 @@ def get_hist(seq, lmdks):
# lmdks_rel = np.append(lmdks_rel, end) # lmdks_rel = np.append(lmdks_rel, end)
# Dealing with zeros. # Dealing with zeros.
if len(seq) == 0 or len(lmdks) == 0: if len(seq) == 0:
return np.zeros(math.ceil(max(seq))), 1 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. # 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 # https://en.wikipedia.org/wiki/Interquartile_range

View File

@ -406,7 +406,7 @@ def find_lmdks_eps(seq, lmdks, epsilon):
''' '''
# The new landmarks # The new landmarks
lmdks_new = lmdks lmdks_new = lmdks
if len(lmdks) > 0 and len(seq) != len(lmdks): if len(seq) != len(lmdks):
# Get landmarks timestamps in sequence # Get landmarks timestamps in sequence
lmdks_seq = find_lmdks_seq(seq, lmdks) lmdks_seq = find_lmdks_seq(seq, lmdks)
# Turn landmarks to histogram # Turn landmarks to histogram
@ -426,7 +426,7 @@ def find_lmdks_eps(seq, lmdks, epsilon):
# Already landmarks # Already landmarks
lmdks_seq_pt = lmdks_seq[(lmdks_seq >= pt[0]) & (lmdks_seq <= pt[1])] lmdks_seq_pt = lmdks_seq[(lmdks_seq >= pt[0]) & (lmdks_seq <= pt[1])]
# Sample randomly from the rest of the sequence # 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) rglr = np.setdiff1d(np.arange(pt[0], pt[1] + 1), lmdks_seq_pt)
# Add already landmarks # Add already landmarks
lmdks_seq_new = np.concatenate([lmdks_seq_new, lmdks_seq_pt]) lmdks_seq_new = np.concatenate([lmdks_seq_new, lmdks_seq_pt])