Skip to content

Commit a3fd7cd

Browse files
committed
DEP: Deprecate nibabel.trackvis, effect old FutureWarning threats
1 parent 97447e2 commit a3fd7cd

File tree

3 files changed

+38
-72
lines changed

3 files changed

+38
-72
lines changed

nibabel/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def setup_test():
7676
flip_axis, OrientationError,
7777
apply_orientation, aff2axcodes)
7878
from .imageclasses import class_map, ext_map, all_image_classes
79-
from . import trackvis
79+
trackvis = _ModuleProxy('nibabel.trackvis')
8080
from . import mriutils
8181
from . import streamlines
8282
from . import viewers

nibabel/tests/test_trackvis.py

+9-31
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
from ..orientations import aff2axcodes
1111
from ..volumeutils import native_code, swapped_code
1212

13-
from nose.tools import assert_true, assert_false, assert_equal, assert_raises
14-
from numpy.testing import assert_array_equal, assert_array_almost_equal
15-
from ..testing import error_warnings, suppress_warnings
13+
from numpy.testing import assert_array_almost_equal
14+
from ..testing import (assert_true, assert_false, assert_equal, assert_raises, assert_warns,
15+
assert_array_equal, suppress_warnings)
1616

1717

1818
def test_write():
@@ -217,8 +217,7 @@ def _rt(streams, hdr, points_space):
217217
assert_raises(tv.HeaderError, tv.read, out_f, False, 'voxel')
218218
# There's a warning for any voxel sizes == 0
219219
hdr = {'voxel_size': [2, 3, 0]}
220-
with error_warnings():
221-
assert_raises(UserWarning, _rt, vx_streams, hdr, 'voxel')
220+
assert_warns(UserWarning, _rt, vx_streams, hdr, 'voxel')
222221
# This should be OK
223222
hdr = {'voxel_size': [2, 3, 4]}
224223
(raw_streams, hdr), (proc_streams, _) = _rt(vx_streams, hdr, 'voxel')
@@ -305,9 +304,8 @@ def test__check_hdr_points_space():
305304
tv._check_hdr_points_space, hdr, 'voxel')
306305
# Warning here only
307306
hdr['voxel_size'] = [2, 3, 0]
308-
with error_warnings():
309-
assert_raises(UserWarning,
310-
tv._check_hdr_points_space, hdr, 'voxel')
307+
assert_warns(UserWarning,
308+
tv._check_hdr_points_space, hdr, 'voxel')
311309
# This is OK
312310
hdr['voxel_size'] = [2, 3, 4]
313311
assert_equal(tv._check_hdr_points_space(hdr, 'voxel'), None)
@@ -370,10 +368,6 @@ def test_empty_header():
370368
def test_get_affine():
371369
# Test get affine behavior, including pending deprecation
372370
hdr = tv.empty_header()
373-
# Using version 1 affine is not a good idea because is fragile and not
374-
# very useful. The default atleast_v2=None mode raises a FutureWarning
375-
with error_warnings():
376-
assert_raises(FutureWarning, tv.aff_from_hdr, hdr)
377371
# testing the old behavior
378372
old_afh = partial(tv.aff_from_hdr, atleast_v2=False)
379373
# default header gives useless affine
@@ -421,9 +415,8 @@ def test_get_affine():
421415
assert_equal(hdr['voxel_order'], o_codes)
422416
# Check it came back the way we wanted
423417
assert_array_equal(old_afh(hdr), in_aff)
424-
# Check that the default case matches atleast_v2=False case
425-
with suppress_warnings():
426-
assert_array_equal(tv.aff_from_hdr(hdr), flipped_aff)
418+
# Check that v1 header raises error
419+
assert_raises(tv.HeaderError, tv.aff_from_hdr, hdr)
427420
# now use the easier vox_to_ras field
428421
hdr = tv.empty_header()
429422
aff = np.eye(4)
@@ -455,15 +448,7 @@ def test_aff_to_hdr():
455448
# Historically we flip the first axis if there is a negative determinant
456449
assert_array_almost_equal(hdr['voxel_size'], [-1, 2, 3])
457450
assert_array_almost_equal(tv.aff_from_hdr(hdr, atleast_v2=False), aff2)
458-
# Test that default mode raises DeprecationWarning
459-
with error_warnings():
460-
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr)
461-
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr, None, None)
462-
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr, False, None)
463-
assert_raises(FutureWarning, tv.aff_to_hdr, affine, hdr, None, False)
464-
# And has same effect as above
465-
with suppress_warnings():
466-
tv.aff_to_hdr(affine, hdr)
451+
tv.aff_to_hdr(affine, hdr, pos_vox=False, set_order=False)
467452
assert_array_almost_equal(tv.aff_from_hdr(hdr, atleast_v2=False), affine)
468453
# Check pos_vox and order flags
469454
for hdr in ({}, {'version': 2}, {'version': 1}):
@@ -515,13 +500,6 @@ def test_tv_class():
515500
affine = np.diag([1, 2, 3, 1])
516501
affine[:3, 3] = [10, 11, 12]
517502
# affine methods will raise same warnings and errors as function
518-
with error_warnings():
519-
assert_raises(FutureWarning, tvf.set_affine, affine)
520-
assert_raises(FutureWarning, tvf.set_affine, affine, None, None)
521-
assert_raises(FutureWarning, tvf.set_affine, affine, False, None)
522-
assert_raises(FutureWarning, tvf.set_affine, affine, None, False)
523-
assert_raises(FutureWarning, tvf.get_affine)
524-
assert_raises(FutureWarning, tvf.get_affine, None)
525503
tvf.set_affine(affine, pos_vox=True, set_order=True)
526504
aff = tvf.get_affine(atleast_v2=True)
527505
assert_array_almost_equal(aff, affine)

nibabel/trackvis.py

+28-40
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717
from .openers import ImageOpener
1818
from .orientations import aff2axcodes
1919
from .affines import apply_affine
20+
from .deprecated import deprecate_with_version
2021

2122
try:
2223
basestring
2324
except NameError: # python 3
2425
basestring = str
2526

27+
warnings.warn("The trackvis interface has been deprecated and will be removed "
28+
"in v4.0; please use the 'nibabel.streamlines' interface.",
29+
DeprecationWarning,
30+
stacklevel=2)
31+
2632
# Definition of trackvis header structure.
2733
# See http://www.trackvis.org/docs/?subsect=fileformat
2834
# See https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
@@ -99,6 +105,9 @@ class DataError(Exception):
99105
"""
100106

101107

108+
@deprecate_with_version('trackvis.read is deprecated; please use '
109+
'nibabel.streamlines.load, instead.',
110+
since='2.5.0', until='4.0.0')
102111
def read(fileobj, as_generator=False, points_space=None, strict=True):
103112
''' Read trackvis file from `fileobj`, return `streamlines`, `header`
104113
@@ -254,6 +263,9 @@ def track_gen():
254263
return streamlines, hdr
255264

256265

266+
@deprecate_with_version('trackvis.write is deprecated; please use '
267+
'nibabel.streamlines.save, instead.',
268+
since='2.5.0', until='4.0.0')
257269
def write(fileobj, streamlines, hdr_mapping=None, endianness=None,
258270
points_space=None):
259271
''' Write header and `streamlines` to trackvis file `fileobj`
@@ -536,6 +548,9 @@ def _hdr_from_mapping(hdr=None, mapping=None, endianness=native_code):
536548
return hdr
537549

538550

551+
@deprecate_with_version('empty_header is deprecated; please use '
552+
'nibabel.streamlines.TrkFile.create_empty_header, instead.',
553+
since='2.5.0', until='4.0.0')
539554
def empty_header(endianness=None, version=2):
540555
''' Empty trackvis header
541556
@@ -590,7 +605,10 @@ def empty_header(endianness=None, version=2):
590605
return hdr
591606

592607

593-
def aff_from_hdr(trk_hdr, atleast_v2=None):
608+
@deprecate_with_version('aff_from_hdr is deprecated; please use '
609+
'nibabel.streamlines.trk.get_affine_trackvis_to_rasmm, instead.',
610+
since='2.5.0', until='4.0.0')
611+
def aff_from_hdr(trk_hdr, atleast_v2=True):
594612
''' Return voxel to mm affine from trackvis header
595613
596614
Affine is mapping from voxel space to Nifti (RAS) output coordinate
@@ -625,12 +643,6 @@ def aff_from_hdr(trk_hdr, atleast_v2=None):
625643
origin field to 0. In future, we'll raise an error rather than try and
626644
estimate the affine from version 1 fields
627645
'''
628-
if atleast_v2 is None:
629-
warnings.warn('Defaulting to `atleast_v2` of False. Future versions '
630-
'will default to True',
631-
FutureWarning,
632-
stacklevel=2)
633-
atleast_v2 = False
634646
if trk_hdr['version'] == 2:
635647
aff = trk_hdr['vox_to_ras']
636648
if aff[3, 3] != 0:
@@ -673,7 +685,10 @@ def aff_from_hdr(trk_hdr, atleast_v2=None):
673685
return aff
674686

675687

676-
def aff_to_hdr(affine, trk_hdr, pos_vox=None, set_order=None):
688+
@deprecate_with_version('aff_to_hdr is deprecated; please use the '
689+
'nibabel.streamlines.TrkFile.affine_to_rasmm property, instead.',
690+
since='2.5.0', until='4.0.0')
691+
def aff_to_hdr(affine, trk_hdr, pos_vox=True, set_order=True):
677692
''' Set affine `affine` into trackvis header `trk_hdr`
678693
679694
Affine is mapping from voxel space to Nifti RAS) output coordinate
@@ -715,18 +730,6 @@ def aff_to_hdr(affine, trk_hdr, pos_vox=None, set_order=None):
715730
application). The application also ignores the origin field, and may not
716731
use the 'image_orientation_patient' field.
717732
'''
718-
if pos_vox is None:
719-
warnings.warn('Default for ``pos_vox`` will change to True in '
720-
'future versions of nibabel',
721-
FutureWarning,
722-
stacklevel=2)
723-
pos_vox = False
724-
if set_order is None:
725-
warnings.warn('Default for ``set_order`` will change to True in '
726-
'future versions of nibabel',
727-
FutureWarning,
728-
stacklevel=2)
729-
set_order = False
730733
try:
731734
version = trk_hdr['version']
732735
except (KeyError, ValueError): # dict or structured array
@@ -797,6 +800,9 @@ class TrackvisFile(object):
797800
relationship between voxels, rasmm and voxmm space (above).
798801
'''
799802

803+
@deprecate_with_version('TrackvisFile is deprecated; please use '
804+
'nibabel.streamlines.TrkFile, instead.',
805+
since='2.5.0', until='4.0.0')
800806
def __init__(self,
801807
streamlines,
802808
mapping=None,
@@ -836,7 +842,7 @@ def to_file(self, file_like):
836842
self.filename = (file_like if isinstance(file_like, basestring)
837843
else None)
838844

839-
def get_affine(self, atleast_v2=None):
845+
def get_affine(self, atleast_v2=True):
840846
""" Get affine from header in object
841847
842848
Returns
@@ -853,15 +859,9 @@ def get_affine(self, atleast_v2=None):
853859
consider it unsafe for version 1 headers, and in future versions of
854860
nibabel we will raise an error for trackvis headers < version 2.
855861
"""
856-
if atleast_v2 is None:
857-
warnings.warn('Defaulting to `atleast_v2` of False. Future '
858-
'versions will default to True',
859-
FutureWarning,
860-
stacklevel=2)
861-
atleast_v2 = False
862862
return aff_from_hdr(self.header, atleast_v2)
863863

864-
def set_affine(self, affine, pos_vox=None, set_order=None):
864+
def set_affine(self, affine, pos_vox=True, set_order=True):
865865
""" Set affine `affine` into trackvis header
866866
867867
Affine is mapping from voxel space to Nifti RAS) output coordinate
@@ -888,16 +888,4 @@ def set_affine(self, affine, pos_vox=None, set_order=None):
888888
-------
889889
None
890890
"""
891-
if pos_vox is None:
892-
warnings.warn('Default for ``pos_vox`` will change to True in '
893-
'future versions of nibabel',
894-
FutureWarning,
895-
stacklevel=2)
896-
pos_vox = False
897-
if set_order is None:
898-
warnings.warn('Default for ``set_order`` will change to True in '
899-
'future versions of nibabel',
900-
FutureWarning,
901-
stacklevel=2)
902-
set_order = False
903891
return aff_to_hdr(affine, self.header, pos_vox, set_order)

0 commit comments

Comments
 (0)