Skip to content

Commit cf62dee

Browse files
committed
logging refactor, minor fixes
1 parent f418b38 commit cf62dee

51 files changed

Lines changed: 472 additions & 352 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ml_grid/model_classes/NeuralNetworkClassifier_class.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
# from ml_grid.model_classes.nni_sklearn_wrapper import *
99
from ml_grid.model_classes.nni_sklearn_wrapper import NeuralNetworkClassifier
10+
import logging
1011

11-
print("Imported NeuralNetworkClassifier class")
12+
logging.getLogger('ml_grid').debug("Imported NeuralNetworkClassifier class")
1213

1314

1415
class NeuralNetworkClassifier_class:

ml_grid/model_classes/adaboost_classifier_class.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
from ml_grid.util import param_space
66
from ml_grid.util.global_params import global_parameters
77
from skopt.space import Categorical, Real, Integer
8+
import logging
89

9-
print("Imported AdaBoostClassifier class")
10+
logging.getLogger('ml_grid').debug("Imported AdaBoostClassifier class")
1011

1112
class adaboost_class:
1213
"""AdaBoostClassifier with support for both Bayesian and non-Bayesian parameter spaces."""

ml_grid/model_classes/catboost_classifier_class.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from skopt.space import Categorical, Real, Integer
77
from ml_grid.util import param_space
88
from ml_grid.util.global_params import global_parameters
9+
import logging
910

1011
class CatBoost_class:
1112
"""CatBoost Classifier with hyperparameter tuning."""
@@ -61,7 +62,7 @@ def __init__(
6162
"verbose": Categorical([0]),
6263
"allow_const_label": Categorical([True]),
6364
}
64-
print(f"Bayesian Parameter Space: {self.parameter_space}")
65+
logging.getLogger('ml_grid').debug(f"Bayesian Parameter Space for CatBoost: {self.parameter_space}")
6566
else:
6667
self.parameter_space = {
6768
"iterations": [100, 200, 500, 1000],
@@ -85,6 +86,6 @@ def __init__(
8586
"verbose": [0],
8687
"allow_const_label": [True],
8788
}
88-
print(f"Traditional Parameter Space: {self.parameter_space}")
89+
logging.getLogger('ml_grid').debug(f"Traditional Parameter Space for CatBoost: {self.parameter_space}")
8990

9091
return None

ml_grid/model_classes/gaussiannb_class.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
from typing import Any, Dict, List, Optional
44

5+
import logging
56
import numpy as np
67
import pandas as pd
78
from ml_grid.util import param_space
89
from ml_grid.util.global_params import global_parameters
910
from sklearn.naive_bayes import GaussianNB
1011
from skopt.space import Categorical
1112

12-
print("Imported gaussiannb class")
13+
logging.getLogger('ml_grid').debug("Imported gaussiannb class")
1314

1415

1516
class GaussianNBWrapper(GaussianNB):
@@ -91,7 +92,7 @@ def __init__(
9192
}
9293

9394
# Log parameter space for verification
94-
print(f"Parameter Space: {self.parameter_space}")
95+
logging.getLogger('ml_grid').debug(f"Parameter Space: {self.parameter_space}")
9596

9697
else:
9798
# For traditional grid search, use lists

ml_grid/model_classes/h2o_classifier_class.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from ml_grid.util.global_params import global_parameters
77
from skopt.space import Categorical, Real, Integer
88
# from h2o.sklearn import H2OAutoMLClassifier
9+
import logging
910

10-
print("Imported h2o_classifier_class")
11+
logging.getLogger('ml_grid').debug("Imported h2o_classifier_class")
1112

1213
class h2o_classifier_class:
1314
"""H2OAutoMLClassifier with support for both Bayesian and non-Bayesian parameter spaces."""
@@ -29,7 +30,7 @@ def __init__(
2930
optimization. Defaults to None.
3031
"""
3132
global_params = global_parameters
32-
print("init h2o_classifier_class")
33+
logging.getLogger('ml_grid').debug("init h2o_classifier_class")
3334
self.X = X
3435
self.y = y
3536

ml_grid/model_classes/knn_classifier_class.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
from ml_grid.util.global_params import global_parameters
66
from sklearn.neighbors import KNeighborsClassifier
77
from skopt.space import Real, Integer, Categorical
8+
import logging
89

9-
print("Imported KNeighborsClassifier class")
10+
logging.getLogger('ml_grid').debug("Imported KNeighborsClassifier class")
1011

1112
class knn_classifiers_class:
1213
"""KNeighborsClassifier with support for both Bayesian and non-Bayesian parameter spaces."""

ml_grid/model_classes/knn_gpu_classifier_class.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
from ml_grid.util import param_space
66
from ml_grid.util.global_params import global_parameters
77
from skopt.space import Categorical, Integer
8+
import logging
89

9-
print("Imported knn__gpu class")
10+
logging.getLogger('ml_grid').debug("Imported knn__gpu class")
1011

1112
class knn__gpu_wrapper_class:
1213
"""KNN with GPU support, including Bayesian and non-Bayesian parameter space."""

ml_grid/model_classes/knn_wrapper_class.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import torch
77
from simbsig.neighbors import KNeighborsClassifier
88
from sklearn import metrics
9+
import logging
910

1011
class KNNWrapper:
1112
"""A scikit-learn compatible wrapper for the GPU-accelerated KNN from simbsig.
@@ -51,7 +52,7 @@ def __init__(
5152
# Auto-detect device
5253
gpu_available = torch.cuda.is_available()
5354
if device == "gpu" and not gpu_available:
54-
print("Warning: GPU requested for KNNWrapper, but torch.cuda.is_available() is False. Falling back to CPU.")
55+
logging.getLogger('ml_grid').warning("GPU requested for KNNWrapper, but torch.cuda.is_available() is False. Falling back to CPU.")
5556
self.device = "cpu"
5657
elif device:
5758
self.device = device

ml_grid/model_classes/light_gbm_class.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from ml_grid.model_classes.lightgbm_class import LightGBMClassifier
1010
from ml_grid.util.global_params import global_parameters
11+
import logging
1112

1213

1314
class LightGBMClassifierWrapper:
@@ -85,6 +86,5 @@ def __init__(
8586

8687

8788

88-
89-
print("Imported LightGBM classifier wrapper class")
89+
logging.getLogger('ml_grid').debug("Imported LightGBM classifier wrapper class")
9090
# light_gbm_class LightGBMClassifierWrapper

ml_grid/model_classes/lightgbm_class.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,22 @@ def fit(
8989
# early_stopping_rounds=self.early_stopping_rounds,
9090
verbose=self.verbosity,
9191
)
92-
# X.columns = X.columns.str.replace('[^a-zA-Z0-9_]', '', regex=True)
93-
# Change columns names ([LightGBM] Do not support special JSON characters in feature name.)
94-
new_names = {col: re.sub(r"[^A-Za-z0-9_]+", "", col) for col in X.columns}
95-
new_n_list = list(new_names.values())
96-
# [LightGBM] Feature appears more than one time.
97-
new_names = {
98-
col: f"{new_col}_{i}" if new_col in new_n_list[:i] else new_col
99-
for i, (col, new_col) in enumerate(new_names.items())
100-
}
101-
X = X.rename(columns=new_names)
92+
93+
X_fit = X
94+
if isinstance(X, pd.DataFrame):
95+
# Change columns names ([LightGBM] Do not support special JSON characters in feature name.)
96+
new_names = {col: re.sub(r"[^A-Za-z0-9_]+", "", col) for col in X.columns}
97+
new_n_list = list(new_names.values())
98+
# [LightGBM] Feature appears more than one time.
99+
new_names = {
100+
col: f"{new_col}_{i}" if new_col in new_n_list[:i] else new_col
101+
for i, (col, new_col) in enumerate(new_names.items())
102+
}
103+
X_fit = X.rename(columns=new_names)
102104

103105
y = np.ravel(y)
104106

105-
self.model.fit(X, y)
107+
self.model.fit(X_fit, y)
106108
if self.objective == "binary":
107109
self.classes_ = np.unique(y)
108110
return self
@@ -127,18 +129,20 @@ def predict(self, X: pd.DataFrame) -> np.ndarray:
127129
"Model has not been fitted yet. Call 'fit' before 'predict'."
128130
)
129131

130-
# Change columns names ([LightGBM] Do not support special JSON characters in feature name.)
131-
new_names = {
132-
col: re.sub(r"[^A-Za-z0-9_]+", "", col) for col in X.columns
133-
}
134-
new_n_list = list(new_names.values())
135-
# [LightGBM] Feature appears more than one time.
136-
new_names = {
137-
col: f"{new_col}_{i}" if new_col in new_n_list[:i] else new_col
138-
for i, (col, new_col) in enumerate(new_names.items())
139-
}
140-
X = X.rename(columns=new_names)
141-
return self.model.predict(X)
132+
X_pred = X
133+
if isinstance(X, pd.DataFrame):
134+
# Change columns names ([LightGBM] Do not support special JSON characters in feature name.)
135+
new_names = {
136+
col: re.sub(r"[^A-Za-z0-9_]+", "", col) for col in X.columns
137+
}
138+
new_n_list = list(new_names.values())
139+
# [LightGBM] Feature appears more than one time.
140+
new_names = {
141+
col: f"{new_col}_{i}" if new_col in new_n_list[:i] else new_col
142+
for i, (col, new_col) in enumerate(new_names.items())
143+
}
144+
X_pred = X.rename(columns=new_names)
145+
return self.model.predict(X_pred)
142146

143147
def score(self, X: pd.DataFrame, y: Union[pd.Series, np.ndarray]) -> float:
144148
"""Returns the mean accuracy on the given test data and labels.

0 commit comments

Comments
 (0)