Skip to content

Commit c68b468

Browse files
committed
TEST: Unit test for read_annot/write_annot, that will be run even if
freesurfer is not present.
1 parent 53f7e4a commit c68b468

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

nibabel/freesurfer/tests/test_io.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,47 @@ def test_annot():
209209
assert_equal(names, names2)
210210

211211

212+
def test_annot_readback():
213+
214+
# This annot file will store a LUT for a mesh made of 10 vertices, with
215+
# 3 colours in the LUT.
216+
nvertices = 10
217+
nlabels = 3
218+
219+
names = ['label {}'.format(l) for l in range(1, nlabels + 1)]
220+
221+
# randomly generate a label for each vertex, making sure
222+
# that at least one of each label value is present. Label
223+
# values are in the range (0, nlabels-1) - they are used
224+
# as indices into the lookup table (generated below).
225+
labels = list(range(nlabels)) + \
226+
list(np.random.randint(0, nlabels, nvertices - nlabels))
227+
labels = np.array(labels, dtype=np.int32)
228+
np.random.shuffle(labels)
229+
230+
# Generate some random colours for the LUT
231+
rgbal = np.zeros((nlabels, 5), dtype=np.int32)
232+
rgbal[:, :4] = np.random.randint(0, 255, (nlabels, 4))
233+
234+
# But make sure we have at least one large alpha, to make sure that when
235+
# it is packed into a signed 32 bit int, it results in a negative value
236+
# for the annotation value.
237+
rgbal[0, 3] = 255
238+
239+
# Generate the annotation values for each LUT entry
240+
rgbal[:, 4] = (rgbal[:, 0] +
241+
rgbal[:, 1] * (2 ** 8) +
242+
rgbal[:, 2] * (2 ** 16) +
243+
rgbal[:, 3] * (2 ** 24))
244+
245+
annot_path = 'c.annot'
246+
247+
with InTemporaryDirectory():
248+
write_annot(annot_path, labels, rgbal, names)
249+
labels2, rgbal2, names2 = read_annot(annot_path)
250+
assert np.all(np.isclose(rgbal2, rgbal))
251+
252+
212253
@freesurfer_test
213254
def test_label():
214255
"""Test IO of .label"""

0 commit comments

Comments
 (0)