compute_client_error.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. import math
  3. import sys
  4. def usage():
  5. print("Usage:")
  6. print("compute_client_error.py -f time,x1[,x2,x3....] server-data client-data")
  7. print("The files are expected to contain space separated fields. The")
  8. print("-f options specifies first the column in which the world time")
  9. print("is, followed by the list of columns to be compared.")
  10. print("It computes for each data point in the client file the closest")
  11. print("the two data points with the closest time stamp in the server")
  12. print("and then interpolates the server position based on the client")
  13. print("time between these positions. The difference between the")
  14. print("intepolated position")
  15. print()
  16. print("Example:")
  17. print("compute_client_error.py-multi -f 6,16,17,18 debug.server debug.client")
  18. print(" to compute the differences between client and server for the")
  19. print(" fields 16,17,18 (which atm is the velocity)")
  20. sys.exit()
  21. # -----------------------------------------------------------------------------
  22. def readFile(name, fields):
  23. f = open(name, "r")
  24. result = []
  25. for i in f.readlines():
  26. if i[:6] == "Rewind":
  27. continue
  28. l = i.split()
  29. try:
  30. l_values = [ float(l[index]) for index in fields ]
  31. except ValueError:
  32. pass
  33. result.append(l_values)
  34. f.close()
  35. return result
  36. # -----------------------------------------------------------------------------
  37. # Compares the client and server data. Each argument is a list of
  38. # triplets containing (time, x, z) data.
  39. #
  40. def computeDifferences(server, client):
  41. index_s = 0
  42. count = 0
  43. sum = 0.0
  44. min = 999999.9
  45. max = -1.0
  46. for items in client:
  47. time, x = items[0], items[1:]
  48. # Find largest entry in server data that is <= client's time:
  49. while index_s<len(server)-2:
  50. t1 = server[index_s+1][0]
  51. if t1>time: break
  52. index_s += 1
  53. #print "time", time, server[index_s][0], server[index_s+1][0]
  54. t1,x1 = server[index_s ][0],server[index_s ][1:]
  55. t2,x2 = server[index_s+1][0],server[index_s+1][1:]
  56. f = (time-t1)/(t2-t1)
  57. interp = []
  58. error = 0
  59. for i, x1_val in enumerate(x1):
  60. x2_val = x2[i]
  61. x_i = x1_val + f * (x2_val-x1_val)
  62. interp.append(x_i)
  63. error = error + (x[i]-x_i)**2
  64. error = math.sqrt(error)
  65. print(time, error)
  66. if (error < min): min=error
  67. if (error > max): max=error
  68. count += 1
  69. sum += error
  70. print("sum %f count %d min %f average %f max %f" \
  71. % (sum, count, min, sum/count, max))
  72. # -----------------------------------------------------------------------------
  73. if __name__=="__main__":
  74. if len(sys.argv)==5 and sys.argv[1]=="-f":
  75. fields = sys.argv[2]
  76. del sys.argv[1:3]
  77. else:
  78. fields = ["6", "9", "11"]
  79. if len(sys.argv)!=3:
  80. usage()
  81. # -1 to convert awk/gnuplot indices (starting with 1) to
  82. # python indices (starting with 0)
  83. field_indices = [int(item)-1 for item in fields.split(",")]
  84. server_name = sys.argv[1]
  85. client_name = sys.argv[2]
  86. server = readFile(server_name, field_indices)
  87. client = readFile(client_name, field_indices)
  88. computeDifferences(server, client)