-
Notifications
You must be signed in to change notification settings - Fork 1
/
latency_estimate.py
91 lines (79 loc) · 3.91 KB
/
latency_estimate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Introduction:
# This script is used to estimate the latency between two sets of data. It reads in two .txt files,
# shifts the data in one of the files by a range of values,
# and then calculates the mean and median absolute error between the two sets of data for each shift value.
# The results are plotted on a graph.
# Usage:
# 1. Place the two .txt files to be compared in the same directory as the script.
# 2. Modify the `ref_data_dir` and `cmp_data_dir` variables to point to the two .txt files.
# 3. Modify the `ref_data_shift_min` and `ref_data_shift_max` variables to set the range of shift values to test.
# 4. Modify the `frame_num` variable to set the number of frames to compare for each shift value.
# 5. Run the script in a Python environment with the required dependencies installed.
# 6. The script will output the mean and median absolute error for each shift value, and plot the results on a graph.
# Dependencies:
# - pandas
# - numpy
# - matplotlib
# You can install the required dependencies using pip:
# pip install pandas numpy matplotlib
# Author: Github Copilot, Zhao Qingrui
import subprocess
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# a function that opens the terminal and runs the input command, then save the output and print
def run_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
output, error = process.communicate()
# find sting "std:" in the output and save the data after it as double
output = output.decode("utf-8")
print(output)
output = output[output.find("mean")+5:output.find("median")-1]
output_std = float(output)
print("-----------------------------------")
return output_std
# a function that reads an .txt file and shift the data by a given amount
def dataModification(fileName, shiftStart, shiftEnd, selectRow):
# read the file
data = pd.read_csv(fileName, sep=" ", header=None)
# create new data
for i in range(shiftStart, shiftEnd):
new_data = pd.DataFrame(columns=[0,1,2,3,4,5,6,7])
# shift the data
new_data = data.shift(i)
new_data.loc[:,0] = data.loc[:,0]
# stlect the middle part of the new_data with length of selectRow
cut_data = new_data.loc[(new_data.shape[0]//2-selectRow//2):(new_data.shape[0]//2+selectRow//2),:]
# save the file
fileName_save = "./processedData/" + fileName[0:-4] + "_" + str(i) + ".txt"
cut_data.to_csv(fileName_save, sep=" ", header=None, index=None)
print("Data modified and saved")
# the directory of the reference data and the data to be compared
ref_data_dir = "imuDataRefineFull.txt"
cmp_data_dir = "mocapDataRefineFull.txt"
# the range of the shift of the reference data
ref_data_shift_min = 5
ref_data_shift_max = 12
frame_num = 20
# script execution -------------------------------------------------------------------
dataModification(cmp_data_dir, ref_data_shift_min, ref_data_shift_max+1, frame_num)
dataModification(ref_data_dir, 0, 1, frame_num)
std = []
for i in range(ref_data_shift_min, ref_data_shift_max+1):
cmp_data_dir_loop = "./processedData/" + cmp_data_dir[0:-4] + "_" + str(i) + ".txt"
ref_data_dir_loop = "./processedData/" + ref_data_dir[0:-4] + "_0.txt"
std.append(run_command("evo_ape tum " + ref_data_dir_loop + " " + cmp_data_dir_loop + " -r angle_deg"))
# plot the average mean
pic_name = "ape-angle_deg-" + str(ref_data_shift_min) + "-" + str(ref_data_shift_max) + "-" + str(frame_num) + ".png"
plt.plot(np.arange(ref_data_shift_min, ref_data_shift_max+1), std)
plt.xlabel("shift/frame")
plt.ylabel("difference/degree")
# set x axis to be integer
plt.xticks(np.arange(ref_data_shift_min, ref_data_shift_max+1, 5))
# save plot to file, resolution 300 dpi
plt.savefig(pic_name, dpi=300)
# label the x axis of every point in the plot
for i in range(len(std)):
plt.text(i+ref_data_shift_min, std[i], str(i+ref_data_shift_min))
plt.title(pic_name)
plt.show()