19from bisect 
import bisect_left 
 
   21import matplotlib.pyplot 
as plt
 
   22from matplotlib.widgets 
import Slider
 
   38  print(
"Need 3 arguments: process_traj_logs.py <file name> <start time> <end_time>")
 
   42with open(sys.argv[1], 
'r') 
as infile, \
 
   43     open(sys.argv[1] + 
".clean", 
'w') 
as outfile:
 
   45    data = data.replace(
"\x1b[0m", 
"")
 
   46    data = data.replace(
"\x1b[32m", 
"")
 
   53with open(sys.argv[1] + 
".clean", 
'r') 
as csv_file:
 
   54  traj_data = csv.reader(csv_file, delimiter=
'|')
 
   60    core_data[
"times"].append(
float(row[0].strip()))
 
   61    core_data[
"content"].append(row[3].strip())
 
   71end_index = min(
binarySearch(core_data[
"times"], 
float(sys.argv[3])) + 1, len(core_data[
"times"]))
 
   73print(
"Start index: " + 
str(start_index))
 
   74print(
"End index: " + 
str(end_index))
 
   76core_data[
"times"] = core_data[
"times"][start_index:end_index]
 
   77core_data[
"content"] = core_data[
"content"][start_index:end_index]
 
   83  TIME_BOUND_POINTS = 2,
 
   84  BACK_AND_FRONT_POINTS = 3,
 
   87  PROCESSED_CURVATURES = 6,
 
   88  CURVATURE_CONSTRAINED_SPEEDS = 7,
 
   90  SPEED_LIMIT_CONSTRAINED_SPEEDS = 9,
 
   91  SPEED_OP_REVERSE_STEP = 10,
 
   92  SPEED_OP_FORWARD = 11,
 
   98data_source = DataSource.NONE
 
  100core_data[
"time_steps"] = []
 
  102for content 
in core_data[
"content"]:
 
  103  if data_source == DataSource.NONE 
and "VehicleState" in content:
 
  104    core_data[
"time_steps"].append({
 
  105      DataSource.RAW_POINTS : [],
 
  106      DataSource.TIME_BOUND_POINTS : [],
 
  107      DataSource.BACK_AND_FRONT_POINTS : [],
 
  108      DataSource.SAMPLED_POINTS : [],
 
  109      DataSource.RAW_CURVATURES : [],
 
  110      DataSource.PROCESSED_CURVATURES : [],
 
  111      DataSource.CURVATURE_CONSTRAINED_SPEEDS : [],
 
  112      DataSource.FINAL_YAWS : [],
 
  113      DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS : [],
 
  114      DataSource.SPEED_OP_REVERSE_STEP : [],
 
  115      DataSource.SPEED_OP_FORWARD : [],
 
  116      DataSource.AFTER_SPEED_OP : [],
 
  117      DataSource.AFTER_AVERAGE : [],
 
  118      DataSource.AFTER_MIN_SPEED : [],
 
  119      DataSource.FINAL_TIMES : []
 
  122    data_source = DataSource.RAW_POINTS
 
  124  if data_source == DataSource.RAW_POINTS 
and "Point:" in content 
and "Speed:" in content:
 
  125    point_speed = content.split(
':')
 
  126    point = point_speed[1].
split(
'Speed')
 
  130    core_data[
"time_steps"][-1][DataSource.RAW_POINTS].append((x,y))
 
  132  if data_source == DataSource.RAW_POINTS 
and "Got time_bound_points with size:" in content:
 
  133    data_source = DataSource.TIME_BOUND_POINTS
 
  135  if data_source == DataSource.TIME_BOUND_POINTS 
and "Point:" in content 
and "Speed:" in content:
 
  136    point_speed = content.split(
':')
 
  137    point = point_speed[1].
split(
'Speed')
 
  138    xy = point[0].
split(
',')
 
  141    core_data[
"time_steps"][-1][DataSource.TIME_BOUND_POINTS].append((x,y))
 
  143  if data_source == DataSource.TIME_BOUND_POINTS 
and "Got back_and_future points with size" in content:
 
  144    data_source = DataSource.BACK_AND_FRONT_POINTS
 
  146  if data_source == DataSource.BACK_AND_FRONT_POINTS 
and "Point:" in content 
and "Speed:" in content:
 
  147    point_speed = content.split(
':')
 
  148    point = point_speed[1].
split(
'Speed')
 
  149    xy = point[0].
split(
',')
 
  152    core_data[
"time_steps"][-1][DataSource.BACK_AND_FRONT_POINTS].append((x,y))
 
  154  if data_source == DataSource.BACK_AND_FRONT_POINTS 
and "Got sampled points with size:" in content:
 
  155    data_source = DataSource.SAMPLED_POINTS
 
  157  if data_source == DataSource.SAMPLED_POINTS 
and "," in content 
and not ":" in content:
 
  158    xy = content.split(
',')
 
  161    core_data[
"time_steps"][-1][DataSource.SAMPLED_POINTS].append((x,y))
 
  163  if (
False and data_source == DataSource.RAW_CURVATURES 
or data_source == DataSource.SAMPLED_POINTS) 
and "better_curvature[i]:" in content:
 
  164    data_source = DataSource.RAW_CURVATURES
 
  165    split = content.split(
':')
 
  167    core_data[
"time_steps"][-1][DataSource.RAW_CURVATURES].append(c)
 
  169  if (data_source == DataSource.PROCESSED_CURVATURES 
or data_source == DataSource.SAMPLED_POINTS) 
and "curvatures[i]:" in content:
 
  170    data_source = DataSource.PROCESSED_CURVATURES
 
  171    split = content.split(
':')
 
  173    core_data[
"time_steps"][-1][DataSource.PROCESSED_CURVATURES].append(c)
 
  175  if (data_source == DataSource.CURVATURE_CONSTRAINED_SPEEDS 
or data_source == DataSource.PROCESSED_CURVATURES) 
and "ideal_speeds:" in content:
 
  176    data_source = DataSource.CURVATURE_CONSTRAINED_SPEEDS
 
  177    split = content.split(
':')
 
  179    core_data[
"time_steps"][-1][DataSource.CURVATURE_CONSTRAINED_SPEEDS].append(c)
 
  181  if (data_source == DataSource.FINAL_YAWS 
or data_source == DataSource.CURVATURE_CONSTRAINED_SPEEDS) 
and "final_yaw_values[i]:" in content:
 
  182    data_source = DataSource.FINAL_YAWS
 
  183    split = content.split(
':')
 
  185    core_data[
"time_steps"][-1][DataSource.FINAL_YAWS].append(c)
 
  187  if (data_source == DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS 
or data_source == DataSource.FINAL_YAWS) 
and "constrained_speed_limits:" in content:
 
  188    data_source = DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS
 
  189    split = content.split(
':')
 
  191    core_data[
"time_steps"][-1][DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS].append(c)
 
  193  if (data_source == DataSource.SPEED_OP_REVERSE_STEP 
or data_source == DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS) 
and "only_reverse[i]:" in content:
 
  194    data_source = DataSource.SPEED_OP_REVERSE_STEP
 
  195    split = content.split(
':')
 
  197    core_data[
"time_steps"][-1][DataSource.SPEED_OP_REVERSE_STEP].append(c)
 
  199  if (data_source == DataSource.SPEED_OP_FORWARD 
or data_source == DataSource.SPEED_OP_REVERSE_STEP) 
and "after_forward[i]:" in content:
 
  200    data_source = DataSource.SPEED_OP_FORWARD
 
  201    split = content.split(
':')
 
  203    core_data[
"time_steps"][-1][DataSource.SPEED_OP_FORWARD].append(c)
 
  205  if (data_source == DataSource.AFTER_SPEED_OP 
or data_source == DataSource.SPEED_OP_FORWARD) 
and "postAccel[i]:" in content:
 
  206    data_source = DataSource.AFTER_SPEED_OP
 
  207    split = content.split(
':')
 
  209    core_data[
"time_steps"][-1][DataSource.AFTER_SPEED_OP].append(c)
 
  211  if (data_source == DataSource.AFTER_AVERAGE 
or data_source == DataSource.AFTER_SPEED_OP) 
and "post_average[i]:" in content:
 
  212    data_source = DataSource.AFTER_AVERAGE
 
  213    split = content.split(
':')
 
  215    core_data[
"time_steps"][-1][DataSource.AFTER_AVERAGE].append(c)
 
  217  if (data_source == DataSource.AFTER_MIN_SPEED 
or data_source == DataSource.AFTER_AVERAGE) 
and "post_min_speed[i]:" in content:
 
  218    data_source = DataSource.AFTER_MIN_SPEED
 
  219    split = content.split(
':')
 
  221    core_data[
"time_steps"][-1][DataSource.AFTER_MIN_SPEED].append(c)
 
  223  if (data_source == DataSource.FINAL_TIMES 
or data_source == DataSource.AFTER_MIN_SPEED) 
and "times[i]:" in content:
 
  224    data_source = DataSource.FINAL_TIMES
 
  225    split = content.split(
':')
 
  227    core_data[
"time_steps"][-1][DataSource.FINAL_TIMES].append(c)
 
  229  if data_source == DataSource.FINAL_TIMES 
and not (
"times[i]:" in content):
 
  230    data_source = DataSource.NONE
 
  233print(
"DONE PROCESSING FILE")
 
  234print(
"CREATING GRAPHS")
 
  239  fig = plt.figure(figure_num)
 
  245  l, = plt.plot([xy[0] 
for xy 
in time_step[key]], [xy[1] 
for xy 
in time_step[key]], 
'.')
 
  247  time_step_ax = plt.axes([0.20, 0.001, 0.65, 0.03])
 
  248  time_step_sldr = Slider(time_step_ax, 
'Time Step', 0.0, len(data) - 1.0, valinit=0, valstep=1)
 
  250  def update_timestep(val):
 
  251    time_step = data[
int(time_step_sldr.val)]
 
  252    l.set_xdata([xy[0] 
for xy 
in time_step[key]])
 
  253    l.set_ydata([xy[1] 
for xy 
in time_step[key]])
 
  254    fig.canvas.draw_idle()
 
  256  time_step_sldr.on_changed(update_timestep)
 
  258  return (fig, l, time_step_sldr)
 
  263  fig = plt.figure(figure_num)
 
  269  l, = plt.plot(range(len(time_step[key])), time_step[key])
 
  271  time_step_ax = plt.axes([0.20, 0.01, 0.65, 0.03])
 
  272  time_step_sldr = Slider(time_step_ax, 
'Time Step', 0.0, len(data) - 1.0, valinit=0, valstep=1)
 
  274  def update_timestep(val):
 
  275    time_step = data[
int(time_step_sldr.val)]
 
  276    l.set_xdata(range(len(time_step[key])))
 
  277    l.set_ydata(time_step[key])
 
  278    fig.canvas.draw_idle()
 
  280  time_step_sldr.on_changed(update_timestep)
 
  282  return (fig, l, time_step_sldr)
 
  285  "Raw Downsampled Points from Lanelet Centerlines", 
"X (m)", 
"Y (m)")
 
  288  "Time Bound Points from Lanelet Centerlines", 
"X (m)", 
"Y (m)")
 
  291  "Back and front points from Lanelet Centerlines", 
"X (m)", 
"Y (m)")
 
  294  "Sampled points from spline fitting", 
"X (m)", 
"Y (m)")
 
  297  "Raw Curvatures", 
"Index", 
"Curvature (1/r) (m)")
 
  300  "Processed Curvatures", 
"Index", 
"Curvature (1/r) (m)")
 
  303  "Curvature constrained speeds", 
"Index", 
"Velocity (m/s)")
 
  306  "Final Yaw values", 
"Index", 
"Yaw (rad)")
 
  309  "Speed Limit Constrained Speeds", 
"Index", 
"Velocity (m/s)")
 
  312  "Speed Optimization Reverse Step", 
"Index", 
"Velocity (m/s)")
 
  315  "Speed Optimization Forward Step", 
"Index", 
"Velocity (m/s)")
 
  318  "Speed Optimization Output", 
"Index", 
"Velocity (m/s)")
 
  321  "Speed after Moving Average", 
"Index", 
"Velocity (m/s)")
 
  324  "Speed after applying minimum speed (FINAL SPEED)", 
"Index", 
"Velocity (m/s)")
 
  327  "Final Times", 
"Index", 
"Seconds (s)")
 
  331core_data["time_steps"].append({ 
  332      DataSource.RAW_POINTS : [], 
  333      DataSource.TIME_BOUND_POINTS : [], 
  334      DataSource.BACK_AND_FRONT_POINTS : [], 
  335      DataSource.SAMPLED_POINTS : [], 
  336      DataSource.RAW_CURVATURES : [], 
  337      DataSource.PROCESSED_CURVATURES : [], 
  338      DataSource.CURVATURE_CONSTRAINED_SPEEDS : [], 
  339      DataSource.FINAL_YAWS : [], 
  340      DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS : [], 
  341      DataSource.SPEED_OP_REVERSE_STEP : [], 
  342      DataSource.SPEED_OP_FORWARD : [], 
  343      DataSource.AFTER_SPEED_OP : [], 
  344      DataSource.AFTER_AVERAGE : [], 
  345      DataSource.AFTER_MIN_SPEED : [], 
  346      DataSource.FINAL_TIMES : [] 
  350# Proceeded by : VehicleState 
  353# Proceeded by : Got time_bound_points with size: 
  356# Proceeded by : Got back_and_future points with size 
  359# Proceeded by : Got sampled points with size 
  360# (nothing ) -74.5649, 329.079 
  362# Proceeded by : nothing 
  363# better_curvature[i]: 
  365# Proceeded by : nothing 
  368# Proceeded by : nothing 
  371# Proceeded by : nothing 
  372# final_yaw_values[i]: 
  374# Proceeded by : nothing 
  375# final_actual_speeds[i]: 
  377# Proceeded by : nothing 
  380# Proceeded by : nothing 
  383# Proceeded by : nothing 
  386# Proceeded by : nothing 
  389# Proceeded by : nothing 
  392# Proceeded by : nothing 
  395# Proceeded by : nothing 
  409#with open('demofile.txt', "r") as f: 
  410#  read_data = f.readline() 
def xy_scatter_with_slider(figure_num, data, key, title, xlabel, ylabel)
 
def index_plot_with_slider(figure_num, data, key, title, xlabel, ylabel)