The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

Commit ec049e49 authored by Benedikt Zoennchen's avatar Benedikt Zoennchen
Browse files

update metric notebook, add data for the notebook such that one can experiment with it.

parent 819e9600
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
......@@ -29,7 +22,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Methods to convert Vadere trajectories into a DataFrame"
"# Convert Vadere trajectories into a DataFrame"
]
},
{
......@@ -50,9 +43,16 @@
" for pedId in trajectories:\n",
" trajectory_append(pedId, trajectories[pedId], llist)\n",
" dataframe = pd.DataFrame(llist, columns=['pedestrianId','startX','startY','startTime','endX','endY','endTime'])\n",
" return dataframe\n",
"\n",
"file = \"./data/trajectories_distance.txt\"\n",
" return dataframe"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file = \"./data/TrajectoryMetric/trajectories_simulation.txt\"\n",
"f = open(file, \"r\")\n",
"header = f.readline();\n",
"trajectories = dict({});\n",
......@@ -67,6 +67,113 @@
"ptrajectories.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Convert experiment data into a DataFrame"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def load_experiment(file):\n",
" fps = 16\n",
" pad = pd.DataFrame([[np.nan, np.nan, np.nan, np.nan, np.nan]], columns=['pedestrianId', 'timeStep', 'x', 'y', 'e'])\n",
" data = pd.read_csv(\n",
" file, \n",
" sep=' ', \n",
" names=['pedestrianId', 'timeStep', 'x', 'y', 'e'], \n",
" index_col=False, \n",
" header=None, \n",
" skiprows=0)\n",
" \n",
" cc = pd.concat([pad, data], ignore_index=True)\n",
" \n",
" data['endX'] = data['x'] / 100 + 18.7\n",
" data['endY'] = data['y'] / 100 + 4.2\n",
" data['startX'] = cc['x'] / 100 + 18.7\n",
" data['startY'] = cc['y'] / 100 + 4.2\n",
" data['startTime'] = data['timeStep'] / fps - 1/fps\n",
" data['endTime'] = data['timeStep'] / fps\n",
" data = data.drop(columns=['timeStep','x','y','e'])\n",
" return data\n",
" \n",
"def to_trajectories(data):\n",
" trajectories = dict({})\n",
" trajectory = []\n",
" for i in range(len(data)-1):\n",
" pedId = data['pedestrianId'][i]\n",
" if pedId == data['pedestrianId'][i+1]:\n",
" pedId = data['pedestrianId'][i]\n",
" x1 = data['x'][i]\n",
" y1 = data['y'][i]\n",
" x2 = data['x'][i+1]\n",
" y2 = data['y'][i+1]\n",
" startTime = data['timeStep'][i] \n",
" endTime = data['timeStep'][i+1]\n",
" fs = {'startTime':startTime, 'endTime': endTime, 'start':{'x':x1, 'y':y1}, 'end':{'x':x2, 'y':y2}}\n",
" trajectory.append(fs)\n",
" else:\n",
" trajectories[pedId] = trajectory\n",
" trajectory = []\n",
" pedId = data['pedestrianId'][i]\n",
" return trajectories"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#times = np.linspace(4,10,10)\n",
"#euclid_d(get_trajectory(1), get_trajectory(1), times)\n",
"#to_trajectories(load_experiment(real_file))[1]\n",
"\n",
"real_file = \"./data/TrajectoryMetric/KO/ko-240-120-240/ko-240-120-240_combined_MB.txt\"\n",
"trajectoriesReal = load_experiment(real_file)\n",
"#trajectoriesReal = to_trajectories(data)\n",
"trajectoriesReal.query('pedestrianId == 1').head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Convert DataFrame to postvis DataFrame"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def to_postVis(df):\n",
" simTimeStep = 0.4\n",
" fps = 16\n",
" df['timeStep'] = np.ceil(df['endTime'] / (1/fps)).astype(np.int)\n",
" df['x'] = df['endX']\n",
" df['y'] = df['endY']\n",
" df['simTime'] = df['endTime']\n",
" df = df.drop(columns=['startX','startY','endX','endY','startTime', 'endTime']) \n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"to_postVis(trajectoriesReal).to_csv('expteriment_2.trajectories',index=False,sep=' ')\n",
"to_postVis(trajectoriesReal).head(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -284,8 +391,15 @@
" \n",
"def total_inter_agent(trajectories1, trajectories2, times):\n",
" \"\"\"too expensive! TODO!\"\"\"\n",
" return sum(map(lambda t: inter_agent_d(trajectories_position(trajectories1, [t])) - inter_agent_d(trajectories_position(trajectories2, [t])), times)) / len(times)\n",
" \n",
" return sum(map(lambda t: inter_agent_d(trajectories_position(trajectories1, [t])) - inter_agent_d(trajectories_position(trajectories2, [t])), times)) / len(times)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#start_time(get_trajectory(1, ptrajectories))\n",
"#max_start_time(ptrajectories)\n",
"#end_time(get_trajectory(1, ptrajectories))\n",
......@@ -305,22 +419,6 @@
"#print(total_inter_agent(ptrajectories, ptrajectories, [1,2]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"trajPos2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
......@@ -329,75 +427,6 @@
},
"outputs": [],
"source": [
"def position(trajectory, time):\n",
" fs = footstep(trajectory, time);\n",
" if fs != None:\n",
" startTime = fs['startTime'];\n",
" endTime = fs['endTime'];\n",
" dur = duration(fs);\n",
" partial_dur = time - startTime;\n",
" ratio = partial_dur / dur;\n",
" start = fs['start'];\n",
" x1 = start['x'];\n",
" y1 = start['y'];\n",
" l = length(fs);\n",
" if l == 0.0:\n",
" return np.array([x1, y1])\n",
" else: \n",
" partial_l = l * ratio;\n",
" v = direction(fs) / l * partial_l;\n",
" return np.array([x1, y1]) + v;\n",
"\n",
"\n",
"def euclid_d(traj1, traj2, times):\n",
" \"\"\"Computes the total (Euclidean) distance between two trajectories at certain times.\"\"\"\n",
" return 0\n",
" sT = max([start_time(traj1), start_time(traj2)])\n",
" eT = min([end_time(traj1), end_time(traj2)])\n",
" filtered_times = list(filter(lambda t: t >= sT and t <= eT, times))\n",
" overlaps = len(filtered_times)\n",
" if overlaps == 0:\n",
" return 0\n",
" return sum(map(lambda t: np.linalg.norm(position(traj1, t)- position(traj2, t)), filtered_times)) / overlaps\n",
" \n",
"def euclid_path_length(traj1, traj2, times):\n",
" sT = max([start_time(traj1), start_time(traj2)]);\n",
" eT = min([end_time(traj1), end_time(traj2)]);\n",
" filtered_times = list(filter(lambda t: t >= sT and t <= eT, times));\n",
" s = np.array([0, 0])\n",
" for i in range(len(filtered_times)-1):\n",
" t1 = filtered_times[i]\n",
" t2 = filtered_times[i+1]\n",
" d1 = position(traj1, t1) - position(traj1, t2)\n",
" d2 = position(traj2, t1) - position(traj2, t2)\n",
" diff = d1 - d2\n",
" s = s + diff\n",
" return s;\n",
"\n",
"def inter_agent_d(trajectories, t):\n",
" s = 0\n",
" min_index = min(trajectories.keys())\n",
" c = 0\n",
" for i in range(len(trajectories)):\n",
" pos1 = position(trajectories[i+min_index], t)\n",
" for j in range(i+1, len(trajectories)):\n",
" pos2 = position(trajectories[j+min_index], t)\n",
" if pos1 is not None and pos2 is not None:\n",
" s = s + np.linalg.norm(pos1 - pos2)\n",
" c = c + 1\n",
" if c == 0:\n",
" return 0\n",
" else:\n",
" return s / c\n",
" \n",
"def total_inter_agent(trajectories1, trajectories2, times):\n",
" return sum(map(lambda t: inter_agent_d(trajectories1, t) - inter_agent_d(trajectories2, t), times)) / len(times)\n",
" \n",
"def euclid_len(trajectory, sTime, eTime):\n",
" \"\"\"Computes the total (Euclidean) length of the trajectory in between [sTime;eTime].\"\"\"\n",
" cut_traj = cut_soft(trajectory, sTime, eTime);\n",
" return trajectory_length(cut_traj)\n",
"\n",
"def greedy_match(trajectories1, trajectories2, times, f):\n",
" \"\"\"Computes a match of trajectories by using a greedy algorithm.\"\"\"\n",
" assert len(trajectories1) == len(trajectories2)\n",
......@@ -418,82 +447,14 @@
" minVal = val\n",
" match[i] = minIndex\n",
" indexSet.remove(minIndex)\n",
" return match\n",
" \n",
"def overlap(traj1, traj2, dt):\n",
" return True\n",
" \n",
"def load_experiment(file):\n",
" fps = 16\n",
" pad = pd.DataFrame([[np.nan, np.nan, np.nan, np.nan, np.nan]], columns=['pedestrianId', 'timeStep', 'x', 'y', 'e'])\n",
" data = pd.read_csv(\n",
" file, \n",
" sep=' ', \n",
" names=['pedestrianId', 'timeStep', 'x', 'y', 'e'], \n",
" index_col=False, \n",
" header=None, \n",
" skiprows=0)\n",
" \n",
" cc = pd.concat([pad, data], ignore_index=True)\n",
" \n",
" data['endX'] = data['x'] / 100 + 18.7\n",
" data['endY'] = data['y'] / 100 + 4.2\n",
" data['startX'] = cc['x'] / 100 + 18.7\n",
" data['startY'] = cc['y'] / 100 + 4.2\n",
" data['startTime'] = data['timeStep'] / fps - 1/fps\n",
" data['endTime'] = data['timeStep'] / fps\n",
" data = data.drop(columns=['timeStep','x','y','e'])\n",
" return data\n",
" \n",
"def to_trajectories(data):\n",
" trajectories = dict({})\n",
" trajectory = []\n",
" for i in range(len(data)-1):\n",
" pedId = data['pedestrianId'][i]\n",
" if pedId == data['pedestrianId'][i+1]:\n",
" pedId = data['pedestrianId'][i]\n",
" x1 = data['x'][i]\n",
" y1 = data['y'][i]\n",
" x2 = data['x'][i+1]\n",
" y2 = data['y'][i+1]\n",
" startTime = data['timeStep'][i] \n",
" endTime = data['timeStep'][i+1]\n",
" fs = {'startTime':startTime, 'endTime': endTime, 'start':{'x':x1, 'y':y1}, 'end':{'x':x2, 'y':y2}}\n",
" trajectory.append(fs)\n",
" else:\n",
" trajectories[pedId] = trajectory\n",
" trajectory = []\n",
" pedId = data['pedestrianId'][i]\n",
" return trajectories\n",
"\n",
"def to_postVis(df):\n",
" simTimeStep = 0.4\n",
" fps = 16\n",
" df['timeStep'] = np.ceil(df['endTime'] / (1/fps)).astype(np.int)\n",
" df['x'] = df['endX']\n",
" df['y'] = df['endY']\n",
" df['simTime'] = df['endTime']\n",
" df = df.drop(columns=['startX','startY','endX','endY','startTime', 'endTime']) \n",
" return df\n",
"#times = np.linspace(4,10,10)\n",
"#euclid_d(get_trajectory(1), get_trajectory(1), times)\n",
"#to_trajectories(load_experiment(real_file))[1]\n",
"\n",
"file = \"./data/trajectories_distance.txt\"\n",
"real_file = \"./data/KO/ko-240-120-240/ko-240-120-240_combined_MB.txt\""
" return match"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"data = load_experiment(real_file)\n",
"#trajectoriesReal = to_trajectories(data)\n",
"data.query('pedestrianId == 1').head()\n",
"to_postVis(data).to_csv('expteriment_2.trajectories',index=False,sep=' ')\n",
"to_postVis(data).head(10)"
"# Plot trajectories"
]
},
{
......@@ -502,44 +463,47 @@
"metadata": {},
"outputs": [],
"source": [
"import seaborn as sns\n",
"sns.set(style=\"ticks\")\n",
"\n",
"current_palette = sns.color_palette()\n",
"\n",
"def to_line(trajectory, xleft):\n",
" x = []\n",
" y = []\n",
" for fs in trajectory:\n",
" x.append(fs['start']['x'])\n",
" y.append(fs['start']['y'])\n",
" \"\"\"Transforms a trajectory into a Line2D.\"\"\"\n",
" x = trajectory['endX'].values\n",
" y = trajectory['endY'].values\n",
" if x[0] < xleft:\n",
" c = current_palette[2]\n",
" else:\n",
" c = current_palette[0]\n",
" return x, y, Line2D(x, y, color=c, linewidth=0.2)\n",
"\n",
"fig1 = plt.figure(figsize=(10,10))\n",
"ax1 = fig1.add_subplot(111)\n",
"def add_lines(trajectories, xleft, ax):\n",
" grouped = trajectories.groupby(['pedestrianId'])\n",
" for name, group in grouped:\n",
" x, y, line = to_line(group, xleft)\n",
" ax.add_line(line)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import seaborn as sns\n",
"sns.set(style=\"ticks\")\n",
"\n",
"current_palette = sns.color_palette()\n",
"\n",
"x_vcenter = 17.5\n",
"y_vcenter = 5.2\n",
"for i in range(len(trajectoriesReal)):\n",
" x, y, line = to_line(trajectoriesReal[i+1], 14)\n",
" ax1.add_line(line)\n",
" \n",
"\n",
"fig1 = plt.figure(figsize=(10,10))\n",
"ax1 = fig1.add_subplot(111)\n",
"add_lines(trajectoriesReal, 14, ax1)\n",
"ax1.set_xlim(x_vcenter-5, x_vcenter+5)\n",
"ax1.set_ylim(y_vcenter-4, y_vcenter+4)\n",
"ax1.set_aspect(1)\n",
"\n",
"fig2 = plt.figure(figsize=(10,10))\n",
"ax2 = fig2.add_subplot(111)\n",
"\n",
"for i in range(len(trajectories)):\n",
" x, y, line = to_line(trajectories[i+1], 14)\n",
" ax2.add_line(line)\n",
"\n",
"add_lines(ptrajectories, 14, ax2)\n",
"ax2.set_xlim(x_vcenter-5, x_vcenter+5)\n",
"ax2.set_ylim(y_vcenter-4, y_vcenter+4)\n",
"ax2.set_aspect(1)\n",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment