-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_risk_informed_tests.py
More file actions
264 lines (226 loc) · 8.44 KB
/
Copy pathrun_risk_informed_tests.py
File metadata and controls
264 lines (226 loc) · 8.44 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
"""
Submit the RiskInformedRetentionModel tests to Databricks serverless compute.
Uploads the full demand subpackage plus the new test file and runs pytest.
"""
import base64
import json
import os
import sys
import time
import urllib.error
import urllib.request
import uuid
# ---------------------------------------------------------------------------
# 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-optimise-ri"
NOTEBOOK_PATH = f"{WORKSPACE_FOLDER}/run_pytest_ri"
BASE = "/home/ralph/burning-cost/repos/insurance-optimise"
def read_file(path: str) -> str:
with open(path) as f:
return f.read()
# ---------------------------------------------------------------------------
# Source files — full demand subpackage + top-level modules
# ---------------------------------------------------------------------------
demand_src_dir = f"{BASE}/src/insurance_optimise/demand"
demand_src_files = {}
for fname in os.listdir(demand_src_dir):
if fname.endswith(".py"):
demand_src_files[f"demand/{fname}"] = read_file(f"{demand_src_dir}/{fname}")
top_src_files = {
"top/__init__.py": read_file(f"{BASE}/src/insurance_optimise/__init__.py"),
"top/constraints.py": read_file(f"{BASE}/src/insurance_optimise/constraints.py"),
"top/result.py": read_file(f"{BASE}/src/insurance_optimise/result.py"),
"top/optimiser.py": read_file(f"{BASE}/src/insurance_optimise/optimiser.py"),
"top/frontier.py": read_file(f"{BASE}/src/insurance_optimise/frontier.py"),
"top/scenarios.py": read_file(f"{BASE}/src/insurance_optimise/scenarios.py"),
"top/_demand_model.py": read_file(f"{BASE}/src/insurance_optimise/_demand_model.py"),
"top/pareto.py": read_file(f"{BASE}/src/insurance_optimise/pareto.py"),
"top/pareto_front.py": read_file(f"{BASE}/src/insurance_optimise/pareto_front.py"),
"top/stochastic.py": read_file(f"{BASE}/src/insurance_optimise/stochastic.py"),
"top/model_quality.py": read_file(f"{BASE}/src/insurance_optimise/model_quality.py"),
"top/audit.py": read_file(f"{BASE}/src/insurance_optimise/audit.py"),
"top/plotting.py": read_file(f"{BASE}/src/insurance_optimise/plotting.py"),
}
test_files = {
"tests/conftest.py": read_file(f"{BASE}/tests/conftest.py"),
"tests/test_risk_informed.py": read_file(
f"{BASE}/tests/demand/test_risk_informed_retention.py"
),
}
# Include pyproject.toml as a special key so the notebook can write it
pyproject_content = (
"[build-system]\n"
'requires = ["hatchling"]\n'
'build-backend = "hatchling.build"\n'
"\n"
"[project]\n"
'name = "insurance-optimise"\n'
'version = "0.0.1"\n'
'requires-python = ">=3.10"\n'
"dependencies = [\n"
' "numpy>=1.24",\n'
' "polars>=0.20",\n'
' "scipy>=1.10",\n'
' "scikit-learn",\n'
' "statsmodels",\n'
' "pandas",\n'
"]\n"
"\n"
"[tool.hatch.build.targets.wheel]\n"
'packages = ["src/insurance_optimise"]\n'
)
all_files = {**demand_src_files, **top_src_files, **test_files}
# Embed pyproject separately so it doesn't need a prefix
all_files["_pyproject_/pyproject.toml"] = pyproject_content
files_json = json.dumps(all_files)
# ---------------------------------------------------------------------------
# Notebook source
# ---------------------------------------------------------------------------
NOTEBOOK_SOURCE = f"""# Databricks notebook source
# MAGIC %pip install polars>=0.20 numpy>=1.24 scipy>=1.10 scikit-learn pytest statsmodels --quiet
# COMMAND ----------
import json, os, sys, uuid, subprocess
pkg_id = uuid.uuid4().hex[:8]
root = f"/tmp/ins_opt_{{pkg_id}}"
src_top = f"{{root}}/src/insurance_optimise"
src_demand = f"{{src_top}}/demand"
tests_dir = f"{{root}}/tests"
os.makedirs(src_demand, exist_ok=True)
os.makedirs(tests_dir, exist_ok=True)
FILES_JSON = {files_json!r}
files_map = json.loads(FILES_JSON)
for key, content in files_map.items():
prefix, fname = key.split("/", 1)
if prefix == "demand":
path = f"{{src_demand}}/{{fname}}"
elif prefix == "top":
path = f"{{src_top}}/{{fname}}"
elif prefix == "_pyproject_":
path = f"{{root}}/{{fname}}"
else:
path = f"{{tests_dir}}/{{fname}}"
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
f.write(content)
print(f"Written {{len(files_map)}} files to {{root}}")
# COMMAND ----------
r = subprocess.run(
[sys.executable, "-m", "pip", "install", "-e", root, "--quiet"],
capture_output=True, text=True
)
if r.returncode != 0:
print("Install error:", r.stderr[:2000])
raise RuntimeError("Package install failed")
else:
print("insurance-optimise installed OK")
# COMMAND ----------
r = subprocess.run(
[sys.executable, "-m", "pytest", tests_dir, "-v", "--tb=short",
"--no-header", "-p", "no:cacheprovider"],
capture_output=True, text=True, cwd=root
)
print(r.stdout[-10000:])
if r.stderr:
print("STDERR:", r.stderr[-2000:])
if r.returncode == 0:
print("\\n=== ALL TESTS PASSED ===")
try:
dbutils.notebook.exit("ALL TESTS PASSED")
except NameError:
pass
else:
msg = f"TESTS FAILED (exit {{r.returncode}})"
print(f"\\n=== {{msg}} ===")
try:
dbutils.notebook.exit(msg)
except NameError:
pass
"""
# ---------------------------------------------------------------------------
# REST helpers
# ---------------------------------------------------------------------------
def api_call(method: str, endpoint: str, body=None):
data = json.dumps(body).encode() 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:
raise RuntimeError(f"API {method} {endpoint} failed {e.code}: {e.read().decode()}")
# ---------------------------------------------------------------------------
# Upload and run
# ---------------------------------------------------------------------------
print(f"Creating folder {WORKSPACE_FOLDER} ...")
try:
api_call("POST", "api/2.0/workspace/mkdirs", {"path": WORKSPACE_FOLDER})
except RuntimeError as e:
print(f"mkdirs note: {e}")
print(f"Uploading notebook to {NOTEBOOK_PATH} ...")
api_call("POST", "api/2.0/workspace/import", {
"path": NOTEBOOK_PATH,
"format": "SOURCE",
"language": "PYTHON",
"content": base64.b64encode(NOTEBOOK_SOURCE.encode()).decode(),
"overwrite": True,
})
print("Upload OK")
print(f"Submitting run {RUN_ID} ...")
result = api_call("POST", "api/2.1/jobs/runs/submit", {
"run_name": f"ri-retention-pytest-{RUN_ID}",
"tasks": [{
"task_key": "pytest",
"notebook_task": {"notebook_path": NOTEBOOK_PATH, "source": "WORKSPACE"},
}],
})
run_id = result["run_id"]
print(f"Run submitted: run_id={run_id}")
print("Polling ...")
lc, rs = "PENDING", "-"
for i in range(120):
time.sleep(15)
state = api_call("GET", f"api/2.1/jobs/runs/get?run_id={run_id}")
lc = state.get("state", {}).get("life_cycle_state", "UNKNOWN")
rs = state.get("state", {}).get("result_state", "-")
print(f" [{i * 15}s] {lc} / {rs}")
if lc in ("TERMINATED", "SKIPPED", "INTERNAL_ERROR"):
break
print(f"\nFinal: {lc} / {rs}")
try:
output = api_call("GET", f"api/2.1/jobs/runs/get-output?run_id={run_id}")
nb_result = output.get("notebook_output", {}).get("result", "")
error = output.get("error", "")
trace = output.get("error_trace", "")
logs = output.get("logs", "")
if nb_result:
print(f"\nExit value: {nb_result}")
if error:
print(f"Error: {error}")
if trace:
print(f"Trace:\n{trace[:3000]}")
if logs:
print(f"\nLogs:\n{logs[-10000:]}")
except Exception as e:
print(f"Could not fetch output: {e}")
if rs == "SUCCESS":
print("\n=== PASS ===")
sys.exit(0)
else:
print(f"\n=== FAIL: {rs} ===")
sys.exit(1)