-
Notifications
You must be signed in to change notification settings - Fork 10
/
predict.py
277 lines (239 loc) · 8.96 KB
/
predict.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
import argparse
import logging
import os
import pickle
import requests
import secrets
import shutil
import sys
import urllib
import time
import random
import io
from typing import Callable
from inspect import signature
from numerapi import NumerAPI
import pandas as pd
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
default="v5.0/live.parquet",
help="Numerapi dataset path or local file.",
)
parser.add_argument(
"--benchmarks",
default="v5.0/live_benchmark_models.parquet",
help="Numerapi benchmark model path or local file.",
)
parser.add_argument("--model", required=True, help="Pickled model file or URL")
parser.add_argument("--output_dir", default="/tmp", help="File output dir")
parser.add_argument("--post_url", help="Url to post model output")
parser.add_argument("--post_data", help="Urlencoded post data dict")
parser.add_argument("--debug", action="store_true", help="Enable DEBUG log level")
args = parser.parse_args()
if args.post_url and not args.post_data:
raise argparse.ArgumentError(
"--post_data arg is required when using --post_url"
)
if args.post_data and not args.post_url:
raise argparse.ArgumentError(
"--post_url arg is required when using --post_data"
)
if not os.path.isdir(args.output_dir):
raise argparse.ArgumentError(
f"--output_dir {args.output_dir} is not an existing directory"
)
if args.post_data:
data = urllib.parse.parse_qs(args.post_data)
if type(data) != dict:
raise argparse.ArgumentError(
"--post_data must be urlencoded and resolve to dict"
)
args.post_data = data
return args
def py_version(separator="."):
return separator.join(sys.version.split(".")[:2])
def exit_with_help(error):
git_ref = os.getenv("GIT_REF", "latest")
docker_image_path = (
f"ghcr.io/numerai/numerai_predict_py_{py_version('_')}:{git_ref}"
)
docker_args = "--debug --model $PWD/[PICKLE_FILE]"
logging.root.handlers[0].flush()
logging.root.handlers[0].setFormatter(logging.Formatter("%(message)s"))
logging.info(
f"""
{"-" * 80}
Debug your pickle model locally via docker command:
docker run -i --rm -v "$PWD:$PWD" {docker_image_path} {docker_args}
Try our other support resources:
[Github] https://github.com/numerai/numerai-predict
[Discord] https://discord.com/channels/894652647515226152/1089652477957246996
{"-" * 80}"""
)
sys.exit(error)
def retry_request_with_backoff(
request_func: Callable[[], requests.Response],
retries: int = 10,
delay_base: float = 1.5,
delay_exp: float = 1.5,
):
delay_base = max(1.1, delay_base)
delay_exp = max(1.1, delay_exp)
curr_delay = delay_base
for i in range(retries):
try:
response = request_func()
logging.info("HTTP Response Status: %s", response.status_code)
if response.status_code >= 500:
logging.error("Encountered Server Error. Retrying...")
time.sleep(curr_delay)
curr_delay **= random.uniform(1, delay_exp)
elif 200 <= response.status_code < 300:
logging.debug("Request successful! Returning...")
return response
else:
raise RuntimeError(f"HTTP Error {response.reason} - {response.text}")
except requests.exceptions.ConnectionError:
logging.error("Connection reset! Retrying...")
time.sleep(curr_delay)
curr_delay **= random.uniform(1, delay_exp)
continue
except requests.exceptions.SSLError as e:
logging.error("SSL Error: %s", e)
finally:
logging.info("Retrying in %s seconds...", curr_delay)
time.sleep(curr_delay)
curr_delay **= random.uniform(1, delay_exp)
raise RuntimeError(f"Could not complete function call after {retries} retries...")
def get_data(dataset, output_dir):
if os.path.exists(dataset):
dataset_path = dataset
logging.info("Using local %s for live data", dataset_path)
elif dataset.startswith("/"):
logging.error("Local dataset not found - %s does not exist!", dataset)
exit_with_help(1)
else:
dataset_path = os.path.join(output_dir, dataset)
logging.info("Using NumerAPI to download %s for live data", dataset)
napi = NumerAPI()
napi.download_dataset(dataset, dataset_path)
logging.info("Loading live features %s", dataset_path)
live_features = pd.read_parquet(dataset_path)
return live_features
def upload_live_output(
predictions: pd.DataFrame,
post_url: str,
post_data: str,
predictions_csv_file_name: str,
):
logging.info("Uploading predictions to %s", post_url)
csv_buffer = io.StringIO()
predictions.to_csv(csv_buffer)
def post_live_output():
csv_buffer.seek(0)
return requests.post(
post_url,
data=post_data,
files={"file": (predictions_csv_file_name, csv_buffer, "text/csv")},
)
retry_request_with_backoff(post_live_output)
def main(args):
logging.getLogger().setLevel(logging.DEBUG if args.debug else logging.INFO)
logging.info(
"Running numerai-predict:%s - Python %s", os.getenv("GIT_REF"), py_version()
)
if args.model.lower().startswith("http"):
truncated_url = args.model.split("?")[0]
logging.info("Downloading model %s", truncated_url)
response = retry_request_with_backoff(
lambda: requests.get(args.model, stream=True, allow_redirects=True)
)
model_name = truncated_url.split("/")[-1]
model_pkl = os.path.join(args.output_dir, model_name)
logging.info("Saving model to %s", model_pkl)
with open(model_pkl, "wb") as f:
shutil.copyfileobj(response.raw, f)
else:
model_pkl = args.model
logging.info("Loading model %s", model_pkl)
try:
model = pd.read_pickle(model_pkl)
except pickle.UnpicklingError as e:
logging.error("Invalid pickle - %s", e)
if args.debug:
logging.exception(e)
exit_with_help(1)
except TypeError as e:
logging.error("Pickle incompatible with %s", py_version())
if args.debug:
logging.exception(e)
exit_with_help(1)
except ModuleNotFoundError as e:
logging.error("Import error reading pickle - %s", e)
if args.debug:
logging.exception(e)
exit_with_help(1)
logging.debug(model)
num_args = len(signature(model).parameters)
live_features = get_data(args.dataset, args.output_dir)
if num_args > 1:
benchmark_models = get_data(args.benchmarks, args.output_dir)
logging.info("Predicting on %s rows of live features", len(live_features))
try:
if num_args == 1:
predictions = model(live_features)
elif num_args == 2:
predictions = model(live_features, benchmark_models)
else:
logging.error(
"Invalid pickle function - %s must have 1 or 2 arguments", model_pkl
)
exit_with_help(1)
if predictions is None:
logging.error("Pickle function is invalid - returned None")
exit_with_help(1)
elif type(predictions) != pd.DataFrame:
logging.error(
"Pickle function is invalid - returned %s instead of pd.DataFrame",
type(predictions),
)
exit_with_help(1)
elif len(predictions) == 0:
logging.error("Pickle function returned 0 predictions")
exit_with_help(1)
elif predictions.isna().any().any():
logging.error("Pickle function returned at least 1 NaN prediction")
exit_with_help(1)
elif not (predictions.iloc[:, 0].between(0, 1).all().all()):
logging.error(
"Pickle function returned invalid predictions. Ensure values are between 0 and 1."
)
exit_with_help(1)
except TypeError as e:
logging.error("Pickle function is invalid - %s", e)
if args.debug:
logging.exception(e)
exit_with_help(1)
except Exception as e:
logging.exception(e)
exit_with_help(1)
logging.info("Generated %s predictions", len(predictions))
logging.debug(predictions)
predictions_csv_file_name = os.path.join(
args.output_dir, f"live_predictions-{secrets.token_hex(6)}.csv"
)
if args.post_url:
upload_live_output(
predictions,
args.post_url,
args.post_data,
predictions_csv_file_name,
)
else:
logging.info("Saving predictions to %s", predictions_csv_file_name)
with open(predictions_csv_file_name, "w") as f:
predictions.to_csv(f)
if __name__ == "__main__":
main(parse_args())