Commit 41985de4 authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Merge branch 'master' of gitlab.lrz.de:vadere/vadere

parents 3cfe06b0 fd910ca2
Pipeline #119632 passed with stages
in 215 minutes and 2 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 gc
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import math import math
import matplotlib as mpl import matplotlib as mpl
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib.lines import Line2D from matplotlib.lines import Line2D
import seaborn as sns import seaborn as sns
sns.set(style="ticks") sns.set(style="ticks")
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'])
dataframe["distance"] = np.sqrt(np.square(dataframe["endX"] - dataframe["startX"]) + np.square(dataframe["endY"] - dataframe["startY"])) dataframe["distance"] = np.sqrt(np.square(dataframe["endX"] - dataframe["startX"]) + np.square(dataframe["endY"] - dataframe["startY"]))
dataframe["velocity"] = dataframe["distance"] / (dataframe["endTime"] - dataframe["startTime"]) dataframe["velocity"] = dataframe["distance"] / (dataframe["endTime"] - dataframe["startTime"])
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
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)
rows = [] rows = []
#print(trajectories) #print(trajectories)
last_ped_id = None last_ped_id = None
lastX = None lastX = None
lastY = None lastY = None
for row in data.itertuples(): for row in data.itertuples():
endX = row.x / 100 + 18.7 endX = row.x / 100 + 18.7
endY = row.y / 100 + 4.2 endY = row.y / 100 + 4.2
startTime = row.timeStep / fps - 1/fps startTime = row.timeStep / fps - 1/fps
endTime = row.timeStep / fps endTime = row.timeStep / fps
if last_ped_id is None or last_ped_id != row.pedestrianId: if last_ped_id is None or last_ped_id != row.pedestrianId:
startX = np.nan startX = np.nan
startY = np.nan startY = np.nan
distance = np.nan distance = np.nan
velocity = np.nan velocity = np.nan
else: else:
startX = lastX / 100 + 18.7 startX = lastX / 100 + 18.7
startY = lastY / 100 + 4.2 startY = lastY / 100 + 4.2
distance = np.sqrt(np.square(endX - startX) + np.square(endY - startY)) distance = np.sqrt(np.square(endX - startX) + np.square(endY - startY))
velocity = distance / (endTime - startTime) velocity = distance / (endTime - startTime)
last_ped_id = row.pedestrianId last_ped_id = row.pedestrianId
lastX = row.x lastX = row.x
lastY = row.y lastY = row.y
rows.append([row.pedestrianId, startX, startY, endX, endY, startTime, endTime, distance, velocity]) rows.append([row.pedestrianId, startX, startY, endX, endY, startTime, endTime, distance, velocity])
dataframe = pd.DataFrame(rows, columns=['pedestrianId', 'startX', 'startY', 'endX', 'endY','startTime','endTime','distance','velocity']) dataframe = pd.DataFrame(rows, columns=['pedestrianId', 'startX', 'startY', 'endX', 'endY','startTime','endTime','distance','velocity'])
return dataframe return dataframe
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:
# Calculate evacution time # Calculate evacution time
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Evacuation time = endTime - startTime Evacuation time = endTime - startTime
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Real data ## Real data
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Sum up all measured time deltas of a pedestrian to get the final evacuation time # Sum up all measured time deltas of a pedestrian to get the final evacuation time
trajectoriesReal["timeDelta"] = trajectoriesReal["endTime"] - trajectoriesReal["startTime"] copy = trajectoriesReal.copy(deep=True)
evacuation_time = trajectoriesReal.groupby(["pedestrianId"])["timeDelta"].sum() copy["timeDelta"] = copy["endTime"] - copy["startTime"]
evacuation_time = copy.groupby(["pedestrianId"])["timeDelta"].sum()
# trajectories starting from left # trajectories starting from left
cut_minX = trajectoriesReal[trajectoriesReal["endX"] < 15].groupby(["pedestrianId"])["endX"].min().max() cut_minX = trajectoriesReal[trajectoriesReal["endX"] < 15].groupby(["pedestrianId"])["endX"].min().max()
# trajectories starting from right # trajectories starting from right
cut_maxX = trajectoriesReal[trajectoriesReal["endX"] > 21].groupby(["pedestrianId"])["endX"].max().min() cut_maxX = trajectoriesReal[trajectoriesReal["endX"] > 21].groupby(["pedestrianId"])["endX"].max().min()
cut_maxY = trajectoriesReal.groupby(["pedestrianId"])["endY"].max().min() cut_maxY = trajectoriesReal.groupby(["pedestrianId"])["endY"].max().min()
print("Evacuation time (real data)") print("Evacuation time (real data)")
print("- mean: {:.2f} [s]".format(evacuation_time.mean())) print("- mean: {:.2f} [s]".format(evacuation_time.mean()))
print("- std: {:.2f} [s]".format(evacuation_time.std())) print("- std: {:.2f} [s]".format(evacuation_time.std()))
print("- min: {:.2f} [s]".format(evacuation_time.min())) print("- min: {:.2f} [s]".format(evacuation_time.min()))
print("- max: {:.2f} [s]".format(evacuation_time.max())) print("- max: {:.2f} [s]".format(evacuation_time.max()))
print("- minX: {:.2f} [m]".format(cut_minX)) print("- minX: {:.2f} [m]".format(cut_minX))
print("- maxX: {:.2f} [m]".format(cut_maxX)) print("- maxX: {:.2f} [m]".format(cut_maxX))
print("- maxY: {:.2f} [m]".format(cut_maxY)) print("- maxY: {:.2f} [m]".format(cut_maxY))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rightX = trajectoriesReal[trajectoriesReal.endX < 16].groupby(["pedestrianId"])["endX"].min().max() rightX = trajectoriesReal[trajectoriesReal.endX < 16].groupby(["pedestrianId"])["endX"].min().max()
leftX = trajectoriesReal[trajectoriesReal.endX > 16].groupby(["pedestrianId"])["endX"].max().min() leftX = trajectoriesReal[trajectoriesReal.endX > 16].groupby(["pedestrianId"])["endX"].max().min()
topY = trajectoriesReal.groupby(["pedestrianId"])["endY"].max().min() topY = trajectoriesReal.groupby(["pedestrianId"])["endY"].max().min()
topY topY
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Simulation data ## Simulation data
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
TODO: Use `PedestrianEvacuationTimeProcessor` to log evacuation time during simulation and analyze it here. TODO: Use `PedestrianEvacuationTimeProcessor` to log evacuation time during simulation and analyze it here.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Helper method to access parts of the trajectory # 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_trajectories(t, trajectories): def get_trajectories(t, trajectories):
return trajectories[np.logical_and(trajectories.startTime <= t, trajectories.endTime >= t)] return trajectories[np.logical_and(trajectories.startTime <= t, trajectories.endTime >= t)]
def get_pedestrianIds(trajectories): def get_pedestrianIds(trajectories):
return trajectories['pedestrianId'].unique() return trajectories['pedestrianId'].unique()
#def get_velocity(trajectories, t, dt): #def get_velocity(trajectories, t, dt):
# trajectories[np.logical_and(trajectory.endX >= xmax, trajectory.startX < xmax)] # trajectories[np.logical_and(trajectory.endX >= xmax, trajectory.startX < xmax)]
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)
def cuthead_trajectory_by(trajectory, ymin, ymax): def cuthead_trajectory_by(trajectory, ymin, ymax):
i1 = trajectory[trajectory.endY >= ymax].index.min() i1 = trajectory[trajectory.endY >= ymax].index.min()
i2 = trajectory[trajectory.endY <= ymin].index.min() i2 = trajectory[trajectory.endY <= ymin].index.min()
#assert (i1 is np.nan and i2 is not np.nan) or (i1 is not np.nan and i2 is np.nan) #assert (i1 is np.nan and i2 is not np.nan) or (i1 is not np.nan and i2 is np.nan)
y = ymax if i2 is np.nan or (i1 is not np.nan and i1 < i2) else ymin y = ymax if i2 is np.nan or (i1 is not np.nan and i1 < i2) else ymin
i = i1 if y == ymax else i2 i = i1 if y == ymax else i2
#print(i) #print(i)
# cut the footstep at the tail to exactly fit xmin or xmax # cut the footstep at the tail to exactly fit xmin or xmax
fs = trajectory.loc[i] fs = trajectory.loc[i]
start = np.array([fs["startX"], fs["startY"]]) start = np.array([fs["startX"], fs["startY"]])
end = np.array([fs["endX"], fs["endY"]]) end = np.array([fs["endX"], fs["endY"]])
endTime = fs["endTime"] endTime = fs["endTime"]
startTime = fs["startTime"] startTime = fs["startTime"]
distance = fs["distance"] distance = fs["distance"]
velocity = fs["velocity"] velocity = fs["velocity"]
d = end - start d = end - start
if abs(fs["endY"] - fs["startY"]) > 0.00001: if abs(fs["endY"] - fs["startY"]) > 0.00001:
r = (y - fs["startY"]) / (fs["endY"] - fs["startY"]) r = (y - fs["startY"]) / (fs["endY"] - fs["startY"])
end = start + (d * r) end = start + (d * r)
time = fs["endTime"] - fs["startTime"] time = fs["endTime"] - fs["startTime"]
endTime = fs["startTime"] + (time * r) endTime = fs["startTime"] + (time * r)
distance = np.linalg.norm(end - start) distance = np.linalg.norm(end - start)
velocity = distance / (endTime - startTime) velocity = distance / (endTime - startTime)
df = trajectory.loc[:i-1] df = trajectory.loc[:i-1]
llist = [[fs["pedestrianId"],fs["startX"],fs["startY"],fs["startTime"],end[0],end[1],endTime,distance,velocity]] llist = [[fs["pedestrianId"],fs["startX"],fs["startY"],fs["startTime"],end[0],end[1],endTime,distance,velocity]]
df_head = pd.DataFrame(llist, columns=['pedestrianId','startX','startY','startTime','endX','endY','endTime','distance','velocity']) df_head = pd.DataFrame(llist, columns=['pedestrianId','startX','startY','startTime','endX','endY','endTime','distance','velocity'])
df = df.append(df_head, ignore_index=True) df = df.append(df_head, ignore_index=True)
return df return df
def cuttail_trajectory_by(trajectory, xmin, xmax): def cuttail_trajectory_by(trajectory, xmin, xmax):
#i1 = trajectory[np.logical_and(trajectory.endX >= xmax, trajectory.startX < xmax)].index.max() #i1 = trajectory[np.logical_and(trajectory.endX >= xmax, trajectory.startX < xmax)].index.max()
i1 = trajectory[trajectory.endX >= xmax].index.max() i1 = trajectory[np.logical_or(trajectory.endX >= xmax, trajectory.startX is np.nan)].index.max()
i2 = trajectory[trajectory.endX <= xmin].index.max() i2 = trajectory[np.logical_or(trajectory.endX <= xmin, trajectory.startX is np.nan)].index.max()
#assert (i1 is np.nan and i2 is not np.nan) or (i1 is not np.nan and i2 is np.nan) #assert (i1 is np.nan and i2 is not np.nan) or (i1 is not np.nan and i2 is np.nan)
x = xmax if i2 is np.nan or (i1 is not np.nan and i1 > i2) else xmin x = xmax if i2 is np.nan or (i1 is not np.nan and i1 > i2) else xmin
i = i1 if x == xmax else i2 i = i1 if x == xmax else i2
i = i+1 i = i+1
# cut the footstep at the tail to exactly fit xmin or xmax # cut the footstep at the tail to exactly fit xmin or xmax
fs = trajectory.loc[i] fs = trajectory.loc[i]
start = np.array([fs["startX"], fs["startY"]]) start = np.array([fs["startX"], fs["startY"]])
end = np.array([fs["endX"], fs["endY"]]) end = np.array([fs["endX"], fs["endY"]])
startTime = fs["startTime"]