17
17
array creation. If it does, this call empties that cache. Implement this
18
18
as a no-op if ``get_data()`` does not cache.
19
19
* ``img[something]`` generates an informative TypeError
20
+ * ``img.in_memory`` is True for an array image, and for a proxy image that is
21
+ cached, but False otherwise.
20
22
"""
21
23
from __future__ import division , print_function , absolute_import
22
24
23
25
import warnings
26
+ from functools import partial
24
27
25
28
import numpy as np
26
29
@@ -172,8 +175,23 @@ def validate_data(self, imaker, params):
172
175
assert_false (isinstance (img .dataobj , np .ndarray ))
173
176
proxy_data = np .asarray (img .dataobj )
174
177
proxy_copy = proxy_data .copy ()
178
+ # Not yet cached, proxy image: in_memory is False
179
+ assert_false (img .in_memory )
180
+ # Load with caching='unchanged'
181
+ data = img .get_data (caching = 'unchanged' )
182
+ # Still not cached
183
+ assert_false (img .in_memory )
184
+ # Default load, does caching
175
185
data = img .get_data ()
186
+ # Data now cached
187
+ assert_true (img .in_memory )
176
188
assert_false (proxy_data is data )
189
+ # Now caching='unchanged' does nothing, returns cached version
190
+ data_again = img .get_data (caching = 'unchanged' )
191
+ assert_true (data is data_again )
192
+ # caching='fill' does nothing because the cache is already full
193
+ data_yet_again = img .get_data (caching = 'fill' )
194
+ assert_true (data is data_yet_again )
177
195
# changing array data does not change proxy data, or reloaded data
178
196
data [:] = 42
179
197
assert_array_equal (proxy_data , proxy_copy )
@@ -182,23 +200,41 @@ def validate_data(self, imaker, params):
182
200
assert_array_equal (img .get_data (), 42 )
183
201
# until we uncache
184
202
img .uncache ()
203
+ # Which unsets in_memory
204
+ assert_false (img .in_memory )
185
205
assert_array_equal (img .get_data (), proxy_copy )
206
+ # Check caching='fill' does cache data
207
+ img = imaker ()
208
+ assert_false (img .in_memory )
209
+ data = img .get_data (caching = 'fill' )
210
+ assert_true (img .in_memory )
211
+ data_again = img .get_data ()
212
+ assert_true (data is data_again )
186
213
else : # not proxy
187
- assert_true (isinstance (img .dataobj , np .ndarray ))
188
- non_proxy_data = np .asarray (img .dataobj )
189
- data = img .get_data ()
190
- assert_true (non_proxy_data is data )
191
- # changing array data does change proxy data, and reloaded data
192
- data [:] = 42
193
- assert_array_equal (np .asarray (img .dataobj ), 42 )
194
- # It does change the result of get_data
195
- assert_array_equal (img .get_data (), 42 )
196
- # Unache has no effect
197
- img .uncache ()
198
- assert_array_equal (img .get_data (), 42 )
199
- # Read only
214
+ for caching in (None , 'fill' , 'unchanged' ):
215
+ img = imaker ()
216
+ get_data_func = (img .get_data if caching is None else
217
+ partial (img .get_data , caching = caching ))
218
+ assert_true (isinstance (img .dataobj , np .ndarray ))
219
+ assert_true (img .in_memory )
220
+ data = get_data_func ()
221
+ assert_true (data is img .dataobj )
222
+ # changing array data does change proxy data, and reloaded data
223
+ data [:] = 42
224
+ assert_array_equal (np .asarray (img .dataobj ), 42 )
225
+ # It does change the result of get_data
226
+ assert_array_equal (get_data_func (), 42 )
227
+ # Unache has no effect
228
+ img .uncache ()
229
+ assert_array_equal (get_data_func (), 42 )
230
+ assert_true (img .in_memory )
231
+ # dataobj is read only
200
232
fake_data = np .zeros (img .shape ).astype (img .get_data_dtype ())
201
233
assert_raises (AttributeError , setattr , img , 'dataobj' , fake_data )
234
+ # So is in_memory
235
+ assert_raises (AttributeError , setattr , img , 'in_memory' , False )
236
+ # Values to get_data caching parameter must be 'fill' or 'unchanged'
237
+ assert_raises (ValueError , img .get_data , caching = 'something' )
202
238
203
239
def validate_data_deprecated (self , imaker , params ):
204
240
# Check _data property still exists, but raises warning
0 commit comments