From 477e2274bbbada56b3241746805fd9565687faa8 Mon Sep 17 00:00:00 2001 From: parisa-zahedi Date: Wed, 26 Jun 2024 16:50:22 +0200 Subject: [PATCH] fix black errors --- .../classifier/data_preparation_dl.py | 1 + .../classifier/data_preparation_svm.py | 1 + .../hpo_dropout_weight.py | 3 ++- .../hpo_epoch_batch.py | 1 + .../hpo_learing_rate.py | 1 + .../classifier/model/acoustic_model.py | 26 +++++++++---------- bioacoustics/classifier/model/cnn10_model.py | 2 +- .../acoustic_features/features.py | 22 ++++++++-------- .../acoustic_features/featuresFunctions.py | 4 +-- .../wav_processing/chunk_wav/make_chunks.py | 1 + .../wav_processing/condensation/extractor.py | 2 +- 11 files changed, 34 insertions(+), 30 deletions(-) diff --git a/bioacoustics/classifier/data_preparation_dl.py b/bioacoustics/classifier/data_preparation_dl.py index f733c8e..265f425 100755 --- a/bioacoustics/classifier/data_preparation_dl.py +++ b/bioacoustics/classifier/data_preparation_dl.py @@ -1,4 +1,5 @@ """Script to prepare train and test data to feed into a model""" + import glob import os import numpy as np diff --git a/bioacoustics/classifier/data_preparation_svm.py b/bioacoustics/classifier/data_preparation_svm.py index 82ca780..1dd66b9 100755 --- a/bioacoustics/classifier/data_preparation_svm.py +++ b/bioacoustics/classifier/data_preparation_svm.py @@ -1,4 +1,5 @@ """Data preparation for SVM classifier.""" + import pandas as pd import numpy as np import glob diff --git a/bioacoustics/classifier/hyperparameter_optimization/hpo_dropout_weight.py b/bioacoustics/classifier/hyperparameter_optimization/hpo_dropout_weight.py index 6e9e94d..2bd7f67 100644 --- a/bioacoustics/classifier/hyperparameter_optimization/hpo_dropout_weight.py +++ b/bioacoustics/classifier/hyperparameter_optimization/hpo_dropout_weight.py @@ -1,4 +1,5 @@ """ Module that uses scikit-learn for grid search on the dropout rate """ + import tensorflow as tf from sklearn.model_selection import GridSearchCV from scikeras.wrappers import KerasClassifier @@ -62,6 +63,7 @@ def create_model(init_mode, dropout_rate, weight_constraint): return model + if __name__ == "__main__": parser = parse_arguments() args = parser.parse_args() @@ -125,4 +127,3 @@ def create_model(init_mode, dropout_rate, weight_constraint): # save model estimator = grid_result.best_estimator_ dump(estimator, args.output_dir + "best_estimator.joblib") - diff --git a/bioacoustics/classifier/hyperparameter_optimization/hpo_epoch_batch.py b/bioacoustics/classifier/hyperparameter_optimization/hpo_epoch_batch.py index ec96024..af47411 100644 --- a/bioacoustics/classifier/hyperparameter_optimization/hpo_epoch_batch.py +++ b/bioacoustics/classifier/hyperparameter_optimization/hpo_epoch_batch.py @@ -1,4 +1,5 @@ """ Module that uses scikit-learn for grid search on the dropout rate """ + import tensorflow as tf from sklearn.model_selection import GridSearchCV from scikeras.wrappers import KerasClassifier diff --git a/bioacoustics/classifier/hyperparameter_optimization/hpo_learing_rate.py b/bioacoustics/classifier/hyperparameter_optimization/hpo_learing_rate.py index 0434dc4..1351da8 100644 --- a/bioacoustics/classifier/hyperparameter_optimization/hpo_learing_rate.py +++ b/bioacoustics/classifier/hyperparameter_optimization/hpo_learing_rate.py @@ -1,4 +1,5 @@ """ Module that uses scikit-learn for grid search on the dropout rate """ + import tensorflow as tf from sklearn.model_selection import GridSearchCV from scikeras.wrappers import KerasClassifier diff --git a/bioacoustics/classifier/model/acoustic_model.py b/bioacoustics/classifier/model/acoustic_model.py index 78f28b2..44136cd 100755 --- a/bioacoustics/classifier/model/acoustic_model.py +++ b/bioacoustics/classifier/model/acoustic_model.py @@ -1,4 +1,5 @@ """Script of a base class for acoustic models""" + import os from abc import ABC import pickle @@ -28,13 +29,13 @@ def _compile(self, learning_rate): learning_rate: float Learning rate for adam optimizer """ - optimizer = keras.optimizers.Adam(learning_rate=learning_rate) #, decay=0.001 + optimizer = keras.optimizers.Adam(learning_rate=learning_rate) # , decay=0.001 # Compile the model self.acoustic_model.compile( - loss="binary_crossentropy", #"categorical_crossentropy" - metrics=['accuracy'], #Recall() - optimizer=optimizer + loss="binary_crossentropy", # "categorical_crossentropy" + metrics=["accuracy"], # Recall() + optimizer=optimizer, ) # Display model architecture summary @@ -151,7 +152,6 @@ def apply_model( self._predict(x_test) def predict_model(self, x_test, file_path, dl_model): - """Load a trained model and make a prediction Parameters @@ -203,17 +203,17 @@ def plot_measures(self, history, file_path, title=""): Title of the graph """ # summarize history for loss - plt.plot(history.history['loss']) - plt.plot(history.history['val_loss']) - plt.title('model loss') - plt.ylabel('loss') - plt.xlabel('epoch') - plt.legend(['train', 'val'], loc='upper left') - fp_loss = os.path.join(file_path, 'loss.png') + plt.plot(history.history["loss"]) + plt.plot(history.history["val_loss"]) + plt.title("model loss") + plt.ylabel("loss") + plt.xlabel("epoch") + plt.legend(["train", "val"], loc="upper left") + fp_loss = os.path.join(file_path, "loss.png") plt.savefig(fp_loss) # convert the history.history dict to a pandas DataFrame: hist_df = pd.DataFrame(history.history) hist_csv_file = os.path.join(file_path, "history.csv") - with open(hist_csv_file, mode='w') as f: + with open(hist_csv_file, mode="w") as f: hist_df.to_csv(f) diff --git a/bioacoustics/classifier/model/cnn10_model.py b/bioacoustics/classifier/model/cnn10_model.py index c085034..e95241a 100755 --- a/bioacoustics/classifier/model/cnn10_model.py +++ b/bioacoustics/classifier/model/cnn10_model.py @@ -1,4 +1,5 @@ """A class for acoustic model with 10 nn blocks""" + from acoustic_model import AcousticModel import tensorflow as tf @@ -59,7 +60,6 @@ def _make_cnn_model(self, init_mode, dropout_rate, weight_constraint): input_shape=input_shape, data_format=data_format, padding="same", - kernel_regularizer=regularizers.l2(l=0.01), kernel_initializer=init_mode, kernel_constraint=MaxNorm(weight_constraint), diff --git a/bioacoustics/feature_extraction/acoustic_features/features.py b/bioacoustics/feature_extraction/acoustic_features/features.py index 618c92a..07839d1 100644 --- a/bioacoustics/feature_extraction/acoustic_features/features.py +++ b/bioacoustics/feature_extraction/acoustic_features/features.py @@ -178,9 +178,9 @@ def _readFeaturesFunctions(self): featuresRefUnique[i] = str(i_feature) # -----> Then extend to all domains for i, domain in enumerate(self.domains): - self.featuresFunctions[ - i * self.n_features : (i + 1) * self.n_features - ] = featuresFunctionsUnique + self.featuresFunctions[i * self.n_features : (i + 1) * self.n_features] = ( + featuresFunctionsUnique + ) self.featuresOptArguments[ i * self.n_features : (i + 1) * self.n_features ] = featuresOptArgumentsUnique @@ -240,15 +240,15 @@ def _computation(self, signals, fs): new_dictionary.update( self.featuresOptArguments[i * self.n_features + j] ) - self.featuresValues[ - i * self.n_features + j - ] = self.featuresFunctions[i * self.n_features + j]( - signals[i], new_dictionary + self.featuresValues[i * self.n_features + j] = ( + self.featuresFunctions[i * self.n_features + j]( + signals[i], new_dictionary + ) ) # Otherwise directly compute feature value. else: - self.featuresValues[ - i * self.n_features + j - ] = self.featuresFunctions[i * self.n_features + j]( - signals[i], self.intermValues + self.featuresValues[i * self.n_features + j] = ( + self.featuresFunctions[i * self.n_features + j]( + signals[i], self.intermValues + ) ) diff --git a/bioacoustics/feature_extraction/acoustic_features/featuresFunctions.py b/bioacoustics/feature_extraction/acoustic_features/featuresFunctions.py index 11d443f..c27b3b2 100644 --- a/bioacoustics/feature_extraction/acoustic_features/featuresFunctions.py +++ b/bioacoustics/feature_extraction/acoustic_features/featuresFunctions.py @@ -241,9 +241,7 @@ def energy_kurtosis(signal, arg_dict): E_kur = 0 else: E_kur = ( - (1 / len(signal) / 2) - * np.sum((E_u / len(signal) - E_bar) ** 4) - / E_bar**4 + (1 / len(signal) / 2) * np.sum((E_u / len(signal) - E_bar) ** 4) / E_bar**4 ) if np.isfinite(E_kur): return E_kur diff --git a/bioacoustics/wav_processing/chunk_wav/make_chunks.py b/bioacoustics/wav_processing/chunk_wav/make_chunks.py index 484d3a7..d960f27 100755 --- a/bioacoustics/wav_processing/chunk_wav/make_chunks.py +++ b/bioacoustics/wav_processing/chunk_wav/make_chunks.py @@ -1,4 +1,5 @@ """Script to make .wav files of the same length.""" + import os import glob import argparse diff --git a/bioacoustics/wav_processing/condensation/extractor.py b/bioacoustics/wav_processing/condensation/extractor.py index 8bea461..183c43a 100644 --- a/bioacoustics/wav_processing/condensation/extractor.py +++ b/bioacoustics/wav_processing/condensation/extractor.py @@ -83,7 +83,7 @@ def detect_vocalizations( # get all indexes of dbs rows of every band that we're # interested in - for (low, high) in freqs: + for low, high in freqs: idx_low = (np.abs(f - low)).argmin() - 1 idx_low = 0 if idx_low < 0 else idx_low idx_high = (np.abs(f - high)).argmin() + 1