Skip to content

Commit 23f4186

Browse files
committed
Refactor: Clean up unused variables and improve code style
This commit focuses on improving code quality by removing unused variables and adhering to more Pythonic style conventions. Key changes include: Removed numerous unused local variables across various modules, including model classes, pipeline steps, and utility functions. Replaced lambda functions with standard def statements for better readability and clarity in keras_classifier_class.py and embeddings.py. Updated boolean comparisons from == False to the more idiomatic not ... in main.py and notebooks. Simplified boolean DataFrame indexing in plot_feature_categories.py and notebooks. Removed unnecessary assignments where the return value of a function was not used. These changes do not affect the functionality but enhance code maintainability and reduce clutter.
1 parent e30229f commit 23f4186

14 files changed

Lines changed: 52 additions & 68 deletions

ml_grid/model_classes/keras_classifier_class.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,22 @@ def __init__(
120120
l1_reg=0.0, # Register l1_reg with a default value
121121
l2_reg=0.0, # Register l2_reg with a default value
122122
)
123-
X_data = self.X
124-
y_data = self.y
125123

126124
# vals = np.linspace(2, 750, 6)
127125
vals = np.logspace(1, 2.0, 3)
128126

129-
floorer = lambda t: math.floor(t)
127+
def floorer(t):
128+
return math.floor(t)
129+
130130
floored_width = np.array([floorer(xi) for xi in vals])
131131
floored_width = np.insert(floored_width, 0, 1, axis=None)
132132
floored_width
133133

134134
vals = np.logspace(1, 2.0, 3)
135135

136-
floorer = lambda t: math.floor(t)
136+
def floorer(t):
137+
return math.floor(t)
138+
137139
floored_depth = np.array([floorer(xi) for xi in vals])
138140
floored_depth = np.insert(floored_depth, 0, 1, axis=None)
139141
floored_depth

ml_grid/model_classes_time_series/CNNClassifier_module.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ def __init__(self, ml_grid_object: pipe):
2929
Args:
3030
ml_grid_object (pipe): An instance of the main data pipeline object.
3131
"""
32-
time_limit_param = ml_grid_object.global_params.time_limit_param
33-
34-
n_jobs_model_val = ml_grid_object.global_params.n_jobs_model_val
3532

3633
random_state_val = ml_grid_object.global_params.random_state_val
3734

ml_grid/model_classes_time_series/Catch22Classifer_module.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def __init__(self, ml_grid_object: pipe):
3131
ml_grid_object (pipe): An instance of the main data pipeline object.
3232
"""
3333

34-
verbose_param = ml_grid_object.verbose
3534
random_state_val = ml_grid_object.global_params.random_state_val
3635
n_jobs_model_val = ml_grid_object.global_params.n_jobs_model_val
3736

ml_grid/model_classes_time_series/ResNetClassifier_module.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ def __init__(self, ml_grid_object: pipe):
3131
ml_grid_object (pipe): An instance of the main data pipeline object.
3232
"""
3333

34-
random_state_val = ml_grid_object.global_params.random_state_val
35-
36-
n_jobs_model_val = ml_grid_object.global_params.n_jobs_model_val
37-
3834
verbose_param = ml_grid_object.verbose
3935

4036
param_space = ParamSpace(

ml_grid/model_classes_time_series/TemporalDictionaryEnsembleClassifier_module.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ def __init__(self, ml_grid_object: pipe):
3232
ml_grid_object (pipe): An instance of the main data pipeline object.
3333
"""
3434

35-
verbose_param = ml_grid_object.verbose
36-
3735
random_state_val = ml_grid_object.global_params.random_state_val
3836

3937
time_limit_param = global_parameters.time_limit_param

ml_grid/pipeline/embeddings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ def create_embedding_pipeline(
152152
elif method_lower == "select_kbest_mi":
153153
default_params = {"random_state": 42}
154154
default_params.update(kwargs)
155-
score_func = lambda X, y: mutual_info_classif(X, y, **default_params)
155+
156+
def score_func(X, y):
157+
return mutual_info_classif(X, y, **default_params)
158+
156159
steps.append(("embed", SelectKBest(score_func=score_func, k=n_components)))
157160

158161
else:

ml_grid/pipeline/grid_search_cross_validate.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,6 @@ def __init__(
593593
if not failed and self.global_parameters.verbose >= 3:
594594
self.logger.debug("Fitting final model")
595595

596-
metric_list = self.metric_list
597-
598596
if not failed and self.y_train.nunique() < 2:
599597
raise ValueError(
600598
"Only one class present in y_train. ROC AUC score is not defined "
@@ -902,8 +900,6 @@ def __init__(
902900
f"Cross-validation for {method_name} completed in {elapsed_time:.2f} seconds."
903901
)
904902

905-
current_algorithm_scores = scores
906-
907903
if self.global_parameters.verbose >= 4:
908904

909905
debug_print_statements_class(scores).debug_print_scores()
@@ -975,7 +971,7 @@ def _adjust_knn_parameters(self, parameter_space: Union[Dict, List[Dict]]):
975971
Dynamically adjusts the 'n_neighbors' parameter for KNN-based models
976972
to prevent errors on small datasets during cross-validation.
977973
"""
978-
n_splits = self.cv.get_n_splits()
974+
self.cv.get_n_splits()
979975

980976
# Correctly calculate the training fold size
981977
dummy_indices = np.arange(len(self.X_train))

ml_grid/pipeline/main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ def execute(self) -> Tuple[List[List[Any]], float]:
361361

362362
self.model_error_list = []
363363
self.highest_score = 0
364-
highest_score = 0 # for optimisation
365364

366365
if self.multiprocess:
367366

@@ -373,11 +372,11 @@ def multi_run_wrapper(args: Tuple) -> Any:
373372
from multiprocessing import Pool
374373

375374
pool = Pool(8)
376-
results = pool.map(multi_run_wrapper, self.arg_list)
375+
pool.map(multi_run_wrapper, self.arg_list)
377376
# print(results)
378377
pool.close() # exp
379378

380-
elif self.multiprocess == False:
379+
elif not self.multiprocess:
381380
for k in range(0, len(self.arg_list)):
382381
try:
383382
self.logger.info(

ml_grid/results_processing/plot_feature_categories.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ def plot_category_impact_on_metric(
187187
if plot_data[category].nunique() < 2:
188188
continue
189189

190-
mean_with = plot_data[plot_data[category] == True][metric].mean()
191-
mean_without = plot_data[plot_data[category] == False][metric].mean()
190+
mean_with = plot_data[plot_data[category]][metric].mean()
191+
mean_without = plot_data[not plot_data[category]][metric].mean()
192192

193193
impact = mean_with - mean_without
194194

ml_grid/util/project_score_save.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,12 @@ def update_score_log(
165165

166166
X_train = ml_grid_object_iter.X_train
167167

168-
y_train = ml_grid_object_iter.y_train
169-
170168
X_test = ml_grid_object_iter.X_test
171169

172170
y_test = ml_grid_object_iter.y_test
173171

174172
X_test_orig = ml_grid_object_iter.X_test_orig
175173

176-
y_test_orig = ml_grid_object_iter.y_test_orig
177-
178174
param_space_index = ml_grid_object.param_space_index
179175

180176
bayessearch = global_params.bayessearch

0 commit comments

Comments
 (0)