Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

generate_report.py 6.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. import datetime as dt
  2. import warnings
  3. import ast
  4. from datetime import datetime
  5. import configparser
  6. import dagshub
  7. from dagshub.upload import Repo
  8. import mlflow
  9. import papermill as pm
  10. from handler import load_data, get_experiment_id
  11. from sklearn import ensemble
  12. import streamlit as st
  13. import os
  14. import sys
  15. from evidently import ColumnMapping
  16. from evidently.metric_preset import DataDriftPreset, TargetDriftPreset
  17. from evidently.metrics import *
  18. from evidently.report import Report
  19. from evidently.test_preset import DataDriftTestPreset
  20. from evidently.test_suite import TestSuite
  21. from dagshub.streaming import install_hooks
  22. warnings.filterwarnings("ignore")
  23. warnings.simplefilter("ignore")
  24. try:
  25. install_hooks()
  26. except:
  27. print("Hooks Loaded")
  28. TARGET = "Close"
  29. PREDICTION = "prediction"
  30. REPO_NAME = " "
  31. USER_NAME = " "
  32. from handler import load_data
  33. st.title('DATA REPORTS')
  34. def setup():
  35. global REPO_NAME
  36. global USER_NAME
  37. config = configparser.ConfigParser()
  38. config.read('config.ini')
  39. os.environ['DAGSHUB_TOKEN'] = dagshub.auth.get_token()
  40. REPO_NAME = config.get('dagshub','repo_name')
  41. USER_NAME = config.get('dagshub','user_name')
  42. dagshub.init(REPO_NAME,USER_NAME)
  43. return config
  44. def generate_model(data, save_local=False):
  45. numerical_features = ['Open', 'High', 'Low', 'Adj Close', 'Volume']
  46. reference = data.loc['2017-02-09':'2021-02-09']
  47. current = data.loc['2021-02-10':'2022-02-07']
  48. column_mapping = ColumnMapping()
  49. column_mapping.target = TARGET
  50. column_mapping.prediction = PREDICTION
  51. column_mapping.numerical_features = numerical_features
  52. experiment_id = get_experiment_id('evidently')
  53. with mlflow.start_run(experiment_id=experiment_id):
  54. regressor = ensemble.RandomForestRegressor(random_state=0, n_estimators=50)
  55. regressor.fit(reference[numerical_features], reference[TARGET])
  56. mlflow.sklearn.log_model(regressor, 'models')
  57. if save_local:
  58. mlflow.sklearn.save_model(regressor,'model')
  59. repo = Repo(USER_NAME, REPO_NAME)
  60. repo.upload(local_path="model", remote_path="model", versioning="dvc")
  61. def load_app(columns):
  62. print('Loading App....')
  63. # config_file = st.form('User Details')
  64. with st.form(key='User Details'):
  65. user_name = st.text_input('*DagsHub User Name',placeholder="[Required] JaneDoe")
  66. repo_name = st.text_input('*DagsHub Repo Name', placeholder="[Required] Sample-Repo")
  67. model_uri = st.text_input("Model URI",placeholder="runs:/000000000000000/model")
  68. col = st.multiselect('*Pick a column', columns)
  69. submit = st.form_submit_button('Submit')
  70. if submit:
  71. st.subheader(f'Writing {user_name}\'s report to {repo_name}')
  72. config_object = configparser.ConfigParser()
  73. config_object["dagshub"]={"user_name":user_name,"repo_name":repo_name}
  74. config_object["mlflow"]={"model_uri":model_uri}
  75. config_object["data"]={"columns":col}
  76. with open("config.ini","w") as file_object:
  77. config_object.write(file_object)
  78. else:
  79. st.stop()
  80. def load_model(data, model_uri='./model'):
  81. current, reference, mapping= data
  82. if not model_uri:
  83. model_uri = './model'
  84. regressor = mlflow.pyfunc.load_model(model_uri=model_uri)
  85. reference["prediction"] = regressor.predict(reference[mapping.numerical_features])
  86. current["prediction"] = regressor.predict(current[mapping.numerical_features])
  87. return current, reference, mapping
  88. def create_data():
  89. config = setup()
  90. data = load_data()
  91. numerical_features = ast.literal_eval(config.get('data','columns'))
  92. if TARGET in numerical_features:
  93. numerical_features.remove(TARGET)
  94. reference = data.loc['2017-02-09':'2021-02-09']
  95. current = data.loc['2021-02-10':'2022-02-07']
  96. column_mapping = ColumnMapping()
  97. column_mapping.target = TARGET
  98. column_mapping.prediction = PREDICTION
  99. column_mapping.numerical_features = numerical_features
  100. model_uri = config.get('mlflow','model_uri')
  101. current, reference,column_mapping = load_model((current,reference,column_mapping), model_uri)
  102. return current, reference, column_mapping
  103. def data_drift_report(data):
  104. current, reference, _ = data
  105. report = Report(metrics=[
  106. DataDriftPreset(),
  107. ])
  108. report.run(reference_data=reference, current_data=current)
  109. return report
  110. def create_report(data, columns=None):
  111. current, reference, column_mapping = data
  112. if columns is None:
  113. columns = reference.columns
  114. values = [RegressionQualityMetric(), DatasetSummaryMetric(), DatasetDriftMetric()]
  115. for column in columns:
  116. values.append(ColumnDriftMetric(column_name=column, stattest="wasserstein"),)
  117. values.extend([ColumnSummaryMetric(column_name=TARGET),ColumnSummaryMetric(column_name=PREDICTION)])
  118. data_drift_report = Report(metrics=values)
  119. data_drift_report.set_batch_size("monthly")
  120. data_drift_report.run(
  121. reference_data=reference,
  122. current_data=current,
  123. column_mapping=column_mapping,
  124. )
  125. return data_drift_report
  126. def create_test_suite(data):
  127. current, reference, column_mapping = data
  128. data_drift_test_suite = TestSuite(
  129. tests=[DataDriftTestPreset()]
  130. )
  131. data_drift_test_suite.run(
  132. reference_data=reference,
  133. current_data=current,
  134. column_mapping=column_mapping,
  135. )
  136. return data_drift_test_suite
  137. def save_report():
  138. now = datetime.now()
  139. date_time = str(now.strftime("%d-%m-%Y_%H-%M-%S"))
  140. output_path = f'./Data_reports/{date_time}.ipynb'
  141. pm.execute_notebook(
  142. './data_report.ipynb',
  143. output_path
  144. )
  145. repo = Repo(USER_NAME, REPO_NAME)
  146. repo.upload(local_path=output_path, remote_path=output_path, versioning="git")
  147. if __name__ == "__main__":
  148. # Load Data
  149. data = load_data()
  150. # Create a model
  151. #### NOTE: Fill in the config.ini manually if its empty ####
  152. # config = setup()
  153. # generate_model(data,True)
  154. # Load app to get config
  155. load_app(data.columns)
  156. # Create Report
  157. config = setup()
  158. save_report()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...