Skip to content

Commit

Permalink
Update all analysis scripts for new json (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsavarela committed Feb 27, 2020
1 parent 3122990 commit 26a856b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 30 deletions.
29 changes: 21 additions & 8 deletions analysis/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import os
from glob import glob


# third-party libs
import dill
import numpy as np
Expand All @@ -33,13 +34,15 @@
from ilurl.envs.base import TrafficLightQLEnv

ROOT_DIR = os.environ['ILURL_HOME']
EMISSION_DIR = f"{ROOT_DIR}/data/emissions/"
CONFIG_DIR = ('4545', '5040', '5434', '6030')
# EMISSION_DIR = f"{ROOT_DIR}/data/emissions/"
EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x04/"
# CONFIG_DIR = ('4545', '5040', '5434', '6030')
CONFIG_DIR = ('6030',)

if __name__ == '__main__':

# this loop acumulates experiments
ext = '.9000.w.info.json'
ext = '.9000.l.info.json'
states = defaultdict(list)

for config_dir in CONFIG_DIR:
Expand All @@ -55,11 +58,22 @@
env = TrafficLightQLEnv.load(f"{filename}.pickle")

# observation spaces
observation_spaces_per_cycle = output['observation_spaces']
for observation_space in observation_spaces_per_cycle:
for i, values in enumerate(env.ql_params.split_space(observation_space)):
if 'cycle' not in output:
# 0x00, 0x01, 0x02, 0x03
# deprecate
observation_spaces = [output['observation_spaces']]
else:
observation_spaces = output['observation_spaces']

for observation_space in observation_spaces:
variables = \
env.ql_params.split_space(observation_space)
for i, values in enumerate(variables):

()
label = env.ql_params.states_labels[i]
states[label] += values
states[label] += \
[val for value in values for val in value]

# plot building
num_bins = 50
Expand All @@ -72,7 +86,6 @@
# mean and standard deviation of the distribution
mu = np.mean(values)
sigma = np.std(values)

# the histogram of the data
values_normalized = [
round((v - mu) / sigma, 2) for v in values
Expand Down
10 changes: 4 additions & 6 deletions analysis/q.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@

ROOT_DIR = os.environ['ILURL_HOME']
# EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x02/"
EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x03/"
EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x04/"
# CONFIG_DIRS = ('4545', '5040', '5434', '6030')
CONFIG_DIRS = ('6030',)


filenames = ('intersection_20200222-0900161582362016.287294.Q.1-49',
'intersection_20200222-0900371582362037.617882.Q.1-49',
'intersection_20200222-0900411582362041.078803.Q.1-49',
'intersection_20200222-0900481582362048.594145.Q.1-49',
'intersection_20200222-0900511582362051.183386.Q.1-49')
filenames = ('intersection_20200227-2043431582836223.796797.Q.1-99',
'intersection_20200227-2128041582838884.952684.Q.1-100',
'intersection_20200227-2128511582838931.2348.Q.1-100')


def qdist(Q, Q1):
Expand Down
10 changes: 4 additions & 6 deletions analysis/rewards.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Analyses aggregate experiment files e.g info and envs"""
__author__ = 'Guilherme Varela'
__date__ = '2020-02-13'
import pdb

import os
import argparse
import json
Expand Down Expand Up @@ -144,13 +144,13 @@ def str2bool(v):

color = 'tab:blue'
ax1.set_ylabel('Avg. speed', color=color)
ax1.plot(vels, color=color)
ax1.plot(np.mean(vels, axis=1), color=color)
ax1.tick_params(axis='y', labelcolor=color)

color = 'tab:red'
ax2 = ax1.twinx()
ax2.set_ylabel('Avg. vehicles', color=color)
ax2.plot(vehs, color=color)
ax2.plot(np.mean(vehs, axis=1), color=color)
ax2.tick_params(axis='y', labelcolor=color)

plt.title(f'{phase_split}:speed and count\n({db_type}, n={num_dbs})')
Expand All @@ -164,7 +164,7 @@ def str2bool(v):

ax1.set_ylabel('Avg. Reward per Cycle', color=color)
ax2.tick_params(axis='y', labelcolor=color)
ax1.plot(rets, color=color)
ax1.plot(np.mean(rets, axis=1), color=color)
plt.title(f'{phase_split}:avg. cycle return\n({db_type}, n={num_dbs})')
plt.savefig(f'{files_dir}{phase_split}_{db_type}_rewards.png')
plt.show()
Expand All @@ -177,8 +177,6 @@ def str2bool(v):
ax1.set_xlabel(f'Cycles ({cycle_time} sec)')
ax1.set_ylabel('ratio optimal action')

import pdb
pdb.set_trace()
cumacts = np.cumsum(acts == optact, axis=0)
weights = np.arange(1, len(acts) + 1)

Expand Down
21 changes: 14 additions & 7 deletions analysis/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
from glob import glob


# third-party libs
import dill
import numpy as np
Expand All @@ -27,13 +28,13 @@

ROOT_DIR = os.environ['ILURL_HOME']
# EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x02/"
EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x03/"
EMISSION_DIR = f"{ROOT_DIR}/data/experiments/0x04/"
# CONFIG_DIR = ('4545', '5040', '5434', '6030')
CONFIG_DIR = ('6030',)

if __name__ == '__main__':
# this loop acumulates experiments
ext = '.450000.l.info.json'
ext = '.9000.l.info.json'
phases = defaultdict(list)
labels = []
desired_velocity = None
Expand Down Expand Up @@ -66,11 +67,17 @@
category_counts = env.ql_params.category_counts

# observation spaces
observation_spaces_per_cycle = output['observation_spaces']
for intersection_space in observation_spaces_per_cycle:
for phase_space in intersection_space:
for i, phase in enumerate(phase_space):
phases[i] += [phase]
if 'cycle' not in output:
# deprecate: 0x00, 0x01, 0x02, 0x03
observation_spaces = [output['observation_spaces']]
else:
observation_spaces = output['observation_spaces']

for observation_space in observation_spaces:
for intersection_space in observation_space:
for phase_space in intersection_space:
for i, phase in enumerate(phase_space):
phases[i] += [phase]

_, ax = plt.subplots()
for i, label in enumerate(labels):
Expand Down
2 changes: 1 addition & 1 deletion ilurl/core/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import tempfile
import time
from collections import defaultdict
import pdb


from tqdm import tqdm

Expand Down
2 changes: 1 addition & 1 deletion ilurl/core/ql/choice.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""The module helps define the q learning dictionary"""
__author__ = "Guilherme Varela"
__date__ = "2019-07-25"
import pdb


from numpy import argmax, sqrt, log, isnan
from numpy.random import choice, rand
Expand Down
2 changes: 1 addition & 1 deletion ilurl/core/ql/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = "Guilherme Varela"
__date__ = "2019-07-26"
import pdb

import numpy as np

REWARD_TYPES = ('fix', 'weighted_average', 'score', 'target_velocity')
Expand Down

0 comments on commit 26a856b

Please sign in to comment.