-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffee.py
executable file
·55 lines (47 loc) · 1.3 KB
/
coffee.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
#!/usr/bin/env python3
import csv
import json
coffees = []
with open('brazil-coffee.csv') as csv_file:
headers = csv_file.readline().split(',')
reader = csv.DictReader(csv_file, fieldnames=headers)
for line in reader:
if line['Variety'] == "":
line['Variety'] = 'Standard'
coffees.append(dict(line))
keys = ['Aroma', 'Flavor', 'Aftertaste', 'Acidity', 'Body',
'Balance', 'Uniformity', 'Moisture', 'Sweetness', 'Clean.Cup']
groups = {x: {k: [] for k in keys}
for x in set([x['Variety'] for x in coffees])}
# Get values
for v in coffees:
for k in keys:
groups[v['Variety']][k].append(float(v[k]))
# Tranform into mean
for g, v in groups.items():
groups[g] = {k: sum(v[k])/len(v[k]) for k in keys}
# print(groups)
val_range = {
k: [
max([g[k] for g in groups.values()])*1.01,
min([g[k] for g in groups.values()])*.98
]
for k in keys
}
# Write in format
json.dump(
{
'varieties': list(groups.keys()),
'reviews': [
[
{
'axis': k,
'value': (g[k]-val_range[k][1])/(val_range[k][0]-val_range[k][1])
}
for k in keys
]
for g in groups.values()
]
},
open('coffee.json', 'w')
)