<s> '''<STR_LIT>''' <EOL> from __future__ import division , print_function , absolute_import <EOL> from functools import partial <EOL> import numpy as np <EOL> from . . externals . six import BytesIO <EOL> from . . import trackvis as tv <EOL> from . . orientations import aff2axcodes <EOL> from . . volumeutils import native_code , swapped_code <EOL> from nose . tools import assert_true , assert_false , assert_equal , assert_raises <EOL> from numpy . testing import assert_array_equal , assert_array_almost_equal <EOL> from . . testing import error_warnings , suppress_warnings <EOL> def test_write ( ) : <EOL> streams = [ ] <EOL> out_f = BytesIO ( ) <EOL> tv . write ( out_f , [ ] , { } ) <EOL> assert_equal ( out_f . getvalue ( ) , tv . empty_header ( ) . tostring ( ) ) <EOL> out_f . truncate ( <NUM_LIT:0> ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tv . write ( out_f , [ ] , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams , hdr = tv . read ( out_f ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> out_f . truncate ( <NUM_LIT:0> ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tv . write ( out_f , [ ] ) <EOL> out_f . truncate ( <NUM_LIT:0> ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tv . write ( out_f , [ ] , None ) <EOL> out_f . truncate ( <NUM_LIT:0> ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . write , out_f , [ ] , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . write , out_f , [ ] , { '<STR_LIT:version>' : <NUM_LIT:3> } ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . write , out_f , [ ] , { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> def test_write_scalars_props ( ) : <EOL> N = <NUM_LIT:6> <EOL> M = <NUM_LIT:2> <EOL> P = <NUM_LIT:4> <EOL> points = np . arange ( N * <NUM_LIT:3> ) . reshape ( ( N , <NUM_LIT:3> ) ) <EOL> scalars = np . arange ( N * M ) . reshape ( ( N , M ) ) + <NUM_LIT:100> <EOL> props = np . arange ( P ) + <NUM_LIT:1000> <EOL> out_f = BytesIO ( ) <EOL> streams = [ ( points , None , None ) , <EOL> ( points , scalars , None ) ] <EOL> assert_raises ( tv . DataError , tv . write , out_f , streams ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams = [ ( points , np . zeros ( ( N , M + <NUM_LIT:1> ) ) , None ) , <EOL> ( points , scalars , None ) ] <EOL> assert_raises ( tv . DataError , tv . write , out_f , streams ) <EOL> bad_scalars = np . zeros ( ( N + <NUM_LIT:1> , M ) ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams = [ ( points , bad_scalars , None ) , <EOL> ( points , bad_scalars , None ) ] <EOL> assert_raises ( tv . DataError , tv . write , out_f , streams ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams = [ ( points , scalars , None ) , <EOL> ( points , scalars , props ) ] <EOL> assert_raises ( tv . DataError , tv . write , out_f , streams ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams = [ ( points , scalars , np . zeros ( ( P + <NUM_LIT:1> , ) ) ) , <EOL> ( points , scalars , props ) ] <EOL> assert_raises ( tv . DataError , tv . write , out_f , streams ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams = [ ( points , scalars , props ) , <EOL> ( points , scalars , props ) ] <EOL> tv . write ( out_f , streams ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> back_streams , hdr = tv . read ( out_f ) <EOL> for actual , expected in zip ( streams , back_streams ) : <EOL> for a_el , e_el in zip ( actual , expected ) : <EOL> assert_array_equal ( a_el , e_el ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams = [ ( points . astype ( '<STR_LIT>' ) , <EOL> scalars . astype ( '<STR_LIT>' ) , <EOL> props . astype ( '<STR_LIT>' ) ) ] <EOL> tv . write ( out_f , streams ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> back_streams , hdr = tv . read ( out_f ) <EOL> for actual , expected in zip ( streams , back_streams ) : <EOL> for a_el , e_el in zip ( actual , expected ) : <EOL> assert_array_almost_equal ( a_el , e_el ) <EOL> def streams_equal ( stream1 , stream2 ) : <EOL> if not np . all ( stream1 [ <NUM_LIT:0> ] == stream2 [ <NUM_LIT:0> ] ) : <EOL> return False <EOL> if stream1 [ <NUM_LIT:1> ] is None : <EOL> if not stream2 [ <NUM_LIT:1> ] is None : <EOL> return False <EOL> if stream1 [ <NUM_LIT:2> ] is None : <EOL> if not stream2 [ <NUM_LIT:2> ] is None : <EOL> return False <EOL> if not np . all ( stream1 [ <NUM_LIT:1> ] == stream2 [ <NUM_LIT:1> ] ) : <EOL> return False <EOL> if not np . all ( stream1 [ <NUM_LIT:2> ] == stream2 [ <NUM_LIT:2> ] ) : <EOL> return False <EOL> return True <EOL> def streamlist_equal ( streamlist1 , streamlist2 ) : <EOL> if len ( streamlist1 ) != len ( streamlist2 ) : <EOL> return False <EOL> for s1 , s2 in zip ( streamlist1 , streamlist2 ) : <EOL> if not streams_equal ( s1 , s2 ) : <EOL> return False <EOL> return True <EOL> def test_round_trip ( ) : <EOL> out_f = BytesIO ( ) <EOL> xyz0 = np . tile ( np . arange ( <NUM_LIT:5> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> xyz1 = np . tile ( np . arange ( <NUM_LIT:5> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:1> ) + <NUM_LIT:10> , ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> streams = [ ( xyz0 , None , None ) , ( xyz1 , None , None ) ] <EOL> tv . write ( out_f , streams , { } ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams2 , hdr = tv . read ( out_f ) <EOL> assert_true ( streamlist_equal ( streams , streams2 ) ) <EOL> for in_dict , back_version in ( ( { } , <NUM_LIT:2> ) , <EOL> ( { '<STR_LIT:version>' : <NUM_LIT:2> } , <NUM_LIT:2> ) , <EOL> ( { '<STR_LIT:version>' : <NUM_LIT:1> } , <NUM_LIT:1> ) ) : <EOL> for endian_code in ( native_code , swapped_code ) : <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tv . write ( out_f , streams , in_dict , endian_code ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams2 , hdr = tv . read ( out_f ) <EOL> assert_true ( streamlist_equal ( streams , streams2 ) ) <EOL> assert_equal ( hdr [ '<STR_LIT:version>' ] , back_version ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams3 , hdr = tv . read ( out_f , as_generator = True ) <EOL> assert_true ( hasattr ( streams3 , '<STR_LIT>' ) ) <EOL> assert_true ( streamlist_equal ( streams , list ( streams3 ) ) ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> streams3 , hdr = tv . read ( out_f , as_generator = True ) <EOL> out_f_write = BytesIO ( ) <EOL> tv . write ( out_f_write , streams3 , { } ) <EOL> out_f_write . seek ( <NUM_LIT:0> ) <EOL> streams2 , hdr = tv . read ( out_f_write ) <EOL> assert_true ( streamlist_equal ( streams , streams2 ) ) <EOL> def test_points_processing ( ) : <EOL> out_f = BytesIO ( ) <EOL> def _rt ( streams , hdr , points_space ) : <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tv . write ( out_f , streams , hdr , points_space = points_space ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> res0 = tv . read ( out_f ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> return res0 , tv . read ( out_f , points_space = points_space ) <EOL> n_pts = <NUM_LIT:5> <EOL> ijk0 = np . arange ( n_pts * <NUM_LIT:3> ) . reshape ( ( n_pts , <NUM_LIT:3> ) ) / <NUM_LIT> <EOL> ijk1 = ijk0 + <NUM_LIT:20> <EOL> for scalars in ( ( None , None ) , <EOL> ( np . arange ( n_pts ) [ : , None ] , <EOL> np . arange ( n_pts ) [ : , None ] + <NUM_LIT> ) ) : <EOL> vx_streams = [ ( ijk0 , scalars [ <NUM_LIT:0> ] , None ) , ( ijk1 , scalars [ <NUM_LIT:1> ] , None ) ] <EOL> vxmm_streams = [ ( ijk0 * [ [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ] , scalars [ <NUM_LIT:0> ] , None ) , <EOL> ( ijk1 * [ [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ] , scalars [ <NUM_LIT:1> ] , None ) ] <EOL> ( raw_streams , hdr ) , ( proc_streams , _ ) = _rt ( vxmm_streams , { } , None ) <EOL> assert_true ( streamlist_equal ( raw_streams , proc_streams ) ) <EOL> assert_true ( streamlist_equal ( vxmm_streams , proc_streams ) ) <EOL> ( raw_streams , hdr ) , ( proc_streams , _ ) = _rt ( vxmm_streams , { } , '<STR_LIT>' ) <EOL> assert_true ( streamlist_equal ( raw_streams , proc_streams ) ) <EOL> assert_true ( streamlist_equal ( vxmm_streams , proc_streams ) ) <EOL> for hdr in ( <EOL> { } , <EOL> { '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] } , <EOL> { '<STR_LIT>' : [ - <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] } , <EOL> ) : <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . write , out_f , vx_streams , hdr , None , '<STR_LIT>' ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tv . write ( out_f , vxmm_streams , hdr , None , points_space = None ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> assert_raises ( tv . HeaderError , tv . read , out_f , False , '<STR_LIT>' ) <EOL> hdr = { '<STR_LIT>' : [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:0> ] } <EOL> with error_warnings ( ) : <EOL> assert_raises ( UserWarning , _rt , vx_streams , hdr , '<STR_LIT>' ) <EOL> hdr = { '<STR_LIT>' : [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] } <EOL> ( raw_streams , hdr ) , ( proc_streams , _ ) = _rt ( vx_streams , hdr , '<STR_LIT>' ) <EOL> assert_true ( streamlist_equal ( vxmm_streams , raw_streams ) ) <EOL> assert_true ( streamlist_equal ( vx_streams , proc_streams ) ) <EOL> aff = np . diag ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> rasmm_streams = vxmm_streams <EOL> for hdr in ( <EOL> { } , <EOL> { '<STR_LIT>' : [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:version>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : np . zeros ( ( <NUM_LIT:4> , <NUM_LIT:4> ) ) } , <EOL> { '<STR_LIT>' : [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:4> ] , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : aff } , <EOL> { '<STR_LIT>' : [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : aff } , <EOL> ) : <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . write , out_f , rasmm_streams , hdr , None , '<STR_LIT>' ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tv . write ( out_f , vxmm_streams , hdr , None , points_space = None ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> assert_raises ( tv . HeaderError , tv . read , out_f , False , '<STR_LIT>' ) <EOL> hdr = { '<STR_LIT>' : [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : aff } <EOL> ( raw_streams , hdr ) , ( proc_streams , _ ) = _rt ( rasmm_streams , hdr , '<STR_LIT>' ) <EOL> assert_true ( streamlist_equal ( vxmm_streams , raw_streams ) ) <EOL> assert_true ( streamlist_equal ( rasmm_streams , proc_streams ) ) <EOL> fancy_affine = np . array ( [ [ <NUM_LIT:0.> , - <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:10> ] , <EOL> [ <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:20> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:30> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> hdr = { '<STR_LIT>' : [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:4> ] , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : fancy_affine } <EOL> def f ( pts ) : <EOL> pts = pts [ : , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ] ] * [ [ - <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ] <EOL> return pts + [ [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ] <EOL> xyz0 , xyz1 = f ( ijk0 ) , f ( ijk1 ) <EOL> fancy_rasmm_streams = [ ( xyz0 , scalars [ <NUM_LIT:0> ] , None ) , <EOL> ( xyz1 , scalars [ <NUM_LIT:1> ] , None ) ] <EOL> fancy_vxmm_streams = [ ( ijk0 * [ [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:4> ] ] , scalars [ <NUM_LIT:0> ] , None ) , <EOL> ( ijk1 * [ [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:4> ] ] , scalars [ <NUM_LIT:1> ] , None ) ] <EOL> ( raw_streams , hdr ) , ( proc_streams , _ ) = _rt ( <EOL> fancy_rasmm_streams , hdr , '<STR_LIT>' ) <EOL> assert_true ( streamlist_equal ( fancy_vxmm_streams , raw_streams ) ) <EOL> assert_true ( streamlist_equal ( fancy_rasmm_streams , proc_streams ) ) <EOL> def test__check_hdr_points_space ( ) : <EOL> assert_equal ( tv . _check_hdr_points_space ( { } , None ) , None ) <EOL> assert_equal ( tv . _check_hdr_points_space ( { } , '<STR_LIT>' ) , None ) <EOL> assert_raises ( ValueError , <EOL> tv . _check_hdr_points_space , { } , '<STR_LIT>' ) <EOL> hdr = tv . empty_header ( ) <EOL> assert_array_equal ( hdr [ '<STR_LIT>' ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . _check_hdr_points_space , hdr , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = [ - <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> assert_raises ( tv . HeaderError , <EOL> tv . _check_hdr_points_space , hdr , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:0> ] <EOL> with error_warnings ( ) : <EOL> assert_raises ( UserWarning , <EOL> tv . _check_hdr_points_space , hdr , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> assert_equal ( tv . _check_hdr_points_space ( hdr , '<STR_LIT>' ) , None ) <EOL> hdr [ '<STR_LIT>' ] = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> assert_raises ( tv . HeaderError , <EOL> tv . _check_hdr_points_space , hdr , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> assert_raises ( tv . HeaderError , <EOL> tv . _check_hdr_points_space , hdr , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = np . diag ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:0> ] ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . _check_hdr_points_space , hdr , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = np . diag ( [ - <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . _check_hdr_points_space , hdr , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = np . diag ( [ <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . _check_hdr_points_space , hdr , '<STR_LIT>' ) <EOL> good_aff = np . diag ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> hdr [ '<STR_LIT>' ] = good_aff <EOL> assert_equal ( tv . _check_hdr_points_space ( hdr , '<STR_LIT>' ) , <EOL> None ) <EOL> hdr [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> assert_raises ( tv . HeaderError , <EOL> tv . _check_hdr_points_space , hdr , '<STR_LIT>' ) <EOL> good_lps = np . dot ( np . diag ( [ - <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) , good_aff ) <EOL> hdr [ '<STR_LIT>' ] = good_lps <EOL> assert_equal ( tv . _check_hdr_points_space ( hdr , '<STR_LIT>' ) , <EOL> None ) <EOL> def test_empty_header ( ) : <EOL> for endian in '<STR_LIT>' : <EOL> for version in ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> hdr = tv . empty_header ( endian , version ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> assert_equal ( hdr [ '<STR_LIT:version>' ] , version ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1000> ) <EOL> assert_array_equal ( <EOL> hdr [ '<STR_LIT>' ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> hdr = tv . empty_header ( version = <NUM_LIT:2> ) <EOL> assert_array_equal ( hdr [ '<STR_LIT>' ] , np . zeros ( ( <NUM_LIT:4> , <NUM_LIT:4> ) ) ) <EOL> hdr_endian = tv . endian_codes [ tv . empty_header ( ) . dtype . byteorder ] <EOL> assert_equal ( hdr_endian , tv . native_code ) <EOL> def test_get_affine ( ) : <EOL> hdr = tv . empty_header ( ) <EOL> with error_warnings ( ) : <EOL> assert_raises ( FutureWarning , tv . aff_from_hdr , hdr ) <EOL> old_afh = partial ( tv . aff_from_hdr , atleast_v2 = False ) <EOL> assert_array_equal ( old_afh ( hdr ) , <EOL> np . diag ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ) ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> assert_array_equal ( old_afh ( hdr ) , <EOL> np . diag ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ) ) <EOL> hdr [ '<STR_LIT>' ] = [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> assert_array_equal ( old_afh ( hdr ) , <EOL> np . diag ( [ - <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) ) <EOL> hdr [ '<STR_LIT>' ] = [ - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> assert_array_equal ( old_afh ( hdr ) , <EOL> np . eye ( <NUM_LIT:4> ) ) <EOL> hdr [ '<STR_LIT>' ] = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> exp_aff = np . eye ( <NUM_LIT:4> ) <EOL> exp_aff [ : <NUM_LIT:3> , <NUM_LIT:3> ] = [ - <NUM_LIT:1> , - <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> assert_array_equal ( old_afh ( hdr ) , <EOL> exp_aff ) <EOL> hdr [ '<STR_LIT>' ] = '<STR_LIT>' . join ( aff2axcodes ( exp_aff ) ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> assert_array_equal ( old_afh ( hdr ) , exp_aff ) <EOL> hdr [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> assert_raises ( tv . HeaderError , old_afh , hdr ) <EOL> hdr [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> exp_aff = exp_aff * [ [ <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:1> ] ] <EOL> assert_array_equal ( old_afh ( hdr ) , exp_aff ) <EOL> flipped_aff = exp_aff <EOL> unflipped_aff = exp_aff * [ <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> for in_aff , o_codes in ( ( unflipped_aff , b'<STR_LIT>' ) , <EOL> ( flipped_aff , b'<STR_LIT>' ) ) : <EOL> hdr = tv . empty_header ( ) <EOL> tv . aff_to_hdr ( in_aff , hdr , pos_vox = True , set_order = True ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , o_codes ) <EOL> assert_array_equal ( old_afh ( hdr ) , in_aff ) <EOL> with suppress_warnings ( ) : <EOL> assert_array_equal ( tv . aff_from_hdr ( hdr ) , flipped_aff ) <EOL> hdr = tv . empty_header ( ) <EOL> aff = np . eye ( <NUM_LIT:4> ) <EOL> aff [ : <NUM_LIT:3> , : ] = np . arange ( <NUM_LIT:12> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> hdr [ '<STR_LIT>' ] = aff <EOL> assert_array_equal ( tv . aff_from_hdr ( hdr , atleast_v2 = False ) , aff ) <EOL> d = { '<STR_LIT:version>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) , <EOL> '<STR_LIT>' : np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ) , <EOL> '<STR_LIT>' : np . array ( [ <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:12> ] ) } <EOL> aff = tv . aff_from_hdr ( d , atleast_v2 = False ) <EOL> def test_aff_to_hdr ( ) : <EOL> old_a2h = partial ( tv . aff_to_hdr , pos_vox = False , set_order = False ) <EOL> hdr = { '<STR_LIT:version>' : <NUM_LIT:1> } <EOL> affine = np . diag ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> affine [ : <NUM_LIT:3> , <NUM_LIT:3> ] = [ <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:12> ] <EOL> old_a2h ( affine , hdr ) <EOL> assert_array_almost_equal ( tv . aff_from_hdr ( hdr , atleast_v2 = False ) , affine ) <EOL> aff2 = affine . copy ( ) <EOL> aff2 [ : , <NUM_LIT:2> ] *= - <NUM_LIT:1> <EOL> old_a2h ( aff2 , hdr ) <EOL> assert_array_almost_equal ( hdr [ '<STR_LIT>' ] , [ - <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> assert_array_almost_equal ( tv . aff_from_hdr ( hdr , atleast_v2 = False ) , aff2 ) <EOL> with error_warnings ( ) : <EOL> assert_raises ( FutureWarning , tv . aff_to_hdr , affine , hdr ) <EOL> assert_raises ( FutureWarning , tv . aff_to_hdr , affine , hdr , None , None ) <EOL> assert_raises ( FutureWarning , tv . aff_to_hdr , affine , hdr , False , None ) <EOL> assert_raises ( FutureWarning , tv . aff_to_hdr , affine , hdr , None , False ) <EOL> with suppress_warnings ( ) : <EOL> tv . aff_to_hdr ( affine , hdr ) <EOL> assert_array_almost_equal ( tv . aff_from_hdr ( hdr , atleast_v2 = False ) , affine ) <EOL> for hdr in ( { } , { '<STR_LIT:version>' : <NUM_LIT:2> } , { '<STR_LIT:version>' : <NUM_LIT:1> } ) : <EOL> tv . aff_to_hdr ( aff2 , hdr , pos_vox = True , set_order = False ) <EOL> assert_array_equal ( hdr [ '<STR_LIT>' ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> assert_false ( '<STR_LIT>' in hdr ) <EOL> tv . aff_to_hdr ( aff2 , hdr , pos_vox = False , set_order = True ) <EOL> assert_array_equal ( hdr [ '<STR_LIT>' ] , [ - <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> tv . aff_to_hdr ( aff2 , hdr , pos_vox = True , set_order = True ) <EOL> assert_array_equal ( hdr [ '<STR_LIT>' ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> if '<STR_LIT:version>' in hdr and hdr [ '<STR_LIT:version>' ] == <NUM_LIT:1> : <EOL> assert_false ( '<STR_LIT>' in hdr ) <EOL> else : <EOL> assert_array_equal ( hdr [ '<STR_LIT>' ] , aff2 ) <EOL> def test_tv_class ( ) : <EOL> tvf = tv . TrackvisFile ( [ ] ) <EOL> assert_equal ( tvf . streamlines , [ ] ) <EOL> assert_true ( isinstance ( tvf . header , np . ndarray ) ) <EOL> assert_equal ( tvf . endianness , tv . native_code ) <EOL> assert_equal ( tvf . filename , None ) <EOL> out_f = BytesIO ( ) <EOL> tvf . to_file ( out_f ) <EOL> assert_equal ( out_f . getvalue ( ) , tv . empty_header ( ) . tostring ( ) ) <EOL> out_f . truncate ( <NUM_LIT:0> ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf = tv . TrackvisFile ( [ ] , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> tvf . to_file ( out_f ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf_back = tv . TrackvisFile . from_file ( out_f ) <EOL> assert_equal ( tvf_back . header [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> out_f . truncate ( <NUM_LIT:0> ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . TrackvisFile , <EOL> [ ] , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . TrackvisFile , <EOL> [ ] , { '<STR_LIT:version>' : <NUM_LIT:3> } ) <EOL> assert_raises ( tv . HeaderError , <EOL> tv . TrackvisFile , <EOL> [ ] , { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> affine = np . diag ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> affine [ : <NUM_LIT:3> , <NUM_LIT:3> ] = [ <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:12> ] <EOL> with error_warnings ( ) : <EOL> assert_raises ( FutureWarning , tvf . set_affine , affine ) <EOL> assert_raises ( FutureWarning , tvf . set_affine , affine , None , None ) <EOL> assert_raises ( FutureWarning , tvf . set_affine , affine , False , None ) <EOL> assert_raises ( FutureWarning , tvf . set_affine , affine , None , False ) <EOL> assert_raises ( FutureWarning , tvf . get_affine ) <EOL> assert_raises ( FutureWarning , tvf . get_affine , None ) <EOL> tvf . set_affine ( affine , pos_vox = True , set_order = True ) <EOL> aff = tvf . get_affine ( atleast_v2 = True ) <EOL> assert_array_almost_equal ( aff , affine ) <EOL> assert_raises ( tv . TrackvisFileError , <EOL> tv . TrackvisFile , <EOL> iter ( [ ] ) ) <EOL> def test_tvfile_io ( ) : <EOL> out_f = BytesIO ( ) <EOL> ijk0 = np . arange ( <NUM_LIT:15> ) . reshape ( ( <NUM_LIT:5> , <NUM_LIT:3> ) ) / <NUM_LIT> <EOL> ijk1 = ijk0 + <NUM_LIT:20> <EOL> vx_streams = [ ( ijk0 , None , None ) , ( ijk1 , None , None ) ] <EOL> vxmm_streams = [ ( ijk0 * [ [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ] , None , None ) , <EOL> ( ijk1 * [ [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ] , None , None ) ] <EOL> tvf = tv . TrackvisFile ( vxmm_streams ) <EOL> tvf . to_file ( out_f ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf2 = tv . TrackvisFile . from_file ( out_f ) <EOL> assert_equal ( tvf2 . filename , None ) <EOL> assert_true ( streamlist_equal ( vxmm_streams , tvf2 . streamlines ) ) <EOL> assert_equal ( tvf2 . points_space , None ) <EOL> tvf = tv . TrackvisFile ( vx_streams , points_space = '<STR_LIT>' ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> assert_raises ( tv . HeaderError , tvf . to_file , out_f ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf . header [ '<STR_LIT>' ] = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> tvf . to_file ( out_f ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf2 = tv . TrackvisFile . from_file ( out_f , points_space = '<STR_LIT>' ) <EOL> assert_true ( streamlist_equal ( vx_streams , tvf2 . streamlines ) ) <EOL> assert_equal ( tvf2 . points_space , '<STR_LIT>' ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf = tv . TrackvisFile ( vx_streams , points_space = '<STR_LIT>' , <EOL> affine = np . diag ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) ) <EOL> tvf . to_file ( out_f ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf2 = tv . TrackvisFile . from_file ( out_f , points_space = '<STR_LIT>' ) <EOL> assert_true ( streamlist_equal ( vx_streams , tvf2 . streamlines ) ) <EOL> fancy_affine = np . array ( [ [ <NUM_LIT:0.> , - <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:10> ] , <EOL> [ <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:20> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:30> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> def f ( pts ) : <EOL> pts = pts [ : , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ] ] * [ [ - <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ] <EOL> return pts + [ [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> ] ] <EOL> xyz0 , xyz1 = f ( ijk0 ) , f ( ijk1 ) <EOL> fancy_rasmm_streams = [ ( xyz0 , None , None ) , ( xyz1 , None , None ) ] <EOL> tvf = tv . TrackvisFile ( fancy_rasmm_streams , points_space = '<STR_LIT>' ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> assert_raises ( tv . HeaderError , tvf . to_file , out_f ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf . set_affine ( fancy_affine , pos_vox = True , set_order = True ) <EOL> tvf . to_file ( out_f ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf2 = tv . TrackvisFile . from_file ( out_f , points_space = '<STR_LIT>' ) <EOL> assert_true ( streamlist_equal ( fancy_rasmm_streams , tvf2 . streamlines ) ) <EOL> assert_equal ( tvf2 . points_space , '<STR_LIT>' ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf = tv . TrackvisFile ( fancy_rasmm_streams , points_space = '<STR_LIT>' , <EOL> affine = fancy_affine ) <EOL> tvf . to_file ( out_f ) <EOL> out_f . seek ( <NUM_LIT:0> ) <EOL> tvf2 = tv . TrackvisFile . from_file ( out_f , points_space = '<STR_LIT>' ) <EOL> assert_true ( streamlist_equal ( fancy_rasmm_streams , tvf2 . streamlines ) ) <EOL> def test_read_truncated ( ) : <EOL> out_f = BytesIO ( ) <EOL> xyz0 = np . tile ( np . arange ( <NUM_LIT:5> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> xyz1 = np . tile ( np . arange ( <NUM_LIT:5> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:1> ) + <NUM_LIT:10> , ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> streams = [ ( xyz0 , None , None ) , ( xyz1 , None , None ) ] <EOL> tv . write ( out_f , streams , { } ) <EOL> value = out_f . getvalue ( ) [ : - ( <NUM_LIT:3> * <NUM_LIT:4> ) ] <EOL> new_f = BytesIO ( value ) <EOL> assert_raises ( tv . DataError , tv . read , new_f ) <EOL> new_f . seek ( <NUM_LIT:0> ) <EOL> assert_raises ( tv . DataError , tv . read , new_f , strict = True ) <EOL> short_streams = [ ( xyz0 , None , None ) , ( xyz1 [ : - <NUM_LIT:1> ] , None , None ) ] <EOL> new_f . seek ( <NUM_LIT:0> ) <EOL> streams2 , hdr = tv . read ( new_f , strict = False ) <EOL> assert_true ( streamlist_equal ( streams2 , short_streams ) ) <EOL> again_hdr = hdr . copy ( ) <EOL> assert_equal ( again_hdr [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> again_hdr [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> again_bytes = again_hdr . tostring ( ) + value [ again_hdr . itemsize : ] <EOL> again_f = BytesIO ( again_bytes ) <EOL> streams2 , _ = tv . read ( again_f , strict = False ) <EOL> assert_true ( streamlist_equal ( streams2 , short_streams ) ) <EOL> again_hdr [ '<STR_LIT>' ] = <NUM_LIT:3> <EOL> again_bytes = again_hdr . tostring ( ) + value [ again_hdr . itemsize : ] <EOL> again_f = BytesIO ( again_bytes ) <EOL> assert_raises ( tv . DataError , tv . read , again_f , strict = False ) </s>
