dragon-ml-toolbox 5.3.0__tar.gz → 6.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dragon-ml-toolbox might be problematic. Click here for more details.

Files changed (39) hide show
  1. {dragon_ml_toolbox-5.3.0/dragon_ml_toolbox.egg-info → dragon_ml_toolbox-6.0.0}/PKG-INFO +9 -6
  2. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/README.md +8 -5
  3. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0/dragon_ml_toolbox.egg-info}/PKG-INFO +9 -6
  4. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/dragon_ml_toolbox.egg-info/SOURCES.txt +1 -0
  5. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/ML_callbacks.py +7 -7
  6. dragon_ml_toolbox-6.0.0/ml_tools/ML_evaluation.py +350 -0
  7. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/ML_trainer.py +17 -15
  8. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/PSO_optimization.py +5 -5
  9. dragon_ml_toolbox-6.0.0/ml_tools/ensemble_evaluation.py +639 -0
  10. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/ensemble_inference.py +10 -10
  11. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/ensemble_learning.py +47 -413
  12. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/keys.py +2 -2
  13. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/utilities.py +27 -3
  14. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/pyproject.toml +1 -1
  15. dragon_ml_toolbox-5.3.0/ml_tools/ML_evaluation.py +0 -260
  16. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/LICENSE +0 -0
  17. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/LICENSE-THIRD-PARTY.md +0 -0
  18. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/dragon_ml_toolbox.egg-info/dependency_links.txt +0 -0
  19. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/dragon_ml_toolbox.egg-info/requires.txt +0 -0
  20. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/dragon_ml_toolbox.egg-info/top_level.txt +0 -0
  21. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/ETL_engineering.py +0 -0
  22. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/GUI_tools.py +0 -0
  23. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/MICE_imputation.py +0 -0
  24. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/ML_datasetmaster.py +0 -0
  25. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/ML_inference.py +0 -0
  26. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/ML_models.py +0 -0
  27. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/ML_optimization.py +0 -0
  28. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/RNN_forecast.py +0 -0
  29. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/SQL.py +0 -0
  30. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/VIF_factor.py +0 -0
  31. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/__init__.py +0 -0
  32. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/_logger.py +0 -0
  33. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/_script_info.py +0 -0
  34. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/custom_logger.py +0 -0
  35. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/data_exploration.py +0 -0
  36. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/handle_excel.py +0 -0
  37. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/optimization_tools.py +0 -0
  38. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/ml_tools/path_manager.py +0 -0
  39. {dragon_ml_toolbox-5.3.0 → dragon_ml_toolbox-6.0.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dragon-ml-toolbox
3
- Version: 5.3.0
3
+ Version: 6.0.0
4
4
  Summary: A collection of tools for data science and machine learning projects.
5
5
  Author-email: Karl Loza <luigiloza@gmail.com>
6
6
  License-Expression: MIT
@@ -141,19 +141,22 @@ pip install "dragon-ml-toolbox[pytorch]"
141
141
  ```bash
142
142
  custom_logger
143
143
  data_exploration
144
- ensemble_learning
144
+ ensemble_evaluation
145
145
  ensemble_inference
146
+ ensemble_learning
146
147
  ETL_engineering
147
- ML_datasetmaster
148
- ML_models
149
148
  ML_callbacks
149
+ ML_datasetmaster
150
150
  ML_evaluation
151
- ML_trainer
152
151
  ML_inference
152
+ ML_models
153
+ ML_optimization
154
+ ML_trainer
155
+ optimization_tools
153
156
  path_manager
154
157
  PSO_optimization
155
- SQL
156
158
  RNN_forecast
159
+ SQL
157
160
  utilities
158
161
  ```
159
162
 
@@ -60,19 +60,22 @@ pip install "dragon-ml-toolbox[pytorch]"
60
60
  ```bash
61
61
  custom_logger
62
62
  data_exploration
63
- ensemble_learning
63
+ ensemble_evaluation
64
64
  ensemble_inference
65
+ ensemble_learning
65
66
  ETL_engineering
66
- ML_datasetmaster
67
- ML_models
68
67
  ML_callbacks
68
+ ML_datasetmaster
69
69
  ML_evaluation
70
- ML_trainer
71
70
  ML_inference
71
+ ML_models
72
+ ML_optimization
73
+ ML_trainer
74
+ optimization_tools
72
75
  path_manager
73
76
  PSO_optimization
74
- SQL
75
77
  RNN_forecast
78
+ SQL
76
79
  utilities
77
80
  ```
78
81
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dragon-ml-toolbox
3
- Version: 5.3.0
3
+ Version: 6.0.0
4
4
  Summary: A collection of tools for data science and machine learning projects.
5
5
  Author-email: Karl Loza <luigiloza@gmail.com>
6
6
  License-Expression: MIT
@@ -141,19 +141,22 @@ pip install "dragon-ml-toolbox[pytorch]"
141
141
  ```bash
142
142
  custom_logger
143
143
  data_exploration
144
- ensemble_learning
144
+ ensemble_evaluation
145
145
  ensemble_inference
146
+ ensemble_learning
146
147
  ETL_engineering
147
- ML_datasetmaster
148
- ML_models
149
148
  ML_callbacks
149
+ ML_datasetmaster
150
150
  ML_evaluation
151
- ML_trainer
152
151
  ML_inference
152
+ ML_models
153
+ ML_optimization
154
+ ML_trainer
155
+ optimization_tools
153
156
  path_manager
154
157
  PSO_optimization
155
- SQL
156
158
  RNN_forecast
159
+ SQL
157
160
  utilities
158
161
  ```
159
162
 
@@ -26,6 +26,7 @@ ml_tools/_logger.py
26
26
  ml_tools/_script_info.py
27
27
  ml_tools/custom_logger.py
28
28
  ml_tools/data_exploration.py
29
+ ml_tools/ensemble_evaluation.py
29
30
  ml_tools/ensemble_inference.py
30
31
  ml_tools/ensemble_learning.py
31
32
  ml_tools/handle_excel.py
@@ -2,7 +2,7 @@ import numpy as np
2
2
  import torch
3
3
  from tqdm.auto import tqdm
4
4
  from .path_manager import make_fullpath
5
- from .keys import LogKeys
5
+ from .keys import PyTorchLogKeys
6
6
  from ._logger import _LOGGER
7
7
  from typing import Optional
8
8
  from ._script_info import _script_info
@@ -96,14 +96,14 @@ class TqdmProgressBar(Callback):
96
96
  def on_batch_end(self, batch, logs=None):
97
97
  self.batch_bar.update(1) # type: ignore
98
98
  if logs:
99
- self.batch_bar.set_postfix(loss=f"{logs.get(LogKeys.BATCH_LOSS, 0):.4f}") # type: ignore
99
+ self.batch_bar.set_postfix(loss=f"{logs.get(PyTorchLogKeys.BATCH_LOSS, 0):.4f}") # type: ignore
100
100
 
101
101
  def on_epoch_end(self, epoch, logs=None):
102
102
  self.batch_bar.close() # type: ignore
103
103
  self.epoch_bar.update(1) # type: ignore
104
104
  if logs:
105
- train_loss_str = f"{logs.get(LogKeys.TRAIN_LOSS, 0):.4f}"
106
- val_loss_str = f"{logs.get(LogKeys.VAL_LOSS, 0):.4f}"
105
+ train_loss_str = f"{logs.get(PyTorchLogKeys.TRAIN_LOSS, 0):.4f}"
106
+ val_loss_str = f"{logs.get(PyTorchLogKeys.VAL_LOSS, 0):.4f}"
107
107
  self.epoch_bar.set_postfix_str(f"Train Loss: {train_loss_str}, Val Loss: {val_loss_str}") # type: ignore
108
108
 
109
109
  def on_train_end(self, logs=None):
@@ -124,7 +124,7 @@ class EarlyStopping(Callback):
124
124
  inferred from the name of the monitored quantity.
125
125
  verbose (int): Verbosity mode.
126
126
  """
127
- def __init__(self, monitor: str=LogKeys.VAL_LOSS, min_delta=0.0, patience=3, mode: Literal['auto', 'min', 'max']='auto', verbose: int=1):
127
+ def __init__(self, monitor: str=PyTorchLogKeys.VAL_LOSS, min_delta: float=0.0, patience: int=5, mode: Literal['auto', 'min', 'max']='auto', verbose: int=1):
128
128
  super().__init__()
129
129
  self.monitor = monitor
130
130
  self.patience = patience
@@ -201,8 +201,8 @@ class ModelCheckpoint(Callback):
201
201
  mode (str): One of {'auto', 'min', 'max'}.
202
202
  verbose (int): Verbosity mode.
203
203
  """
204
- def __init__(self, save_dir: Union[str,Path], monitor: str = LogKeys.VAL_LOSS,
205
- save_best_only: bool = False, mode: Literal['auto', 'min', 'max']= 'auto', verbose: int = 1):
204
+ def __init__(self, save_dir: Union[str,Path], monitor: str = PyTorchLogKeys.VAL_LOSS,
205
+ save_best_only: bool = True, mode: Literal['auto', 'min', 'max']= 'auto', verbose: int = 0):
206
206
  super().__init__()
207
207
  self.save_dir = make_fullpath(save_dir, make=True, enforce="directory")
208
208
  if not self.save_dir.is_dir():
@@ -0,0 +1,350 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
5
+ from sklearn.calibration import CalibrationDisplay
6
+ from sklearn.metrics import (
7
+ classification_report,
8
+ ConfusionMatrixDisplay,
9
+ roc_curve,
10
+ roc_auc_score,
11
+ mean_squared_error,
12
+ mean_absolute_error,
13
+ r2_score,
14
+ median_absolute_error,
15
+ precision_recall_curve,
16
+ average_precision_score
17
+ )
18
+ import torch
19
+ import shap
20
+ from pathlib import Path
21
+ from .path_manager import make_fullpath
22
+ from ._logger import _LOGGER
23
+ from typing import Union, Optional
24
+ from ._script_info import _script_info
25
+
26
+
27
+ __all__ = [
28
+ "plot_losses",
29
+ "classification_metrics",
30
+ "regression_metrics",
31
+ "shap_summary_plot"
32
+ ]
33
+
34
+
35
+ def plot_losses(history: dict, save_dir: Union[str, Path]):
36
+ """
37
+ Plots training & validation loss curves from a history object.
38
+
39
+ Args:
40
+ history (dict): A dictionary containing 'train_loss' and 'val_loss'.
41
+ save_dir (str | Path): Directory to save the plot image.
42
+ """
43
+ train_loss = history.get('train_loss', [])
44
+ val_loss = history.get('val_loss', [])
45
+
46
+ if not train_loss and not val_loss:
47
+ print("Warning: Loss history is empty or incomplete. Cannot plot.")
48
+ return
49
+
50
+ fig, ax = plt.subplots(figsize=(10, 5), dpi=100)
51
+
52
+ # Plot training loss only if data for it exists
53
+ if train_loss:
54
+ epochs = range(1, len(train_loss) + 1)
55
+ ax.plot(epochs, train_loss, 'o-', label='Training Loss')
56
+
57
+ # Plot validation loss only if data for it exists
58
+ if val_loss:
59
+ epochs = range(1, len(val_loss) + 1)
60
+ ax.plot(epochs, val_loss, 'o-', label='Validation Loss')
61
+
62
+ ax.set_title('Training and Validation Loss')
63
+ ax.set_xlabel('Epochs')
64
+ ax.set_ylabel('Loss')
65
+ ax.legend()
66
+ ax.grid(True)
67
+ plt.tight_layout()
68
+
69
+ save_dir_path = make_fullpath(save_dir, make=True, enforce="directory")
70
+ save_path = save_dir_path / "loss_plot.svg"
71
+ plt.savefig(save_path)
72
+ _LOGGER.info(f"📉 Loss plot saved as '{save_path.name}'")
73
+
74
+ plt.close(fig)
75
+
76
+
77
+ def classification_metrics(save_dir: Union[str, Path], y_true: np.ndarray, y_pred: np.ndarray, y_prob: Optional[np.ndarray] = None,
78
+ cmap: str = "Blues"):
79
+ """
80
+ Saves classification metrics and plots.
81
+
82
+ Args:
83
+ y_true (np.ndarray): Ground truth labels.
84
+ y_pred (np.ndarray): Predicted labels.
85
+ y_prob (np.ndarray, optional): Predicted probabilities for ROC curve.
86
+ cmap (str): Colormap for the confusion matrix.
87
+ save_dir (str | Path): Directory to save plots.
88
+ """
89
+ print("--- Classification Report ---")
90
+ # Generate report as both text and dictionary
91
+ report_text: str = classification_report(y_true, y_pred) # type: ignore
92
+ report_dict: dict = classification_report(y_true, y_pred, output_dict=True) # type: ignore
93
+ print(report_text)
94
+
95
+ save_dir_path = make_fullpath(save_dir, make=True, enforce="directory")
96
+ # Save text report
97
+ report_path = save_dir_path / "classification_report.txt"
98
+ report_path.write_text(report_text, encoding="utf-8")
99
+ _LOGGER.info(f"📝 Classification report saved as '{report_path.name}'")
100
+
101
+ # --- Save Classification Report Heatmap ---
102
+ try:
103
+ plt.figure(figsize=(8, 6), dpi=100)
104
+ sns.heatmap(pd.DataFrame(report_dict).iloc[:-1, :].T, annot=True, cmap='viridis', fmt='.2f')
105
+ plt.title("Classification Report")
106
+ plt.tight_layout()
107
+ heatmap_path = save_dir_path / "classification_report_heatmap.svg"
108
+ plt.savefig(heatmap_path)
109
+ _LOGGER.info(f"📊 Report heatmap saved as '{heatmap_path.name}'")
110
+ plt.close()
111
+ except Exception as e:
112
+ _LOGGER.error(f"❌ Could not generate classification report heatmap: {e}")
113
+
114
+ # Save Confusion Matrix
115
+ fig_cm, ax_cm = plt.subplots(figsize=(6, 6), dpi=100)
116
+ ConfusionMatrixDisplay.from_predictions(y_true, y_pred, cmap=cmap, ax=ax_cm)
117
+ ax_cm.set_title("Confusion Matrix")
118
+ cm_path = save_dir_path / "confusion_matrix.svg"
119
+ plt.savefig(cm_path)
120
+ _LOGGER.info(f"❇️ Confusion matrix saved as '{cm_path.name}'")
121
+ plt.close(fig_cm)
122
+
123
+ # Plotting logic for ROC and PR Curves
124
+ if y_prob is not None and y_prob.ndim > 1 and y_prob.shape[1] >= 2:
125
+ # Use probabilities of the positive class
126
+ y_score = y_prob[:, 1]
127
+
128
+ # --- Save ROC Curve ---
129
+ fpr, tpr, _ = roc_curve(y_true, y_score)
130
+ auc = roc_auc_score(y_true, y_score)
131
+ fig_roc, ax_roc = plt.subplots(figsize=(6, 6), dpi=100)
132
+ ax_roc.plot(fpr, tpr, label=f'AUC = {auc:.2f}')
133
+ ax_roc.plot([0, 1], [0, 1], 'k--')
134
+ ax_roc.set_title('Receiver Operating Characteristic (ROC) Curve')
135
+ ax_roc.set_xlabel('False Positive Rate')
136
+ ax_roc.set_ylabel('True Positive Rate')
137
+ ax_roc.legend(loc='lower right')
138
+ ax_roc.grid(True)
139
+ roc_path = save_dir_path / "roc_curve.svg"
140
+ plt.savefig(roc_path)
141
+ _LOGGER.info(f"📈 ROC curve saved as '{roc_path.name}'")
142
+ plt.close(fig_roc)
143
+
144
+ # --- Save Precision-Recall Curve ---
145
+ precision, recall, _ = precision_recall_curve(y_true, y_score)
146
+ ap_score = average_precision_score(y_true, y_score)
147
+ fig_pr, ax_pr = plt.subplots(figsize=(6, 6), dpi=100)
148
+ ax_pr.plot(recall, precision, label=f'AP = {ap_score:.2f}')
149
+ ax_pr.set_title('Precision-Recall Curve')
150
+ ax_pr.set_xlabel('Recall')
151
+ ax_pr.set_ylabel('Precision')
152
+ ax_pr.legend(loc='lower left')
153
+ ax_pr.grid(True)
154
+ pr_path = save_dir_path / "pr_curve.svg"
155
+ plt.savefig(pr_path)
156
+ _LOGGER.info(f"📈 PR curve saved as '{pr_path.name}'")
157
+ plt.close(fig_pr)
158
+
159
+ # --- Save Calibration Plot ---
160
+ if y_prob.ndim > 1 and y_prob.shape[1] >= 2:
161
+ y_score = y_prob[:, 1] # Use probabilities of the positive class
162
+
163
+ fig_cal, ax_cal = plt.subplots(figsize=(8, 8), dpi=100)
164
+ CalibrationDisplay.from_predictions(y_true, y_score, n_bins=15, ax=ax_cal)
165
+
166
+ ax_cal.set_title('Calibration Plot (Reliability Curve)')
167
+ ax_cal.set_xlabel('Mean Predicted Probability')
168
+ ax_cal.set_ylabel('Fraction of Positives')
169
+ ax_cal.grid(True)
170
+ plt.tight_layout()
171
+
172
+ cal_path = save_dir_path / "calibration_plot.svg"
173
+ plt.savefig(cal_path)
174
+ _LOGGER.info(f"✅ Calibration plot saved as '{cal_path.name}'")
175
+ plt.close(fig_cal)
176
+
177
+
178
+ def regression_metrics(y_true: np.ndarray, y_pred: np.ndarray, save_dir: Union[str, Path]):
179
+ """
180
+ Saves regression metrics and plots.
181
+
182
+ Args:
183
+ y_true (np.ndarray): Ground truth values.
184
+ y_pred (np.ndarray): Predicted values.
185
+ save_dir (str | Path): Directory to save plots and report.
186
+ """
187
+ rmse = np.sqrt(mean_squared_error(y_true, y_pred))
188
+ mae = mean_absolute_error(y_true, y_pred)
189
+ r2 = r2_score(y_true, y_pred)
190
+ medae = median_absolute_error(y_true, y_pred)
191
+
192
+ report_lines = [
193
+ "--- Regression Report ---",
194
+ f" Root Mean Squared Error (RMSE): {rmse:.4f}",
195
+ f" Mean Absolute Error (MAE): {mae:.4f}",
196
+ f" Median Absolute Error (MedAE): {medae:.4f}",
197
+ f" Coefficient of Determination (R²): {r2:.4f}"
198
+ ]
199
+ report_string = "\n".join(report_lines)
200
+ print(report_string)
201
+
202
+ save_dir_path = make_fullpath(save_dir, make=True, enforce="directory")
203
+ # Save text report
204
+ report_path = save_dir_path / "regression_report.txt"
205
+ report_path.write_text(report_string)
206
+ _LOGGER.info(f"📝 Regression report saved as '{report_path.name}'")
207
+
208
+ # Save residual plot
209
+ residuals = y_true - y_pred
210
+ fig_res, ax_res = plt.subplots(figsize=(8, 6), dpi=100)
211
+ ax_res.scatter(y_pred, residuals, alpha=0.6)
212
+ ax_res.axhline(0, color='red', linestyle='--')
213
+ ax_res.set_xlabel("Predicted Values")
214
+ ax_res.set_ylabel("Residuals")
215
+ ax_res.set_title("Residual Plot")
216
+ ax_res.grid(True)
217
+ plt.tight_layout()
218
+ res_path = save_dir_path / "residual_plot.svg"
219
+ plt.savefig(res_path)
220
+ _LOGGER.info(f"📈 Residual plot saved as '{res_path.name}'")
221
+ plt.close(fig_res)
222
+
223
+ # Save true vs predicted plot
224
+ fig_tvp, ax_tvp = plt.subplots(figsize=(8, 6), dpi=100)
225
+ ax_tvp.scatter(y_true, y_pred, alpha=0.6)
226
+ ax_tvp.plot([y_true.min(), y_true.max()], [y_true.min(), y_true.max()], 'k--', lw=2)
227
+ ax_tvp.set_xlabel('True Values')
228
+ ax_tvp.set_ylabel('Predictions')
229
+ ax_tvp.set_title('True vs. Predicted Values')
230
+ ax_tvp.grid(True)
231
+ plt.tight_layout()
232
+ tvp_path = save_dir_path / "true_vs_predicted_plot.svg"
233
+ plt.savefig(tvp_path)
234
+ _LOGGER.info(f"📉 True vs. Predicted plot saved as '{tvp_path.name}'")
235
+ plt.close(fig_tvp)
236
+
237
+ # Save Histogram of Residuals
238
+ fig_hist, ax_hist = plt.subplots(figsize=(8, 6), dpi=100)
239
+ sns.histplot(residuals, kde=True, ax=ax_hist)
240
+ ax_hist.set_xlabel("Residual Value")
241
+ ax_hist.set_ylabel("Frequency")
242
+ ax_hist.set_title("Distribution of Residuals")
243
+ ax_hist.grid(True)
244
+ plt.tight_layout()
245
+ hist_path = save_dir_path / "residuals_histogram.svg"
246
+ plt.savefig(hist_path)
247
+ _LOGGER.info(f"📊 Residuals histogram saved as '{hist_path.name}'")
248
+ plt.close(fig_hist)
249
+
250
+
251
+ def shap_summary_plot(model, background_data: Union[torch.Tensor,np.ndarray], instances_to_explain: Union[torch.Tensor,np.ndarray],
252
+ feature_names: Optional[list[str]]=None, save_dir: Optional[Union[str, Path]] = None):
253
+ """
254
+ Calculates SHAP values and saves summary plots and data.
255
+
256
+ Args:
257
+ model (nn.Module): The trained PyTorch model.
258
+ background_data (torch.Tensor): A sample of data for the explainer background.
259
+ instances_to_explain (torch.Tensor): The specific data instances to explain.
260
+ feature_names (list of str | None): Names of the features for plot labeling.
261
+ save_dir (str | Path | None): Directory to save SHAP artifacts. If None, dot plot is shown.
262
+ """
263
+ # everything to numpy
264
+ if isinstance(background_data, np.ndarray):
265
+ background_data_np = background_data
266
+ else:
267
+ background_data_np = background_data.numpy()
268
+
269
+ if isinstance(instances_to_explain, np.ndarray):
270
+ instances_to_explain_np = instances_to_explain
271
+ else:
272
+ instances_to_explain_np = instances_to_explain.numpy()
273
+
274
+ # --- Data Validation Step ---
275
+ if np.isnan(background_data_np).any() or np.isnan(instances_to_explain_np).any():
276
+ _LOGGER.error("❌ Input data for SHAP contains NaN values. Aborting explanation.")
277
+ return
278
+
279
+ print("\n--- SHAP Value Explanation ---")
280
+
281
+ model.eval()
282
+ model.cpu()
283
+
284
+ # 1. Summarize the background data.
285
+ # Summarize the background data using k-means. 10-50 clusters is a good starting point.
286
+ background_summary = shap.kmeans(background_data_np, 30)
287
+
288
+ # 2. Define a prediction function wrapper that SHAP can use. It must take a numpy array and return a numpy array.
289
+ def prediction_wrapper(x_np: np.ndarray) -> np.ndarray:
290
+ # Convert numpy data to torch tensor
291
+ x_torch = torch.from_numpy(x_np).float()
292
+ with torch.no_grad():
293
+ # Get model output
294
+ output = model(x_torch)
295
+ # Return as numpy array
296
+ return output.cpu().numpy().flatten()
297
+
298
+ # 3. Create the KernelExplainer
299
+ explainer = shap.KernelExplainer(prediction_wrapper, background_summary)
300
+
301
+ print("Calculating SHAP values with KernelExplainer...")
302
+ shap_values = explainer.shap_values(instances_to_explain_np, l1_reg="aic")
303
+
304
+ if save_dir:
305
+ save_dir_path = make_fullpath(save_dir, make=True, enforce="directory")
306
+ plt.ioff()
307
+
308
+ # Save Bar Plot
309
+ bar_path = save_dir_path / "shap_bar_plot.svg"
310
+ shap.summary_plot(shap_values, instances_to_explain_np, feature_names=feature_names, plot_type="bar", show=False)
311
+ plt.title("SHAP Feature Importance")
312
+ plt.tight_layout()
313
+ plt.savefig(bar_path)
314
+ _LOGGER.info(f"📊 SHAP bar plot saved as '{bar_path.name}'")
315
+ plt.close()
316
+
317
+ # Save Dot Plot
318
+ dot_path = save_dir_path / "shap_dot_plot.svg"
319
+ shap.summary_plot(shap_values, instances_to_explain_np, feature_names=feature_names, plot_type="dot", show=False)
320
+ plt.title("SHAP Feature Importance")
321
+ plt.tight_layout()
322
+ plt.savefig(dot_path)
323
+ _LOGGER.info(f"📊 SHAP dot plot saved as '{dot_path.name}'")
324
+ plt.close()
325
+
326
+ # Save Summary Data to CSV
327
+ summary_path = save_dir_path / "shap_summary.csv"
328
+ # Ensure the array is 1D before creating the DataFrame
329
+ mean_abs_shap = np.abs(shap_values).mean(axis=0).flatten()
330
+
331
+ if feature_names is None:
332
+ feature_names = [f'feature_{i}' for i in range(len(mean_abs_shap))]
333
+
334
+ summary_df = pd.DataFrame({
335
+ 'feature': feature_names,
336
+ 'mean_abs_shap_value': mean_abs_shap
337
+ }).sort_values('mean_abs_shap_value', ascending=False)
338
+
339
+ summary_df.to_csv(summary_path, index=False)
340
+
341
+ _LOGGER.info(f"📝 SHAP summary data saved as '{summary_path.name}'")
342
+ plt.ion()
343
+
344
+ else:
345
+ _LOGGER.info("No save directory provided. Displaying SHAP dot plot.")
346
+ shap.summary_plot(shap_values, instances_to_explain_np, feature_names=feature_names, plot_type="dot")
347
+
348
+
349
+ def info():
350
+ _script_info(__all__)
@@ -8,16 +8,16 @@ import numpy as np
8
8
  from .ML_callbacks import Callback, History, TqdmProgressBar
9
9
  from .ML_evaluation import classification_metrics, regression_metrics, plot_losses, shap_summary_plot
10
10
  from ._script_info import _script_info
11
- from .keys import LogKeys
11
+ from .keys import PyTorchLogKeys
12
12
  from ._logger import _LOGGER
13
13
 
14
14
 
15
15
  __all__ = [
16
- "MyTrainer"
16
+ "MLTrainer"
17
17
  ]
18
18
 
19
19
 
20
- class MyTrainer:
20
+ class MLTrainer:
21
21
  def __init__(self, model: nn.Module, train_dataset: Dataset, test_dataset: Dataset,
22
22
  kind: Literal["regression", "classification"],
23
23
  criterion: nn.Module, optimizer: torch.optim.Optimizer,
@@ -95,14 +95,16 @@ class MyTrainer:
95
95
  batch_size=batch_size,
96
96
  shuffle=shuffle,
97
97
  num_workers=loader_workers,
98
- pin_memory=(self.device.type == "cuda")
98
+ pin_memory=("cuda" in self.device.type),
99
+ drop_last=True # Drops the last batch if incomplete, selecting a good batch size is key.
99
100
  )
101
+
100
102
  self.test_loader = DataLoader(
101
103
  dataset=self.test_dataset,
102
104
  batch_size=batch_size,
103
105
  shuffle=False,
104
106
  num_workers=loader_workers,
105
- pin_memory=(self.device.type == "cuda")
107
+ pin_memory=("cuda" in self.device.type)
106
108
  )
107
109
 
108
110
  def fit(self, epochs: int = 10, batch_size: int = 10, shuffle: bool = True):
@@ -159,8 +161,8 @@ class MyTrainer:
159
161
  for batch_idx, (features, target) in enumerate(self.train_loader): # type: ignore
160
162
  # Create a log dictionary for the batch
161
163
  batch_logs = {
162
- LogKeys.BATCH_INDEX: batch_idx,
163
- LogKeys.BATCH_SIZE: features.size(0)
164
+ PyTorchLogKeys.BATCH_INDEX: batch_idx,
165
+ PyTorchLogKeys.BATCH_SIZE: features.size(0)
164
166
  }
165
167
  self.callbacks_hook('on_batch_begin', batch_idx, logs=batch_logs)
166
168
 
@@ -178,11 +180,11 @@ class MyTrainer:
178
180
  running_loss += batch_loss * features.size(0)
179
181
 
180
182
  # Add the batch loss to the logs and call the end-of-batch hook
181
- batch_logs[LogKeys.BATCH_LOSS] = batch_loss
183
+ batch_logs[PyTorchLogKeys.BATCH_LOSS] = batch_loss
182
184
  self.callbacks_hook('on_batch_end', batch_idx, logs=batch_logs)
183
185
 
184
186
  # Return the average loss for the entire epoch
185
- return {LogKeys.TRAIN_LOSS: running_loss / len(self.train_loader.dataset)} # type: ignore
187
+ return {PyTorchLogKeys.TRAIN_LOSS: running_loss / len(self.train_loader.dataset)} # type: ignore
186
188
 
187
189
  def _validation_step(self):
188
190
  self.model.eval()
@@ -195,7 +197,7 @@ class MyTrainer:
195
197
  output = output.view_as(target)
196
198
  loss = self.criterion(output, target)
197
199
  running_loss += loss.item() * features.size(0)
198
- logs = {LogKeys.VAL_LOSS: running_loss / len(self.test_loader.dataset)} # type: ignore
200
+ logs = {PyTorchLogKeys.VAL_LOSS: running_loss / len(self.test_loader.dataset)} # type: ignore
199
201
  return logs
200
202
 
201
203
  def _predict_for_eval(self, dataloader: DataLoader):
@@ -230,14 +232,14 @@ class MyTrainer:
230
232
 
231
233
  yield y_pred_batch, y_prob_batch, y_true_batch
232
234
 
233
- def evaluate(self, save_dir: Optional[Union[str,Path]], data: Optional[Union[DataLoader, Dataset]] = None):
235
+ def evaluate(self, save_dir: Union[str,Path], data: Optional[Union[DataLoader, Dataset]] = None):
234
236
  """
235
237
  Evaluates the model on the given data.
236
238
 
237
239
  Args:
238
240
  data (DataLoader | Dataset | None ): The data to evaluate on.
239
241
  Can be a DataLoader or a Dataset. If None, defaults to the trainer's internal test_dataset.
240
- save_dir (str | Path | None): Directory to save all reports and plots. If None, metrics are shown but not saved.
242
+ save_dir (str | Path): Directory to save all reports and plots.
241
243
  """
242
244
  eval_loader = None
243
245
  if isinstance(data, DataLoader):
@@ -273,14 +275,14 @@ class MyTrainer:
273
275
  y_prob = np.concatenate(all_probs) if self.kind == "classification" else None
274
276
 
275
277
  if self.kind == "classification":
276
- classification_metrics(y_true, y_pred, y_prob, save_dir=save_dir)
278
+ classification_metrics(save_dir, y_true, y_pred, y_prob)
277
279
  else:
278
- regression_metrics(y_true.flatten(), y_pred.flatten(), save_dir=save_dir)
280
+ regression_metrics(y_true.flatten(), y_pred.flatten(), save_dir)
279
281
 
280
282
  print("\n--- Training History ---")
281
283
  plot_losses(self.history, save_dir=save_dir)
282
284
 
283
- def explain(self, explain_dataset: Optional[Dataset] = None, n_samples: int = 100,
285
+ def explain(self, explain_dataset: Optional[Dataset] = None, n_samples: int = 1000,
284
286
  feature_names: Optional[List[str]] = None, save_dir: Optional[Union[str,Path]] = None):
285
287
  """
286
288
  Explains model predictions using SHAP and saves all artifacts.
@@ -12,7 +12,7 @@ from .path_manager import sanitize_filename, make_fullpath, list_files_by_extens
12
12
  import torch
13
13
  from tqdm import trange
14
14
  from ._logger import _LOGGER
15
- from .keys import ModelSaveKeys
15
+ from .keys import EnsembleKeys
16
16
  from ._script_info import _script_info
17
17
  from .SQL import DatabaseManager
18
18
  from contextlib import nullcontext
@@ -48,9 +48,9 @@ class ObjectiveFunction():
48
48
  self.is_hybrid = False if binary_features <= 0 else True
49
49
  self.use_noise = add_noise
50
50
  self._artifact = deserialize_object(trained_model_path, verbose=False, raise_on_error=True)
51
- self.model = self._get_from_artifact(ModelSaveKeys.MODEL)
52
- self.feature_names: Optional[list[str]] = self._get_from_artifact(ModelSaveKeys.FEATURES) # type: ignore
53
- self.target_name: Optional[str] = self._get_from_artifact(ModelSaveKeys.TARGET) # type: ignore
51
+ self.model = self._get_from_artifact(EnsembleKeys.MODEL)
52
+ self.feature_names: Optional[list[str]] = self._get_from_artifact(EnsembleKeys.FEATURES) # type: ignore
53
+ self.target_name: Optional[str] = self._get_from_artifact(EnsembleKeys.TARGET) # type: ignore
54
54
  self.task = task
55
55
  self.check_model() # check for classification models and None values
56
56
 
@@ -126,7 +126,7 @@ class ObjectiveFunction():
126
126
  if self._artifact is None:
127
127
  raise TypeError("Load model error")
128
128
  val = self._artifact.get(key)
129
- if key == ModelSaveKeys.FEATURES:
129
+ if key == EnsembleKeys.FEATURES:
130
130
  result = val if isinstance(val, list) and val else None
131
131
  else:
132
132
  result = val if val else None