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

import os
import pickle
import itertools
import sys

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.patches as mpatches
import seaborn as sns
sns.set_style('darkgrid', {'legend.frameon': True})
import pandas as pd

sys.path.append('..')
from antlia.record import Record, load_file
from antlia.dtype import load_converted_record
from antlia import plot_braking as braking
from antlia import dtc
from antlia.plotdf import plotjoint

%load_ext autoreload
%autoreload 2

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

colors = sns.color_palette('Paired', 10)

In [2]:
import IPython.display

def display_animation(animation):
    plt.close(animation._fig)
    return IPython.display.HTML(animation.to_jshtml())

In [2]:
bicycle_record_files = [
    '2018-04-23_12-30-38.csv',
    '2018-04-23_13-13-36.csv',
    '2018-04-23_14-22-58.csv',
    '2018-04-23_15-27-48.csv',
    '2018-04-23_16-32-27.csv',
    '2018-04-23_17-14-00.csv',
    '2018-04-25_09-27-24.csv',
    '2018-04-25_10-20-28.csv',
    '2018-04-25_11-34-04.csv',
    '2018-04-25_12-41-48.csv',
    '2018-04-25_14-14-57.csv',
    '2018-04-25_14-49-39.csv',
    '2018-04-25_16-15-57.csv',
    '2018-04-25_17-23-04.csv',
    '2018-04-26_11-19-31.csv',
    '2018-04-26_14-50-53.csv',
    '2018-04-27_14-59-52.csv'
]

lidar_record_files = [
    '2018-04-23-12-17-37_0.pkl.gz',
    '2018-04-23-13-01-00_0.pkl.gz',
    '2018-04-23-14-10-33_0.pkl.gz',
    '2018-04-23-15-15-14_0.pkl.gz',
    '2018-04-23-16-19-35_0.pkl.gz',
    '2018-04-23-17-01-24_0.pkl.gz',
    '2018-04-25-09-15-00_0.pkl.gz',
    '2018-04-25-10-07-31_0.pkl.gz',
    '2018-04-25-11-21-29_0.pkl.gz',
    '2018-04-25-12-29-06_0.pkl.gz',
    '2018-04-25-14-02-15_0.pkl.gz',
    '2018-04-25-14-36-55_0.pkl.gz',
    '2018-04-25-16-03-24_0.pkl.gz',
    '2018-04-25-17-10-07_0.pkl.gz',
    '2018-04-26-11-07-38_0.pkl.gz',
    '2018-04-26-14-38-03_0.pkl.gz',
    '2018-04-27-14-47-07_0.pkl.gz',
    '2018-04-27-15-39-56_0.pkl.gz'
]

i = 8 # rider
f1 = bicycle_record_files[i]
f2 = lidar_record_files[i]

with open('../config.p', 'rb') as f:
        bicycle_calibration = pickle.load(f)

data_dir = '../../data/comfort/'
bicycle_rec = load_file(
    os.path.join(data_dir, f1),
    bicycle_calibration['convbike'])
lidar_rec = load_converted_record(
    os.path.join(data_dir, f2))

r = Record(lidar_rec, bicycle_rec)
r.sync()


Out[2]:
-5.4800000000000182

In [6]:
sns.palplot(sns.color_palette('Paired', 12))



In [24]:
1/np.diff(r.lidar.time).mean()


Out[24]:
19.999836212938582

In [52]:
#r._calculate_trials2(lidar_bbmask={'xlim': (3.8, 4.4), 'ylim': (2.9, 3.4)})
#r._calculate_trials2(trial_mask=0)
#r._calculate_trials2(missing_sync=[680])

DEFAULT_FIGSIZE = (12, 8)
mpl.rcParams['figure.figsize'] = DEFAULT_FIGSIZE
%matplotlib notebook

plt.close('all')
colors = sns.color_palette('Paired', 12)

try:
    #raise AssertionError
    r._calculate_trials2(trial_mask=[9, 10])
except AssertionError:
    # unable to detect events for a trial
    from antlia import trial2
    from antlia.trial2 import Trial2
    from antlia import util
    
    valid_index = Trial2.detect_valid_index(r.lidar)
    event_clumps = np.ma.extras._ezclump(valid_index)
    event_clumps = [c for c in event_clumps if c.stop - c.start > int(5.5*20)]
    
    assert len(event_clumps) > 0
    event_index = event_clumps[-1]
    
    fig, ax = plt.subplots(3, 1, sharex=True)
    # plot sync signal
    ax[0].plot(r.bicycle.time, r.bicycle.sync, color=colors[2]) 
    ax[0].plot(r.lidar.time, r.lidar.sync, color=colors[3]) 
    ax[1].plot(r.lidar.time, r.lidar.sync, color=colors[3]) 
    ax[2].plot(r.lidar.time, r.lidar.sync, color=colors[3]) 
    
    ax[0].plot(r.bicycle.time, r.bicycle.speed, color=colors[0])
    ax[1].plot(r.bicycle.time, r.bicycle.speed > 0.5, color=colors[0])
    #ax[2].plot(r.bicycle.time, mask_b, color=colors[1])
    for c in event_clumps:
        t0 = r.lidar.time[c.start]
        t1 = r.lidar.time[c.stop - 1]
        ax[0].axvspan(t0, t1, color=colors[5], alpha=0.3)
        ax[1].axvspan(t0, t1, color=colors[5], alpha=0.3)
        ax[2].axvspan(t0, t1, color=colors[5], alpha=0.3)
    
    # draw entry/exit bounding box masks
    mask_c = r.lidar.cartesian(**trial2.ENTRY_BB)[0].count(axis=1) > 1
    mask_d0 = r.lidar.cartesian(**EXIT_BB_BRAKE)[0].count(axis=1) > 1
    mask_d1 = r.lidar.cartesian(**EXIT_BB_STEER)[0].count(axis=1) > 1
    ax[2].plot(r.lidar.time, mask_c, color=colors[6])
    ax[2].plot(r.lidar.time, mask_d0, color=colors[8])
    ax[2].plot(r.lidar.time, mask_d1, color=colors[10])
    
    plt.show()


../antlia/trial2.py:1146: UserWarning: Unable to detect cyclist exit or braking for event ending at t = 1351.011 seconds
  except IndexError:

In [48]:
r.plot_trial_detection()


Out[48]:
(<Figure size 864x576 with 3 Axes>,
 array([<matplotlib.axes._subplots.AxesSubplot object at 0x13d6b2d68>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1690cde48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1690f96a0>], dtype=object))

In [49]:
plt.close('all')
r.trials[15].event.plot_trajectory()


Out[49]:
(<Figure size 864x576 with 2 Axes>,
 array([<matplotlib.axes._subplots.AxesSubplot object at 0x1692c06d8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x16094f240>], dtype=object))

In [15]:
DEFAULT_FIGSIZE = (12, 8)
mpl.rcParams['figure.figsize'] = DEFAULT_FIGSIZE
%matplotlib notebook

plt.close()
colors = sns.color_palette('Paired', 12)
#fig, ax = plt.subplots(4, 1, sharex=True)
fig, ax = plt.subplots(3, 1, sharex=True)

i = 0
for t in r.trials:
    i = i ^ 1
    ax[0].plot(t.data.time, t.data.speed, color=colors[i])
    
    ax[1].plot(t.data.time, t.event_detection.mask_a, color=colors[i])
    ax[2].plot(t.data.time, t.event_detection.mask_b, color=colors[i])
    
    t0, t1 = t.event.bicycle.time[[0, -1]]
    ax[0].axvspan(t0, t1, color=colors[5], alpha=0.5)
    ax[1].axvspan(t0, t1, color=colors[5], alpha=0.5)
    ax[2].axvspan(t0, t1, color=colors[5], alpha=0.5)
    
    # cyclist enters from right
    ax[2].plot(t.lidar.time,
               t.lidar.cartesian(xlim=(20, 50), ylim=(2, 3.5))[0].count(axis=1) > 1,
               color=colors[6])
    
    # cyclist exist at left
    ax[2].plot(t.lidar.time,
               t.lidar.cartesian(xlim=(-20, -10), ylim=(2, 3.5))[0].count(axis=1) > 1,
               color=colors[8])
    
    #lidar_subsample = t.lidar[::100]
    #object_count = lidar_object_count(lidar_subsample, xlim=(-10, 30), ylim=(1, 4))
    #ax[3].plot(lidar_subsample.time, object_count > 0, color=colors[i], alpha=0.5)
    #
    #count_interp = np.interp(t.data.time, lidar_subsample.time, object_count)
    #ax[3].plot(t.data.time, count_interp > 0, color=colors[i])
    
ax[1].plot(r.bicycle.time, r.bicycle.sync, color=colors[3]) 
ax[2].plot(r.bicycle.time, r.bicycle.sync, color=colors[3]) 

ax[0].set_ylabel('velocity')
ax[1].set_ylabel('mask a')
ax[2].set_ylabel('mask b')

plt.show()



In [50]:
color = sns.color_palette('Paired', 12)[1::2]
plt.close('all')
fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
cart_kw = {'xlim': (-20, 50), 'ylim': (0.5, 3.5)}
x, y = [z.compressed() for z in r.trials[15].lidar.cartesian(**cart_kw)]
ax.scatter(x, y, marker='.', color=color[0])

x, y = [z.compressed() for z in r.trials[15].event.lidar.cartesian(**cart_kw)]
ax.scatter(x, y, marker='.', color=color[2])

ax.set_xlim(cart_kw['xlim'])
ax.set_ylim(cart_kw['ylim'])
plt.show()



In [ ]:
DEAFAULT_FIGSIZE = (16, 12)
mpl.rcParams['figure.figsize'] = DEFAULT_FIGSIZE
%matplotlib inline

for trial in [r.trials[3]]:
    d = display_animation(trial.event.lidar.animate(
        xlim=(-40, 40),
        ylim=(0, 4),
        rlim=(0, 110),
        figsize=DEFAULT_FIGSIZE))
    IPython.display.display(d)

In [53]:
for i, t in enumerate(r.trials):
    print(i, t.event.type)
    #print(i, dict(t.event_detection._asdict()))
    #print()


0 Braking
1 Braking
2 Braking
3 Overtaking
4 Overtaking
5 Overtaking
6 Braking
7 Braking
8 Braking
9 Overtaking
10 Overtaking
11 Overtaking
12 Braking
13 Braking
14 Braking
15 Overtaking
16 Overtaking
17 Overtaking

In [58]:
from antlia import plot_braking as braking
from antlia import trial2

plt.close('all')
for trial in r.trials:
    #if trial.event.type != EventType.Braking:
    if trial.event.type.value != trial2.EventType.Braking.value:
        continue
    
    metrics_kw = {'braking_threshold': 0.2, 'min_size': 30}
        
    m = braking.get_metrics(trial.bicycle, **metrics_kw)
    print(m)
    print()
    
    braking.plot_trial_braking_event(
        trial.bicycle, metrics_kw=metrics_kw)
    
plt.show()


(array([ (-1.38573454,  114.74875584, -0.92134532,   1.08839852e-168,  0.02901747,  3.85448161,  3.264409,  6.6237451,  79.710703,  82.975112, 55, [8131, 8540], 0, 0, 0)],
      dtype=[('linregress slope', '<f8'), ('linregress intercept', '<f8'), ('linregress r-value', '<f8'), ('linregress p-value', '<f8'), ('linregress stderr', '<f8'), ('starting velocity', '<f8'), ('braking duration', '<f8'), ('braking distance', '<f8'), ('braking starttime', '<f8'), ('braking endtime', '<f8'), ('window size', '<i8'), ('braking range', '<i8', (2,)), ('lockup ranges', '<i8'), ('rider id', '<i8'), ('trial id', '<i8')]), array([ 0.00064302,  0.00066976,  0.00069645, ...,  0.00069645,
        0.00066976,  0.00064302]), array([-0.02362814, -0.02416733, -0.02454105, ...,  0.01431309,
        0.01111335,  0.00796344]), [])