anonymity_metrics.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import math
  2. import numpy as np
  3. def getEntropy(data, num_target_packets):
  4. columnsNames = ['Entropy'+str(x) for x in range(num_target_packets)]
  5. entropies = []
  6. for column in columnsNames:
  7. dist = data.iloc[0][column]
  8. # suma = sum([float(x) for x in dist])
  9. # print("For column %s the sum is %f" % (column, suma))
  10. # entropies.append()
  11. entropies.append(dist)
  12. return np.mean(entropies)
  13. # def getEntropy(data):
  14. # tmp = data[data["Type"] == "ENTROPY"]
  15. # entropy = np.mean(tmp["Entropy"].tolist())
  16. # return entropy
  17. def getUnlinkability(data):
  18. epsilon = []
  19. dlts = 0
  20. est_senderA = data["PrSenderA"]
  21. est_senderB = data["PrSenderB"]
  22. realSenderLabel = data["RealSenderLabel"]
  23. for (prA, prB, label) in zip(est_senderA, est_senderB, realSenderLabel):
  24. if label == 1:
  25. if not float(prB) == 0.0:
  26. ratio = float(prA) / float(prB)
  27. if not ratio == 0.0:
  28. epsilon.append(math.log(ratio))
  29. else:
  30. dlts += 1
  31. elif label == 2:
  32. if not float(prA) == 0.0:
  33. ratio = float(prB) / float(prA)
  34. if not ratio == 0.0:
  35. epsilon.append(math.log(ratio))
  36. else:
  37. dlts += 1
  38. else:
  39. pass
  40. meanEps = None
  41. if epsilon != []:
  42. meanEps = np.mean(epsilon)
  43. delta = float(dlts) / float(len(est_senderA))
  44. return (meanEps, delta)
  45. def computeE2ELatency(df):
  46. travelTime = []
  47. for i, r in df.iterrows():
  48. timeSent = r['PacketTimeSent']
  49. timeDelivered = r['PacketTimeDelivered']
  50. travelTime.append(timeDelivered - timeSent)
  51. return np.mean(travelTime)
  52. def computeAverageE2ELatency(df, gr_start):
  53. travelTime = []
  54. #cnt = 0
  55. #print("TimeDelivered, TimeSent, TravelTime")
  56. for i, r in df.iterrows():
  57. receiverID = r['ClientID']
  58. timeSent = r['MsgTimeSent']
  59. timeDelivered = r['MsgTimeDelivered']
  60. if receiverID == 'PUBLISHER':
  61. #cnt += 1
  62. timeDelivered = gr_start
  63. travelTime.append(timeDelivered - timeSent)
  64. #print(timeDelivered, timeSent, timeDelivered-timeSent)
  65. #assert cnt == 5
  66. return np.mean(travelTime) * 2