-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchurn_script_logging_and_tests.py
152 lines (133 loc) · 4.42 KB
/
churn_script_logging_and_tests.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
'''
Contains the unit-tests and logging for the
churn_library.py file
Author: Gunnvant Singh Saini
Date: 5/10/22
'''
import os
import logging
import churn_library as cls
logging.basicConfig(
filename='./logs/churn_library.log',
level=logging.INFO,
filemode='w',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def test_data_exists():
'''
test existence of data file.
'''
try:
assert os.path.exists(cls.constants.PATH_DATA)
logging.info('File exists:SUCCESS')
except FileExistsError:
logging.error('File exists:FAILED')
def test_import():
'''
test data import - this example is completed for you to assist with the other test functions
'''
df = cls.import_data(cls.constants.PATH_DATA)
try:
assert df.shape[0] > 0
assert df.shape[1] > 0
logging.info('File read : SUCCESS')
except AssertionError as err:
logging.error("File read : FAILED")
raise err
def test_eda():
'''
test perform eda function
'''
df = cls.import_data(cls.constants.PATH_DATA)
cls.create_dependent_var(df)
try:
cls.perform_eda(df)
logging.info('EDA function ran: SUCCESS')
except AssertionError:
logging.error('EDA function: FAILED')
eda_files = ['churn_hist.png', 'cust_age_hist.png',
'heatmap.png', 'marital_status.png',
'total_trans.png']
for file in eda_files:
path_file = os.path.join(cls.constants.PATH_IMG_EDA, file)
try:
assert os.path.exists(path_file)
logging.info(f'File {file} exists : SUCCESS')
except AssertionError:
logging.error(f'File {file} exists : FAILED')
def test_encoder_helper():
'''
test encoder helper
'''
df = cls.import_data(cls.constants.PATH_DATA)
cls.create_dependent_var(df)
cat_list = cls.constants.CATEGORY_LIST
cls.encoder_helper(df, cat_list)
for cat_col in cat_list:
col_name = cat_col + "_Churn"
try:
assert col_name in df.columns
logging.info(f"Column {cat_col} encoded : SUCCESS")
except AssertionError:
logging.error(f"Column {cat_col} encoded : FAILED")
def test_perform_feature_engineering():
'''
test perform_feature_engineering
'''
df = cls.import_data(cls.constants.PATH_DATA)
cls.create_dependent_var(df)
cat_list = cls.constants.CATEGORY_LIST
cls.encoder_helper(df, cat_list)
try:
X_train, x_test, y_train, y_test = cls.perform_feature_engineering(df)
logging.info('Feature engineering : SUCCESS')
except AssertionError:
logging.info('Feature engineering : FAILED')
try:
assert X_train.shape[0] > 0
assert x_test.shape[0] > 0
assert y_train.shape[0] > 0
assert y_test.shape[0] > 0
logging.info('X,y train/test have data : SUCCESS')
except AssertionError:
logging.info('X,y train/test are empty : FAILED')
def test_train_models():
'''
test train_models
'''
df = cls.import_data(cls.constants.PATH_DATA)
cls.create_dependent_var(df)
cat_list = cls.constants.CATEGORY_LIST
cls.encoder_helper(df, cat_list)
X_train, x_test, y_train, y_test = cls.perform_feature_engineering(df)
try:
cls.train_models(X_train, x_test, y_train, y_test, df)
logging.info('Models trained : SUCCESS')
except AssertionError:
logging.error('Models trained : FAILED')
res_plots = [
'feature_imp.png',
'logistic regression_featimp.png',
'random forest_featimp.png',
'roc_plot.png']
for plot in res_plots:
path_plot = os.path.join(cls.constants.PATH_IMG_RES, plot)
try:
assert os.path.exists(path_plot)
logging.info(f'Plot {plot} exists : SUCCESS')
except AssertionError:
logging.error(f'Plot {plot} exists : FAILED')
model_files = ['logistic_model.pkl', 'rfc_model.pkl']
for model in model_files:
path_model = os.path.join(cls.constants.PATH_MODELS, model)
try:
assert os.path.exists(path_model)
logging.info(f'Model {model} exists : SUCCESS')
except AssertionError:
logging.error(f'Model {model} exists : FAILED')
if __name__ == '__main__':
test_data_exists()
test_import()
test_eda()
test_encoder_helper()
test_perform_feature_engineering()
test_train_models()