Skip to content

Commit 7b00bd4

Browse files
committed
strided_scalar to return read-only array
Update tests to verify read-only, instead of checking values
1 parent e2873a0 commit 7b00bd4

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

nibabel/fileslice.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,9 +767,12 @@ def strided_scalar(shape, scalar=0.):
767767
strided_arr : array
768768
Array of shape `shape` for which all values == `scalar`, built by
769769
setting all strides of `strided_arr` to 0, so the scalar is broadcast
770-
out to the full array `shape`.
770+
out to the full array `shape`. `strided_arr` is flagged as not
771+
`writeable`.
771772
"""
772773
shape = tuple(shape)
773774
scalar = np.array(scalar)
774775
strides = [0] * len(shape)
775-
return np.lib.stride_tricks.as_strided(scalar, shape, strides)
776+
strided_scalar = np.lib.stride_tricks.as_strided(scalar, shape, strides)
777+
strided_scalar.flags.writeable = False
778+
return strided_scalar

nibabel/tests/test_fileslice.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,9 @@ def test_strided_scalar():
639639
assert_equal(observed.shape, shape)
640640
assert_equal(observed.dtype, expected.dtype)
641641
assert_array_equal(observed.strides, 0)
642-
observed[..., 0] = 99
643-
assert_array_equal(observed, expected * 0 + 99)
642+
def setval(x):
643+
x[..., 0] = 99
644+
assert_raises(ValueError, setval, observed)
644645
# Default scalar value is 0
645646
assert_array_equal(strided_scalar((2, 3, 4)), np.zeros((2, 3, 4)))
646647

0 commit comments

Comments
 (0)