Skip to content

Commit b5b6550

Browse files
committed
Zeropad Extension to 16 bytes, test writing
Also, fix _guess_implicit_VR method.
1 parent dbe3946 commit b5b6550

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

nibabel/nifti1.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class Nifti1DicomExtension(Nifti1Extension):
390390
def __init__(self, code, content):
391391
self._code = code
392392
self._raw_content = content
393-
self._guess_implicit_VR()
393+
self._is_implicit_VR = self._guess_implicit_VR()
394394
self._content = self._unmangle(content)
395395

396396
super(Nifti1DicomExtension,self)
@@ -400,12 +400,12 @@ def _guess_implicit_VR(self):
400400
Representations (VRs) are included in the DICOM encoding or not.
401401
This reads where the first VR would be and checks it against a list of
402402
valid VRs"""
403-
potential_vr = self._raw_content[8:12]
403+
potential_vr = self._raw_content[4:6].decode()
404404
if potential_vr in dicom_converters.keys():
405-
self._is_implicit_VR=False
405+
implicit_VR=False
406406
else:
407-
self._is_implicit_VR=True
408-
return self._is_implicit_VR
407+
implicit_VR=True
408+
return implicit_VR
409409

410410
def _is_little_endian(self):
411411
return True
@@ -415,19 +415,24 @@ def _unmangle(self,value):
415415
ds=read_dataset(raw_io,self._is_implicit_VR,self._is_little_endian)
416416
return ds
417417

418-
def _mangle(self):
418+
def _mangle(self, value):
419419
raw_io=BytesIO()
420420
dio=DicomFileLike(raw_io)
421421
dio.is_implicit_VR = self._is_implicit_VR
422422
dio.is_little_endian = self._is_little_endian
423-
ds_len=write_dataset(dio,self._content)
423+
ds_len=write_dataset(dio,value)
424424
dio.seek(0)
425425
return dio.read(ds_len)
426426

427427
def get_sizeondisk(self):
428428
"""Return the size of the extension in the NIfTI file.
429429
"""
430-
return len(self._mangle())
430+
size = len(self._mangle(self._content))
431+
size += 8
432+
# extensions size has to be a multiple of 16 bytes
433+
size += 16 - (size % 16)
434+
435+
return size
431436

432437
try:
433438
from dicom.filereader import read_dataset

nibabel/tests/test_nifti1.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
try:
4545
import dicom
4646
import struct
47+
from io import BytesIO
4748
except ImportError:
4849
have_dicom = False
4950
else:
@@ -1087,29 +1088,37 @@ def test_nifti_dicom_extension():
10871088
exts_container = hdr.extensions
10881089

10891090
# create a single dicom tag (Patien ID, [0010,0020]) with Explicit VR
1090-
dcmbytes = struct.pack('<HH2sH4s',0x10,0x20,
1091+
dcmbytes_explicit = struct.pack('<HH2sH4s',0x10,0x20,
10911092
'LO'.encode('utf-8'),4,'NiPy'.encode('utf-8'))
1092-
dcmext = Nifti1DicomExtension(2,dcmbytes)
1093+
dcmext = Nifti1DicomExtension(2,dcmbytes_explicit)
10931094
assert_equal(dcmext.__class__, Nifti1DicomExtension)
1095+
assert_equal(dcmext._guess_implicit_VR(),False)
1096+
assert_equal(dcmext.get_code(),2)
10941097
assert_equal(dcmext.get_content().PatientID, 'NiPy')
10951098
assert_equal(len(dcmext.get_content().values()), 1)
1096-
1099+
assert_equal(dcmext._mangle(dcmext.get_content()),dcmbytes_explicit)
1100+
assert_equal(dcmext.get_sizeondisk() % 16, 0)
1101+
10971102
# create a single dicom tag (Patien ID, [0010,0020]) with Implicit VR
1098-
dcmbytes = struct.pack('<HHL4s',0x10,0x20,4,'NiPy'.encode('utf-8'))
1099-
dcmext = Nifti1DicomExtension(2,dcmbytes)
1103+
dcmbytes_implicit = struct.pack('<HHL4s',0x10,0x20,4,'NiPy'.encode('utf-8'))
1104+
dcmext = Nifti1DicomExtension(2,dcmbytes_implicit)
1105+
assert_equal(dcmext._guess_implicit_VR(),True)
1106+
assert_equal(dcmext.get_code(),2)
11001107
assert_equal(dcmext.get_content().PatientID, 'NiPy')
11011108
assert_equal(len(dcmext.get_content().values()), 1)
1102-
assert_equal(dcmext.get_sizeondisk(), len(dcmbytes))
1103-
assert_equal(dcmext._mangle(),dcmbytes)
1109+
assert_equal(dcmext._mangle(dcmext.get_content()),dcmbytes_implicit)
1110+
assert_equal(dcmext.get_sizeondisk() % 16, 0)
11041111

11051112
# dicom extension access from nifti extensions
11061113
assert_equal(exts_container.count('dicom'),0)
11071114
exts_container.append(dcmext)
11081115
assert_equal(exts_container.count('dicom'), 1)
11091116
assert_equal(exts_container.get_codes(), [6, 6, 2])
1110-
assert_equal(dcmext.get_sizeondisk(), len(dcmbytes))
1111-
assert_equal(dcmext._mangle(),dcmbytes)
1112-
# assert_equal((exts_container.get_sizeondisk()) % 16, 0)
1117+
assert_equal(dcmext._mangle(dcmext.get_content()),dcmbytes_implicit)
1118+
assert_equal(dcmext.get_sizeondisk() % 16, 0)
1119+
1120+
raw_io = BytesIO()
1121+
dcmext.write_to(raw_io,False)
11131122

11141123
class TestNifti1General(object):
11151124
""" Test class to test nifti1 in general

0 commit comments

Comments
 (0)