-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathopenAI_montior_processing.py
executable file
·131 lines (96 loc) · 3.78 KB
/
openAI_montior_processing.py
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
#! /usr/bin/env python
###############################################################################
# openAI_montior_processing.py
#
# Script to process the json data generated by the monitoring wrapper on
# OpenAI gym environments.
#
# NOTE: Any plotting is set up for output, not viewing on screen.
# So, it will likely be ugly on screen. The saved PDFs should look
# better.
#
# Created: 07/12/17
# - Joshua Vaughan
# - http://www.ucs.louisiana.edu/~jev9637
#
# Modified:
# *
#
# TODO:
# *
###############################################################################
import numpy as np
import matplotlib.pyplot as plt
import json # the data files generated are json
# TODO: 07/12/17 - JEV - Add GUI, argparse, or CLI for selecting file
FILENAME = "example_data/duel_dqn_planar_crane-v0_monitor_1024_4_100000_2017-07-13_222427/openaigym.episode_batch.0.5356.stats.json"
with open(FILENAME) as data_file:
data = json.load(data_file)
# This is the key data that we're interested in plotting. You can use the
# method data.keys() to see others
time = np.array(data['timestamps']) - data['timestamps'][0]
rewards = np.array(data['episode_rewards'])
episode_lengts = np.array(data['episode_lengths'])
#----- Plot reward vs computation time -----
# Set the plot size - 3x2 aspect ratio is best
fig = plt.figure(figsize=(6,4))
ax = plt.gca()
plt.subplots_adjust(bottom=0.17, left=0.17, top=0.96, right=0.96)
# Change the axis units font
plt.setp(ax.get_ymajorticklabels(),fontsize=18)
plt.setp(ax.get_xmajorticklabels(),fontsize=18)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Turn on the plot grid and set appropriate linestyle and color
ax.grid(True,linestyle=':', color='0.75')
ax.set_axisbelow(True)
# Define the X and Y axis labels
plt.xlabel('Computation Time (s)', fontsize=22, weight='bold', labelpad=5)
plt.ylabel('Reward', fontsize=22, weight='bold', labelpad=10)
plt.plot(time, rewards, linewidth=2, linestyle='-', label=r'Data 1')
# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(0,10)
# Create the legend, then fix the fontsize
# leg = plt.legend(loc='upper right', ncol = 1, fancybox=True)
# ltext = leg.get_texts()
# plt.setp(ltext,fontsize=18)
# Adjust the page layout filling the page using the new tight_layout command
plt.tight_layout(pad=0.5)
# save the figure as a high-res pdf in the current folder
# plt.savefig('plot_filename.pdf')
#----- Plot reward vs episode number -----
# Set the plot size - 3x2 aspect ratio is best
fig = plt.figure(figsize=(6,4))
ax = plt.gca()
plt.subplots_adjust(bottom=0.17, left=0.17, top=0.96, right=0.96)
# Change the axis units font
plt.setp(ax.get_ymajorticklabels(),fontsize=18)
plt.setp(ax.get_xmajorticklabels(),fontsize=18)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Turn on the plot grid and set appropriate linestyle and color
ax.grid(True,linestyle=':', color='0.75')
ax.set_axisbelow(True)
# Define the X and Y axis labels
plt.xlabel('Episode', fontsize=22, weight='bold', labelpad=5)
plt.ylabel('Reward', fontsize=22, weight='bold', labelpad=10)
plt.plot(rewards, linewidth=2, linestyle='-', label=r'Data 1')
# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(0,10)
# Create the legend, then fix the fontsize
# leg = plt.legend(loc='upper right', ncol = 1, fancybox=True)
# ltext = leg.get_texts()
# plt.setp(ltext,fontsize=18)
# Adjust the page layout filling the page using the new tight_layout command
plt.tight_layout(pad=0.5)
# save the figure as a high-res pdf in the current folder
# plt.savefig('plot_filename.pdf')
# show the figure
plt.show()