-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
154 lines (116 loc) · 4.98 KB
/
api.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
import csv
from survey import Survey
import collections
import json
CSV_PATH = './Code-for-Good-EM2030_data.csv'
def complete_indicator_line(table):
last_indicator = ''
for index, indicator in enumerate(table[0]):
if indicator == '':
table[0][index] = last_indicator
else:
last_indicator = indicator
def read_data(file):
table = []
with open(file, 'r') as f:
reader = csv.reader(f)
for row in reader:
table.append(row)
return table
def extract_surveys(table):
surveys = []
for row_id, row in enumerate(table):
# Ignore header
if (row_id < 4):
continue
# Ignore empty rows
if len(row) is 0:
continue
country = row[0]
survey = Survey(table[0], table[3])
# Ignore blank lines
if country is '':
continue
for index, value in enumerate(row):
crt_indicator = table[0][index]
crt_category = table[3][index]
if index < 2:
survey.indicators[crt_indicator][crt_category] = value
continue
elif value is not '':
survey.indicators[crt_indicator][crt_category] += float(value)
surveys.append(survey)
return surveys
def parse_data(file):
table = read_data(file)
complete_indicator_line(table)
surveys = extract_surveys(table)
return surveys
def horizontal_chart(countries, surveys_by_indicator):
# dataset = [
# {label: "Men", "Not Satisfied": 20, "Not Much Satisfied": 10, "Satisfied": 50, "Very Satisfied": 20},
# {label: "Women", "Not Satisfied": 15, "Not Much Satisfied": 30, "Satisfied": 40, "Very Satisfied": 15}
# ];
answer = []
print(surveys_by_indicator)
for indicator in surveys_by_indicator.keys():
crt_pairs = {}
crt_pairs['label'] = indicator
# print(surveys_by_country[c].indicators)
for c in surveys_by_indicator[indicator]:
# print(c)
if 'Total' in surveys_by_indicator[indicator][c]:
print('yes')
# print(surveys_by_indicator[indicator][c].indicators[indicator])
crt_pairs[c] = surveys_by_indicator[indicator][c]['Total']
# print('adsdasdasdasdasd')
# print(crt_pairs)
ordered_dict = collections.OrderedDict(sorted(crt_pairs.items()))
answer.append(ordered_dict)
return json.dumps(answer)
def hisogram(countries, indicator, surveys_by_country):
answer = []
pairs_by_country = {}
for country in surveys_by_country.keys():
pairs_by_country[country] = surveys_by_country[country].indicators[indicator]
print('asdasdasdas')
print(pairs_by_country)
for category in pairs_by_country[list(pairs_by_country.keys())[0]]:
crt_bars = {}
crt_bars["categorie"] = category
values = []
for country in pairs_by_country.keys():
print(pairs_by_country[country][category])
values.append({'value': pairs_by_country[country][category], 'rate': country})
crt_bars["values"] = values
answer.append(crt_bars)
# print(c)
# print(surveys_by_country[c].indicators[indicator])
# for category in surveys_by_country[c].indicators[indicator]:
return json.dumps(answer)
if __name__ == "__main__":
surveys = parse_data(CSV_PATH)
surveys_by_country = {}
for s in surveys:
if s.indicators['Indicator']['Country'].startswith('Colombia') and s.indicators['Indicator']['Survey'].startswith('2015 DHS'):
surveys_by_country['Colombia'] = s
if s.indicators['Indicator']['Country'].startswith('India') and s.indicators['Indicator']['Survey'].startswith('2005-06 DHS'):
surveys_by_country['India'] = s
if s.indicators['Indicator']['Country'].startswith('Indonesia') and s.indicators['Indicator']['Survey'].startswith('2012 DHS'):
surveys_by_country['Indonesia'] = s
if s.indicators['Indicator']['Country'].startswith('Kenya') and s.indicators['Indicator']['Survey'].startswith('2015 MIS'):
surveys_by_country['Kenya'] = s
if s.indicators['Indicator']['Country'].startswith('Senegal') and s.indicators['Indicator']['Survey'].startswith('2016 DHS'):
surveys_by_country['Senegal'] = s
print('sbc')
print(surveys_by_country)
surveys_by_indicator = {}
for c in surveys_by_country.keys():
for i in surveys_by_country[c].indicators:
if i not in surveys_by_indicator:
surveys_by_indicator[i] = {}
surveys_by_indicator[i][c] = surveys_by_country[c].indicators[i]
print(len(surveys_by_indicator.keys()))
# print(horizontal_chart(['Indonesia', 'India'], surveys_by_indicator))
# print(hisogram(['Indonesia', 'India'], 'Wife beating justified for at least one specific reason [Women]', surveys_by_country))
print(horizontal_chart(['Indonesia', 'India'], surveys_by_indicator))