-
Notifications
You must be signed in to change notification settings - Fork 2
/
assert_commit_retrain.py
163 lines (120 loc) · 5.02 KB
/
assert_commit_retrain.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
# Standard
import os
import json
import pickle
# Third-party
from google.cloud import bigquery
import pandas as pd
import great_expectations as ge
import wandb
from sklearn.model_selection import train_test_split
import joblib
# Local application
from definitions import TEMP_DIR
from . import lakefs_connector
from ..models import train_final
def script_path(filename):
"""
Function to find the file in the current directory even if it is called from another directory.
"""
filepath = os.path.join(os.path.dirname(__file__))
return os.path.join(filepath, filename)
def get_data_from_production(bq_client):
QUERY = (
'SELECT *'
'FROM `sunny-emissary-293912.fakenewsdeploy.model_predictions`'
'WHERE ground_truth in ("Real","Fake") ')
df = bq_client.query(QUERY).to_dataframe()
return df
def assert_data_quality(df):
assert df.expect_column_values_to_be_in_set("ground_truth",["Fake","Real"])["success"]
assert df.expect_column_values_to_be_unique("url")["success"]
assert df.expect_column_values_to_not_be_null("content")["success"]
assert df.expect_column_values_to_be_between("coverage",min_value=0.6,max_value=1)['success']
assert df.expect_column_values_to_be_between("word_count",min_value=100)['success']
return df
def upload_prediction_data(lake_conn,run_name,df,filename):
predictions_path = script_path("online_predictions.csv")
destination_path = "data/external/online_predictions.csv"
df.to_csv(predictions_path)
res = lake_conn.upload_file(run_name,predictions_path,destination_path)
return res
def save_expectations_to_wandb(df):
with open( "my_expectation_file.json", "w") as my_file:
my_file.write(
json.dumps(df.get_expectation_suite().to_json_dict())
)
wandb.save('my_expectation_file.json')
def merge_online_predictions(true_df,fake_df,df):
for index,row in df.iterrows():
if row['ground_truth'] == "Fake":
new_row = {'text':row['content']}
fake_df = fake_df.append(new_row,ignore_index=True)
else:
new_row = {'text':row['content']}
true_df = true_df.append(new_row,ignore_index=True)
return true_df,fake_df
def get_train_test(fake,true):
fake['category'] = 0
true['category'] = 1
df = pd.concat([true, fake])
df['text'] = df['text'] + " " + df['title']
del df['title']
del df['subject']
del df['date']
x_train, x_test, y_train, y_test = train_test_split(
df.text, df.category, random_state=42)
to_return = {
'x_train': x_train,
'x_test': x_test,
'y_train': y_train,
'y_test': y_test
}
return to_return
def upload_train_test(lake_conn,run_name,train_test):
for key in train_test:
keyfile_path = os.path.join(script_path(TEMP_DIR),'{}.csv'.format(key))
train_test[key].to_csv(keyfile_path)
destination_path = "data/interim/{}.csv".format(key)
lake_conn.upload_file(run_name,keyfile_path,destination_path)
def upload_model_files(lake_conn,run_name,vocab,clf):
feature_name = 'feature_{}.pickle'.format(run_name)
feature_path = os.path.join(script_path(TEMP_DIR),feature_name)
destination_path = "models/{}/{}".format(run_name,feature_name)
pickle.dump(vocab,open(feature_path,"wb"))
lake_conn.upload_file(run_name,feature_path,destination_path)
model_name = '{}.joblib'.format(run_name)
model_path = os.path.join(script_path(TEMP_DIR), model_name)
destination_path = "models/{}/{}".format(run_name,model_name)
joblib.dump(clf, model_path)
lake_conn.upload_file(run_name,model_path,destination_path)
def main():
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = script_path('creds.json')
bq_client = bigquery.Client()
model_type = 'svm'
# Create lakefs connector
lake_conn = lakefs_connector.lakefs_conn()
# Init wandb run
wandb.init(project='fn_experiments', job_type='retrain',config={"threshold":0.9})
run_name = wandb.run.name
df = get_data_from_production(bq_client)
df = df.drop_duplicates(subset=['url'])
df = df[df['word_count']>=100]
df = ge.from_pandas(df)
assert_data_quality(df)
lake_conn.create_branch_from_master(run_name)
upload_prediction_data(lake_conn,run_name,df,"online_predictions.csv")
save_expectations_to_wandb(df)
fake_path = "data/external/Fake.csv"
fake_df = lake_conn.get_csv(fake_path)
true_path = "data/external/True.csv"
true_df = lake_conn.get_csv(true_path)
true_df,fake_df = merge_online_predictions(true_df,fake_df,df)
train_test = get_train_test(fake=fake_df.sample(n=40),true=true_df.sample(n=40))
upload_train_test(lake_conn,run_name,train_test)
clf,vocab = train_final.train_final_model(train_test,model_type)
upload_model_files(lake_conn,run_name,vocab,clf)
commit_message = "Added online_predictions, model files and train test splits for branch {}".format(run_name)
lake_conn.commit_to_branch(run_name,commit_message)
if __name__ == "__main__":
main()