-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotter.py
executable file
·324 lines (293 loc) · 13.8 KB
/
plotter.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import CovidData
import constants
import copy
import numpy as np
import sys
from converters import Path2Name
def plot(rootRegion, paths, dateRange, events, combine,
dependentVariables, independentVariable, xScale, yScale):
"""Plots disease data
arguments:
rootRegion -- the region containing all the data, should be of type region
paths -- a list containing paths to all used subregion
dateRange -- a list of two dateNums representing the inclusive
range of dates to use for data
events -- a dict with dateNum (float) keys which point to a string
describing an event to be marked on the graph
combine -- a string which can be either 'add', 'seperate' or 'stack'
'add' adds the data together for all regions in paths
'seperate' plots each region as its own line
'stack' stacks the graphs on top of eachother, where the value of the
dependent variable is equal to the difference of the line and the
line below it
dependentVariables -- this is a list of keys found in rootRegion to be
used as dependent variables these values will be displayed as seperate
lines, does not work withcombine == 'stack'
xScale -- scale for x axis, can be 'linear' 'log' or 'semilog'
yScale -- scale for x axis, can be 'linear' 'log' or 'semilog'
"""
#setup data
allData = graphData(rootRegion, dependentVariables, independentVariable,
paths, events, dateRange, combine)
#plot
fig, ax = plt.subplots()
for i in allData:
plt.plot(i[constants.INDEPENDENT_KEY()],
i[constants.DEPENDENT_KEY()],
'-',
label = i[constants.LABEL_KEY()])
for j in i[constants.EVENTS_KEY()]:
plt.annotate(xy=[j[0],j[1]], s=j[2], xytext=[-20, 20],
textcoords='offset points', arrowprops=dict(arrowstyle="-"))
ax.set(xlabel=allData.getXLabel(), ylabel=allData.getYLabel(),
title=allData.getTitle())
dateRange = allData.getDateRange()
if independentVariable == constants.DATE_KEY():
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
#plt.gcf().autofmt_xdate()
plt.xticks(np.arange(dateRange[0], dateRange[1]+1,
((dateRange[1]-dateRange[0])/float(constants.XTICKS()-1))),
rotation = constants.XTICK_ROTATION())
#show legend
plt.legend(loc="best")
plt.show()
class graphData():
def __init__(self, init_region, init_dependentKeys, init_independentKey,
init_pathsList, init_eventsList = [], init_dateRange = None,
init_combine = 'seperate'):
#checks
if type(init_region) is not CovidData.region:
raise TypeError("Expected region of type CovidData.region, got " +
str(type(init_region)))
if type(init_dependentKeys) is not list:
raise TypeError("Expected type of list from dependentKeys, got " +
str(type(init_dependentKeys)))
if type(init_independentKey) is not str:
raise TypeError("Expected type of str from independentKey, got " +
str(type(init_independentKey)))
if type(init_pathsList) is not list:
raise TypeError("Expected type of list from pathsList, got " +
str(type(init_pathsList)))
if type(init_eventsList) is not dict:
raise TypeError("Expected type of dict from eventsList, got " +
str(type(init_eventsList)))
if type(init_combine) is not str:
raise TypeError("Expected type of str from combine, got " +
str(type(init_combine)))
if not init_dependentKeys:
raise ValueError("dependentKeys cannot be empty")
if not init_pathsList:
raise ValueError("pathsList cannot be empty")
if type(init_dependentKeys[0]) is not str:
raise TypeError("dependentKeys does not contain str, got " +
str(type(init_dependentKeys[0])))
if type(init_pathsList[0]) is not list:
raise TypeError("pathsList[0] is not list, got " +
str(type(init_pathsList[0])))
if type(init_pathsList[0][0]) is not str:
raise TypeError("pathsList[0][0] is not str, got " +
str(type(init_pathsList[0])))
if init_dateRange is not None:
if type(init_dateRange) is not tuple and\
type(init_dateRange) is not list:
raise TypeError("Expected type of tuple, list, or none" +
"from dateRange, got "
+ str(type(init_dateRange)))
if type(init_dateRange[0]) is not float and\
type(init_dateRange[0]) is not int:
raise TypeError("Expected float or int inside dateRange, got "
+ str(type(init_dateRange[0])))
if len(init_dateRange) > 2:
raise ValueError("dateRange contains too many elements")
if len(init_dateRange) < 2:
raise ValueError("dateRange contains too few elements")
#assigned variables
self._dependentDataKeys = init_dependentKeys
self._independentDataKey = init_independentKey
self._combine = init_combine
self._region = init_region
self._paths = init_pathsList
self._eventsList = init_eventsList
self._dateRange = init_dateRange
#generated variables
self._title = ''
self._xLabel = ''
self._yLabel = ''
self._dataDict = {}
self._minDate = sys.float_info.max
self._maxDate = sys.float_info.min
#space savers
self._depLength = len(self._dependentDataKeys)
self._pathsLength = len(self._paths)
#init process
self.initLabels()
self.initData()
if self._dateRange is None:
self._dateRange = (self._minDate, self._maxDate)
def initLabels(self):
if self._depLength > 1 and self._pathsLength > 1:
self._xLabel = constants.KEY_TO_LABEL(self._independentDataKey)
self._yLabel = 'Number'
self._title =\
'Coronavirus'\
+ ' Over '\
+ constants.KEY_TO_LABEL(self._independentDataKey)
elif self._depLength > 1 and self._pathsLength == 1:
self._xLabel = constants.KEY_TO_LABEL(self._independentDataKey)
self._yLabel = 'Number'
self._title =\
'Coronavirus'\
+ ' Over '\
+ constants.KEY_TO_LABEL(self._independentDataKey)\
+ ' In '\
+ Path2Name(self._paths[0])
elif self._depLength == 1 and self._pathsLength > 1:
self._xLabel = constants.KEY_TO_LABEL(self._independentDataKey)
self._yLabel = constants.KEY_TO_LABEL(self._dependentDataKeys[0])
self._title =\
'Number of '\
+ constants.KEY_TO_LABEL(self._dependentDataKeys[0])\
+ ' Over '\
+ constants.KEY_TO_LABEL(self._independentDataKey)
elif self._depLength == 1 and self._pathsLength == 1:
self._xLabel = constants.KEY_TO_LABEL(self._independentDataKey)
self._yLabel = constants.KEY_TO_LABEL(self._dependentDataKeys[0])
self._title =\
'Number of '\
+ constants.KEY_TO_LABEL(self._dependentDataKeys[0])\
+ ' Over '\
+ constants.KEY_TO_LABEL(self._independentDataKey)\
+ ' In '\
+ Path2Name(self._paths[0])
def initData(self):
for path in self._paths:
curRegion = self._region
#loop through all elements in path
for i in path:
curRegion = curRegion.getSubRegion(i)
if self._dateRange is None:
self._dataDict[Path2Name(path)] = curRegion.getData()
else:
self._dataDict[Path2Name(path)] = CovidData.data()
for i in curRegion.getData():
if i[constants.DATE_KEY()] >= self._dateRange[0] and\
i[constants.DATE_KEY()] <= self._dateRange[1]:
self._dataDict[Path2Name(path)].\
addEntry(i[constants.TOTAL_CASES_KEY()],
i[constants.TOTAL_DEATHS_KEY()],
i[constants.DATE_KEY()])
tempMin = min(curRegion.getData()[constants.DATE_KEY()])
tempMax = max(curRegion.getData()[constants.DATE_KEY()])
if tempMin < self._minDate:
self._minDate = tempMin
if tempMax > self._maxDate:
self._maxDate = tempMax
def __iter__(self):
self._pos = 0
return self
def __next__(self):
try:
returnValue = self[self._pos]
self._pos += 1
except ValueError:
raise StopIteration
except IndexError:
raise StopIteration
else:
return returnValue
def getXLabel(self):
return self._xLabel
def getYLabel(self):
return self._yLabel
def getTitle(self):
return self._title
def getDateRange(self):
return self._dateRange
#TODO: add 'add' for combine
def __getitem__(self, key):
"""Returns a dataDict"""
if self._depLength == 0 or self._pathsLength == 0:
print("Error: size of _paths or _dependentDataKeys is 0")
raise ValueError
if type(key) is tuple:
pathPos = key[0]
depPos = key[1]
elif type(key) is int:
pathPos = key//self._depLength
depPos = key%self._depLength
else:
print("ERROR: key must be int or tuple")
raise TypeError
if pathPos >= self._pathsLength:
raise IndexError
returnDict = {}
curData = self._dataDict[Path2Name(self._paths[pathPos])]
if self._depLength > 1 and self._pathsLength > 1:
returnDict[constants.LABEL_KEY()] = constants.KEY_TO_LABEL\
(self._dependentDataKeys[depPos]) + ' in '\
+ Path2Name(self._paths[pathPos])
elif self._depLength > 1 and self._pathsLength == 1:
returnDict[constants.LABEL_KEY()] =\
constants.KEY_TO_LABEL(self._dependentDataKeys[depPos])
elif self._depLength == 1 and self._pathsLength > 1:
returnDict[constants.LABEL_KEY()] =\
Path2Name(self._paths[pathPos])
elif self._depLength == 1 and self._pathsLength == 1:
returnDict[constants.LABEL_KEY()] = ''
if self._combine == 'add':
pass #TODO:
else:
returnDict[constants.DEPENDENT_KEY()] = curData\
[self._dependentDataKeys[depPos]]
returnDict[constants.INDEPENDENT_KEY()] = curData\
[self._independentDataKey]
returnDict[constants.PATH_KEY()] = self._paths[pathPos]
#add events
returnDict[constants.EVENTS_KEY()] = []
for date in self._eventsList:
for dataKey in self._dataDict:
for depKey in self._dependentDataKeys:
index, exists = curData.findEntry(date)
if not exists:
if index == len(curData.getDates()) or\
index == 0 or\
index < self._dateRange[0] or\
index > self._dateRange[1]:
pass #don't add event if outside ranges
else:
x = ((curData[self._independentDataKey,index] +
curData[self._independentDataKey,index+1])/2)
y = ((curData[depKey,index] +
curData[depKey,index+1])/2)
exists = True
else: #if exist
x = curData[self._independentDataKey,index]
y = curData[depKey,index]
if exists:
returnDict[constants.EVENTS_KEY()].\
append([x,y,self._eventsList[date]])
return returnDict
"""
if len(dependentVariables) > 1:
for dependentVariable in dependentVariables:
dependentData[constants.KEY_TO_LABEL(dependentVariable) +\
' in ' + Path2Name(path)] = regionDataDict[dependentVariable]
independentData[constants.KEY_TO_LABEL(dependentVariable) +\
' in ' + Path2Name(path)] = regionDataDict[independentVariable]
else:
dependentData[constants.KEY_TO_LABEL(Path2Name(path))]\
= regionDataDict[dependentVariables[0]]
independentData[constants.KEY_TO_LABEL(Path2Name(path))]\
= regionDataDict[independentVariable]
elif len(paths) == 1:
for i in paths[0]:
curRegion = curRegion.getSubRegion(i)
regionData = curRegion.getData().getAll()
for i in dependentVariables:
dependentData[constants.KEY_TO_LABEL(i)] = regionData[i]
independentData[constants.KEY_TO_LABEL(i)] = regionData[independentVariable]
"""