-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.py
166 lines (131 loc) · 4.45 KB
/
init_db.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
import pandas as pd
from app import models, db
def get_or_create(session, model, **kwargs):
instance = session.query(model).filter_by(**kwargs).first()
if instance:
return False, instance
else:
instance = model(**kwargs)
try:
session.add(instance)
session.commit()
return True, instance
except Exception as e:
session.rollback()
raise e
def read_recipe_sheet(fname, sheet_name):
with pd.ExcelFile(fname) as xls:
print('reading sheet "{}" from "{}"'.format(sheet_name, fname))
data = pd.read_excel(xls, sheet_name, index_col=None, header=None,
usecols=[0, 1, 2])
data.columns = ["id", "recipe_name", 'value']
return data
def read_data():
fnames = ['./GA Tech Data Final 2.xlsx', './GA Tech Data Final.xlsx']
sheets = ['Ingredients', 'Occasion', 'Ethnicity', 'Recipe Type', 'Diet',
'Course']
dat = {
sheet.lower(): pd.concat(
[read_recipe_sheet(fname, sheet) for fname in fnames],
axis=0
)
for sheet in sheets
}
return dat
def clean_data(data):
data['ingredients'].loc[data['ingredients'].value.isnull(), "value"] = ','.join(["cheddar cheese","smoked cheddar cheese","croissants","cream","Vegemite"])
data['recipe type'].loc[data['recipe type'].value.isnull(), 'value'] = ["Sandwiches & burgers"]
data['course'].loc[data['course'].value.isnull(), "value"] = ["Breakfast / brunch"]
data['diet'].loc[data['diet'].value.isnull(), 'value'] = 'Vegetarian'
data['ethnicity'].loc[data['ethnicity'].value.isnull(), 'value'] = ['French']
data['diet'].drop("recipe_name", axis=1, inplace=True)
data['recipe type'].drop("recipe_name", axis=1, inplace=True)
data['occasion'].drop("recipe_name", axis=1, inplace=True)
data['course'].drop("recipe_name", axis=1, inplace=True)
data['ethnicity'].drop("recipe_name", axis=1, inplace=True)
data['ingredients'].drop("recipe_name", axis=1, inplace=True)
return data
def extract_recipes(data):
recipe_index = data['ingredients'].loc[:,["id","recipe_name"]]
recipe_index.drop_duplicates(subset='id', inplace=True)
recipe_index.reset_index(inplace=True)
recipe_index.drop(['index'], axis=1, inplace=True)
return recipe_index
def populate_mapping(df, model, association_table):
uniques = pd.DataFrame(df.value.unique(), columns=['value'])
uniques['id'] = uniques.index
objs = [
model(id=row.id, name=row.value)
for idx, row in uniques.iterrows()
]
try:
db.session.bulk_save_objects(objs)
db.session.commit()
except Exception:
db.session.rollback()
pass
df.columns = ['recipe_id', 'value']
recipe_map = df.merge(
uniques,
left_on='value',
right_on='value'
)[['recipe_id', 'id']]
recipe_map.columns = association_table.columns.keys()
print('writing recipe map')
recipe_map.to_sql(
association_table.name,
db.session.connection(),
index=False,
if_exists='replace'
)
db.session.commit()
def populate_database():
data = read_data()
recipes = extract_recipes(data)
data = clean_data(data)
db.create_all()
print('adding recipes to database')
recs = [
models.Recipe(id=row.id, name=row.recipe_name)
for id, row in recipes.iterrows()
]
db.session.bulk_save_objects(recs)
db.session.commit()
print('adding ingredients to database')
populate_mapping(
data['ingredients'],
models.Ingredient,
models.ingredient_association
)
print('adding recipe type to database')
populate_mapping(
data['recipe type'],
models.RecipeType,
models.type_association
)
print('adding courses to database')
populate_mapping(
data['course'],
models.Course,
models.course_association
)
print('adding diets to database')
populate_mapping(
data['diet'],
models.Diet,
models.diet_association
)
print('adding occasions to database')
populate_mapping(
data['occasion'],
models.Occasion,
models.occasion_association
)
print('adding ethnicities to database')
populate_mapping(
data['ethnicity'],
models.Ethnicity,
models.ethnicity_association
)
if __name__ == "__main__":
populate_database()