Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions nibabel/arrayproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ def __getitem__(self, slicer):
# Upcast as necessary for big slopes, intercepts
return apply_read_scaling(raw_data, self._slope, self._inter)

def reshape(self, shape):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One-liner docstring?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to allow -1 as element in shape, for compatibility with numpy array reshape.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

size = np.prod(self._shape)
if np.prod(shape) != size:
raise ValueError("cannot reshape array of size {:d} into shape "
"{!s}".format(size, shape))
new_ap = ArrayProxy(file_like=self.file_like,
header=self._header,
mmap=self._mmap)
new_ap._shape = shape
return new_ap


def is_proxy(obj):
""" Return True if `obj` is an array proxy
Expand Down
1 change: 0 additions & 1 deletion nibabel/cifti2/parse_cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ def __init__(self, dataobj, affine, header=None,
if self.cifti_img is None:
raise ValueError('Nifti2 header does not contain a CIFTI2 '
'extension')
self.cifti_img.data = self.get_data()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or could be self.cifti_img.dataobj = self.dataobj. This doesn't load the data, but keeps it as an array proxy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the cifti_img attribute is doing any work at all. Even the check on the header is replicated here. I think it may be a refactoring hold-over that never got cleaned up. I'm pretty sure we could, without losing functionality, rewrite the entire class as:

class _Cifti2AsNiftiImage(Nifti2Image):
    header_class = _Cifti2AsNiftiHeader
    makeable = False



class Cifti2Parser(xml.XmlParser):
Expand Down
6 changes: 3 additions & 3 deletions nibabel/cifti2/tests/test_cifti2io.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def test_read_and_proxies():
assert_true(isinstance(img2.header, ci.Cifti2Header))
assert_equal(img2.shape, (1, 91282))
# While we cannot reshape arrayproxies, all images are in-memory
assert_true(img2.in_memory)
assert_true(not img2.in_memory)
data = img2.get_data()
assert_true(data is img2.dataobj)
assert_true(data is not img2.dataobj)
# Uncaching has no effect, images are always array images
img2.uncache()
assert_true(data is img2.get_data())
assert_true(data is not img2.get_data())


@needs_nibabel_data('nitest-cifti2')
Expand Down