-
Notifications
You must be signed in to change notification settings - Fork 0
/
HouseholdSurveysProcessor.py
284 lines (243 loc) · 13.3 KB
/
HouseholdSurveysProcessor.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
import os
import glob
import re
from openpyxl import load_workbook
import pandas as pd
import numpy
import json
import S3Api
# Constants
STORE_DATA = True
class HouseholdSurveysProcessor:
"""Processes household survey data in csv files"""
def __init__(self, file_storage, s3_api):
""" Create a new instance of the HouseholdSurveysProcessor class
Parameters
----------
:param file_storage: FileStorage, Required
The file storage class used to store raw/processed data
:param s3_api: S3_API, Required
The S3 api wrapper class used to store data in AWS S3
----------
"""
self._survey_data_folder = 'survey_data'
self._input_base_path = f'{file_storage.get_raw_base_path()}/{self._survey_data_folder}/'
# Some filters for the raw xlsx files
self._food_sufficiency_filter = 'food sufficiency'
self._children_information_filter = 'with children'
# The desired breakdown characteristics from each survey
self._characteristics = [{'topic': 'total', 'characteristic': 'total', 'exact_match': True},
{'topic': 'age', 'characteristic': '18 - 24', 'exact_match': True},
{'topic': 'age', 'characteristic': '25 - 39', 'exact_match': True},
{'topic': 'age', 'characteristic': '40 - 54', 'exact_match': True},
{'topic': 'age', 'characteristic': '55 - 64', 'exact_match': True},
{'topic': 'age', 'characteristic': '65 and above', 'exact_match': True},
{'topic': 'gender', 'characteristic': 'male', 'exact_match': True},
{'topic': 'gender', 'characteristic': 'female', 'exact_match': True},
{'topic': 'race', 'characteristic': 'hispanic or latino', 'exact_match': False},
{'topic': 'race', 'characteristic': 'white alone', 'exact_match': False},
{'topic': 'race', 'characteristic': 'black alone', 'exact_match': False},
{'topic': 'race', 'characteristic': 'asian alone', 'exact_match': False},
{'topic': 'race', 'characteristic': 'two or more races', 'exact_match': False}]
# The list of columns for each csv
self._columns = ['State',
'Topic',
'Characteristic',
'Total',
'Enough of the kinds of food wanted',
'Enough food but not always the kinds wanted',
'Sometimes not enough to eat',
'Often not enough to eat',
'Did not report']
# The map of processed data folders
self._processed_survey_data = [
{'input_folder': '/survey_data/standard/all/', 'output_folder': '/consolidated_survey_data/standard/all/',
'normalized_data': True},
{'input_folder': '/survey_data/standard/children/',
'output_folder': '/consolidated_survey_data/standard/children/', 'normalized_data': True},
{'input_folder': '/survey_data/errors/all/', 'output_folder': '/consolidated_survey_data/errors/all/',
'normalized_data': False},
{'input_folder': '/survey_data/errors/children/',
'output_folder': '/consolidated_survey_data/errors/children/', 'normalized_data': False}
]
self._file_storage = file_storage
self._s3_api = s3_api
def process_survey_data(self):
"""Processes each survey file"""
survey_files = self.retrieve_survey_files()
print('Survey Files', survey_files)
for survey_file in survey_files:
wb = load_workbook(survey_file)
print('Reading the workbook and processing into a csv', survey_file)
# If the file contains information on food sufficiency and does not contain data prior to Covid19,
# continue processing the file
if self.__workbook_contains_food_sufficiency(wb) and not self.__workbook_contains_prior_to_covid_19(wb):
print('Contains food sufficiency information', survey_file)
# Survey files sometimes contain information on standard errors
contains_errors = self.__file_contains_standard_errors(survey_file)
print('Does it contain standard errors?', contains_errors)
# Survey files sometimes contain information on families with children versus without
contains_child_info = self.workbook_includes_information_on_children(wb)
print('Does it include information on children?', contains_child_info)
self.__process_workbook(wb, contains_errors=contains_errors, contains_child_info=contains_child_info)
wb.close()
def consolidate_survey_data(self):
"""
Consolidate the survey data by grouped the initially processed survey files by state and
normalizing the population answering the survey questions
"""
survey_metadata = json.load(open('raw_data/survey_data/survey_metadata.json', 'r'))
for survey_data in self._processed_survey_data:
processed_data_folder = self._file_storage.get_processed_base_path() + survey_data['input_folder']
# In some cases, we are not normalizing the data (for standard errors)
normalized_data = survey_data['normalized_data']
processed_files = list(glob.iglob(processed_data_folder + '*.csv'))
consolidated_data = {}
consolidated_normalized_data = {}
print('Processing files in folder', processed_data_folder)
print('Processing files', processed_files)
for file in processed_files:
print('Processing', file)
extracted_week = os.path.basename(file).replace('Week', '').replace('.csv', '').strip()
weekly_data = pd.read_csv(file, index_col=False)
for group in weekly_data.groupby(['State']):
state = group[0]
frame = group[1]
frame['Week'] = extracted_week
frame['Date'] = survey_metadata[extracted_week]
for column in self._columns[3:]:
frame[column] = frame[column].apply(lambda x: 0 if str(x).strip() == '-' else numpy.float32(x))
if normalized_data:
normalized_frame = frame.copy(deep=True)
total_row = frame[frame['Characteristic'] == 'total']
total = numpy.float32(total_row['Total'].values[0])
# Normalize the totals
for column in self._columns[3:]:
normalized_frame[column] = normalized_frame[column].apply(lambda x: x / total)
if state in consolidated_data:
consolidated_data[state] = consolidated_data[state].append(frame)
else:
consolidated_data[state] = frame
if normalized_data and state in consolidated_normalized_data:
consolidated_normalized_data[state] = consolidated_normalized_data[state].append(
normalized_frame)
elif normalized_data:
consolidated_normalized_data[state] = normalized_frame
print('Formatting consolidated data into json')
consolidated_output_path = self._file_storage.get_processed_base_path() + survey_data['output_folder']
self._file_storage.create_directory_if_not_exists(consolidated_output_path)
for key in consolidated_data.keys():
consolidated_data[key].reset_index(drop=True, inplace=True)
output_file = f'{consolidated_output_path}{key}.csv'
print('Formatting and saving data to files for state', key)
print('Output file', output_file)
consolidated_data[key].to_csv(output_file, index=False)
if normalized_data:
consolidated_output_file = f'{consolidated_output_path}{key}-normalized.csv'
consolidated_normalized_data[key].reset_index(drop=True, inplace=True)
consolidated_normalized_data[key].to_csv(consolidated_output_file, index=False)
def retrieve_survey_files(self):
"""Retrieve all raw survey files"""
return list(glob.iglob(self._input_base_path + '**/*.xlsx', recursive=True))
def __workbook_contains_food_sufficiency(self, wb):
"""Determines whether the workbook contains information on food sufficiency
Parameters
----------
:param wb: openpyxl.Workbook, Required
The data structure containing the excel workbook
----------
"""
first_cell = wb['US']['A1'].value
return self._food_sufficiency_filter in str(first_cell).lower()
def __workbook_contains_prior_to_covid_19(self, wb):
"""Determines whether the workbook contains information prior to Covid19
Parameters
----------
:param wb: openpyxl.Workbook, Required
The data structure containing the excel workbook
----------
"""
first_cell = wb['US']['A1'].value
return 'prior to covid-19' in str(first_cell).lower()
def __file_contains_standard_errors(self, file_path):
"""Determines whether the file contains standard errors
Parameters
----------
:param file_path: String, Required
The path of the file
----------
"""
return '_se_' in file_path
def workbook_includes_information_on_children(self, wb):
"""Determines whether the file contains information on children
Parameters
----------
:param wb: openpyxl.Workbook, Required
The data structure containing the excel workbook
----------
"""
first_cell = wb['US']['A1'].value
return self._children_information_filter in str(first_cell).lower()
def __process_workbook(self, wb, contains_errors, contains_child_info):
"""Processes each individual workbook
Parameters
----------
:param wb: openpyxl.Workbook, Required
The data structure containing the excel workbook
:param contains_errors: Boolean, Required
Whether the workbook contains standard errors rather than normal values
:param contains_child_info: Boolean, Required
Whether the workbook contains informatin on families with children
----------
"""
sheet_names = wb.sheetnames
r = re.compile(r'[A-Z]{2}')
filtered_sheet_names = list(filter(r.match, sheet_names))
week_information = wb['US']['A2'].value
week_regex = re.compile(r'Week [0-9]{1,2}')
week = week_regex.search(week_information).group(0)
output = ','.join(self._columns) + '\n'
for sheet_name in filtered_sheet_names:
if sheet_name != 'US':
for c in self._characteristics:
sheet = wb[sheet_name]
characteristic = c['characteristic']
topic = c['topic']
exact_match = c['exact_match']
output += f'{sheet_name},{topic},{characteristic},'
for row in sheet.iter_rows():
if row[0].value is not None:
row_title = row[0].value.lower().strip()
if row_title == characteristic or (
not exact_match and row_title.startswith(characteristic)):
output += ','.join([str(statistic.value) for statistic in row[1:]]) + '\n'
break
output_path = '/'.join([
self._survey_data_folder,
'errors' if contains_errors else 'standard',
'children' if contains_child_info else 'all',
f'{week}.csv'])
print(output_path)
self._file_storage.store_as_processed_file(output_path, output)
def store_survey_data(self):
print('Store processed survey data in S3')
processed_files = list(glob.iglob('processed_data/consolidated_survey_data/**/*.csv', recursive=True))
for file in processed_files:
print('Opening file', file)
contents = open(file, 'rb')
print('Uploading', file, 'to S3')
self._s3_api.upload_bytes(contents, file.replace('processed_data/', ''), S3Api.S3Location.PROCESSED_DATA)
contents.close()
print('Uploaded all files')
if __name__ == '__main__':
from dotenv import load_dotenv
from FileStorage import FileStorage
load_dotenv()
household_surveys_processor_instance = HouseholdSurveysProcessor(FileStorage(), S3Api.S3Api())
# print('Processing survey data')
# household_surveys_processor_instance.process_survey_data()
# print('Consolidating survey data')
# household_surveys_processor_instance.consolidate_survey_data()
if STORE_DATA:
print('Storing processed survey data in S3')
household_surveys_processor_instance.store_survey_data()