Commit afde08fe authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Added analysis of evacuation time (of real data) to "TrajectoryMetric.ipynb".

parent b6424114
Pipeline #116168 passed with stages
in 136 minutes and 42 seconds
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# expand the cell of the notebook # expand the cell of the notebook
import json import json
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import math import math
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib.lines import Line2D from matplotlib.lines import Line2D
from IPython.core.display import display, HTML from IPython.core.display import display, HTML
display(HTML('<style>.container { width:100% !important; }</style>')) display(HTML('<style>.container { width:100% !important; }</style>'))
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Convert Vadere trajectories into a DataFrame # Convert Vadere trajectories into a DataFrame
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def fs_append(pedestrianId, fs, llist): def fs_append(pedestrianId, fs, llist):
llist.append([pedestrianId, fs['start']['x'], fs['start']['y'], fs['startTime'], fs['end']['x'], fs['end']['y'], fs['endTime']]) llist.append([pedestrianId, fs['start']['x'], fs['start']['y'], fs['startTime'], fs['end']['x'], fs['end']['y'], fs['endTime']])
def trajectory_append(pedestrianId, trajectory, llist): def trajectory_append(pedestrianId, trajectory, llist):
for fs in trajectory: for fs in trajectory:
fs_append(pedestrianId, fs, llist) fs_append(pedestrianId, fs, llist)
def trajectories_to_dataframe(trajectories): def trajectories_to_dataframe(trajectories):
llist = [] llist = []
for pedId in trajectories: for pedId in trajectories:
trajectory_append(pedId, trajectories[pedId], llist) trajectory_append(pedId, trajectories[pedId], llist)
dataframe = pd.DataFrame(llist, columns=['pedestrianId','startX','startY','startTime','endX','endY','endTime']) dataframe = pd.DataFrame(llist, columns=['pedestrianId','startX','startY','startTime','endX','endY','endTime'])
return dataframe return dataframe
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
file = "./data/TrajectoryMetric/trajectories_simulation.txt" file = "./data/TrajectoryMetric/trajectories_simulation.txt"
f = open(file, "r") f = open(file, "r")
header = f.readline(); header = f.readline();
trajectories = dict({}); trajectories = dict({});
for row in f: for row in f:
s = row.split(" "); s = row.split(" ");
pedId = int(s[0]); pedId = int(s[0]);
footsteps = json.loads(s[1]); footsteps = json.loads(s[1]);
trajectories[pedId] = footsteps[0]['footSteps']; trajectories[pedId] = footsteps[0]['footSteps'];
ptrajectories = trajectories_to_dataframe(trajectories) ptrajectories = trajectories_to_dataframe(trajectories)
ptrajectories.head() ptrajectories.head()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Convert experiment data into a DataFrame # Convert experiment data into a DataFrame
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def load_experiment(file): def load_experiment(file):
fps = 16 fps = 16
pad = pd.DataFrame([[np.nan, np.nan, np.nan, np.nan, np.nan]], columns=['pedestrianId', 'timeStep', 'x', 'y', 'e']) pad = pd.DataFrame([[np.nan, np.nan, np.nan, np.nan, np.nan]], columns=['pedestrianId', 'timeStep', 'x', 'y', 'e'])
data = pd.read_csv( data = pd.read_csv(
file, file,
sep=' ', sep=' ',
names=['pedestrianId', 'timeStep', 'x', 'y', 'e'], names=['pedestrianId', 'timeStep', 'x', 'y', 'e'],
index_col=False, index_col=False,
header=None, header=None,
skiprows=0) skiprows=0)
cc = pd.concat([pad, data], ignore_index=True) cc = pd.concat([pad, data], ignore_index=True)
data['endX'] = data['x'] / 100 + 18.7 data['endX'] = data['x'] / 100 + 18.7
data['endY'] = data['y'] / 100 + 4.2 data['endY'] = data['y'] / 100 + 4.2
data['startX'] = cc['x'] / 100 + 18.7 data['startX'] = cc['x'] / 100 + 18.7
data['startY'] = cc['y'] / 100 + 4.2 data['startY'] = cc['y'] / 100 + 4.2
data['startTime'] = data['timeStep'] / fps - 1/fps data['startTime'] = data['timeStep'] / fps - 1/fps
data['endTime'] = data['timeStep'] / fps data['endTime'] = data['timeStep'] / fps
data = data.drop(columns=['timeStep','x','y','e']) data = data.drop(columns=['timeStep','x','y','e'])
return data return data
def to_trajectories(data): def to_trajectories(data):
trajectories = dict({}) trajectories = dict({})
trajectory = [] trajectory = []
for i in range(len(data)-1): for i in range(len(data)-1):
pedId = data['pedestrianId'][i] pedId = data['pedestrianId'][i]
if pedId == data['pedestrianId'][i+1]: if pedId == data['pedestrianId'][i+1]:
pedId = data['pedestrianId'][i] pedId = data['pedestrianId'][i]
x1 = data['x'][i] x1 = data['x'][i]
y1 = data['y'][i] y1 = data['y'][i]
x2 = data['x'][i+1] x2 = data['x'][i+1]
y2 = data['y'][i+1] y2 = data['y'][i+1]
startTime = data['timeStep'][i] startTime = data['timeStep'][i]
endTime = data['timeStep'][i+1] endTime = data['timeStep'][i+1]
fs = {'startTime':startTime, 'endTime': endTime, 'start':{'x':x1, 'y':y1}, 'end':{'x':x2, 'y':y2}} fs = {'startTime':startTime, 'endTime': endTime, 'start':{'x':x1, 'y':y1}, 'end':{'x':x2, 'y':y2}}
trajectory.append(fs) trajectory.append(fs)
else: else:
trajectories[pedId] = trajectory trajectories[pedId] = trajectory
trajectory = [] trajectory = []
pedId = data['pedestrianId'][i] pedId = data['pedestrianId'][i]
return trajectories return trajectories
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#times = np.linspace(4,10,10) #times = np.linspace(4,10,10)
#euclid_d(get_trajectory(1), get_trajectory(1), times) #euclid_d(get_trajectory(1), get_trajectory(1), times)
#to_trajectories(load_experiment(real_file))[1] #to_trajectories(load_experiment(real_file))[1]
real_file = "./data/TrajectoryMetric/KO/ko-240-120-240/ko-240-120-240_combined_MB.txt" real_file = "./data/TrajectoryMetric/KO/ko-240-120-240/ko-240-120-240_combined_MB.txt"
trajectoriesReal = load_experiment(real_file) trajectoriesReal = load_experiment(real_file)
#trajectoriesReal = to_trajectories(data) #trajectoriesReal = to_trajectories(data)
trajectoriesReal.query('pedestrianId == 1').head() trajectoriesReal.query('pedestrianId == 1').head()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Convert DataFrame to postvis DataFrame # Convert DataFrame to postvis DataFrame
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def to_postVis(df): def to_postVis(df):
simTimeStep = 0.4 simTimeStep = 0.4
fps = 16 fps = 16
df['timeStep'] = np.ceil(df['endTime'] / (1/fps)).astype(np.int) df['timeStep'] = np.ceil(df['endTime'] / (1/fps)).astype(np.int)
df['x'] = df['endX'] df['x'] = df['endX']
df['y'] = df['endY'] df['y'] = df['endY']
df['simTime'] = df['endTime'] df['simTime'] = df['endTime']
df = df.drop(columns=['startX','startY','endX','endY','startTime', 'endTime']) df = df.drop(columns=['startX','startY','endX','endY','startTime', 'endTime'])
return df return df
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
to_postVis(trajectoriesReal).to_csv('expteriment_2.trajectories',index=False,sep=' ') to_postVis(trajectoriesReal).to_csv('expteriment_2.trajectories',index=False,sep=' ')
to_postVis(trajectoriesReal).head(10) to_postVis(trajectoriesReal).head(10)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Helpler method to access parts of the trajectory # Calculate evacution time
%% Cell type:markdown id: tags:
Evacuation time = endTime - startTime
%% Cell type:markdown id: tags:
## Real data
%% Cell type:code id: tags:
``` python
# Sum up all measured time deltas of a pedestrian to get the final evacuation time
trajectoriesReal["timeDelta"] = trajectoriesReal["endTime"] - trajectoriesReal["startTime"]
evacuation_time = trajectoriesReal.groupby(["pedestrianId"])["timeDelta"].sum()
print("Evacuation time (real data)")
print("- mean: {:.2f} [s]".format(evacuation_time.mean()))
print("- std: {:.2f} [s]".format(evacuation_time.std()))
print("- min: {:.2f} [s]".format(evacuation_time.min()))
print("- max: {:.2f} [s]".format(evacuation_time.max()))
```
%% Cell type:markdown id: tags:
## Simulation data
%% Cell type:markdown id: tags:
TODO: Use `PedestrianEvacuationTimeProcessor` to log evacuation time during simulation and analyze it here.
%% Cell type:markdown id: tags:
# Helper method to access parts of the trajectory
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def get_trajectory(pedId, trajectories): def get_trajectory(pedId, trajectories):
"""returns a data frame containing the trajectory of one specific agent.""" """returns a data frame containing the trajectory of one specific agent."""
query = 'pedestrianId == ' + str(pedId) query = 'pedestrianId == ' + str(pedId)
return trajectories.query(query) return trajectories.query(query)
def get_pedestrianIds(trajectories): def get_pedestrianIds(trajectories):
return trajectories['pedestrianId'].unique() return trajectories['pedestrianId'].unique()
def get_footstep(trajectory, i): def get_footstep(trajectory, i):
"""returns the i-ths footstep.""" """returns the i-ths footstep."""
return trajectory.iloc[i]; return trajectory.iloc[i];
def get_footstep_by_time(trajectory, time): def get_footstep_by_time(trajectory, time):
"""returns the footstep which happens at time or nothing (None).""" """returns the footstep which happens at time or nothing (None)."""
query = 'startTime <= ' + str(time) + ' and ' + str(time) + ' < endTime' query = 'startTime <= ' + str(time) + ' and ' + str(time) + ' < endTime'
fs = trajectories.query(query) fs = trajectories.query(query)
assert len(fs) >= 1 assert len(fs) >= 1
return fs return fs
def start_time(trajectory): def start_time(trajectory):
"""returns the time of the first footstep of the trajectory.""" """returns the time of the first footstep of the trajectory."""
return get_footstep(trajectory, 0)['startTime']; return get_footstep(trajectory, 0)['startTime'];
def end_time(trajectory): def end_time(trajectory):
return get_footstep(trajectory, len(trajectory)-1)['endTime']; return get_footstep(trajectory, len(trajectory)-1)['endTime'];
def max_start_time(trajectories): def max_start_time(trajectories):
"""returns the time of the first footstep of the trajectory which starts last.""" """returns the time of the first footstep of the trajectory which starts last."""
pedestrianIds = get_pedestrianIds(trajectories) pedestrianIds = get_pedestrianIds(trajectories)
return max(map(lambda pedId: start_time(get_trajectory(pedId, trajectories)), pedestrianIds)) return max(map(lambda pedId: start_time(get_trajectory(pedId, trajectories)), pedestrianIds))
def min_end_time(trajectories): def min_end_time(trajectories):
"""returns the time of the last footstep of the trajectory which ends first.""" """returns the time of the last footstep of the trajectory which ends first."""
pedestrianIds = get_pedestrianIds(trajectories) pedestrianIds = get_pedestrianIds(trajectories)
return min(map(lambda pedId: end_time(get_trajectory(pedId, trajectories)), pedestrianIds)) return min(map(lambda pedId: end_time(get_trajectory(pedId, trajectories)), pedestrianIds))
def footstep_is_between(fs, time): def footstep_is_between(fs, time):
"""true if the foostep and the intersection with time is not empty.""" """true if the foostep and the intersection with time is not empty."""
startTime = fs['startTime']; startTime = fs['startTime'];
endTime = fs['endTime']; endTime = fs['endTime'];
return startTime <= time and time < endTime; return startTime <= time and time < endTime;
def cut(trajectory, sTime, eTime): def cut(trajectory, sTime, eTime):
query = 'startTime >= ' + str(sTime) + ' and endTime < ' + str(eTime) query = 'startTime >= ' + str(sTime) + ' and endTime < ' + str(eTime)
return trajectory.query(query) return trajectory.query(query)
def cut_soft(trajectory, sTime, eTime): def cut_soft(trajectory, sTime, eTime):
query = 'endTime > ' + str(sTime) + ' and startTime < ' + str(eTime) query = 'endTime > ' + str(sTime) + ' and startTime < ' + str(eTime)
return trajectory.query(query) return trajectory.query(query)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Helper methods to compute different metrices # Helper methods to compute different metrices
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def footstep_length(fs): def footstep_length(fs):
"""Euclidean length of a footstep.""" """Euclidean length of a footstep."""
x1 = fs['startX']; x1 = fs['startX'];
y1 = fs['startY']; y1 = fs['startY'];
x2 = fs['endX']; x2 = fs['endX'];
y2 = fs['endY']; y2 = fs['endY'];
dx = x1-x2; dx = x1-x2;
dy = y1-y2; dy = y1-y2;
return np.sqrt(dx*dx + dy*dy); return np.sqrt(dx*dx + dy*dy);
def trajectory_length(trajectory): def trajectory_length(trajectory):
"""Euclidean length of a trajectory.""" """Euclidean length of a trajectory."""
dx = trajectory['startX']-trajectory['endX'] dx = trajectory['startX']-trajectory['endX']
dy = trajectory['startY']-trajectory['endY'] dy = trajectory['startY']-trajectory['endY']
return np.sqrt(dx*dx + dy*dy).sum(); return np.sqrt(dx*dx + dy*dy).sum();
def footstep_direction(fs): def footstep_direction(fs):
"""Vector from start to end position.""" """Vector from start to end position."""
x1 = fs['startX']; x1 = fs['startX'];
y1 = fs['startY']; y1 = fs['startY'];
x2 = fs['endX']; x2 = fs['endX'];
y2 = fs['endY']; y2 = fs['endY'];
return np.array([x2-x1, y2-y1]); return np.array([x2-x1, y2-y1]);
def footstep_duration(fs): def footstep_duration(fs):
"""Duration of a footstep.""" """Duration of a footstep."""
startTime = fs['startTime']; startTime = fs['startTime'];
endTime = fs['endTime']; endTime = fs['endTime'];
return endTime-startTime; return endTime-startTime;
def trajectory_duration(trajectory): def trajectory_duration(trajectory):
"""Euclidean length of a trajectory.""" """Euclidean length of a trajectory."""
return (trajectory['endTime'] - trajectory['startTime']).sum(); return (trajectory['endTime'] - trajectory['startTime']).sum();
def footstep_speed(fs): def footstep_speed(fs):
"""Speed of the footstep.""" """Speed of the footstep."""
return footstep_length(fs) / footstep_duration(fs); return footstep_length(fs) / footstep_duration(fs);
def trajectory_speed(fs): def trajectory_speed(fs):
"""Speed of the trajectory.""" """Speed of the trajectory."""
return trajectory_length(fs) / trajectory_duration(fs); return trajectory_length(fs) / trajectory_duration(fs);
#def trajectory_positions(trajectory, times): #def trajectory_positions(trajectory, times):
# mask = trajectory[['startTime', 'endTime']].mask(lambda x: x**2) # mask = trajectory[['startTime', 'endTime']].mask(lambda x: x**2)
# (df['date'] > '2000-6-1') & (df['date'] <= '2000-6-10') # (df['date'] > '2000-6-1') & (df['date'] <= '2000-6-10')
# duration = trajectory['endTime'] - trajectory['startTime'] # duration = trajectory['endTime'] - trajectory['startTime']
# dx = trajectory['endX'] - trajectory['startX'] # dx = trajectory['endX'] - trajectory['startX']
# dy = trajectory['endY'] - trajectory['startY'] # dy = trajectory['endY'] - trajectory['startY']
# direction = # direction =
def filter_trajectories(trajectories, times): def filter_trajectories(trajectories, times):
"""Filters trajectory by times.""" """Filters trajectory by times."""
rows = [] rows = []
for row in trajectories.itertuples(): for row in trajectories.itertuples():
if len(list(filter(lambda b: b, map(lambda t: row.startTime <= t and t < row.endTime, times)))) > 0: if len(list(filter(lambda b: b, map(lambda t: row.startTime <= t and t < row.endTime, times)))) > 0:
rows.append(row) rows.append(row)
return pd.DataFrame(rows) return pd.DataFrame(rows)
def trajectories_position(trajectories, times): def trajectories_position(trajectories, times):
"""Transforms trajectories into positions at each time in times such that each position is computed by linear interpolation.""" """Transforms trajectories into positions at each time in times such that each position is computed by linear interpolation."""
rows = [] rows = []
#print(trajectories) #print(trajectories)
for row in trajectories.itertuples(): for row in trajectories.itertuples():
llist = list(filter(lambda t: row.startTime <= t and t < row.endTime, times)) llist = list(filter(lambda t: row.startTime <= t and t < row.endTime, times))
assert len(llist) == 1 or len(llist) == 0 assert len(llist) == 1 or len(llist) == 0
if len(llist) > 0: if len(llist) > 0:
time = llist[0] time = llist[0]
dur = row.endTime - row.startTime dur = row.endTime - row.startTime
partial_dur = time - row.startTime partial_dur = time - row.startTime
ratio = partial_dur / dur ratio = partial_dur / dur
direction = np.array([row.endX - row.startX, row.endY - row.startY]) direction = np.array([row.endX - row.startX, row.endY - row.startY])
l = np.linalg.norm(direction) l = np.linalg.norm(direction)
if l > 0: if l > 0:
partial_l = l * ratio; partial_l = l * ratio;
v = direction / l * partial_l; v = direction / l * partial_l;
pos = np.array([row.startX, row.startY]) + v; pos = np.array([row.startX, row.startY]) + v;
rows.append([row.pedestrianId, pos[0], pos[1], time]) rows.append([row.pedestrianId, pos[0], pos[1], time])
else: else:
rows.append([row.pedestrianId, np.nan, np.nan, time]) rows.append([row.pedestrianId, np.nan, np.nan, time])
dataframe = pd.DataFrame(rows, columns=['pedestrianId','x','y','time']) dataframe = pd.DataFrame(rows, columns=['pedestrianId','x','y','time'])
return dataframe return dataframe
def euclid_d(trajPos1, trajPos2): def euclid_d(trajPos1, trajPos2):
"""Computes the total (Euclidean) distance between two trajectories. """Computes the total (Euclidean) distance between two trajectories.
Assumption: trajectories are both cut acccordingly! Assumption: trajectories are both cut acccordingly!
""" """
assert len(trajPos1) == len(trajPos2) assert len(trajPos1) == len(trajPos2)
dx = trajPos1['x'] - trajPos2['x'] dx = trajPos1['x'] - trajPos2['x']
dy = trajPos1['y'] - trajPos2['y'] dy = trajPos1['y'] - trajPos2['y']
norm = np.sqrt(dx**2 + dy**2) norm = np.sqrt(dx**2 + dy**2)
return norm.sum() / len(dx) return norm.sum() / len(dx)
def euclid_path_length(trajPos1, trajPos2): def euclid_path_length(trajPos1, trajPos2):
"""Computes the total (Euclidean) path length difference between two trajectories. """Computes the total (Euclidean) path length difference between two trajectories.
Assumption: trajectories are both cut acccordingly! Assumption: trajectories are both cut acccordingly!
""" """
count = len(trajPos1) count = len(trajPos1)
pad = pd.DataFrame([[np.nan, np.nan, np.nan, np.nan]], columns=['pedestrianId','x','y','time']) pad = pd.DataFrame([[np.nan, np.nan, np.nan, np.nan]], columns=['pedestrianId','x','y','time'])
trajPos1Pad = pd.concat([pad, trajPos1], ignore_index=True) trajPos1Pad = pd.concat([pad, trajPos1], ignore_index=True)
trajPos2Pad = pd.concat([pad, trajPos1], ignore_index=True) trajPos2Pad = pd.concat([pad, trajPos1], ignore_index=True)