-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoving_analyzer.py
135 lines (121 loc) · 5.53 KB
/
moving_analyzer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import numpy as np
import matplotlib.pyplot as plt
"""
Reads Lab 2 moving data in a .txt file and analyzes the data in the column structure
0: latitude
1: longitude
2: altitude
3: UTM easting
4: UTM northing
5: GNSS Fix
author: [email protected]
"""
########## load raw data ##########
mode = "isec" # {"isec", "soccer"}
raw_data = np.loadtxt('Downloads/Soccer_field_moving_txt.txt').reshape(848, 6) if mode == "soccer" else np.loadtxt('Downloads/ISEC_moving_txt.txt').reshape(1259, 6)
########## calculate averages ##########
averages = np.mean(raw_data, axis=0)
########## diff between values and averages ##########
differences = np.empty_like(raw_data)
for i in range(raw_data.shape[1]):
differences[:, i] = raw_data[:, i] - averages[i,]
diff_abs = np.absolute(differences)
avg_diff = np.mean(diff_abs, axis=0)
########## plotting UTM data ########## Soccer == 0:275, 290:440, 455:675, 690: ##### ISEC == 0:160, 170:645, 660:765, 770:
plt.close()
plt.plot(raw_data[:, 3], raw_data[:, 4], 'b-', label='Data')
if mode == "soccer":
# Soccer field
m1, b1 = np.polyfit(raw_data[0:275, 3], raw_data[0:275, 4], 1)
m2, b2 = np.polyfit(raw_data[290:440, 3], raw_data[290:440, 4], 1)
m3, b3 = np.polyfit(raw_data[455:675, 3], raw_data[455:675, 4], 1)
m4, b4 = np.polyfit(raw_data[690:, 3], raw_data[690:, 4], 1)
plt.plot(raw_data[0:275, 3], m1*raw_data[0:275, 3]+b1, 'r-', label='Line of Best Fit 1')
plt.plot(raw_data[290:440, 3], m2*raw_data[290:440, 3]+b2, 'g-', label='Line of Best Fit 2')
plt.plot(raw_data[455:675, 3], m3*raw_data[455:675, 3]+b3, 'y-', label='Line of Best Fit 3')
plt.plot(raw_data[690:, 3], m4*raw_data[690:, 3]+b4, 'c-', label='Line of Best Fit 4')
elif mode == "isec":
# ISEC
m1,b1 = np.polyfit(raw_data[0:160, 3], raw_data[0:160, 4], 1)
m2,b2 = np.polyfit(raw_data[170:645, 3], raw_data[170:645, 4], 1)
m3,b3 = np.polyfit(raw_data[660:765, 3], raw_data[660:765, 4], 1)
m4,b4 = np.polyfit(raw_data[770:, 3], raw_data[770:, 4], 1)
plt.plot(raw_data[0:160, 3], m1*raw_data[0:160, 3]+b1, 'r-', label='Line of Best Fit 1')
plt.plot(raw_data[170:645, 3], m2*raw_data[170:645, 3]+b2, 'g-', label='Line of Best Fit 2')
plt.plot(raw_data[660:765, 3], m3*raw_data[660:765, 3]+b3, 'y-', label='Line of Best Fit 3')
plt.plot(raw_data[770:, 3], m4*raw_data[770:, 3]+b4, 'c-', label='Line of Best Fit 4')
plt.grid()
plt.legend()
plt.title('ISEC Transient UTM Data')
plt.ylabel('UTM Northing (m)')
plt.xlabel('UTM Easting (m)')
plt.show()
########## dist from line of best fit UTM ##########
def dist_lobf(x_data, y_data, m, b):
dist_eq = np.absolute(x_data*m - y_data + b)/(np.sqrt(m**2+1))
avgerage_dist_from_lobf = np.mean(dist_eq)
return avgerage_dist_from_lobf
if mode == "soccer":
# Soccer field
avg_error_1 = dist_lobf(raw_data[0:275, 3], raw_data[0:275, 4], m1, b1)
avg_error_2 = dist_lobf(raw_data[290:440, 3], raw_data[290:440, 4], m2, b2)
avg_error_3 = dist_lobf(raw_data[455:675, 3], raw_data[455:675, 4], m3, b3)
avg_error_4 = dist_lobf(raw_data[690:, 3], raw_data[690:, 4], m4, b4)
elif mode == "isec":
# ISEC
avg_error_1 = dist_lobf(raw_data[0:160, 3], raw_data[0:160, 4], m1, b1)
avg_error_2 = dist_lobf(raw_data[170:645, 3], raw_data[170:645, 4], m2, b2)
avg_error_3 = dist_lobf(raw_data[660:765, 3], raw_data[660:765, 4], m3, b3)
avg_error_4 = dist_lobf(raw_data[770:, 3], raw_data[770:, 4], m4, b4)
print('Avg error 1: {}'.format(avg_error_1))
print('Avg error 2: {}'.format(avg_error_2))
print('Avg error 3: {}'.format(avg_error_3))
print('Avg error 4: {}'.format(avg_error_4))
print('Total Avg Error: {}'.format((avg_error_1+avg_error_2+avg_error_3+avg_error_4)/4))
########## plotting data vs trial ##########
def data_vs_trial(y_data, length, title, ylabel, xlabel):
"""
y_data: array, y data to be plotted
length: int, length of data for x range
title: String, title
ylabel: String, y-label
xlabel: String, x-label
"""
x_data = np.arange(length)
m,b = np.polyfit(x_data, y_data, 1)
plt.plot(x_data, y_data, 'b-', label='Data')
plt.plot(x_data, m*x_data+b, 'r-', label='Line of Best Fit')
plt.title(title)
plt.ylabel(ylabel)
plt.xlabel(xlabel)
plt.xlim(right=length)
plt.legend()
plt.grid()
plt.show()
########## latitude vs trial ##########
data_vs_trial(raw_data[:, 0], 998, 'Latitude vs. Trial', 'Latitude (deg)', 'Trial #')
########## longitude vs trial ##########
data_vs_trial(raw_data[:, 1], 998, 'Longitude vs. Trial', 'Longitude (deg)', 'Trial #')
########## altitude vs trial ##########
data_vs_trial(raw_data[:, 2], 848, 'Soccer Field Transient Altitude vs. Trial', 'Altitude (m)', 'Trial #')
########## UTM Easting vs trial ##########
data_vs_trial(raw_data[:, 3], 848, 'UTM Easting vs. Trial', 'UTM Easting (m)', 'Trial #')
########## UTM Northing vs trial ##########
data_vs_trial(raw_data[:, 4], 848, 'UTM Northing vs. Trial', 'UTM Northing (m)', 'Trial #')
########## GNSS Fix vs trial ##########
data_vs_trial(raw_data[:, 5], 1259, 'GNSS Fix vs. Trial', 'GNSS Fix', 'Trial #')
for i in range(5):
i += 1
count = 0
for j in range(848):
if raw_data[j,5] == i:
count += 1
print('Number of {}s: {}'.format(i, count))
labels = ['Fix']
sizes = [100]
colors = ['g']
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True, colors=colors, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.title('Soccer Field Trasient Data GNSS Fix Status\n')
plt.show()