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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160 | 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):
"""Store the base calculator whose estimator will be tuned.
Everything the search needs — model class, default parameters,
problem type — is read off the wrapped calculator, so one tuner
serves every sklearn-backed model node without per-model code.
"""
self.model_calculator = model_calculator
@property
def problem_type(self) -> str:
"""The wrapped calculator's problem type (classification/regression/clustering).
Delegated so splitter construction and metric/scorer resolution see
the tuner as the base model — tuning must honor the same contract.
"""
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)
def pruning_support_reason(self, config: TuningConfig) -> str | None:
"""Inspect compatibility with the legacy direct incremental Optuna loop.
Uses calculator defaults and the same estimator preparation as tuning.
Use ``pruning_support`` to include native and CV-fold pruning modes.
The current pruner selection does not affect capability; the explicit
``pruning=False`` opt-out does.
"""
if config.strategy_params.get("pruning") is False:
return "pruning=False explicitly disables incremental trial pruning"
model_class = getattr(self.model_calculator, "model_class", None)
if model_class is None:
return "The model does not expose a tunable estimator class"
optuna_config = replace(config, strategy="optuna")
estimator, search_config, _, _ = self._prepare_search_estimator(
model_class, optuna_config, None, None, None, None, None
)
return unsupported_pruning_reason(estimator, clean_search_space(search_config.search_space))
def pruning_support(
self,
config: TuningConfig,
*,
n_splits: int | None = None,
preprocessing: bool = False,
) -> dict[str, Any]:
"""Describe current pruning support without fitting or reading any data.
Graph callers supply the actual split count when a validation partition
overrides CV, and indicate whether fold preprocessing will be attached.
The selected pruner does not disable capability; the legacy opt-out does.
"""
if config.strategy_params.get("pruning") is False:
return {
"supported": False,
"mode": "none",
"reason": "pruning=False explicitly disables trial pruning",
}
model_class = getattr(self.model_calculator, "model_class", None)
if model_class is None:
return {
"supported": False,
"mode": "none",
"reason": "The model does not expose a tunable estimator class",
}
optuna_config = replace(config, strategy="optuna")
estimator, prepared, _, _ = self._prepare_search_estimator(
model_class, optuna_config, None, None, None, None, None
)
if n_splits is None:
n_splits = splitters.select_cv_by_type(config, self.problem_type).get_n_splits()
plan = resolve_pruning_plan(
estimator,
clean_search_space(prepared.search_space),
n_splits=n_splits,
preprocessing=preprocessing,
)
return {"supported": plan.mode != "none", "mode": plan.mode, "reason": plan.reason}
@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,
sample_weight: Any = None,
) -> Any:
return refit_best_model(
self.model_calculator,
tuning_result,
tuning_config,
X_np,
y_np,
log_callback,
iteration_callback,
**weight_kwargs(sample_weight),
)
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,
sample_weight: Any = 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,
**weight_kwargs(sample_weight),
)
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,
sample_weight: Any = 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,
**weight_kwargs(sample_weight),
)
@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 whose configured estimator
reports native missing-value support; 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)
def _allows_missing_features(self, config: TuningConfig) -> bool:
"""Check configured capabilities without fixing parameters that will be searched."""
model_class = getattr(self.model_calculator, "model_class", None)
if model_class is None:
return False
constructor_params, _ = split_class_weight_params(
model_class, {**self.model_calculator.default_params, **seed_params(config)}
)
estimator = instantiate_model(model_class, constructor_params)
if _estimator_allows_nan(estimator):
return True
# A search can replace a NaN-incompatible default (e.g. a regression
# tree's absolute_error criterion). Check the library baseline for
# searched axes rather than rejecting on a value that need not be used.
# Candidate fits remain authoritative; no search product is enumerated.
required_params = {
name
for name, parameter in inspect.signature(model_class).parameters.items()
if parameter.default is inspect.Parameter.empty
}
fixed_params = {
key: value
for key, value in constructor_params.items()
if key not in config.search_space or key in required_params
}
if fixed_params.keys() == constructor_params.keys():
return False
return _estimator_allows_nan(instantiate_model(model_class, fixed_params))
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,
*,
sample_weight: Any = 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.
"""
sample_weight = _prepare_tuning_weights(
sample_weight, len(X), preprocessing, self.problem_type
)
tuning_config = self._build_tuning_config(config)
original_columns = tuple(getattr(X, "columns", ()))
# 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.
X, y, validation_data, validation_frames, split_metadata, sample_weight = (
_prepare_policy_inputs(
X,
y,
tuning_config,
self.problem_type,
validation_data,
validation_frames,
preprocessing,
sample_weight,
)
)
if split_metadata is None:
X, y, validation_data, validation_frames, sample_weight = _prepare_time_series_data(
X,
y,
tuning_config,
validation_data,
validation_frames,
preprocessing,
log_callback,
sample_weight,
)
# Convert data to Numpy for tuning
X_np, y_np = SklearnBridge.to_sklearn((X, y), validate_features=preprocessing is None)
# --- VALIDATION: Check for NaNs/Inf in Data ---
# Query the installed estimator rather than a model-name allowlist:
# support can depend on its configuration (e.g. a tree's criterion).
# Each candidate's fit still enforces its own constraints, including
# restrictions not expressed in tags such as tree monotonicity.
x_allows_nan = self._allows_missing_features(tuning_config)
# 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), validate_features=preprocessing is None
)
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,
split_metadata=split_metadata,
**weight_kwargs(sample_weight),
)
_report_search_warnings(caught, log_callback)
_record_feature_exclusions(tuning_result, original_columns, X)
# 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, sample_weight = fit_preprocessor(
preprocessing, X, y, sample_weight
)
X_refit, y_refit = SklearnBridge.to_sklearn(
(X_refit_frame, y_refit_frame), validate_features=True
)
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,
**weight_kwargs(sample_weight),
)
# 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.
_finish_threshold_selection(
self.model_calculator,
model,
tuning_result,
tuning_config,
validation_data,
log_callback,
y,
y_refit,
preprocessing,
)
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 _prepare_search_estimator(
self,
model_class: Any,
config: TuningConfig,
preprocessing: FoldPreprocessor | None,
preprocessing_frames: tuple[Any, Any] | None,
validation_data: tuple[Any, Any] | None,
validation_frames: tuple[Any, Any] | None,
log_callback: Callable[[str], None] | None,
sample_weight: Any = None,
) -> tuple[Any, TuningConfig, bool, bool]:
"""Prepare the estimator and parameter routing for the requested search strategy."""
# ``default_params`` may carry structural args (e.g. an ensemble's
# resolved ``estimators``); the instantiator filters/routes them safely.
constructor_params, class_weight = split_class_weight_params(
model_class, {**self.model_calculator.default_params, **seed_params(config)}
)
base_estimator = instantiate_model(model_class, constructor_params)
# 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")
# Nonnative class weights also need a fit-time step, even without
# preprocessing: each clone must derive weights from its train fold.
weighted = (
searcher_strategy
and not constructor_accepts_class_weight(model_class)
and (
"class_weight" in self.model_calculator.default_params
or "class_weight" in config.search_space
)
)
wrapped = _can_wrap_preprocessing(
preprocessing,
searcher_strategy,
preprocessing_frames,
validation_data,
validation_frames,
log_callback,
)
estimator: Any = base_estimator
search_config = config
if wrapped or weighted or (searcher_strategy and sample_weight is not None):
estimator, search_config = _wrap_search_estimator(
base_estimator,
config,
preprocessing,
preprocessing_frames,
class_weight,
wrapped,
weighted,
log_callback,
)
return estimator, search_config, wrapped, weighted or sample_weight is not None
def _prepare_search_splitter(
self,
X: Any,
y: Any,
config: TuningConfig,
preprocessing: FoldPreprocessor | None,
preprocessing_frames: tuple[Any, Any] | None,
validation_data: tuple[Any, Any] | None,
validation_frames: tuple[Any, Any] | None,
) -> tuple[Any, Any, Any]:
"""Build holdout frame splits when available, otherwise the configured CV splitter."""
# 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
)
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
)
return cv, X_for_search, y_for_search
def _run_searcher_and_collect_results(
self,
searcher: Any,
config: TuningConfig,
metric: str,
search_data: tuple[Any, Any],
wrapped: bool,
weighted: bool,
validation_data: tuple[Any, Any] | None,
preprocessing_frames: tuple[Any, Any] | None,
log_callback: Callable[[str], None] | None,
sample_weight: Any = None,
) -> TuningResult:
"""Execute the searcher and restore public parameter names in its results."""
X_for_search, y_for_search = search_data
# 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, **weight_kwargs(sample_weight)
)
# 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 or weighted:
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,
)
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,
split_metadata: dict[str, np.ndarray] | None = None,
cv_override: Any = None,
*,
sample_weight: Any = None,
) -> TuningResult:
"""Runs hyperparameter tuning."""
sample_weight = _prepare_tuning_weights(
sample_weight, len(X), preprocessing, self.problem_type
)
X, y, validation_data, validation_frames, preprocessing_frames, sample_weight = (
_direct_time_data(
X,
y,
config,
validation_data,
validation_frames,
preprocessing_frames,
preprocessing,
log_callback,
split_metadata,
sample_weight,
)
)
X, y, preprocessing_frames, split_metadata, sample_weight = _policy_search_data(
X,
y,
config,
self.problem_type,
preprocessing,
preprocessing_frames,
split_metadata,
sample_weight,
)
if config.cv_enabled and config.cv_type == "nested_cv":
raw_x, raw_y = preprocessing_frames if preprocessing_frames is not None else (X, y)
return run_nested_search(
self,
raw_x,
raw_y,
config,
preprocessing=preprocessing,
progress_callback=progress_callback,
log_callback=log_callback,
split_metadata=split_metadata,
**weight_kwargs(sample_weight),
)
# 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
config = normalize_logistic_search_config(
model_class, self.model_calculator.default_params, config
)
estimator, search_config, wrapped, weighted = self._prepare_search_estimator(
model_class,
config,
preprocessing,
preprocessing_frames,
validation_data,
validation_frames,
log_callback,
sample_weight,
)
# 2. Prepare Splitter
# If validation data is provided, use PredefinedSplit to train on X and validate on validation_data
# Otherwise use CV
cv, X_for_search, y_for_search = self._prepare_search_splitter(
X, y, config, preprocessing, preprocessing_frames, validation_data, validation_frames
)
cv = _effective_search_cv(cv, cv_override, config, self.problem_type, y, split_metadata)
sample_weight = search_weights(sample_weight, len(X_for_search))
preflight_weights(sample_weight, cv, X_for_search, y_for_search)
# 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.
X_for_search, y_for_search = self._grid_search_frames(
(X_for_search, y_for_search), preprocessing, preprocessing_frames, validation_data
)
# 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,
**weight_kwargs(sample_weight),
)
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"]:
search_config = _halving.bound_sample_resources(search_config, len(X_for_search))
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,
weighted=sample_weight is not None,
)
else:
raise_invalid_choice(
config.strategy,
("grid", "random", "halving_grid", "halving_random", "optuna"),
"tuning strategy",
)
return self._run_searcher_and_collect_results(
searcher,
config,
metric,
(X_for_search, y_for_search),
wrapped,
weighted,
validation_data,
preprocessing_frames,
log_callback,
**weight_kwargs(sample_weight),
)
@staticmethod
def _grid_search_frames(
search_frames: tuple[Any, Any],
preprocessing: FoldPreprocessor | None,
preprocessing_frames: tuple[Any, Any] | None,
validation_data: tuple[Any, Any] | None,
) -> tuple[Any, Any]:
"""Retain named raw frames for per-fold refits outside explicit holdout mode."""
if (
preprocessing is not None
and preprocessing_frames is not None
and validation_data is None
):
return preprocessing_frames
return search_frames
|