Skip to content

Commit 1566d9b

Browse files
committed
TST: Test dimension, size, and fnum constraints
Squeeze array to simplify dimension test
1 parent d684b01 commit 1566d9b

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

nibabel/freesurfer/io.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,22 @@ def write_morph_data(file_like, values, fnum=0):
184184
"""
185185
magic_bytes = np.array([255, 255, 255], dtype=np.uint8)
186186

187+
array = np.asarray(values).astype('>f4').squeeze()
188+
if len(array.shape) > 1:
189+
raise ValueError("Multi-dimensional values not supported")
190+
187191
i4info = np.iinfo('i4')
188-
if len(values) > i4info.max:
192+
if len(array) > i4info.max:
189193
raise ValueError("Too many values for morphometry file")
190194
if not i4info.min <= fnum <= i4info.max:
191195
raise ValueError("Argument fnum must be between {0} and {1}".format(
192196
i4info.min, i4info.max))
193197

194-
array = np.asarray(values).astype('>f4')
195-
if len(array.shape) > 1:
196-
raise ValueError("Multi-dimensional values not supported")
197-
198198
with Opener(file_like, 'wb') as fobj:
199199
fobj.write(magic_bytes)
200200

201201
# vertex count, face count (unused), vals per vertex (only 1 supported)
202-
fobj.write(np.array([len(values), fnum, 1], dtype='>i4'))
202+
fobj.write(np.array([len(array), fnum, 1], dtype='>i4'))
203203

204204
fobj.write(array)
205205

nibabel/freesurfer/tests/test_io.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
from nose.tools import assert_true
1212
import numpy as np
13-
from numpy.testing import assert_equal, dec
13+
from numpy.testing import assert_equal, assert_raises, dec
1414

1515
from .. import (read_geometry, read_morph_data, read_annot, read_label,
1616
write_geometry, write_morph_data, write_annot)
1717

1818
from ...tests.nibabel_data import get_nibabel_data
19+
from ...fileslice import strided_scalar
1920

2021

2122
DATA_SDIR = 'fsaverage'
@@ -99,6 +100,25 @@ def test_morph_data():
99100
assert_equal(curv2, curv)
100101

101102

103+
def test_write_morph_data():
104+
"""Test write_morph_data edge cases"""
105+
values = np.arange(20, dtype='>f4')
106+
okay_shapes = [(20,), (20, 1), (20, 1, 1), (1, 20)]
107+
bad_shape = (10, 2)
108+
big_num = np.iinfo('i4').max + 1
109+
with InTemporaryDirectory():
110+
for shape in okay_shapes:
111+
write_morph_data('test.curv', values.reshape(shape))
112+
# Check ordering is preserved, regardless of shape
113+
assert_equal(values, read_morph_data('test.curv'))
114+
assert_raises(ValueError, write_morph_data, 'test.curv',
115+
np.zeros(shape), big_num)
116+
assert_raises(ValueError, write_morph_data, 'test.curv',
117+
values.reshape(bad_shape))
118+
assert_raises(ValueError, write_morph_data, 'test.curv',
119+
strided_scalar((big_num,)))
120+
121+
102122
@freesurfer_test
103123
def test_annot():
104124
"""Test IO of .annot"""

0 commit comments

Comments
 (0)