Skip to content

Commit

Permalink
Plotting now works in general for all datasets. plot_throughput only …
Browse files Browse the repository at this point in the history
…has to be run in the current directory where the data resides.
  • Loading branch information
temetski committed Feb 5, 2014
1 parent a915760 commit 775d1e8
Showing 1 changed file with 55 additions and 58 deletions.
113 changes: 55 additions & 58 deletions plot_throughput.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
@author: Damian
"""
import numpy as np
import cPickle
#import cPickle
import matplotlib
matplotlib.use("Agg")
import matplotlib.lines as lines
import matplotlib.pyplot as plt
import glob
import re
from subprocess import call
import h5py

Expand All @@ -19,72 +21,67 @@
ROADLENGTH = 50
TRIALS = 50

area = 1*(REAL_LANES)*ROADLENGTH
speed = 0
size = -1
last = -1
AREA = 1*(REAL_LANES)*ROADLENGTH
SPEED = 0
SIZE = -1
LAST = -1

def throughputnorm(trial, sizes):
x= [vehicles[speed]/ROADLENGTH for vehicles in trial[-1]]
return 1.*np.sum(x)/len(x)
def throughput(_trial):
'''Computes the throughput of one trial of the simulation.'''
return np.sum([vehicles[SPEED]/ROADLENGTH for vehicles in _trial[LAST]])

def throughput(trial, sizes):
return np.sum([vehicles[speed]/ROADLENGTH for vehicles, s in zip(trial[last],
sizes[last]) if s])
def through(vehicles):
return 1.*np.sum(i[0] for i in vehicles)/ROADLENGTH/len(vehicles)

def load(ratio, density):
throughput = np.array([], dtype=np.int8)
filename = "CarRatio.%.2f_Density.%.2f.h5" % (ratio,density)
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 t in xrange(TRIALS):
group = "CarRatio::%.2f/Density::%.2f/" % (ratio, density)
trial = "Trial::%04d" % (t+1)
dset = fid[group+trial]
throughput = np.append(throughput, dset)

throughput = np.reshape(throughput, (TRIALS, dset.shape[0],
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 throughput


return vehicledata

fluxes = np.zeros([1,19])
variance = np.zeros([1,19, 2])
for x, ratio in enumerate(np.arange(1)):#0.1,1,0.1)):
if ratio == 1:
ratio = 1
if ratio == 0:
ratio = 0
for y, density in enumerate(np.arange(0.05,1,0.05)):
vehicledata = load(ratio, density)
sizedata = vehicledata[:,2001:,:,size]
vehicledata = np.cumsum(vehicledata[:,2001:,:,:], axis=1)
vehicleflux = [throughput(trial,sizes)
for trial, sizes in zip(vehicledata, sizedata)]
fluxes[x,y] = np.mean(vehicleflux)
variance[x,y] = [np.max(vehicleflux), np.min(vehicleflux)]
if __name__ == "__main__":
FILES = glob.glob("*.bz2")
DENSITIES = np.arange(0.05, 1, 0.05)
RATIOS = np.array([])
for i in FILES:
car_ratio = float(re.findall(r"CarRatio.(\d.\d+)", i)[0])
if car_ratio not in RATIOS:
RATIOS = np.append(RATIOS, car_ratio)

THROUGHPUT = np.zeros([len(RATIOS), len(DENSITIES)])
STDEV = np.zeros([len(RATIOS), len(DENSITIES)])
for x, ratio in enumerate(np.arange(1)):#0.1,1,0.1)):
for y, density in enumerate(DENSITIES):
data = load(ratio, density)
# sizedata = vehicledata[:,2001:,:,SIZE]
data = np.cumsum(data[:, :, :, :], axis=1)
flux = [throughput(trial)
for trial in data]
THROUGHPUT[x, y] = np.mean(flux)
STDEV[x, y] = np.std(flux)

markers = list(lines.Line2D.markers.keys())
markers.sort()
N = len(markers)
labels = np.arange(0.1, 1, 0.1)
for ydata, label, i in zip(fluxes, labels, range(len(labels))):
marker = markers[i%N]
# plt.plot(np.arange(0.05,1,0.05), ydata, label=label, marker=marker)
plt.errorbar(np.arange(0.05,1,0.05), ydata, yerr=np.abs(variance[i].T-ydata),label=label, marker=marker)
#plt.legend(loc="upper left", bbox_to_anchor=(1,1))
#plt.legend()
plt.xlabel('road density', size=18)
plt.ylabel('number of exiting vehicles', size=18)
#plt.ylim(0,2000)
#plt.xlim(0,1.2)
plt.title(r'Two Real lanes with adaptation', size=18)
__markers__ = list(lines.Line2D.markers.keys())
__markers__.sort()
N = len(__markers__)
__labels__ = np.arange(0.1, 1, 0.1)
for ydata, label, i in zip(THROUGHPUT, __labels__, range(len(__labels__))):
marker = __markers__[i % N]
plt.errorbar(DENSITIES, ydata,
yerr=STDEV[i, :], label=label, marker=marker)
plt.legend(loc="upper left", bbox_to_anchor=(1, 1))
plt.xlabel('road density', size=18)
plt.ylabel('number of exiting vehicles', size=18)
#plt.ylim(0,2000)
#plt.xlim(0,1.2)
plt.title(r'Two Real lanes with adaptation', size=18)

plt.savefig("throughput.png")
plt.savefig("throughput.png")

0 comments on commit 775d1e8

Please sign in to comment.