In [1]:
import sys
import itertools

import matplotlib.pyplot as plt

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

%load_ext autoreload
%autoreload 2

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 [3]:
records = load_records(sync=False)


Unable to determine range 2
Unable to determine range 1

In [4]:
import numpy as np
import scipy
import seaborn as sns


# rider, speed, amplitude, freq
data = np.zeros((4*15, 5))

plt.close()
for rid, tid in itertools.product(range(4), range(15)):
    record = records[rid]
    trial = record.trial[tid]
    
    try:
        ax = trial.plot_steer_event_detection(figsize=DEFAULT_FIGSIZE)
        ax.set_title('rider {}, trial {:02d}, events: {}'.format(rid, tid, ax.get_title()))
    except ValueError:
        continue
    
    est, (t, y, est_y), ev = trial.steer_event_parameters()
    
    fit_desc = 'rider {}, trial {:02d}, a: {:0.02f}, t: {:0.02f}, v: {:0.02f}'.format(
        rid, tid, est[0], 1/est[1], ev.speed)
    data[rid*15 + tid, :] = np.array([rid, tid, ev.speed, -est[0], 1/est[1]])
    
    colors = sns.color_palette('Paired', 10)
    fig2, ax2 = plt.subplots(figsize=DEFAULT_FIGSIZE)
    ax2.plot(t, y, label='steer angle event', color=colors[1])
    ax2.plot(t, est_y, label='fitted sinusoid', color=colors[7])
    ax2.legend()
    ax2.set_title(fit_desc)
    ax2.axhline(0, color='black')
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 [5]:
import pandas as pd

# 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', 'trial', 'v', 'a', 't'])
print(df)


    rider  trial         v         a          t
0     0.0    0.0  0.294541  0.299567   2.881839
1     0.0    0.0  0.000000  0.000000   0.000000
2     0.0    2.0  6.022226  0.024990   2.079116
3     0.0    3.0  4.016655  0.534899   3.492856
4     0.0    4.0  5.368322  0.038307   1.523387
5     0.0    5.0  5.994924  0.115004   2.467268
6     0.0    6.0  4.567633  0.377467   4.025128
7     0.0    7.0  5.189857  0.172512   2.834785
8     0.0    8.0  6.206539  0.042693   2.629231
9     0.0    9.0  4.198477  0.068356   3.164535
10    0.0   10.0  5.559790  0.171571   3.232057
11    0.0   11.0  6.314231  0.087470   1.824191
12    0.0   12.0  4.550632  0.222515   2.574741
13    0.0   13.0  5.537313  0.140905   3.347173
14    0.0   14.0  6.351615  1.302284  18.851687
15    1.0    0.0  4.986281  0.041481   2.421580
16    1.0    1.0  6.477340  0.026489   1.647223
17    1.0    2.0  5.304934  0.166526   3.321516
18    1.0    3.0  4.390369  0.091368   3.360120
19    1.0    4.0  6.106657  2.111708  29.654893
20    1.0    5.0  5.739989  0.102116   3.327779
21    1.0    6.0  4.175392  0.095814   2.582523
22    1.0    7.0  6.214822  0.083323   4.417071
23    1.0    8.0  5.651706  3.520673  27.934293
24    1.0    9.0  4.715197  0.108532   4.017427
25    1.0   10.0  6.485302  0.020502   2.029246
26    1.0   11.0  5.226656  0.034662   2.484915
27    1.0   12.0  4.188521  0.123701   3.320421
28    1.0   13.0  6.058139  0.029670   2.206419
29    1.0   14.0  5.765138  0.096400   4.847651
30    2.0    0.0  5.095661  0.239699   3.908990
31    2.0    1.0  4.688756  0.929831   4.106283
32    2.0    2.0  6.085611  0.120115   2.116276
33    2.0    3.0  5.236590  0.478982   3.788295
34    2.0    4.0  6.403137  0.080357   1.492263
35    2.0    5.0  4.225960  1.140370   4.707194
36    2.0    6.0  6.133219  0.070524   1.401707
37    2.0    7.0  4.526156  0.283244   2.657877
38    2.0    8.0  5.240013  0.222068   2.053587
39    2.0    9.0  6.277094  0.105812   1.270644
40    2.0   10.0  5.263065  0.412305   2.402824
41    2.0   11.0  4.072648  0.704964   3.891995
42    2.0   12.0  4.009402  0.331997   2.492673
43    2.0   13.0  5.976247  0.075811   1.242975
44    2.0   14.0  5.196231  0.770046   3.549507
45    3.0    0.0  6.318945  0.139344   2.416542
46    3.0    1.0  3.837248  0.386699   3.093245
47    3.0    2.0  4.859785  0.135438   2.190735
48    3.0    3.0  5.852140  0.139051   2.452148
49    3.0    4.0  4.951027  0.142588   2.491314
50    3.0    5.0  4.437325  0.207141   2.811646
51    3.0    6.0  4.313985  0.509363   3.491852
52    3.0    7.0  5.596588  0.095930   1.081050
53    3.0    8.0  4.977496  0.301067   2.654965
54    3.0    9.0  3.910169  0.173916   2.457491
55    3.0   10.0  4.709803  0.285675   2.390949
56    3.0   11.0  5.831154  0.508998   4.072251
57    3.0   12.0  4.683639  0.151150   1.737240
58    3.0   13.0  3.858607  0.137258   1.797076
59    3.0   14.0  5.654759  0.225350   2.619303

In [6]:
fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider', y='a', data=df, ax=ax)
ax.set_title('rider vs fitted amplitude')

fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider', y='t', data=df, ax=ax)
ax.set_title('rider vs fitted period')

fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider', y='v', data=df, ax=ax)
ax.set_title('rider vs average velocity')
plt.show()



In [7]:
from antlia.plotdf import colormap, plotjoint
colors = sns.color_palette()

plt.close()
g = plotjoint('a', 't', df, ('rider', colors))
g.fig.set_size_inches(DEFAULT_FIGSIZE)
g = plotjoint('v', 'a', df[df.v > 1], ('rider', colors))
g.fig.set_size_inches(DEFAULT_FIGSIZE)
g = plotjoint('v', 't', df[df.v > 1], ('rider', colors))
g.fig.set_size_inches(DEFAULT_FIGSIZE)
plt.show()



In [8]:
fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider', y='a', data=df[df.a < 2], ax=ax)
ax.set_title('rider vs fitted amplitude (a < 2)')

fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider', y='t', data=df[df.t < 6], ax=ax)
ax.set_title('rider vs fitted period (t < 6)')

fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
sns.swarmplot(x='rider', y='v', data=df[df.v > 1], ax=ax)
ax.set_title('rider vs average velocity (v > 1)')
plt.show()



In [9]:
colors = sns.color_palette()

plt.close()
g = plotjoint('a', 't', df[(df.t < 6) & (df.v > 1)], ('rider', colors))
g.fig.set_size_inches(DEFAULT_FIGSIZE)
g = plotjoint('v', 'a', df[(df.t < 6) & (df.v > 1)], ('rider', colors))
g.fig.set_size_inches(DEFAULT_FIGSIZE)
g = plotjoint('v', 't', df[(df.t < 6) & (df.v > 1)], ('rider', colors))
g.fig.set_size_inches(DEFAULT_FIGSIZE)
plt.show()