generate_experiment.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # main imports
  2. import base64
  3. import json
  4. import re
  5. import argparse
  6. import sys, os
  7. import requests
  8. import random
  9. # modules imports
  10. sys.path.insert(0, '') # trick to enable import of main folder module
  11. # config imports
  12. import links.config as cfg
  13. def decoded_data(data):
  14. decoded_data = str(base64.b64decode(data), "utf-8")
  15. return decoded_data
  16. def encode_data(data):
  17. json_data = json.dumps(data)
  18. link_data = base64.b64encode(str(json_data).encode('utf-8'))
  19. return link_data
  20. def extract_data(line):
  21. data = line.replace('\n', '').split(';')
  22. return (data[0], data[-1])
  23. def main():
  24. parser = argparse.ArgumentParser(description="Compute experiment data")
  25. parser.add_argument('--data', type=str, help='data links to use', required=True)
  26. parser.add_argument('--scenes', type=int, help="number of scenes", required=True)
  27. parser.add_argument('--users', type=int, help="number of users of experiment", required=True)
  28. parser.add_argument('--userId', type=int, help="tell if user identifier is used or not", required=False, default=0)
  29. parser.add_argument('--output', type=str, help="output filename of user links", required=True)
  30. args = parser.parse_args()
  31. p_data = args.data
  32. p_scenes = args.scenes
  33. p_users = args.users
  34. p_userId = bool(args.userId)
  35. p_output = args.output
  36. print(p_userId)
  37. # generate link for each scene
  38. with open(p_data, 'r') as f:
  39. lines = f.readlines()
  40. data_lines = [extract_data(l) for l in lines]
  41. nb_elements = len(lines)
  42. # check if number of scenes is higher than number of elements in data links
  43. if p_scenes > nb_elements:
  44. p_scenes = nb_elements
  45. # open output filename
  46. filename_path = os.path.join(cfg.expe_data_folder, p_output)
  47. if not os.path.exists(cfg.expe_data_folder):
  48. os.makedirs(cfg.expe_data_folder)
  49. output_f = open(filename_path, 'w')
  50. for i in range(p_users):
  51. scene_links = random.sample(data_lines, k=p_scenes)
  52. # generate output line
  53. output_line = str(i) + ';'
  54. for scene_name, link in scene_links:
  55. if p_userId:
  56. data = link.split('?q=')
  57. hostname = data[0]
  58. link_data = data[1]
  59. # decode and add user id link if asked
  60. decoded_data_link = decoded_data(link_data)
  61. json_data = json.loads(decoded_data_link)
  62. json_data['userId'] = str(i)
  63. encoded_data_link = encode_data(json_data)
  64. new_link = hostname + '?q=' + str(encoded_data_link, "utf-8")
  65. # add new link
  66. output_line += scene_name + ':::' + new_link + ';'
  67. else:
  68. output_line += scene_name + ':::' + link + ';'
  69. output_line += '\n'
  70. output_f.write(output_line)
  71. output_f.flush()
  72. output_f.close()
  73. print("Experiment generated output file is saved into.. %s" % filename_path)
  74. if __name__== "__main__":
  75. main()