-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_databricks_pytest_v3.py
More file actions
340 lines (287 loc) · 11.6 KB
/
Copy pathrun_databricks_pytest_v3.py
File metadata and controls
340 lines (287 loc) · 11.6 KB
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
"""
Submit insurance-quantile pytest suite (v0.3.3, torch/lightgbm optional [eqrn] extra)
to Databricks serverless compute.
Runs both core tests and EQRN tests (torch is installed on Databricks).
Run from the project root:
python run_databricks_pytest_v3.py
"""
import base64
import json
import os
import sys
import time
import urllib.error
import urllib.request
import uuid
# ---------------------------------------------------------------------------
# Load credentials
# ---------------------------------------------------------------------------
env_path = os.path.expanduser("~/.config/burning-cost/databricks.env")
with open(env_path) as f:
for line in f:
line = line.strip()
if line and not line.startswith("#") and "=" in line:
k, v = line.split("=", 1)
os.environ[k.strip()] = v.strip()
api_base = os.environ["DATABRICKS_HOST"].rstrip("/")
token = os.environ["DATABRICKS_TOKEN"]
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
RUN_ID = uuid.uuid4().hex[:8]
WORKSPACE_FOLDER = "/Workspace/insurance-quantile-v033"
NOTEBOOK_PATH = f"{WORKSPACE_FOLDER}/run_pytest_v033"
# ---------------------------------------------------------------------------
# Read source files
# ---------------------------------------------------------------------------
BASE = "/home/ralph/burning-cost/repos/insurance-quantile"
def read_file(path: str) -> str:
with open(path, "r") as f:
return f.read()
src_files = {
"__init__.py": read_file(f"{BASE}/src/insurance_quantile/__init__.py"),
"_types.py": read_file(f"{BASE}/src/insurance_quantile/_types.py"),
"_model.py": read_file(f"{BASE}/src/insurance_quantile/_model.py"),
"_calibration.py": read_file(f"{BASE}/src/insurance_quantile/_calibration.py"),
"_tvar.py": read_file(f"{BASE}/src/insurance_quantile/_tvar.py"),
"_loading.py": read_file(f"{BASE}/src/insurance_quantile/_loading.py"),
"_exceedance.py": read_file(f"{BASE}/src/insurance_quantile/_exceedance.py"),
"_two_part.py": read_file(f"{BASE}/src/insurance_quantile/_two_part.py"),
}
eqrn_src_files = {
"eqrn/__init__.py": read_file(f"{BASE}/src/insurance_quantile/eqrn/__init__.py"),
"eqrn/gpd.py": read_file(f"{BASE}/src/insurance_quantile/eqrn/gpd.py"),
"eqrn/network.py": read_file(f"{BASE}/src/insurance_quantile/eqrn/network.py"),
"eqrn/intermediate.py": read_file(f"{BASE}/src/insurance_quantile/eqrn/intermediate.py"),
"eqrn/model.py": read_file(f"{BASE}/src/insurance_quantile/eqrn/model.py"),
"eqrn/diagnostics.py": read_file(f"{BASE}/src/insurance_quantile/eqrn/diagnostics.py"),
}
test_files = {
"conftest.py": read_file(f"{BASE}/tests/conftest.py"),
"test_model.py": read_file(f"{BASE}/tests/test_model.py"),
"test_calibration.py": read_file(f"{BASE}/tests/test_calibration.py"),
"test_tvar.py": read_file(f"{BASE}/tests/test_tvar.py"),
"test_loading.py": read_file(f"{BASE}/tests/test_loading.py"),
"test_exceedance.py": read_file(f"{BASE}/tests/test_exceedance.py"),
"test_types.py": read_file(f"{BASE}/tests/test_types.py"),
"test_two_part.py": read_file(f"{BASE}/tests/test_two_part.py"),
}
eqrn_test_files = {
"eqrn/__init__.py": "",
"eqrn/conftest.py": read_file(f"{BASE}/tests/eqrn/conftest.py"),
"eqrn/test_gpd.py": read_file(f"{BASE}/tests/eqrn/test_gpd.py"),
"eqrn/test_network.py": read_file(f"{BASE}/tests/eqrn/test_network.py"),
"eqrn/test_intermediate.py": read_file(f"{BASE}/tests/eqrn/test_intermediate.py"),
"eqrn/test_model.py": read_file(f"{BASE}/tests/eqrn/test_model.py"),
"eqrn/test_diagnostics.py": read_file(f"{BASE}/tests/eqrn/test_diagnostics.py"),
}
all_src = {**src_files, **eqrn_src_files}
all_tests = {**test_files, **eqrn_test_files}
files_json = json.dumps({
"src": all_src,
"tests": all_tests,
})
# v0.3.3: torch and lightgbm are now optional [eqrn] extras, not hard deps.
# We still install them for CI because Databricks has torch available.
pyproject_content = """[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "insurance-quantile"
version = "0.3.3"
requires-python = ">=3.10"
dependencies = [
"numpy>=1.24",
"polars>=1.0",
"catboost>=1.2",
"scikit-learn>=1.3",
"scipy>=1.10",
"pandas>=2.0",
"matplotlib>=3.6",
]
[project.optional-dependencies]
eqrn = [
"torch>=2.0",
"lightgbm>=4.0",
]
[tool.hatch.build.targets.wheel]
packages = ["src/insurance_quantile"]
"""
pyproject_json = json.dumps(pyproject_content)
# ---------------------------------------------------------------------------
# Build notebook source
# Use a plain string template with __FILES_JSON__ and __PYPROJECT_JSON__ as
# placeholders to avoid f-string escaping issues with notebook Python code.
# ---------------------------------------------------------------------------
NOTEBOOK_TEMPLATE = r"""# Databricks notebook source
# MAGIC %pip install polars>=1.0 numpy>=1.24 catboost>=1.2 scikit-learn>=1.3 torch>=2.0 lightgbm>=4.0 scipy>=1.10 pandas>=2.0 matplotlib>=3.6 pytest>=7.0 hatchling --quiet
# COMMAND ----------
import json, os, sys, uuid, subprocess
pkg_id = uuid.uuid4().hex[:8]
pkg_dir = f"/tmp/insurance_quantile_{pkg_id}"
src_dir = f"{pkg_dir}/src/insurance_quantile"
eqrn_src_dir = f"{src_dir}/eqrn"
tests_dir = f"{pkg_dir}/tests"
eqrn_tests_dir = f"{tests_dir}/eqrn"
for d in [src_dir, eqrn_src_dir, tests_dir, eqrn_tests_dir]:
os.makedirs(d, exist_ok=True)
FILES_JSON = __FILES_JSON__
PYPROJECT_JSON = __PYPROJECT_JSON__
data = json.loads(FILES_JSON)
pyproject_src = json.loads(PYPROJECT_JSON)
for name, content in data["src"].items():
path = f"{src_dir}/{name}"
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as fh:
fh.write(content)
for name, content in data["tests"].items():
path = f"{tests_dir}/{name}"
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as fh:
fh.write(content)
with open(f"{pkg_dir}/pyproject.toml", "w") as fh:
fh.write(pyproject_src)
print(f"Written {len(data['src'])} source files, {len(data['tests'])} test files to {pkg_dir}")
# COMMAND ----------
# Verify the lazy-import machinery is in place
print("Smoke test: __getattr__ in __init__.py ->", "__getattr__" in open(f"{src_dir}/__init__.py").read())
print("Smoke test: torch NOT in hard deps ->", "torch" not in pyproject_src.split("[project.optional-dependencies]")[0])
# COMMAND ----------
r = subprocess.run(
[sys.executable, "-m", "pip", "install", "-e", pkg_dir, "--quiet"],
capture_output=True, text=True
)
if r.returncode != 0:
print("Install error:", r.stderr[:2000])
else:
print("insurance-quantile v0.3.3 installed from", pkg_dir)
# COMMAND ----------
# Run core (non-EQRN) tests
print("=== Running core tests (no EQRN) ===")
r_core = subprocess.run(
[sys.executable, "-m", "pytest", tests_dir,
"--ignore", f"{tests_dir}/eqrn",
"-v", "--tb=short", "--no-header", "-p", "no:cacheprovider"],
capture_output=True, text=True, cwd=pkg_dir
)
print(r_core.stdout[-10000:])
if r_core.stderr:
print("STDERR:", r_core.stderr[-1000:])
# COMMAND ----------
# Run EQRN tests (torch is installed on this cluster)
print("=== Running EQRN tests ===")
r_eqrn = subprocess.run(
[sys.executable, "-m", "pytest", f"{tests_dir}/eqrn",
"-v", "--tb=short", "--no-header", "-p", "no:cacheprovider"],
capture_output=True, text=True, cwd=pkg_dir
)
print(r_eqrn.stdout[-10000:])
if r_eqrn.stderr:
print("STDERR:", r_eqrn.stderr[-1000:])
# COMMAND ----------
total_pass = r_core.returncode == 0 and r_eqrn.returncode == 0
if total_pass:
msg = "ALL TESTS PASSED (core + EQRN)"
print(f"\n=== {msg} ===")
try:
dbutils.notebook.exit(msg)
except NameError:
pass
else:
codes = f"core={r_core.returncode} eqrn={r_eqrn.returncode}"
msg = f"TESTS FAILED ({codes})"
print(f"\n=== {msg} ===")
try:
dbutils.notebook.exit(msg)
except NameError:
pass
"""
# Inject the data blobs
NOTEBOOK_SOURCE = NOTEBOOK_TEMPLATE.replace("__FILES_JSON__", repr(files_json)).replace(
"__PYPROJECT_JSON__", repr(pyproject_json)
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def api_call(method: str, endpoint: str, body: dict | None = None):
data = json.dumps(body).encode("utf-8") if body else None
req = urllib.request.Request(
f"{api_base}/{endpoint}",
data=data,
headers=headers,
method=method,
)
try:
with urllib.request.urlopen(req) as resp:
return json.loads(resp.read())
except urllib.error.HTTPError as e:
err_body = e.read().decode()
raise RuntimeError(f"API {method} {endpoint} failed {e.code}: {err_body}")
# ---------------------------------------------------------------------------
# Ensure workspace folder exists
# ---------------------------------------------------------------------------
print(f"Creating workspace folder {WORKSPACE_FOLDER} ...")
try:
api_call("POST", "api/2.0/workspace/mkdirs", {"path": WORKSPACE_FOLDER})
print("Folder ready.")
except RuntimeError as exc:
print(f"mkdirs note: {exc}")
# ---------------------------------------------------------------------------
# Upload notebook
# ---------------------------------------------------------------------------
print(f"Uploading notebook to {NOTEBOOK_PATH} ...")
notebook_b64 = base64.b64encode(NOTEBOOK_SOURCE.encode("utf-8")).decode("ascii")
api_call("POST", "api/2.0/workspace/import", {
"path": NOTEBOOK_PATH,
"format": "SOURCE",
"language": "PYTHON",
"content": notebook_b64,
"overwrite": True,
})
print("Notebook uploaded.")
# ---------------------------------------------------------------------------
# Submit job run
# ---------------------------------------------------------------------------
print("Submitting job run ...")
run_resp = api_call("POST", "api/2.1/jobs/runs/submit", {
"run_name": f"insurance-quantile-v033-pytest-{RUN_ID}",
"tasks": [{
"task_key": "pytest",
"notebook_task": {
"notebook_path": NOTEBOOK_PATH,
"source": "WORKSPACE",
},
}],
})
run_id = run_resp["run_id"]
print(f"Job run submitted: run_id={run_id}")
print(f"UI: {api_base}/#job/runs/{run_id}")
# ---------------------------------------------------------------------------
# Poll for completion
# ---------------------------------------------------------------------------
print("Polling for completion (timeout 20 min) ...")
deadline = time.time() + 1200
while time.time() < deadline:
status = api_call("GET", f"api/2.1/jobs/runs/get?run_id={run_id}")
life = status["state"]["life_cycle_state"]
result = status["state"].get("result_state", "")
print(f" [{time.strftime('%H:%M:%S')}] {life} / {result}")
if life in ("TERMINATED", "SKIPPED", "INTERNAL_ERROR"):
break
time.sleep(30)
if result == "SUCCESS":
output = api_call("GET", f"api/2.1/jobs/runs/get-output?run_id={run_id}")
notebook_out = output.get("notebook_output", {}).get("result", "")
print(f"\nNotebook exit message: {notebook_out}")
print("\nRESULT: PASSED")
sys.exit(0)
else:
output = api_call("GET", f"api/2.1/jobs/runs/get-output?run_id={run_id}")
notebook_out = output.get("notebook_output", {}).get("result", "")
error = output.get("error", "")
print(f"\nNotebook exit message: {notebook_out}")
if error:
print(f"Error: {error}")
print(f"\nRESULT: FAILED ({result})")
sys.exit(1)