Skip to content

ENH: Add bytestring serialization to CIFTI-2 #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all 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: 9 additions & 2 deletions nibabel/cifti2/cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
from collections.abc import MutableSequence, MutableMapping, Iterable
from collections import OrderedDict
from .. import xmlutils as xml
from ..filebasedimages import FileBasedHeader
from ..filebasedimages import FileBasedHeader, SerializableImage
from ..dataobj_images import DataobjImage
from ..nifti1 import Nifti1Extensions
from ..nifti2 import Nifti2Image, Nifti2Header
from ..arrayproxy import reshape_dataobj
from warnings import warn
Expand Down Expand Up @@ -1255,6 +1256,9 @@ def _to_xml_element(self):
cifti.append(mat_xml)
return cifti

def __eq__(self, other):
return self.to_xml() == other.to_xml()

@classmethod
def may_contain_header(klass, binaryblock):
from .parse_cifti2 import _Cifti2AsNiftiHeader
Expand Down Expand Up @@ -1326,7 +1330,7 @@ def from_axes(cls, axes):
return cifti2_axes.to_header(axes)


class Cifti2Image(DataobjImage):
class Cifti2Image(DataobjImage, SerializableImage):
""" Class for single file CIFTI-2 format image
"""
header_class = Cifti2Header
Expand Down Expand Up @@ -1454,6 +1458,9 @@ def to_file_map(self, file_map=None):
self.update_headers()
header = self._nifti_header
extension = Cifti2Extension(content=self.header.to_xml())
header.extensions = Nifti1Extensions(
ext for ext in header.extensions if not isinstance(ext, Cifti2Extension)
)
header.extensions.append(extension)
if self._dataobj.shape != self.header.matrix.get_data_shape():
raise ValueError(
Expand Down
3 changes: 2 additions & 1 deletion nibabel/cifti2/tests/test_cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pytest

from nibabel.tests.test_dataobj_images import TestDataobjAPI as _TDA
from nibabel.tests.test_image_api import SerializeMixin


def compare_xml_leaf(str1, str2):
Expand Down Expand Up @@ -406,7 +407,7 @@ def test_underscoring():
assert ci.cifti2._underscore(camel) == underscored


class TestCifti2ImageAPI(_TDA):
class TestCifti2ImageAPI(_TDA, SerializeMixin):
""" Basic validation for Cifti2Image instances
"""
# A callable returning an image from ``image_maker(data, header)``
Expand Down