|
1 | 1 | import os |
| 2 | +import pickle |
2 | 3 | import time |
3 | 4 | import urllib.request |
4 | 5 |
|
|
17 | 18 | "tabarena_wide_y.csv": "https://github.com/user-attachments/files/25682566/y.csv", |
18 | 19 | "tabarena_var_x.csv": "https://github.com/user-attachments/files/25739177/X.csv", |
19 | 20 | "tabarena_var_y.csv": "https://github.com/user-attachments/files/25739178/y.csv", |
| 21 | + "tabarena_load_x.csv": "https://github.com/user-attachments/files/25764663/X.csv", |
| 22 | + "tabarena_load_y.csv": "https://github.com/user-attachments/files/25764665/y.csv", |
20 | 23 | } |
21 | 24 |
|
22 | 25 |
|
23 | 26 | def _get_csv(filename): |
24 | 27 | """Try to read a CSV from resources dir, then download, or skip the test.""" |
| 28 | + print(f"Loading {filename}...") |
25 | 29 | local_path = os.path.join(RESOURCES_DIR, filename) |
26 | 30 | if os.path.isfile(local_path): |
27 | | - return pd.read_csv(local_path) |
| 31 | + res = pd.read_csv(local_path) |
| 32 | + print(f"Loaded {filename} from resources.") |
| 33 | + return res |
28 | 34 |
|
29 | 35 | # Try downloading to the resources folder |
30 | 36 | url = DOWNLOAD_URLS.get(filename) |
31 | 37 | if url is None: |
32 | 38 | pytest.skip(f"No download URL configured for {filename}") |
33 | 39 | try: |
| 40 | + print(f"Downloading {filename} from {url}...") |
34 | 41 | os.makedirs(RESOURCES_DIR, exist_ok=True) |
35 | 42 | urllib.request.urlretrieve(url, local_path) |
| 43 | + print(f"Downloaded {filename}.") |
36 | 44 | except Exception as exc: |
37 | 45 | pytest.skip(f"Could not download {filename}: {exc}") |
38 | 46 |
|
@@ -171,8 +179,8 @@ def test_tabarena_wide(): |
171 | 179 | objective="LogLoss", |
172 | 180 | budget=2.0, |
173 | 181 | categorical_features=categorical_features, |
174 | | - memory_limit=1, # 30 |
175 | | - iteration_limit=3, # 10000 |
| 182 | + memory_limit=1, |
| 183 | + iteration_limit=3, |
176 | 184 | timeout=60 * 15, |
177 | 185 | ) |
178 | 186 |
|
@@ -225,3 +233,104 @@ def test_tabarena_var(): |
225 | 233 | categorical_features=categorical_features, iteration_limit=3, memory_limit=1 |
226 | 234 | ) |
227 | 235 | model.fit(X, y) |
| 236 | + |
| 237 | + |
| 238 | +def test_tabarena_save_load(): |
| 239 | + print("test_tabarena_save_load started.") |
| 240 | + X = _get_csv("tabarena_load_x.csv") |
| 241 | + y = _get_csv("tabarena_load_y.csv") |
| 242 | + |
| 243 | + categorical_features = [ |
| 244 | + "position_-30", |
| 245 | + "position_-29", |
| 246 | + "position_-28", |
| 247 | + "position_-27", |
| 248 | + "position_-26", |
| 249 | + "position_-25", |
| 250 | + "position_-24", |
| 251 | + "position_-23", |
| 252 | + "position_-22", |
| 253 | + "position_-21", |
| 254 | + "position_-20", |
| 255 | + "position_-19", |
| 256 | + "position_-18", |
| 257 | + "position_-17", |
| 258 | + "position_-16", |
| 259 | + "position_-15", |
| 260 | + "position_-14", |
| 261 | + "position_-13", |
| 262 | + "position_-12", |
| 263 | + "position_-11", |
| 264 | + "position_-10", |
| 265 | + "position_-9", |
| 266 | + "position_-8", |
| 267 | + "position_-7", |
| 268 | + "position_-6", |
| 269 | + "position_-5", |
| 270 | + "position_-4", |
| 271 | + "position_-3", |
| 272 | + "position_-2", |
| 273 | + "position_-1", |
| 274 | + "position_1", |
| 275 | + "position_2", |
| 276 | + "position_3", |
| 277 | + "position_4", |
| 278 | + "position_5", |
| 279 | + "position_6", |
| 280 | + "position_7", |
| 281 | + "position_8", |
| 282 | + "position_9", |
| 283 | + "position_10", |
| 284 | + "position_11", |
| 285 | + "position_12", |
| 286 | + "position_13", |
| 287 | + "position_14", |
| 288 | + "position_15", |
| 289 | + "position_16", |
| 290 | + "position_17", |
| 291 | + "position_18", |
| 292 | + "position_19", |
| 293 | + "position_20", |
| 294 | + "position_21", |
| 295 | + "position_22", |
| 296 | + "position_23", |
| 297 | + "position_24", |
| 298 | + "position_25", |
| 299 | + "position_26", |
| 300 | + "position_27", |
| 301 | + "position_28", |
| 302 | + "position_29", |
| 303 | + "position_30", |
| 304 | + ] |
| 305 | + |
| 306 | + model = PerpetualBooster( |
| 307 | + categorical_features=categorical_features, |
| 308 | + memory_limit=3, |
| 309 | + num_threads=8, |
| 310 | + objective="LogLoss", |
| 311 | + iteration_limit=10, |
| 312 | + budget=2.0, |
| 313 | + ) |
| 314 | + |
| 315 | + print(f"Starting fit... memory_limit={model.memory_limit}") |
| 316 | + model.fit(X, y) |
| 317 | + print("Fit completed.") |
| 318 | + print(f"Number of trees: {model.number_of_trees}") |
| 319 | + |
| 320 | + model_path = os.path.join(RESOURCES_DIR, "model.pkl") |
| 321 | + |
| 322 | + print(f"Saving model to {model_path}...") |
| 323 | + with open(model_path, "wb") as f: |
| 324 | + pickle.dump(model, f) |
| 325 | + print("Model saved.") |
| 326 | + |
| 327 | + print("Loading model...") |
| 328 | + with open(model_path, "rb") as f: |
| 329 | + loaded_model = pickle.load(f) |
| 330 | + print("Model loaded.") |
| 331 | + |
| 332 | + del loaded_model |
| 333 | + try: |
| 334 | + os.remove(model_path) |
| 335 | + except OSError: |
| 336 | + pass |
0 commit comments