File: cache.rst

package info (click to toggle)
python-librosa 0.11.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 166,732 kB
  • sloc: python: 21,731; makefile: 141; sh: 2
file content (130 lines) | stat: -rw-r--r-- 4,219 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
Caching
^^^^^^^

This section covers the *librosa* function cache.  This allows you
to store and reuse intermediate computations across sessions.

Enabling the cache
------------------
By default, caching is disabled.  To enable caching, the environment 
variable `LIBROSA_CACHE_DIR` must be set prior to loading *librosa*.
This can be done on the command line prior to instantiating a python interpreter::

    $ export LIBROSA_CACHE_DIR=/tmp/librosa_cache
    $ ipython

or from within python, prior to importing *librosa*::

    >>> import os
    >>> os.environ['LIBROSA_CACHE_DIR'] = '/tmp/librosa_cache'
    >>> import librosa

.. warning::
    The cache does not implement any eviction policy.  As such, 
    it can grow without bound on disk if not purged.
    To purge the cache directly, call::

        >>> librosa.cache.clear()



Cache configuration
-------------------
The cache is implemented on top of `joblib.Memory`.
The default configuration can be overridden by setting the following environment variables

  - `LIBROSA_CACHE_DIR` : path (on disk) to the cache directory
  - `LIBROSA_CACHE_MMAP` : optional memory mapping mode `{None, 'r+', 'r', 'w+', 'c'}`
  - `LIBROSA_CACHE_COMPRESS` : flag to enable compression of data on disk `{0, 1}`
  - `LIBROSA_CACHE_VERBOSE` : controls how much debug info is displayed. `{int, non-negative}`
  - `LIBROSA_CACHE_LEVEL` : controls the caching level: the larger this value, the more data is cached. `{int}`

Please refer to the `joblib.Memory` documentation for a detailed explanation of these parameters.

As of 0.7, librosa's cache wraps (rather than extends) the `joblib.Memory` object.
The memory object can be directly accessed by `librosa.cache.memory`.


Cache levels
------------

Cache levels operate in a fashion similar to logging levels.
For small values of `LIBROSA_CACHE_LEVEL`, only the most important (frequently used) data are cached.
As the cache level increases, broader classes of functions are cached.
As a result, application code may run faster at the expense of larger disk usage.

The caching levels are described as follows:

    - 10: filter bases, independent of audio data (mel, chroma, constant-q)
    - 20: low-level features (cqt, stft, zero-crossings, etc)
    - 30: high-level features (tempo, beats, decomposition, recurrence, etc)
    - 40: post-processing (delta, stack_memory, normalize, sync)

The default cache level is 10.


Example
-------
To demonstrate how to use the cache, we'll first call an example script twice without caching::

    $ time -p ./estimate_tuning.py Kevin_MacLeod_-_Vibe_Ace.ogg 
    Loading  Kevin_MacLeod_-_Vibe_Ace.ogg
    Separating harmonic component ... 
    Estimating tuning ... 
    +9.00 cents
    real 6.74
    user 6.03
    sys 1.09

    $ time -p ./estimate_tuning.py Kevin_MacLeod_-_Vibe_Ace.ogg 
    Loading  Kevin_MacLeod_-_Vibe_Ace.ogg
    Separating harmonic component ... 
    Estimating tuning ... 
    +9.00 cents
    real 6.68
    user 6.04
    sys 1.05


Next, we'll enable caching to `/tmp/librosa`::

    $ export LIBROSA_CACHE_DIR=/tmp/librosa

and set the cache level to 50::

    $ export LIBROSA_CACHE_LEVEL=50

And now we'll re-run the example script twice.  The first time, there will be no cached values, so the time
should be similar to running without cache.  The second time, we'll be able to reuse intermediate values, so
it should be significantly faster.::

    $ time -p ./estimate_tuning.py Kevin_MacLeod_-_Vibe_Ace.ogg 
    Loading  Kevin_MacLeod_-_Vibe_Ace.ogg
    Separating harmonic component ... 
    Estimating tuning ... 
    +9.00 cents
    real 7.60
    user 6.79
    sys 1.15

    $ time -p ./estimate_tuning.py Kevin_MacLeod_-_Vibe_Ace.ogg 
    Loading  Kevin_MacLeod_-_Vibe_Ace.ogg
    Separating harmonic component ... 
    Estimating tuning ... 
    +9.00 cents
    real 1.64
    user 1.30
    sys 0.74

Reducing the cache level to 20 yields an intermediate acceleration::

    $ export LIBROSA_CACHE_LEVEL=20

    $ time -p ./estimate_tuning.py Kevin_MacLeod_-_Vibe_Ace.ogg 
    Loading  Kevin_MacLeod_-_Vibe_Ace.ogg
    Separating harmonic component ... 
    Estimating tuning ... 
    +9.00 cents
    real 4.98
    user 4.17
    sys 1.22