In [1]:
%matplotlib inline
DEFAULT_FIGSIZE = (12, 8)

import itertools
import sys

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')

sys.path.append('..')
from antlia import dtc
from antlia.record import load_records
from antlia import util

%load_ext autoreload
%autoreload 2

import matplotlib as mpl
mpl.rcParams['figure.figsize'] = DEFAULT_FIGSIZE

In [2]:
def set_title(ax, title):
    try:
        mpld3
    except NameError:
        ax.figure.suptitle(title)
    else:
        ax.set_title(title)
        
DEFAULT_FIGSIZE = (14, 7)

In [4]:
records = load_records(sync=True)


Unable to determine range 2
Unable to determine range 1

In [5]:
# rider, trial, speed, dist1, dist2, dist_interp, max_y
data = np.zeros((4*15, 7))

plt.close('all')
for rid, tid in itertools.product(range(4), range(15)):
    if (rid == 0) and (tid == 1):
        data[rid*15 + tid, :] = np.array([
            rid,
            tid,
            np.nan,  # speed
            np.nan,  # dist1
            np.nan,  # dist2
            np.nan,  # dist_interp
            np.nan]) # max_y
        continue
        
    record = records[rid]
    trial = record.trial[tid]
    
    # get steer event 
    _, (ev_time, _, _), ev = trial.steer_event_parameters()
    
    # determine max y-position
    indices = record.lidar.frame_index(
        lambda t: (t > ev_time[0]) & (t < ev_time[-1]))
    max_y = record.lidar[indices].cartesian(xlim=(-6, 2), ylim=(0.5, 3))[1].max()
    
    # get index before time 0
    index = record.lidar.frame_index(ev_time[0])
    # indices before and after event start
    index = np.insert(index, 0, index[0] - 1)

    ax = None
    dist = []
    for i in index:
        x, y = record.lidar[[i]].cartesian(xlim=(-19, 1),
                                           ylim=(0.5, 3))
        
        if ax is None:
            colors = sns.color_palette('Paired', 10)[::2]
        else:
            colors = sns.color_palette('Paired', 10)[1::2]
            
        ax, pair = dtc.plot_closest_pair(
            *dtc.cluster(x, y),
            ax=ax,
            colors=colors)
        dist.append(dtc.dist(pair))
        
        for l in ax.get_lines()[-5:]:
            l.set_label('{} (t = {:0.3f} s)'.format(
                l.get_label(),
                record.lidar.time[i]))
    
    ax.legend()
    ax.set_aspect('equal', 'datalim')
    ax.set_title('rider {} trial {:02d}, dist {}'.format(
        rid, tid, ','.join('{:0.02f}'.format(d) for d in dist)))
    
    dist_interp = np.interp(ev_time[0], record.lidar.time[index], dist)
    data[rid*15 + tid, :] = np.array([rid, tid, ev.speed, dist[0], dist[-1], dist_interp, max_y])
plt.show()


/Users/oliver/miniconda3/envs/dev/lib/python3.5/site-packages/scipy/optimize/minpack.py:427: RuntimeWarning: Number of calls to function has reached maxfev = 1000.
  warnings.warn(errors[info][0], RuntimeWarning)
/Users/oliver/miniconda3/envs/dev/lib/python3.5/site-packages/matplotlib/pyplot.py:524: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  max_open_warning, RuntimeWarning)

In [6]:
import pandas as pd
from antlia.plotdf import plotjoint
colors = sns.color_palette()

# remove rows with nan
index = np.where(np.any(np.isnan(data), axis=1))[0]
data = np.delete(data, index, axis=0)

# Average speed (v) in this dataframe is over the
# steer avoidance event (infl, min, infl, max, infl, min, infl)
# and not just the sinusoid fit region (infl, min, infl)
df = pd.DataFrame(data=data, columns=[
    'rider id',
    'trial id',
    'starting velocity',
    'distance-to-collision frame-before',
    'distance-to-collision frame-after',
    'distance-to-collision',
    'maximum lateral distance'])
df[['rider id', 'trial id']] = df[['rider id', 'trial id']].apply(np.int64)

df['time-to-collision frame-before'] = df.apply(
    lambda row: row['distance-to-collision frame-before']/row['starting velocity'], axis=1)
df['time-to-collision frame-after'] = df.apply(
    lambda row: row['distance-to-collision frame-after']/row['starting velocity'], axis=1)
df['time-to-collision'] = df.apply(
    lambda row: row['distance-to-collision']/row['starting velocity'], axis=1)
df.to_pickle('steering_ttc.p.gz')
df # display dataframe


Out[6]:
rider id trial id starting velocity distance-to-collision frame-before distance-to-collision frame-after distance-to-collision maximum lateral distance time-to-collision frame-before time-to-collision frame-after time-to-collision
0 0 0 0.294541 3.261250 3.110582 3.177190 1.763553 11.072318 10.560783 10.786922
1 0 2 6.022226 10.043523 9.785744 9.838382 1.409885 1.667743 1.624938 1.633679
2 0 3 4.016655 2.931695 2.752570 2.897776 1.655686 0.729885 0.685289 0.721440
3 0 4 5.368322 5.347680 5.116617 5.320816 1.421042 0.996155 0.953113 0.991151
4 0 5 5.994924 4.713290 4.420386 4.553382 1.414660 0.786214 0.737355 0.759540
5 0 6 4.567633 4.540185 4.293193 4.323005 1.638882 0.993991 0.939916 0.946443
6 0 7 5.189857 4.540644 4.274900 4.401065 1.655332 0.874907 0.823703 0.848013
7 0 8 6.206539 6.002774 5.673217 5.985420 1.598131 0.967169 0.914071 0.964373
8 0 9 4.198477 5.416400 5.200622 5.335181 1.704569 1.290087 1.238693 1.270742
9 0 10 5.559790 4.131030 3.893611 4.064961 1.546172 0.743019 0.700316 0.731136
10 0 11 6.314231 5.311269 5.017478 5.311099 1.523066 0.841159 0.794630 0.841132
11 0 12 4.550632 3.230887 3.047451 3.103487 1.577757 0.709986 0.669676 0.681990
12 0 13 5.537313 5.611388 5.371683 5.439524 1.586409 1.013377 0.970088 0.982340
13 0 14 6.351615 8.468095 8.222667 8.321226 1.530407 1.333219 1.294579 1.310096
14 1 0 4.986281 9.956227 9.807913 9.906453 1.616500 1.996724 1.966980 1.986742
15 1 1 6.477340 12.433838 12.212591 12.281885 1.901041 1.919590 1.885433 1.896131
16 1 2 5.304934 5.977250 5.762107 5.895586 1.819514 1.126734 1.086179 1.111340
17 1 3 4.390369 6.730260 6.584749 6.726297 1.787467 1.532960 1.499817 1.532057
18 1 4 6.106657 10.980584 10.600280 10.727545 2.181157 1.798133 1.735857 1.756697
19 1 5 5.739989 7.742954 7.469715 7.549665 1.939514 1.348949 1.301347 1.315275
20 1 6 4.175392 5.950516 5.791825 5.851029 1.780911 1.425140 1.387133 1.401313
21 1 7 6.214822 9.288704 9.235536 9.267477 1.938125 1.494605 1.486050 1.491189
22 1 8 5.651706 7.493670 7.303897 7.351917 2.008193 1.325913 1.292335 1.300831
23 1 9 4.715197 7.148019 6.849745 6.873630 1.778192 1.515953 1.452695 1.457761
24 1 10 6.485302 12.778123 12.363583 12.589814 1.842341 1.970320 1.906401 1.941284
25 1 11 5.226656 8.465915 8.241527 8.267291 1.875995 1.619758 1.576826 1.581755
26 1 12 4.188521 5.520884 5.359874 5.394581 1.765266 1.318099 1.279658 1.287944
27 1 13 6.058139 10.679884 10.383650 10.401436 1.807574 1.762898 1.714000 1.716936
28 1 14 5.765138 10.237574 9.880956 10.145010 2.074258 1.775773 1.713915 1.759717
29 2 0 5.095661 6.267777 6.016627 6.098447 1.601283 1.230022 1.180735 1.196792
30 2 1 4.688756 2.791772 2.620830 2.624662 1.726682 0.595418 0.558961 0.559778
31 2 2 6.085611 6.122560 5.869086 6.073234 1.610519 1.006071 0.964420 0.997966
32 2 3 5.236590 4.888721 4.687478 4.725441 1.637703 0.933570 0.895139 0.902389
33 2 4 6.403137 6.051160 5.760211 5.874123 1.552946 0.945030 0.899592 0.917382
34 2 5 4.225960 2.795461 2.639928 2.712459 1.585290 0.661497 0.624693 0.641856
35 2 6 6.133219 5.367898 5.062733 5.347189 1.509184 0.875217 0.825461 0.871841
36 2 7 4.526156 3.149725 2.994089 3.069062 1.540859 0.695894 0.661508 0.678072
37 2 8 5.240013 4.542443 4.297440 4.410151 1.698249 0.866876 0.820120 0.841630
38 2 9 6.277094 4.891708 4.559532 4.872714 1.485196 0.779295 0.726376 0.776269
39 2 10 5.263065 3.714998 3.444595 3.547570 1.604102 0.705862 0.654485 0.674050
40 2 11 4.072648 2.906868 2.743830 2.864246 1.625919 0.713754 0.673721 0.703289
41 2 12 4.009402 3.032018 2.862908 2.874262 1.614169 0.756227 0.714049 0.716880
42 2 13 5.976247 5.469759 5.121711 5.465012 1.489436 0.915250 0.857011 0.914455
43 2 14 5.196231 3.316261 3.097818 3.281350 1.702494 0.638205 0.596166 0.631486
44 3 0 6.318945 4.839007 4.541199 4.653210 1.690680 0.765793 0.718664 0.736390
45 3 1 3.837248 2.269919 2.090311 2.231357 1.968213 0.591549 0.544742 0.581499
46 3 2 4.859785 3.688609 3.485441 3.601202 1.579179 0.759007 0.717201 0.741021
47 3 3 5.852140 5.939147 5.635675 5.896570 1.628943 1.014868 0.963011 1.007592
48 3 4 4.951027 4.649760 4.418581 4.537014 1.845527 0.939151 0.892458 0.916378
49 3 5 4.437325 3.734298 3.506121 3.549183 1.759324 0.841565 0.790143 0.799847
50 3 6 4.313985 3.434190 3.268535 3.287036 1.797892 0.796060 0.757660 0.761949
51 3 7 5.596588 3.554101 3.278240 3.375569 1.611583 0.635048 0.585757 0.603148
52 3 8 4.977496 4.394474 4.166873 4.169477 1.740075 0.882868 0.837143 0.837666
53 3 9 3.910169 4.491527 4.316073 4.320508 1.613058 1.148679 1.103807 1.104942
54 3 10 4.709803 3.588070 3.347649 3.526604 1.621542 0.761830 0.710783 0.748779
55 3 11 5.831154 4.702359 4.428155 4.603360 1.543657 0.806420 0.759396 0.789442
56 3 12 4.683639 3.170147 2.940594 3.040978 1.659988 0.676855 0.627844 0.649277
57 3 13 3.858607 2.427551 2.251466 2.303115 1.721162 0.629126 0.583492 0.596877
58 3 14 5.654759 4.265573 3.987100 4.016880 1.614563 0.754333 0.705088 0.710354

In [7]:
# exclude trials without working velocity sensor
plt.close('all')
#
g = plotjoint('starting velocity', 'distance-to-collision', df[df['starting velocity'] > 1],
              ('rider id', colors))
g.fig.set_size_inches(DEFAULT_FIGSIZE)
g.set_axis_labels('velocity [m/s]', 'distance-to-collision [m]')

g = plotjoint('starting velocity', 'time-to-collision', df[df['starting velocity'] > 1],
              ('rider id', colors))
g.fig.set_size_inches(DEFAULT_FIGSIZE)
g.set_axis_labels('velocity [m/s]', 'time-to-collision [s]')

g = plotjoint('starting velocity', 'maximum lateral distance', df[df['starting velocity'] > 1],
              ('rider id', colors))
g.fig.set_size_inches(DEFAULT_FIGSIZE)
g.set_axis_labels('velocity [m/s]', 'maximum y-position [m]')
plt.show()



In [8]:
%matplotlib inline
plt.close('all')
# exclude trial without working velocity sensor
fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider id', y='starting velocity', data=df[df['starting velocity'] > 1], ax=ax)
ax.set_title('rider id vs velocity')

fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider id', y='distance-to-collision', data=df, ax=ax)
ax.set_title('rider id vs distance')

fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider id', y='time-to-collision', data=df[df['starting velocity'] > 1], ax=ax)
ax.set_title('rider id vs time to collision')

fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider id', y='maximum lateral distance', data=df[df['starting velocity'] > 1], ax=ax)
ax.set_title('rider id vs time to collision')
plt.show()