Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
85ac452
changed nosetools assert_true to assert in test_io.py
orduek Feb 4, 2020
2bc5308
changed nosetools assertion to pytest in test_mghformat.py
orduek Feb 4, 2020
dba6f59
changed from numpy raises to pytest.raises and skipif
orduek Feb 4, 2020
406e6dc
Changed test_mghformat.py to pytest
orduek Feb 4, 2020
e1bb16d
small fixes
orduek Feb 4, 2020
8d92977
fixes to assert numpy
orduek Feb 5, 2020
ae39e06
changed nosetools assert_true to assert in test_io.py
orduek Feb 4, 2020
665b28a
changed nosetools assertion to pytest in test_mghformat.py
orduek Feb 4, 2020
441e4fe
changed from numpy raises to pytest.raises and skipif
orduek Feb 4, 2020
f99d926
Changed test_mghformat.py to pytest
orduek Feb 4, 2020
6e1e1c1
small fixes
orduek Feb 4, 2020
d27b124
fixes to assert numpy
orduek Feb 5, 2020
6890a62
Update nibabel/freesurfer/tests/test_io.py
orduek Feb 5, 2020
b7e8abb
Update nibabel/freesurfer/tests/test_io.py
orduek Feb 5, 2020
5dbe408
Update nibabel/freesurfer/tests/test_io.py
orduek Feb 5, 2020
24d96e4
Update nibabel/freesurfer/tests/test_io.py
orduek Feb 5, 2020
e8965c1
Update nibabel/freesurfer/tests/test_io.py
orduek Feb 5, 2020
bbc36d7
Update nibabel/freesurfer/tests/test_io.py
orduek Feb 5, 2020
e626a38
Update nibabel/freesurfer/tests/test_io.py
orduek Feb 5, 2020
2fb977b
Update nibabel/freesurfer/tests/test_io.py
orduek Feb 5, 2020
30a4639
added test_io to Ignore list in azure and travis
orduek Feb 5, 2020
1987f79
changes
orduek Feb 5, 2020
165da32
Update nibabel/freesurfer/tests/test_mghformat.py
orduek Feb 5, 2020
54fc107
Update nibabel/freesurfer/tests/test_mghformat.py
orduek Feb 5, 2020
27ef400
fixed style and convetion in test_io.py
orduek Feb 5, 2020
a3a2517
Merge branch 'test/pytest_freesurfer' of https://github.com/orduek/ni…
orduek Feb 5, 2020
fdd26a2
added more changes to style
orduek Feb 5, 2020
e3f171b
MNT: CI YAML formatting, extraneous .coveragerc
effigies Feb 6, 2020
eaab253
TEST: Style and formatting
effigies Feb 6, 2020
6dd27a3
Merge pull request #1 from effigies/test/pytest_freesurfer
orduek Feb 6, 2020
5064f9a
Update nibabel/freesurfer/tests/test_mghformat.py
orduek Feb 6, 2020
27f0f03
Merge branch 'pytest' into test/pytest_freesurfer
orduek Feb 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .azure-pipelines/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ jobs:
-I test_round_trip ^
-I test_rstutils ^
-I test_scaling ^
-I test_wrapstruct ^
-I test_io ^
-I test_scripts ^
-I test_spaces ^
-I test_testing ^
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@ doc/source/reference
venv/
.buildbot.patch
.vscode
for_testing/
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ script:
-I test_round_trip \
-I test_rstutils \
-I test_scaling \
-I test_wrapstruct \
-I test_io \
-I test_scripts \
-I test_spaces \
-I test_testing \
Expand Down
126 changes: 61 additions & 65 deletions nibabel/freesurfer/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@

from ...tmpdirs import InTemporaryDirectory

from nose.tools import assert_true

import pytest
import numpy as np
from numpy.testing import assert_equal, assert_raises, dec, assert_allclose, assert_array_equal
from numpy.testing import assert_allclose, assert_array_equal

from .. import (read_geometry, read_morph_data, read_annot, read_label,
write_geometry, write_morph_data, write_annot)
from ..io import _pack_rgb

from ...tests.nibabel_data import get_nibabel_data, needs_nibabel_data
from ...fileslice import strided_scalar
from ...testing import clear_and_catch_warnings
from ...testing_pytest import clear_and_catch_warnings

DATA_SDIR = 'fsaverage'

Expand All @@ -35,10 +36,8 @@
data_path = pjoin(nib_data, 'nitest-freesurfer', DATA_SDIR)
have_freesurfer = isdir(data_path)

freesurfer_test = dec.skipif(
not have_freesurfer,
'cannot find freesurfer {0} directory'.format(DATA_SDIR))

freesurfer_test = pytest.mark.skipif(not have_freesurfer,
reason='cannot find freesurfer {0} directory'.format(DATA_SDIR))

def _hash_file_content(fname):
hasher = hashlib.md5()
Expand All @@ -53,19 +52,18 @@ def test_geometry():
"""Test IO of .surf"""
surf_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "inflated"))
coords, faces = read_geometry(surf_path)
assert_equal(0, faces.min())
assert_equal(coords.shape[0], faces.max() + 1)
assert 0 == faces.min()
assert coords.shape[0] == faces.max() + 1

surf_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "sphere"))
coords, faces, volume_info, create_stamp = read_geometry(
surf_path, read_metadata=True, read_stamp=True)

assert_equal(0, faces.min())
assert_equal(coords.shape[0], faces.max() + 1)
assert_equal(9, len(volume_info))
assert_equal([2, 0, 20], volume_info['head'])
assert_equal(u'created by greve on Thu Jun 8 19:17:51 2006',
create_stamp)
assert 0 == faces.min()
assert coords.shape[0] == faces.max() + 1
assert 9 == len(volume_info)
assert np.array_equal([2, 0, 20], volume_info['head'])
assert create_stamp == 'created by greve on Thu Jun 8 19:17:51 2006'

# Test equivalence of freesurfer- and nibabel-generated triangular files
# with respect to read_geometry()
Expand All @@ -82,7 +80,8 @@ def test_geometry():
for key in ('xras', 'yras', 'zras', 'cras'):
assert_allclose(volume_info2[key], volume_info[key],
rtol=1e-7, atol=1e-30)
assert_equal(volume_info2['cras'], volume_info['cras'])

assert np.array_equal(volume_info2['cras'], volume_info['cras'])
with open(surf_path, 'rb') as fobj:
np.fromfile(fobj, ">u1", 3)
read_create_stamp = fobj.readline().decode().rstrip('\n')
Expand All @@ -92,27 +91,27 @@ def test_geometry():
with clear_and_catch_warnings() as w:
warnings.filterwarnings('always', category=DeprecationWarning)
read_geometry(surf_path, read_metadata=True)
assert_true(any('volume information contained' in str(ww.message)
for ww in w))
assert_true(any('extension code' in str(ww.message) for ww in w))

assert any('volume information contained' in str(ww.message) for ww in w)
assert any('extension code' in str(ww.message) for ww in w)
volume_info['head'] = [1, 2]
with clear_and_catch_warnings() as w:
write_geometry(surf_path, coords, faces, create_stamp, volume_info)
assert_true(any('Unknown extension' in str(ww.message) for ww in w))
assert any('Unknown extension' in str(ww.message) for ww in w)
volume_info['a'] = 0
assert_raises(ValueError, write_geometry, surf_path, coords,
faces, create_stamp, volume_info)
with pytest.raises(ValueError):
write_geometry(surf_path, coords, faces, create_stamp, volume_info)

assert_equal(create_stamp, read_create_stamp)
assert create_stamp == read_create_stamp

np.testing.assert_array_equal(coords, coords2)
np.testing.assert_array_equal(faces, faces2)
assert np.array_equal(coords, coords2)
assert np.array_equal(faces, faces2)

# Validate byte ordering
coords_swapped = coords.byteswap().newbyteorder()
faces_swapped = faces.byteswap().newbyteorder()
np.testing.assert_array_equal(coords_swapped, coords)
np.testing.assert_array_equal(faces_swapped, faces)
assert np.array_equal(coords_swapped, coords)
assert np.array_equal(faces_swapped, faces)


@freesurfer_test
Expand All @@ -122,28 +121,28 @@ def test_quad_geometry():
new_quad = pjoin(get_nibabel_data(), 'nitest-freesurfer', 'subjects',
'bert', 'surf', 'lh.inflated.nofix')
coords, faces = read_geometry(new_quad)
assert_equal(0, faces.min())
assert_equal(coords.shape[0], faces.max() + 1)
assert 0 == faces.min()
assert coords.shape[0] == (faces.max() + 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert coords.shape[0] == (faces.max() + 1)
assert coords.shape[0] == faces.max() + 1

with InTemporaryDirectory():
new_path = 'test'
write_geometry(new_path, coords, faces)
coords2, faces2 = read_geometry(new_path)
assert_equal(coords, coords2)
assert_equal(faces, faces2)
assert np.array_equal(coords,coords2)
assert np.array_equal(faces, faces2)


@freesurfer_test
def test_morph_data():
"""Test IO of morphometry data file (eg. curvature)."""
curv_path = pjoin(data_path, "surf", "%s.%s" % ("lh", "curv"))
curv = read_morph_data(curv_path)
assert_true(-1.0 < curv.min() < 0)
assert_true(0 < curv.max() < 1.0)
assert -1.0 < curv.min() < 0
assert 0 < curv.max() < 1.0
with InTemporaryDirectory():
new_path = 'test'
write_morph_data(new_path, curv)
curv2 = read_morph_data(new_path)
assert_equal(curv2, curv)
assert np.array_equal(curv2, curv)


def test_write_morph_data():
Expand All @@ -156,17 +155,17 @@ def test_write_morph_data():
for shape in okay_shapes:
write_morph_data('test.curv', values.reshape(shape))
# Check ordering is preserved, regardless of shape
assert_equal(values, read_morph_data('test.curv'))
assert_raises(ValueError, write_morph_data, 'test.curv',
np.zeros(shape), big_num)
assert np.array_equal(read_morph_data('test.curv'), values)

with pytest.raises(ValueError):
write_morph_data('test.curv', np.zeros(shape), big_num)
# Windows 32-bit overflows Python int
if np.dtype(np.int) != np.dtype(np.int32):
assert_raises(ValueError, write_morph_data, 'test.curv',
strided_scalar((big_num,)))
with pytest.raises(ValueError):
write_morph_data('test.curv', strided_scalar((big_num,)))
for shape in bad_shapes:
assert_raises(ValueError, write_morph_data, 'test.curv',
values.reshape(shape))

with pytest.raises(ValueError):
write_morph_data('test.curv', values.reshape(shape))

@freesurfer_test
def test_annot():
Expand All @@ -177,18 +176,18 @@ def test_annot():
hash_ = _hash_file_content(annot_path)

labels, ctab, names = read_annot(annot_path)
assert_true(labels.shape == (163842, ))
assert_true(ctab.shape == (len(names), 5))
assert labels.shape == (163842, )
assert ctab.shape == (len(names), 5)

labels_orig = None
if a == 'aparc':
labels_orig, _, _ = read_annot(annot_path, orig_ids=True)
np.testing.assert_array_equal(labels == -1, labels_orig == 0)
# Handle different version of fsaverage
if hash_ == 'bf0b488994657435cdddac5f107d21e8':
assert_true(np.sum(labels_orig == 0) == 13887)
assert np.sum(labels_orig == 0) == 13887
elif hash_ == 'd4f5b7cbc2ed363ac6fcf89e19353504':
assert_true(np.sum(labels_orig == 1639705) == 13327)
assert np.sum(labels_orig == 1639705) == 13327
else:
raise RuntimeError("Unknown freesurfer file. Please report "
"the problem to the maintainer of nibabel.")
Expand All @@ -203,11 +202,11 @@ def test_annot():
if labels_orig is not None:
labels_orig_2, _, _ = read_annot(annot_path, orig_ids=True)

np.testing.assert_array_equal(labels, labels2)
assert np.array_equal(labels, labels2)
if labels_orig is not None:
np.testing.assert_array_equal(labels_orig, labels_orig_2)
np.testing.assert_array_equal(ctab, ctab2)
assert_equal(names, names2)
assert np.array_equal(labels_orig, labels_orig_2)
assert np.array_equal(ctab, ctab2)
assert names == names2


def test_read_write_annot():
Expand Down Expand Up @@ -269,12 +268,10 @@ def test_write_annot_fill_ctab():
# values back.
badannot = (10 * np.arange(nlabels, dtype=np.int32)).reshape(-1, 1)
rgbal = np.hstack((rgba, badannot))
print(labels)
with clear_and_catch_warnings() as w:
write_annot(annot_path, labels, rgbal, names, fill_ctab=False)
assert_true(
any('Annotation values in {} will be incorrect'.format(
annot_path) == str(ww.message) for ww in w))
assert any('Annotation values in {} will be incorrect'.format(annot_path) == str(ww.message)
for ww in w)
labels2, rgbal2, names2 = read_annot(annot_path, orig_ids=True)
names2 = [n.decode('ascii') for n in names2]
assert np.all(np.isclose(rgbal2[:, :4], rgba))
Expand All @@ -288,9 +285,8 @@ def test_write_annot_fill_ctab():
rgbal[:, 2] * (2 ** 16))
with clear_and_catch_warnings() as w:
write_annot(annot_path, labels, rgbal, names, fill_ctab=False)
assert_true(
not any('Annotation values in {} will be incorrect'.format(
annot_path) == str(ww.message) for ww in w))
assert all('Annotation values in {} will be incorrect'.format(annot_path) != str(ww.message)
for ww in w)
labels2, rgbal2, names2 = read_annot(annot_path)
names2 = [n.decode('ascii') for n in names2]
assert np.all(np.isclose(rgbal2[:, :4], rgba))
Expand Down Expand Up @@ -348,13 +344,13 @@ def test_label():
label_path = pjoin(data_path, "label", "lh.cortex.label")
label = read_label(label_path)
# XXX : test more
assert_true(label.min() >= 0)
assert_true(label.max() <= 163841)
assert_true(label.shape[0] <= 163842)
assert label.min() >= 0
assert label.max() <= 163841
assert label.shape[0] <= 163842

labels, scalars = read_label(label_path, True)
assert_true(np.all(labels == label))
assert_true(len(labels) == len(scalars))
assert np.all(labels == label)
assert len(labels) == len(scalars)


def test_write_annot_maxstruct():
Expand All @@ -371,6 +367,6 @@ def test_write_annot_maxstruct():
# Validate the file can be read
rt_labels, rt_ctab, rt_names = read_annot(annot_path)
# Check round-trip
assert_array_equal(labels, rt_labels)
assert_array_equal(rgba, rt_ctab[:, :4])
assert_equal(names, [n.decode('ascii') for n in rt_names])
assert np.array_equal(labels, rt_labels)
assert np.array_equal(rgba, rt_ctab[:, :4])
assert names == [n.decode('ascii') for n in rt_names]
Loading