diff --git a/madmom/audio/cepstrogram.py b/madmom/audio/cepstrogram.py index ad9fbcc68..c8cfc0142 100644 --- a/madmom/audio/cepstrogram.py +++ b/madmom/audio/cepstrogram.py @@ -19,6 +19,7 @@ from .filters import MelFilterbank from .spectrogram import Spectrogram from ..processors import Processor +from ..utils import lazyprop class Cepstrogram(np.ndarray): @@ -37,7 +38,6 @@ class applies some transformation (usually a DCT) on a spectrogram. one is instantiated with these additional keyword arguments. """ - # pylint: disable=super-on-old-class # pylint: disable=super-init-not-called # pylint: disable=attribute-defined-outside-init @@ -130,19 +130,6 @@ def process(self, data, **kwargs): MFCC_DELTADELTA_FILTER = np.linspace(1, -1, 3) / 2 -# https://stackoverflow.com/questions/3012421/python-memoising-deferred-lookup-property-decorator#3013910 -def lazyprop(fn): - attr_name = '_lazy_' + fn.__name__ - - @property - def _lazyprop(self): - if not hasattr(self, attr_name): - setattr(self, attr_name, fn(self)) - return getattr(self, attr_name) - - return _lazyprop - - class MFCC(Cepstrogram): """ MFCC class. diff --git a/madmom/utils/__init__.py b/madmom/utils/__init__.py index 4e68c9678..2ee17d742 100644 --- a/madmom/utils/__init__.py +++ b/madmom/utils/__init__.py @@ -591,6 +591,35 @@ def segment_axis(signal, frame_size, hop_size, axis=None, end='cut', dtype=signal.dtype) +# taken from: https://stackoverflow.com/questions/3012421/ +def lazyprop(fn): + """ + A decorator for a caching, lazily evaluated property. If a function is + decorated with @lazyprop, the original function of the resulting property + is only called on the first access. Afterwards the result which was + produced then is returned again. + + Parameters + ---------- + fn: Function + A function without argument which returns the value of the property + + Returns + ------- + property + A property which wraps the original one and caches it first result + """ + attr_name = '_lazy_' + fn.__name__ + + @property + def _lazyprop(self): + if not hasattr(self, attr_name): + setattr(self, attr_name, fn(self)) + return getattr(self, attr_name) + + return _lazyprop + + # keep namespace clean del contextlib