Example multiple subplot ensemble figure of equidistantly interpolated speed. In this example each subplot represents the level of independent conditions.
from __future__ import print_function
# Copyright (c) 2013, Roger Lew
# All rights reserved.
"""
Example multiple subplot ensemble figure of equidistantly interpolated speed.
In this example each subplot represents the level of independent conditions.
"""
from collections import OrderedDict
import os
import glob
import time
import numpy as np
from scipy import stats
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
import matplotlib.pyplot as plt
from undaqTools import Daq, fslice
if __name__ == '__main__':
# data is on a local SSD drive. This is very important for performance.
data_dir = 'C:\\LocalData\\Left Lane\\'
# change the directory of the kernel
print("Changing wd to '%s'"%data_dir)
os.chdir(data_dir)
print('\nStep 1. Find speeds interpolated by distance...')
t0 = time.time()
hd5_files = tuple(glob.glob('*/*.hdf5'))
# define an linearly spaced distance vector to interpolate to
veh_dist_ip = np.arange(0, 6604, 8)
# goal of step 1 is to fill this list of lists structure
interp_spds_by_scenario = [[] for i in xrange(10)]
for hd5_file in hd5_files:
print(" interpolating '%s'..."%hd5_file)
# load hd5
daq = Daq()
daq.read_hd5(hd5_file, elemlist=['VDS_Veh*'])
# for each trial...
for i in xrange(10):
if not ( i*10+1 in daq.etc['epochs'] and \
i*10+3 in daq.etc['epochs']):
# encountered partial trial
continue
scenario = daq.etc['scen_order'][i]
# unpack the start frame of the 1-to-2 lane addition transition
# and the stop frame of the 2-to-1 lane reduction transition
f0 = daq.etc['epochs'][i*10+1].start
fend = daq.etc['epochs'][i*10+3].stop
print(" on scenario %i [f%i:f%i]..."%(scenario, f0, fend))
# next we need to linearly interpolate speed by distance
veh_dist = daq['VDS_Veh_Dist'][0, fslice(f0, fend)].flatten()
veh_dist -= veh_dist[0]
veh_spd = daq['VDS_Veh_Speed'][0, fslice(f0, fend)].flatten()
veh_spd_ip = np.interp(veh_dist_ip, veh_dist, veh_spd)
# store interpolated speed so we can plot it
interp_spds_by_scenario[scenario].append(veh_spd_ip)
print('\nStep 2. Plot speeds...')
# names of scenarios
scenario_names = ['Baseline',
'Advisory',
'Reg',
'Reg + Adv',
'Chevrons',
'Lines',
'Narrowing',
'Parallax',
'Force Rh',
'Lines w/ mid']
# intialize plot
plt.figure(figsize=(9,14))
plt.subplots_adjust(left=.05, right=.95,
bottom=.05, top=.95,
hspace=.27)
xticks = np.linspace(0,5280*1.25,11)
yticks = np.linspace(30,100,8)
for scenario in xrange(10):
print(" plotting scenario '%s'..."%scenario_names[scenario])
plt.subplot(10 ,1, scenario+1)
# figure out ensemble mean and confidence interval for scenario
means = np.mean(interp_spds_by_scenario[scenario], 0)
ci = stats.sem(interp_spds_by_scenario[scenario], 0)*1.96
ci_upper = np.add(means, ci)
ci_lower = np.subtract(means, ci)
# plot ci fill first. The z-order makes it look better if this
# is done first
plt.fill(np.concatenate((veh_dist_ip, veh_dist_ip[::-1])),
np.concatenate((ci_upper, ci_lower[::-1])),
linewidth=0, facecolor='r', alpha=.4)
plt.plot(veh_dist_ip, means, color='r')
# plot individual trends
for veh_spd_ip in interp_spds_by_scenario[scenario]:
plt.plot(veh_dist_ip, veh_spd_ip, color='b', alpha=.4)
# make things pretty
plt.xlim([xticks[0], xticks[-1]])
if scenario < 9:
plt.xticks(xticks, ['' for t in xticks])
else:
plt.xticks(xticks)
plt.ylim([yticks[0], yticks[-1]])
plt.yticks(yticks)
plt.grid()
plt.axhline(65, color='k', ls='-')
# put title on subplot
plt.title(scenario_names[scenario])
# save figure
plt.savefig('ensemble_spd_plot.png', dpi=300)
plt.close('all')
print('\nDone.\n\nMaking ensemble plot took %.1f s'%(time.time()-t0))
The Resulting Plot Download [hi-res]
Example Output:
Changing wd to 'C:\LocalData\Left Lane\'
Step 1. Find speeds interpolated by distance...
interpolating 'Part01\Left_01_20130424102744.hdf5'...
on scenario 0 [f136160:f140556]...
on scenario 1 [f160100:f164368]...
on scenario 9 [f183285:f187341]...
on scenario 2 [f207045:f211789]...
on scenario 8 [f238117:f242045]...
on scenario 3 [f287021:f291085]...
on scenario 7 [f305438:f309226]...
on scenario 4 [f329226:f333598]...
on scenario 6 [f353678:f357770]...
on scenario 5 [f372298:f376218]...
interpolating 'Part02\Left_02_20130425084730.hdf5'...
on scenario 1 [f66263:f70823]...
on scenario 2 [f91112:f96104]...
on scenario 0 [f117048:f121352]...
on scenario 3 [f142015:f146212]...
on scenario 9 [f168527:f172848]...
on scenario 4 [f215719:f219919]...
on scenario 8 [f235457:f239776]...
on scenario 5 [f260152:f264312]...
on scenario 7 [f284508:f288800]...
on scenario 6 [f303060:f307264]...
interpolating 'Part03\Left_03_20130425102301.hdf5'...
on scenario 2 [f404752:f409600]...
on scenario 3 [f431315:f435691]...
on scenario 1 [f454307:f458531]...
on scenario 4 [f480483:f484611]...
on scenario 0 [f505759:f510439]...
on scenario 5 [f553323:f557883]...
on scenario 9 [f572459:f576939]...
on scenario 6 [f596355:f600895]...
on scenario 8 [f619335:f623983]...
on scenario 7 [f638619:f643187]...
interpolating 'Part04\Left_04_20130425142804.hdf5'...
on scenario 3 [f1283366:f1288110]...
on scenario 4 [f1306317:f1310657]...
on scenario 2 [f1328473:f1333157]...
on scenario 5 [f1352321:f1356557]...
on scenario 1 [f1377257:f1381397]...
on scenario 6 [f1413697:f1417721]...
on scenario 0 [f1432357:f1436629]...
on scenario 7 [f1455933:f1460093]...
on scenario 9 [f1478705:f1482969]...
on scenario 8 [f1496273:f1500461]...
interpolating 'Part05\Left_05_20130425161122.hdf5'...
on scenario 4 [f1653820:f1658688]...
on scenario 5 [f1680123:f1684619]...
on scenario 3 [f1704163:f1708767]...
on scenario 6 [f1731115:f1735971]...
on scenario 2 [f1760943:f1765891]...
on scenario 7 [f1804847:f1809527]...
on scenario 1 [f1827111:f1831875]...
on scenario 8 [f1853443:f1858175]...
on scenario 0 [f1882583:f1887103]...
on scenario 9 [f1902147:f1906711]...
interpolating 'Part06\Left_06_20130426111502.hdf5'...
on scenario 5 [f68660:f72808]...
on scenario 6 [f91104:f95076]...
on scenario 4 [f111648:f115748]...
on scenario 7 [f133908:f138228]...
on scenario 3 [f159172:f163260]...
on scenario 8 [f197305:f201325]...
on scenario 2 [f215661:f219869]...
on scenario 9 [f237577:f241773]...
on scenario 1 [f259933:f263945]...
on scenario 0 [f277981:f281909]...
interpolating 'Part07\Left_07_20130426143846.hdf5'...
on scenario 6 [f102221:f106749]...
on scenario 7 [f125477:f130101]...
on scenario 5 [f148541:f153097]...
on scenario 8 [f172413:f177389]...
on scenario 4 [f199593:f204277]...
on scenario 9 [f241606:f246174]...
on scenario 3 [f261674:f266622]...
on scenario 0 [f286358:f290970]...
on scenario 2 [f310562:f315610]...
on scenario 1 [f331194:f335698]...
interpolating 'Part08\Left_08_20130426164114.hdf5'...
interpolating 'Part08\Left_08_20130426164301.hdf5'...
on scenario 7 [f541359:f546007]...
on scenario 8 [f566855:f571364]...
on scenario 6 [f590232:f594244]...
on scenario 9 [f614620:f619328]...
on scenario 5 [f639332:f643476]...
on scenario 0 [f679184:f683816]...
on scenario 4 [f698784:f702880]...
on scenario 1 [f722628:f725924]...
on scenario 3 [f743804:f747188]...
on scenario 2 [f759380:f763144]...
interpolating 'Part09\Left09_20130423155149.hdf5'...
on scenario 8 [f162205:f166433]...
on scenario 9 [f184789:f188889]...
on scenario 7 [f205988:f210024]...
on scenario 0 [f229996:f234073]...
on scenario 6 [f255428:f259481]...
on scenario 1 [f286700:f290676]...
on scenario 5 [f303416:f307184]...
on scenario 2 [f325500:f329456]...
on scenario 4 [f348236:f352152]...
on scenario 3 [f366216:f370244]...
interpolating 'Part10\Left10_20130423155149.hdf5'...
on scenario 9 [f162205:f166433]...
on scenario 0 [f184789:f188889]...
on scenario 8 [f205988:f210024]...
on scenario 1 [f229996:f234073]...
on scenario 7 [f255428:f259481]...
on scenario 2 [f286700:f290676]...
on scenario 6 [f303416:f307184]...
on scenario 3 [f325500:f329456]...
on scenario 5 [f348236:f352152]...
on scenario 4 [f366216:f370244]...
interpolating 'Part111\Left_11_20130430081052.hdf5'...
on scenario 0 [f92423:f97123]...
on scenario 1 [f118102:f122846]...
on scenario 9 [f140842:f145226]...
on scenario 2 [f165282:f170494]...
on scenario 8 [f193686:f198202]...
on scenario 3 [f228618:f233258]...
on scenario 7 [f247986:f252122]...
on scenario 4 [f271982:f276514]...
on scenario 6 [f296050:f300198]...
on scenario 5 [f314262:f318790]...
interpolating 'Part12\Left_12_20130429163745.hdf5'...
on scenario 1 [f534384:f538784]...
on scenario 2 [f560516:f564968]...
on scenario 0 [f584440:f588804]...
on scenario 3 [f609488:f613860]...
on scenario 9 [f635836:f640100]...
on scenario 4 [f667388:f671568]...
on scenario 8 [f686032:f690352]...
on scenario 5 [f711204:f715576]...
on scenario 7 [f734824:f739024]...
on scenario 6 [f753936:f757720]...
interpolating 'Part13\Left_13_20130429182923.hdf5'...
on scenario 2 [f935065:f939713]...
on scenario 3 [f962201:f966645]...
on scenario 1 [f984565:f988745]...
on scenario 4 [f1009065:f1013357]...
on scenario 0 [f1034765:f1038953]...
on scenario 5 [f1078849:f1083133]...
on scenario 9 [f1098269:f1102653]...
on scenario 6 [f1122573:f1127009]...
on scenario 8 [f1145449:f1149833]...
on scenario 7 [f1164257:f1168361]...
interpolating 'Part14\Left_14_20130430102504.hdf5'...
on scenario 3 [f570812:f575000]...
on scenario 4 [f593800:f598044]...
on scenario 2 [f616236:f619736]...
on scenario 5 [f636168:f640292]...
on scenario 1 [f660312:f663904]...
on scenario 6 [f735348:f738864]...
on scenario 0 [f749824:f753124]...
on scenario 7 [f768724:f772052]...
on scenario 9 [f786808:f790220]...
on scenario 8 [f801136:f804620]...
interpolating 'Part15\Left_15_20130430171947.hdf5'...
on scenario 4 [f43678:f48046]...
on scenario 5 [f74722:f79226]...
on scenario 3 [f97874:f102354]...
on scenario 6 [f127074:f131462]...
on scenario 2 [f153702:f158278]...
on scenario 7 [f210222:f214646]...
on scenario 1 [f228986:f233506]...
on scenario 8 [f254746:f259050]...
on scenario 0 [f280054:f284350]...
on scenario 9 [f299126:f303462]...
interpolating 'Part16\Left_16_20130501103917.hdf5'...
on scenario 5 [f194685:f198913]...
on scenario 6 [f217917:f222121]...
on scenario 4 [f239185:f243177]...
on scenario 7 [f261570:f265477]...
on scenario 3 [f284877:f288517]...
on scenario 8 [f323037:f326945]...
on scenario 2 [f340117:f344069]...
on scenario 9 [f361065:f364929]...
on scenario 1 [f382085:f386221]...
on scenario 0 [f400113:f404153]...
interpolating 'Part170\Left_17_20130501163745.hdf5'...
on scenario 9 [f38731:f43467]...
on scenario 0 [f63987:f68467]...
on scenario 8 [f86919:f91295]...
on scenario 1 [f111211:f115619]...
on scenario 7 [f137631:f142151]...
on scenario 2 [f189827:f194155]...
on scenario 6 [f209155:f213739]...
on scenario 3 [f234083:f238439]...
on scenario 5 [f257267:f262239]...
on scenario 4 [f277815:f282435]...
interpolating 'Part18\Left_18_20130502084422.hdf5'...
on scenario 7 [f73558:f78466]...
on scenario 8 [f96998:f101398]...
on scenario 6 [f121198:f125486]...
interpolating 'Part18\Left_18_reset_20130502090909.hdf5'...
on scenario 9 [f28675:f33231]...
on scenario 5 [f54815:f59131]...
on scenario 0 [f108075:f112539]...
on scenario 4 [f127539:f132243]...
on scenario 1 [f151951:f156467]...
on scenario 3 [f175747:f180227]...
on scenario 2 [f195191:f199687]...
interpolating 'Part19\Left_19_20130502153547.hdf5'...
on scenario 8 [f86913:f91289]...
on scenario 9 [f110897:f115154]...
on scenario 7 [f134410:f138358]...
on scenario 0 [f158478:f162838]...
on scenario 6 [f184926:f189350]...
on scenario 1 [f232014:f236686]...
on scenario 5 [f251954:f256378]...
on scenario 2 [f277142:f282482]...
on scenario 4 [f303294:f307894]...
on scenario 3 [f322662:f327246]...
interpolating 'Part200\Left_20_20130509094509.hdf5'...
on scenario 9 [f85480:f90052]...
on scenario 0 [f116921:f121541]...
on scenario 8 [f140221:f144701]...
on scenario 1 [f168982:f173186]...
on scenario 7 [f195587:f199755]...
on scenario 2 [f236595:f241227]...
on scenario 6 [f257391:f261695]...
on scenario 3 [f282387:f286815]...
on scenario 5 [f306847:f311175]...
on scenario 4 [f325439:f329763]...
Step 2. Plot speeds...
plotting scenario 'Baseline'...
plotting scenario 'Advisory'...
plotting scenario 'Reg'...
plotting scenario 'Reg + Adv'...
plotting scenario 'Chevrons'...
plotting scenario 'Lines'...
plotting scenario 'Narrowing'...
plotting scenario 'Parallax'...
plotting scenario 'Force Rh'...
plotting scenario 'Lines w/ mid'...
Done.
Making ensemble plot took 23.8 s