Skip to content

Commit 932f237

Browse files
committed
NF: add in_memory property
`in_memory` property is True if image has an array as its data object, or if a proxy image has been cached.
1 parent 9876563 commit 932f237

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

nibabel/spatialimages.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,13 @@ def get_data(self):
464464
self._data_cache = np.asanyarray(self._dataobj)
465465
return self._data_cache
466466

467+
@property
468+
def in_memory(self):
469+
""" True when array data is in memory
470+
"""
471+
return (isinstance(self._dataobj, np.ndarray)
472+
or self._data_cache is not None)
473+
467474
def uncache(self):
468475
""" Delete any cached read of data from proxied data
469476

nibabel/tests/test_image_api.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
array creation. If it does, this call empties that cache. Implement this
1818
as a no-op if ``get_data()`` does not cache.
1919
* ``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.
2022
"""
2123
from __future__ import division, print_function, absolute_import
2224

@@ -172,7 +174,11 @@ def validate_data(self, imaker, params):
172174
assert_false(isinstance(img.dataobj, np.ndarray))
173175
proxy_data = np.asarray(img.dataobj)
174176
proxy_copy = proxy_data.copy()
177+
# Not yet cached, proxy image: in_memory is False
178+
assert_false(img.in_memory)
175179
data = img.get_data()
180+
# Data now cached
181+
assert_true(img.in_memory)
176182
assert_false(proxy_data is data)
177183
# changing array data does not change proxy data, or reloaded data
178184
data[:] = 42
@@ -182,9 +188,12 @@ def validate_data(self, imaker, params):
182188
assert_array_equal(img.get_data(), 42)
183189
# until we uncache
184190
img.uncache()
191+
# Which unsets in_memory
192+
assert_false(img.in_memory)
185193
assert_array_equal(img.get_data(), proxy_copy)
186194
else: # not proxy
187195
assert_true(isinstance(img.dataobj, np.ndarray))
196+
assert_true(img.in_memory)
188197
non_proxy_data = np.asarray(img.dataobj)
189198
data = img.get_data()
190199
assert_true(non_proxy_data is data)
@@ -196,9 +205,11 @@ def validate_data(self, imaker, params):
196205
# Unache has no effect
197206
img.uncache()
198207
assert_array_equal(img.get_data(), 42)
199-
# Read only
208+
# dataobj is read only
200209
fake_data = np.zeros(img.shape).astype(img.get_data_dtype())
201210
assert_raises(AttributeError, setattr, img, 'dataobj', fake_data)
211+
# So is in_memory
212+
assert_raises(AttributeError, setattr, img, 'in_memory', False)
202213

203214
def validate_data_deprecated(self, imaker, params):
204215
# Check _data property still exists, but raises warning

0 commit comments

Comments
 (0)