-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6sigma_results.py
333 lines (254 loc) · 11.4 KB
/
6sigma_results.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
325
326
327
328
329
330
331
332
333
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 23 16:51:10 2016
@author: pawel.cwiek
"""
# ElementTree is the fastest and uses least memory for parsing xml (even better is cElementTree)
#import xml.etree.ElementTree as ET
#
#cabs_xml = ET.parse('cabs.xml')
#
#cabs = cabs_xml.findall('type')
#for item in cabs.iter('urn:schemas-microsoft-com:office:spreadsheet'):
# print(item)
# http://docs.python-guide.org/en/latest/scenarios/xml/
# ------ using xmltodict because of particular msExcell XML format generated by 6Sigma
#import xmltodict
#
#with open('cabs.xml') as fd:
# cabs = xmltodict.parse(fd.read())
#
#sheet = cabs['Workbook']['Worksheet']['Table']['Row']
# xmltodict does not work well with MS xml
# ------ using xml.sax
from xml.sax import saxutils
from xml.sax import parse
# just to have neat console prints for checks/testing
import pprint
pp = pprint.PrettyPrinter(indent=1,depth=2)
printout = pp.pprint
class ExcelHandler(saxutils.XMLGenerator):
def __init__(self):
saxutils.XMLGenerator.__init__(self)
self.chars=[]
self.cells=[]
self.rows=[]
self.tables=[]
def characters(self, content):
self.chars.append(content)
def startElement(self, name, atts):
if name=="Cell":
self.chars=[]
elif name=="Row":
self.cells=[]
elif name=="Table":
self.rows=[]
def endElement(self, name):
if name=="Cell":
self.cells.append(''.join(self.chars))
elif name=="Row":
self.rows.append(self.cells)
elif name=="Table":
self.tables.append(self.rows)
def parse_6sigma_xml_old(cabs_filepath,servers_filepath,cab_cols_filter,server_cols_filter):
'''
wrapper over xml.sax
returns [cabs,servers] - each is a list of dicts
'''
import functools
cabsHandler=ExcelHandler()
serversHandler=ExcelHandler()
parse(cabs_filepath, cabsHandler)
print()
parse(servers_filepath, serversHandler)
print()
cabs_header = cabsHandler.tables[0].pop(0)
servers_header = serversHandler.tables[0].pop(0)
# clean-up header names parsed from xml
repls = (('\n', ''), (' ', ''))
for no, item in enumerate(cabs_header):
cabs_header[no] = functools.reduce(lambda a, kv: a.replace(*kv), repls,cabs_header[no])
for no, item in enumerate(servers_header):
servers_header[no] = functools.reduce(lambda a, kv: a.replace(*kv), repls,servers_header[no])
# print(cabs_header)
# print(servers_header)
# get column index number of each relevant/needed column (out of all columns available)
for key, value in cab_cols_filter.items():
cab_cols_filter[key] = cabs_header.index(value)+1
# cab_cols_filter[key] = 1+1
for key, value in server_cols_filter.items():
server_cols_filter[key] = servers_header.index(value)
# get only relevant values from all rows available in xml and assign their column names via dict
cabs = extract_values(cabsHandler.tables[0],cab_cols_filter)
servers = extract_values(serversHandler.tables[0],server_cols_filter)
return [cabs,servers]
def parse_6sigma_xml(server_filepath,relevant_server_cols):
'''
wrapper over xml.sax
returns [servers] - list of dicts
'''
import functools
serversHandler=ExcelHandler()
parse(server_filepath, serversHandler)
servers_header = serversHandler.tables[0].pop(0)
# clean-up header names parsed from xml
repls = (('\n', ''), (' ', ''))
for no, item in enumerate(servers_header):
servers_header[no] = functools.reduce(lambda a, kv: a.replace(*kv), repls,servers_header[no])
# get column index number of each relevant/needed column (out of all columns available)
server_cols_filter = relevant_server_cols.copy()
for key, value in server_cols_filter.items():
server_cols_filter[key] = servers_header.index(value)
# get only relevant values from all rows available in xml and assign their column names via dict
servers = extract_values(serversHandler.tables[0],server_cols_filter)
return servers
def extract_values(unfiltered_row_list,filter_dict):
'''
returns list of dicts with key,value pairs only as per filter_dict
i.e. list of cabinet instances. each instance is represented with a dict and only having keys per filter_dict
'''
item_list=[]
for row in unfiltered_row_list:
item = filter_dict.copy()
for parameter,index in item.items():
try:
value = float(row[index])
except:
value = row[index]
item[parameter] = value
item_list.append(item)
return item_list
def calc_report_old(cabs,servers,max_temp):
from collections import OrderedDict
condition = 'over_{}degC'.format(max_temp)
report = OrderedDict([
(condition+'_cabs',None),
('total_cabs',None),
(condition+'_servers',None),
('total_servers',None),
('average_mean_servers_temp_in',None),
('max_mean_servers_temp_in',None),
('percent_servers_overheating',None)
])
# report.update()
report['total_cabs'] = len(cabs)
report[condition+'_cabs'] = len([True for item in cabs if item['mean_temp_in'] > max_temp])
print(sorted([item['mean_temp_in'] for item in cabs if item['mean_temp_in'] > max_temp]))
report['total_servers'] = len(servers)
report[condition+'_servers'] = len([True for item in servers if item['mean_temp_in'] > max_temp])
report['percent_servers_overheating'] = float("{0:.1f}".format(report[condition+'_servers']*100 / report['total_servers']))
temps = [item['mean_temp_in'] for item in servers]
report['average_mean_servers_temp_in'] = float("{0:.1f}".format(sum(temps) / len(servers)))
report['max_mean_servers_temp_in'] = float("{0:.1f}".format(max(temps)))
return report
def calc_report(file_path,servers,max_temp):
from collections import OrderedDict
import os
condition = 'over_{}degC'.format(max_temp)
report = OrderedDict([
('filename',file_path.split(os.sep)[-1]),
(condition+'_cabs',None),
('total_cabs',None),
(condition+'_servers',None),
('total_servers',None),
('average_mean_servers_temp_in',None),
('max_mean_servers_temp_in',None),
('percent_servers_overheating',None)
])
server_temps_by_cabs = dict()
for server in servers:
location = server['location'].split(':')[0]
location = location.replace(' \n','')
try:
server_temps_by_cabs[location] = server_temps_by_cabs[location] + [server['mean_temp_in']] * int(server['u_height'])
except KeyError:
server_temps_by_cabs[location] = [server['mean_temp_in']] * int(server['u_height'])
cabs_mean_temp = {item[0]:sum(item[1])/len(item[1]) for item in server_temps_by_cabs.items()}
cabs_mean_over_max = {key:value for key,value in cabs_mean_temp.items() if float("{0:.2f}".format(value)) > max_temp}
'''
# check of cabinet mean temp calcs
for key,value in server_temps_by_cabs.items():
printout('{}({}): {}'.format(key,len(value),value))
import operator
printout(sorted(cabs_mean_over_max.items(),key=operator.itemgetter(1)))
'''
report['total_cabs'] = len(cabs_mean_temp.keys())
report[condition+'_cabs'] = len(cabs_mean_over_max.keys())
report['total_servers'] = len(servers)
report[condition+'_servers'] = len([True for item in servers if item['mean_temp_in'] > max_temp])
report['percent_servers_overheating'] = float("{0:.1f}".format(report[condition+'_servers']*100 / report['total_servers']))
temps = [item['mean_temp_in'] for item in servers]
report['average_mean_servers_temp_in'] = float("{0:.1f}".format(sum(temps) / len(servers)))
report['max_mean_servers_temp_in'] = float("{0:.1f}".format(max(temps)))
return report
def find_xmls(dir_path):
# returns list of filepaths including only xml files (no subfolders)
import os
files = [fn for fn in next(os.walk(dir_path))[2] if fn.count('.xml')]
return [os.path.join(dir_path,fn) for fn in files]
def reports_to_csv(mydir, rows):
import csv
from time import strftime
import os.path
with open(os.path.join(mydir,strftime('%Y-%m-%d_%H%M%S') + '-bulk_6Sigma_results'+'.csv'), 'w') as csvfile:
mywriter = csv.DictWriter(csvfile, rows[0].keys(), delimiter=',', lineterminator='\n', quoting=csv.QUOTE_MINIMAL, dialect='excel', extrasaction='ignore')
mywriter.writeheader()
for simulation in rows:
mywriter.writerow(simulation)
return
def calc_one_file(dir_path,cab_cols_filter,server_cols_filter,max_temp):
import os
cabs_filename = 'cabs.xml'
servers_filename = 'servers.xml'
cabs,servers = parse_6sigma_xml_old(os.path.join(dir_path,cabs_filename),os.path.join(dir_path,servers_filename),cab_cols_filter,server_cols_filter)
report = calc_report_old(cabs,servers,max_temp)
return [report]
def calc_bulk_files(dir_path,server_cols_filter,max_temp):
file_paths_list = find_xmls(dir_path)
reports = []
for file_path in file_paths_list:
servers = parse_6sigma_xml(file_path,server_cols_filter)
reports.append(calc_report(file_path,servers,max_temp))
return reports
def gui(results):
import tkinter as tk
import tkTreeWitget
headers = [key for key,value in results[0].items()]
values_list = []
for item in results:
values = [value for key,value in item.items()]
values_list.append(tuple(values))
root = tk.Tk()
root.wm_title("6Sigma results summary")
mc_listbox = tkTreeWitget.McListBox(headers,values_list)
mc_listbox.intro_string = 'costam costam costam costam costam \n costam costam costam costam '
mc_listbox.build()
return root.mainloop()
if __name__ == '__main__':
import os
# values in filter dict can not have spaces or '\n' newlines. eg. 'CamelCaseNoSpaces%'
cab_cols_filter = {'kWe_installed':'Cabinet Power kW',
'mean_temp_in':'Mean Temperature In C'
}
server_cols_filter = {'location':'Location ID',
'u_height':'Height U',
'name_plate_power':'Name Plate Power kW',
'heat_power_ratio':'Heat Power Factor %',
'mean_temp_in':'Mean Temperature In C'
}
max_temp = 27.0
reports = []
dir_path = os.getcwd()
# dir_path = 'C:/Python34/WinPython-32bit-3.4.3.6/_my projects/6Sigma results/_old'
# reports.extend(calc_one_file(dir_path,cab_cols_filter,server_cols_filter,max_temp))
dir_path = 'C:/Python34/WinPython-32bit-3.4.3.6/_my projects/6Sigma results'
reports.extend(calc_bulk_files(dir_path,server_cols_filter,max_temp))
gui(reports)
# reports_to_csv(dir_path,reports)
#dir_path = 'C:/install'
#printout(cabs)
#printout(servers)
#a = calc_report(cabs,servers,27.0)
#b = calc_report(cabs,servers,26.0)
#reports_to_csv(dir_path,[a,b])
#printout(summarise(cabs,servers,27.0))