-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeed_variance.py
More file actions
141 lines (130 loc) · 4.92 KB
/
speed_variance.py
File metadata and controls
141 lines (130 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 22 00:18:38 2013
@author: Damian
"""
from __future__ import division
import numpy as np
import matplotlib
from load_params import ROADLENGTH, TRIALS, REAL_LANES, \
VIRTUAL_LANES, SLOWDOWN, LANE_CHANGE_PROB
matplotlib.use("Agg")
matplotlib.rcParams.update({'font.size': 15})
matplotlib.rcParams.update({'axes.labelsize': 17})
import matplotlib.pyplot as plt
import glob
import re
import os
from subprocess import call
import h5py
#REAL_LANES = 4
#ROADLENGTH = 100
#TRIALS = 50
#AREA = 1 * (REAL_LANES) * ROADLENGTH
POS = 0
LANE = 1
SPEED = 2
SIZE = 3
LAST = -1
def v_dist(trial):
"""Computes the throughput of one trial of the simulation."""
return [np.mean(time) for time in trial]
def load(_ratio, _density):
"""Loads data from the hdf5 dataset."""
vehicledata = np.array([], dtype=np.int8)
filename = "CarRatio.%.2f_Density.%.2f.h5" % (_ratio, _density)
call(['bunzip2', filename + '.bz2'])
fid = h5py.File(filename, 'r')
for n in xrange(TRIALS):
group = "CarRatio::%.2f/Density::%.2f/" % (_ratio, _density)
_trial = "Trial::%04d" % (n + 1)
dset = fid[group + _trial]
vehicledata = np.append(vehicledata, dset)
vehicledata = np.reshape(vehicledata, (TRIALS, dset.shape[0],
dset.shape[1],
dset.shape[2]))
fid.close()
call(["bzip2", "-6", filename])
return vehicledata
if __name__ == "__main__":
import time
to = time.time()
__plot_ratios__ = [0, 0.25, 0.5, 0.75, 1]
DIRNAME = os.path.split(os.getcwd())[1]
DENSITIES = np.arange(0.01, 1.,0.01)
RATIOS = [0, 0.25, 0.5, 0.75, 1]
p_lambda = np.arange(0,1.1,0.1)
RATIOS = [0,0.5,1]
DENSITIES = [0.09,0.5,0.95]
density = 0.9
ratio = 1.
VDIST = [[None for i in range(len(DENSITIES))]
for j in range(len(RATIOS))]
VDIST_SPACE = [[None for i in range(len(DENSITIES))]
for j in range(len(RATIOS))]
# for x, ratio in enumerate(RATIOS):
variance = []
mean = []
# for y, density in enumerate(DENSITIES):
for p in p_lambda:
data = np.load("lanechange_%.1f_virt_0/CarRatio.%.2f_Density.%.2f.npz" %
(p,ratio, density))["data"]
datasum = data[:, :, :, SPEED]
VDIST_SPACE = np.mean(datasum, axis=2)
variance.append(np.percentile(VDIST_SPACE,[25,75]))
mean.append(np.mean(VDIST_SPACE))
variance = np.array(variance)
fig = plt.figure(1)
ax = fig.add_subplot(111)
plt.grid()
bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.9)
ax.plot(p_lambda, mean, color='k')
# ax2.plot(p_lambda, variance[0,:])
# ax3.plot(p_lambda, variance[1,:])
ax.fill_between(p_lambda, variance[:,0], variance[:,1], color='0.7')
# ax.plot(p_lambda, variance, ls='none', marker='o', markersize=10, label="variance")
# ax2 = ax.twinx()
# ax.plot(p_lambda, mean, ls='none', marker='o', markersize=10, label="mean")
# ydata = VDIST.flatten()
# bin1 = (np.max(ydata)-np.min(ydata))/50
# weights = np.ones_like(ydata)/len(ydata)
# plt.hist(ydata, bins=50, color='b', alpha=0.7, label=r"$<v>_{time}$", weights=weights, edgecolor='b')
#
# ydata = VDIST_SPACE.flatten()
# weights = np.ones_like(ydata)/len(ydata)
# bin2 = (np.max(ydata)-np.min(ydata))/50
# ratiobin=bin2/bin1
# plt.hist(ydata, bins=50, color='0.5', alpha=0.8, label=r"$<v>_{space}$", weights=weights/ratiobin, edgecolor='0.5')
ax.set_xlabel(r'Lanechane Probability ($p_\lambda$)')
ax.set_ylabel('Speed Variance')
# ax2.set_ylabel("Mean Speed")
ax.legend(loc='best')
# ax2.legend(loc='best')
fig.savefig('distributions/variance%.1f_%.1f_%s.pdf' % (ratio,density,DIRNAME), bbox_inches='tight', dpi=300)
fig.clf()
# data = np.load("CarRatio.%.2f_Density.%.2f.npz" % (0, 0.72))["data"]
# datasum = data[:, :, :, SPEED]
# VDIST = np.mean(datasum, axis=1)
# VDIST_SPACE = np.mean(datasum, axis=2)
#
# fig = plt.figure(1)
# ax = fig.add_subplot(111)
# plt.grid()
# bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.9)
# ydata = VDIST.flatten()
# bin1 = (np.max(ydata)-np.min(ydata))/50
# weights = np.ones_like(ydata)/len(ydata)
# plt.hist(ydata, bins=50, color='b', alpha=0.7, label=r"$<v>_{time}$", weights=weights, edgecolor='b')
#
# ydata = VDIST_SPACE.flatten()
# weights = np.ones_like(ydata)/len(ydata)
# bin2 = (np.max(ydata)-np.min(ydata))/50
# ratiobin=bin2/bin1
# plt.hist(ydata, bins=50, color='0.5', alpha=0.8, label=r"$<v>_{space}$", weights=weights/ratiobin, edgecolor='0.5')
# ax.set_xlabel(r'Speed ($v$)')
# ax.set_ylabel('Probability')
# plt.legend(loc='best')
#
# fig.savefig('../distributions/vdist_stacked_%.1f_%.1f_%s.pdf' % (0,0.72,DIRNAME), bbox_inches='tight', dpi=300)
# ax.cla()