-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_pipeline.py
507 lines (401 loc) · 16.6 KB
/
data_pipeline.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import ast
import csv
import numpy
import pandas
import pycountry
import luigi
import luigi.contrib.target
import luigi.contrib.postgres
from mlxtend.frequent_patterns import apriori
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import association_rules
class Config(luigi.Config):
"""
Luigi configuration file containing information such as PostgreSQL connection details, database table names etc
"""
date = luigi.DateParameter()
host = 'localhost'
database = 'sales_dw'
user = 'abhishekzambre'
password = 'sky'
customer_info_table = 'customer_info'
invoice_table = 'invoice'
invoice_time_table = 'invoice_time'
product_info_table = 'product_info'
association_rules_table = 'association_rules'
outliers_table = 'invoice_outliers'
column_separator = "\t"
class DataDump(luigi.ExternalTask):
"""
This is an external data dump task.
This task is the top of the dependency graph and will only be successful if the data dump is available.
"""
date = luigi.DateParameter()
def output(self):
"""
Returns the target output for this task.
In this case, it expects a csv file to be present in data directory
:return: list of target output containing csv files.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return [luigi.LocalTarget(self.date.strftime("data/%Y_%m_%d" + "/" + Config.customer_info_table + ".csv")),
luigi.LocalTarget(self.date.strftime("data/%Y_%m_%d" + "/" + Config.invoice_table + ".csv")),
luigi.LocalTarget(self.date.strftime("data/%Y_%m_%d" + "/" + Config.product_info_table + ".csv"))]
class CustomerInfoPreProcessing(luigi.Task):
"""
This task perform data cleansing and transformations on the customer_info table's csv file.
It will also add column named called Country_Code, which will be used in data insights dashboard.
"""
date = luigi.DateParameter()
def requires(self):
"""
This task depends on the DataDump task.
"""
return DataDump(self.date)
def run(self):
"""
This function picks up the csv containing customer information, and perform data cleansing and transformations.
It will also add Country_Code column. Output will be written in intermediate csv file.
"""
data = pandas.read_csv(self.input()[0].path)
data.country.fillna("Not Available", inplace=True)
data.country = data.country.str.strip().str.title()
countries = dict()
for country in pycountry.countries:
countries[country.name] = country.alpha_2
data['Country_Code'] = data.country.map(countries)
data.rename(columns={"customerid": "Customer_ID", "country": "Country_Name"}, inplace=True)
data.to_csv(self.input()[0].path + "_processed", encoding="utf-8", header=False, index=None)
def output(self):
"""
Returns the csv file as target to be loaded into database.
:return: target output containing csv files.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return luigi.LocalTarget(self.input()[0].path + "_processed")
class InvoicePreProcessing(luigi.Task):
"""
This task perform data cleansing and transformations on the invoice table's csv file.
"""
date = luigi.DateParameter()
def requires(self):
"""
This task depends on the DataDump task.
"""
return DataDump(self.date)
def run(self):
"""
This function picks up the csv containing invoice information, and perform data cleansing and transformations.
"""
data = pandas.read_csv(self.input()[1].path)
data.invoicedate = pandas.to_datetime(data.invoicedate)
data.rename(columns={"invoiceno": "Invoice_No", "stockcode": "Stock_Code",
"invoicedate": "Invoice_Date", "customerid": "Customer_Id"}, inplace=True)
data.to_csv(self.input()[1].path + "_processed", encoding="utf-8", header=False, index=None)
def output(self):
"""
Returns the csv file as target to be loaded into database.
:return: target output containing csv files.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return luigi.LocalTarget(self.input()[1].path + "_processed")
class InvoiceTimeGeneration(luigi.Task):
"""
This task will divide invoice_date column from invoice table's csv file, into separate columns such as Day,
Week, Quarter, Year etc.
This will be helpful in performing data analytics.
"""
date = luigi.DateParameter()
def requires(self):
"""
This task depends on the DataDump task.
"""
return DataDump(self.date)
def run(self):
"""
This function picks up invoice_date column from invoice csv file.
Date will be further broken down in to columns such as Day, Week, Quarter, Year etc., and will be written in
separate csv file.
"""
data = pandas.read_csv(self.input()[1].path)
data.invoicedate = pandas.to_datetime(data.invoicedate)
data_splitted = pandas.DataFrame({
"Invoice_Date": data.invoicedate,
"DayOfWeek": data.invoicedate.dt.dayofweek,
"Year": data.invoicedate.dt.year,
"Month": data.invoicedate.dt.month,
"Day": data.invoicedate.dt.day,
"Hour": data.invoicedate.dt.hour,
"Minute": data.invoicedate.dt.minute,
"DayOfYear": data.invoicedate.dt.dayofyear,
"Week": data.invoicedate.dt.week,
"Quarter": data.invoicedate.dt.quarter
})
data_splitted.to_csv(self.input()[1].path + "_time", sep="\t", encoding="utf-8", header=False, index=None)
def output(self):
"""
Returns the csv file as target to be loaded into database.
:return: target output containing csv files.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return luigi.LocalTarget(self.input()[1].path + "_time")
class ProductInfoPreProcessing(luigi.Task):
"""
This task perform data cleansing and transformations on the product_info table's csv file.
"""
date = luigi.DateParameter()
def requires(self):
"""
This task depends on the DataDump task.
"""
return DataDump(self.date)
def run(self):
"""
This function will pick up csv file and perform data cleansing and transformations.
It will remove extra whitespaces from description column. Finally, will load data into intermediate csv file.
"""
data = pandas.read_csv(self.input()[2].path)
data.description.replace('\s+', ' ', regex=True, inplace=True)
data.description = data.description.str.strip()
data.rename(columns={"stockcode": "Stock_Code", "unitprice": "Unit_Price"}, inplace=True)
data.to_csv(self.input()[2].path + "_processed", sep="\t", encoding="utf-8", header=False, index=None)
def output(self):
"""
Returns the csv file as target to be loaded into database.
:return: target output containing csv files.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return luigi.LocalTarget(self.input()[2].path + "_processed")
class CustomerInfoLoading(luigi.contrib.postgres.CopyToTable):
"""
This task will load csv file into Postgres table.
"""
date = luigi.DateParameter()
host = Config.host
database = Config.database
user = Config.user
password = Config.password
table = Config.customer_info_table
column_separator = ","
columns = [("Customer_ID", "INT"),
("Country", "TEXT"),
("Country_Code", "TEXT")]
def requires(self):
"""
This task depends on customer_info csv cleaning task
"""
return CustomerInfoPreProcessing(self.date)
class InvoiceLoading(luigi.contrib.postgres.CopyToTable):
"""
This task will load csv file into Postgres table.
"""
date = luigi.DateParameter()
host = Config.host
database = Config.database
user = Config.user
password = Config.password
table = Config.invoice_table
column_separator = ","
columns = [("Invoice_No", "TEXT"),
("Stock_Code", "TEXT"),
("Quantity", "INT"),
("Invoice_Date", "TEXT"),
("Customer_Id", "INT")]
def requires(self):
"""
This task depends on invoice csv cleaning task
"""
return InvoicePreProcessing(self.date)
class InvoiceTimeLoading(luigi.contrib.postgres.CopyToTable):
"""
This task will load csv file into Postgres table.
"""
date = luigi.DateParameter()
host = Config.host
database = Config.database
user = Config.user
password = Config.password
table = Config.invoice_time_table
column_separator = "\t"
columns = [("Day", "INT"),
("DayOfWeek", "INT"),
("DayOfYear", "INT"),
("Hour", "INT"),
("Invoice_Date", "TEXT"),
("Minute", "INT"),
("Month", "TEXT"),
("Quarter", "INT"),
("WeekOfYear", "INT"),
("Year", "INT")]
def requires(self):
"""
This task depends on invoice_time csv generation task
"""
return InvoiceTimeGeneration(self.date)
class ProductInfoLoading(luigi.contrib.postgres.CopyToTable):
"""
This task will load csv file into Postgres table.
"""
date = luigi.DateParameter()
host = Config.host
database = Config.database
user = Config.user
password = Config.password
table = Config.product_info_table
column_separator = "\t"
columns = [("Stock_Code", "TEXT"),
("Description", "TEXT"),
("Unit_Price", "FLOAT")]
def requires(self):
"""
This task depends on product_info csv cleaning task
"""
return ProductInfoPreProcessing(self.date)
class AssociationRulesGeneration(luigi.Task):
"""
This task will generate association rules i.e. customers who have bought this, also bought this.
This will help business owner to launch bundle offers.
Further extension can be made to check what items are being returned frequently.
"""
date = luigi.DateParameter()
def requires(self):
"""
This task will execute once all previous csv files are loaded into the database.
"""
return [CustomerInfoLoading(self.date),
InvoiceLoading(self.date),
ProductInfoLoading(self.date),
InvoiceTimeLoading(self.date)]
def run(self):
"""
Apriori algorithm will be used in order to generate association rules.
"""
data = pandas.read_csv(self.date.strftime("data/%Y_%m_%d" + "/" + Config.invoice_table + ".csv"))
grouped = data[['customerid', 'stockcode']].groupby('customerid')
aggregated_data = grouped.aggregate(lambda x: list(x))
aggregated_data.to_csv(self.date.strftime("data/%Y_%m_%d" + "/" + Config.invoice_table + ".csv_aggregated"),
encoding="utf-8", header=False, index=None)
temp = list()
with open(self.date.strftime("data/%Y_%m_%d" + "/" + Config.invoice_table + ".csv_aggregated"), 'r') as f:
for row in csv.reader(f):
eval_temp = ast.literal_eval(''.join(row))
if len(eval_temp) == 1:
continue
temp.append(eval_temp)
te = TransactionEncoder()
te_ary = te.fit(temp).transform(temp)
df = pandas.DataFrame(te_ary, columns=te.columns_)
frequent_itemsets = apriori(df, min_support=0.05, use_colnames=True)
association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7)
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.2)
final_rules = pandas.DataFrame([rules['antecedants'].str.join(''),
rules['consequents'].str.join(''),
rules['antecedent support'],
rules['consequent support'],
rules['support'],
rules['confidence'],
rules['lift'],
rules['leverage'],
rules['conviction']]).T
final_rules.to_csv(self.date.strftime("data/%Y_%m_%d" + "/" + Config.invoice_table + ".csv_association_rules"),
encoding="utf-8", header=False, index=None)
def output(self):
"""
Returns the csv file as target to be loaded into database.
:return: target output containing csv files.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return luigi.LocalTarget(
self.date.strftime("data/%Y_%m_%d" + "/" + Config.invoice_table + ".csv_association_rules"))
class OutliersDetection(luigi.Task):
"""
This task will pick up invoice table csv and perform Tukey's IQR outlier detection algorithm on the data.
"""
date = luigi.DateParameter()
def requires(self):
"""
This task will execute once all previous csv files are loaded into the database.
"""
return [CustomerInfoLoading(self.date),
InvoiceLoading(self.date),
ProductInfoLoading(self.date),
InvoiceTimeLoading(self.date)]
def run(self):
"""
Tukey's IQR will be used to detect outliers/anomalies.
We have only performed this on quantity column as a demonstration purpose.
More fields can be added to widen the search for anomalies.
"""
data = pandas.read_csv(self.date.strftime("data/%Y_%m_%d" + "/" + Config.invoice_table + ".csv"))
quantity = data['quantity']
q75, q25 = numpy.percentile(quantity, [75, 25])
iqr = q75 - q25
upper_fence = q75 + (30.0 * iqr)
outliers = data[data.quantity > upper_fence]
outliers.to_csv(self.date.strftime("data/%Y_%m_%d" + "/" + Config.invoice_table + ".csv_outliers"),
encoding="utf-8", header=False, index=None)
def output(self):
"""
Returns the csv file as target to be loaded into database.
:return: target output containing csv files.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return luigi.LocalTarget(self.date.strftime("data/%Y_%m_%d" + "/" + Config.invoice_table + ".csv_outliers"))
class AssociationRulesLoading(luigi.contrib.postgres.CopyToTable):
"""
This task will load generated association rules into database.
"""
date = luigi.DateParameter()
host = Config.host
database = Config.database
user = Config.user
password = Config.password
table = Config.association_rules_table
column_separator = ","
columns = [("Antecedants", "TEXT"),
("Consequents", "TEXT"),
("Antecedent_Support", "FLOAT"),
("Consequent_Support", "FLOAT"),
("Support", "FLOAT"),
("Confidence", "FLOAT"),
("Lift", "FLOAT"),
("Leverage", "FLOAT"),
("Conviction", "FLOAT")]
def requires(self):
"""
This task depends on association rules generation.
"""
return AssociationRulesGeneration(self.date)
class OutliersLoading(luigi.contrib.postgres.CopyToTable):
"""
This task will load detected outliers into database.
"""
date = luigi.DateParameter()
host = Config.host
database = Config.database
user = Config.user
password = Config.password
table = Config.outliers_table
column_separator = ","
columns = [("Invoice_No", "TEXT"),
("Stock_Code", "TEXT"),
("Quantity", "INT"),
("Invoice_Date", "TEXT"),
("Customer_Id", "INT")]
def requires(self):
"""
This task depends on outliers detection task.
"""
return OutliersDetection(self.date)
class CompleteDataDumpLoad(luigi.Task):
"""
This is the final task in the data pipeline
"""
date = luigi.DateParameter()
def requires(self):
"""
This task will execute only if association rules and outliers are loaded into database.
:rtype: object (:py:class:`luigi.target.Target`)
"""
return [AssociationRulesLoading(self.date),
OutliersLoading(self.date)]