<reponame>DocVaughan/CRAWLAB-Code-Snippets<gh_stars>10-100
#! /usr/bin/env python

###############################################################################
# kerasRL_FileLogger_processing.py
#
# Script to process the json data generated by the FileLogger callback in 
# the keras-rl python module
#
# 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/13/17
#   - <NAME>
#   - <EMAIL>
#   - 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/13/17 - JEV - Add GUI, argparse, or CLI for selecting file
FILENAME = 'logs/ddpg_planar_crane_continuous-v0_log_32_3_100000_2017-07-20_012022.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
duration = np.array(data['duration'])
episode = np.array(data['episode'])
episode_reward = np.array(data['episode_reward'])
episode_length = np.array(data['nb_episode_steps'])
loss = np.array(data['loss'])
mean_abs_error = np.array(data['mean_absolute_error'])
mean_q = np.array(data['mean_q'])
episode_steps = np.array(data['nb_episode_steps'])
cumulative_steps = np.array(data['nb_steps'])

# time = 

# ---- Plot evolution of reward over episodes --------------------------------
# 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(episode, episode_reward, linewidth=2, linestyle='-', label=r'Data 1')

# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(-1e4,1e3)

# 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('episode_reward.pdf')


# ---- Plot evolution of reward over episodes --------------------------------
# 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('Loss', fontsize=22, weight='bold', labelpad=10)
 
plt.plot(episode, loss, linewidth=2, linestyle='-', label=r'Loss')

# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(-1e4,1e3)

# 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('episode_loss.pdf')


# ---- Plot evolution of reward over episodes --------------------------------
# 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('Mean($Q$)', fontsize=22, weight='bold', labelpad=10)
 
plt.plot(episode, mean_q, linewidth=2, linestyle='-', label=r'Loss')

# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(-1e4,1e3)

# 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('episode_meanQ.pdf')

plt.show()

#! /usr/bin/env python

###############################################################################
# kerasRL_FileLogger_processing.py
#
# Script to process the json data generated by the FileLogger callback in 
# the keras-rl python module
#
# 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/13/17
#   - <NAME>
#   - <EMAIL>
#   - 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/13/17 - JEV - Add GUI, argparse, or CLI for selecting file
FILENAME = 'logs/ddpg_planar_crane_continuous-v0_log_32_3_100000_2017-07-20_012022.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
duration = np.array(data['duration'])
episode = np.array(data['episode'])
episode_reward = np.array(data['episode_reward'])
episode_length = np.array(data['nb_episode_steps'])
loss = np.array(data['loss'])
mean_abs_error = np.array(data['mean_absolute_error'])
mean_q = np.array(data['mean_q'])
episode_steps = np.array(data['nb_episode_steps'])
cumulative_steps = np.array(data['nb_steps'])

# time = 

# ---- Plot evolution of reward over episodes --------------------------------
# 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(episode, episode_reward, linewidth=2, linestyle='-', label=r'Data 1')

# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(-1e4,1e3)

# 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('episode_reward.pdf')


# ---- Plot evolution of reward over episodes --------------------------------
# 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('Loss', fontsize=22, weight='bold', labelpad=10)
 
plt.plot(episode, loss, linewidth=2, linestyle='-', label=r'Loss')

# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(-1e4,1e3)

# 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('episode_loss.pdf')


# ---- Plot evolution of reward over episodes --------------------------------
# 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('Mean($Q$)', fontsize=22, weight='bold', labelpad=10)
 
plt.plot(episode, mean_q, linewidth=2, linestyle='-', label=r'Loss')

# uncomment below and set limits if needed
# plt.xlim(0,5)
# plt.ylim(-1e4,1e3)

# 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('episode_meanQ.pdf')

plt.show()
#! /usr/bin/env python ############################################################################### # kerasRL_FileLogger_processing.py # # Script to process the json data generated by the FileLogger callback in # the keras-rl python module # # 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/13/17 #   - <NAME> #   - <EMAIL> #   - http://www.ucs.louisiana.edu/~jev9637 # # Modified: #   * # # TODO: #   * ############################################################################### # the data files generated are json # TODO: 07/13/17 - JEV - Add GUI, argparse, or CLI for selecting file # This is the key data that we're interested in plotting. You can use the # method data.keys() to see others # time = # ---- Plot evolution of reward over episodes -------------------------------- # Set the plot size - 3x2 aspect ratio is best # Change the axis units font # Turn on the plot grid and set appropriate linestyle and color # Define the X and Y axis labels # uncomment below and set limits if needed # plt.xlim(0,5) # plt.ylim(-1e4,1e3) # 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 # save the figure as a high-res pdf in the current folder # plt.savefig('episode_reward.pdf') # ---- Plot evolution of reward over episodes -------------------------------- # Set the plot size - 3x2 aspect ratio is best # Change the axis units font # Turn on the plot grid and set appropriate linestyle and color # Define the X and Y axis labels # uncomment below and set limits if needed # plt.xlim(0,5) # plt.ylim(-1e4,1e3) # 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 # save the figure as a high-res pdf in the current folder # plt.savefig('episode_loss.pdf') # ---- Plot evolution of reward over episodes -------------------------------- # Set the plot size - 3x2 aspect ratio is best # Change the axis units font # Turn on the plot grid and set appropriate linestyle and color # Define the X and Y axis labels # uncomment below and set limits if needed # plt.xlim(0,5) # plt.ylim(-1e4,1e3) # 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 # save the figure as a high-res pdf in the current folder # plt.savefig('episode_meanQ.pdf')