Skip to content

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
class BenchmarkWrapper(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.

    Args:
        task (str): Task for evaluation ('pocketclosure', 'pocketclosureinf',
            'improvement', or 'pdgrouprevaluation'.).
        learners (List[str]): List of learners to benchmark ('xgb', 'rf', 'lr' or
            'mlp').
        tuning_methods (List[str]): Tuning methods for each learner ('holdout', 'cv').
        hpo_methods (List[str]): HPO methods ('hebo' or 'rs').
        criteria (List[str]): List of evaluation criteria ('f1', 'macro_f1',
            'brier_score').
        encodings (List[str]): List of encodings ('one_hot' or 'target').
        sampling (Optional[List[str]]): Sampling strategies to handle class imbalance.
            Includes None, 'upsampling', 'downsampling', and 'smote'.
        factor (Optional[float]): Factor to apply during resampling.
        n_configs (int): Number of configurations for hyperparameter tuning.
            Defaults to 10.
        n_jobs (int): Number of parallel jobs for processing.
        cv_folds (int): Number of folds for cross-validation. Defaults to 10.
        racing_folds (Optional[int]): Number of racing folds for Random Search (RS).
            Defaults to None.
        test_seed (int): Random seed for test splitting. Defaults to 0.
        test_size (float): Proportion of data used for testing. Defaults to
            0.2.
        val_size (Optional[float]): Size of validation set in holdout tuning.
            Defaults to 0.2.
        cv_seed (int): Random seed for cross-validation. Defaults to 0
        mlp_flag (Optional[bool]): Enables MLP training with early stopping.
            Defaults to True.
        threshold_tuning (Optional[bool]): Enables threshold tuning for binary
            classification. Defaults to None.
        verbose (bool): If True, enables detailed logging during benchmarking.
            Defaults to False.
        path (Path): Path to the directory containing processed data files.
            Defaults to Path("data/processed").

    Attributes:
        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:
        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")
        ```
    """

    def __init__(
        self,
        task: str,
        learners: List[str],
        tuning_methods: List[str],
        hpo_methods: List[str],
        criteria: List[str],
        encodings: List[str],
        sampling: Optional[List[Union[str, None]]] = None,
        factor: Optional[float] = None,
        n_configs: int = 10,
        n_jobs: int = 1,
        cv_folds: Optional[int] = 10,
        racing_folds: Optional[int] = None,
        test_seed: int = 0,
        test_size: float = 0.2,
        val_size: Optional[float] = 0.2,
        cv_seed: Optional[int] = 0,
        mlp_flag: Optional[bool] = None,
        threshold_tuning: Optional[bool] = None,
        verbose: bool = False,
        path: Path = Path("data/processed/processed_data.csv"),
    ) -> None:
        """Initializes the BenchmarkWrapper.

        Args:
            task (str): Task for evaluation ('pocketclosure', 'pocketclosureinf',
                'improvement', or 'pdgrouprevaluation'.).
            learners (List[str]): List of learners to benchmark ('xgb', 'rf', 'lr' or
                'mlp').
            tuning_methods (List[str]): Tuning methods for each learner
                ('holdout', 'cv').
            hpo_methods (List[str]): HPO methods ('hebo' or 'rs').
            criteria (List[str]): List of evaluation criteria ('f1', 'macro_f1',
                'brier_score').
            encodings (List[str]): List of encodings ('one_hot' or 'target').
            sampling (Optional[List[str]]): Sampling strategies to handle class
                imbalance. Includes None, 'upsampling', 'downsampling', and 'smote'.
            factor (Optional[float]): Factor to apply during resampling.
            n_configs (int): Number of configurations for hyperparameter tuning.
                Defaults to 10.
            n_jobs (int): Number of parallel jobs for processing.
            cv_folds (int): Number of folds for cross-validation. Defaults to 10.
            racing_folds (Optional[int]): Number of racing folds for Random Search (RS).
                Defaults to None.
            test_seed (int): Random seed for test splitting. Defaults to 0.
            test_size (float): Proportion of data used for testing. Defaults to
                0.2.
            val_size (Optional[float]): Size of validation set in holdout tuning.
                Defaults to 0.2.
            cv_seed (int): Random seed for cross-validation. Defaults to 0
            mlp_flag (Optional[bool]): Enables MLP training with early stopping.
                Defaults to True.
            threshold_tuning (Optional[bool]): Enables threshold tuning for binary
                classification. Defaults to None.
            verbose (bool): If True, enables detailed logging during benchmarking.
                Defaults to False.
            path (Path): Path to the directory containing processed data files.
                Defaults to Path("data/processed").
        """
        super().__init__(
            task=task,
            learners=learners,
            tuning_methods=tuning_methods,
            hpo_methods=hpo_methods,
            criteria=criteria,
            encodings=encodings,
            sampling=sampling,
            factor=factor,
            n_configs=n_configs,
            n_jobs=n_jobs,
            cv_folds=cv_folds,
            racing_folds=racing_folds,
            test_seed=test_seed,
            test_size=test_size,
            val_size=val_size,
            cv_seed=cv_seed,
            mlp_flag=mlp_flag,
            threshold_tuning=threshold_tuning,
            verbose=verbose,
            path=path,
        )
        self.classification = "multiclass" if task == "pdgrouprevaluation" else "binary"

    def baseline(self) -> pd.DataFrame:
        """Runs baseline benchmark for each encoding type.

        Returns:
            pd.DataFrame: Combined baseline benchmark dataframe with encoding info.
        """
        baseline_dfs = []

        for encoding in self.encodings:
            baseline_df = Baseline(
                task=self.task,
                encoding=encoding,
                path=self.path,
                random_state=self.test_seed,
            ).baseline()
            baseline_df["Encoding"] = encoding
            baseline_dfs.append(baseline_df)

        combined_baseline_df = pd.concat(baseline_dfs, ignore_index=True)
        column_order = ["Model", "Encoding"] + [
            col
            for col in combined_baseline_df.columns
            if col not in ["Model", "Encoding"]
        ]
        combined_baseline_df = combined_baseline_df[column_order]

        return combined_baseline_df

    def wrapped_benchmark(self) -> Tuple[pd.DataFrame, dict]:
        """Runs baseline and benchmarking tasks.

        Returns:
            Tuple: Benchmark and learners used for evaluation.
        """
        benchmarker = Benchmarker(
            task=self.task,
            learners=self.learners,
            tuning_methods=self.tuning_methods,
            hpo_methods=self.hpo_methods,
            criteria=self.criteria,
            encodings=self.encodings,
            sampling=self.sampling,
            factor=self.factor,
            n_configs=self.n_configs,
            n_jobs=self.n_jobs,
            cv_folds=self.cv_folds,
            test_size=self.test_size,
            val_size=self.val_size,
            test_seed=self.test_seed,
            cv_seed=self.cv_seed,
            mlp_flag=self.mlp_flag,
            threshold_tuning=self.threshold_tuning,
            verbose=self.verbose,
            path=self.path,
        )

        return benchmarker.run_benchmarks()

    @staticmethod
    def save_benchmark(benchmark_df: pd.DataFrame, path: Union[str, Path]) -> None:
        """Saves the benchmark DataFrame to the specified path as a CSV file.

        Args:
            benchmark_df (pd.DataFrame): The benchmark DataFrame to save.
            path (Union[str, Path]): Path (including filename) where CSV file will be
                saved.

        Raises:
            ValueError: If the benchmark DataFrame is empty.
            FileNotFoundError: If the parent directory of the path does not exist.
        """
        path = Path(path)
        if not path.is_absolute():
            path = Path.cwd() / path

        if benchmark_df.empty:
            raise ValueError("Benchmark DataFrame is empty and cannot be saved.")

        if not path.suffix == ".csv":
            path = path.with_suffix(".csv")

        os.makedirs(path.parent, exist_ok=True)
        benchmark_df.to_csv(path, index=False)
        print(f"Saved benchmark report to {path}")

    @staticmethod
    def save_learners(learners_dict: dict, path: Union[str, Path]) -> None:
        """Saves the learners to the specified directory.

        Args:
            learners_dict (dict): Dictionary containing learners to save.
            path (Union[str, Path]): Path to the directory where models will be saved.

        Raises:
            ValueError: If the learners dictionary is empty.
        """
        path = Path(path)
        if not path.is_absolute():
            path = Path.cwd() / path

        if not learners_dict:
            raise ValueError("Learners dictionary is empty and cannot be saved.")

        os.makedirs(path, exist_ok=True)

        for model_name, model in learners_dict.items():
            model_file_name = f"{model_name}.pkl"
            model_path = path / model_file_name
            joblib.dump(model, model_path)
            print(f"Saved model {model_name} to {model_path}")

__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
def __init__(
    self,
    task: str,
    learners: List[str],
    tuning_methods: List[str],
    hpo_methods: List[str],
    criteria: List[str],
    encodings: List[str],
    sampling: Optional[List[Union[str, None]]] = None,
    factor: Optional[float] = None,
    n_configs: int = 10,
    n_jobs: int = 1,
    cv_folds: Optional[int] = 10,
    racing_folds: Optional[int] = None,
    test_seed: int = 0,
    test_size: float = 0.2,
    val_size: Optional[float] = 0.2,
    cv_seed: Optional[int] = 0,
    mlp_flag: Optional[bool] = None,
    threshold_tuning: Optional[bool] = None,
    verbose: bool = False,
    path: Path = Path("data/processed/processed_data.csv"),
) -> None:
    """Initializes the BenchmarkWrapper.

    Args:
        task (str): Task for evaluation ('pocketclosure', 'pocketclosureinf',
            'improvement', or 'pdgrouprevaluation'.).
        learners (List[str]): List of learners to benchmark ('xgb', 'rf', 'lr' or
            'mlp').
        tuning_methods (List[str]): Tuning methods for each learner
            ('holdout', 'cv').
        hpo_methods (List[str]): HPO methods ('hebo' or 'rs').
        criteria (List[str]): List of evaluation criteria ('f1', 'macro_f1',
            'brier_score').
        encodings (List[str]): List of encodings ('one_hot' or 'target').
        sampling (Optional[List[str]]): Sampling strategies to handle class
            imbalance. Includes None, 'upsampling', 'downsampling', and 'smote'.
        factor (Optional[float]): Factor to apply during resampling.
        n_configs (int): Number of configurations for hyperparameter tuning.
            Defaults to 10.
        n_jobs (int): Number of parallel jobs for processing.
        cv_folds (int): Number of folds for cross-validation. Defaults to 10.
        racing_folds (Optional[int]): Number of racing folds for Random Search (RS).
            Defaults to None.
        test_seed (int): Random seed for test splitting. Defaults to 0.
        test_size (float): Proportion of data used for testing. Defaults to
            0.2.
        val_size (Optional[float]): Size of validation set in holdout tuning.
            Defaults to 0.2.
        cv_seed (int): Random seed for cross-validation. Defaults to 0
        mlp_flag (Optional[bool]): Enables MLP training with early stopping.
            Defaults to True.
        threshold_tuning (Optional[bool]): Enables threshold tuning for binary
            classification. Defaults to None.
        verbose (bool): If True, enables detailed logging during benchmarking.
            Defaults to False.
        path (Path): Path to the directory containing processed data files.
            Defaults to Path("data/processed").
    """
    super().__init__(
        task=task,
        learners=learners,
        tuning_methods=tuning_methods,
        hpo_methods=hpo_methods,
        criteria=criteria,
        encodings=encodings,
        sampling=sampling,
        factor=factor,
        n_configs=n_configs,
        n_jobs=n_jobs,
        cv_folds=cv_folds,
        racing_folds=racing_folds,
        test_seed=test_seed,
        test_size=test_size,
        val_size=val_size,
        cv_seed=cv_seed,
        mlp_flag=mlp_flag,
        threshold_tuning=threshold_tuning,
        verbose=verbose,
        path=path,
    )
    self.classification = "multiclass" if task == "pdgrouprevaluation" else "binary"

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
def baseline(self) -> pd.DataFrame:
    """Runs baseline benchmark for each encoding type.

    Returns:
        pd.DataFrame: Combined baseline benchmark dataframe with encoding info.
    """
    baseline_dfs = []

    for encoding in self.encodings:
        baseline_df = Baseline(
            task=self.task,
            encoding=encoding,
            path=self.path,
            random_state=self.test_seed,
        ).baseline()
        baseline_df["Encoding"] = encoding
        baseline_dfs.append(baseline_df)

    combined_baseline_df = pd.concat(baseline_dfs, ignore_index=True)
    column_order = ["Model", "Encoding"] + [
        col
        for col in combined_baseline_df.columns
        if col not in ["Model", "Encoding"]
    ]
    combined_baseline_df = combined_baseline_df[column_order]

    return combined_baseline_df

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
@staticmethod
def save_benchmark(benchmark_df: pd.DataFrame, path: Union[str, Path]) -> None:
    """Saves the benchmark DataFrame to the specified path as a CSV file.

    Args:
        benchmark_df (pd.DataFrame): The benchmark DataFrame to save.
        path (Union[str, Path]): Path (including filename) where CSV file will be
            saved.

    Raises:
        ValueError: If the benchmark DataFrame is empty.
        FileNotFoundError: If the parent directory of the path does not exist.
    """
    path = Path(path)
    if not path.is_absolute():
        path = Path.cwd() / path

    if benchmark_df.empty:
        raise ValueError("Benchmark DataFrame is empty and cannot be saved.")

    if not path.suffix == ".csv":
        path = path.with_suffix(".csv")

    os.makedirs(path.parent, exist_ok=True)
    benchmark_df.to_csv(path, index=False)
    print(f"Saved benchmark report to {path}")

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
@staticmethod
def save_learners(learners_dict: dict, path: Union[str, Path]) -> None:
    """Saves the learners to the specified directory.

    Args:
        learners_dict (dict): Dictionary containing learners to save.
        path (Union[str, Path]): Path to the directory where models will be saved.

    Raises:
        ValueError: If the learners dictionary is empty.
    """
    path = Path(path)
    if not path.is_absolute():
        path = Path.cwd() / path

    if not learners_dict:
        raise ValueError("Learners dictionary is empty and cannot be saved.")

    os.makedirs(path, exist_ok=True)

    for model_name, model in learners_dict.items():
        model_file_name = f"{model_name}.pkl"
        model_path = path / model_file_name
        joblib.dump(model, model_path)
        print(f"Saved model {model_name} to {model_path}")

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
def wrapped_benchmark(self) -> Tuple[pd.DataFrame, dict]:
    """Runs baseline and benchmarking tasks.

    Returns:
        Tuple: Benchmark and learners used for evaluation.
    """
    benchmarker = Benchmarker(
        task=self.task,
        learners=self.learners,
        tuning_methods=self.tuning_methods,
        hpo_methods=self.hpo_methods,
        criteria=self.criteria,
        encodings=self.encodings,
        sampling=self.sampling,
        factor=self.factor,
        n_configs=self.n_configs,
        n_jobs=self.n_jobs,
        cv_folds=self.cv_folds,
        test_size=self.test_size,
        val_size=self.val_size,
        test_seed=self.test_seed,
        cv_seed=self.cv_seed,
        mlp_flag=self.mlp_flag,
        threshold_tuning=self.threshold_tuning,
        verbose=self.verbose,
        path=self.path,
    )

    return benchmarker.run_benchmarks()