BenchmarkWrapper
Bases: BaseBenchmark
Wrapper class for model benchmarking, baseline evaluation, and result storage.
Inherits
BaseBenchmark
: Initializes parameters for benchmarking models and provides configuration for task, learners, tuning methods, HPO, and criteria.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task
|
str
|
Task for evaluation ('pocketclosure', 'pocketclosureinf', 'improvement', or 'pdgrouprevaluation'.). |
required |
learners
|
List[str]
|
List of learners to benchmark ('xgb', 'rf', 'lr' or 'mlp'). |
required |
tuning_methods
|
List[str]
|
Tuning methods for each learner ('holdout', 'cv'). |
required |
hpo_methods
|
List[str]
|
HPO methods ('hebo' or 'rs'). |
required |
criteria
|
List[str]
|
List of evaluation criteria ('f1', 'macro_f1', 'brier_score'). |
required |
encodings
|
List[str]
|
List of encodings ('one_hot' or 'target'). |
required |
sampling
|
Optional[List[str]]
|
Sampling strategies to handle class imbalance. Includes None, 'upsampling', 'downsampling', and 'smote'. |
None
|
factor
|
Optional[float]
|
Factor to apply during resampling. |
None
|
n_configs
|
int
|
Number of configurations for hyperparameter tuning. Defaults to 10. |
10
|
n_jobs
|
int
|
Number of parallel jobs for processing. |
1
|
cv_folds
|
int
|
Number of folds for cross-validation. Defaults to 10. |
10
|
racing_folds
|
Optional[int]
|
Number of racing folds for Random Search (RS). Defaults to None. |
None
|
test_seed
|
int
|
Random seed for test splitting. Defaults to 0. |
0
|
test_size
|
float
|
Proportion of data used for testing. Defaults to 0.2. |
0.2
|
val_size
|
Optional[float]
|
Size of validation set in holdout tuning. Defaults to 0.2. |
0.2
|
cv_seed
|
int
|
Random seed for cross-validation. Defaults to 0 |
0
|
mlp_flag
|
Optional[bool]
|
Enables MLP training with early stopping. Defaults to True. |
None
|
threshold_tuning
|
Optional[bool]
|
Enables threshold tuning for binary classification. Defaults to None. |
None
|
verbose
|
bool
|
If True, enables detailed logging during benchmarking. Defaults to False. |
False
|
path
|
Path
|
Path to the directory containing processed data files. Defaults to Path("data/processed"). |
Path('data/processed/processed_data.csv')
|
Attributes:
Name | Type | Description |
---|---|---|
task |
str
|
The specified task for evaluation. |
learners |
List[str]
|
List of learners to evaluate. |
tuning_methods |
List[str]
|
Tuning methods for model evaluation. |
hpo_methods |
List[str]
|
HPO methods for hyperparameter tuning. |
criteria |
List[str]
|
List of evaluation metrics. |
encodings |
List[str]
|
Encoding types for categorical features. |
sampling |
List[str]
|
Resampling strategies for class balancing. |
factor |
float
|
Resampling factor for balancing. |
n_configs |
int
|
Number of configurations for hyperparameter tuning. |
n_jobs |
int
|
Number of parallel jobs for model training. |
cv_folds |
int
|
Number of cross-validation folds. |
racing_folds |
int
|
Number of racing folds for random search. |
test_seed |
int
|
Seed for reproducible train-test splits. |
test_size |
float
|
Size of the test split. |
val_size |
float
|
Size of the validation split in holdout tuning. |
cv_seed |
int
|
Seed for cross-validation splits. |
mlp_flag |
bool
|
Indicates if MLP training with early stopping is used. |
threshold_tuning |
bool
|
Enables threshold tuning for binary classification. |
verbose |
bool
|
Enables detailed logging during benchmarking. |
path |
Path
|
Directory path for processed data. |
classification |
str
|
'binary' or 'multiclass' based on the task. |
Methods:
Name | Description |
---|---|
baseline |
Evaluates baseline models for each encoding and returns metrics. |
wrapped_benchmark |
Runs benchmarks with various learners, encodings, and tuning methods. |
save_benchmark |
Saves benchmark results to a specified directory. |
save_learners |
Saves learners to a specified directory as serialized files. |
Example
from periomod.wrapper import BenchmarkWrapper
# Initialize the BenchmarkWrapper
benchmarker = BenchmarkWrapper(
task="pocketclosure",
encodings=["one_hot", "target"],
learners=["rf", "xgb", "lr", "mlp"],
tuning_methods=["holdout", "cv"],
hpo_methods=["rs", "hebo"],
criteria=["f1", "brier_score"],
sampling=["upsampling"],
factor=2,
path="/data/processed/processed_data.csv",
)
# Run baseline benchmarking
baseline_df = benchmarker.baseline()
# Run full benchmark and retrieve results
benchmark, learners = benchmarker.wrapped_benchmark()
# Save the benchmark results
benchmarker.save_benchmark(
benchmark_df=benchmark,
path="reports/experiment/benchmark.csv",
)
# Save the trained learners
benchmarker.save_learners(learners_dict=learners, path="models/experiment")
Source code in periomod/wrapper/_wrapper.py
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 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 |
|
__init__(task, learners, tuning_methods, hpo_methods, criteria, encodings, sampling=None, factor=None, n_configs=10, n_jobs=1, cv_folds=10, racing_folds=None, test_seed=0, test_size=0.2, val_size=0.2, cv_seed=0, mlp_flag=None, threshold_tuning=None, verbose=False, path=Path('data/processed/processed_data.csv'))
¶
Initializes the BenchmarkWrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task
|
str
|
Task for evaluation ('pocketclosure', 'pocketclosureinf', 'improvement', or 'pdgrouprevaluation'.). |
required |
learners
|
List[str]
|
List of learners to benchmark ('xgb', 'rf', 'lr' or 'mlp'). |
required |
tuning_methods
|
List[str]
|
Tuning methods for each learner ('holdout', 'cv'). |
required |
hpo_methods
|
List[str]
|
HPO methods ('hebo' or 'rs'). |
required |
criteria
|
List[str]
|
List of evaluation criteria ('f1', 'macro_f1', 'brier_score'). |
required |
encodings
|
List[str]
|
List of encodings ('one_hot' or 'target'). |
required |
sampling
|
Optional[List[str]]
|
Sampling strategies to handle class imbalance. Includes None, 'upsampling', 'downsampling', and 'smote'. |
None
|
factor
|
Optional[float]
|
Factor to apply during resampling. |
None
|
n_configs
|
int
|
Number of configurations for hyperparameter tuning. Defaults to 10. |
10
|
n_jobs
|
int
|
Number of parallel jobs for processing. |
1
|
cv_folds
|
int
|
Number of folds for cross-validation. Defaults to 10. |
10
|
racing_folds
|
Optional[int]
|
Number of racing folds for Random Search (RS). Defaults to None. |
None
|
test_seed
|
int
|
Random seed for test splitting. Defaults to 0. |
0
|
test_size
|
float
|
Proportion of data used for testing. Defaults to 0.2. |
0.2
|
val_size
|
Optional[float]
|
Size of validation set in holdout tuning. Defaults to 0.2. |
0.2
|
cv_seed
|
int
|
Random seed for cross-validation. Defaults to 0 |
0
|
mlp_flag
|
Optional[bool]
|
Enables MLP training with early stopping. Defaults to True. |
None
|
threshold_tuning
|
Optional[bool]
|
Enables threshold tuning for binary classification. Defaults to None. |
None
|
verbose
|
bool
|
If True, enables detailed logging during benchmarking. Defaults to False. |
False
|
path
|
Path
|
Path to the directory containing processed data files. Defaults to Path("data/processed"). |
Path('data/processed/processed_data.csv')
|
Source code in periomod/wrapper/_wrapper.py
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 |
|
baseline()
¶
Runs baseline benchmark for each encoding type.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Combined baseline benchmark dataframe with encoding info. |
Source code in periomod/wrapper/_wrapper.py
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 |
|
save_benchmark(benchmark_df, path)
staticmethod
¶
Saves the benchmark DataFrame to the specified path as a CSV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
benchmark_df
|
DataFrame
|
The benchmark DataFrame to save. |
required |
path
|
Union[str, Path]
|
Path (including filename) where CSV file will be saved. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the benchmark DataFrame is empty. |
FileNotFoundError
|
If the parent directory of the path does not exist. |
Source code in periomod/wrapper/_wrapper.py
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 |
|
save_learners(learners_dict, path)
staticmethod
¶
Saves the learners to the specified directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
learners_dict
|
dict
|
Dictionary containing learners to save. |
required |
path
|
Union[str, Path]
|
Path to the directory where models will be saved. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the learners dictionary is empty. |
Source code in periomod/wrapper/_wrapper.py
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 |
|
wrapped_benchmark()
¶
Runs baseline and benchmarking tasks.
Returns:
Name | Type | Description |
---|---|---|
Tuple |
Tuple[DataFrame, dict]
|
Benchmark and learners used for evaluation. |
Source code in periomod/wrapper/_wrapper.py
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 |
|