|
| 1 | +import numpy as np |
| 2 | +import glob |
| 3 | +import sys |
| 4 | + |
| 5 | +import matplotlib.pyplot as plt |
| 6 | + |
| 7 | +# Define grid dimensions globally |
| 8 | +ROWS = 28 |
| 9 | +COLS = 28 |
| 10 | + |
| 11 | + |
| 12 | +def plotGrids(gridData): |
| 13 | + if(gridData.shape[0] % ROWS != 0 or gridData.shape[1] != COLS): |
| 14 | + raise('Incompatible grid dimensionality: check data and assumed dimensions.') |
| 15 | + |
| 16 | + grids = gridData.shape[0]//ROWS |
| 17 | + |
| 18 | + print('Reshaping into', grids, 'grids of shape (', ROWS, ',', COLS, ')') |
| 19 | + gridData = gridData.reshape((grids, ROWS, COLS)) |
| 20 | + |
| 21 | + plotAnotherRange = True |
| 22 | + |
| 23 | + while(plotAnotherRange): |
| 24 | + start = -1 |
| 25 | + end = 1 |
| 26 | + print('Select the range of iterations to generate grid plots from.') |
| 27 | + print('0 means plot all iterations.') |
| 28 | + while((start < 0 or grids-1 < start) or (end < 1 or grids < end)): |
| 29 | + start = int(input('Start: ')) |
| 30 | + |
| 31 | + # If start is set to zero, plot everything. |
| 32 | + if(start == 0): |
| 33 | + continue |
| 34 | + |
| 35 | + end = int(input('End: ')) |
| 36 | + |
| 37 | + if start == 0: |
| 38 | + print('\nPlotting whole shebang!') |
| 39 | + else: |
| 40 | + print('\nPlotting range from iteration', start, 'to', end) |
| 41 | + |
| 42 | + # Plotting time! |
| 43 | + plt.figure() |
| 44 | + plt.ion() |
| 45 | + plt.imshow(gridData[start], cmap='hot', interpolation='nearest') |
| 46 | + plt.colorbar() |
| 47 | + plt.pause(.001) # Pause so that that GUI can do its thing. |
| 48 | + for g in gridData[start+1:end]: |
| 49 | + plt.imshow(g, cmap='hot', interpolation='nearest') |
| 50 | + plt.pause(.001) # Pause so that that GUI can do its thing. |
| 51 | + |
| 52 | + plotAnotherRange = str.lower(input('Plot another range? (y/n): ')) == 'y' |
| 53 | + |
| 54 | + |
| 55 | +def plotRewards(rewData, fname): |
| 56 | + cumRewards = np.cumsum(rewData) |
| 57 | + tsteps = np.array(range(len(cumRewards))) |
| 58 | + |
| 59 | + # Plotting time! |
| 60 | + plt.figure() |
| 61 | + plt.plot(tsteps, cumRewards) |
| 62 | + plt.xlabel('Timesteps') |
| 63 | + plt.ylabel('Cumulative Reward') |
| 64 | + plt.title("Cumulative Reward by Iteration") |
| 65 | + plt.savefig(fname[0:-4] + '.png', dpi=200) |
| 66 | + plt.pause(.001) # Pause so that that GUI can do its thing. |
| 67 | + |
| 68 | + |
| 69 | +def plotPerformance(perfData, fname): |
| 70 | + |
| 71 | + # Set bins to a tenth of the episodes, rounded up. |
| 72 | + binIdx = np.array(range(len(perfData)))//10 |
| 73 | + bins = np.bincount(binIdx, perfData).astype('uint32') |
| 74 | + |
| 75 | + # Plotting time! |
| 76 | + plt.figure() |
| 77 | + plt.bar(np.unique(binIdx), bins, color='seagreen') |
| 78 | + plt.xlabel('Episode Bins') |
| 79 | + plt.ylabel('Number of Intercepts') |
| 80 | + plt.title("Interception Performance Across Episodes") |
| 81 | + plt.savefig(fname[0:-4] + '.png', dpi=200) |
| 82 | + plt.pause(.001) # Pause so that that GUI can do its thing. |
| 83 | + |
| 84 | + |
| 85 | +def main(): |
| 86 | + """ |
| 87 | + File types: |
| 88 | + |
| 89 | + 0) grid - the 2D matrix observation |
| 90 | + 1) reward - list of rewards per iteration |
| 91 | + 2) performance - list of performance values |
| 92 | + """ |
| 93 | + fileType = 0 # default to grid |
| 94 | + |
| 95 | + # By default, we'll search the examples directory, but tweak as needed. |
| 96 | + files = glob.glob('../../examples/*/out/*csv') |
| 97 | + |
| 98 | + if len(files) == 0: |
| 99 | + print('Could not find any csv files. Exiting...') |
| 100 | + sys.exit() |
| 101 | + |
| 102 | + plotAnotherFile = True |
| 103 | + |
| 104 | + while plotAnotherFile: |
| 105 | + print('Select the file to generate grid plots from.') |
| 106 | + for i,f in enumerate(files): |
| 107 | + print(str(i), '-', f) |
| 108 | + |
| 109 | + # Select the intended file. |
| 110 | + sel = -1 |
| 111 | + while sel < 0 or len(files) < sel: |
| 112 | + sel = int(input('\nFile selection: ')) |
| 113 | + |
| 114 | + fileToPlot = files[sel] |
| 115 | + |
| 116 | + # Check file type |
| 117 | + if(0 < fileToPlot.find('grid')): |
| 118 | + print('\nFound \'grid\' in name: assuming a grid file type.') |
| 119 | + fileType = 0 |
| 120 | + elif(0 < fileToPlot.find('rew')): |
| 121 | + print('\nFound \'rew\' in name: assuming a reward file type.') |
| 122 | + fileType = 1 |
| 123 | + |
| 124 | + elif(0 < fileToPlot.find('perf')): |
| 125 | + print('\nFound \'perf\' in name: assuming a performance file type.') |
| 126 | + fileType = 2 |
| 127 | + else: |
| 128 | + print('\nUnknown file type. Which type are we plotting?') |
| 129 | + print('\n0) grid\n1) reward\n2) performance') |
| 130 | + fileType = -1 |
| 131 | + while fileType < 0 or 2 < fileType: |
| 132 | + fileType = int(input('\nFile type: ')) |
| 133 | + |
| 134 | + print('\nPlotting: ', fileToPlot) |
| 135 | + data = np.genfromtxt(fileToPlot, delimiter=',') |
| 136 | + |
| 137 | + # Plot by file type |
| 138 | + if(fileType == 0): |
| 139 | + plotGrids(data) |
| 140 | + elif(fileType == 1): |
| 141 | + plotRewards(data, fileToPlot) |
| 142 | + elif(fileType == 2): |
| 143 | + plotPerformance(data, fileToPlot) |
| 144 | + else: |
| 145 | + print('ERROR: Unknown file type') |
| 146 | + |
| 147 | + plotAnotherFile = str.lower(input('Plot another file? (y/n): ')) == 'y' |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == '__main__': |
| 152 | + main() |
| 153 | + |
0 commit comments