This repository was archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.py
More file actions
183 lines (144 loc) · 5.78 KB
/
plot.py
File metadata and controls
183 lines (144 loc) · 5.78 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#****************************************************************************
# *
# File: plotUtil.py *
# *
# Author: Branch Vincent *
# *
# Date: Jun 12, 2016 *
# *
# Purpose: This file plots the utilization data from either one or *
# multiple simulation runs. *
# *
#****************************************************************************
# Packages
#from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import csv
import sys
# Set arguments
inFile = sys.argv[1]
outFile1 = '/Users/Branch/Desktop/engGraph.pdf'
outFile2 = '/Users/Branch/Desktop/conGraph.pdf'
numInts = 24
# Global variables
filePath = '/Users/Branch/Documents/Academic/Year 1/Summer/DES Code/Data/'
numTypes = 9
numRuns = 1
oneRun = 0
#****************************************************************************
# *
# Function: main *
# *
#****************************************************************************
def main():
# Set input and output files
inFile = '/Users/Branch/Desktop/DES/DES/Output/Results/eng.csv'
outFile = '/Users/Branch/Desktop/DES/DES/Output/Results/engGraph.jpg'
# if oneRun:
# inFile = filePath + 'singleRun.csv'
# outFile = filePath + 'singleGraph.pdf'
# else:
# inFile = filePath + 'batchRun.csv'
# outFile = filePath + 'batchGraph.pdf'
# Get and plot data
time, data, error = getData(inFile) # time, utilization, std dev
plotData(time, data, error, outFile)
#****************************************************************************
# *
# Function: getData *
# *
# Purpose: To get the utilization data from the specified csv file *
# *
#****************************************************************************
def getData(inputFile):
# Set global variable to change value
global numRuns
# Initialize csv reader and data arrays
reader = csv.reader(open(inputFile), delimiter=',') # Open file
header = next(reader) # Skip header line
time, error = [], []
data = [[] for i in range(numTypes)]
# Read csv file, depending on number of runs
if oneRun: # For one run,
for row in reader:
time.append(float(row[0])) # Add time interval
for i in range(numTypes):
data[i].append(float(row[i+1])) # Add utilization for each task type
else: # For multiple runs,
print("Time, Util, Std Dev")
for row in reader:
# if (row[0] == 'Utilization'):
# for i in xrange(24):
print("%s, %s, %s" %(row[0], row[-3], row[-2]))
time.append(float(row[0])) # Add time interval
data[0].append(float(row[-3])) # Add avg utilization
error.append(float(row[-2])) # Add std dev
numRuns = len(row) - 2 # Update number of runs
return time, data, error
#****************************************************************************
# *
# Function: plotData *
# *
# Purpose: To plot the utilization data *
# *
#****************************************************************************
def plotData(time, data, error, outFile):
# Set variables
intSize = time[1] - time[0] # Interval size
endTime = time[-1] + intSize # Simulation end time
endTimes = [30, endTime - 30, endTime] # Phase end times
numInts = endTimes[2]/intSize # Number of intervals
time[:] = [t + intSize/2 for t in time] # Adjust to center bars
# Initialize plot parameters
# colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
colors = color_list = plt.cm.Set1(np.linspace(0, 1, numTypes))
# patterns = ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']
labels = ['0: Communicating', '1: Exception Handling', '2: Paperwork',
'3: MOW', '4: Speed Restriction', '5: Signal Response',
'6: Monitoring In', '7: Monitoring Out', '8: Planning Ahead']
# Initialize graph
plt.title("Engineer's Utilization for %d Runs" %numRuns)
plt.xlabel("Time (min)")
plt.ylabel("Utilization (%)")
plt.ylim(0, 1.1)
plt.xlim(0, endTimes[2])
# plt.xticks(np.arange(0, endTimes[2] + intSize, intSize))
ax = plt.gca()
# Plot utilization
if oneRun: # For one run,
for i in range(len(data)):
plt.bar(time, data[i], # Plot all task types
width = intSize/2, # Set bar width
bottom = np.sum(data[:i], axis = 0), # Set bottom bars
color = colors[i % len(colors)], # Set bar color
linewidth = 0.1,
# edgecolor = colors[i % len(colors)], # Set bar edge color
# hatch = patterns[i % len(patterns)], # Set pattern
label = labels[i], # Set label
align = 'center') # Align bars
# lgd = plt.legend(title='Task Types', # Set legend
# loc='center left',
# bbox_to_anchor=(1, 0.5))
handles, labels = ax.get_legend_handles_labels()
lgd = ax.legend(handles[::-1], labels[::-1],
title='Task Types', loc='center left',
bbox_to_anchor=(1, 0.5), prop={'size':'medium'})
# lgd.get_title().set_fontsize('6') #legend 'Title' fontsize
# plt.setp(plt.gca().get_legend().get_texts(), fontsize='12')
else: # For multiple runs,
plt.bar(time, data[0], # Plot avg for all types
width = intSize/2, # Set bar width
color = 'r', # Set color
linewidth = 0.1, # Set bar border width
align = 'center', # Align bars
yerr = error) # Set error bars
# lgd = [] # Set legend
lgd = ax.legend(['All task types', 'Std Dev'], loc=9, bbox_to_anchor=(0.5, -0.1))
# Plot phase lines
for i in range(2):
plt.plot((endTimes[i], endTimes[i]), (0, 1.10), 'm--')
# Save file
plt.savefig(outFile, additional_artists=lgd, bbox_inches="tight")
# Forward declaration
if __name__ == '__main__': main()