get-stats.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/env python3
  2. import csv
  3. import numpy
  4. import os
  5. import sys
  6. # Starting day: February Nth
  7. #N = 1
  8. N = 20
  9. # See note in readme
  10. JAN_31 = 2459245
  11. # 2021 February Nth as Julian date
  12. FIRST_DAY = JAN_31 + N
  13. TOTAL_BRIDGES = 1890
  14. OBFS4_EMAIL_BRIDGES = 93
  15. def sigfigs(n):
  16. if n == 0.0:
  17. n = 0 # as an int
  18. else:
  19. i=0
  20. while n * (10**i) < 1:
  21. i += 1
  22. n = round(n * 10**i) / 10**i
  23. return n
  24. email_bridges = set()
  25. with open ("data/obfs4-email-bridges", 'r') as f:
  26. for line in f:
  27. if line != "":
  28. email_bridges.add(line.strip())
  29. loesing_table = """
  30. \\hline
  31. \\textbf{TP} & \\textbf{TN} & \\textbf{FP} & \\textbf{FN} & \\textbf{Precision} & \\textbf{Recall} \\\\
  32. \\hline
  33. """
  34. abs_table = """
  35. \\hline
  36. $t$ & $m$ & \\textbf{TP} & \\textbf{TN} & \\textbf{FP} & \\textbf{FN} & \\textbf{Precision} & \\textbf{Recall} \\\\
  37. \\hline
  38. """
  39. rel_table = """
  40. \\hline
  41. $d$ & \\textbf{TP} & \\textbf{TN} & \\textbf{FP} & \\textbf{FN} & \\textbf{Precision} & \\textbf{Recall} \\\\
  42. \\hline
  43. """
  44. def accuracy (filename):
  45. with open (filename, 'r') as f:
  46. # obfs4 email bridges correctly identified as blocked
  47. correct = 0
  48. # obfs4 email bridges identified as blocked before they actually were
  49. too_soon = 0
  50. # non-obfs4-email bridges incorrectly identified as blocked
  51. incorrect = 0
  52. for line in f:
  53. if line != "":
  54. line = line.strip()
  55. fingerprint = line[:40]
  56. date = int(line[41:])
  57. if fingerprint in email_bridges:
  58. if date >= FIRST_DAY:
  59. correct += 1
  60. else:
  61. too_soon += 1
  62. else:
  63. incorrect += 1
  64. tn = TOTAL_BRIDGES - OBFS4_EMAIL_BRIDGES - incorrect
  65. tp = correct
  66. fn = OBFS4_EMAIL_BRIDGES - correct - too_soon
  67. fp = too_soon + incorrect
  68. precision = sigfigs(tp / (tp + fp))
  69. recall = sigfigs(tp / (tp + fn))
  70. return f"{tp} & {tn} & {fp} & {fn} & {precision} & {recall} \\\\\n"
  71. # Loesing
  72. loesing_table += accuracy ("data/blocked_loesing")
  73. # Absolute threshold
  74. for t in range (8, 40, 8):
  75. for m in range (t+8, 112, 8):
  76. abs_table += f"{t} & {m} & " + accuracy (f"data/blocked_abs_{t}_{m}")
  77. # Relative threshold
  78. for d in range (8, 112, 8):
  79. rel_table += f"{d} & " + accuracy (f"data/blocked_rel_{d}")
  80. print ("Loesing's algorithm:")
  81. print (loesing_table)
  82. print ("Absolute threshold:")
  83. print (abs_table)
  84. print ("Relative threshold:")
  85. print (rel_table)
  86. # Now let's look at stddevs
  87. email_bridges = list(email_bridges)
  88. email_bridge_data = []
  89. email_bridge_max = []
  90. for fingerprint in email_bridges:
  91. # We're going to get all the data for each bridge
  92. bridge_data = dict()
  93. begun = False
  94. max_count = 0
  95. filename = f"data/bridge_data_cleaned/{fingerprint.upper()}"
  96. if os.path.isfile(filename) and os.path.getsize(filename) > 0:
  97. with open(filename, 'r') as csvfile:
  98. data = csv.reader(csvfile, delimiter=',')
  99. for line in data:
  100. # Ignore 0 values until we see a non-zero value
  101. if not begun:
  102. if line[1] != "0":
  103. begun = True
  104. if begun:
  105. date = int(line[0][:line[0].find(' ')])
  106. val = int(line[1])
  107. bridge_data[date] = val
  108. max_count = max(max_count, val)
  109. if begun:
  110. email_bridge_data.append(bridge_data)
  111. email_bridge_max.append(max_count)
  112. # Look at bridges individually
  113. for i in range(len(email_bridge_data)):
  114. bridge = email_bridge_data[i]
  115. vals = []
  116. # Get smallest key, i.e., first date we have data for
  117. start_date = min(bridge)
  118. # Get counts before censorship started
  119. #for d in range(start_date, FIRST_DAY):
  120. # if d in bridge:
  121. # vals.append(bridge[d])
  122. # If this day is not represented, the bridge did not report
  123. # stats; this is different from 0.
  124. # Note: This is cheaper than the above impelmentation.
  125. for date, val in bridge.items():
  126. if date < FIRST_DAY:
  127. vals.append(val)
  128. # If we have no data, don't worry about it
  129. if len(vals) == 0:
  130. continue
  131. mu = numpy.mean(vals)
  132. sigma = numpy.std(vals)
  133. if sigma > 0:
  134. print (f"Single: Bridge {i}: max={email_bridge_max[i]}, mean={mu}, std={sigma}")
  135. print (f"Single: Zero is {mu / sigma} standard deviations away from the mean ({mu})")
  136. print (f"Single: We are looking at data from {len(vals)} days, starting on {start_date}\n")
  137. # Look at pairs of bridges
  138. for i in range(len(email_bridge_data)):
  139. for j in range(i+1, len(email_bridge_data)):
  140. max_count = 0
  141. bridge_i = email_bridge_data[i]
  142. bridge_j = email_bridge_data[j]
  143. vals = []
  144. # Get smallest key, i.e., the first date BOTH bridges have data for
  145. start_date = max(min(bridge_i), min(bridge_j))
  146. # Get counts before censorship started
  147. #for d in range(start_date, FIRST_DAY):
  148. # Get set of keys between start_date and FIRST_DAY
  149. keys = set()
  150. for d in bridge_i:
  151. if d >= start_date and d <= FIRST_DAY:
  152. keys.add(d)
  153. for d in bridge_j:
  154. if d >= start_date and d <= FIRST_DAY:
  155. keys.add(d)
  156. for d in keys:
  157. val = 0
  158. if d in bridge_i and d in bridge_j:
  159. val = bridge_i[d] + bridge_j[d]
  160. elif d in bridge_i:
  161. val = bridge_i[d]
  162. elif d in bridge_j:
  163. val = bridge_j[d]
  164. vals.append(val)
  165. max_count = max(max_count, val)
  166. # If we have no data, don't worry about it
  167. if len(vals) == 0:
  168. continue
  169. mu = numpy.mean(vals)
  170. sigma = numpy.std(vals)
  171. if sigma > 0:
  172. print (f"Double: Bridges {i} and {j}: max={max_count}, mean={mu}, std={sigma}")
  173. print (f"Double: Zero is {mu / sigma} standard deviations away from the mean ({mu})")
  174. print (f"Double: We are looking at data from {len(vals)} days, starting on {start_date}\n")