Skip to content

Commit

Permalink
Merge pull request #1336 from effigies/rf/nifti_extensions
Browse files Browse the repository at this point in the history
ENH: Add `.content`, `.text` and `.json` properties to NIfTI extensions
  • Loading branch information
effigies authored Sep 23, 2024
2 parents f51dced + 398488e commit c742552
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 120 deletions.
2 changes: 1 addition & 1 deletion nibabel/cifti2/cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ def to_file_map(self, file_map=None, dtype=None):

self.update_headers()
header = self._nifti_header
extension = Cifti2Extension(content=self.header.to_xml())
extension = Cifti2Extension.from_bytes(self.header.to_xml())
header.extensions = Nifti1Extensions(
ext for ext in header.extensions if not isinstance(ext, Cifti2Extension)
)
Expand Down
12 changes: 4 additions & 8 deletions nibabel/cifti2/parse_cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@
)


class Cifti2Extension(Nifti1Extension):
class Cifti2Extension(Nifti1Extension[Cifti2Header]):
code = 32

def __init__(self, code=None, content=None):
Nifti1Extension.__init__(self, code=code or self.code, content=content)

def _unmangle(self, value):
def _unmangle(self, value: bytes) -> Cifti2Header:
parser = Cifti2Parser()
parser.parse(string=value)
self._content = parser.header
return self._content
return parser.header

def _mangle(self, value):
def _mangle(self, value: Cifti2Header) -> bytes:
if not isinstance(value, Cifti2Header):
raise ValueError('Can only mangle a Cifti2Header.')
return value.to_xml()
Expand Down
Loading

0 comments on commit c742552

Please sign in to comment.