Skip to content

Commit 581b713

Browse files
committed
Configure hierarchical acousticbrainz.models logger
1 parent baaf96a commit 581b713

12 files changed

Lines changed: 156 additions & 159 deletions

File tree

acousticbrainz/models/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
# -*- coding: utf-8 -*-
1+
import logging
2+
3+
ACOUSTICBRAINZ_SKLEARN_LOGGER = "acousticbrainz.models"
4+
_logger = logging.getLogger(ACOUSTICBRAINZ_SKLEARN_LOGGER)
5+
_handler = logging.StreamHandler()
6+
_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
7+
_handler.setFormatter(_formatter)
8+
_logger.addHandler(_handler)
9+
_logger.setLevel(logging.INFO)

acousticbrainz/models/sklearn/classification/classification_task.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
import logging
12
import os
23
import json
34
from ..classification.classifier_grid import TrainGridClassifier
45
from ..classification.evaluation import evaluation
56

67

8+
logger = logging.getLogger(__name__)
9+
10+
711
class ClassificationTask:
812
"""
913
This class is the core of the model classification. It loads the relevant classifier to
1014
be used for training, the features, the labels, and the tracks. It uses a corresponding
1115
to the configuration file declared class to train the model and then it uses that model
1216
for evaluation.
1317
"""
14-
def __init__(self, config, classifier, train_class, training_processes, X, y, exports_path, tracks, logger):
18+
def __init__(self, config, classifier, train_class, training_processes, X, y, exports_path, tracks):
1519
"""
1620
Args:
1721
config: The configuration data that contain the settings from the configuration
@@ -27,7 +31,6 @@ def __init__(self, config, classifier, train_class, training_processes, X, y, ex
2731
y: The labels (NumPy array) of the target class
2832
exports_path: Path to where the classification project's results will be stored to.
2933
tracks: The tracks (numpy.ndarray) that are exported from the Groundtruth file.
30-
log_level: The logging level (0-4).
3134
"""
3235
self.config = config
3336
self.classifier = classifier
@@ -38,13 +41,12 @@ def __init__(self, config, classifier, train_class, training_processes, X, y, ex
3841
self.training_processes = training_processes
3942
self.exports_path = exports_path
4043
self.tracks = tracks
41-
self.logger = logger
4244

4345

4446
def run(self):
4547
# grid search train
4648
if self.config["train_kind"] == "grid":
47-
self.logger.info("Train Classifier: Classifier with GridSearchCV")
49+
logger.info("Train Classifier: Classifier with GridSearchCV")
4850
grid_svm_train = TrainGridClassifier(config=self.config,
4951
classifier=self.classifier,
5052
class_name=self.train_class,
@@ -57,15 +59,15 @@ def run(self):
5759
grid_svm_train.train_grid_search_clf()
5860
grid_svm_train.export_best_classifier()
5961
else:
60-
self.logger.error("Use a valid classifier in the configuration file.")
61-
self.logger.info("Training the classifier is completed successfully.")
62+
logger.error("Use a valid classifier in the configuration file.")
63+
logger.info("Training the classifier is completed successfully.")
6264

6365
# load best model to check its parameters
64-
self.logger.debug("Loading the Best Model..")
66+
logger.debug("Loading the Best Model..")
6567
best_model_name = "best_model_{}.json".format(self.train_class)
6668
with open(os.path.join(self.exports_path, best_model_name)) as best_model_file:
6769
best_model = json.load(best_model_file)
68-
self.logger.debug("BEST MODEL: {}".format(best_model))
70+
logger.debug("BEST MODEL: {}".format(best_model))
6971

7072
# evaluation
7173
evaluation(config=self.config,
@@ -75,5 +77,4 @@ def run(self):
7577
tracks=self.tracks,
7678
process=best_model["preprocessing"],
7779
exports_path=self.exports_path,
78-
logger=self.logger
7980
)

acousticbrainz/models/sklearn/classification/classification_task_manager.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
from time import time
34
from termcolor import colored
@@ -7,6 +8,8 @@
78
from ..classification.classification_task import ClassificationTask
89

910

11+
logger = logging.getLogger(__name__)
12+
1013
validClassifiers = ["svm", "NN"]
1114
validEvaluations = ["nfoldcrossvalidation"]
1215

@@ -20,7 +23,7 @@ class ClassificationTaskManager:
2023
with their corresponding preprocessing steps and parameters declaration for the
2124
classifier, and executes the classification task for each step.
2225
"""
23-
def __init__(self, config, train_class, X, y, tracks, exports_path, logger):
26+
def __init__(self, config, train_class, X, y, tracks, exports_path):
2427
"""
2528
Args:
2629
config: The configuration file name.
@@ -34,7 +37,6 @@ def __init__(self, config, train_class, X, y, tracks, exports_path, logger):
3437
self.y = y
3538
self.tracks = tracks
3639
self.exports_path = exports_path
37-
self.logger = logger
3840

3941
self.results_path = ""
4042
self.logs_path = ""
@@ -72,33 +74,33 @@ def config_file_analysis(self):
7274
"""
7375
Check the keys of the configuration template file if they are set up correctly.
7476
"""
75-
self.logger.info("---- CHECK FOR INAPPROPRIATE CONFIG FILE FORMAT ----")
77+
logger.info("---- CHECK FOR INAPPROPRIATE CONFIG FILE FORMAT ----")
7678
if "processing" not in self.config:
77-
self.logger.error("No preprocessing defined in config.")
79+
logger.error("No preprocessing defined in config.")
7880

7981
if "evaluations" not in self.config:
80-
self.logger.error("No evaluations defined in config.")
81-
self.logger.error("Setting default evaluation to 10-fold cross-validation")
82+
logger.error("No evaluations defined in config.")
83+
logger.error("Setting default evaluation to 10-fold cross-validation")
8284
self.config["evaluations"] = {"nfoldcrossvalidation": [{"nfold": [10]}]}
8385

8486
for classifier in self.config['classifiers'].keys():
8587
if classifier not in validClassifiers:
86-
self.logger.error("Not a valid classifier: {}".format(classifier))
88+
logger.error("Not a valid classifier: {}".format(classifier))
8789
raise ValueError("The classifier name must be valid.")
8890

8991
for evaluation in self.config['evaluations'].keys():
9092
if evaluation not in validEvaluations:
91-
self.logger.error("Not a valid evaluation: {}".format(evaluation))
93+
logger.error("Not a valid evaluation: {}".format(evaluation))
9294
raise ValueError("The evaluation must be valid.")
93-
self.logger.info("No errors in config file format found.")
95+
logger.info("No errors in config file format found.")
9496

9597
def apply_processing(self):
9698
"""
9799
Evaluation steps extraction and classification task execution for each step.
98100
"""
99101
start_time = time()
100102
training_processes = TrainingProcesses(self.config).training_processes()
101-
self.logger.info("Classifiers detected: {}".format(self.config["classifiers"].keys()))
103+
logger.info("Classifiers detected: {}".format(self.config["classifiers"].keys()))
102104
for classifier in self.config["classifiers"].keys():
103105
print("Before Classification task: ", classifier)
104106
task = ClassificationTask(config=self.config,
@@ -109,18 +111,17 @@ def apply_processing(self):
109111
y=self.y,
110112
exports_path=self.exports_path,
111113
tracks=self.tracks,
112-
logger=self.logger
113114
)
114115
try:
115116
task.run()
116117
except Exception as e:
117-
self.logger.error('Running task failed: {}'.format(e))
118+
logger.error('Running task failed: {}'.format(e))
118119
print(colored('Running task failed: {}'.format(e), "red"))
119120
end_time = time()
120121

121122
print()
122123
print(colored("Last evaluation took place at: {}".format(datetime.now()), "magenta"))
123-
self.logger.info("Last evaluation took place at: {}".format(datetime.now()))
124+
logger.info("Last evaluation took place at: {}".format(datetime.now()))
124125

125126
# test duration
126127
time_duration = end_time - start_time

acousticbrainz/models/sklearn/classification/classifier_grid.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import json
34
from termcolor import colored
@@ -9,8 +10,11 @@
910
from ..transformation.transform import Transform
1011

1112

13+
logger = logging.getLogger(__name__)
14+
15+
1216
class TrainGridClassifier:
13-
def __init__(self, config, classifier, class_name, X, y, tr_processes, exports_path, logger):
17+
def __init__(self, config, classifier, class_name, X, y, tr_processes, exports_path):
1418
self.config = config
1519
self.classifier = classifier
1620
self.class_name = class_name
@@ -19,7 +23,6 @@ def __init__(self, config, classifier, class_name, X, y, tr_processes, exports_p
1923
self.tr_processes = tr_processes
2024
self.exports_path = exports_path
2125

22-
self.logger = logger
2326
self.best_models_list = []
2427
# self.train_grid_search_clf()
2528

@@ -28,7 +31,7 @@ def train_grid_search_clf(self):
2831
process_counter = 1
2932
for tr_process in self.tr_processes:
3033
print(colored("Train process {} - {}".format(process_counter, tr_process), "green"))
31-
self.logger.info("(Grid) - Train process {} - {}".format(process_counter, tr_process))
34+
logger.info("(Grid) - Train process {} - {}".format(process_counter, tr_process))
3235
# initiate SVM classifier object
3336
if self.classifier == "svm":
3437
grid_clf = SVC(gamma="auto", probability=True)
@@ -42,16 +45,14 @@ def train_grid_search_clf(self):
4245
df_feats=self.X,
4346
process=tr_process["preprocess"],
4447
train_class=self.class_name,
45-
exports_path=self.exports_path,
46-
logger=self.logger).post_processing()
48+
exports_path=self.exports_path).post_processing()
4749

4850
# train the grid classifier and return the trained model
4951
gsvc = train_grid(tr_process=tr_process,
5052
grid_clf=grid_clf,
5153
features_prepared=features_prepared,
5254
y=self.y,
53-
config=self.config,
54-
logger=self.logger)
55+
config=self.config)
5556

5657
# save best results for each train process
5758
# paths declaration for saving the grid training results
@@ -65,8 +66,7 @@ def train_grid_search_clf(self):
6566
class_name=self.class_name,
6667
tr_process=tr_process,
6768
results_path=results_path,
68-
best_process_model_path=best_process_model_path,
69-
logger=self.logger)
69+
best_process_model_path=best_process_model_path)
7070

7171
# return a list that includes the best models exported from each processing
7272
self.best_models_list.append(results_dict)
@@ -81,21 +81,21 @@ def train_grid_search_clf(self):
8181
def export_best_classifier(self):
8282
# Gather the best scores from the exported grid clf models
8383
scores = [x["score"] for x in self.best_models_list]
84-
self.logger.info("This is the max score of all the training processes: {}".format(max(scores)))
84+
logger.info("This is the max score of all the training processes: {}".format(max(scores)))
8585
for model in self.best_models_list:
8686
if model["score"] == max(scores):
87-
self.logger.info("Best {} model parameters:".format(self.class_name))
87+
logger.info("Best {} model parameters:".format(self.class_name))
8888
# log2 --> convert values to initial parameters' values
8989
# model["params"]["C"] = math.log2(model["params"]["C"])
9090
# model["params"]["gamma"] = math.log2(model["params"]["gamma"])
91-
self.logger.info("{}".format(model))
91+
logger.info("{}".format(model))
9292
best_model_name = "best_model_{}.json".format(self.class_name)
9393
with open(os.path.join(self.exports_path, best_model_name), "w") as best_model:
9494
json.dump(model, best_model, indent=4)
95-
self.logger.info("Best {} model parameters saved successfully to disk.".format(self.class_name))
95+
logger.info("Best {} model parameters saved successfully to disk.".format(self.class_name))
9696

9797

98-
def train_grid(tr_process, grid_clf, features_prepared, y, config, logger):
98+
def train_grid(tr_process, grid_clf, features_prepared, y, config):
9999
# define the length of parameters
100100
parameters_grid = {'kernel': tr_process["kernel"],
101101
'C': tr_process["C"],
@@ -136,7 +136,7 @@ def train_grid(tr_process, grid_clf, features_prepared, y, config, logger):
136136
return gsvc
137137

138138

139-
def save_grid_results(gsvc, class_name, tr_process, results_path, best_process_model_path, logger):
139+
def save_grid_results(gsvc, class_name, tr_process, results_path, best_process_model_path):
140140
results_best_dict_name = "result_{}_{}_best_{}.json" \
141141
.format(class_name, tr_process["preprocess"], gsvc.best_score_)
142142

0 commit comments

Comments
 (0)