From f290d5a8d8eb2e9e05eec90cc756045c872a7162 Mon Sep 17 00:00:00 2001 From: Si11ium Date: Tue, 19 May 2020 10:03:35 +0200 Subject: [PATCH] Save imports --- audio_toolset/audio_augmentation.py | 7 ++++- audio_toolset/audio_io.py | 13 ++++++-- evaluation/classification.py | 47 ++++++++++++++++++++++++++--- visualization/tools.py | 7 ++++- 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/audio_toolset/audio_augmentation.py b/audio_toolset/audio_augmentation.py index df8279c..244dd4d 100644 --- a/audio_toolset/audio_augmentation.py +++ b/audio_toolset/audio_augmentation.py @@ -1,4 +1,9 @@ -import librosa +try: + import librosa +except ImportError: # pragma: no-cover + raise ImportError('You want to use `librosa` plugins which are not installed yet,' # pragma: no-cover + ' install it with `pip install librosa`.') + import numpy as np diff --git a/audio_toolset/audio_io.py b/audio_toolset/audio_io.py index c34f4c9..696e3ed 100644 --- a/audio_toolset/audio_io.py +++ b/audio_toolset/audio_io.py @@ -1,5 +1,14 @@ -import librosa -from scipy.signal import butter, lfilter +try: + import librosa +except ImportError: # pragma: no-cover + raise ImportError('You want to use `librosa` plugins which are not installed yet,' # pragma: no-cover + ' install it with `pip install librosa`.') +try: + from scipy.signal import butter, lfilter +except ImportError: # pragma: no-cover + raise ImportError('You want to use `scikit` plugins which are not installed yet,' # pragma: no-cover + ' install it with `pip install scikit-learn`.') + import numpy as np diff --git a/evaluation/classification.py b/evaluation/classification.py index 695ee07..ab011ff 100644 --- a/evaluation/classification.py +++ b/evaluation/classification.py @@ -1,13 +1,21 @@ -import matplotlib.pyplot as plt -from sklearn.metrics import roc_curve, auc +try: + import matplotlib.pyplot as plt +except ImportError: # pragma: no-cover + raise ImportError('You want to use `matplotlib` plugins which are not installed yet,' # pragma: no-cover + ' install it with `pip install matplotlib`.') +try: + from sklearn.metrics import roc_curve, auc, recall_score +except ImportError: # pragma: no-cover + raise ImportError('You want to use `sklearn` plugins which are not installed yet,' # pragma: no-cover + ' install it with `pip install scikit-learn`.') class ROCEvaluation(object): linewidth = 2 - def __init__(self, plot_roc=False): - self.plot_roc = plot_roc + def __init__(self, plot=False): + self.plot = plot self.epoch = 0 def __call__(self, prediction, label): @@ -15,7 +23,7 @@ class ROCEvaluation(object): # Compute ROC curve and ROC area fpr, tpr, _ = roc_curve(prediction, label) roc_auc = auc(fpr, tpr) - if self.plot_roc: + if self.plot: _ = plt.gcf() plt.plot(fpr, tpr, color='darkorange', lw=self.linewidth, label=f'ROC curve (area = {roc_auc})') self._prepare_fig() @@ -32,3 +40,32 @@ class ROCEvaluation(object): fig.legend(loc="lower right") return fig + + +class UAREvaluation(object): + + def __init__(self, labels: list, plot=False): + self.labels = labels + self.plot_roc = plot + self.epoch = 0 + + def __call__(self, prediction, label): + + # Compute uar score - UnweightedAverageRecal + + uar_score = recall_score(label, prediction, labels=self.labels, average='macro', + sample_weight=None, zero_division='warn') + return uar_score + + def _prepare_fig(self): + raise NotImplementedError # TODO Implement a nice visualization + fig = plt.gcf() + ax = plt.gca() + plt.plot([0, 1], [0, 1], color='navy', lw=self.linewidth, linestyle='--') + plt.xlim([0.0, 1.0]) + plt.ylim([0.0, 1.05]) + plt.xlabel('False Positive Rate') + plt.ylabel('True Positive Rate') + fig.legend(loc="lower right") + + return fig \ No newline at end of file diff --git a/visualization/tools.py b/visualization/tools.py index 32dd67c..1110f50 100644 --- a/visualization/tools.py +++ b/visualization/tools.py @@ -1,5 +1,10 @@ +try: + import matplotlib.pyplot as plt +except ImportError: # pragma: no-cover + raise ImportError('You want to use `matplotlib` plugins which are not installed yet,' # pragma: no-cover + ' install it with `pip install matplotlib`.') + from pathlib import Path -import matplotlib.pyplot as plt class Plotter(object):