This file is indexed.

/usr/share/pyshared/mdp/test/test_caching.py is in python-mdp 3.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""Test caching extension."""
from __future__ import with_statement
import tempfile
from _tools import *

requires_joblib = skip_on_condition(
    "not mdp.config.has_joblib",
    "This test requires the 'joblib' module.")


_counter = 0
class _CounterNode(mdp.Node):
    def __init__(self):
        super(_CounterNode, self).__init__()

    def is_trainable(self):
        return False

    def _execute(self, x):
        """The execute method has the side effect of increasing
        a global counter by one."""
        global _counter
        _counter += 1
        return x

@requires_joblib
def test_caching_extension():
    """Test that the caching extension is working at the global level."""

    global _counter
    _counter = 0
    node = _CounterNode()

    # before decoration the global counter is incremented at every call
    k = 0
    for i in range(3):
        x = mdp.numx.array([[i]], dtype='d')
        for j in range(2):
            k += 1
            assert mdp.numx.all(node.execute(x) == x)
            assert _counter == k

    # reset counter
    _counter = 0
    # activate the extension
    cachedir = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
                                dir=py.test.mdp_tempdirname)
    mdp.caching.activate_caching(cachedir=cachedir)
    assert mdp.get_active_extensions() == ['cache_execute']

    # after decoration the global counter is incremented for each new 'x'
    for i in range(3):
        x = mdp.numx.array([[i]], dtype='d')
        for _ in range(2):
            assert mdp.numx.all(node.execute(x) == x)
            assert _counter == i + 1

    # after deactivation
    mdp.caching.deactivate_caching()
    assert mdp.get_active_extensions() == []
    # reset counter
    _counter = 0

    k = 0
    for i in range(3):
        x = mdp.numx.array([[i]], dtype='d')
        for j in range(2):
            k += 1
            assert mdp.numx.all(node.execute(x) == x)
            assert _counter == k

@requires_joblib
def test_different_instances_same_content():
    global _counter
    x = mdp.numx.array([[100.]], dtype='d')

    cachedir = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
                                dir=py.test.mdp_tempdirname)
    mdp.caching.activate_caching(cachedir=cachedir)
    node = _CounterNode()
    _counter = 0

    # add attribute to make instance unique
    node.attr = 'unique'

    # cache x
    node.execute(x)
    assert _counter == 1
    # should be cached now
    node.execute(x)
    assert _counter == 1

    # create new instance, make it also unique and check that
    # result is still cached
    _counter = 0
    node = _CounterNode()
    node.attr = 'unique and different'
    node.execute(x)
    assert _counter == 1

    mdp.caching.deactivate_caching()

@requires_joblib
def test_caching_context_manager():
    global _counter
    node = _CounterNode()
    _counter = 0

    assert mdp.get_active_extensions() == []
    cachedir = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
                                dir=py.test.mdp_tempdirname)
    with mdp.caching.cache(cachedir=cachedir):
        assert mdp.get_active_extensions() == ['cache_execute']

        for i in range(3):
            x = mdp.numx.array([[i]], dtype='d')
            for _ in range(2):
                assert mdp.numx.all(node.execute(x) == x)
                assert _counter == i + 1
    assert mdp.get_active_extensions() == []

@requires_joblib
def test_class_caching():
    """Test that we can cache individual classes."""
    cached = mdp.nodes.PCANode()
    notcached = mdp.nodes.SFANode()
    with mdp.caching.cache(cache_classes=[mdp.nodes.PCANode]):
        assert cached.is_cached()
        assert not notcached.is_cached()

@requires_joblib
def test_class_caching_functionality():
    """Test that cached classes really cache."""
    global _counter
    x = mdp.numx.array([[210]], dtype='d')

    node = _CounterNode()

    # here _CounterNode is not cached
    _counter = 0
    with mdp.caching.cache(cache_classes=[mdp.nodes.PCANode]):
        node.execute(x)
        assert _counter == 1
        node.execute(x)
        assert _counter == 2

    # here _CounterNode is cached
    _counter = 0
    with mdp.caching.cache(cache_classes=[_CounterNode]):
        node.execute(x)
        assert _counter == 1
        node.execute(x)
        assert _counter == 1

@requires_joblib
def test_instance_caching():
    """Test that we can cache individual instances."""
    cached = mdp.nodes.PCANode()
    notcached = mdp.nodes.PCANode()
    with mdp.caching.cache(cache_instances=[cached]):
        assert cached.is_cached()
        assert not notcached.is_cached()

@requires_joblib
def test_instance_caching_functionality():
    """Test that cached instances really cache."""
    global _counter
    x = mdp.numx.array([[130]], dtype='d')

    node = _CounterNode()
    othernode = _CounterNode()

    # here _CounterNode is not cached
    _counter = 0
    with mdp.caching.cache(cache_instances=[othernode]):
        node.execute(x)
        assert _counter == 1
        node.execute(x)
        assert _counter == 2

    # here _CounterNode is cached
    _counter = 0
    with mdp.caching.cache(cache_instances=[node]):
        node.execute(x)
        assert _counter == 1
        node.execute(x)
        assert _counter == 1

@requires_joblib
def test_preexecution_problem():
    """Test that automatic setting of e.g. input_dim does not stop
    the caching extension from caching on the first run."""
    global _counter
    x = mdp.numx.array([[102.]])

    node = _CounterNode()

    # here _CounterNode is cached
    _counter = 0
    with mdp.caching.cache():
        # on the first execution, input_dim and dtype are set ...
        node.execute(x)
        assert _counter == 1
        # ... yet the result is cached
        node.execute(x)
        assert _counter == 1

@requires_joblib
def test_switch_cache():
    """Test changing cache directory while extension is active."""
    global _counter

    dir1 = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
                            dir=py.test.mdp_tempdirname)
    dir2 = tempfile.mkdtemp(prefix='mdp-tmp-joblib-cache.',
                            dir=py.test.mdp_tempdirname)
    x = mdp.numx.array([[10]], dtype='d')

    mdp.caching.activate_caching(cachedir=dir1)

    node = _CounterNode()
    _counter = 0
    node.execute(x)
    assert _counter == 1
    node.execute(x)
    assert _counter == 1

    # now change path
    mdp.caching.set_cachedir(cachedir=dir2)
    node.execute(x)
    assert _counter == 2
    node.execute(x)
    assert _counter == 2

    mdp.caching.deactivate_caching()


@requires_joblib
def test_execute_magic():
    """Test calling execute with magic while caching."""
    x = mdp.numx_rand.rand(100, 10)

    node = mdp.nodes.PCANode()

    with mdp.caching.cache():
        y = node(x)
        y2 = node(x)
        assert_array_equal(y, y2)