Skip to content

Commit c3e7d97

Browse files
committed
RF: copy input parameters on header creation
The input parameters are small, and it is easy to get confused when using the mutable input parameters elsewhere, so copy the input dict, array so each header has their own copy.
1 parent 539c1c1 commit c3e7d97

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

nibabel/parrec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ def __init__(self, info, image_defs, permit_truncated=False):
513513
If True, a warning is emitted instead of an error when a truncated
514514
recording is detected.
515515
"""
516-
self.general_info = info
517-
self.image_defs = image_defs
516+
self.general_info = info.copy()
517+
self.image_defs = image_defs.copy()
518518
_truncation_checks(info, image_defs, permit_truncated)
519519
# charge with basic properties to be able to use base class
520520
# functionality

nibabel/tests/test_parrec.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,10 @@ def test_orientation():
140140
hdr = PARRECHeader(HDR_INFO, HDR_DEFS)
141141
assert_array_equal(HDR_DEFS['slice orientation'], 1)
142142
assert_equal(hdr.get_slice_orientation(), 'transverse')
143-
hdr_defc = HDR_DEFS.copy()
144-
hdr = PARRECHeader(HDR_INFO, hdr_defc)
143+
hdr_defc = hdr.image_defs
145144
hdr_defc['slice orientation'] = 2
146145
assert_equal(hdr.get_slice_orientation(), 'sagittal')
147146
hdr_defc['slice orientation'] = 3
148-
hdr = PARRECHeader(HDR_INFO, hdr_defc)
149147
assert_equal(hdr.get_slice_orientation(), 'coronal')
150148

151149

@@ -357,3 +355,16 @@ def test__get_uniqe_image_defs():
357355
assert_raises(PARRECError, uip, 'image angulation')
358356
# This one differs from the outset
359357
assert_raises(PARRECError, uip, 'slice number')
358+
359+
360+
def test_copy_on_init():
361+
# Test that input dict / array gets copied when making header
362+
hdr = PARRECHeader(HDR_INFO, HDR_DEFS)
363+
assert_false(hdr.general_info is HDR_INFO)
364+
hdr.general_info['max_slices'] = 10
365+
assert_equal(hdr.general_info['max_slices'], 10)
366+
assert_equal(HDR_INFO['max_slices'], 9)
367+
assert_false(hdr.image_defs is HDR_DEFS)
368+
hdr.image_defs['image pixel size'] = 8
369+
assert_array_equal(hdr.image_defs['image pixel size'], 8)
370+
assert_array_equal(HDR_DEFS['image pixel size'], 16)

0 commit comments

Comments
 (0)