code: Various corrections
This commit is contained in:
		@ -73,31 +73,25 @@ def main(args):
 | 
			
		||||
      # Skip
 | 
			
		||||
      rls_data_s, bgts_s = lmdk_bgt.skip_cont(seq, lmdks, eps_out)
 | 
			
		||||
      # lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_s)
 | 
			
		||||
      mae_s[i] += lmdk_bgt.mae_cont(rls_data_s)/args.iter
 | 
			
		||||
      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, eps_out)
 | 
			
		||||
      # lmdk_bgt.validate_bgts(seq, lmdks, epsilon, bgts_u)
 | 
			
		||||
      mae_u[i] += lmdk_bgt.mae_cont(rls_data_u)/args.iter
 | 
			
		||||
      mae_u[i] += (lmdk_bgt.mae_cont(rls_data_u)/args.iter)*100
 | 
			
		||||
 | 
			
		||||
      # Adaptive
 | 
			
		||||
      rls_data_a, _, _ = lmdk_bgt.adaptive_cont(seq, lmdks, eps_out, .5, .5)
 | 
			
		||||
      mae_a[i] += lmdk_bgt.mae_cont(rls_data_a)/args.iter
 | 
			
		||||
      mae_a[i] += (lmdk_bgt.mae_cont(rls_data_a)/args.iter)*100
 | 
			
		||||
 | 
			
		||||
      # Calculate once
 | 
			
		||||
      if i == 0:
 | 
			
		||||
        # Event
 | 
			
		||||
        rls_data_evt, _ = lmdk_bgt.uniform_cont(seq, lmdk_lib.find_lmdks_cont(lmdk_data, seq, uid, 0), epsilon)
 | 
			
		||||
        mae_evt += lmdk_bgt.mae_cont(rls_data_evt)/args.iter
 | 
			
		||||
        mae_evt += (lmdk_bgt.mae_cont(rls_data_evt)/args.iter)*100
 | 
			
		||||
        # User
 | 
			
		||||
        rls_data_usr, _ = lmdk_bgt.uniform_cont(seq, lmdk_lib.find_lmdks_cont(lmdk_data, seq, uid, 100), epsilon)
 | 
			
		||||
        mae_usr += lmdk_bgt.mae_cont(rls_data_usr)/args.iter
 | 
			
		||||
 | 
			
		||||
  mae_u *= 100
 | 
			
		||||
  mae_s *= 100
 | 
			
		||||
  mae_a *= 100
 | 
			
		||||
  mae_evt *= 100
 | 
			
		||||
  mae_usr *= 100
 | 
			
		||||
        mae_usr += (lmdk_bgt.mae_cont(rls_data_usr)/args.iter)*100
 | 
			
		||||
 | 
			
		||||
  plt.axhline(
 | 
			
		||||
    y = mae_evt,
 | 
			
		||||
@ -144,7 +138,7 @@ def main(args):
 | 
			
		||||
  # # Show plot
 | 
			
		||||
  # plt.show()
 | 
			
		||||
  # Save plot
 | 
			
		||||
  lmdk_lib.save_plot(path + '-sel-emd.pdf')
 | 
			
		||||
  lmdk_lib.save_plot(path + '-sel.pdf')
 | 
			
		||||
  print('[OK]', flush=True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -27,7 +27,7 @@ def main(args):
 | 
			
		||||
  # The landmarks percentages
 | 
			
		||||
  lmdks_pct = [0, 20, 40, 60, 80, 100]
 | 
			
		||||
  # The privacy budget
 | 
			
		||||
  epsilon = .1
 | 
			
		||||
  epsilon = 1.0
 | 
			
		||||
 | 
			
		||||
  # Number of methods
 | 
			
		||||
  n = 3
 | 
			
		||||
@ -87,7 +87,7 @@ def main(args):
 | 
			
		||||
        # User
 | 
			
		||||
        rls_data_usr, _ = lmdk_bgt.uniform_cont(seq, lmdk_lib.find_lmdks_cont(lmdk_data, seq, uid, 100), epsilon)
 | 
			
		||||
        mae_usr += (lmdk_bgt.mae_cont(rls_data_usr)/args.iter)*100
 | 
			
		||||
  exit()
 | 
			
		||||
 | 
			
		||||
  plt.axhline(
 | 
			
		||||
    y = mae_evt,
 | 
			
		||||
    color = '#212121',
 | 
			
		||||
 | 
			
		||||
@ -1,107 +0,0 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
import sys
 | 
			
		||||
sys.path.insert(1, '../lib')
 | 
			
		||||
import argparse
 | 
			
		||||
import lmdk_lib
 | 
			
		||||
import lmdk_sel
 | 
			
		||||
import exp_mech
 | 
			
		||||
import numpy as np
 | 
			
		||||
import os
 | 
			
		||||
from matplotlib import pyplot as plt
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main(args):
 | 
			
		||||
  # Privacy goal
 | 
			
		||||
  epsilon = [.001, .01, .1, 1.0, 10.0, 100.0]
 | 
			
		||||
  # Number of timestamps
 | 
			
		||||
  seq = lmdk_lib.get_seq(1, args.time)
 | 
			
		||||
  # Distribution type
 | 
			
		||||
  dist_type = np.array(range(-1, 4))
 | 
			
		||||
  # Number of landmarks
 | 
			
		||||
  lmdk_n = np.array(range(int(.2*args.time), args.time, int(args.time/5)))
 | 
			
		||||
  # Width of bars
 | 
			
		||||
  bar_width = 1/(len(epsilon) + 1)
 | 
			
		||||
  # The x axis
 | 
			
		||||
  x_i = np.arange(len(lmdk_n))
 | 
			
		||||
  x_margin = bar_width*(len(epsilon)/2 + 1)
 | 
			
		||||
  for d_i, d in enumerate(dist_type):
 | 
			
		||||
    # Logging
 | 
			
		||||
    title =  lmdk_lib.dist_type_to_str(d) + ' landmark distribution'
 | 
			
		||||
    print('(%d/%d) %s... ' %(d_i + 1, len(dist_type), title), end='', flush=True)
 | 
			
		||||
    # Initialize plot
 | 
			
		||||
    lmdk_lib.plot_init()
 | 
			
		||||
    # The x axis
 | 
			
		||||
    plt.xticks(x_i, ((lmdk_n/len(seq))*100).astype(int))
 | 
			
		||||
    plt.xlabel('Landmarks (%)')  # Set x axis label.
 | 
			
		||||
    plt.xlim(x_i.min() - x_margin, x_i.max() + x_margin)
 | 
			
		||||
    # The y axis
 | 
			
		||||
    plt.ylabel('Mean absolute error')  # Set y axis label.
 | 
			
		||||
    # plt.ylim(0, len(seq)*1.5)
 | 
			
		||||
    # Bar offset
 | 
			
		||||
    x_offset = -(bar_width/2)*(len(epsilon) - 1)
 | 
			
		||||
    for e_i, e in enumerate(epsilon):
 | 
			
		||||
      mae = np.zeros(len(lmdk_n))
 | 
			
		||||
      for n_i, n in enumerate(lmdk_n):
 | 
			
		||||
        for r in range(args.reps):
 | 
			
		||||
          lmdks = lmdk_lib.get_lmdks(seq, n, d)
 | 
			
		||||
          hist, h = lmdk_lib.get_hist(seq, lmdks)
 | 
			
		||||
          opts = lmdk_sel.get_opts_from_top_h(seq, lmdks)
 | 
			
		||||
          delta = 1.0
 | 
			
		||||
          res, _ = exp_mech.exponential_pareto(hist, opts, exp_mech.score, delta, e)
 | 
			
		||||
          mae[n_i] += lmdk_lib.get_norm(hist, res)/args.reps
 | 
			
		||||
      # Plot bar for current epsilon
 | 
			
		||||
      plt.bar(
 | 
			
		||||
        x_i + x_offset,
 | 
			
		||||
        mae,
 | 
			
		||||
        bar_width,
 | 
			
		||||
        label=u'\u03B5 = ' + str("{:.0e}".format(e)),
 | 
			
		||||
        linewidth=lmdk_lib.line_width
 | 
			
		||||
      )
 | 
			
		||||
      # Change offset for next bar
 | 
			
		||||
      x_offset += bar_width
 | 
			
		||||
    path = str('../../rslt/lmdk_sel-pareto/' + title)
 | 
			
		||||
    # Plot legend
 | 
			
		||||
    lmdk_lib.plot_legend()
 | 
			
		||||
    # Show plot
 | 
			
		||||
    # plt.show()
 | 
			
		||||
    # Save plot
 | 
			
		||||
    lmdk_lib.save_plot(path + '.pdf')
 | 
			
		||||
    print('[OK]', flush=True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
'''
 | 
			
		||||
  Parse arguments.
 | 
			
		||||
 | 
			
		||||
  Optional:
 | 
			
		||||
    reps - The number of repetitions.
 | 
			
		||||
    time - The time limit of the sequence.
 | 
			
		||||
'''
 | 
			
		||||
def parse_args():
 | 
			
		||||
  # Create argument parser.
 | 
			
		||||
  parser = argparse.ArgumentParser()
 | 
			
		||||
 | 
			
		||||
  # Mandatory arguments.
 | 
			
		||||
 | 
			
		||||
  # Optional arguments.
 | 
			
		||||
  parser.add_argument('-r', '--reps', help='The number of repetitions.', type=int, default=1)
 | 
			
		||||
  parser.add_argument('-t', '--time', help='The time limit of the sequence.', type=int, default=100)
 | 
			
		||||
 | 
			
		||||
  # 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 elapsed: %s' % (time.strftime('%H:%M:%S', time.gmtime(end_time - start_time))))
 | 
			
		||||
    print('##############################')
 | 
			
		||||
  except KeyboardInterrupt:
 | 
			
		||||
    print('Interrupted by user.')
 | 
			
		||||
    exit()
 | 
			
		||||
@ -48,7 +48,7 @@ def main(args):
 | 
			
		||||
  # The y axis
 | 
			
		||||
  plt.ylabel('Mean absolute error (kWh)')  # Set y axis label.
 | 
			
		||||
  plt.yscale('log')
 | 
			
		||||
  # plt.ylim(.01, 10000)
 | 
			
		||||
  plt.ylim(.01, 1000)
 | 
			
		||||
  # Bar offset
 | 
			
		||||
  x_offset = -(bar_width/2)*(n - 1)
 | 
			
		||||
 | 
			
		||||
@ -134,7 +134,7 @@ def main(args):
 | 
			
		||||
  # Show plot
 | 
			
		||||
  # plt.show()
 | 
			
		||||
  # Save plot
 | 
			
		||||
  lmdk_lib.save_plot(path + '-sel-emd.pdf')
 | 
			
		||||
  lmdk_lib.save_plot(path + '-sel.pdf')
 | 
			
		||||
  print('[OK]', flush=True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -46,7 +46,7 @@ def main(args):
 | 
			
		||||
  # The y axis
 | 
			
		||||
  plt.ylabel('Mean absolute error (kWh)')  # Set y axis label.
 | 
			
		||||
  plt.yscale('log')
 | 
			
		||||
  # plt.ylim(.01, 10000)
 | 
			
		||||
  plt.ylim(.01, 1000)
 | 
			
		||||
  # Bar offset
 | 
			
		||||
  x_offset = -(bar_width/2)*(n - 1)
 | 
			
		||||
 | 
			
		||||
@ -162,7 +162,7 @@ if __name__ == '__main__':
 | 
			
		||||
    main(parse_args())
 | 
			
		||||
    end_time = time.time()
 | 
			
		||||
    print('##############################')
 | 
			
		||||
    print('Time   : %.4fs' % (end_time - start_time))
 | 
			
		||||
    print('Time elapsed: %s' % (time.strftime('%H:%M:%S', time.gmtime(end_time - start_time))))
 | 
			
		||||
    print('##############################')
 | 
			
		||||
  except KeyboardInterrupt:
 | 
			
		||||
    print('Interrupted by user.')
 | 
			
		||||
 | 
			
		||||
@ -34,6 +34,7 @@ def main(args):
 | 
			
		||||
  # The y axis
 | 
			
		||||
  # plt.yscale('log')
 | 
			
		||||
  plt.ylabel('Euclidean distance')  # Set y axis label.
 | 
			
		||||
  # plt.ylabel('Wasserstein distance')  # Set y axis label.
 | 
			
		||||
  # Bar offset
 | 
			
		||||
  x_offset = -(bar_width/2)*(len(dist_type) - 1)
 | 
			
		||||
  for d_i, d in enumerate(dist_type):
 | 
			
		||||
@ -66,6 +67,7 @@ def main(args):
 | 
			
		||||
    # Change offset for next bar
 | 
			
		||||
    x_offset += bar_width
 | 
			
		||||
  path = str('../../rslt/lmdk_sel_cmp/' + 'lmdk_sel_cmp-norm')
 | 
			
		||||
  # path = str('../../rslt/lmdk_sel_cmp/' + 'lmdk_sel_cmp-emd')
 | 
			
		||||
  # Plot legend
 | 
			
		||||
  lmdk_lib.plot_legend()
 | 
			
		||||
  # Show plot
 | 
			
		||||
 | 
			
		||||
@ -155,7 +155,7 @@ def main(args):
 | 
			
		||||
    # Show plot
 | 
			
		||||
    # plt.show()
 | 
			
		||||
    # Save plot
 | 
			
		||||
    lmdk_lib.save_plot(path + '-sel-emd.pdf')
 | 
			
		||||
    lmdk_lib.save_plot(path + '-sel.pdf')
 | 
			
		||||
    print('[OK]', flush=True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -188,7 +188,7 @@ if __name__ == '__main__':
 | 
			
		||||
    main(parse_args())
 | 
			
		||||
    end_time = time.time()
 | 
			
		||||
    print('##############################')
 | 
			
		||||
    print('Time   : %.4fs' % (end_time - start_time))
 | 
			
		||||
    print('Time elapsed: %s' % (time.strftime('%H:%M:%S', time.gmtime(end_time - start_time))))
 | 
			
		||||
    print('##############################')
 | 
			
		||||
  except KeyboardInterrupt:
 | 
			
		||||
    print('Interrupted by user.')
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								rslt/bgt_cmp/Copenhagen-sel.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								rslt/bgt_cmp/Copenhagen-sel.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								rslt/bgt_cmp/HUE-sel.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								rslt/bgt_cmp/HUE-sel.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								rslt/lmdk_sel_cmp/lmdk_sel_cmp-emd.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								rslt/lmdk_sel_cmp/lmdk_sel_cmp-emd.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								rslt/lmdk_sel_cmp/lmdk_sel_cmp-norm.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								rslt/lmdk_sel_cmp/lmdk_sel_cmp-norm.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user