-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_pedestrian.py
227 lines (166 loc) · 7.64 KB
/
old_pedestrian.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
def current_velocity(pedestrian_df, vehicle_df, interact_onepair):
#ped = interact_df.iloc[0]['Pedestrian TrackID']
timestamps = interact_onepair['Timestamp'].values.flatten()
pedestrian_speeds = interact_onepair['Pedestrian Speed'].values
vehicle_speeds = interact_onepair['Vehicle Speed'].values
# plot the data
plt.plot(timestamps, pedestrian_speeds, 'o', markersize=1, label='Pedestrian Speed')
plt.plot(timestamps, vehicle_speeds, 'o', markersize=1, label='Vehicle Speed')
# calculate linear regression for pedestrian speed
x_ped = np.array(timestamps)
y_ped = np.array(pedestrian_speeds)
slope_ped, intercept_ped = np.polyfit(x_ped, y_ped, 1)
plt.plot(x_ped, slope_ped*x_ped + intercept_ped, '-')
# calculate linear regression for vehicle speed
valid_indices = np.where(~np.isnan(vehicle_speeds))[0]
x_veh = np.array(timestamps)[valid_indices]
y_veh = np.array(vehicle_speeds)[valid_indices]
slope_veh, intercept_veh = np.polyfit(x_veh, y_veh, 1)
plt.plot(x_veh, slope_veh*x_veh + intercept_veh, '-')
# add labels and legend
plt.xlabel('Timestamp')
plt.ylabel('Speed')
plt.legend()
# show the plot
plt.show()
"""
# Extract the timestamp and speed values into separate arrays
X = interact_onepair['Timestamp'].values.reshape(-1, 1)
ped_y = interact_onepair['Pedestrian Speed'].values
veh_y = interact_onepair['Vehicle Speed'].values
#curr_ped_df = pedestrian_df[pedestrian_df['track_id'] == ped]
# Create a linear regression model
model = LinearRegression()
# Fit the model to the data
model.fit(X, ped_y)
# Plot the data points and the linear regression line
plt.scatter(X, ped_y, s=1, alpha=0.8)
plt.plot(X, model.predict(X), color='red')
# Create a linear regression model
model = LinearRegression()
# Fit the model to the data
model.fit(X, veh_y)
# Plot the data points and the linear regression line
plt.scatter(X, veh_y, s=10, alpha=0.8)
plt.plot(X, model.predict(X), color='red')
plt.xlabel('Timestamp')
plt.ylabel('Speed')
plt.title('Current Speed of Pedestrian')
plt.show()
print(type(X))
#df = pd.DataFrame({'Timestamp': X, 'Current Speed at that timestamp': y})
#df.to_csv("Generated Files/Current Speed of Pedestrian.csv")
"""
def average_velocity(pedestrian_df, vehicle_df, interact_onepair):
timestamps = interact_onepair['Timestamp'].values.flatten()
pedestrian_speeds = interact_onepair['Pedestrian Speed'].values
vehicle_speeds = interact_onepair['Vehicle Speed'].values
ped_avg = [sum(pedestrian_speeds[:i+1])/(i+1) for i in range(len(pedestrian_speeds))]
ped_avg = np.array(ped_avg)
veh_avg = [sum(vehicle_speeds[:i+1])/(i+1) for i in range(len(vehicle_speeds))]
veh_avg = np.array(veh_avg)
print(veh_avg)
# plot the data
plt.plot(timestamps, ped_avg, 'o', markersize=1, label='Pedestrian Speed')
plt.plot(timestamps, veh_avg, 'o', markersize=1, label='Vehicle Speed')
# calculate linear regression for pedestrian speed
x_ped = np.array(timestamps)
y_ped = np.array(ped_avg)
slope_ped, intercept_ped = np.polyfit(x_ped, y_ped, 1)
plt.plot(x_ped, slope_ped*x_ped + intercept_ped, '-')
# calculate linear regression for vehicle speed
x_veh = np.array(timestamps)
y_veh = np.array(veh_avg)
slope_veh, intercept_veh = np.polyfit(x_veh, y_veh, 1)
plt.plot(x_veh, slope_veh*x_veh + intercept_veh, '-')
# add labels and legend
plt.xlabel('Timestamp')
plt.ylabel('Speed')
plt.legend()
# show the plot
plt.show()
"""
ped = interact_df.iloc[0]['Pedestrian TrackID']
curr_ped_df = pedestrian_df[pedestrian_df['track_id'] == ped]
# Extract the timestamp and speed values into separate arrays
X = curr_ped_df['timestamp_ms'].values.reshape(-1, 1)
y = curr_ped_df['speed'].values
avg = [sum(y[:i+1])/(i+1) for i in range(len(y))]
# Create a linear regression model
model = LinearRegression()
# Fit the model to the data
model.fit(X, avg)
# Plot the data points and the linear regression line
plt.scatter(X, avg, s=10, alpha=0.8)
plt.plot(X, model.predict(X), color='red')
plt.xlabel('Timestamp')
plt.ylabel('Speed')
plt.title('Average Speed of Pedestrian')
plt.show()
#df = pd.DataFrame({'Timestamp': X, 'Average Speed at that timestamp': avg})
#df.to_csv("Generated Files/Average Speed of Pedestrian.csv")
"""
def maximum_velocity(pedestrian_df, interact_df):
ped = interact_df.iloc[0]['Pedestrian TrackID']
curr_ped_df = pedestrian_df[pedestrian_df['track_id'] == ped]
# Extract the timestamp and speed values into separate arrays
X = curr_ped_df['timestamp_ms'].values.reshape(-1, 1)
y = curr_ped_df['speed'].values
max_vals = [max(y[:i+1]) for i in range(len(y))]
# Create a linear regression model
model = LinearRegression()
# Fit the model to the data
model.fit(X, max_vals)
plt.scatter(X, max_vals, s=10, alpha=0.8)
plt.plot(X, model.predict(X), color='red')
plt.xlabel('Timestamp')
plt.ylabel('Speed')
plt.title('Maximum Speed of Pedestrian')
plt.show()
X = curr_ped_df['timestamp_ms'].values
df = pd.DataFrame({'Timestamp': X, 'Maximum Speed at that timestamp': max_vals})
df.to_csv("Generated Files/Maximum Speed of Pedestrian.csv")
def variance_velocity(pedestrian_df, interact_df):
ped = interact_df.iloc[0]['Pedestrian TrackID']
curr_ped_df = pedestrian_df[pedestrian_df['track_id'] == ped]
X = curr_ped_df['timestamp_ms'].values.reshape(-1, 1)
y = curr_ped_df['speed'].values
diff = [0] + [y[i] - y[i-1] for i in range(1, len(y))]
model = LinearRegression()
model.fit(X, diff)
plt.scatter(X, diff, s=10, alpha=0.8)
plt.plot(X, model.predict(X), color='red')
plt.xlabel('Timestamp')
plt.ylabel('Speed')
plt.title('Variance Speed of Pedestrian')
plt.show()
#df = pd.DataFrame({'Timestamp': X, 'Variance Speed at that timestamp': diff})
#df.to_csv("Generated Files/Variance Speed of Pedestrian.csv")
def ratio_velocity(pedestrian_df, interact_df):
ped = interact_df.iloc[0]['Pedestrian TrackID']
curr_ped_df = pedestrian_df[pedestrian_df['track_id'] == ped]
X = curr_ped_df['timestamp_ms'].values.reshape(-1, 1)
distance = curr_ped_df['distance'].values
# Replace NaN values with 0
distance[np.isnan(distance)] = 0
ratio1 = [sum(distance[:i+1]) for i in range(len(distance))]
ratio2 = [0] + [distance[i] - distance[i-1] for i in range(1, len(distance))]
ratio1 = np.array(ratio1)
ratio2 = np.array(ratio2)
y=ratio1/ratio2
y[np.isnan(y)] = 0
y[np.isinf(y)] = 0
model = LinearRegression()
model.fit(X, y)
plt.scatter(X, y, s=10, alpha=0.8)
plt.plot(X, model.predict(X), color='red')
plt.xlabel('Timestamp')
plt.ylabel('Speed')
plt.title('Ratio Distance of Pedestrian')
plt.show()
#df = pd.DataFrame({'Timestamp': X, 'Ratio Speed at that timestamp': y})
#df.to_csv("Generated Files/Ratio Speed of Pedestrian.csv")