This repository has been archived by the owner on Feb 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2chartgeneration.py
287 lines (216 loc) · 6.4 KB
/
ec2chartgeneration.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
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import os
import json
import sys
from colorama import init, Fore
from PIL import Image
import matplotlib.image as mpimg
import random
import math
from docx import Document
import boto3
import botocore
init() #colorama
#Test results will contain tuples of the tests results rom the ingest engine
#_TestResultGroup = []
#Contain all of the organized groups
_TestGroups = []
_decodedJson = dict()
_localDir = ""
_jsonPath = ""
_writePath = ""
_imagePaths = []
BUCKETNAME = "doctordocx-patientqueue"
_count = 0.0
_totalDotsH = 0.0
DEBUG = False
SHOWCHART = False
BOGUSVALS = True
FIRSTPAGE = True
DONE = False
AUTOPATH = False
PAGECOUNT = 1
GENDOCX = True
def main():
global DONE
global _jsonPath
global _writePath
# if len(sys.argv) < 3:
# _jsonPath = sys.argv[1]
# _writePath = sys.argv[2]
print(Fore.GREEN + "Generating Table")
ingestDataFile()
#generateChart()
#saveChart()
print(Fore.BLUE + "Generating Image Page ")
#loadNormalCurve()
#concatImages()
#generateDoc()
def list_bucket_objects(bucket_name):
"""List the objects in an Amazon S3 bucket
:param bucket_name: string
:return: List of bucket objects. If error, return None.
"""
# Retrieve the list of bucket objects
s_3 = boto3.client('s3')
try:
response = s_3.list_objects_v2(Bucket=BUCKETNAME)
except ClientError as e:
# AllAccessDisabled error == bucket not found
logging.error(e)
return None
# Only return the contents if we found some keys
if response['KeyCount'] > 0:
return response['Contents']
return None
#Assume the data file is in the same directory with name dataFile.txt
def ingestDataFile():
print("Intaking Data File")
global DEBUG
global _decodedJson
global _localDir
global AUTOPATH
_localDir = os.getcwd()
s3 = boto3.client('s3')
if DEBUG:
print("Local Directory is: " + _localDir)
objects = list_bucket_objects(bucket_name)
if objects is not None:
# List the object names
s3.download_file(BUCKETNAME, objects[0], "local.json")
else:
return 0
_dataFile = open('local.json', "r")
_rawDataJson = _dataFile.read()
_decodedJson = json.loads(_rawDataJson)
print(_decodedJson.pop("ATTENTION").pop("W4-DSFd"))
_dataFile.close()
def generateChart():
global DEBUG
global _decodedJson
global SHOWCHART
global _count
global BOGUSVALS
global PAGECOUNT
print("Generating Chart")
_categories = []
for _key in _decodedJson.keys():
_categories.append(_key)
if DEBUG:
print("Categories: ")
print(_categories)
_xList = []
_yList = []
_colors = []
for _cat in _categories:
_xList.append(r"$\bf{" + str(_cat.replace(" ", "\ ")) + "}$") #magic, don't touch
_yList.append(0)
_colors.append('black')
# _testList = []
# for _key in _decodedJson[_cat].keys():
# _testList.append(_key)
for index in range(0, len(_decodedJson[_cat])):
_testName = _decodedJson[_cat][index]['RelatedTest']['Name']
_testVal = _decodedJson[_cat][index]['ZScore']
if BOGUSVALS:
_testVal = random.uniform(-5,5)
_xList.append(_testName)
_yList.append(_testVal)
_colors.append(getColor(_testVal))
_xList.append("")
_yList.append(0)
_colors.append('black')
_xList.pop()
_yList.pop()
_colors.pop()
while not len(_xList) % 36 == 0:
_xList.append("")
_yList.append(0)
_colors.append('black')
if DEBUG:
print(_xList)
print(_yList)
#_xList.reverse()
#_yList.reverse()
#_colors.reverse()
PAGECOUNT = math.ceil(len(_xList) / 36.0)
for page in range(1, PAGECOUNT + 1):
_tmpX = _xList[0 + 36 * (page - 1): 36 * page]
_tmpY = _yList[0 + 36 * (page - 1): 36 * page]
_tmpcolors = _colors[0 + 36 * (page - 1): 36 * page]
_tmpX.reverse()
_tmpY.reverse()
_tmpcolors.reverse()
_count = len(_tmpX)
_yScal = np.arange(_count)
plt.barh(_yScal, _tmpY, color = _tmpcolors, align='center', alpha=1)
plt.yticks(_yScal, _tmpX, rotation = 35, fontsize = 10)
plt.xlim([-4, 4])
plt.grid(b=True, axis = 'x')
plt.subplots_adjust(left = 0.25)
#plt.tight_layout(pad = 0.5)
# if SHOWCHART:
# plt.show()
saveChart(page)
def getColor(_val):
#color = 'green'
if _val < 0 and _val >= -1:
color = 'yellow'
elif _val < -1 and _val >= -2:
color = 'orange'
elif _val < -2:
color = 'red'
else:
color = 'green'
return color
def saveChart(pageNumber):
global _localDir
print(Fore.YELLOW + "Saving Graph " + str(pageNumber))
fig = plt.gcf()
fig.set_size_inches(8, 10.5)
fig.savefig(_localDir + "\\graph" + str(pageNumber) + ".png", tranparent=True, dpi = 300, orientation = 'portrait', pad_inches = 0)
plt.clf()
def loadNormalCurve():
print("Loading Curve Image")
img = mpimg.imread(_localDir + "\\NormalCurve.png")
def concatImages():
global PAGECOUNT
global _imagePaths
for _page in range(1, PAGECOUNT + 1):
print(Fore.CYAN + "Generating Visualization Page " + str(_page))
_curve = Image.open(_localDir + "\\NormalCurveResized.png")
_table = Image.open(_localDir + "\\graph" + str(_page) + ".png")
_dest = Image.new('RGB', (_table.width, _table.height + _curve.height), (255, 255, 255))
_dest.paste(_table, (0, _curve.height - 470))
_dest.paste(_curve, (186, 0))
_dest = _dest.crop((0, 0, _dest.width, 300 * 10))
_dest.save(_localDir + "\\renderedVisualization" + str(_page) + ".png")
_imagePaths.append(_localDir + "\\renderedVisualization" + str(_page) + ".png")
# print("PRINTING PAGE: " + str(PAGECOUNT))
# _curve = Image.open(_localDir + "\\NormalCurveResized.png")
# _table = Image.open(_localDir + "\\graph" + str(PAGECOUNT) + ".png")
# _dest = Image.new('RGB', (_table.width, _table.height + _curve.height), (255, 255, 255))
# _dest.paste(_table, (0, _curve.height - 150))
# _dest.paste(_curve, (55, 0))
# _dest = _dest.crop((0, 0, _dest.width, 300 * 10))
# _dest.save(_localDir + "\\finishedVisualization" + str(PAGECOUNT) + ".png")
# _cutoff = _table.height - (3000 - 601 - 150)
# print(_cutoff)
# if _cutoff <= 0:
# DONE = True
# else:
# _table = _table.crop((0, _cutoff, _table.width, _table.height - _cutoff))
# _table.save(_localDir + "\\graph" + str(PAGECOUNT + 1) + ".png")
# FIRSTPAGE = False
# PAGECOUNT = PAGECOUNT + 1
def generateDoc():
document = Document()
para = document.add_paragraph()
run = para.add_run()
for image in _imagePaths:
run.add_picture(image)
document.save(_localDir + "/visualization.docx")
if __name__ == "__main__":
main()