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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670 | class TuningCalculator(BaseModelCalculator):
"""Tune a plain ``BaseModelCalculator`` and refit it with the best parameters.
``fit`` accepts a ``TuningConfig`` or dict with ``strategy``, ``metric``,
``n_trials``, ``search_space`` (a mapping of parameter names to candidate
lists), and optional cross-validation and ``strategy_params`` settings.
``grid`` and ``random`` use Skyulf's candidate loop; ``halving_grid`` and
``halving_random`` use sklearn searchers; ``optuna`` uses OptunaSearchCV
and requires the Optuna integration package.
Examples:
>>> tuner = TuningCalculator(LogisticRegressionCalculator())
>>> model, result = tuner.fit(X, y, {"strategy": "random", "search_space": {"C": [0.1, 1.0]}})
"""
def __init__(self, model_calculator: BaseModelCalculator):
self.model_calculator = model_calculator
@property
def problem_type(self) -> str:
return self.model_calculator.problem_type
# ------------------------------------------------------------------
# Thin delegates to the split leaf modules (F-18). Kept so the pinned
# internal surface — direct calls and monkeypatches in the test suite —
# keeps resolving on ``TuningCalculator``.
# ------------------------------------------------------------------
def _clean_search_space(self, search_space: dict[str, Any]) -> dict[str, Any]:
return clean_search_space(search_space)
@staticmethod
def _instantiate_model(model_class: Any, params: dict[str, Any]) -> Any:
return instantiate_model(model_class, params)
def _resolve_scorer(self, metric: str, y: Any) -> Any:
return resolve_scorer(metric, y, getattr(self.model_calculator, "problem_type", None))
@staticmethod
def _is_multiclass_target(y: Any) -> bool:
return is_multiclass_target(y)
def _refit_best_model(
self,
tuning_result: TuningResult,
tuning_config: TuningConfig,
X_np: Any,
y_np: Any,
log_callback: Callable[[str], None] | None,
iteration_callback: Callable[..., None] | None = None,
) -> Any:
return refit_best_model(
self.model_calculator,
tuning_result,
tuning_config,
X_np,
y_np,
log_callback,
iteration_callback,
)
def _resolve_threshold_metric(
self,
metric_name: str,
log_callback: Callable[[str], None] | None,
pos_label: Any = None,
) -> tuple[Callable[[Any, Any], float], str]:
return resolve_threshold_metric(metric_name, log_callback, pos_label)
def _tune_decision_thresholds(
self,
model: Any,
tuning_result: TuningResult,
tuning_config: TuningConfig,
validation_data: tuple[Any, Any] | None,
log_callback: Callable[[str], None] | None,
) -> None:
tune_decision_thresholds(
self.model_calculator,
model,
tuning_result,
tuning_config,
validation_data,
log_callback,
)
def _fit_and_score_candidate_fold(
self,
candidate_idx: int,
fold_idx: int,
params: dict[str, Any],
model_class: Any,
cv: Any,
X_any: Any,
y_any: Any,
X_arr: Any,
y_arr: Any,
train_idx: Any,
val_idx: Any,
metric: str,
log_callback: Callable[[str], None] | None,
preprocessing: "FoldPreprocessor | None" = None,
fold_errors: list[str] | None = None,
seed_params_overlay: dict[str, Any] | None = None,
) -> float:
return fit_and_score_candidate_fold(
candidate_idx,
fold_idx,
params,
model_class,
cv,
X_any,
y_any,
X_arr,
y_arr,
train_idx,
val_idx,
metric,
log_callback,
preprocessing,
fold_errors,
seed_params_overlay,
model_calculator=self.model_calculator,
)
def _run_grid_or_random_search(
self,
X_for_search: Any,
y_for_search: Any,
config: TuningConfig,
model_class: Any,
cv: Any,
metric: str,
progress_callback: Callable[[int, int, float | None, dict | None], None] | None,
log_callback: Callable[[str], None] | None,
preprocessing: "FoldPreprocessor | None" = None,
) -> TuningResult:
return run_grid_or_random_search(
X_for_search,
y_for_search,
config,
model_class,
cv,
metric,
progress_callback,
log_callback,
preprocessing,
model_calculator=self.model_calculator,
)
@staticmethod
def _collect_trials(searcher: Any, config: TuningConfig) -> list[dict[str, Any]]:
return _runner.collect_trials(searcher, config)
@staticmethod
def _strip_model_prefix(params: Any) -> Any:
return _runner.strip_model_prefix(params)
# ------------------------------------------------------------------
# Config + input validation (kept on the orchestrator).
# ------------------------------------------------------------------
@staticmethod
def _build_tuning_config(config: dict[str, Any] | TuningConfig) -> TuningConfig:
"""Convert a raw config dict (or an already-built TuningConfig) into a TuningConfig."""
if isinstance(config, TuningConfig):
return config
# Extract valid keys for TuningConfig
valid_keys = TuningConfig.__annotations__.keys()
filtered_config = {k: v for k, v in config.items() if k in valid_keys}
return TuningConfig(**filtered_config)
@staticmethod
def _validate_no_nan_inf(
arr: Any,
nan_msg: str,
inf_msg: str,
object_nan_msg: str,
*,
allow_nan: bool = False,
) -> None:
"""Raise ValueError if a numpy array contains NaN/Inf (numeric) or NaN (object dtype).
Many tuning errors ("No trials completed") are actually due to dirty data causing
instant failures. We catch this early to give a clear message. Object-dtype arrays
(e.g. mixed dtypes or leftover categorical/string columns that were never encoded)
are also scanned via pd.isna, since np.isnan/np.isinf raise on non-numeric dtypes.
``allow_nan`` skips the NaN checks for models that handle missing values
natively (XGBoost, LightGBM, HistGradientBoosting); Inf is still rejected.
"""
if not isinstance(arr, np.ndarray):
return
if np.issubdtype(arr.dtype, np.number):
if not allow_nan and np.isnan(arr).any():
raise ValueError(nan_msg)
if np.isinf(arr).any():
raise ValueError(inf_msg)
elif arr.dtype == object and not allow_nan and pd.isna(arr).any():
raise ValueError(object_nan_msg)
# Model classes that natively handle missing values in X; NaN must not be
# rejected for these (y still is — no estimator accepts missing targets).
_MISSING_NATIVE_MODEL_CLASSES = frozenset(
{
"XGBClassifier",
"XGBRegressor",
"LGBMClassifier",
"LGBMRegressor",
"HistGradientBoostingClassifier",
"HistGradientBoostingRegressor",
}
)
def fit(
self,
X: pd.DataFrame | SkyulfDataFrame,
y: pd.Series | Any,
config: dict[str, Any] | TuningConfig,
progress_callback: Callable[[int, int, float | None, dict | None], None] | None = None,
log_callback: Callable[[str], None] | None = None,
validation_data: tuple[pd.DataFrame | SkyulfDataFrame, pd.Series | Any] | None = None,
iteration_callback: Callable[..., None] | None = None,
preprocessing: "FoldPreprocessor | None" = None,
validation_frames: tuple[Any, Any] | None = None,
) -> Any:
"""
Fits the tuner (runs tuning).
Adapts the generic fit interface to the specific tune method.
``preprocessing`` (F-15): re-fits the given preprocessor on
each candidate fold's training rows during search, so tuning scores
stop leaking held-out rows into preprocessing statistics. The custom
``grid``/``random`` loop applies it directly; ``halving_*``/``optuna``
get it via a fold-aware meta-estimator whose ``fit`` runs the chain
inside every searcher-internal fold — safe even for chains that
resample rows or re-encode the target. Holdout tuning
(``validation_data`` set) refits the chain on the train rows only and
scores candidates against the untouched validation split when
``validation_frames`` (the pre-transform validation payload) is
supplied; frameless calls fall back to raw-payload scoring with an
explicit log instead. When set, the final best-model refit runs the
full split through the preprocessor once — the artifact serving uses.
Polars payloads are accepted for both frame pairs — the engine and
the fold-aware wrap convert them via ``to_pandas()`` with dtypes
intact.
"""
tuning_config = self._build_tuning_config(config)
# Core-only console progress: the backend always supplies its own
# callback (and never sets `progress`), so this only ever fires for
# direct SDK use.
reporter: ConsoleTrialReporter | None = None
if progress_callback is None and tuning_config.progress:
reporter = ConsoleTrialReporter()
progress_callback = reporter
# For Time Series Split, sort data chronologically (and drop the time
# column from features) before converting to numpy below - numpy has
# no column names, so this must happen while X still carries them.
# Mirrors the same fix already applied to perform_cross_validation();
# without it, tuning with cv_type="time_series_split" silently leaks
# the time column and evaluates folds out of chronological order.
if tuning_config.cv_type == "time_series_split" and hasattr(X, "columns"):
X, y = _sort_by_time(X, y, tuning_config.cv_time_column, log_callback, logger)
# Convert data to Numpy for tuning
X_np, y_np = SklearnBridge.to_sklearn((X, y))
# --- VALIDATION: Check for NaNs/Inf in Data ---
# Models with native missing-value support (XGBoost, LightGBM,
# HistGradientBoosting) accept NaN in X; forcing an Imputer on them
# would wrongly block a legitimate configuration. y is always checked.
model_cls = getattr(self.model_calculator, "model_class", None)
x_allows_nan = (
model_cls is not None
and getattr(model_cls, "__name__", "") in self._MISSING_NATIVE_MODEL_CLASSES
)
# With per-fold refit, X is the pre-transform payload: NaN there is
# legitimate when the pipeline carries an imputer, which re-runs on
# each fold's training rows before the model ever sees the data.
self._validate_no_nan_inf(
X_np,
"Input features (X) contain NaN values. Please use an 'Imputer' node before this model.",
"Input features (X) contain Infinite values. Please scale or clean your data.",
"Input features (X) contain missing/NaN values. Please use an 'Imputer' node before this model.",
allow_nan=x_allows_nan or preprocessing is not None,
)
self._validate_no_nan_inf(
y_np,
"Target variable (y) contains NaN values. Please drop rows with missing targets or impute them.",
"Target variable (y) contains Infinite values.",
"Target variable (y) contains missing/NaN values. Please drop rows with missing targets or impute them.",
)
# ----------------------------------------------
validation_data_np = None
if validation_data:
X_val, y_val = validation_data
X_val_np, y_val_np = SklearnBridge.to_sklearn((X_val, y_val))
validation_data_np = (X_val_np, y_val_np)
# Wrap the whole candidate/fold search: sklearn's ConvergenceWarning
# (raised via `warnings.warn`, not the `logging` module) would
# otherwise only reach the server's stderr, once per fold/candidate,
# and never surface to the user. Aggregate into a single summary log
# line instead of one per fold (a full grid/random search can run
# hundreds of fold fits) and re-emit any other warning category
# unchanged so existing behavior for those is preserved.
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
tuning_result = self.tune(
X_np,
y_np,
tuning_config,
progress_callback=progress_callback,
log_callback=log_callback,
validation_data=validation_data_np,
preprocessing=preprocessing,
preprocessing_frames=(X, y) if preprocessing is not None else None,
validation_frames=validation_frames,
)
convergence_count = 0
for w in caught:
if issubclass(w.category, ConvergenceWarning):
convergence_count += 1
else:
warnings.warn_explicit(w.message, w.category, w.filename, w.lineno)
if convergence_count:
conv_msg = (
f"{convergence_count} candidate fit(s) during hyperparameter search did not "
"fully converge (max_iter reached). Consider increasing max_iter, scaling "
"features, or picking a different solver."
)
logger.warning(conv_msg)
if log_callback:
log_callback(conv_msg)
# Refit the best model on the full dataset. With per-fold refit
# enabled, the full dataset is the full-split frame run once through
# the preprocessor — the same artifact the folds were scored against
# and what serving will use for predictions.
if preprocessing is not None:
X_refit_frame, y_refit_frame = preprocessing.fit_transform(X, y)
X_refit, y_refit = SklearnBridge.to_sklearn((X_refit_frame, y_refit_frame))
else:
X_refit, y_refit = X_np, y_np
model = refit_best_model(
self.model_calculator,
tuning_result,
tuning_config,
X_refit,
y_refit,
log_callback,
iteration_callback=iteration_callback,
)
# F-13: optionally search a decision threshold for a binary
# classifier on the validation split. Best-effort — a failure logs and
# leaves predict() on the default decision rule.
if tuning_config.tune_threshold:
tune_decision_thresholds(
self.model_calculator,
model,
tuning_result,
tuning_config,
validation_data,
log_callback,
)
if reporter is not None:
reporter.finish(tuning_result)
return (model, tuning_result)
@staticmethod
def _to_numpy(data: Any) -> Any:
"""Converts a pandas object to a numpy array, leaving numpy arrays unchanged."""
return data.to_numpy() if hasattr(data, "to_numpy") else data
def tune(
self,
X: Any,
y: Any,
config: TuningConfig,
progress_callback: Callable[[int, int, float | None, dict | None], None] | None = None,
log_callback: Callable[[str], None] | None = None,
validation_data: tuple[Any, Any] | None = None,
preprocessing: "FoldPreprocessor | None" = None,
preprocessing_frames: tuple[Any, Any] | None = None,
validation_frames: tuple[Any, Any] | None = None,
) -> TuningResult:
"""
Runs hyperparameter tuning.
"""
# Holdout tuning with per-fold preprocessing refit: the train and
# validation frames are concatenated (train rows masked -1 in a
# PredefinedSplit) so every strategy refits the chain on train rows
# only and scores candidates against the untouched validation split.
holdout_refit = (
preprocessing is not None
and validation_data is not None
and preprocessing_frames is not None
and validation_frames is not None
)
# 1. Prepare Estimator
# We need a base estimator. Since our Calculator wraps the class,
# we need to instantiate the underlying sklearn model with default params.
# Assuming model_calculator is SklearnCalculator
if not hasattr(self.model_calculator, "model_class"):
raise ValueError("Tuner currently only supports SklearnCalculator")
# `model_class` only on SklearnCalculator; `Any` keeps call sites type-clean.
model_class: Any = self.model_calculator.model_class
# ``default_params`` may carry structural args (e.g. an ensemble's
# resolved ``estimators``); the instantiator filters/routes them safely.
base_estimator = instantiate_model(
model_class, {**self.model_calculator.default_params, **seed_params(config)}
)
# halving/optuna searchers run their CV internally, where the engine's
# per-fold hook cannot reach; wrap preprocessing + model in one
# fit-time meta-estimator so the searcher's own folds drive a true
# per-fold refit (F-15). Preprocessing runs inside ``fit`` on the
# fold's training rows, so chains that resample rows or re-encode
# the target are safe here too.
searcher_strategy = config.strategy in ("halving_grid", "halving_random", "optuna")
wrapped = preprocessing is not None and searcher_strategy
if wrapped and preprocessing_frames is None:
# Numpy-only SDK call: the preprocessor needs named frames to
# run, so the wrap cannot be built. Keep today's behaviour —
# score the raw pre-transform payload — with an explicit log.
wrapped = False
if log_callback:
log_callback(
"Per-fold preprocessing refit skipped for this tuning strategy: "
"no named frames are available to run the preprocessing chain "
"inside the searcher's folds. Scores are computed on the raw "
"pre-transform payload."
)
if wrapped and validation_data is not None and validation_frames is None:
# Holdout tuning needs the validation rows as named frames too
# (the searcher scores them in the original pre-transform space).
# Without them the wrap would score misaligned rows, so fall back
# to raw-payload scoring with an explicit log instead.
wrapped = False
if log_callback:
log_callback(
"Per-fold preprocessing refit skipped for holdout tuning: "
"no named validation frames are available to score against. "
"Scores are computed on the raw pre-transform payload."
)
estimator: Any = base_estimator
search_config = config
if wrapped and preprocessing_frames is not None:
frame_x = preprocessing_frames[0]
feature_names = (
tuple(map(str, frame_x.columns)) if hasattr(frame_x, "columns") else None
)
estimator = Pipeline(
[
(
"model",
FoldAwareModelStep(
estimator=base_estimator,
preprocessor=preprocessing,
feature_names=feature_names,
),
)
]
)
if log_callback:
log_callback(
"Per-fold preprocessing refit runs inside the searcher via "
"the fold-aware estimator."
)
# Search-space keys must route through the pipeline to the base estimator.
search_config = replace(
config,
search_space={
f"model__estimator__{key}": values
for key, values in (config.search_space or {}).items()
},
)
# 2. Prepare Splitter
# If validation data is provided, use PredefinedSplit to train on X and validate on validation_data
# Otherwise use CV
if holdout_refit and preprocessing_frames is not None and validation_frames is not None:
cv, X_for_search, y_for_search = splitters.build_predefined_split_cv_frames(
preprocessing_frames, validation_frames
)
else:
cv, X_for_search, y_for_search = splitters.build_cv_splitter(
X, y, config, validation_data, self.model_calculator.problem_type
)
# 3. Select Search Strategy
# Handle multiclass metrics and map user-friendly names
metric = resolve_metric(config, y, self.model_calculator.problem_type)
if config.strategy in ["grid", "random"]:
# Per-fold refit needs named frames (the adapter rebuilds a real
# FeatureEngineer), not the numpy arrays used by the searchers.
# Holdout mode is excluded: the splitter stage already produced
# the concatenated train+validation frames the mask is aligned to.
if (
preprocessing is not None
and preprocessing_frames is not None
and validation_data is None
):
X_for_search, y_for_search = preprocessing_frames
# Use custom loop to support progress and log callbacks
return run_grid_or_random_search(
X_for_search,
y_for_search,
config,
model_class,
cv,
metric,
progress_callback,
log_callback,
preprocessing,
model_calculator=self.model_calculator,
)
elif config.strategy in ["halving_grid", "halving_random", "optuna"]:
# Searchers score the payload handed to them below: the raw
# pre-transform frames when wrapped (the fold-aware step encodes
# inside fit but maps predictions back), otherwise the payload
# labels. Resolve the scorer against that label space so binary
# scorers get a valid pos_label even for string targets.
scoring_y = (
preprocessing_frames[1]
if wrapped and preprocessing_frames is not None
else y_for_search
)
scoring = resolve_scorer(
metric, scoring_y, getattr(self.model_calculator, "problem_type", None)
)
if config.strategy in ["halving_grid", "halving_random"]:
searcher = _halving.build_halving_searcher(
search_config, estimator, cv, scoring, log_callback
)
else:
searcher = _optuna_strategy.build_optuna_searcher(
search_config, estimator, cv, scoring, progress_callback, log_callback
)
else:
raise_invalid_choice(
config.strategy,
("grid", "random", "halving_grid", "halving_random", "optuna"),
"tuning strategy",
)
# 4. Run Search
# The wrapped pipeline needs named frames (the adapter rebuilds a real
# FeatureEngineer); numpy conversion would strip column names. In
# holdout mode the splitter stage already produced the concatenated
# train+validation frames the PredefinedSplit mask is aligned to.
if wrapped:
if validation_data is None and preprocessing_frames is not None:
X_for_search, y_for_search = preprocessing_frames
X_arr, y_arr = X_for_search, y_for_search
else:
X_arr = self._to_numpy(X_for_search)
y_arr = self._to_numpy(y_for_search)
trial_errors = _runner.execute_search(searcher, X_arr, y_arr, config, log_callback)
# 5. Extract Results
first_trial_error = trial_errors[0] if trial_errors else None
best_params, best_score = _runner.extract_best_result(searcher, first_trial_error)
# Collect trials
trials = _runner.collect_trials(searcher, config)
if wrapped:
best_params = _runner.strip_model_prefix(best_params)
trials = [
{**trial, "params": _runner.strip_model_prefix(trial["params"])} for trial in trials
]
# Final completion log for strategies that don't emit per-trial callbacks
# (halving_grid / halving_random / optuna). The grid/random branch above
# already logs completion inside its custom loop.
_runner.log_final_completion(log_callback, config, trials, best_score, best_params)
return TuningResult(
best_params=best_params,
best_score=best_score,
n_trials=len(trials),
trials=trials,
scoring_metric=metric,
)
|