get-stats.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. ## Set up all the LaTeX
  30. table_start = """
  31. \\documentclass[sigconf]{acmart}
  32. \\begin{document}
  33. \\begin{table*}[t]
  34. \\caption{Results from running Algorithm~1 (using an absolute threshold of 32 and an absolute connection count minimum of 100), Algorithm~2 (using an absolute threshold $t$ and an absolute connection count minimum $m$), and Algorithm~3 (using a relative connection count difference $d$).}
  35. \\begin{tabular}{c c | c c c c c c c}
  36. \\hline
  37. """
  38. loesing_table = """
  39. \\multicolumn{8}{c}{\\textbf{Algorithm~1}} \\\\
  40. \\multicolumn{2}{c@{}}{} & \\textbf{TP} & \\textbf{TN} & \\textbf{FP} & \\textbf{FN} & \\textbf{Precision} & \\textbf{Recall} \\\\
  41. \\hline
  42. """
  43. abs_table = """
  44. \\\\
  45. \\hline
  46. \\multicolumn{8}{c}{\\textbf{Algorithm~2}} \\\\
  47. $t$ & $m$ & \\textbf{TP} & \\textbf{TN} & \\textbf{FP} & \\textbf{FN} & \\textbf{Precision} & \\textbf{Recall} \\\\
  48. \\hline
  49. """
  50. abs_table_start_2 = """
  51. \\end{tabular}
  52. \\hspace{3em}
  53. \\begin{tabular}{c c | c c c c c c}
  54. \\hline
  55. \\multicolumn{8}{c}{\\textbf{Algorithm~2 (continued)}} \\\\
  56. $t$ & $m$ & \\textbf{TP} & \\textbf{TN} & \\textbf{FP} & \\textbf{FN} & \\textbf{Precision} & \\textbf{Recall} \\\\
  57. \\hline
  58. """
  59. rel_table = """
  60. \\\\
  61. \\hline
  62. \\multicolumn{8}{c}{\\textbf{Algorithm~3}} \\\\
  63. & $d$ & \\textbf{TP} & \\textbf{TN} & \\textbf{FP} & \\textbf{FN} & \\textbf{Precision} & \\textbf{Recall} \\\\
  64. \\hline
  65. """
  66. table_end = """
  67. \\end{tabular}
  68. \\end{table*}
  69. \\end{document}
  70. """
  71. def accuracy (filename):
  72. with open (filename, 'r') as f:
  73. # obfs4 email bridges correctly identified as blocked
  74. correct = 0
  75. # obfs4 email bridges identified as blocked before they actually were
  76. too_soon = 0
  77. # non-obfs4-email bridges incorrectly identified as blocked
  78. incorrect = 0
  79. for line in f:
  80. if line != "":
  81. line = line.strip()
  82. fingerprint = line[:40]
  83. date = int(line[41:])
  84. if fingerprint in email_bridges:
  85. if date >= FIRST_DAY:
  86. correct += 1
  87. else:
  88. too_soon += 1
  89. else:
  90. incorrect += 1
  91. tn = TOTAL_BRIDGES - OBFS4_EMAIL_BRIDGES - incorrect
  92. tp = correct
  93. fn = OBFS4_EMAIL_BRIDGES - correct - too_soon
  94. fp = too_soon + incorrect
  95. precision = sigfigs(tp / (tp + fp))
  96. recall = sigfigs(tp / (tp + fn))
  97. return f"{tp} & {tn} & {fp} & {fn} & {precision} & {recall} \\\\\n"
  98. ## Add the data to the table
  99. # Loesing
  100. loesing_table += "\\multicolumn{2}{c@{}}{} & " + accuracy ("data/blocked_loesing")
  101. # Absolute threshold
  102. for t in range (8, 40, 8):
  103. for m in range (t+8, 112, 8):
  104. abs_table += f"{t} & {m} & " + accuracy (f"data/blocked_abs_{t}_{m}")
  105. if t == 24 and m == 56:
  106. # push to second column
  107. abs_table += abs_table_start_2
  108. # Relative threshold
  109. for d in range (8, 112, 8):
  110. rel_table += f"& {d} & " + accuracy (f"data/blocked_rel_{d}")
  111. ## Output the LaTeX file
  112. table = table_start + loesing_table + abs_table + rel_table + table_end
  113. with open("appendix-a-results.tex", 'w') as f:
  114. f.write(table)
  115. # Now let's look at stddevs
  116. email_bridges = list(email_bridges)
  117. email_bridge_data = []
  118. email_bridge_max = []
  119. for fingerprint in email_bridges:
  120. # We're going to get all the data for each bridge
  121. bridge_data = dict()
  122. begun = False
  123. max_count = 0
  124. filename = f"data/bridge_data_cleaned/{fingerprint.upper()}"
  125. if os.path.isfile(filename) and os.path.getsize(filename) > 0:
  126. with open(filename, 'r') as csvfile:
  127. data = csv.reader(csvfile, delimiter=',')
  128. for line in data:
  129. # Ignore 0 values until we see a non-zero value
  130. if not begun:
  131. if line[1] != "0":
  132. begun = True
  133. if begun:
  134. date = int(line[0][:line[0].find(' ')])
  135. if date > FIRST_DAY:
  136. break
  137. val = int(line[1])
  138. bridge_data[date] = val
  139. max_count = max(max_count, val)
  140. if begun:
  141. email_bridge_data.append(bridge_data)
  142. email_bridge_max.append(max_count)
  143. # Look at bridges individually
  144. for i in range(len(email_bridge_data)):
  145. bridge = email_bridge_data[i]
  146. vals = []
  147. # Get smallest key, i.e., first date we have data for
  148. start_date = min(bridge)
  149. # Get counts before censorship started
  150. #for d in range(start_date, FIRST_DAY):
  151. # if d in bridge:
  152. # vals.append(bridge[d])
  153. # If this day is not represented, the bridge did not report
  154. # stats; this is different from 0.
  155. # Note: This is cheaper than the above impelmentation.
  156. for date, val in bridge.items():
  157. if date < FIRST_DAY:
  158. vals.append(val)
  159. # If we have no data, don't worry about it
  160. if len(vals) == 0:
  161. continue
  162. mu = numpy.mean(vals)
  163. sigma = numpy.std(vals)
  164. if sigma > 0:
  165. print (f"Single: Bridge {i}: max={email_bridge_max[i]}, mean={mu}, std={sigma}")
  166. print (f"Single: Zero is {mu / sigma} standard deviations away from the mean ({mu})")
  167. print (f"Single: We are looking at data from {len(vals)} days, starting on {start_date}\n")
  168. # Look at pairs of bridges
  169. for i in range(len(email_bridge_data)):
  170. for j in range(i+1, len(email_bridge_data)):
  171. max_count = 0
  172. bridge_i = email_bridge_data[i]
  173. bridge_j = email_bridge_data[j]
  174. vals = []
  175. # Get smallest key, i.e., the first date BOTH bridges have data for
  176. start_date = max(min(bridge_i), min(bridge_j))
  177. # Get set of keys between start_date and FIRST_DAY
  178. keys = set()
  179. for d in bridge_i:
  180. if d >= start_date and d <= FIRST_DAY:
  181. keys.add(d)
  182. for d in bridge_j:
  183. if d >= start_date and d <= FIRST_DAY:
  184. keys.add(d)
  185. for d in keys:
  186. val = 0
  187. if d in bridge_i and d in bridge_j:
  188. val = bridge_i[d] + bridge_j[d]
  189. elif d in bridge_i:
  190. val = bridge_i[d]
  191. elif d in bridge_j:
  192. val = bridge_j[d]
  193. vals.append(val)
  194. max_count = max(max_count, val)
  195. # If we have no data, don't worry about it
  196. if len(vals) == 0:
  197. continue
  198. mu = numpy.mean(vals)
  199. sigma = numpy.std(vals)
  200. if sigma > 0:
  201. print (f"Double: Bridges {i} and {j}: max={max_count}, mean={mu}, std={sigma}")
  202. print (f"Double: Zero is {mu / sigma} standard deviations away from the mean ({mu})")
  203. print (f"Double: We are looking at data from {len(vals)} days, starting on {start_date}\n")