-
Notifications
You must be signed in to change notification settings - Fork 1
/
emission_by_fuel.py
117 lines (103 loc) · 4.08 KB
/
emission_by_fuel.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
'''
Created on 25 de mar de 2017
@author: rubemkalebe
'''
import data_helper
from bokeh.charts import Bar
from bokeh.charts.operations import blend
from bokeh.charts.attributes import cat
from bokeh.models.formatters import NumeralTickFormatter
class EmissionByFuelPlot(object):
'''
classdocs
'''
def __init__(self, data):
'''
Constructor receiving the data
'''
self.__data = data
self.__fuel = {}
self.__table = {}
self.__samples = {}
self.__title = ''
'''
Process data to generate relevant information
Works on all countries
'''
def process(self):
# Reset
self.__samples.clear()
self.__fuel.clear()
# Process
for emission, regs, ftype in zip(self.__data.data[data_helper.data_field['emission']],\
self.__data.data[data_helper.data_field['registrations']],\
self.__data.data[data_helper.data_field['fuel_type']]):
if ftype in self.__fuel and regs != '' and emission != '':
self.__fuel[ftype] += (int(regs) * float(emission))
self.__samples[ftype] += int(regs)
elif ftype != '' and ftype not in self.__fuel and regs != '' and emission != '':
self.__fuel[ftype] = (int(regs) * float(emission))
self.__samples[ftype] = int(regs)
# Update
self.updateTable()
self.__title = 'European Union'
'''
Process data by country to generate relevant information
'''
def processByCountry(self, country):
# Reset
self.__samples.clear()
self.__fuel.clear()
# Process
for emission, regs, ftype, ct in zip(self.__data.data[data_helper.data_field['emission']],\
self.__data.data[data_helper.data_field['registrations']],\
self.__data.data[data_helper.data_field['fuel_type']],\
self.__data.data[data_helper.data_field['country']]):
if ftype in self.__fuel and regs != '' and emission != '' and ct == country:
self.__fuel[ftype] += (int(regs) * float(emission))
self.__samples[ftype] += int(regs)
elif ftype != '' and ftype not in self.__fuel and regs != '' and\
emission != '' and ct == country:
self.__fuel[ftype] = (int(regs) * float(emission))
self.__samples[ftype] = int(regs)
# Update
self.updateTable()
self.__title = data_helper.country[country]
'''
Update table
'''
def updateTable(self):
self.__table = {
'fuel' : sorted(self.__fuel.keys()),
'total' : [self.__fuel[x] for x in sorted(self.__fuel.keys())],
'average' : [(self.__fuel[x] / self.__samples[x]) for x in sorted(self.__fuel.keys())]
}
'''
Generate chart
'''
def plotAverage(self):
bar = Bar(\
self.__table,
values = blend('average', name = 'average', labels_name = 'average'),\
label = cat(columns = 'fuel'),\
stack = cat(columns = 'average'),\
agg = 'mean',\
title = 'Average of CO2 emissions (by fuel type) in ' + self.__title,\
legend = False,\
tooltips = [('Average', '@average{1.11}' + ' g/km')])
return bar
'''
Generate chart
'''
def plotTotal(self):
bar = Bar(\
self.__table,
values = blend('total', name = 'total', labels_name = 'total'),\
label = cat(columns = 'fuel'),\
stack = cat(columns = 'total'),\
agg = 'mean',\
title = 'Total of CO2 emissions (by fuel type) in ' + self.__title,\
legend = False,\
tooltips = [('Total', '@total{1,1}' + ' g/km')])
bar._yaxis.formatter = NumeralTickFormatter(format = '0,0' + ' g/km')
return bar