get-stats.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. if date > FIRST_DAY:
  107. break
  108. val = int(line[1])
  109. bridge_data[date] = val
  110. max_count = max(max_count, val)
  111. if begun:
  112. email_bridge_data.append(bridge_data)
  113. email_bridge_max.append(max_count)
  114. # Look at bridges individually
  115. for i in range(len(email_bridge_data)):
  116. bridge = email_bridge_data[i]
  117. vals = []
  118. # Get smallest key, i.e., first date we have data for
  119. start_date = min(bridge)
  120. # Get counts before censorship started
  121. #for d in range(start_date, FIRST_DAY):
  122. # if d in bridge:
  123. # vals.append(bridge[d])
  124. # If this day is not represented, the bridge did not report
  125. # stats; this is different from 0.
  126. # Note: This is cheaper than the above impelmentation.
  127. for date, val in bridge.items():
  128. if date < FIRST_DAY:
  129. vals.append(val)
  130. # If we have no data, don't worry about it
  131. if len(vals) == 0:
  132. continue
  133. mu = numpy.mean(vals)
  134. sigma = numpy.std(vals)
  135. if sigma > 0:
  136. print (f"Single: Bridge {i}: max={email_bridge_max[i]}, mean={mu}, std={sigma}")
  137. print (f"Single: Zero is {mu / sigma} standard deviations away from the mean ({mu})")
  138. print (f"Single: We are looking at data from {len(vals)} days, starting on {start_date}\n")
  139. # Look at pairs of bridges
  140. for i in range(len(email_bridge_data)):
  141. for j in range(i+1, len(email_bridge_data)):
  142. max_count = 0
  143. bridge_i = email_bridge_data[i]
  144. bridge_j = email_bridge_data[j]
  145. vals = []
  146. # Get smallest key, i.e., the first date BOTH bridges have data for
  147. start_date = max(min(bridge_i), min(bridge_j))
  148. # Get counts before censorship started
  149. #for d in range(start_date, FIRST_DAY):
  150. # Get set of keys between start_date and FIRST_DAY
  151. keys = set()
  152. for d in bridge_i:
  153. if d >= start_date and d <= FIRST_DAY:
  154. keys.add(d)
  155. for d in bridge_j:
  156. if d >= start_date and d <= FIRST_DAY:
  157. keys.add(d)
  158. for d in keys:
  159. val = 0
  160. if d in bridge_i and d in bridge_j:
  161. val = bridge_i[d] + bridge_j[d]
  162. elif d in bridge_i:
  163. val = bridge_i[d]
  164. elif d in bridge_j:
  165. val = bridge_j[d]
  166. vals.append(val)
  167. max_count = max(max_count, val)
  168. # If we have no data, don't worry about it
  169. if len(vals) == 0:
  170. continue
  171. mu = numpy.mean(vals)
  172. sigma = numpy.std(vals)
  173. if sigma > 0:
  174. print (f"Double: Bridges {i} and {j}: max={max_count}, mean={mu}, std={sigma}")
  175. print (f"Double: Zero is {mu / sigma} standard deviations away from the mean ({mu})")
  176. print (f"Double: We are looking at data from {len(vals)} days, starting on {start_date}\n")