diff --git a/.gitignore b/.gitignore
index cb0a5b4..388482b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,10 +1,11 @@
test*.py
*checkpoint*
*.xml
-Fracture_*
+
*py_cache*
.*
!.gitignore
!.gitkeep
-!*best_experiment*
-*.csv
\ No newline at end of file
+*.csv
+logs.log
+experiments
diff --git a/README.md b/README.md
index 799a9cb..8fcc6e7 100644
--- a/README.md
+++ b/README.md
@@ -4,15 +4,39 @@ It uses the [HAIM multimodal dataset](https://physionet.org/content/haim-multimo
(tabular, time-series, text and images) and 11 unique sources
to perform 12 predictive tasks (10 chest pathologies, length-of-stay and 48 h mortality predictions).
-This package is our own adaptation of the [HAIM GitHub package](https://github.com/lrsoenksen/HAIM.git).
+This [HAIM GitHub package](https://github.com/MEDomics-UdeS/HAIM) is MEDomicsLab’s own adaptation of the [HAIM GitHub package](https://github.com/lrsoenksen/HAIM.git).
+This version has the same purpose with differents tools, as we are incorporating our custom PyCaretEvaluator class for the training part.
+
+The PyCaretEvaluator class is designed to streamline and enhance the model evaluation process by integrating the PyCaret library with Ray for parallel execution, allowing for efficient memory management and performance optimization. PyCaret is an open-source, low-code machine learning library in Python that simplifies the process of building, training, and deploying machine learning models. This class is particularly useful for cases involving extensive model evaluations or hyperparameter tuning across multiple cross-validation folds. By leveraging Ray, the class executes each fold in parallel, reducing computation time and improving scalability on larger datasets.
+
+## 2. How to use the package?
+The dataset used to replicate this study is publicly available on [physionet](https://physionet.org/content/haim-multimodal/1.0.1/).
+To run this package on **Python 3.11**, you need to set up a Conda environment to manage dependencies.
+
+### 2.1 Creating and Activating a Conda Environment
+
+1. **Create the environment** with the required Python version:
+
+ -
+ ```bash
+ $ conda create --name haim_env python=3.11
+ ```
+
+2. **Activate the Conda environment**:
+
+ ```bash
+ $ conda activate haim_env
+
+ ```
+
+### 2.2 Installing the requirements
+
+ ```bash
+ $ pip install -r requirements.txt
+
+ ```
+
-## 2. How to use the package ?
-The dataset used to replicate this study is publicly available in [physionet](https://physionet.org/content/haim-multimodal/1.0.1/). To run this package:
-- Download the dataset and move the file ``cxr_ic_fusion_1103.csv`` to [csvs](csvs).
-- Install the requirements under **Python 3.9.13** as following:
-```
-$ pip install requirements.txt
-```
The package can be used with different sources combinations to predict one of the 12 predictive tasks defined above. Here is a code snippet which uses one
combination of sources to predict patient's length-of-stay:
```python
@@ -66,13 +90,6 @@ run the following command:
```
$ python run_experiments.py
```
-
-> **Warning**
->
-> The HAIM experiment performs 14324 evaluations (1023 evaluations for each of the chest pathologies prediction tasks and 2047 for the length-of-stay and 48h mortality). We didn't run the experiment but we approximate the execution time to 200 days run with the current implementation using only 10 CPUs.
-
-The experiments results (metrics values and figures) will be stored in the [``experiments``](experiments) directory where the name of each folder is structured as ``TaskName_NumberOfTheExperiment``
-(ex. Fracture_25). For each prediction task, the sources combination with the best AUC will be stored in the directory ``TaskName_best_experiment``.
To reproduce the HAIM exepriment on one single predictive task, run the following command:
```
@@ -106,22 +123,23 @@ Below are the ``AUC`` values reported from our experiments compared to those rep
-Task | AUC from our experiment | AUC from the paper |
----------| -----------| ----------- |
-Fracture | 0.828 +- 0.110 | 0.838 |
-Pneumothorax| 0.811 +- 0.021 | 0.836 |
-Pneumonia | 0.871 +- 0.013 | 0.883 |
-Lung opacity | 0.797 +- 0.015 | 0.816 |
-Lung lesion | 0.829 +- 0.053 | 0.844 |
-Enlarged Cardiomediastinum | 0.877 +- 0.035 | 0.876 |
-Edema | 0.915 +- 0.007 |0.917 |
-Consolidation | 0.918 +- 0.018 | 0.929 |
-Cardiomegaly | 0.908 +- 0.004 | 0.914 |
-Atelectasis | 0.765 +- 0.013 | 0.779 |
-Length of stay | 0.932 +- 0.012 | 0.939|
-48 hours mortality | 0.907 +- 0.007 | 0.912 |
+| Task | AUC from our 2nd experiment | AUC from our 1st experiment | AUC from the paper |
+|-------------------------|-----------------------------|-----------------------------|--------------------|
+| Fracture | 0.731 +- 0.134 | 0.828 +- 0.110 | 0.838 |
+| Pneumothorax | 0.898 +- 0.012 | 0.811 +- 0.021 | 0.836 |
+| Pneumonia | 0.877 +- 0.012 | 0.871 +- 0.013 | 0.883 |
+| Lung opacity | 0.809 +- 0.012 | 0.797 +- 0.015 | 0.816 |
+| Lung lesion | 0.888 +- 0.069 | 0.829 +- 0.053 | 0.844 |
+| Enlarged Cardiomediastinum | 0.888 +- 0.019 | 0.877 +- 0.035 | 0.876 |
+| Edema | 0.915 +- 0.005 | 0.915 +- 0.007 | 0.917 |
+| Consolidation | 0.912 +- 0.015 | 0.918 +- 0.018 | 0.929 |
+| Cardiomegaly | 0.922 +- 0.005 | 0.908 +- 0.004 | 0.914 |
+| Atelectasis | 0.796 +- 0.022 | 0.765 +- 0.013 | 0.779 |
+| Length of stay | 0.959 +- 0.003 | 0.932 +- 0.012 | 0.939 |
+| 48 hours mortality | 0.960 +- 0.004 | 0.907 +- 0.007 | 0.912 |
-More statistics and metrics are reported from each of the 12 experiments above and can be found in the ``experiments`` directory. Each experiment directory is named after the task on which the prediction model was evaluated.
+
+More statistics and metrics are reported from each of the 12 experiments above and can be found in the ``results`` directory. Each experiment directory is named after the task on which the prediction model was evaluated.
> **Note**
>
@@ -132,19 +150,52 @@ More statistics and metrics are reported from each of the 12 experiments above a
We tried to reproduce the HAIM experiment and used all the 1023 possible sources combinations to predict the presence or absence of a fracture in a patient and select the one resulting in the best ``AUC``.
Below the ``AUC`` value reported from our experiments compared to the one reported in the HAIM paper.
- AUC from our experiment | AUC from the paper |
- -----------| ----------- |
-0.862 +- 0.112 | 0.838 |
+ | AUC from our experiment with PyCaret | AUC from our experiment | AUC from the paper |
+|--------------------------------------|-------------------------|---------------------|
+| 0.731 ± 0.134 | 0.862 ± 0.112 | 0.838 |
+
The above experiment can be performed using the following command
```
$ python run_experiments.py -t "Fracture"
```
-A recap of the experiment named [``Fracture_best_experiment``](experiments/Fracture_best_experiment) is generated at the end of the experiment containing more statistics and metrics values.
+A recap of the experiment (results/fracture) is generated at the end of the experiment containing :
+
+#### 1. ```{task}_results.json``` :
+
+This JSON file stores detailed results for each fold in the cross-validation process.
+For each fold, it includes:
+ - Train Results: Performance metrics obtained on the training data, captured after training and tuning the model.
+ - Test Predictions: Predictions generated by the model on the test data for this fold.
+ - Best Hyperparameters: The best hyperparameters found during tuning for this specific fold (if tuning was performed).
+This file serves as a comprehensive record of all results and configurations for each fold.
+
+#### 2. ```CP_{task}_final_metrics.csv``` :
+
+This CSV file consolidates the mean and standard deviation of key performance metrics calculated across all folds.
+Included columns:
+ - Metric: The name of each evaluation metric (e.g., AUC, F1 Score, Precision, Recall, MCC and Kappa).
+ - Mean: The average value of each metric across all folds.
+ - Std Dev: The standard deviation of each metric.
+This file gives an overall view of the model’s performance and consistency across folds.
+
+> **Note**
+>
+> The Matthews Correlation Coefficient (MCC) is a metric that evaluates how well a model’s predictions match actual outcomes, balancing correct and incorrect predictions even when classes are imbalanced.
+ Cohen’s Kappa measures the level of agreement between two raters or classifiers, showing how often they agree beyond what would be expected by chance.
+
+
+#### 3. ```best_model_fold_X.pkl``` (where X is the fold number):
+
+For each fold, the best-performing model is saved as a .pkl file.
+Each model file can be reloaded independently if further analysis or testing is needed.
+Saving the best models for each fold allows you to compare models or even ensemble them if desired.
+
+All the results can be accessed via this link: https://usherbrooke-my.sharepoint.com/:f:/g/personal/kalm7073_usherbrooke_ca/EtKnOhTN1kdJmRbkziHYX9EBkgMSWzhfvXMV4lb_fZw3uQ?e=Olejde.
## 5. Future work
-The next step of our package is to regenerate the embeddings for each source type. For each modality (tabular, time-series, image, text), we will also explore new embeddings generators.
+This adaptation aims to evaluate HAIM with PyCaret to better understand its effectiveness within the MEDomicsLab platform. Moving forward, we plan to test additional task variations to further optimize performance.
## Project Tree
```
@@ -155,10 +206,8 @@ The next step of our package is to regenerate the embeddings for each source typ
│ ├── data
│ │ ├── constants.py <- Constants related to the HAIM study
│ │ ├── datasets.py <- Custom dataset implementation for the HAIM study
-│ │ └── sampling.py <- Samples the dataset to test, train and validation
│ ├── evaluation
-│ │ ├── tuning.py <- Hyper-parameters optimizations using different optimizers
-│ │ └── evaluating.py <- Skeleton of each experiment process
+│ │ └── pycaret_evaluator.py <- Skeleton of each experiment process
│ └── utils
│ │ └── metric_scores.py <- Custom metrics implementations and wrappers
├── requirements.txt <- All the requirements to install to run the project
diff --git a/notebooks/48h-mortality.ipynb b/notebooks/48h-mortality.ipynb
index d92896b..60b81c9 100644
--- a/notebooks/48h-mortality.ipynb
+++ b/notebooks/48h-mortality.ipynb
@@ -49,14 +49,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -71,7 +68,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -97,7 +94,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -110,86 +107,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "0d9f8354",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "2fe30725",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6abdd3c8",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "f935bbb8",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "3ceec6b8",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "114f173f",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "8d6cb174",
"metadata": {},
"outputs": [],
@@ -198,13 +126,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -244,169 +166,301 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name=''\n",
- " evaluation_name='48h mortality'\n",
- " )\n",
- "evaluation.evaluate()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "
\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0 +- 0.0 | \n",
- " 0.0005 +- 0.0004 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.9737 +- 0.002 | \n",
- " 0.5182 +- 0.0037 | \n",
- " 0.1896 +- 0.0196 | \n",
- " 0.0363 +- 0.0074 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.9066 +- 0.0072 | \n",
- " 0.0195 +- 0.0011 | \n",
- " 0.1098 +- 0.0052 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.912 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.889 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9737 +- 0.002 0.5182 +- 0.0037 0.1896 +- 0.0196 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.0363 +- 0.0074 1.0 +- 0.0 0.9066 +- 0.0072 \n",
- "HAIM -- -- 0.912 \n",
- "NON_HAIM -- -- 0.889 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0 +- 0.0 0.0005 +- 0.0004 \n",
- "test_metrics 0.0195 +- 0.0011 0.1098 +- 0.0052 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-10-04 13:54:33,961\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Outer fold 1\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(raylet)\u001b[0m Spilled 3869 MiB, 2 objects, write throughput 589 MiB/s. Set RAY_verbose_spill_logs=0 to disable this message.\n",
+ "\u001b[36m(raylet)\u001b[0m Spilled 7738 MiB, 4 objects, write throughput 583 MiB/s.\n",
+ "\u001b[36m(raylet)\u001b[0m Spilled 11607 MiB, 5 objects, write throughput 579 MiB/s.\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Configuring PyCaret for outer fold 1\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.84s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [13:23<04:57, 297.24s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 0.9806 0.9568 0.3675 0.8971 0.5214 0.5132 0.5673\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 1 0.9846 0.9536 0.3897 0.8983 0.5436 0.5370 0.5861\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 2 0.9844 0.9631 0.4082 0.9524 0.5714 0.5648 0.6181\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 3 0.9813 0.9470 0.3772 0.9403 0.5385 0.5307 0.5892\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 4 0.9832 0.9540 0.4177 0.9296 0.5764 0.5691 0.6170\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Mean 0.9828 0.9549 0.3921 0.9235 0.5503 0.5430 0.5955\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Std 0.0016 0.0052 0.0187 0.0223 0.0207 0.0211 0.0195\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 Extreme Gradient Boosting 0.9819 0.971 ... 0.5699 0.5621 0.6103\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Outer fold 2\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(raylet)\u001b[0m Spilled 19844 MiB, 11 objects, write throughput 498 MiB/s.\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Configuring PyCaret for outer fold 2\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.78s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [13:04<04:50, 290.25s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 0.9801 0.9662 0.3846 0.9589 0.5490 0.5407 0.6006\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 1 0.9854 0.9470 0.4046 0.8983 0.5579 0.5516 0.5975\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 2 0.9820 0.9571 0.3469 0.8644 0.4951 0.4877 0.5412\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 3 0.9825 0.9588 0.4438 0.9146 0.5976 0.5898 0.6304\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 4 0.9846 0.9611 0.4333 0.9420 0.5936 0.5868 0.6333\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Mean 0.9829 0.9580 0.4027 0.9157 0.5587 0.5513 0.6006\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Std 0.0019 0.0063 0.0348 0.0331 0.0371 0.0372 0.0332\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 Extreme Gradient Boosting 0.9824 0.9648 ... 0.5644 0.5568 0.6088\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Configuring PyCaret for outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.87s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [12:36<04:39, 279.82s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 0.9792 0.9500 0.3062 0.8448 0.4495 0.4413 0.5014\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 1 0.9863 0.9555 0.4730 0.9859 0.6393 0.6332 0.6780\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 2 0.9844 0.9686 0.4615 0.9231 0.6154 0.6083 0.6467\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 3 0.9814 0.9443 0.4057 0.9595 0.5703 0.5624 0.6175\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 4 0.9814 0.9446 0.3113 0.9400 0.4677 0.4606 0.5352\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Mean 0.9826 0.9526 0.3915 0.9307 0.5484 0.5412 0.5958\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Std 0.0025 0.0090 0.0713 0.0477 0.0768 0.0773 0.0669\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 Extreme Gradient Boosting 0.985 0.974 ... 0.624 0.6173 0.6601\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Configuring PyCaret for outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.90s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [12:37<04:40, 280.19s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 0.9825 0.9562 0.3812 0.9683 0.5471 0.5399 0.6018\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 1 0.9849 0.9665 0.4286 0.9545 0.5915 0.5850 0.6343\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 2 0.9806 0.9432 0.3013 0.9400 0.4563 0.4491 0.5263\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 3 0.9811 0.9359 0.3943 0.9583 0.5587 0.5508 0.6083\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 4 0.9828 0.9433 0.3733 0.9180 0.5308 0.5236 0.5795\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Mean 0.9824 0.9490 0.3757 0.9478 0.5369 0.5297 0.5900\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Std 0.0015 0.0109 0.0417 0.0174 0.0449 0.0450 0.0363\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 Extreme Gradient Boosting 0.9837 0.9785 ... 0.5905 0.5833 0.6267\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.89s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [12:36<04:39, 279.90s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 0.9811 0.9537 0.3636 0.9375 0.5240 0.5163 0.5775\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 1 0.9849 0.9607 0.4345 0.9265 0.5915 0.5849 0.6289\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 2 0.9807 0.9553 0.3765 0.9275 0.5356 0.5275 0.5843\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 3 0.9811 0.9491 0.3681 0.9091 0.5240 0.5161 0.5719\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 4 0.9837 0.9530 0.4214 0.9710 0.5877 0.5807 0.6341\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Mean 0.9823 0.9544 0.3928 0.9343 0.5526 0.5451 0.5993\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Std 0.0017 0.0038 0.0293 0.0205 0.0306 0.0311 0.0266\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m 0 Extreme Gradient Boosting 0.9853 0.9745 ... 0.6024 0.5959 0.6407\n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=631326)\u001b[0m [1 rows x 8 columns]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\u001b[33m(raylet)\u001b[0m [2024-10-07 08:24:39,646 E 631211 631211] (raylet) node_manager.cc:3065: 1 Workers (tasks / actors) killed due to memory pressure (OOM), 0 Workers crashed due to other reasons at node (ID: 4a76051cba771f3e77cb11a71acab1dc5b4773f9ce3f3946b8921878, IP: 10.44.86.85) over the last time period. To see more information about the Workers killed on this node, use `ray logs raylet.out -ip 10.44.86.85`\n",
+ "\u001b[33m(raylet)\u001b[0m \n",
+ "\u001b[33m(raylet)\u001b[0m Refer to the documentation on how to address the out of memory issue: https://docs.ray.io/en/latest/ray-core/scheduling/ray-oom-prevention.html. Consider provisioning more memory on this node or reducing task parallelism by requesting more CPUs per task. To adjust the kill threshold, set the environment variable `RAY_memory_usage_threshold` when starting Ray. To disable worker killing, set the environment variable `RAY_memory_monitor_refresh_ms` to zero.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.98066 0.001385\n",
+ "1 AUC 0.96012 0.004346\n",
+ "2 Recall 0.35174 0.034586\n",
+ "3 Prec. 0.94114 0.021459\n",
+ "4 F1 0.51146 0.038680\n",
+ "5 Kappa 0.50372 0.038772\n",
+ "6 MCC 0.56860 0.032578\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/48h mortality', constants.MORTALITY)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"Mortality\", experiment_name=\"CP_Mortality\", filepath=\"./results/mortality\")\n",
+ "\n",
+ "# Model training\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='kfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='kfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "726f3c65",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -418,7 +472,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/atelectasis.ipynb b/notebooks/atelectasis.ipynb
index a94bc92..c286de5 100644
--- a/notebooks/atelectasis.ipynb
+++ b/notebooks/atelectasis.ipynb
@@ -56,14 +56,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -78,7 +75,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -104,7 +101,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -117,86 +114,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "cae73927",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "3a01b40e",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "ab601313",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "5a9141d4",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "0e83e476",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "8a507d0a",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "b45ae204",
"metadata": {},
"outputs": [],
@@ -205,13 +133,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -251,168 +173,270 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Atelectasis'\n",
- " )\n",
- "evaluation.evaluate()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0005 +- 0.0002 | \n",
- " 0.0097 +- 0.0032 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.9797 +- 0.0044 | \n",
- " 0.6456 +- 0.0162 | \n",
- " 0.5407 +- 0.0293 | \n",
- " 0.9973 +- 0.0013 | \n",
- " 0.294 +- 0.0322 | \n",
- " 0.7654 +- 0.0132 | \n",
- " 0.0186 +- 0.0046 | \n",
- " 0.0974 +- 0.0243 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.779 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.767 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9797 +- 0.0044 0.6456 +- 0.0162 0.5407 +- 0.0293 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9973 +- 0.0013 0.294 +- 0.0322 0.7654 +- 0.0132 \n",
- "HAIM -- -- 0.779 \n",
- "NON_HAIM -- -- 0.767 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0005 +- 0.0002 0.0097 +- 0.0032 \n",
- "test_metrics 0.0186 +- 0.0046 0.0974 +- 0.0243 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-10-23 10:10:30,609\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Outer fold 1\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Train indices: [ 1 2 3 ... 15209 15210 15212]\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Test indices: [ 0 4 7 ... 15194 15203 15211]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=19474)\u001b[0m Configuring PyCaret for outer fold 2\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m Outer fold 4\u001b[32m [repeated 3x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)\u001b[0m\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m Train indices: [ 0 1 3 ... 15210 15211 15212]\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m Test indices: [ 2 6 9 ... 15197 15198 15206]\u001b[32m [repeated 3x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.27s/it]\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "Processing: 75%|███████▌ | 3/4 [04:24<01:37, 97.86s/it]\u001b[32m [repeated 4x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=19483)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m 0 0.9764 0.7349 0.9995 0.9768 0.9880 0.1442 0.2510\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m 1 0.9795 0.7903 1.0000 0.9794 0.9896 0.2806 0.4040\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m 2 0.9810 0.8725 1.0000 0.9809 0.9904 0.3671 0.4741\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m 3 0.9805 0.8576 1.0000 0.9804 0.9901 0.3392 0.4519\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m 4 0.9795 0.8038 0.9995 0.9799 0.9896 0.3271 0.4254\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m Mean 0.9794 0.8119 0.9998 0.9795 0.9895 0.2916 0.4013\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m Std 0.0016 0.0494 0.0003 0.0014 0.0008 0.0788 0.0788\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m Configuring PyCaret for outer fold 4\u001b[32m [repeated 3x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n",
+ "Processing: 75%|███████▌ | 3/4 [04:28<01:39, 99.27s/it]\u001b[32m [repeated 3x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=19483)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=19474)\u001b[0m 0 0.9790 0.7633 1.0000 0.9789 0.9893 0.2756 0.3998\n",
+ "\u001b[36m(run_fold pid=19474)\u001b[0m 1 0.9815 0.8122 1.0000 0.9814 0.9906 0.3940 0.4953\n",
+ "\u001b[36m(run_fold pid=19474)\u001b[0m 2 0.9795 0.7988 1.0000 0.9794 0.9896 0.2806 0.4040\n",
+ "\u001b[36m(run_fold pid=19474)\u001b[0m 3 0.9815 0.8443 1.0000 0.9814 0.9906 0.3940 0.4953\n",
+ "\u001b[36m(run_fold pid=19474)\u001b[0m 4 0.9825 0.8213 0.9989 0.9834 0.9911 0.4929 0.5511\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m 0 0.9790 0.7845 0.9995 0.9794 0.9893 0.2991 0.4013\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m 1 0.9779 0.7262 1.0000 0.9779 0.9888 0.1849 0.3192\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m 2 0.9779 0.7824 0.9995 0.9784 0.9888 0.2132 0.3225\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m 3 0.9795 0.8090 1.0000 0.9794 0.9896 0.2806 0.4040\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m 4 0.9820 0.8483 0.9995 0.9824 0.9909 0.4548 0.5302\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \u001b[32m [repeated 3x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 0 0.9790 0.7848 1.0000 0.9789 0.9893 0.2756 0.3998\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 1 0.9810 0.7670 0.9989 0.9819 0.9903 0.4057 0.4785\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 2 0.9836 0.8641 0.9995 0.9839 0.9916 0.5085 0.5730\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 3 0.9810 0.7881 1.0000 0.9809 0.9904 0.3671 0.4741\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 4 0.9800 0.7511 1.0000 0.9799 0.9898 0.3333 0.4472\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Fold \u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Mean 0.9809 0.7910 0.9997 0.9811 0.9903 0.3780 0.4745\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Std 0.0015 0.0389 0.0004 0.0017 0.0008 0.0780 0.0566\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 0 Extreme Gradient Boosting 0.9796 0.8197 ... 0.9897 0.3057 0.4248\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Train indices: [ 0 1 2 ... 15210 15211 15212]\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Test indices: [ 3 13 17 ... 15202 15207 15209]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.24s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=19474)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=19474)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=19474)\u001b[0m 0 Extreme Gradient Boosting 0.9793 0.8089 ... 0.9895 0.2869 0.4093\n",
+ "\u001b[36m(run_fold pid=19474)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=19474)\u001b[0m [1 rows x 8 columns]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 75%|███████▌ | 3/4 [04:02<01:29, 89.84s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 0 0.9790 0.8044 0.9989 0.9799 0.9893 0.3212 0.4063\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 1 0.9815 0.7720 1.0000 0.9814 0.9906 0.3940 0.4953\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 2 0.9815 0.8534 1.0000 0.9814 0.9906 0.3940 0.4953\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 3 0.9800 0.8465 0.9995 0.9804 0.9898 0.3329 0.4299\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 4 0.9815 0.7715 1.0000 0.9814 0.9906 0.4132 0.5103\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Mean 0.9807 0.8096 0.9997 0.9809 0.9902 0.3710 0.4674\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Std 0.0011 0.0351 0.0004 0.0006 0.0005 0.0368 0.0413\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m 0 Extreme Gradient Boosting 0.9836 0.869 ... 0.9916 0.5035 0.58\n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=19483)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m 0 Extreme Gradient Boosting 0.9809 0.828 ... 0.9903 0.3638 0.4715\n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=19469)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m 0 Extreme Gradient Boosting 0.9799 0.8169 ... 0.9898 0.3092 0.4277\n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=19477)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.97866 0.000805\n",
+ "1 AUC 0.79646 0.022589\n",
+ "2 Recall 0.99990 0.000224\n",
+ "3 Prec. 0.97870 0.000837\n",
+ "4 F1 0.98916 0.000410\n",
+ "5 Kappa 0.26126 0.050434\n",
+ "6 MCC 0.38298 0.041548\n",
+ "Best hyperparameters across all folds: objective binary:logistic\n",
+ "base_score NaN\n",
+ "booster gbtree\n",
+ "callbacks NaN\n",
+ "colsample_bylevel NaN\n",
+ "colsample_bynode NaN\n",
+ "colsample_bytree NaN\n",
+ "device cpu\n",
+ "early_stopping_rounds NaN\n",
+ "enable_categorical False\n",
+ "eval_metric NaN\n",
+ "feature_types NaN\n",
+ "gamma NaN\n",
+ "grow_policy NaN\n",
+ "importance_type NaN\n",
+ "interaction_constraints NaN\n",
+ "learning_rate 0.1\n",
+ "max_bin NaN\n",
+ "max_cat_threshold NaN\n",
+ "max_cat_to_onehot NaN\n",
+ "max_delta_step NaN\n",
+ "max_depth 5\n",
+ "max_leaves NaN\n",
+ "min_child_weight NaN\n",
+ "missing NaN\n",
+ "monotone_constraints NaN\n",
+ "multi_strategy NaN\n",
+ "n_estimators 300.0\n",
+ "n_jobs 1.0\n",
+ "num_parallel_tree NaN\n",
+ "random_state 42.0\n",
+ "reg_alpha NaN\n",
+ "reg_lambda NaN\n",
+ "sampling_method NaN\n",
+ "scale_pos_weight NaN\n",
+ "subsample NaN\n",
+ "tree_method auto\n",
+ "validate_parameters NaN\n",
+ "verbosity 0.0\n",
+ "Name: 0, dtype: object\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Atelectasis', constants.ATELECTASIS)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"Atelectasis\", experiment_name=\"CP_Atelectasis\", filepath=\"./results/atelectasis\")\n",
+ "\n",
+ "# Model training\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -424,7 +448,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/cardiomegaly.ipynb b/notebooks/cardiomegaly.ipynb
index 5b85801..347cb9c 100644
--- a/notebooks/cardiomegaly.ipynb
+++ b/notebooks/cardiomegaly.ipynb
@@ -57,14 +57,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -79,7 +76,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -105,7 +102,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -118,86 +115,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "16aaaba3",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b45111d8",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "06e48fbf",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6a883696",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "c7928244",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "ad7c9d6d",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "9ba42585",
"metadata": {},
"outputs": [],
@@ -206,13 +134,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -252,168 +174,270 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Cardiomegaly'\n",
- " )\n",
- "evaluation.evaluate()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0003 +- 0.0 | \n",
- " 0.0089 +- 0.001 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.8826 +- 0.0071 | \n",
- " 0.814 +- 0.0086 | \n",
- " 0.8079 +- 0.0098 | \n",
- " 0.9125 +- 0.0091 | \n",
- " 0.7155 +- 0.0197 | \n",
- " 0.908 +- 0.0038 | \n",
- " 0.0736 +- 0.0042 | \n",
- " 0.2708 +- 0.0133 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.914 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.912 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.8826 +- 0.0071 0.814 +- 0.0086 0.8079 +- 0.0098 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9125 +- 0.0091 0.7155 +- 0.0197 0.908 +- 0.0038 \n",
- "HAIM -- -- 0.914 \n",
- "NON_HAIM -- -- 0.912 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0003 +- 0.0 0.0089 +- 0.001 \n",
- "test_metrics 0.0736 +- 0.0042 0.2708 +- 0.0133 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-10-24 08:49:09,601\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Outer fold 1\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Train indices: [ 0 3 4 ... 18565 18568 18570]\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Test indices: [ 1 2 9 ... 18566 18567 18569]\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Configuring PyCaret for outer fold 1\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Outer fold 3\u001b[32m [repeated 2x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)\u001b[0m\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Train indices: [ 0 1 2 ... 18567 18568 18569]\u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Test indices: [ 6 18 19 ... 18542 18549 18570]\u001b[32m [repeated 2x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:01<00:04, 1.43s/it]\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "Processing: 75%|███████▌ | 3/4 [07:34<02:48, 168.01s/it]\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ " \n",
+ "Processing: 75%|███████▌ | 3/4 [07:36<02:49, 169.04s/it]\u001b[32m [repeated 2x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 0 0.9066 0.9091 0.9757 0.9191 0.9465 0.5803 0.5970\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 1 0.9011 0.9069 0.9737 0.9151 0.9435 0.5528 0.5703\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 2 0.9112 0.9187 0.9801 0.9203 0.9493 0.5974 0.6170\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 3 0.9016 0.9124 0.9747 0.9147 0.9438 0.5530 0.5714\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 4 0.9045 0.9044 0.9727 0.9193 0.9453 0.5736 0.5880\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Mean 0.9050 0.9103 0.9754 0.9177 0.9457 0.5714 0.5887\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Std 0.0037 0.0049 0.0026 0.0023 0.0021 0.0170 0.0174\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Configuring PyCaret for outer fold 3\u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 0 0.9032 0.9100 0.9752 0.9160 0.9447 0.5618 0.5798\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 1 0.9024 0.9072 0.9727 0.9171 0.9441 0.5626 0.5781\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 2 0.9108 0.9062 0.9796 0.9202 0.9490 0.5961 0.6152\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 3 0.9100 0.9134 0.9796 0.9194 0.9486 0.5912 0.6109\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 4 0.9171 0.9188 0.9846 0.9228 0.9527 0.6210 0.6431\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m 0 0.9062 0.9003 0.9786 0.9163 0.9465 0.5711 0.5921\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m 1 0.8965 0.8926 0.9687 0.9142 0.9407 0.5374 0.5516\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m 2 0.9032 0.9175 0.9801 0.9122 0.9449 0.5499 0.5752\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m 3 0.9070 0.9123 0.9767 0.9187 0.9468 0.5806 0.5982\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m 4 0.9159 0.9202 0.9821 0.9235 0.9519 0.6188 0.6381\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Fold \u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Mean 0.9058 0.9086 0.9773 0.9170 0.9462 0.5716 0.5910\u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Std 0.0063 0.0105 0.0046 0.0039 0.0036 0.0281 0.0285\u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 0 Extreme Gradient Boosting 0.916 0.9307 ... 0.9519 0.6255 0.6413\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Outer fold 4\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Train indices: [ 1 2 4 ... 18567 18569 18570]\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Test indices: [ 0 3 11 ... 18560 18561 18568]\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Configuring PyCaret for outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ " \u001b[32m [repeated 2x across cluster]\u001b[0m\n",
+ "Processing: 25%|██▌ | 1/4 [00:01<00:04, 1.38s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [07:38<02:49, 169.54s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 0 0.9062 0.9173 0.9782 0.9167 0.9464 0.5723 0.5925\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 1 0.9024 0.9038 0.9816 0.9102 0.9446 0.5409 0.5695\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 2 0.9011 0.9001 0.9811 0.9093 0.9439 0.5344 0.5630\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 3 0.9079 0.9155 0.9732 0.9224 0.9471 0.5930 0.6060\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 4 0.9074 0.9029 0.9752 0.9204 0.9470 0.5851 0.6007\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Mean 0.9050 0.9079 0.9779 0.9158 0.9458 0.5651 0.5863\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Std 0.0027 0.0070 0.0033 0.0052 0.0013 0.0235 0.0171\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 0 Extreme Gradient Boosting 0.9192 0.9317 ... 0.9538 0.6356 0.6548\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Train indices: [ 0 1 2 ... 18568 18569 18570]\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Test indices: [ 5 8 10 ... 18556 18563 18565]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:04, 1.35s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m 0 Extreme Gradient Boosting 0.9152 0.9322 ... 0.9516 0.6125 0.6347\n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=124833)\u001b[0m [1 rows x 8 columns]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 75%|███████▌ | 3/4 [07:12<02:39, 159.89s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 0 0.9163 0.9228 0.9762 0.9287 0.9518 0.6339 0.6456\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 1 0.9058 0.9145 0.9796 0.9151 0.9463 0.5663 0.5890\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 2 0.9028 0.9031 0.9762 0.9148 0.9445 0.5569 0.5766\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 3 0.9058 0.9070 0.9826 0.9128 0.9464 0.5592 0.5868\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 4 0.9121 0.9261 0.9727 0.9272 0.9494 0.6164 0.6269\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Mean 0.9085 0.9147 0.9775 0.9197 0.9477 0.5866 0.6050\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Std 0.0049 0.0088 0.0034 0.0068 0.0026 0.0322 0.0265\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m 0 Extreme Gradient Boosting 0.9179 0.9388 ... 0.953 0.6311 0.649\n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=124826)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m 0 Extreme Gradient Boosting 0.9152 0.928 ... 0.9514 0.6203 0.637\n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=124832)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.90928 0.002180\n",
+ "1 AUC 0.92242 0.004915\n",
+ "2 Recall 0.98034 0.003454\n",
+ "3 Prec. 0.91816 0.001760\n",
+ "4 F1 0.94824 0.001324\n",
+ "5 Kappa 0.58570 0.008514\n",
+ "6 MCC 0.60712 0.009794\n",
+ "Best hyperparameters across all folds: objective binary:logistic\n",
+ "base_score NaN\n",
+ "booster gbtree\n",
+ "callbacks NaN\n",
+ "colsample_bylevel NaN\n",
+ "colsample_bynode NaN\n",
+ "colsample_bytree NaN\n",
+ "device cpu\n",
+ "early_stopping_rounds NaN\n",
+ "enable_categorical False\n",
+ "eval_metric NaN\n",
+ "feature_types NaN\n",
+ "gamma NaN\n",
+ "grow_policy NaN\n",
+ "importance_type NaN\n",
+ "interaction_constraints NaN\n",
+ "learning_rate 0.1\n",
+ "max_bin NaN\n",
+ "max_cat_threshold NaN\n",
+ "max_cat_to_onehot NaN\n",
+ "max_delta_step NaN\n",
+ "max_depth 8\n",
+ "max_leaves NaN\n",
+ "min_child_weight NaN\n",
+ "missing NaN\n",
+ "monotone_constraints NaN\n",
+ "multi_strategy NaN\n",
+ "n_estimators 300\n",
+ "n_jobs 1\n",
+ "num_parallel_tree NaN\n",
+ "random_state 42\n",
+ "reg_alpha NaN\n",
+ "reg_lambda NaN\n",
+ "sampling_method NaN\n",
+ "scale_pos_weight NaN\n",
+ "subsample NaN\n",
+ "tree_method auto\n",
+ "validate_parameters NaN\n",
+ "verbosity 0\n",
+ "Name: 0, dtype: object\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Cardiomegaly', constants.CARDIOMEGALY)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"Cardiomegaly\", experiment_name=\"CP_Cardiomegaly\", filepath=\"./results/cardiomegaly\")\n",
+ "\n",
+ "# Model training\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -425,7 +449,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/consolidation.ipynb b/notebooks/consolidation.ipynb
index 2b596ce..81e0c7b 100644
--- a/notebooks/consolidation.ipynb
+++ b/notebooks/consolidation.ipynb
@@ -56,14 +56,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -78,7 +75,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -104,7 +101,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -117,86 +114,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "16aaaba3",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b45111d8",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "06e48fbf",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6a883696",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "c7928244",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "ad7c9d6d",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "9ba42585",
"metadata": {},
"outputs": [],
@@ -205,13 +133,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -251,168 +173,257 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Consolidation'\n",
- " )\n",
- "evaluation.evaluate()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0009 +- 0.0013 | \n",
- " 0.0151 +- 0.0135 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.8712 +- 0.0233 | \n",
- " 0.8451 +- 0.0193 | \n",
- " 0.8437 +- 0.0197 | \n",
- " 0.8856 +- 0.0298 | \n",
- " 0.8046 +- 0.0383 | \n",
- " 0.9181 +- 0.0183 | \n",
- " 0.0794 +- 0.0156 | \n",
- " 0.2927 +- 0.0787 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.929 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.92 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.8712 +- 0.0233 0.8451 +- 0.0193 0.8437 +- 0.0197 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.8856 +- 0.0298 0.8046 +- 0.0383 0.9181 +- 0.0183 \n",
- "HAIM -- -- 0.929 \n",
- "NON_HAIM -- -- 0.92 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0009 +- 0.0013 0.0151 +- 0.0135 \n",
- "test_metrics 0.0794 +- 0.0156 0.2927 +- 0.0787 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-09-26 15:25:14,862\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8266 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Outer fold 2\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Configuring PyCaret for outer fold 2\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Outer fold 1\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:02, 1.28it/s]\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [02:47<01:02, 62.04s/it]\u001b[32m [repeated 2x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 0 0.8986 0.8979 0.9699 0.9111 0.9396 0.6263 0.6383\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 1 0.8899 0.9118 0.9720 0.9004 0.9349 0.5823 0.6006\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 2 0.9335 0.9586 0.9763 0.9439 0.9598 0.7663 0.7700\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 3 0.9037 0.9242 0.9763 0.9116 0.9429 0.6379 0.6535\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 4 0.8914 0.8882 0.9591 0.9119 0.9349 0.6098 0.6169\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Mean 0.9034 0.9161 0.9707 0.9158 0.9424 0.6445 0.6559\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Std 0.0158 0.0245 0.0064 0.0147 0.0092 0.0637 0.0599\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Configuring PyCaret for outer fold 1\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n",
+ "Processing: 75%|███████▌ | 3/4 [02:48<01:02, 62.39s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 0 0.8794 0.8812 0.9548 0.9024 0.9279 0.5607 0.5692\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 1 0.8934 0.9233 0.9720 0.9040 0.9368 0.5989 0.6154\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 2 0.8967 0.9169 0.9656 0.9126 0.9383 0.6210 0.6305\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 3 0.9124 0.9516 0.9741 0.9224 0.9476 0.6828 0.6923\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 4 0.9019 0.9289 0.9720 0.9130 0.9415 0.6390 0.6513\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Mean 0.8968 0.9204 0.9677 0.9109 0.9384 0.6205 0.6317\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Std 0.0108 0.0228 0.0070 0.0072 0.0064 0.0407 0.0406\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 0 Extreme Gradient Boosting 0.8914 0.9072 ... 0.9353 0.5994 0.611\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Outer fold 3\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Configuring PyCaret for outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ " \n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:01, 1.56it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 0 Extreme Gradient Boosting 0.888 0.9128 ... 0.933 0.5942 0.6014\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Outer fold 4\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Configuring PyCaret for outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:01, 1.55it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [02:29<00:55, 55.35s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 0 0.8864 0.9067 0.9613 0.9049 0.9322 0.5828 0.5932\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 1 0.9126 0.9244 0.9699 0.9261 0.9475 0.6879 0.6945\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 2 0.8809 0.9005 0.9699 0.8931 0.9299 0.5390 0.5600\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 3 0.9177 0.9438 0.9828 0.9214 0.9511 0.6931 0.7078\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 4 0.9054 0.9221 0.9806 0.9100 0.9440 0.6433 0.6623\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Mean 0.9006 0.9195 0.9729 0.9111 0.9409 0.6292 0.6435\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Std 0.0145 0.0151 0.0079 0.0118 0.0084 0.0600 0.0576\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n",
+ "Processing: 75%|███████▌ | 3/4 [02:28<00:54, 54.91s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 0 0.9038 0.9265 0.9720 0.9150 0.9426 0.6470 0.6585\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 1 0.8899 0.9060 0.9763 0.8972 0.9351 0.5752 0.5985\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 2 0.8967 0.9256 0.9720 0.9076 0.9387 0.6116 0.6265\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 3 0.9194 0.9282 0.9785 0.9267 0.9519 0.7057 0.7157\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 4 0.8879 0.8897 0.9591 0.9082 0.9329 0.5940 0.6023\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Mean 0.8995 0.9152 0.9716 0.9109 0.9403 0.6267 0.6403\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Std 0.0114 0.0151 0.0068 0.0097 0.0067 0.0460 0.0433\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 0 Extreme Gradient Boosting 0.9104 0.9406 ... 0.946 0.6835 0.6884\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:02, 1.41it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [02:29<00:55, 55.17s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 0 0.8934 0.8945 0.9656 0.9089 0.9364 0.6085 0.6193\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 1 0.9091 0.9206 0.9742 0.9189 0.9457 0.6676 0.6786\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 2 0.9054 0.9067 0.9720 0.9168 0.9436 0.6517 0.6626\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 3 0.9107 0.9308 0.9806 0.9157 0.9470 0.6642 0.6805\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 4 0.8914 0.8867 0.9720 0.9020 0.9357 0.5905 0.6079\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Mean 0.9020 0.9079 0.9729 0.9125 0.9417 0.6365 0.6498\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Std 0.0081 0.0162 0.0048 0.0062 0.0047 0.0312 0.0304\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m 0 Extreme Gradient Boosting 0.9071 0.9376 ... 0.945 0.6481 0.6679\n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=469091)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m 0 Extreme Gradient Boosting 0.9093 0.9518 ... 0.9462 0.6602 0.6771\n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=469104)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.89718 0.006071\n",
+ "1 AUC 0.91178 0.015454\n",
+ "2 Recall 0.97072 0.009087\n",
+ "3 Prec. 0.90902 0.004655\n",
+ "4 F1 0.93884 0.003827\n",
+ "5 Kappa 0.61870 0.019738\n",
+ "6 MCC 0.63286 0.021338\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Consolidation', constants.CONSOLIDATION)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"Consolidation\", experiment_name=\"CP_Consolidation\", filepath=\"./results/consolidation\")\n",
+ "\n",
+ "# Model training\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -424,7 +435,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/edema.ipynb b/notebooks/edema.ipynb
index 0c73d0f..2f1e083 100644
--- a/notebooks/edema.ipynb
+++ b/notebooks/edema.ipynb
@@ -56,14 +56,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -78,7 +75,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -104,7 +101,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -117,86 +114,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "4491a25a",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "c64760c8",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6c37ae3b",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4992944e",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "d45b9072",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "e280f91d",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "8d254fef",
"metadata": {},
"outputs": [],
@@ -205,13 +133,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -251,168 +173,282 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Edema'\n",
- " )\n",
- "evaluation.evaluate()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0011 +- 0.0012 | \n",
- " 0.0222 +- 0.0129 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.8084 +- 0.0148 | \n",
- " 0.8244 +- 0.0053 | \n",
- " 0.8208 +- 0.007 | \n",
- " 0.7594 +- 0.0459 | \n",
- " 0.8895 +- 0.038 | \n",
- " 0.9147 +- 0.0072 | \n",
- " 0.1151 +- 0.0059 | \n",
- " 0.383 +- 0.0182 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.917 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.912 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.8084 +- 0.0148 0.8244 +- 0.0053 0.8208 +- 0.007 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.7594 +- 0.0459 0.8895 +- 0.038 0.9147 +- 0.0072 \n",
- "HAIM -- -- 0.917 \n",
- "NON_HAIM -- -- 0.912 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0011 +- 0.0012 0.0222 +- 0.0129 \n",
- "test_metrics 0.1151 +- 0.0059 0.383 +- 0.0182 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-09-24 16:07:16,490\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Outer fold 1\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(raylet)\u001b[0m Spilled 2750 MiB, 3 objects, write throughput 586 MiB/s. Set RAY_verbose_spill_logs=0 to disable this message.\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Configuring PyCaret for outer fold 1\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Outer fold 2\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:04, 1.38s/it]\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [07:56<02:56, 176.16s/it]\u001b[32m [repeated 2x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)\u001b[0m\n",
+ " \n",
+ "Processing: 75%|███████▌ | 3/4 [07:55<02:56, 176.09s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 0 0.8214 0.8954 0.8796 0.8409 0.8598 0.6140 0.6152\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 1 0.8372 0.9096 0.9021 0.8465 0.8734 0.6460 0.6486\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 2 0.8404 0.9054 0.8926 0.8569 0.8744 0.6557 0.6567\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 3 0.8486 0.9204 0.9080 0.8573 0.8820 0.6713 0.6734\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 4 0.8399 0.9034 0.8942 0.8554 0.8744 0.6541 0.6553\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Mean 0.8375 0.9068 0.8953 0.8514 0.8728 0.6482 0.6499\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Std 0.0089 0.0082 0.0096 0.0066 0.0072 0.0189 0.0192\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Configuring PyCaret for outer fold 2\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 0 0.8355 0.9064 0.8934 0.8500 0.8712 0.6439 0.6454\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 1 0.8381 0.9115 0.9072 0.8443 0.8746 0.6470 0.6503\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 2 0.8445 0.9094 0.8963 0.8598 0.8777 0.6645 0.6655\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 3 0.8463 0.9163 0.8971 0.8619 0.8791 0.6684 0.6694\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 4 0.8408 0.9026 0.8927 0.8576 0.8748 0.6566 0.6576\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Mean 0.8410 0.9092 0.8973 0.8547 0.8755 0.6561 0.6576\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Std 0.0040 0.0046 0.0052 0.0066 0.0027 0.0095 0.0090\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 0 Extreme Gradient Boosting 0.8539 0.9211 ... 0.8858 0.6836 0.6854\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Configuring PyCaret for outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.19s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 0 Extreme Gradient Boosting 0.8496 0.9244 ... 0.8829 0.6732 0.6757\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Configuring PyCaret for outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.25s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [07:39<02:50, 170.01s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 0 0.8377 0.9101 0.8861 0.8580 0.8718 0.6509 0.6515\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 1 0.8445 0.9121 0.9065 0.8529 0.8789 0.6622 0.6646\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 2 0.8276 0.8968 0.8890 0.8428 0.8653 0.6266 0.6283\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 3 0.8213 0.8997 0.8942 0.8316 0.8618 0.6100 0.6132\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 4 0.8427 0.9118 0.8964 0.8575 0.8765 0.6600 0.6612\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Mean 0.8348 0.9061 0.8944 0.8486 0.8709 0.6419 0.6438\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Std 0.0089 0.0065 0.0070 0.0101 0.0065 0.0204 0.0199\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 75%|███████▌ | 3/4 [07:49<02:53, 173.78s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 0 0.8336 0.9135 0.8971 0.8453 0.8704 0.6387 0.6409\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 1 0.8272 0.8985 0.8860 0.8441 0.8646 0.6263 0.6277\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 2 0.8345 0.9081 0.8853 0.8541 0.8694 0.6436 0.6443\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 3 0.8449 0.9213 0.9036 0.8556 0.8789 0.6637 0.6656\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 4 0.8422 0.9144 0.8949 0.8579 0.8760 0.6593 0.6604\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Mean 0.8365 0.9112 0.8934 0.8514 0.8719 0.6463 0.6478\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Std 0.0064 0.0076 0.0069 0.0056 0.0051 0.0137 0.0137\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 0 Extreme Gradient Boosting 0.8609 0.9302 ... 0.8909 0.6992 0.7007\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.29s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m 0 Extreme Gradient Boosting 0.8577 0.9257 ... 0.8894 0.6904 0.6932\n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=121391)\u001b[0m [1 rows x 8 columns]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 75%|███████▌ | 3/4 [07:34<02:48, 168.37s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 0 0.8350 0.9053 0.8854 0.8548 0.8698 0.6447 0.6455\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 1 0.8386 0.9076 0.9028 0.8477 0.8744 0.6491 0.6516\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 2 0.8477 0.9090 0.9014 0.8605 0.8805 0.6708 0.6721\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 3 0.8427 0.9037 0.9000 0.8551 0.8770 0.6592 0.6609\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 4 0.8404 0.9038 0.9066 0.8478 0.8762 0.6523 0.6552\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Mean 0.8409 0.9059 0.8992 0.8532 0.8756 0.6552 0.6571\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Std 0.0042 0.0021 0.0073 0.0049 0.0035 0.0091 0.0091\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m 0 Extreme Gradient Boosting 0.8641 0.9311 ... 0.8929 0.7071 0.708\n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=121386)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.84274 0.006124\n",
+ "1 AUC 0.91526 0.005277\n",
+ "2 Recall 0.90320 0.009301\n",
+ "3 Prec. 0.85300 0.007575\n",
+ "4 F1 0.87734 0.004765\n",
+ "5 Kappa 0.65872 0.013536\n",
+ "6 MCC 0.66098 0.013425\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Edema', constants.EDEMA)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"Edema\", experiment_name=\"CP_Edema\", filepath=\"./results/edema\")\n",
+ "\n",
+ "# Model training and results evaluation\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -424,7 +460,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/enlarged-cardiomediastinum.ipynb b/notebooks/enlarged-cardiomediastinum.ipynb
index 1a08ac0..5e2368d 100644
--- a/notebooks/enlarged-cardiomediastinum.ipynb
+++ b/notebooks/enlarged-cardiomediastinum.ipynb
@@ -57,14 +57,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -79,7 +76,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -105,7 +102,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -113,91 +110,22 @@
"dataset = HAIMDataset(df, \n",
" constants.CHEST_PREDICTORS, \n",
" constants.ALL_MODALITIES, \n",
- " constants.ENLARGED_CARDIOMEDIASTINUM \n",
+ " constants.ENLARGED_CARDIOMEDIASTINUM,\n",
" constants.IMG_ID, \n",
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "4491a25a",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "c64760c8",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6c37ae3b",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4992944e",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "d45b9072",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "e280f91d",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "8d254fef",
"metadata": {},
"outputs": [],
@@ -206,13 +134,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -252,168 +174,187 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Enlarged Cardiomediastinum'\n",
- " )\n",
- "evaluation.evaluate()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0005 +- 0.0005 | \n",
- " 0.0145 +- 0.0081 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.788 +- 0.0306 | \n",
- " 0.799 +- 0.0314 | \n",
- " 0.7981 +- 0.0316 | \n",
- " 0.7794 +- 0.0366 | \n",
- " 0.8186 +- 0.0543 | \n",
- " 0.8768 +- 0.035 | \n",
- " 0.1208 +- 0.0225 | \n",
- " 0.4129 +- 0.0921 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.876 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.868 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.788 +- 0.0306 0.799 +- 0.0314 0.7981 +- 0.0316 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.7794 +- 0.0366 0.8186 +- 0.0543 0.8768 +- 0.035 \n",
- "HAIM -- -- 0.876 \n",
- "NON_HAIM -- -- 0.868 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0005 +- 0.0005 0.0145 +- 0.0081 \n",
- "test_metrics 0.1208 +- 0.0225 0.4129 +- 0.0921 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-09-26 11:11:21,998\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=409777)\u001b[0m Outer fold 2\n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m Configuring PyCaret for outer fold 2\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m Outer fold 4\u001b[32m [repeated 3x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:02, 1.43it/s]\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "Processing: 75%|███████▌ | 3/4 [02:45<01:01, 61.29s/it]\u001b[32m [repeated 4x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=409766)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m 0 0.8370 0.8827 0.9379 0.8567 0.8955 0.5287 0.5401\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m 1 0.8394 0.8575 0.9608 0.8448 0.8991 0.5140 0.5405\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m 2 0.8537 0.8810 0.9443 0.8701 0.9057 0.5820 0.5918\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m 3 0.8415 0.8587 0.9216 0.8731 0.8967 0.5574 0.5612\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m 4 0.8366 0.8807 0.9379 0.8567 0.8955 0.5247 0.5361\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m Mean 0.8416 0.8721 0.9405 0.8603 0.8985 0.5414 0.5539\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m Std 0.0063 0.0115 0.0126 0.0102 0.0038 0.0249 0.0208\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Configuring PyCaret for outer fold 1\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n",
+ "Processing: 75%|███████▌ | 3/4 [02:46<01:01, 61.57s/it]\u001b[32m [repeated 3x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=409770)\u001b[0m 0 0.8491 0.8723 0.9608 0.8547 0.9046 0.5500 0.5722\n",
+ "\u001b[36m(run_fold pid=409770)\u001b[0m 1 0.8394 0.8769 0.9444 0.8550 0.8975 0.5309 0.5452\n",
+ "\u001b[36m(run_fold pid=409770)\u001b[0m 2 0.8341 0.8788 0.9410 0.8516 0.8941 0.5164 0.5303\n",
+ "\u001b[36m(run_fold pid=409770)\u001b[0m 3 0.8366 0.8694 0.9443 0.8521 0.8958 0.5218 0.5369\n",
+ "\u001b[36m(run_fold pid=409770)\u001b[0m 4 0.8341 0.8473 0.9346 0.8563 0.8938 0.5192 0.5297\n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m 0 0.8297 0.8504 0.9346 0.8512 0.8910 0.5059 0.5177\n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m 1 0.8662 0.9081 0.9706 0.8659 0.9153 0.6022 0.6250\n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m 2 0.8390 0.8627 0.9377 0.8589 0.8966 0.5370 0.5477\n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m 3 0.8415 0.8996 0.9377 0.8614 0.8980 0.5456 0.5556\n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m 4 0.8463 0.8605 0.9837 0.8384 0.9053 0.5121 0.5616\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 0 0.8637 0.8928 0.9608 0.8698 0.9130 0.6020 0.6182\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 1 0.8293 0.8623 0.9279 0.8550 0.8899 0.5123 0.5209\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 2 0.8634 0.8887 0.9410 0.8831 0.9111 0.6177 0.6235\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 3 0.8707 0.8723 0.9706 0.8710 0.9181 0.6159 0.6367\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 4 0.8634 0.8802 0.9771 0.8592 0.9144 0.5838 0.6145\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Fold \u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Mean 0.8581 0.8793 0.9555 0.8676 0.9093 0.5863 0.6028\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Std 0.0147 0.0111 0.0184 0.0099 0.0100 0.0390 0.0416\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 0 Extreme Gradient Boosting 0.8551 0.9127 ... 0.9078 0.574 0.5917\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ " \u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:02, 1.40it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=409777)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m 0 Extreme Gradient Boosting 0.8658 0.9142 ... 0.9143 0.6082 0.6233\n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=409777)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m 0 Extreme Gradient Boosting 0.8471 0.8904 ... 0.9016 0.5617 0.5718\n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=409766)\u001b[0m [1 rows x 8 columns]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 75%|███████▌ | 3/4 [02:33<00:56, 56.59s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 0 0.8540 0.8860 0.9444 0.8705 0.9060 0.5823 0.5921\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 1 0.8516 0.8758 0.9346 0.8746 0.9036 0.5824 0.5885\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 2 0.8561 0.8756 0.9377 0.8773 0.9065 0.5958 0.6021\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 3 0.8537 0.8907 0.9314 0.8796 0.9048 0.5901 0.5946\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 4 0.8390 0.8829 0.9346 0.8614 0.8966 0.5366 0.5458\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Mean 0.8509 0.8822 0.9366 0.8727 0.9035 0.5774 0.5846\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Std 0.0061 0.0058 0.0044 0.0064 0.0036 0.0210 0.0199\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=409770)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=409770)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=409770)\u001b[0m 0 Extreme Gradient Boosting 0.8768 0.9039 ... 0.9212 0.6409 0.6561\n",
+ "\u001b[36m(run_fold pid=409770)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=409770)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m 0 Extreme Gradient Boosting 0.844 0.8559 ... 0.901 0.5386 0.5575\n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=409778)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.84914 0.014792\n",
+ "1 AUC 0.88818 0.019344\n",
+ "2 Recall 0.95358 0.007442\n",
+ "3 Prec. 0.85932 0.011658\n",
+ "4 F1 0.90398 0.009124\n",
+ "5 Kappa 0.55656 0.046711\n",
+ "6 MCC 0.57342 0.045301\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Enlarged Cardiomediastinum', constants.ENLARGED_CARDIOMEDIASTINUM)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"EnlargedCardiomediastinum\", experiment_name=\"CP_EnlargedCardiomediastinum\", filepath=\"./results/enlargedcardiomediastinum\")\n",
+ "\n",
+ "# Model training and results evaluation\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -425,7 +366,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/fracture.ipynb b/notebooks/fracture.ipynb
index 182dace..0bdcf42 100644
--- a/notebooks/fracture.ipynb
+++ b/notebooks/fracture.ipynb
@@ -55,14 +55,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -77,7 +74,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -103,7 +100,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -116,86 +113,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "4491a25a",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "c64760c8",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6c37ae3b",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4992944e",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "d45b9072",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "e280f91d",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "8d254fef",
"metadata": {},
"outputs": [],
@@ -205,12 +133,12 @@
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
" }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ "# Fixed parameters to pass\n",
+ "fixed_params = {\n",
+ " 'seed': 42,\n",
+ " 'eval_metric': 'logloss',\n",
+ " 'verbosity': 0\n",
+ "}"
]
},
{
@@ -250,168 +178,482 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Fracture'\n",
- " )\n",
- "evaluation.evaluate()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0029 +- 0.0006 | \n",
- " 0.039 +- 0.0037 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.9052 +- 0.0434 | \n",
- " 0.6851 +- 0.1605 | \n",
- " 0.5897 +- 0.2393 | \n",
- " 0.9363 +- 0.0343 | \n",
- " 0.4339 +- 0.3228 | \n",
- " 0.828 +- 0.1103 | \n",
- " 0.0516 +- 0.0214 | \n",
- " 0.1901 +- 0.067 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.838 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.787 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9052 +- 0.0434 0.6851 +- 0.1605 0.5897 +- 0.2393 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9363 +- 0.0343 0.4339 +- 0.3228 0.828 +- 0.1103 \n",
- "HAIM -- -- 0.838 \n",
- "NON_HAIM -- -- 0.787 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0029 +- 0.0006 0.039 +- 0.0037 \n",
- "test_metrics 0.0516 +- 0.0214 0.1901 +- 0.067 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-10-21 11:46:41,343\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8267 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Outer fold 1\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Train indices: [ 0 3 5 6 7 8 9 10 11 12 13 15 16 18 19 20 21 22\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 24 25 26 27 30 31 32 33 34 35 37 39 40 41 42 44 46 47\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 48 49 51 52 53 54 56 57 58 59 63 64 65 66 68 71 77 79\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 80 81 83 84 86 87 88 89 91 93 94 95 96 97 98 99 100 101\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 102 103 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 121 122 123 124 126 128 130 131 132 133 134 137 139 140 141 142 144 146\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 147 148 149 150 151 152 154 155 156 157 159 162 164 165 166 168 169 171\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 172 173 175 177 179 180 182 183 185 186 187 189 190 191 192 195 196 197\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 198 199 200 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 217 218 219 220 222 223 224 225 226 227 228 230 231 232 234 235 236 237\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 240 241 242 243 245 246 247 248 250 251 252 253 254 255 256 257 258 259\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 260 261 263 264 265 266 268 270 271 272 273 275 276 277 278 279 280 281\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 282 283 284 285 287 288 289 290 291 293 294 295 296 297 298 300 301 302\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 358 359 360 362 364 366 367 368 369 373 374 375 376 377 380 381 382 383\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 384 386 387 388 389 390 391 392 394 395 397 398 399 400 401 402 404 405\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 409 410 411 413 415 416 417 418 419 420 421 422 423 425 426 427 429 430\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 432 434 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 452 453 454 455 456 457 458 460 461 462 463 464 465 467 469 470 472 474\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 476 477 478 479 480 482 483 485 486 487 489 491 492 493 494 495 496 497\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 498 499 500 501 503 505 506 507 508 509 510 511 512 513 514 515 516 518\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 520 521 522 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 539 540 541 542 544 546 547 548 549 550 553 554 555]\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Test indices: [ 1 2 4 14 17 23 28 29 36 38 43 45 50 55 60 61 62 67\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 69 70 72 73 74 75 76 78 82 85 90 92 104 125 127 129 135 136\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 138 143 145 153 158 160 161 163 167 170 174 176 178 181 184 188 193 194\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 201 221 229 233 238 239 244 249 262 267 269 274 286 292 299 303 361 363\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 365 370 371 372 378 379 385 393 396 403 406 407 408 412 414 424 428 431\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 433 435 459 466 468 471 473 475 481 484 488 490 502 504 517 519 523 543\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 545 551 552 556]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Configuring PyCaret for outer fold 1\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:00<00:01, 1.81it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [00:22<00:08, 8.33s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 0.9444 0.7821 1.0 0.9437 0.9710 0.3175 0.4344\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 1 0.9437 0.6940 1.0 0.9437 0.9710 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 2 0.9718 0.8507 1.0 0.9710 0.9853 0.6537 0.6968\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 3 0.9577 0.8394 1.0 0.9565 0.9778 0.5535 0.6186\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 4 0.9437 0.5455 1.0 0.9429 0.9706 0.3173 0.4342\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Mean 0.9523 0.7423 1.0 0.9515 0.9751 0.3684 0.4368\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Std 0.0112 0.1130 0.0 0.0110 0.0057 0.2265 0.2414\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 Extreme Gradient Boosting 0.9375 0.7088 ... 0.9677 0.0 0.0\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Outer fold 2\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Train indices: [ 0 1 2 3 4 5 7 8 9 12 13 14 15 16 17 18 19 21\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 22 23 26 27 28 29 30 31 32 33 35 36 37 38 41 43 45 46\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 47 49 50 51 52 55 56 57 58 60 61 62 63 64 65 66 67 69\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 87 88\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 107 108 109 111 112 113 114 116 117 118 119 120 121 122 123 124 125 127\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 128 129 130 132 134 135 136 137 138 139 140 141 143 144 145 146 147 148\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 149 150 152 153 155 156 157 158 159 160 161 162 163 164 165 167 168 169\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 170 171 172 173 174 176 177 178 179 180 181 182 184 186 187 188 190 191\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 192 193 194 196 198 199 201 203 205 207 209 210 213 214 215 216 217 218\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 220 221 222 225 226 227 229 231 233 235 236 237 238 239 243 244 245 246\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 247 249 250 251 252 253 254 255 256 257 258 259 260 261 262 265 266 267\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 268 269 271 272 273 274 275 276 277 279 280 281 283 285 286 287 289 290\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 292 293 295 296 298 299 301 302 303 304 305 309 310 311 312 313 314 315\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 317 318 319 320 322 323 324 325 326 327 328 329 330 331 334 335 336 337\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 338 339 340 341 345 346 349 350 351 352 354 355 357 358 360 361 362 363\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 364 365 366 368 370 371 372 375 376 377 378 379 380 381 382 383 385 386\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 387 389 390 391 392 393 394 395 396 397 398 399 400 401 403 404 405 406\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 407 408 409 410 412 413 414 415 416 417 418 419 421 422 424 427 428 429\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 430 431 432 433 435 436 437 438 439 440 441 442 443 444 445 447 448 450\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 451 452 453 454 455 456 459 460 461 463 464 466 467 468 469 470 471 472\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 473 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 492 494 495 496 498 499 500 501 502 503 504 505 507 510 512 513 515 517\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 518 519 520 521 522 523 524 525 527 528 530 532 533 534 535 536 537 538\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 542 543 544 545 546 547 548 549 551 552 554 555 556]\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Test indices: [ 6 10 11 20 24 25 34 39 40 42 44 48 53 54 59 68 86 110\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 115 126 131 133 142 151 154 166 175 183 185 189 195 197 200 202 204 206\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 208 211 212 219 223 224 228 230 232 234 240 241 242 248 263 264 270 278\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 282 284 288 291 294 297 300 306 307 308 316 321 332 333 342 343 344 347\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 348 353 356 359 367 369 373 374 384 388 402 411 420 423 425 426 434 446\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 449 457 458 462 465 474 493 497 506 508 509 511 514 516 526 529 531 539\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 540 541 550 553]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Configuring PyCaret for outer fold 2\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:00<00:01, 1.78it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [00:22<00:08, 8.23s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 0.9306 0.5970 1.0 0.9306 0.9640 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 1 0.9437 0.9216 1.0 0.9437 0.9710 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 2 0.9437 0.7799 1.0 0.9437 0.9710 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 3 0.9437 0.6716 1.0 0.9437 0.9710 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 4 0.9437 0.7939 1.0 0.9429 0.9706 0.3173 0.4342\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Mean 0.9410 0.7528 1.0 0.9409 0.9695 0.0635 0.0868\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Std 0.0052 0.1112 0.0 0.0052 0.0028 0.1269 0.1737\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 Extreme Gradient Boosting 0.9554 0.7728 ... 0.9765 0.527 0.5982\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Outer fold 3\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Train indices: [ 0 1 2 4 5 6 7 9 10 11 12 13 14 15 16 17 20 21\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 22 23 24 25 28 29 30 31 33 34 35 36 37 38 39 40 41 42\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 43 44 45 46 47 48 50 51 52 53 54 55 58 59 60 61 62 63\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 64 65 67 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 84 85 86 89 90 91 92 93 95 96 98 100 101 103 104 105 109 110\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 111 112 113 115 116 118 119 120 121 123 124 125 126 127 128 129 131 133\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 134 135 136 137 138 140 141 142 143 145 146 147 148 149 150 151 152 153\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 154 155 157 158 159 160 161 163 165 166 167 169 170 171 173 174 175 176\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 177 178 179 180 181 182 183 184 185 186 187 188 189 192 193 194 195 196\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 197 198 199 200 201 202 203 204 206 207 208 209 210 211 212 213 214 215\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 217 218 219 220 221 223 224 226 227 228 229 230 231 232 233 234 235 236\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 237 238 239 240 241 242 243 244 245 246 247 248 249 250 253 254 257 260\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 262 263 264 265 267 268 269 270 271 272 274 275 277 278 280 281 282 283\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 284 286 287 288 290 291 292 293 294 295 297 298 299 300 302 303 305 306\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 307 308 310 311 312 314 316 317 319 321 322 324 325 327 330 331 332 333\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 334 335 336 337 338 340 341 342 343 344 347 348 349 351 352 353 355 356\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 357 358 359 360 361 362 363 365 367 368 369 370 371 372 373 374 375 376\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 378 379 381 382 383 384 385 386 387 388 390 392 393 395 396 398 401 402\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 403 406 407 408 410 411 412 413 414 415 418 420 421 422 423 424 425 426\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 427 428 429 430 431 433 434 435 436 437 438 440 442 443 444 446 449 450\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 452 453 455 457 458 459 461 462 465 466 467 468 469 470 471 472 473 474\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 475 476 478 479 480 481 482 483 484 485 488 489 490 491 493 494 495 496\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 497 499 500 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 517 518 519 520 523 524 525 526 528 529 530 531 533 534 535 536 539 540\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 541 542 543 544 545 546 547 549 550 551 552 553 555 556]\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Test indices: [ 3 8 18 19 26 27 32 49 56 57 66 77 87 88 94 97 99 102\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 106 107 108 114 117 122 130 132 139 144 156 162 164 168 172 190 191 205\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 216 222 225 251 252 255 256 258 259 261 266 273 276 279 285 289 296 301\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 304 309 313 315 318 320 323 326 328 329 339 345 346 350 354 364 366 377\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 380 389 391 394 397 399 400 404 405 409 416 417 419 432 439 441 445 447\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 448 451 454 456 460 463 464 477 486 487 492 498 501 521 522 527 532 537\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 538 548 554]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Configuring PyCaret for outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:00<00:01, 1.80it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [00:21<00:07, 7.99s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 0.9306 0.6090 1.0 0.9306 0.9640 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 1 0.9718 0.7388 1.0 0.9710 0.9853 0.6537 0.6968\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 2 0.9577 0.9739 1.0 0.9571 0.9781 0.3862 0.4892\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 3 0.9437 0.6424 1.0 0.9429 0.9706 0.3173 0.4342\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 4 0.9577 0.7788 1.0 0.9565 0.9778 0.5535 0.6186\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Mean 0.9523 0.7486 1.0 0.9516 0.9752 0.3821 0.4478\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Std 0.0141 0.1285 0.0 0.0138 0.0073 0.2251 0.2423\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 Extreme Gradient Boosting 0.955 0.7788 ... 0.9765 0.4284 0.5221\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Outer fold 4\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Train indices: [ 0 1 2 3 4 6 8 9 10 11 12 13 14 15 17 18 19 20\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 23 24 25 26 27 28 29 32 33 34 36 37 38 39 40 42 43 44\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 45 46 47 48 49 50 51 53 54 55 56 57 59 60 61 62 65 66\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 67 68 69 70 71 72 73 74 75 76 77 78 80 81 82 83 85 86\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 87 88 89 90 91 92 93 94 96 97 98 99 100 102 104 105 106 107\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 108 109 110 111 114 115 116 117 119 120 122 123 125 126 127 128 129 130\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 131 132 133 135 136 137 138 139 141 142 143 144 145 147 150 151 153 154\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 155 156 158 159 160 161 162 163 164 166 167 168 169 170 172 173 174 175\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 176 178 179 181 182 183 184 185 186 187 188 189 190 191 193 194 195 197\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 198 199 200 201 202 203 204 205 206 207 208 210 211 212 216 218 219 221\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 222 223 224 225 227 228 229 230 231 232 233 234 235 238 239 240 241 242\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 243 244 247 248 249 251 252 254 255 256 257 258 259 261 262 263 264 266\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 267 268 269 270 272 273 274 275 276 277 278 279 280 282 284 285 286 288\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 289 291 292 294 296 297 299 300 301 303 304 306 307 308 309 311 313 314\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 315 316 318 319 320 321 322 323 326 328 329 332 333 334 337 339 340 342\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 343 344 345 346 347 348 350 351 353 354 355 356 357 359 360 361 362 363\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 364 365 366 367 369 370 371 372 373 374 377 378 379 380 381 384 385 386\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 388 389 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 407 408 409 411 412 413 414 416 417 419 420 422 423 424 425 426 427 428\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 430 431 432 433 434 435 437 438 439 441 442 443 445 446 447 448 449 450\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 451 452 453 454 456 457 458 459 460 462 463 464 465 466 468 469 470 471\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 472 473 474 475 476 477 478 479 481 483 484 486 487 488 490 492 493 495\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 496 497 498 499 501 502 504 505 506 507 508 509 511 512 513 514 515 516\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 517 518 519 521 522 523 525 526 527 528 529 531 532 533 535 537 538 539\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 540 541 543 545 546 548 549 550 551 552 553 554 555 556]\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Test indices: [ 5 7 16 21 22 30 31 35 41 52 58 63 64 79 84 95 101 103\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 112 113 118 121 124 134 140 146 148 149 152 157 165 171 177 180 192 196\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 209 213 214 215 217 220 226 236 237 245 246 250 253 260 265 271 281 283\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 287 290 293 295 298 302 305 310 312 317 324 325 327 330 331 335 336 338\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 341 349 352 358 368 375 376 382 383 387 390 410 415 418 421 429 436 440\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 444 455 461 467 480 482 485 489 491 494 500 503 510 520 524 530 534 536\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 542 544 547]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Configuring PyCaret for outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:00<00:01, 1.72it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [00:22<00:08, 8.11s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 0.9444 0.5642 1.0 0.9437 0.9710 0.3175 0.4344\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 1 0.9437 0.5261 1.0 0.9437 0.9710 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 2 0.9718 0.8022 1.0 0.9710 0.9853 0.6537 0.6968\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 3 0.9296 0.8000 1.0 0.9296 0.9635 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 4 0.9437 0.9818 1.0 0.9429 0.9706 0.3173 0.4342\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Mean 0.9466 0.7349 1.0 0.9462 0.9723 0.2577 0.3131\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Std 0.0138 0.1688 0.0 0.0135 0.0071 0.2436 0.2730\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 Extreme Gradient Boosting 0.9459 0.8104 ... 0.972 0.238 0.3675\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Train indices: [ 1 2 3 4 5 6 7 8 10 11 14 16 17 18 19 20 21 22\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 23 24 25 26 27 28 29 30 31 32 34 35 36 38 39 40 41 42\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 43 44 45 48 49 50 52 53 54 55 56 57 58 59 60 61 62 63\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 64 66 67 68 69 70 72 73 74 75 76 77 78 79 82 84 85 86\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 87 88 90 92 94 95 97 99 101 102 103 104 106 107 108 110 112 113\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 114 115 117 118 121 122 124 125 126 127 129 130 131 132 133 134 135 136\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 138 139 140 142 143 144 145 146 148 149 151 152 153 154 156 157 158 160\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 161 162 163 164 165 166 167 168 170 171 172 174 175 176 177 178 180 181\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 183 184 185 188 189 190 191 192 193 194 195 196 197 200 201 202 204 205\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 206 208 209 211 212 213 214 215 216 217 219 220 221 222 223 224 225 226\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 228 229 230 232 233 234 236 237 238 239 240 241 242 244 245 246 248 249\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 250 251 252 253 255 256 258 259 260 261 262 263 264 265 266 267 269 270\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 271 273 274 276 278 279 281 282 283 284 285 286 287 288 289 290 291 292\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 312 313 315 316 317 318 320 321 323 324 325 326 327 328 329 330 331 332\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 333 335 336 338 339 341 342 343 344 345 346 347 348 349 350 352 353 354\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 356 358 359 361 363 364 365 366 367 368 369 370 371 372 373 374 375 376\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 377 378 379 380 382 383 384 385 387 388 389 390 391 393 394 396 397 399\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 400 402 403 404 405 406 407 408 409 410 411 412 414 415 416 417 418 419\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 420 421 423 424 425 426 428 429 431 432 433 434 435 436 439 440 441 444\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 445 446 447 448 449 451 454 455 456 457 458 459 460 461 462 463 464 465\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 466 467 468 471 473 474 475 477 480 481 482 484 485 486 487 488 489 490\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 491 492 493 494 497 498 500 501 502 503 504 506 508 509 510 511 514 516\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 517 519 520 521 522 523 524 526 527 529 530 531 532 534 536 537 538 539\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 540 541 542 543 544 545 547 548 550 551 552 553 554 556]\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Test indices: [ 0 9 12 13 15 33 37 46 47 51 65 71 80 81 83 89 91 93\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 96 98 100 105 109 111 116 119 120 123 128 137 141 147 150 155 159 169\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 173 179 182 186 187 198 199 203 207 210 218 227 231 235 243 247 254 257\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 268 272 275 277 280 311 314 319 322 334 337 340 351 355 357 360 362 381\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 386 392 395 398 401 413 422 427 430 437 438 442 443 450 452 453 469 470\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 472 476 478 479 483 495 496 499 505 507 512 513 515 518 525 528 533 535\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 546 549 555]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:00<00:01, 1.73it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [00:21<00:08, 8.02s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 0.9306 0.8687 0.9851 0.9429 0.9635 0.2562 0.2863\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 1 0.9577 0.8545 1.0000 0.9571 0.9781 0.3862 0.4892\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 2 0.9437 0.8470 1.0000 0.9437 0.9710 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 3 0.9577 0.7091 1.0000 0.9565 0.9778 0.5535 0.6186\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 4 0.9437 0.7424 1.0000 0.9429 0.9706 0.3173 0.4342\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Mean 0.9467 0.8043 0.9970 0.9486 0.9722 0.3026 0.3657\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Std 0.0102 0.0654 0.0060 0.0067 0.0054 0.1810 0.2116\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m 0 Extreme Gradient Boosting 0.955 0.6538 ... 0.9765 0.4284 0.5221\n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=329239)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.94168 0.018077\n",
+ "1 AUC 0.73134 0.134492\n",
+ "2 Recall 0.99702 0.006663\n",
+ "3 Prec. 0.94376 0.016501\n",
+ "4 F1 0.96956 0.009332\n",
+ "5 Kappa 0.26200 0.302226\n",
+ "6 MCC 0.29680 0.321266\n",
+ "Best hyperparameters across all folds: objective binary:logistic\n",
+ "base_score NaN\n",
+ "booster gbtree\n",
+ "callbacks NaN\n",
+ "colsample_bylevel NaN\n",
+ "colsample_bynode NaN\n",
+ "colsample_bytree NaN\n",
+ "device cpu\n",
+ "early_stopping_rounds NaN\n",
+ "enable_categorical False\n",
+ "eval_metric NaN\n",
+ "feature_types NaN\n",
+ "gamma NaN\n",
+ "grow_policy NaN\n",
+ "importance_type NaN\n",
+ "interaction_constraints NaN\n",
+ "learning_rate 0.05\n",
+ "max_bin NaN\n",
+ "max_cat_threshold NaN\n",
+ "max_cat_to_onehot NaN\n",
+ "max_delta_step NaN\n",
+ "max_depth 5.0\n",
+ "max_leaves NaN\n",
+ "min_child_weight NaN\n",
+ "missing NaN\n",
+ "monotone_constraints NaN\n",
+ "multi_strategy NaN\n",
+ "n_estimators 200.0\n",
+ "n_jobs 1.0\n",
+ "num_parallel_tree NaN\n",
+ "random_state 42.0\n",
+ "reg_alpha NaN\n",
+ "reg_lambda NaN\n",
+ "sampling_method NaN\n",
+ "scale_pos_weight NaN\n",
+ "subsample NaN\n",
+ "tree_method auto\n",
+ "validate_parameters NaN\n",
+ "verbosity 0.0\n",
+ "Name: 0, dtype: object\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Fracture', constants.FRACTURE)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"Fracture\", experiment_name=\"CP_Fracture\", filepath=\"./results/fracture\")\n",
+ "\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps, # Grid d'hyperparamètres\n",
+ " fixed_params=fixed_params # Paramètres fixes\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -423,7 +665,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/length-of-stay.ipynb b/notebooks/length-of-stay.ipynb
index 282aca8..1563ea5 100644
--- a/notebooks/length-of-stay.ipynb
+++ b/notebooks/length-of-stay.ipynb
@@ -46,14 +46,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -68,12 +65,12 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
"source": [
- "df = read_csv(FILE_DF, nrows=df = read_csv(constants.FILE_DF, nrows=constants.N_DATA))\n"
+ "df = read_csv(constants.FILE_DF, nrows=constants.N_DATA)\n"
]
},
{
@@ -94,7 +91,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -107,86 +104,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "0d9f8354",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "2fe30725",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6abdd3c8",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "f935bbb8",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "3ceec6b8",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "114f173f",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "8d6cb174",
"metadata": {},
"outputs": [],
@@ -195,13 +123,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -241,160 +163,360 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='48h los'\n",
- " )\n",
- "evaluation.evaluate()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0 +- 0.0 | \n",
- " 0.0009 +- 0.0004 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.924 +- 0.0038 | \n",
- " 0.5532 +- 0.006 | \n",
- " 0.3266 +- 0.0182 | \n",
- " 0.1071 +- 0.0118 | \n",
- " 0.9993 +- 0.0003 | \n",
- " 0.9323 +- 0.0115 | \n",
- " 0.0461 +- 0.0033 | \n",
- " 0.2077 +- 0.0293 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.939 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.919 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.924 +- 0.0038 0.5532 +- 0.006 0.3266 +- 0.0182 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.1071 +- 0.0118 0.9993 +- 0.0003 0.9323 +- 0.0115 \n",
- "HAIM -- -- 0.939 \n",
- "NON_HAIM -- -- 0.919 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0 +- 0.0 0.0009 +- 0.0004 \n",
- "test_metrics 0.0461 +- 0.0033 0.2077 +- 0.0293 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-10-25 22:05:42,189\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Outer fold 1\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Train indices: [ 0 1 3 ... 45046 45047 45049]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Test indices: [ 2 13 21 ... 45039 45045 45048]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(raylet)\u001b[0m Spilled 3869 MiB, 1 objects, write throughput 563 MiB/s. Set RAY_verbose_spill_logs=0 to disable this message.\n",
+ "\u001b[36m(raylet)\u001b[0m Spilled 7738 MiB, 3 objects, write throughput 564 MiB/s.\n",
+ "\u001b[36m(raylet)\u001b[0m Spilled 11607 MiB, 5 objects, write throughput 555 MiB/s.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Configuring PyCaret for outer fold 1\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.98s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [14:44<05:27, 327.11s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 0.9584 0.9578 0.5791 0.8896 0.7015 0.6802 0.6984\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 1 0.9561 0.9585 0.5832 0.8503 0.6918 0.6691 0.6829\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 2 0.9580 0.9524 0.6049 0.8547 0.7084 0.6865 0.6984\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 3 0.9570 0.9563 0.5947 0.8500 0.6998 0.6774 0.6899\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 4 0.9544 0.9550 0.5576 0.8495 0.6733 0.6499 0.6666\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Mean 0.9568 0.9560 0.5839 0.8588 0.6950 0.6726 0.6872\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Std 0.0014 0.0022 0.0160 0.0155 0.0121 0.0127 0.0118\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 Extreme Gradient Boosting 0.9645 0.9691 ... 0.7527 0.7342 0.7475\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Outer fold 2\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Train indices: [ 0 2 3 ... 45045 45047 45048]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Test indices: [ 1 10 15 ... 45041 45046 45049]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(raylet)\u001b[0m Spilled 19844 MiB, 11 objects, write throughput 560 MiB/s.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Configuring PyCaret for outer fold 2\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.74s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [14:18<05:17, 317.72s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 0.9534 0.9481 0.5585 0.8344 0.6691 0.6451 0.6602\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 1 0.9560 0.9520 0.6016 0.8300 0.6976 0.6745 0.6847\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 2 0.9502 0.9509 0.5329 0.8119 0.6435 0.6180 0.6338\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 3 0.9561 0.9563 0.5700 0.8629 0.6865 0.6640 0.6805\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 4 0.9592 0.9515 0.5988 0.8792 0.7124 0.6913 0.7061\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Mean 0.9550 0.9518 0.5724 0.8437 0.6818 0.6586 0.6730\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Std 0.0030 0.0026 0.0257 0.0241 0.0238 0.0252 0.0244\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 Extreme Gradient Boosting 0.9657 0.9732 ... 0.7621 0.7442 0.757\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Outer fold 3\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Train indices: [ 1 2 4 ... 45047 45048 45049]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Test indices: [ 0 3 8 ... 45033 45042 45044]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Configuring PyCaret for outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.92s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [14:15<05:16, 316.42s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 0.9591 0.9538 0.6345 0.8420 0.7237 0.7020 0.7102\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 1 0.9558 0.9575 0.5606 0.8694 0.6816 0.6591 0.6774\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 2 0.9572 0.9592 0.5988 0.8484 0.7021 0.6797 0.6917\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 3 0.9547 0.9558 0.5905 0.8223 0.6874 0.6637 0.6743\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 4 0.9563 0.9543 0.5823 0.8524 0.6919 0.6693 0.6834\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Mean 0.9566 0.9561 0.5933 0.8469 0.6973 0.6748 0.6874\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Std 0.0015 0.0020 0.0242 0.0153 0.0148 0.0153 0.0129\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 Extreme Gradient Boosting 0.9596 0.9619 ... 0.7255 0.7042 0.7131\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Outer fold 4\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Train indices: [ 0 1 2 ... 45047 45048 45049]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Test indices: [ 4 6 12 ... 45028 45029 45031]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Configuring PyCaret for outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.90s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [14:16<05:16, 316.87s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 0.9584 0.9550 0.6119 0.8539 0.7129 0.6911 0.7023\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 1 0.9570 0.9611 0.6057 0.8405 0.7041 0.6815 0.6922\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 2 0.9539 0.9552 0.5617 0.8374 0.6724 0.6486 0.6636\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 3 0.9575 0.9547 0.5885 0.8640 0.7001 0.6781 0.6927\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 4 0.9568 0.9591 0.5905 0.8516 0.6974 0.6750 0.6882\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Mean 0.9567 0.9570 0.5917 0.8495 0.6974 0.6749 0.6878\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Std 0.0015 0.0026 0.0174 0.0096 0.0135 0.0142 0.0129\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 Extreme Gradient Boosting 0.9634 0.9684 ... 0.7446 0.7255 0.7389\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Train indices: [ 0 1 2 ... 45046 45048 45049]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Test indices: [ 5 7 14 ... 45040 45043 45047]\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:02<00:08, 2.93s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [14:00<05:10, 310.99s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 0.9553 0.9513 0.5934 0.8281 0.6914 0.6680 0.6788\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 1 0.9594 0.9600 0.6119 0.8688 0.7181 0.6969 0.7094\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 2 0.9546 0.9543 0.5658 0.8436 0.6773 0.6539 0.6690\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 3 0.9572 0.9451 0.5823 0.8654 0.6962 0.6741 0.6894\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 4 0.9592 0.9611 0.6214 0.8555 0.7199 0.6985 0.7089\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Mean 0.9571 0.9544 0.5950 0.8523 0.7006 0.6783 0.6911\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Std 0.0020 0.0059 0.0200 0.0150 0.0163 0.0172 0.0161\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m 0 Extreme Gradient Boosting 0.9664 0.9693 ... 0.7678 0.7502 0.7623\n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=249894)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.95774 0.000913\n",
+ "1 AUC 0.95950 0.003070\n",
+ "2 Recall 0.58768 0.023370\n",
+ "3 Prec. 0.87118 0.024657\n",
+ "4 F1 0.70128 0.010241\n",
+ "5 Kappa 0.67952 0.010264\n",
+ "6 MCC 0.69510 0.007584\n",
+ "Best hyperparameters across all folds: objective binary:logistic\n",
+ "base_score NaN\n",
+ "booster gbtree\n",
+ "callbacks NaN\n",
+ "colsample_bylevel NaN\n",
+ "colsample_bynode NaN\n",
+ "colsample_bytree NaN\n",
+ "device cpu\n",
+ "early_stopping_rounds NaN\n",
+ "enable_categorical False\n",
+ "eval_metric NaN\n",
+ "feature_types NaN\n",
+ "gamma NaN\n",
+ "grow_policy NaN\n",
+ "importance_type NaN\n",
+ "interaction_constraints NaN\n",
+ "learning_rate 0.1\n",
+ "max_bin NaN\n",
+ "max_cat_threshold NaN\n",
+ "max_cat_to_onehot NaN\n",
+ "max_delta_step NaN\n",
+ "max_depth 8\n",
+ "max_leaves NaN\n",
+ "min_child_weight NaN\n",
+ "missing NaN\n",
+ "monotone_constraints NaN\n",
+ "multi_strategy NaN\n",
+ "n_estimators 300\n",
+ "n_jobs 1\n",
+ "num_parallel_tree NaN\n",
+ "random_state 42\n",
+ "reg_alpha NaN\n",
+ "reg_lambda NaN\n",
+ "sampling_method NaN\n",
+ "scale_pos_weight NaN\n",
+ "subsample NaN\n",
+ "tree_method auto\n",
+ "validate_parameters NaN\n",
+ "verbosity 0\n",
+ "Name: 0, dtype: object\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/48h los', constants.LOS)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"LengthOfStay\", experiment_name=\"CP_LengthOfStay\", filepath=\"./results/lengthofstay\")\n",
+ "\n",
+ "# Model training and results evaluation\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -406,7 +528,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/lung-lesion.ipynb b/notebooks/lung-lesion.ipynb
index 857c12a..1b43aa1 100644
--- a/notebooks/lung-lesion.ipynb
+++ b/notebooks/lung-lesion.ipynb
@@ -56,14 +56,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -78,7 +75,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -104,7 +101,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -117,86 +114,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "4491a25a",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "c64760c8",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6c37ae3b",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4992944e",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "d45b9072",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "e280f91d",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "8d254fef",
"metadata": {},
"outputs": [],
@@ -205,13 +133,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -251,168 +173,187 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Lung Lesion'\n",
- " )\n",
- "evaluation.evaluate()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0035 +- 0.0008 | \n",
- " 0.0353 +- 0.0042 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.9601 +- 0.0113 | \n",
- " 0.6165 +- 0.0991 | \n",
- " 0.3786 +- 0.3094 | \n",
- " 0.9922 +- 0.0024 | \n",
- " 0.2407 +- 0.1973 | \n",
- " 0.8286 +- 0.0529 | \n",
- " 0.0374 +- 0.0082 | \n",
- " 0.147 +- 0.0333 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.844 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.831 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9601 +- 0.0113 0.6165 +- 0.0991 0.3786 +- 0.3094 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9922 +- 0.0024 0.2407 +- 0.1973 0.8286 +- 0.0529 \n",
- "HAIM -- -- 0.844 \n",
- "NON_HAIM -- -- 0.831 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0035 +- 0.0008 0.0353 +- 0.0042 \n",
- "test_metrics 0.0374 +- 0.0082 0.147 +- 0.0333 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-09-26 10:03:33,478\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Outer fold 1\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Configuring PyCaret for outer fold 1\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m Outer fold 2\u001b[32m [repeated 3x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:01, 1.73it/s]\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "Processing: 75%|███████▌ | 3/4 [00:27<00:10, 10.12s/it]\u001b[32m [repeated 4x across cluster]\u001b[0m\n",
+ " \n",
+ "Processing: 75%|███████▌ | 3/4 [00:28<00:10, 10.56s/it]\u001b[32m [repeated 3x across cluster]\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=396014)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m 0 0.9748 0.9912 1.0 0.9744 0.9870 0.5609 0.6243\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m 1 0.9664 0.5982 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m 2 0.9664 0.8491 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m 3 0.9664 0.6754 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m 4 0.9664 0.8230 1.0 0.9658 0.9826 0.4871 0.5674\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m Mean 0.9681 0.7874 1.0 0.9677 0.9836 0.4039 0.5021\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m Std 0.0034 0.1378 0.0 0.0033 0.0017 0.1008 0.0786\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m Configuring PyCaret for outer fold 2\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 0 0.9748 0.9140 1.0 0.9744 0.9870 0.5609 0.6243\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 1 0.9580 0.8474 1.0 0.9580 0.9785 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 2 0.9580 0.8614 1.0 0.9580 0.9785 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 3 0.9580 0.7509 1.0 0.9580 0.9785 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 4 0.9580 0.9248 1.0 0.9576 0.9784 0.2753 0.3995\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m 0 0.9664 0.8965 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m 1 0.9580 0.7368 1.0 0.9580 0.9785 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m 2 0.9664 0.7228 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m 3 0.9580 0.7789 1.0 0.9580 0.9785 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m 4 0.9664 0.8687 1.0 0.9658 0.9826 0.4871 0.5674\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m 0 0.9664 0.7509 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m 1 0.9748 0.6825 1.0 0.9744 0.9870 0.5609 0.6243\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m 2 0.9664 0.5632 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m 3 0.9580 0.7158 1.0 0.9580 0.9785 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m 4 0.9580 0.8274 1.0 0.9576 0.9784 0.2753 0.3995\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m Fold \u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m Mean 0.9647 0.7079 1.0 0.9644 0.9819 0.2968 0.3806\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m Std 0.0063 0.0869 0.0 0.0062 0.0032 0.1788 0.2057\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 0 Extreme Gradient Boosting 0.9677 0.7893 ... 0.9834 0.3895 0.4918\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ " \u001b[32m [repeated 3x across cluster]\u001b[0m\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:02, 1.47it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=396014)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m 0 Extreme Gradient Boosting 0.957 0.7781 ... 0.978 0.0 0.0\n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=396014)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m 0 Extreme Gradient Boosting 0.9677 0.9256 ... 0.9834 0.3895 0.4918\n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=396009)\u001b[0m [1 rows x 8 columns]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 75%|███████▌ | 3/4 [00:26<00:09, 9.83s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 0 0.9664 0.8263 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 1 0.9664 0.7684 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 2 0.9664 0.8368 1.0 0.9661 0.9828 0.3239 0.4396\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 3 0.9580 0.9070 1.0 0.9580 0.9785 0.0000 0.0000\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 4 0.9580 0.9307 1.0 0.9576 0.9784 0.2753 0.3995\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Mean 0.9630 0.8539 1.0 0.9628 0.9810 0.2494 0.3436\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Std 0.0041 0.0584 0.0 0.0041 0.0021 0.1261 0.1725\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=396012)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m 0 Extreme Gradient Boosting 0.9624 0.8729 ... 0.9807 0.2147 0.3468\n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=396012)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m 0 Extreme Gradient Boosting 0.9624 0.7866 ... 0.9806 0.3522 0.4624\n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=396015)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.96976 0.004601\n",
+ "1 AUC 0.88772 0.069591\n",
+ "2 Recall 1.00000 0.000000\n",
+ "3 Prec. 0.96942 0.004546\n",
+ "4 F1 0.98448 0.002300\n",
+ "5 Kappa 0.41870 0.129810\n",
+ "6 MCC 0.51348 0.101164\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Lung Lesion', constants.LUNG_LESION)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"LungLesion\", experiment_name=\"CP_LungLesion\", filepath=\"./results/lunglesion\")\n",
+ "\n",
+ "# Model training and results evaluation\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -424,7 +365,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/lung-opacity.ipynb b/notebooks/lung-opacity.ipynb
index 9a66c14..4595ba2 100644
--- a/notebooks/lung-opacity.ipynb
+++ b/notebooks/lung-opacity.ipynb
@@ -57,14 +57,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -79,7 +76,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -105,7 +102,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -118,86 +115,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "4491a25a",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "c64760c8",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6c37ae3b",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4992944e",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "d45b9072",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "e280f91d",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "8d254fef",
"metadata": {},
"outputs": [],
@@ -206,13 +134,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -252,168 +174,333 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Lung Opacity'\n",
- " )\n",
- "evaluation.evaluate()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0008 +- 0.0007 | \n",
- " 0.0139 +- 0.0071 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.9735 +- 0.0022 | \n",
- " 0.6665 +- 0.0319 | \n",
- " 0.5759 +- 0.0573 | \n",
- " 0.9968 +- 0.0027 | \n",
- " 0.3362 +- 0.0659 | \n",
- " 0.7971 +- 0.0152 | \n",
- " 0.0246 +- 0.0018 | \n",
- " 0.1173 +- 0.0055 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.816 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.813 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9735 +- 0.0022 0.6665 +- 0.0319 0.5759 +- 0.0573 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.9968 +- 0.0027 0.3362 +- 0.0659 0.7971 +- 0.0152 \n",
- "HAIM -- -- 0.816 \n",
- "NON_HAIM -- -- 0.813 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0008 +- 0.0007 0.0139 +- 0.0071 \n",
- "test_metrics 0.0246 +- 0.0018 0.1173 +- 0.0055 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-10-22 09:17:01,087\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8266 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Outer fold 1\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Train indices: [ 0 1 2 ... 14132 14133 14135]\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Test indices: [ 8 20 21 ... 14118 14126 14134]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(raylet)\u001b[0m Spilled 2262 MiB, 2 objects, write throughput 473 MiB/s. Set RAY_verbose_spill_logs=0 to disable this message.\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Configuring PyCaret for outer fold 1\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.24s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [04:21<01:36, 96.70s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 0.9718 0.7957 0.9994 0.9721 0.9856 0.3613 0.4576\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 1 0.9757 0.8396 1.0000 0.9754 0.9875 0.4672 0.5521\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 2 0.9735 0.8690 0.9989 0.9743 0.9864 0.4188 0.4949\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 3 0.9746 0.8002 1.0000 0.9743 0.9870 0.4302 0.5235\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 4 0.9707 0.8133 0.9994 0.9710 0.9850 0.3205 0.4236\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Mean 0.9732 0.8236 0.9995 0.9734 0.9863 0.3996 0.4903\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Std 0.0018 0.0274 0.0004 0.0016 0.0009 0.0522 0.0457\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 Extreme Gradient Boosting 0.9788 0.8664 ... 0.9891 0.5686 0.6302\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Outer fold 2\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Train indices: [ 0 1 3 ... 14132 14134 14135]\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Test indices: [ 2 6 10 ... 14124 14131 14133]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Configuring PyCaret for outer fold 2\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.16s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [04:19<01:35, 95.91s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 0.9685 0.8111 1.0000 0.9684 0.9839 0.2130 0.3452\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 1 0.9713 0.8137 1.0000 0.9711 0.9853 0.3253 0.4407\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 2 0.9707 0.8495 0.9994 0.9710 0.9850 0.3034 0.4088\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 3 0.9729 0.8590 0.9989 0.9737 0.9861 0.3999 0.4793\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 4 0.9729 0.8500 0.9989 0.9737 0.9861 0.4136 0.4909\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Mean 0.9713 0.8367 0.9994 0.9716 0.9853 0.3310 0.4330\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Std 0.0016 0.0201 0.0005 0.0020 0.0008 0.0725 0.0526\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 Extreme Gradient Boosting 0.9759 0.8309 ... 0.9877 0.4833 0.5586\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Outer fold 3\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Train indices: [ 0 2 3 ... 14132 14133 14134]\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Test indices: [ 1 4 17 ... 14127 14128 14135]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Configuring PyCaret for outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.07s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [03:51<01:25, 85.76s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 0.9724 0.8201 0.9994 0.9727 0.9859 0.3810 0.4737\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 1 0.9751 0.8831 0.9994 0.9754 0.9873 0.4732 0.5476\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 2 0.9740 0.8461 1.0000 0.9738 0.9867 0.4110 0.5086\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 3 0.9746 0.7670 1.0000 0.9743 0.9870 0.4302 0.5235\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 4 0.9779 0.8824 1.0000 0.9776 0.9887 0.5365 0.6055\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Mean 0.9748 0.8398 0.9998 0.9747 0.9871 0.4464 0.5318\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Std 0.0018 0.0434 0.0003 0.0017 0.0009 0.0541 0.0440\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 Extreme Gradient Boosting 0.9738 0.8479 ... 0.9866 0.4215 0.51\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Outer fold 4\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Train indices: [ 1 2 3 ... 14133 14134 14135]\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Test indices: [ 0 5 9 ... 14103 14116 14119]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Configuring PyCaret for outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.10s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [03:53<01:26, 86.36s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 0.9740 0.7871 0.9994 0.9743 0.9867 0.4376 0.5193\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 1 0.9707 0.7265 0.9994 0.9710 0.9850 0.3205 0.4236\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 2 0.9768 0.8194 0.9994 0.9770 0.9881 0.5133 0.5793\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 3 0.9735 0.8589 0.9989 0.9743 0.9864 0.4188 0.4949\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 4 0.9746 0.7693 0.9994 0.9748 0.9870 0.4430 0.5235\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Mean 0.9739 0.7922 0.9993 0.9743 0.9866 0.4266 0.5081\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Std 0.0020 0.0449 0.0002 0.0019 0.0010 0.0620 0.0505\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 Extreme Gradient Boosting 0.9756 0.8936 ... 0.9875 0.4721 0.5559\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Train indices: [ 0 1 2 ... 14133 14134 14135]\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Test indices: [ 3 7 24 ... 14129 14130 14132]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.24s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [03:51<01:25, 85.70s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 0.9713 0.7538 1.0000 0.9711 0.9853 0.3253 0.4407\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 1 0.9718 0.8414 0.9989 0.9727 0.9856 0.3758 0.4594\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 2 0.9724 0.8019 0.9994 0.9727 0.9859 0.3660 0.4613\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 3 0.9701 0.7914 0.9994 0.9705 0.9848 0.2814 0.3898\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 4 0.9762 0.8357 1.0000 0.9760 0.9878 0.4851 0.5659\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Mean 0.9724 0.8048 0.9995 0.9726 0.9859 0.3667 0.4634\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Std 0.0021 0.0319 0.0004 0.0019 0.0010 0.0680 0.0574\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m 0 Extreme Gradient Boosting 0.9752 0.8579 ... 0.9873 0.4606 0.547\n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=419355)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.97172 0.002366\n",
+ "1 AUC 0.80930 0.011812\n",
+ "2 Recall 1.00000 0.000000\n",
+ "3 Prec. 0.97150 0.002347\n",
+ "4 F1 0.98554 0.001176\n",
+ "5 Kappa 0.33816 0.093030\n",
+ "6 MCC 0.44896 0.077605\n",
+ "Best hyperparameters across all folds: objective binary:logistic\n",
+ "base_score NaN\n",
+ "booster gbtree\n",
+ "callbacks NaN\n",
+ "colsample_bylevel NaN\n",
+ "colsample_bynode NaN\n",
+ "colsample_bytree NaN\n",
+ "device cpu\n",
+ "early_stopping_rounds NaN\n",
+ "enable_categorical False\n",
+ "eval_metric NaN\n",
+ "feature_types NaN\n",
+ "gamma NaN\n",
+ "grow_policy NaN\n",
+ "importance_type NaN\n",
+ "interaction_constraints NaN\n",
+ "learning_rate 0.05\n",
+ "max_bin NaN\n",
+ "max_cat_threshold NaN\n",
+ "max_cat_to_onehot NaN\n",
+ "max_delta_step NaN\n",
+ "max_depth 7.0\n",
+ "max_leaves NaN\n",
+ "min_child_weight NaN\n",
+ "missing NaN\n",
+ "monotone_constraints NaN\n",
+ "multi_strategy NaN\n",
+ "n_estimators 200.0\n",
+ "n_jobs 1.0\n",
+ "num_parallel_tree NaN\n",
+ "random_state 42.0\n",
+ "reg_alpha NaN\n",
+ "reg_lambda NaN\n",
+ "sampling_method NaN\n",
+ "scale_pos_weight NaN\n",
+ "subsample NaN\n",
+ "tree_method auto\n",
+ "validate_parameters NaN\n",
+ "verbosity 0.0\n",
+ "Name: 0, dtype: object\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Lung Opacity', constants.LUNG_OPACITY)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"LungOpacity\", experiment_name=\"CP_LungOpacity\", filepath=\"./results/lungopacity\")\n",
+ "\n",
+ "# Model training and results evaluation\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -425,7 +512,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/pneumonia.ipynb b/notebooks/pneumonia.ipynb
index 907ed91..68df631 100644
--- a/notebooks/pneumonia.ipynb
+++ b/notebooks/pneumonia.ipynb
@@ -55,14 +55,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -77,7 +74,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -103,7 +100,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -116,86 +113,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "4491a25a",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "c64760c8",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6c37ae3b",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4992944e",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "d45b9072",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "e280f91d",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "8d254fef",
"metadata": {},
"outputs": [],
@@ -204,13 +132,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -250,168 +172,257 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Pneumonia'\n",
- " )\n",
- "evaluation.evaluate()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0016 +- 0.0019 | \n",
- " 0.0288 +- 0.0191 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.7282 +- 0.0293 | \n",
- " 0.7398 +- 0.0244 | \n",
- " 0.7095 +- 0.0403 | \n",
- " 0.5433 +- 0.0885 | \n",
- " 0.9362 +- 0.0421 | \n",
- " 0.8714 +- 0.0126 | \n",
- " 0.1528 +- 0.0102 | \n",
- " 0.4679 +- 0.0308 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.883 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.876 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.7282 +- 0.0293 0.7398 +- 0.0244 0.7095 +- 0.0403 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.5433 +- 0.0885 0.9362 +- 0.0421 0.8714 +- 0.0126 \n",
- "HAIM -- -- 0.883 \n",
- "NON_HAIM -- -- 0.876 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0016 +- 0.0019 0.0288 +- 0.0191 \n",
- "test_metrics 0.1528 +- 0.0102 0.4679 +- 0.0308 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-09-27 17:42:40,522\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Outer fold 1\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Configuring PyCaret for outer fold 1\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Outer fold 2\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:02, 1.18it/s]\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [04:58<01:50, 110.38s/it]\u001b[32m [repeated 2x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 0 0.7535 0.8412 0.7789 0.7634 0.7711 0.5042 0.5043\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 1 0.7968 0.8702 0.8357 0.7938 0.8142 0.5902 0.5912\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 2 0.8076 0.8898 0.8215 0.8182 0.8198 0.6133 0.6133\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 3 0.8065 0.8876 0.8089 0.8240 0.8164 0.6119 0.6120\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 4 0.7987 0.8789 0.8435 0.7920 0.8169 0.5939 0.5953\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Mean 0.7926 0.8735 0.8177 0.7983 0.8077 0.5827 0.5832\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Std 0.0200 0.0176 0.0227 0.0216 0.0184 0.0403 0.0404\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Configuring PyCaret for outer fold 2\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n",
+ "Processing: 75%|███████▌ | 3/4 [05:02<01:51, 111.85s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 0 0.7892 0.8759 0.7931 0.8079 0.8004 0.5771 0.5772\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 1 0.7741 0.8655 0.7830 0.7910 0.7870 0.5465 0.5465\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 2 0.7676 0.8514 0.7992 0.7725 0.7856 0.5320 0.5323\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 3 0.7762 0.8740 0.8089 0.7789 0.7936 0.5494 0.5499\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 4 0.7630 0.8413 0.8008 0.7650 0.7825 0.5224 0.5231\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Mean 0.7740 0.8616 0.7970 0.7831 0.7898 0.5455 0.5458\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Std 0.0089 0.0134 0.0087 0.0150 0.0064 0.0186 0.0184\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 0 Extreme Gradient Boosting 0.7972 0.8831 ... 0.8113 0.5922 0.5924\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Outer fold 3\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Configuring PyCaret for outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ " \n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:02, 1.18it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [04:55<01:49, 109.48s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 0 0.8097 0.8857 0.8398 0.8102 0.8247 0.6168 0.6173\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 1 0.7827 0.8684 0.8053 0.7908 0.7980 0.5629 0.5631\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 2 0.7903 0.8739 0.8354 0.7844 0.8091 0.5770 0.5784\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 3 0.7946 0.8708 0.8232 0.7972 0.8100 0.5866 0.5870\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 4 0.7706 0.8686 0.7785 0.7881 0.7832 0.5396 0.5396\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Mean 0.7896 0.8735 0.8164 0.7941 0.8050 0.5766 0.5771\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Std 0.0130 0.0064 0.0224 0.0091 0.0138 0.0256 0.0258\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 0 Extreme Gradient Boosting 0.8166 0.9027 ... 0.8278 0.6317 0.6317\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Outer fold 4\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Configuring PyCaret for outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:02, 1.19it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [04:57<01:50, 110.12s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 0 0.8000 0.8672 0.8337 0.7996 0.8163 0.5970 0.5977\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 1 0.7795 0.8587 0.7911 0.7943 0.7927 0.5571 0.5571\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 2 0.7892 0.8676 0.8211 0.7906 0.8056 0.5755 0.5760\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 3 0.7870 0.8728 0.8069 0.7956 0.8012 0.5719 0.5720\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 4 0.7933 0.8766 0.8232 0.7957 0.8092 0.5838 0.5842\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Mean 0.7898 0.8686 0.8152 0.7952 0.8050 0.5771 0.5774\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Std 0.0068 0.0060 0.0148 0.0029 0.0079 0.0132 0.0134\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 0 Extreme Gradient Boosting 0.8194 0.8958 ... 0.8326 0.6366 0.6368\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Configuring PyCaret for outer fold 5\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:00<00:02, 1.18it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [04:54<01:49, 109.16s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 0 0.7643 0.8667 0.7972 0.7691 0.7829 0.5254 0.5258\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 1 0.7849 0.8718 0.8114 0.7905 0.8008 0.5670 0.5673\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 2 0.8076 0.8769 0.8333 0.8103 0.8216 0.6128 0.6131\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 3 0.7827 0.8697 0.8008 0.7928 0.7968 0.5633 0.5634\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 4 0.7684 0.8509 0.7886 0.7791 0.7838 0.5344 0.5345\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Mean 0.7816 0.8672 0.8063 0.7883 0.7972 0.5606 0.5608\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Std 0.0152 0.0088 0.0154 0.0139 0.0141 0.0307 0.0307\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m 0 Extreme Gradient Boosting 0.8048 0.8925 ... 0.8224 0.6063 0.6076\n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=678263)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m 0 Extreme Gradient Boosting 0.7979 0.8916 ... 0.8119 0.5936 0.5937\n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=678258)\u001b[0m [1 rows x 8 columns]\n",
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.79220 0.014893\n",
+ "1 AUC 0.87688 0.012199\n",
+ "2 Recall 0.81380 0.016299\n",
+ "3 Prec. 0.79992 0.014935\n",
+ "4 F1 0.80676 0.013924\n",
+ "5 Kappa 0.58210 0.029971\n",
+ "6 MCC 0.58230 0.029977\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Pneumonia', constants.PNEUMONIA)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"Pneumonia\", experiment_name=\"CP_Pneumonia\", filepath=\"./results\")\n",
+ "\n",
+ "# Model training and results evaluation\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -423,7 +434,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/notebooks/pneumothorax.ipynb b/notebooks/pneumothorax.ipynb
index dbd6df3..89ca9d0 100644
--- a/notebooks/pneumothorax.ipynb
+++ b/notebooks/pneumothorax.ipynb
@@ -55,14 +55,11 @@
"import os\n",
"os.chdir('../')\n",
"\n",
- "from xgboost import XGBClassifier\n",
"from pandas import read_csv\n",
"\n",
"from src.data import constants\n",
"from src.data.dataset import HAIMDataset\n",
- "from src.data.sampling import Sampler\n",
- "from src.evaluation.evaluating import Evaluator\n",
- "from src.evaluation.tuning import SklearnTuner\n",
+ "from src.evaluation.pycaret_evaluator import PyCaretEvaluator\n",
"from src.utils.metric_scores import *"
]
},
@@ -77,7 +74,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "a948c18c",
"metadata": {},
"outputs": [],
@@ -103,7 +100,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 3,
"id": "239db8a3",
"metadata": {},
"outputs": [],
@@ -116,86 +113,17 @@
" constants.GLOBAL_ID)"
]
},
- {
- "cell_type": "markdown",
- "id": "4491a25a",
- "metadata": {},
- "source": [
- "#### Create the sampler\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "c64760c8",
- "metadata": {},
- "source": [
- "Sample the data using a 5 folds cross-validation method based on unique ``haim_id`` "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "d8cc844e",
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "100%|███████████████████████████████████████████| 5/5 [00:00<00:00, 2788.77it/s]\n"
- ]
- }
- ],
- "source": [
- "sampler = Sampler(dataset, constants.GLOBAL_ID, 5)\n",
- "_, masks = sampler()"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6c37ae3b",
- "metadata": {},
- "source": [
- "#### Select the evaluation metrics"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4992944e",
- "metadata": {},
- "source": [
- "Initilialize a list containing the evaluation metrics to report"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "d45b9072",
- "metadata": {},
- "outputs": [],
- "source": [
- "# Initialization of the list containing the evaluation metrics\n",
- "evaluation_metrics = [BinaryAccuracy(), \n",
- " BinaryBalancedAccuracy(),\n",
- " BinaryBalancedAccuracy(Reduction.GEO_MEAN),\n",
- " Sensitivity(), \n",
- " Specificity(), \n",
- " AUC(), \n",
- " BrierScore(),\n",
- " BinaryCrossEntropy()]"
- ]
- },
{
"cell_type": "markdown",
"id": "e280f91d",
"metadata": {},
"source": [
- "#### Set hyper-parameters and fixed parameters"
+ "#### Set hyper-parameters"
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"id": "8d254fef",
"metadata": {},
"outputs": [],
@@ -204,13 +132,7 @@
"grid_hps = {'max_depth': [5, 6, 7, 8],\n",
" 'n_estimators': [200, 300],\n",
" 'learning_rate': [0.3, 0.1, 0.05],\n",
- " }\n",
- "\n",
- "# Save the fixed parameters of the model\n",
- "fixed_params = {'seed': 42,\n",
- " 'eval_metric': 'logloss',\n",
- " 'verbosity': 0\n",
- " }"
+ " }"
]
},
{
@@ -250,168 +172,335 @@
},
{
"cell_type": "code",
- "execution_count": 14,
+ "execution_count": 5,
"id": "726c2332",
"metadata": {},
- "outputs": [],
- "source": [
- "evaluation = Evaluator(dataset=dataset,\n",
- " masks=masks,\n",
- " metrics=evaluation_metrics,\n",
- " model=XGBClassifier,\n",
- " tuner=SklearnTuner,\n",
- " tuning_metric=AUC(),\n",
- " hps=grid_hps,\n",
- " n_tuning_splits=5,\n",
- " fixed_params=fixed_params,\n",
- " filepath=constants.EXPERIMENT_PATH,\n",
- " weight='scale_pos_weight',\n",
- " evaluation_name='CP_Pneumothorax'\n",
- " )\n",
- "evaluation.evaluate()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6787a8cc",
- "metadata": {},
- "source": [
- "#### Comparison with the paper results:\n",
- "\n",
- "\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "b517ec5e",
- "metadata": {},
"outputs": [
{
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " Accuracy | \n",
- " BalancedAcc | \n",
- " GeoBalancedAcc | \n",
- " Sensitivity | \n",
- " Specificity | \n",
- " AUC | \n",
- " BrierScore | \n",
- " BCE | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " | train_metrics | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.0002 +- 0.0002 | \n",
- " 0.0081 +- 0.0047 | \n",
- "
\n",
- " \n",
- " | test_metrics | \n",
- " 0.8754 +- 0.0165 | \n",
- " 0.6423 +- 0.035 | \n",
- " 0.5295 +- 0.0642 | \n",
- " 0.2845 +- 0.0699 | \n",
- " 1.0 +- 0.0 | \n",
- " 0.8114 +- 0.0208 | \n",
- " 0.1066 +- 0.0128 | \n",
- " 0.4283 +- 0.0531 | \n",
- "
\n",
- " \n",
- " | HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.836 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- " | NON_HAIM | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " -- | \n",
- " 0.804 | \n",
- " -- | \n",
- " -- | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " Accuracy BalancedAcc GeoBalancedAcc \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.8754 +- 0.0165 0.6423 +- 0.035 0.5295 +- 0.0642 \n",
- "HAIM -- -- -- \n",
- "NON_HAIM -- -- -- \n",
- "\n",
- " Sensitivity Specificity AUC \\\n",
- "train_metrics 1.0 +- 0.0 1.0 +- 0.0 1.0 +- 0.0 \n",
- "test_metrics 0.2845 +- 0.0699 1.0 +- 0.0 0.8114 +- 0.0208 \n",
- "HAIM -- -- 0.836 \n",
- "NON_HAIM -- -- 0.804 \n",
- "\n",
- " BrierScore BCE \n",
- "train_metrics 0.0002 +- 0.0002 0.0081 +- 0.0047 \n",
- "test_metrics 0.1066 +- 0.0128 0.4283 +- 0.0531 \n",
- "HAIM -- -- \n",
- "NON_HAIM -- -- "
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "2024-10-21 16:37:00,227\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Outer fold 1\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Train indices: [ 0 1 2 ... 17156 17157 17158]\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Test indices: [ 13 14 15 ... 17135 17147 17155]\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Configuring PyCaret for outer fold 1\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Outer fold 2\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Train indices: [ 0 1 2 ... 17155 17157 17158]\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Test indices: [ 3 6 8 ... 17152 17154 17156]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.27s/it]\n",
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 75%|███████▌ | 3/4 [07:00<02:35, 155.51s/it]\u001b[32m [repeated 2x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)\u001b[0m\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 0 0.9126 0.8939 0.5759 0.8800 0.6962 0.6477 0.6676\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 1 0.9144 0.9043 0.5733 0.8975 0.6997 0.6526 0.6749\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 2 0.9030 0.8527 0.5249 0.8621 0.6525 0.6000 0.6250\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 3 0.9144 0.8781 0.5591 0.9142 0.6938 0.6474 0.6739\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 4 0.9071 0.8892 0.4961 0.9403 0.6495 0.6018 0.6427\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Mean 0.9103 0.8836 0.5459 0.8988 0.6783 0.6299 0.6568\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Std 0.0045 0.0176 0.0308 0.0271 0.0224 0.0238 0.0197\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Configuring PyCaret for outer fold 2\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n",
+ "Processing: 75%|███████▌ | 3/4 [07:03<02:36, 156.57s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 0 0.9103 0.8928 0.5497 0.8936 0.6807 0.6320 0.6572\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 1 0.9098 0.8933 0.5550 0.8833 0.6817 0.6323 0.6555\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 2 0.9057 0.8889 0.5302 0.8783 0.6612 0.6103 0.6366\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 3 0.8998 0.8724 0.4961 0.8710 0.6321 0.5791 0.6099\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 4 0.9048 0.8937 0.5328 0.8675 0.6602 0.6085 0.6329\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Mean 0.9061 0.8882 0.5328 0.8787 0.6632 0.6124 0.6384\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Std 0.0038 0.0081 0.0207 0.0093 0.0180 0.0195 0.0173\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 0 Extreme Gradient Boosting 0.9219 0.9006 ... 0.7304 0.6868 0.706\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Outer fold 3\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Train indices: [ 2 3 4 ... 17154 17155 17156]\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Test indices: [ 0 1 10 ... 17149 17157 17158]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Configuring PyCaret for outer fold 3\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.25s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [06:48<02:31, 151.02s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 0 Extreme Gradient Boosting 0.9181 0.9126 ... 0.7082 0.6637 0.6901\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Outer fold 4\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Train indices: [ 0 1 2 ... 17156 17157 17158]\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Test indices: [ 4 5 9 ... 17139 17141 17150]\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Configuring PyCaret for outer fold 4\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n",
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.23s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 0 0.9030 0.8693 0.5157 0.8756 0.6491 0.5972 0.6254\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 1 0.8966 0.8647 0.4843 0.8605 0.6198 0.5653 0.5966\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 2 0.9030 0.8771 0.5144 0.8750 0.6479 0.5960 0.6244\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 3 0.9167 0.8959 0.5906 0.8929 0.7109 0.6646 0.6840\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 4 0.9062 0.8833 0.5118 0.9070 0.6544 0.6049 0.6381\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Mean 0.9051 0.8781 0.5234 0.8822 0.6564 0.6056 0.6337\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Std 0.0066 0.0110 0.0355 0.0161 0.0298 0.0324 0.0286\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 75%|███████▌ | 3/4 [06:47<02:30, 150.81s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 0 0.8999 0.8737 0.5079 0.8584 0.6382 0.5844 0.6116\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 1 0.9021 0.8722 0.4974 0.8920 0.6387 0.5872 0.6208\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 2 0.9030 0.8825 0.5171 0.8717 0.6491 0.5970 0.6245\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 3 0.9103 0.8773 0.5643 0.8740 0.6858 0.6363 0.6570\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 4 0.9199 0.8796 0.5696 0.9476 0.7115 0.6683 0.6975\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Mean 0.9070 0.8771 0.5312 0.8887 0.6646 0.6147 0.6423\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Std 0.0073 0.0038 0.0299 0.0313 0.0292 0.0326 0.0316\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 0 Extreme Gradient Boosting 0.9222 0.9172 ... 0.7216 0.6794 0.7077\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m [1 rows x 8 columns]\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Outer fold 5\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Train indices: [ 0 1 3 ... 17156 17157 17158]\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Test indices: [ 2 7 12 ... 17146 17151 17153]\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m \n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 0%| | 0/4 [00:00, ?it/s]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Configuring PyCaret for outer fold 5\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m 0 Extreme Gradient Boosting 0.9301 0.9202 ... 0.7595 0.7204 0.7395\n",
+ "\u001b[36m(run_fold pid=352122)\u001b[0m [1 rows x 8 columns]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Processing: 25%|██▌ | 1/4 [00:01<00:03, 1.21s/it]\n",
+ "Processing: 75%|███████▌ | 3/4 [06:26<02:23, 143.10s/it]\n",
+ " \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Accuracy AUC Recall Prec. F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Fold \n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 0 0.9085 0.8677 0.5419 0.8884 0.6732 0.6236 0.6494\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 1 0.9053 0.8679 0.5288 0.8783 0.6601 0.6090 0.6355\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 2 0.8966 0.8590 0.4672 0.8812 0.6106 0.5574 0.5948\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 3 0.9048 0.8603 0.5092 0.8981 0.6499 0.5997 0.6321\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 4 0.9121 0.8828 0.5591 0.8950 0.6882 0.6402 0.6642\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Mean 0.9055 0.8675 0.5212 0.8882 0.6564 0.6060 0.6352\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Std 0.0051 0.0085 0.0316 0.0077 0.0262 0.0279 0.0232\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Tuning hyperparameters for model xgboost with custom grid using grid search\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Transformation Pipeline and Model Successfully Saved\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m Model Accuracy AUC ... F1 Kappa MCC\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m 0 Extreme Gradient Boosting 0.9196 0.9191 ... 0.7172 0.673 0.6962\n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m \n",
+ "\u001b[36m(run_fold pid=352119)\u001b[0m [1 rows x 8 columns]\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\u001b[36m(raylet)\u001b[0m Spilled 7659 MiB, 15 objects, write throughput 1327 MiB/s. Set RAY_verbose_spill_logs=0 to disable this message.\n",
+ "\u001b[33m(raylet)\u001b[0m [2024-10-22 15:55:01,994 E 352020 352020] (raylet) node_manager.cc:3065: 1 Workers (tasks / actors) killed due to memory pressure (OOM), 0 Workers crashed due to other reasons at node (ID: 852018e6044b47b3741b52a8d7dd68d1f083fb92e9c7b6b292aa1541, IP: 10.44.86.85) over the last time period. To see more information about the Workers killed on this node, use `ray logs raylet.out -ip 10.44.86.85`\n",
+ "\u001b[33m(raylet)\u001b[0m \n",
+ "\u001b[33m(raylet)\u001b[0m Refer to the documentation on how to address the out of memory issue: https://docs.ray.io/en/latest/ray-core/scheduling/ray-oom-prevention.html. Consider provisioning more memory on this node or reducing task parallelism by requesting more CPUs per task. To adjust the kill threshold, set the environment variable `RAY_memory_usage_threshold` when starting Ray. To disable worker killing, set the environment variable `RAY_memory_monitor_refresh_ms` to zero.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Final metrics table:\n",
+ " Metric Mean Std Dev\n",
+ "0 Accuracy 0.91280 0.003134\n",
+ "1 AUC 0.89782 0.012412\n",
+ "2 Recall 0.55234 0.022815\n",
+ "3 Prec. 0.91136 0.011377\n",
+ "4 F1 0.68752 0.016419\n",
+ "5 Kappa 0.64042 0.017331\n",
+ "6 MCC 0.66772 0.013369\n",
+ "Best hyperparameters across all folds: objective binary:logistic\n",
+ "base_score NaN\n",
+ "booster gbtree\n",
+ "callbacks NaN\n",
+ "colsample_bylevel NaN\n",
+ "colsample_bynode NaN\n",
+ "colsample_bytree NaN\n",
+ "device cpu\n",
+ "early_stopping_rounds NaN\n",
+ "enable_categorical False\n",
+ "eval_metric NaN\n",
+ "feature_types NaN\n",
+ "gamma NaN\n",
+ "grow_policy NaN\n",
+ "importance_type NaN\n",
+ "interaction_constraints NaN\n",
+ "learning_rate 0.1\n",
+ "max_bin NaN\n",
+ "max_cat_threshold NaN\n",
+ "max_cat_to_onehot NaN\n",
+ "max_delta_step NaN\n",
+ "max_depth 8\n",
+ "max_leaves NaN\n",
+ "min_child_weight NaN\n",
+ "missing NaN\n",
+ "monotone_constraints NaN\n",
+ "multi_strategy NaN\n",
+ "n_estimators 300\n",
+ "n_jobs 1\n",
+ "num_parallel_tree NaN\n",
+ "random_state 42\n",
+ "reg_alpha NaN\n",
+ "reg_lambda NaN\n",
+ "sampling_method NaN\n",
+ "scale_pos_weight NaN\n",
+ "subsample NaN\n",
+ "tree_method auto\n",
+ "validate_parameters NaN\n",
+ "verbosity 0\n",
+ "Name: 0, dtype: object\n"
+ ]
}
],
"source": [
- "Evaluator.visualize_results('experiments/CP_Pneumothorax', constants.PNEUMOTHORAX)"
+ "# Initialize the PyCaret Evaluator\n",
+ "evaluator = PyCaretEvaluator(dataset=dataset, target=\"Pneumothorax\", experiment_name=\"CP_Pneumothorax\", filepath=\"./results/pneumothorax\")\n",
+ "\n",
+ "# Model training and results evaluation\n",
+ "evaluator.run_experiment(\n",
+ " train_size=0.8,\n",
+ " fold=5,\n",
+ " fold_strategy='stratifiedkfold',\n",
+ " outer_fold=5,\n",
+ " outer_strategy='stratifiedkfold',\n",
+ " session_id=42,\n",
+ " model='xgboost',\n",
+ " optimize='AUC',\n",
+ " custom_grid=grid_hps\n",
+ ")"
]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2ce87c55",
- "metadata": {},
- "outputs": [],
- "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "HAIM",
+ "display_name": "Python (pycaret_env)",
"language": "python",
- "name": "haim"
+ "name": "pycaret_env"
},
"language_info": {
"codemirror_mode": {
@@ -423,7 +512,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.11.9"
}
},
"nbformat": 4,
diff --git a/requirements.txt b/requirements.txt
index d3a24e3..ccb3e09 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,8 @@
pandas~=1.4.4
numpy~=1.23.4
-scikit-learn~=1.0.2
+scikit-learn~=1.4.2
matplotlib~=3.5.2
xgboost~=1.7.1
-tqdm~=4.64.1
\ No newline at end of file
+tqdm~=4.64.1
+pycaret~=3.3.1
+ray[default]~=2.36.1
\ No newline at end of file
diff --git a/run_experiments.py b/run_experiments.py
index c4ce522..54bf828 100644
--- a/run_experiments.py
+++ b/run_experiments.py
@@ -1,39 +1,36 @@
"""
Filename: run_experiments.py
-Author : Hakima Laribi
+Author : Hakima Laribi, Mariem Kallel
Description: This file is used to perform all the HAIM experiments presented
- in the paper: https://doi.org/10.1038/s41746-022-00689-4
+ in the paper: https://doi.org/10.1038/s41746-022-00689-4
Date of last modification : 2023/02/07
"""
import argparse
+import os
from itertools import combinations
-from tqdm import tqdm
-from typing import List, Callable, Optional
+from typing import List, Optional
from numpy import unique
from pandas import read_csv, DataFrame
-from xgboost import XGBClassifier
+from tqdm import tqdm
from src.data import constants
-from src.data.dataset import Task, HAIMDataset
-from src.data.sampling import Sampler
-from src.evaluation.evaluating import Evaluator
-from src.evaluation.tuning import SklearnTuner
-from src.utils.metric_scores import *
+from src.data.dataset import HAIMDataset
+from src.evaluation.pycaret_evaluator import PyCaretEvaluator
def get_all_sources_combinations(sources: List[str]) -> List[List[str]]:
"""
- Function to extract all possible combinations of sources
+ Function to extract all possible combinations of sources
- Args:
- sources(List[str]): list of sources types
+ Args:
+ sources(List[str]): list of sources types
- Returns: list of combinations
+ Returns: list of combinations
"""
comb = []
for i in range(len(sources)):
@@ -50,15 +47,20 @@ def run_single_experiment(prediction_task: str,
dataset: Optional[DataFrame] = None,
evaluation_name: Optional[str] = None) -> None:
"""
- Function to perform one single experiment
-
- Args:
- prediction_task(task): task label, must be a HAIM prediction task
- sources_predictors(List[str]): predictors to use for prediction, each source has one or more predictors
- sources_modalities(List[str]): the modalities of the sources used for prediction
- dataset(Optional[DataFrame]): HAIM dataframe
- evaluation_name(Optional[str]): name of the experiment
+ Function to perform one single experiment
+
+ Args:
+ prediction_task(task): task label, must be a HAIM prediction task
+ sources_predictors(List[str]): predictors to use for prediction, each source has one or more predictors
+ sources_modalities(List[str]): the modalities of the sources used for prediction
+ dataset(Optional[DataFrame]): HAIM dataframe
+ evaluation_name(Optional[str]): name of the experiment
"""
+
+ # Set up the folder path specific to the prediction task
+ task_folder = f"experiments/{prediction_task}"
+ if not os.path.exists(task_folder):
+ os.makedirs(task_folder)
dataset = read_csv(constants.FILE_DF, nrows=constants.N_DATA) if dataset is None else dataset
# Create the HAIMDataset
@@ -69,47 +71,29 @@ def run_single_experiment(prediction_task: str,
constants.IMG_ID,
constants.GLOBAL_ID)
- # Sample the dataset using a 5-folds cross-validation method
- sampler = Sampler(dataset, constants.GLOBAL_ID, 5)
- _, masks = sampler()
-
- # Initialization of the list containing the evaluation metrics
- evaluation_metrics = [BinaryAccuracy(),
- BinaryBalancedAccuracy(),
- BinaryBalancedAccuracy(Reduction.GEO_MEAN),
- Sensitivity(),
- Specificity(),
- AUC(),
- BrierScore(),
- BinaryCrossEntropy()]
-
# Define the grid of hyper-parameters for the tuning
grid_hps = {'max_depth': [5, 6, 7, 8],
'n_estimators': [200, 300],
- 'learning_rate': [0.3, 0.1, 0.05],
- }
-
- # Save the fixed parameters of the model
- fixed_params = {'seed': 42,
- 'eval_metric': 'logloss',
- 'verbosity': 1
- }
-
- # Launch the evaluation
- evaluation = Evaluator(dataset=dataset,
- masks=masks,
- metrics=evaluation_metrics,
- model=XGBClassifier,
- tuner=SklearnTuner,
- tuning_metric=AUC(),
- hps=grid_hps,
- n_tuning_splits=5,
- fixed_params=fixed_params,
- filepath=constants.EXPERIMENT_PATH,
- weight='scale_pos_weight',
- evaluation_name=evaluation_name
- )
- evaluation.evaluate()
+ 'learning_rate': [0.3, 0.1, 0.05]}
+
+ # Initialize the PyCaret Evaluator
+ evaluator = PyCaretEvaluator(dataset=dataset,
+ target=prediction_task,
+ experiment_name=evaluation_name,
+ filepath=task_folder)
+
+ # Model training and results evaluation
+ evaluator.run_experiment(
+ train_size=0.8,
+ fold=5,
+ fold_strategy='stratifiedkfold',
+ outer_fold=5,
+ outer_strategy='stratifiedkfold',
+ session_id=42,
+ model='xgboost',
+ optimize='AUC',
+ custom_grid=grid_hps
+ )
if __name__ == '__main__':
@@ -122,25 +106,34 @@ def run_single_experiment(prediction_task: str,
# Load the dataframe from disk
df = read_csv(constants.FILE_DF, nrows=constants.N_DATA)
- all_tasks = Task() if args.task is None else [args.task]
+ # Handle all tasks if none specified
+ all_tasks = [args.task] if args.task else [constants.FRACTURE, constants.PNEUMOTHORAX, constants.PNEUMONIA,
+ constants.LUNG_OPACITY, constants.LUNG_LESION, constants.ENLARGED_CARDIOMEDIASTINUM,
+ constants.EDEMA, constants.CONSOLIDATION, constants.CARDIOMEGALY,
+ constants.ATELECTASIS, constants.LOS, constants.MORTALITY]
+
for task in all_tasks:
- print("#"*23, f"{task} experiment", "#"*23)
+ print("#" * 23, f"{task} experiment", "#" * 23)
+
# Get all possible combinations of sources for the current task
sources_comb = get_all_sources_combinations(constants.SOURCES) if task in [constants.MORTALITY, constants.LOS] \
else get_all_sources_combinations(constants.CHEST_SOURCES)
with tqdm(total=len(sources_comb)) as bar:
for count, combination in enumerate(sources_comb):
-
# Get all predictors and modalities for each source
predictors = []
for c in combination:
- predictors = predictors + c.sources
+ predictors.extend(c.sources) # Collect all predictors
modalities = unique([c.modality for c in combination])
- run_single_experiment(prediction_task=task, sources_predictors=predictors, sources_modalities=modalities,
- dataset=df, evaluation_name=task + '_' + str(count))
+ # Run the single experiment
+ run_single_experiment(prediction_task=task,
+ sources_predictors=predictors,
+ sources_modalities=modalities,
+ dataset=df,
+ evaluation_name=task + '_' + str(count))
bar.update()
- Evaluator.get_best_of_experiments(task, constants.EXPERIMENT_PATH, count)
+
diff --git a/src/evaluation/evaluating.py b/src/evaluation/evaluating.py
deleted file mode 100644
index a463919..0000000
--- a/src/evaluation/evaluating.py
+++ /dev/null
@@ -1,439 +0,0 @@
-"""
-Filename: evaluating.py
-
-Author : Hakima Laribi
-
-Description: This file is used to store Evaluator object which performs different evaluations on the dataset
-
-Date of last modification : 2023/02/07
-
-"""
-import json
-from os import makedirs, path
-from re import search
-import shutil
-from time import strftime
-from typing import Any, Callable, Dict, Union, List, Optional
-
-from src.data.dataset import HAIMDataset
-from src.evaluation.tuning import SklearnHpsOptimizer
-
-import matplotlib.pyplot as plt
-import numpy as np
-import pandas as pd
-from sklearn.metrics import roc_curve
-
-from src.data import constants
-from src.utils.metric_scores import Metric
-
-
-class Evaluator:
- """
- Object used to perform models evaluations
- """
-
- def __init__(self,
- dataset: HAIMDataset,
- masks: Dict[int, Dict[str, List[int]]],
- metrics: List[Callable],
- model: Callable,
- tuner: Callable,
- tuning_metric: Metric,
- hps: Dict[str, Union[List[Any], Any]],
- n_tuning_splits: int,
- fixed_params: Dict[str, Any],
- filepath: str,
- model_selector: str = SklearnHpsOptimizer.GS,
- parallel_tuning: bool = True,
- weight: Optional[str] = None,
- evaluation_name: Optional[str] = None,
- stratified_sampling: bool = False):
- """
- Sets protected attributes of the Evaluator
-
- Args:
- dataset(HAIMDataset): custom HAIM dataset
- masks(Dict[int, Dict[str, List[int]]]): dictionary with train, test and valid set at each split
- metrics(List[Callable]): list of metrics to report at the end of the experiment
- model(Callable): model to evaluate
- tuner(Callable): tuner which perform hyper-parameter tuning
- tuning_metric(Metric): metric to optimize (maximize/minimize) during the hyper-parameter tuning
- n_tuning_splits(int): number of inner data splits, used in the tuning
- hps(Dict[str, Union[List[Any], Any]]): each hyper-parameter with its search space
- fixed_params(Dict[str, Any]): model's fixed parameters
- filepath(str): path to the directory where to store the experiment
- model_selector(str): hyper-parameter optimizer
- parallel_tuning(hps): boolean to specify if the tuning would be in parallel
- weight(str): weight parameter
- evaluation_name(str): name of current evaluation
- stratified_sampling(bool): if the sampling performed was stratified
-
- """
-
- if evaluation_name is not None:
- if path.exists(path.join(filepath, evaluation_name)):
- raise ValueError("evaluation with this name already exists")
- else:
- makedirs(filepath, exist_ok=True)
- evaluation_name = f"{strftime('%Y%m%d-%H%M%S')}"
-
- # Set protected attributes
- self._dataset = dataset
- self._masks = masks
- self._metrics = metrics
- self._model = model
- self._tuner = tuner(tuning_metric, hps, n_tuning_splits, model_selector, parallel_tuning)
- self._inner_splits = n_tuning_splits
- self._fixed_params = fixed_params
- self._weighted_param = weight
- self._filepath = path.join(filepath, evaluation_name)
- self._stratified_sampling = stratified_sampling
-
- # Set public attributes
- self.evaluation_name = evaluation_name
-
- def evaluate(self) -> None:
- """
- Performs nested evaluation and saves the result of each experiment in a json file
- """
- # Perform the evaluation over all the splits of the dataset
- for i, mask in self._masks.items():
- # Extract masks
- train, test, valid = mask['train'], mask['test'], mask['valid']
- # Get data and targets for each mask
- x, y = {}, {}
- for (mask_, idx) in [('train', train), ('test', test), ('valid', valid)]:
- if idx is not None:
- x[mask_], y[mask_] = self._dataset[idx]
- else:
- x[mask_], y[mask_] = None, None
-
- if self._weighted_param is not None:
- # Compute weights to assign to the positive class
- positive_weight = (len(y['train']) - sum(y['train'])) / sum(y['train'])
- # Update the fixed parameters of the model
- self._fixed_params[self._weighted_param] = positive_weight
- if self._inner_splits > 0:
- # Perform the tuning to extract the model with the best hyper-pramaters
- best_model = self._tuner.tune(self._model(**self._fixed_params), x['train'], y['train'])
- best_hps = self._tuner.get_best_hps()
-
- else:
- best_model = self._model(**self._fixed_params)
- best_hps = self._fixed_params
-
- # Get probabilities predicted for each mask
- y_proba = {}
- for mask_ in ['train', 'test', 'valid']:
- y_proba[mask_] = best_model.predict_proba(x[mask_])[:, 1] if x[mask_] is not None else None
-
- # Get predictions on the training set and compute the optimal threshold on the training set
- threshold = self.optimize_j_statistic(y['train'], y_proba['train'])
-
- # Save the experiment of the current split in a json file
- self.record_experiment(y_proba, y, threshold, mask, i, best_hps)
-
- # Summarize experiment over all the splits
- self.summarize_experiment(i + 1)
-
- @staticmethod
- def optimize_j_statistic(targets: List[int],
- pred: List[float]) -> float:
- """
- Finds the optimal threshold from ROC curve that separates the negative and positive classes
- by optimizing the Youden's J statistics
- J = TruePositiveRate – FalsePositiveRate
-
- Args:
- targets(List[int]): ground truth labels
- pred(List[float]): predicted probabilities to belong to the positive class
-
- Returns a float representing the optimal threshold
- """
- # Calculate roc curves
- fpr, tpr, thresholds = roc_curve(targets, pred, pos_label=1)
-
- # Get the best threshold
- J = tpr - fpr
- threshold = thresholds[np.argmax(J)]
-
- return threshold
-
- def record_experiment(self,
- predictions: Dict[str, List[float]],
- targets: Dict[str, List[int]],
- threshold: float,
- masks: Dict[str, List[int]],
- split: int,
- hps: Dict[str, Any]
- ) -> None:
- """
- Records the results of one single experiment on the test, train and valid sets in a json file
-
- Args:
- predictions(Dict[str, List[float]]): probabilities predicted on all the sets
- targets(Dict[str, List[int]]): ground truth labels of observation in each set
- threshold(float): prediction threshold
- masks(Dict[str, List[int]]): train, test and valid masks
- split(int): index of the current split
- hps(Dict[str, Any]): best hyper-parameters selected after the tuning
-
- """
- # Create the saving directory
- saving_path = self._filepath + '/split_' + str(split)
- makedirs(saving_path)
-
- # Initialize the file structure
- summary = {'split': str(split),
- 'sources': str(self._dataset.sources),
- 'task': self._dataset.task,
- 'threshold': str(threshold),
- 'hyper-parameters': hps}
- # Save statistics of each set
- for mask, idx in masks.items():
- if idx is not None:
- # Save number of elements in current set
- summary['N_' + mask + 'ed'] = len(idx)
-
- # If observations has a global_id according to which the sampling was performed
- if (self._dataset.global_ids is not None) and (not self._stratified_sampling):
- summary['N_' + mask + 'ed_global_ids'] = len(self._dataset.get_global_ids(idx))
-
- summary[f"proportion_positive_class_{mask}ed"] = ''
-
- # Initialize the metrics recorded
- summary[mask + '_metrics'] = {}
-
- # Get metrics and prediction values for each mask
- for mask, idx in masks.items():
- if idx is not None:
-
- # Get predicted probabilities and ground truth labels and predicted classes
- y_proba, target = predictions[mask], targets[mask]
- y_pred = (y_proba >= threshold).astype(float)
-
- # Save proportion of classes
- summary[f"proportion_positive_class_{mask}ed"] = f"{round(np.sum(target) / len(target), 4) * 100} %"
-
- # Save metrics for current mask
- for metric in self._metrics:
- summary[mask + '_metrics'][metric.name] = str(metric(y_proba, target, threshold))
-
- # Initialize the predictions recorded
- summary[mask + '_predictions'] = {}
-
- # Save predictions for each element
- if (self._dataset.global_ids is not None) and (not self._stratified_sampling):
- # Map indexes to global ids
- map_idx_global_ids = self._dataset.map_idx_to_global_ids()
-
- # Get the global_ids of the indexes present in the current set
- global_ids = self._dataset.get_global_ids(idx)
-
- # Map indexes to their position in the current mask
- map_idx_positions = {index: i for i, index in enumerate(idx)}
-
- # Map each index to its id in the dataset
- map_idx_to_ids = self.reverse_map(self._dataset.map_idx_to_ids())
-
- # Save predictions for each global id
- for global_id in global_ids:
- summary[mask + '_predictions'][str(global_id)] = {}
-
- # Initialize the information structure for each global_id
- summary[mask + '_predictions'][str(global_id)] = {}
-
- # Get the indexes of the observations present in the global id
- indexes = [i for i in map_idx_global_ids[global_id] if i in idx]
-
- # Save predictions for each observation
- for index in indexes:
- summary[mask + '_predictions'][str(global_id)][str(index)] = {
- 'id': str(map_idx_to_ids[index]),
- 'prediction': str(y_pred[map_idx_positions[index]]),
- 'probability': str(y_proba[map_idx_positions[index]]),
- 'target': str(target[map_idx_positions[index]])
- }
- else:
- # Map each index to its id in the dataset
- map_idx_to_ids = self.reverse_map(self._dataset.map_idx_to_ids())
-
- # Save predictions of each observation independently
- for i, index in enumerate(idx):
- summary[mask + '_predictions'][str(index)] = {
- 'index': str(index),
- 'id': str(map_idx_to_ids[index]),
- 'prediction': str(y_pred[i]),
- 'probability': str(y_proba[i]),
- 'target': str(target[i])
- }
-
- # Generate ROC curve
- self.plot_roc_curve(saving_path, target, y_proba, mask)
-
- # Generate the Json file of the split
- with open(path.join(saving_path, 'records.json'), "w") as file:
- json.dump(summary, file, indent=True)
-
- def summarize_experiment(self,
- n_splits: int
- ) -> None:
- """
- Summarizes an experiment performed on different splits of the dataset ans saves it in a json file.
- The mean, standard deviation, min and max are computed for each metric.
-
- Args:
- n_splits(int): number of splits the model was evaluated on
- """
- metrics_values = {}
- # Get the folders where each split evaluation was saved
- folders = [path.join(self._filepath, 'split_' + str(i)) for i in range(n_splits)]
-
- for folder in folders:
- with open(path.join(folder, 'records.json'), "r") as read_file:
- split_data = json.load(read_file)
-
- # Get metric values over all the splits
- for section, data in split_data.items():
- # Get the sections where the metrics are saved
- if search("(metric)", section):
- # For each split, get the value of the metric
- for metric, value in data.items():
- try:
- metrics_values[section][metric].append(float(value))
- except KeyError:
- try:
- metrics_values[section][metric] = [float(value)]
- except KeyError:
- metrics_values[section] = {metric: [float(value)]}
-
- # Save statistics on the metrics
- recap = {}
- for section, data in metrics_values.items():
- recap[section] = {}
- for metric, values in data.items():
- values = np.array(values)
- mean_scores, std_scores = round(np.mean(values), 4), round(np.std(values), 4)
- med_scores = round(np.median(values), 4)
- min_scores, max_scores = round(np.min(values), 4), round(np.max(values), 4)
- recap[section][metric] = {
- 'info': f"{mean_scores} +- {std_scores} [{med_scores}; {min_scores}-{max_scores}]",
- 'mean': mean_scores,
- 'std': std_scores,
- }
-
- # Save the file in disk
- with open(path.join(self._filepath, 'recap.json'), "w") as file:
- json.dump(recap, file, indent=True)
-
- @staticmethod
- def visualize_results(file_path: str,
- task: str,
- recap_file: str = None,
- ) -> pd.DataFrame:
- """
- Saves metrics scores regrouped in the recap json file in a dataframe and prints it
-
- Args:
- file_path(str): directory where the experiment is saved
- task(str): prediction task
- recap_file(str): recap json file of the experiment
- """
- recap_file = 'recap.json' if recap_file is None else recap_file
- with open(path.join(file_path, recap_file), "r") as file:
- recap = json.load(file)
-
- metrics = {}
- # Get the mean and std for each metric in all the sets (train, test and valid)
- for _set, values in recap.items():
- for metric, stats in values.items():
- try:
- metrics[metric][_set] = str(stats['mean']) + ' +- ' + str(stats['std'])
- except KeyError:
- metrics[metric] = {_set: str(stats['mean']) + ' +- ' + str(stats['std'])}
-
- for _set in ['HAIM', 'NON_HAIM']:
- for metric in metrics.keys():
- if metric == 'AUC':
- metrics[metric][_set] = str(constants.AUC[_set][task])
- else:
- metrics[metric][_set] = '--'
-
- # Transform the dictionary to a dataframe
- df_metrics = pd.DataFrame(metrics)
-
- return df_metrics
-
- @staticmethod
- def get_best_of_experiments(task: str,
- path_file: str,
- n_experiments: int,
- metric: str = 'AUC') -> None:
- """
- Gets the experiment with the best metric value from a set of saved experiments
-
- Args:
- task(str): file name format of the experiments
- path_file(str): path to directory where the experiments are saved
- n_experiments(int): number of experiments to compare
- metric(str): metric name
- """
- metric_values = []
- # Get the folders where each recap evaluation was saved
- folders = [path.join(path_file, task + '_' + str(i)) for i in range(n_experiments)]
-
- for folder in folders:
- with open(path.join(folder, 'recap.json'), "r") as read_file:
- recap_data = json.load(read_file)
- # Get AUC values of all experiments
- infos = recap_data["test_metrics"][metric]
- metric_values.append(float(infos['mean']))
-
- best_experiment = np.argmax(np.array(metric_values))
-
- # Copy the files of the best experiment to the directory file_format_best_experiment
- shutil.copytree(folders[best_experiment], path.join(path_file, task + '_best_experiment'))
-
- @staticmethod
- def reverse_map(map_: Dict[Any, Any]) -> Dict[Any, Any]:
- """
- Reverses the keys and values of a dictionary
-
- Args:
- map_(Dict[Any, Any]): dictionary
-
- Returns: a dictionary
- """
- reversed_map = {}
- for k, v in map_.items():
- for value in v:
- reversed_map[value] = k
- return reversed_map
-
- @staticmethod
- def plot_roc_curve(
- saving_path: str,
- targets: np.array,
- y_proba: np.array,
- mask: str) -> None:
-
- """
- Plots the Area Under AUC curve and saves it
-
- Args:
- saving_path(str): path where to save the figure
- targets(np.array): ground truth labels
- y_proba(np.array): probabilities predicted
- mask(str): label of the current mask
- """
-
- fpr, tpr, _ = roc_curve(targets, y_proba, pos_label=1)
-
- # create ROC curve
- plt.clf()
- plt.plot(fpr, tpr)
- plt.ylabel('True Positive Rate')
- plt.xlabel('False Positive Rate')
-
- # Save the figure in the disk
- plt.savefig(path.join(saving_path, 'roc_curve_' + mask + '.png'))
diff --git a/src/evaluation/pycaret_evaluator.py b/src/evaluation/pycaret_evaluator.py
new file mode 100644
index 0000000..51ba466
--- /dev/null
+++ b/src/evaluation/pycaret_evaluator.py
@@ -0,0 +1,235 @@
+import gc # Garbage collector to free memory after each fold
+import json
+import os
+from time import strftime
+from typing import Any, Dict, List, Optional, Union
+
+import numpy as np
+import pandas as pd
+from sklearn.model_selection import StratifiedKFold, KFold
+
+import ray # Import Ray
+from pycaret.classification import (create_model, predict_model, pull, save_model,
+ setup, tune_model)
+
+os.environ["RAY_DEDUP_LOGS"] = "0" # Disables log deduplication in Ray
+
+
+class PyCaretEvaluator:
+ """
+ Class to evaluate models using PyCaret, optimized for memory management.
+ """
+
+ def __init__(self, dataset: Any, target: str, experiment_name: Optional[str], filepath: str, columns: Optional[List[str]] = None):
+ """
+ Initialize the class parameters.
+
+ Args:
+ dataset (Any): the used dataset for the task.
+ target (str): the target class.
+ experiment_name (Optional[str]): optional name for the experiment.
+ filepath (str): path for saving results.
+ columns (Optional[List[str]]): optional list of column names.
+ """
+ # Initialize instance variables
+ self.dataset = dataset
+ self.target = target
+ self.experiment_name = experiment_name if experiment_name else f"experiment_{strftime('%Y%m%d-%H%M%S')}"
+ self.filepath = filepath
+ self.columns = columns
+
+ # Create the directory if it doesn't exist
+ if not os.path.exists(self.filepath):
+ os.makedirs(self.filepath)
+
+ def save_results(self, results: List[Dict], filename: str) -> None:
+ """
+ Save the results in a JSON file.
+
+ Args:
+ results (List[Dict]): results to save.
+ filename (str): file name where the results are saved.
+ """
+ with open(os.path.join(self.filepath, filename), 'w', encoding='utf-8') as f:
+ json.dump(results, f, indent=4)
+
+ @ray.remote(memory=8e9) # Mark this function to be executed in parallel by Ray
+ def run_fold(self,
+ train_index: np.ndarray,
+ test_index: np.ndarray,
+ fold_num: int,
+ train_size: float,
+ fold: int,
+ fold_strategy: str,
+ session_id: int,
+ model: str,
+ optimize: Union[str, List[str]],
+ custom_grid: Optional[Dict[str, List[Any]]],
+ search_algorithm: str,
+ fixed_params: Dict[str, Any]) -> Dict[str, Any]:
+ """
+ Run a single fold in parallel using Ray.
+
+ Args:
+ train_index (np.ndarray): Array of training indices for the fold.
+ test_index (np.ndarray): Array of testing indices for the fold.
+ fold_num (int): Current fold number.
+ train_size (float): Proportion of data to use for training within the fold.
+ fold (int): Number of folds for inner cross-validation.
+ fold_strategy (str): Strategy for inner cross-validation (e.g., 'kfold', 'stratifiedkfold').
+ session_id (int): Random seed for reproducibility.
+ model (str): Name of the model to be created (e.g., 'xgboost', 'lightgbm').
+ optimize (Union[str, List[str]]): Metric(s) to optimize during model tuning (e.g., 'AUC', 'Accuracy').
+ custom_grid (Optional[Dict[str, List[Any]]]): Custom hyperparameter grid for tuning the model.
+ search_algorithm (str): Hyperparameter search algorithm to use ('grid' or 'random').
+ fixed_params (Dict[str, Any]): Dictionary of fixed parameters for model setup (e.g., seed, eval_metric).
+
+ Returns:
+ Dict[str, Any]: A dictionary containing training results, test predictions, and the best hyperparameters for the fold.
+ """
+ print(f"Outer fold {fold_num}")
+
+ # Extract training and testing subsets
+ train_data_x = self.dataset.x[train_index]
+ train_data_y = self.dataset.y[train_index]
+ test_data_x = self.dataset.x[test_index]
+ test_data_y = self.dataset.y[test_index]
+
+ # Convert NumPy arrays to DataFrames for PyCaret setup
+ train_df = pd.DataFrame(train_data_x, columns=self.columns)
+ train_df[self.target] = train_data_y
+ test_df = pd.DataFrame(test_data_x, columns=self.columns)
+ test_df[self.target] = test_data_y
+
+ print(f"Train indices: {train_index}")
+ print(f"Test indices: {test_index}")
+
+ # Configure PyCaret for the current fold
+ exp = setup(data=train_df,
+ target=self.target,
+ train_size=train_size,
+ fold=fold,
+ fold_strategy=fold_strategy,
+ session_id=fixed_params['seed'],
+ verbose=False,
+ n_jobs=1)
+
+ print(f"Configuring PyCaret for outer fold {fold_num}")
+
+ # Create and tune the specified model
+ best_model = create_model(model, fold=fold)
+
+ if custom_grid:
+ print(f"Tuning hyperparameters for model {model} with custom grid using {search_algorithm} search")
+ best_model = tune_model(best_model, custom_grid=custom_grid, fold=fold, optimize=optimize, search_algorithm=search_algorithm, verbose=False)
+
+ # Extract the best hyperparameters after tuning
+ best_hyperparams = best_model.get_params()
+ else:
+ best_hyperparams = best_model.get_params() # Default parameters if no tuning
+
+ # Get the results and predictions
+ model_results = pull() # Pull the results after create_model or tune_model
+ save_model(best_model, os.path.join(self.filepath, f"best_model_fold_{fold_num}"))
+ test_predictions = predict_model(best_model, data=test_df)
+
+ # Save fold results
+ split_result = {
+ 'fold': fold_num,
+ 'train_results': model_results.to_dict(),
+ 'test_predictions': test_predictions.to_dict(),
+ 'best_hyperparams': best_hyperparams # Save best hyperparameters for this fold
+ }
+
+ # Clean up memory after each fold (memory management)
+ del train_df, test_df, best_model, model_results, test_predictions, exp
+ gc.collect()
+
+ return split_result
+
+ def run_experiment(self,
+ train_size: float = 0.8,
+ fold: int = 5,
+ fold_strategy: str = 'stratifiedkfold',
+ outer_fold: int = 5,
+ outer_strategy: str = 'stratifiedkfold',
+ session_id: int = 42,
+ model: Optional[str] = 'xgboost',
+ optimize: Union[str, List[str]] = 'AUC',
+ custom_grid: Optional[Dict[str, List[Any]]] = None,
+ search_algorithm: str = 'grid',
+ fixed_params: Dict[str, Any] = None) -> None:
+ """
+ Runs the entire experiment, including external cross-validation, training, and model optimization.
+
+ Args:
+ train_size (float): Proportion of the dataset to include in the training split.
+ fold (int): Number of folds for internal cross-validation.
+ fold_strategy (str): Strategy for internal cross-validation ('kfold', 'stratifiedkfold').
+ outer_fold (int): Number of folds for external cross-validation.
+ outer_strategy (str): Strategy for external cross-validation ('kfold', 'stratifiedkfold').
+ session_id (int): Session ID for reproducibility.
+ model (Optional[str]): Specific model to use.
+ optimize (Union[str, List[str]]): The metric to optimize.
+ custom_grid (Optional[Dict[str, List[Any]]]): Custom grid of parameters for tuning.
+ search_algorithm (str): Algorithm to use for hyperparameter tuning ('grid' or 'random').
+ fixed_params (Dict[str, Any]): Fixed parameters such as seed and eval_metric.
+ """
+ # Params fixed by the original study
+ if fixed_params is None:
+ fixed_params = {'seed': 42, 'eval_metric': 'logloss', 'verbosity': 0}
+
+ # Define the outer cross-validation strategy
+ if outer_strategy == 'stratifiedkfold':
+ outer_cv = StratifiedKFold(n_splits=outer_fold, shuffle=True, random_state=session_id)
+ elif outer_strategy == 'kfold':
+ outer_cv = KFold(n_splits=outer_fold, shuffle=True, random_state=session_id)
+ else:
+ raise ValueError(f"Unknown outer_strategy: {outer_strategy}")
+
+ ray.init(ignore_reinit_error=True, num_cpus=os.cpu_count()) # Initialize Ray with available CPUs
+ ray_tasks = [] # List to store Ray tasks
+
+ # Generate Ray tasks for each fold
+ for i, (train_index, test_index) in enumerate(outer_cv.split(self.dataset.x, self.dataset.y)):
+ ray_task = self.run_fold.remote(self, train_index, test_index, i + 1, train_size, fold, fold_strategy, session_id, model,
+ optimize, custom_grid, search_algorithm, fixed_params)
+ ray_tasks.append(ray_task)
+
+ # Execute and collect results of Ray tasks
+ results = ray.get(ray_tasks)
+ self.save_results(results, f"{self.experiment_name}_results.json")
+
+ # Collect and compute final metrics after training
+ fold_metrics_list = []
+ best_hyperparams_list = []
+
+ for result in results:
+ train_results = result.get('train_results', {})
+ best_hyperparams = result.get('best_hyperparams', {})
+ if isinstance(train_results, dict):
+ metrics_df = pd.DataFrame(train_results, index=[0])
+ fold_metrics_list.append(metrics_df)
+ if best_hyperparams:
+ best_hyperparams_list.append(best_hyperparams)
+
+ # Calculate and save the mean and standard deviation of metrics
+ if fold_metrics_list:
+ all_fold_metrics = pd.concat(fold_metrics_list, ignore_index=True)
+ final_metrics_mean = all_fold_metrics.mean()
+ final_metrics_std = all_fold_metrics.std()
+
+ metrics_table = pd.DataFrame({'Metric': final_metrics_mean.index, 'Mean': final_metrics_mean.values,
+ 'Std Dev': final_metrics_std.values})
+ print("Final metrics table:")
+ print(metrics_table)
+
+ metrics_table.to_csv(os.path.join(self.filepath, f"{self.experiment_name}_final_metrics.csv"), index=False)
+
+ # Determine the most common hyperparameters across folds
+ if best_hyperparams_list:
+ best_hyperparams_df = pd.DataFrame(best_hyperparams_list)
+ most_common_hyperparams = best_hyperparams_df.mode().iloc[0] # Most frequent hyperparameters across folds
+ print(f"Best hyperparameters across all folds: {most_common_hyperparams}")
+
+ ray.shutdown()
diff --git a/src/evaluation/tuning.py b/src/evaluation/tuning.py
deleted file mode 100644
index 6851b48..0000000
--- a/src/evaluation/tuning.py
+++ /dev/null
@@ -1,159 +0,0 @@
-"""
-Filename: Tuning.py
-
-Author : Hakima Laribi
-
-Description: This file is used to store objects used for tuning
-
-Date of last modification : 2023/02/06
-
-"""
-from abc import abstractmethod, ABC
-from typing import Callable, Dict, List, Any, Union
-
-from numpy import array
-from sklearn.base import BaseEstimator
-from sklearn.metrics import make_scorer
-from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
-
-from src.utils.metric_scores import Metric
-
-
-class SklearnHpsOptimizer:
- """
- Object used to store Scikit-learn hyper-parameter optimizers labels
- """
-
- GS = 'grid_search'
- RS = 'random_search'
-
- def __iter__(self):
- return iter([self.GS, self.RS])
-
- def __getitem__(self, item: str) -> Union[Callable, None]:
- if item == self.GS:
- return GridSearchCV
- elif item == self.RS:
- return RandomizedSearchCV
- else:
- raise ValueError(f"{item}: hyper-parameters optimizer not supported")
-
-
-class Tuner(ABC):
- """
- Abstract class used to define tuner skeleton
- """
-
- def __init__(self,
- metric: Metric,
- hps: Dict[str, List[Any]],
- n_splits: int,
- parallel: bool = True):
- """
- Sets protected attributes
-
- Args:
- metric(Metric): callable function to optimize
- hps(Dict[str, List[Any]]): dictionary with the hyper-parameters to optimize and the corresponding values
- to explore
- n_splits(int): number of inner splits on which test each combination of hyper-parameters
- parallel(bool): whether to run the tuning in parallel or not
- """
- # Set protected attributes
- self._metric = metric
- self._hps = hps
- self._n_splits = n_splits
- self._n_cpus = -1 if parallel else 1 # Use all cpus for parallel tuning or a single one only
-
- @abstractmethod
- def tune(self,
- model: Any,
- x: array,
- y: array):
- """
- Performs the tuning of a model on specific data
-
- Args:
- model(Any): the model to which find the best combination of hyper-parameters
- x(array): (N, D) array where N is the number of observations and D the number of predictors
- y(array): (N, 1) ground truth labels
- """
- raise NotImplementedError
-
- @abstractmethod
- def get_best_hps(self):
- """
- Returns the combination of hyper-parameters which optimized the metric value
- """
- raise NotImplementedError
-
-
-class SklearnTuner(Tuner):
- """
- Object used to perform hyper-parameter tuning using Scikit-learn optimizers
- """
-
- def __init__(self,
- metric: Metric,
- hps: Dict[str, List[Any]],
- n_splits: int,
- model_selector: str = SklearnHpsOptimizer.GS,
- parallel: bool = True):
- """
- Sets protected attributes
-
- Args:
- metric(Metric): callable function to optimize
- hps(Dict[str, List[Any]]): dictionary with the hyper-parameters to optimize and the corresponding values
- to explore
- n_splits(int): number of inner splits on which test each combination of hyper-parameters
- model_selector(str): sckit-learn hyper-parameters optimizer
- parallel(bool): whether to run the tuning in parallel or not
-
- """
- # Validation of inputs
- if model_selector not in SklearnHpsOptimizer():
- raise ValueError(f"{model_selector}: unsupported hyper-parameters optimizer in Scikit-Learn")
-
- # Create a custom scikit-learn score metric
- metric = make_scorer(metric, greater_is_better=(metric.direction == 'maximize'))
-
- # Call parent constructor
- super().__init__(metric, hps, n_splits, parallel)
-
- # Get the Hyper-parameter optimizer in Scikit-learn
- self._hps_optimizer = SklearnHpsOptimizer()[model_selector]
- self._optimizer = None
-
- def tune(self,
- model: BaseEstimator,
- x: array,
- y: array) -> BaseEstimator:
- """
- Performs the tuning of a model on specific data using a Scikit-learn Hyper-parameters optimizer
-
- Args:
- model(Any): the model to which find the best combination of hyper-parameters
- x(array): (N, D) array where N is the number of observations and D the number of predictors
- y(array): (N, 1) ground truth labels
-
- Returns:
- Scikit-Learn optimized model
- """
- if not isinstance(model, BaseEstimator):
- raise ValueError(f"{model} is not a Scikit-Learn estimator, cannot perform tuning with SKlearnTuner")
-
- # instantiate the hyper-parameter Sklearn optimizer
- self._optimizer = self._hps_optimizer(model, self._hps, scoring=self._metric, cv=self._n_splits,
- n_jobs=self._n_cpus, refit=True, verbose=0)
- # Launch the hyper-parameter optimization
- self._optimizer.fit(x, y)
-
- # return the model with best hyper-parameters
- return self._optimizer.best_estimator_
-
- def get_best_hps(self) -> Dict[str, Any]:
- """
- Returns the combination of hyper-parameters which optimized the metric value
- """
- return self._optimizer.best_params_