<s> '''<STR_LIT>''' <EOL> from __future__ import division , print_function , absolute_import <EOL> import os <EOL> import warnings <EOL> import struct <EOL> import numpy as np <EOL> from nibabel import nifti1 as nifti1 <EOL> from nibabel . affines import from_matvec <EOL> from nibabel . casting import type_info , have_binary128 <EOL> from nibabel . eulerangles import euler2mat <EOL> from nibabel . externals . six import BytesIO <EOL> from nibabel . nifti1 import ( load , Nifti1Header , Nifti1PairHeader , Nifti1Image , <EOL> Nifti1Pair , Nifti1Extension , Nifti1DicomExtension , <EOL> Nifti1Extensions , data_type_codes , extension_codes , <EOL> slice_order_codes ) <EOL> from nibabel . spatialimages import HeaderDataError <EOL> from nibabel . tmpdirs import InTemporaryDirectory <EOL> from . . freesurfer import load as mghload <EOL> from . test_arraywriters import rt_err_estimate , IUINT_TYPES <EOL> from . test_helpers import bytesio_filemap , bytesio_round_trip <EOL> from . nibabel_data import get_nibabel_data , needs_nibabel_data <EOL> from numpy . testing import ( assert_array_equal , assert_array_almost_equal , <EOL> assert_almost_equal ) <EOL> from nose . tools import ( assert_true , assert_false , assert_equal , <EOL> assert_raises ) <EOL> from . . testing import data_path , suppress_warnings , runif_extra_has <EOL> from . import test_analyze as tana <EOL> from . import test_spm99analyze as tspm <EOL> header_file = os . path . join ( data_path , '<STR_LIT>' ) <EOL> image_file = os . path . join ( data_path , '<STR_LIT>' ) <EOL> from nibabel . pydicom_compat import pydicom , dicom_test <EOL> R = [ [ <NUM_LIT:0> , - <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] <EOL> Z = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> T = [ <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT> ] <EOL> A = np . eye ( <NUM_LIT:4> ) <EOL> A [ : <NUM_LIT:3> , : <NUM_LIT:3> ] = np . array ( R ) * Z <EOL> A [ : <NUM_LIT:3> , <NUM_LIT:3> ] = T <EOL> class TestNifti1PairHeader ( tana . TestAnalyzeHeader , tspm . HeaderScalingMixin ) : <EOL> header_class = Nifti1PairHeader <EOL> example_file = header_file <EOL> quat_dtype = np . float32 <EOL> supported_np_types = tana . TestAnalyzeHeader . supported_np_types . union ( ( <EOL> np . int8 , <EOL> np . uint16 , <EOL> np . uint32 , <EOL> np . int64 , <EOL> np . uint64 , <EOL> np . complex128 ) ) <EOL> if have_binary128 ( ) : <EOL> supported_np_types = supported_np_types . union ( ( <EOL> np . longdouble , <EOL> np . longcomplex ) ) <EOL> tana . add_intp ( supported_np_types ) <EOL> def test_empty ( self ) : <EOL> tana . TestAnalyzeHeader . test_empty ( self ) <EOL> hdr = self . header_class ( ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , hdr . pair_magic ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> def test_from_eg_file ( self ) : <EOL> hdr = self . header_class . from_fileobj ( open ( self . example_file , '<STR_LIT:rb>' ) ) <EOL> assert_equal ( hdr . endianness , '<STR_LIT:<>' ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , hdr . pair_magic ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , self . sizeof_hdr ) <EOL> def test_data_scaling ( self ) : <EOL> super ( TestNifti1PairHeader , self ) . test_data_scaling ( ) <EOL> hdr = self . header_class ( ) <EOL> data = np . arange ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:0.5> ) . reshape ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> hdr . set_data_shape ( data . shape ) <EOL> hdr . set_data_dtype ( np . float32 ) <EOL> S = BytesIO ( ) <EOL> hdr . data_to_fileobj ( data , S , rescale = True ) <EOL> assert_array_equal ( hdr . get_slope_inter ( ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> rdata = hdr . data_from_fileobj ( S ) <EOL> assert_array_almost_equal ( data , rdata ) <EOL> hdr . set_data_dtype ( np . int8 ) <EOL> hdr . set_slope_inter ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> hdr . data_to_fileobj ( data , S , rescale = True ) <EOL> assert_false ( np . allclose ( hdr . get_slope_inter ( ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ) ) <EOL> rdata = hdr . data_from_fileobj ( S ) <EOL> assert_array_almost_equal ( data , rdata ) <EOL> hdr . set_slope_inter ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> with np . errstate ( invalid = '<STR_LIT:ignore>' ) : <EOL> hdr . data_to_fileobj ( data , S , rescale = False ) <EOL> assert_array_equal ( hdr . get_slope_inter ( ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> rdata = hdr . data_from_fileobj ( S ) <EOL> assert_array_almost_equal ( np . round ( data ) , rdata ) <EOL> def test_big_scaling ( self ) : <EOL> hdr = self . header_class ( ) <EOL> hdr . set_data_shape ( ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> hdr . set_data_dtype ( np . int16 ) <EOL> sio = BytesIO ( ) <EOL> dtt = np . float32 <EOL> finf = type_info ( dtt ) <EOL> data = np . array ( [ finf [ '<STR_LIT>' ] , finf [ '<STR_LIT>' ] ] , dtype = dtt ) [ : , None , None ] <EOL> hdr . data_to_fileobj ( data , sio ) <EOL> data_back = hdr . data_from_fileobj ( sio ) <EOL> assert_true ( np . allclose ( data , data_back ) ) <EOL> def test_slope_inter ( self ) : <EOL> hdr = self . header_class ( ) <EOL> nan , inf , minf = np . nan , np . inf , - np . inf <EOL> HDE = HeaderDataError <EOL> assert_equal ( hdr . get_slope_inter ( ) , ( <NUM_LIT:1.0> , <NUM_LIT:0.0> ) ) <EOL> for in_tup , exp_err , out_tup , raw_values in ( <EOL> ( ( None , None ) , None , ( None , None ) , ( nan , nan ) ) , <EOL> ( ( nan , None ) , None , ( None , None ) , ( nan , nan ) ) , <EOL> ( ( None , nan ) , None , ( None , None ) , ( nan , nan ) ) , <EOL> ( ( nan , nan ) , None , ( None , None ) , ( nan , nan ) ) , <EOL> ( ( None , <NUM_LIT:0> ) , HDE , ( None , None ) , ( nan , <NUM_LIT:0> ) ) , <EOL> ( ( nan , <NUM_LIT:0> ) , HDE , ( None , None ) , ( nan , <NUM_LIT:0> ) ) , <EOL> ( ( <NUM_LIT:1> , None ) , HDE , ( None , None ) , ( <NUM_LIT:1> , nan ) ) , <EOL> ( ( <NUM_LIT:1> , nan ) , HDE , ( None , None ) , ( <NUM_LIT:1> , nan ) ) , <EOL> ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , HDE , ( None , None ) , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) , <EOL> ( ( <NUM_LIT:0> , None ) , HDE , ( None , None ) , ( <NUM_LIT:0> , nan ) ) , <EOL> ( ( <NUM_LIT:0> , nan ) , HDE , ( None , None ) , ( <NUM_LIT:0> , nan ) ) , <EOL> ( ( <NUM_LIT:0> , inf ) , HDE , ( None , None ) , ( <NUM_LIT:0> , inf ) ) , <EOL> ( ( <NUM_LIT:0> , minf ) , HDE , ( None , None ) , ( <NUM_LIT:0> , minf ) ) , <EOL> ( ( inf , <NUM_LIT:0> ) , HDE , ( None , None ) , ( inf , <NUM_LIT:0> ) ) , <EOL> ( ( inf , None ) , HDE , ( None , None ) , ( inf , nan ) ) , <EOL> ( ( inf , nan ) , HDE , ( None , None ) , ( inf , nan ) ) , <EOL> ( ( inf , inf ) , HDE , ( None , None ) , ( inf , inf ) ) , <EOL> ( ( inf , minf ) , HDE , ( None , None ) , ( inf , minf ) ) , <EOL> ( ( minf , <NUM_LIT:0> ) , HDE , ( None , None ) , ( minf , <NUM_LIT:0> ) ) , <EOL> ( ( minf , None ) , HDE , ( None , None ) , ( minf , nan ) ) , <EOL> ( ( minf , nan ) , HDE , ( None , None ) , ( minf , nan ) ) , <EOL> ( ( minf , inf ) , HDE , ( None , None ) , ( minf , inf ) ) , <EOL> ( ( minf , minf ) , HDE , ( None , None ) , ( minf , minf ) ) , <EOL> ( ( <NUM_LIT:2> , None ) , HDE , HDE , ( <NUM_LIT:2> , nan ) ) , <EOL> ( ( <NUM_LIT:2> , nan ) , HDE , HDE , ( <NUM_LIT:2> , nan ) ) , <EOL> ( ( <NUM_LIT:2> , inf ) , HDE , HDE , ( <NUM_LIT:2> , inf ) ) , <EOL> ( ( <NUM_LIT:2> , minf ) , HDE , HDE , ( <NUM_LIT:2> , minf ) ) , <EOL> ( ( <NUM_LIT:2> , <NUM_LIT:0> ) , None , ( <NUM_LIT:2> , <NUM_LIT:0> ) , ( <NUM_LIT:2> , <NUM_LIT:0> ) ) , <EOL> ( ( <NUM_LIT:2> , <NUM_LIT:1> ) , None , ( <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:1> ) ) ) : <EOL> hdr = self . header_class ( ) <EOL> if not exp_err is None : <EOL> assert_raises ( exp_err , hdr . set_slope_inter , * in_tup ) <EOL> in_list = [ v if not v is None else np . nan for v in in_tup ] <EOL> hdr [ '<STR_LIT>' ] , hdr [ '<STR_LIT>' ] = in_list <EOL> else : <EOL> hdr . set_slope_inter ( * in_tup ) <EOL> if isinstance ( out_tup , Exception ) : <EOL> assert_raises ( out_tup , hdr . get_slope_inter ) <EOL> else : <EOL> assert_equal ( hdr . get_slope_inter ( ) , out_tup ) <EOL> hdr = self . header_class . from_header ( hdr , check = True ) <EOL> assert_equal ( hdr . get_slope_inter ( ) , out_tup ) <EOL> assert_array_equal ( [ hdr [ '<STR_LIT>' ] , hdr [ '<STR_LIT>' ] ] , <EOL> raw_values ) <EOL> def test_nifti_qsform_checks ( self ) : <EOL> HC = self . header_class <EOL> hdr = HC ( ) <EOL> hdr [ '<STR_LIT>' ] [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> fhdr , message , raiser = self . log_chk ( hdr , <NUM_LIT:20> ) <EOL> assert_equal ( fhdr [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> assert_equal ( message , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> hdr = HC ( ) <EOL> hdr [ '<STR_LIT>' ] = - <NUM_LIT:1> <EOL> fhdr , message , raiser = self . log_chk ( hdr , <NUM_LIT:30> ) <EOL> assert_equal ( fhdr [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( message , <EOL> '<STR_LIT>' ) <EOL> hdr = HC ( ) <EOL> hdr [ '<STR_LIT>' ] = - <NUM_LIT:1> <EOL> fhdr , message , raiser = self . log_chk ( hdr , <NUM_LIT:30> ) <EOL> assert_equal ( fhdr [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( message , <EOL> '<STR_LIT>' ) <EOL> def test_magic_offset_checks ( self ) : <EOL> HC = self . header_class <EOL> hdr = HC ( ) <EOL> hdr [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> fhdr , message , raiser = self . log_chk ( hdr , <NUM_LIT> ) <EOL> assert_equal ( fhdr [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> assert_equal ( message , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> svo = hdr . single_vox_offset <EOL> for magic , ok , bad_spm in ( ( hdr . pair_magic , <NUM_LIT:32> , <NUM_LIT> ) , <EOL> ( hdr . single_magic , svo + <NUM_LIT:32> , svo + <NUM_LIT> ) ) : <EOL> hdr [ '<STR_LIT>' ] = magic <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . assert_no_log_err ( hdr ) <EOL> hdr [ '<STR_LIT>' ] = ok <EOL> self . assert_no_log_err ( hdr ) <EOL> hdr [ '<STR_LIT>' ] = bad_spm <EOL> fhdr , message , raiser = self . log_chk ( hdr , <NUM_LIT:30> ) <EOL> assert_equal ( fhdr [ '<STR_LIT>' ] , bad_spm ) <EOL> assert_equal ( message , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:value>' . format ( bad_spm ) ) <EOL> hdr [ '<STR_LIT>' ] = hdr . single_magic <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:10> <EOL> fhdr , message , raiser = self . log_chk ( hdr , <NUM_LIT> ) <EOL> assert_equal ( fhdr [ '<STR_LIT>' ] , hdr . single_vox_offset ) <EOL> assert_equal ( message , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' + str ( hdr . single_vox_offset ) ) <EOL> def test_freesurfer_large_vector_hack ( self ) : <EOL> HC = self . header_class <EOL> hdr = HC ( ) <EOL> hdr . set_data_shape ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> assert_equal ( hdr . get_data_shape ( ) , ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> dim_type = hdr . template_dtype [ '<STR_LIT>' ] . base <EOL> glmin = hdr . template_dtype [ '<STR_LIT>' ] . base <EOL> too_big = int ( np . iinfo ( dim_type ) . max ) + <NUM_LIT:1> <EOL> hdr . set_data_shape ( ( too_big - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> assert_equal ( hdr . get_data_shape ( ) , ( too_big - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> full_shape = ( too_big , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> for dim in range ( <NUM_LIT:3> , <NUM_LIT:8> ) : <EOL> expected_dim = np . array ( [ dim , - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> with suppress_warnings ( ) : <EOL> hdr . set_data_shape ( full_shape [ : dim ] ) <EOL> assert_equal ( hdr . get_data_shape ( ) , full_shape [ : dim ] ) <EOL> assert_array_equal ( hdr [ '<STR_LIT>' ] , expected_dim ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , too_big ) <EOL> with suppress_warnings ( ) : <EOL> hdr . set_data_shape ( ( too_big , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:4> ) ) <EOL> assert_equal ( hdr . get_data_shape ( ) , ( too_big , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:4> ) ) <EOL> assert_array_equal ( hdr [ '<STR_LIT>' ] [ : <NUM_LIT:5> ] , np . array ( [ <NUM_LIT:4> , - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:4> ] ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( too_big , ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( too_big , <NUM_LIT:1> ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( too_big , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( too_big , <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( <NUM_LIT:1> , too_big ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( <NUM_LIT:1> , too_big , <NUM_LIT:1> ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( <NUM_LIT:1> , <NUM_LIT:1> , too_big ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , too_big ) ) <EOL> far_too_big = int ( np . iinfo ( glmin ) . max ) + <NUM_LIT:1> <EOL> with suppress_warnings ( ) : <EOL> hdr . set_data_shape ( ( far_too_big - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> assert_equal ( hdr . get_data_shape ( ) , ( far_too_big - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( far_too_big , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> hdr . set_data_shape ( ( - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> assert_raises ( HeaderDataError , hdr . get_data_shape ) <EOL> for shape in ( ( too_big - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( too_big , <NUM_LIT:1> , <NUM_LIT:1> ) ) : <EOL> for constructor in ( list , tuple , np . array ) : <EOL> with suppress_warnings ( ) : <EOL> hdr . set_data_shape ( constructor ( shape ) ) <EOL> assert_equal ( hdr . get_data_shape ( ) , shape ) <EOL> @ needs_nibabel_data ( '<STR_LIT>' ) <EOL> def test_freesurfer_ico7_hack ( self ) : <EOL> HC = self . header_class <EOL> hdr = HC ( ) <EOL> full_shape = ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> for dim in range ( <NUM_LIT:3> , <NUM_LIT:8> ) : <EOL> expected_dim = np . array ( [ dim , <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:6> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> hdr . set_data_shape ( full_shape [ : dim ] ) <EOL> assert_equal ( hdr . get_data_shape ( ) , full_shape [ : dim ] ) <EOL> assert_array_equal ( hdr . _structarr [ '<STR_LIT>' ] , expected_dim ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , full_shape [ : <NUM_LIT:1> ] ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , full_shape [ : <NUM_LIT:2> ] ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> assert_raises ( HeaderDataError , hdr . set_data_shape , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> nitest_path = os . path . join ( get_nibabel_data ( ) , '<STR_LIT>' ) <EOL> mgh = mghload ( os . path . join ( nitest_path , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> nii = load ( os . path . join ( nitest_path , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> assert_equal ( mgh . shape , nii . shape ) <EOL> assert_array_equal ( mgh . get_data ( ) , nii . get_data ( ) ) <EOL> assert_array_equal ( nii . header . _structarr [ '<STR_LIT>' ] [ <NUM_LIT:1> : <NUM_LIT:4> ] , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:6> ] ) ) <EOL> with InTemporaryDirectory ( ) : <EOL> nii . to_filename ( '<STR_LIT>' ) <EOL> nii2 = load ( '<STR_LIT>' ) <EOL> assert_equal ( nii . shape , nii2 . shape ) <EOL> assert_array_equal ( nii . get_data ( ) , nii2 . get_data ( ) ) <EOL> assert_array_equal ( nii . get_affine ( ) , nii2 . get_affine ( ) ) <EOL> def test_qform_sform ( self ) : <EOL> HC = self . header_class <EOL> hdr = HC ( ) <EOL> assert_array_equal ( hdr . get_qform ( ) , np . eye ( <NUM_LIT:4> ) ) <EOL> empty_sform = np . zeros ( ( <NUM_LIT:4> , <NUM_LIT:4> ) ) <EOL> empty_sform [ - <NUM_LIT:1> , - <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> assert_array_equal ( hdr . get_sform ( ) , empty_sform ) <EOL> assert_equal ( hdr . get_qform ( coded = True ) , ( None , <NUM_LIT:0> ) ) <EOL> assert_equal ( hdr . get_sform ( coded = True ) , ( None , <NUM_LIT:0> ) ) <EOL> nice_aff = np . diag ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> another_aff = np . diag ( [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:1> ] ) <EOL> nasty_aff = from_matvec ( np . arange ( <NUM_LIT:9> ) . reshape ( ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , [ <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:11> ] ) <EOL> fixed_aff = unshear_44 ( nasty_aff ) <EOL> for in_meth , out_meth in ( ( hdr . set_qform , hdr . get_qform ) , <EOL> ( hdr . set_sform , hdr . get_sform ) ) : <EOL> in_meth ( nice_aff , <NUM_LIT:2> ) <EOL> aff , code = out_meth ( coded = True ) <EOL> assert_array_equal ( aff , nice_aff ) <EOL> assert_equal ( code , <NUM_LIT:2> ) <EOL> assert_array_equal ( out_meth ( ) , nice_aff ) <EOL> in_meth ( another_aff , <NUM_LIT:0> ) <EOL> assert_equal ( out_meth ( coded = True ) , ( None , <NUM_LIT:0> ) ) <EOL> assert_array_almost_equal ( out_meth ( ) , another_aff ) <EOL> in_meth ( nice_aff ) <EOL> aff , code = out_meth ( coded = True ) <EOL> assert_equal ( code , <NUM_LIT:2> ) <EOL> in_meth ( nice_aff , <NUM_LIT:1> ) <EOL> in_meth ( nice_aff ) <EOL> aff , code = out_meth ( coded = True ) <EOL> assert_equal ( code , <NUM_LIT:1> ) <EOL> assert_array_equal ( aff , nice_aff ) <EOL> in_meth ( None , <NUM_LIT:3> ) <EOL> aff , code = out_meth ( coded = True ) <EOL> assert_array_equal ( aff , nice_aff ) <EOL> assert_equal ( code , <NUM_LIT:3> ) <EOL> in_meth ( None , <NUM_LIT:0> ) <EOL> assert_equal ( out_meth ( coded = True ) , ( None , <NUM_LIT:0> ) ) <EOL> in_meth ( None ) <EOL> assert_equal ( out_meth ( coded = True ) , ( None , <NUM_LIT:0> ) ) <EOL> in_meth ( nice_aff . tolist ( ) ) <EOL> assert_array_equal ( out_meth ( ) , nice_aff ) <EOL> hdr . set_qform ( nasty_aff , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( hdr . get_qform ( ) , fixed_aff ) <EOL> assert_raises ( HeaderDataError , hdr . set_qform , nasty_aff , <NUM_LIT:1> , False ) <EOL> hdr . set_sform ( None ) <EOL> hdr . set_qform ( nice_aff , <NUM_LIT:1> ) <EOL> assert_equal ( hdr . get_sform ( coded = True ) , ( None , <NUM_LIT:0> ) ) <EOL> hdr . set_sform ( nasty_aff , <NUM_LIT:1> ) <EOL> aff , code = hdr . get_sform ( coded = True ) <EOL> assert_array_equal ( aff , nasty_aff ) <EOL> assert_equal ( code , <NUM_LIT:1> ) <EOL> def test_datatypes ( self ) : <EOL> hdr = self . header_class ( ) <EOL> for code in data_type_codes . value_set ( ) : <EOL> dt = data_type_codes . type [ code ] <EOL> if dt == np . void : <EOL> continue <EOL> hdr . set_data_dtype ( code ) <EOL> ( assert_equal , <EOL> hdr . get_data_dtype ( ) , <EOL> data_type_codes . dtype [ code ] ) <EOL> hdr . set_data_dtype ( np . complex128 ) <EOL> hdr . check_fix ( ) <EOL> def test_quaternion ( self ) : <EOL> hdr = self . header_class ( ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> assert_true ( np . allclose ( hdr . get_qform_quaternion ( ) , [ <NUM_LIT:1.0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> assert_true ( np . allclose ( hdr . get_qform_quaternion ( ) , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:1> + np . finfo ( self . quat_dtype ) . eps <EOL> assert_array_almost_equal ( hdr . get_qform_quaternion ( ) , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> def test_qform ( self ) : <EOL> ehdr = self . header_class ( ) <EOL> ehdr . set_qform ( A ) <EOL> qA = ehdr . get_qform ( ) <EOL> assert_true , np . allclose ( A , qA , atol = <NUM_LIT> ) <EOL> assert_true , np . allclose ( Z , ehdr [ '<STR_LIT>' ] [ <NUM_LIT:1> : <NUM_LIT:4> ] ) <EOL> xfas = nifti1 . xform_codes <EOL> assert_true , ehdr [ '<STR_LIT>' ] == xfas [ '<STR_LIT>' ] <EOL> ehdr . set_qform ( A , '<STR_LIT>' ) <EOL> assert_true , ehdr [ '<STR_LIT>' ] == xfas [ '<STR_LIT>' ] <EOL> ehdr . set_qform ( A , xfas [ '<STR_LIT>' ] ) <EOL> assert_true , ehdr [ '<STR_LIT>' ] == xfas [ '<STR_LIT>' ] <EOL> for dims in ( ( - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> ) ) : <EOL> ehdr [ '<STR_LIT>' ] [ <NUM_LIT:1> : <NUM_LIT:4> ] = dims <EOL> assert_raises ( HeaderDataError , ehdr . get_qform ) <EOL> def test_sform ( self ) : <EOL> ehdr = self . header_class ( ) <EOL> ehdr . set_sform ( A ) <EOL> sA = ehdr . get_sform ( ) <EOL> assert_true , np . allclose ( A , sA , atol = <NUM_LIT> ) <EOL> xfas = nifti1 . xform_codes <EOL> assert_true , ehdr [ '<STR_LIT>' ] == xfas [ '<STR_LIT>' ] <EOL> ehdr . set_sform ( A , '<STR_LIT>' ) <EOL> assert_true , ehdr [ '<STR_LIT>' ] == xfas [ '<STR_LIT>' ] <EOL> ehdr . set_sform ( A , xfas [ '<STR_LIT>' ] ) <EOL> assert_true , ehdr [ '<STR_LIT>' ] == xfas [ '<STR_LIT>' ] <EOL> def test_dim_info ( self ) : <EOL> ehdr = self . header_class ( ) <EOL> assert_true ( ehdr . get_dim_info ( ) == ( None , None , None ) ) <EOL> for info in ( ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> ) , <EOL> ( None , None , None ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:2> , None ) , <EOL> ( <NUM_LIT:0> , None , None ) , <EOL> ( None , <NUM_LIT:2> , <NUM_LIT:1> ) , <EOL> ( None , None , <NUM_LIT:1> ) , <EOL> ) : <EOL> ehdr . set_dim_info ( * info ) <EOL> assert_true ( ehdr . get_dim_info ( ) == info ) <EOL> def test_slice_times ( self ) : <EOL> hdr = self . header_class ( ) <EOL> assert_raises ( HeaderDataError , hdr . get_slice_times ) <EOL> hdr . set_dim_info ( slice = <NUM_LIT:2> ) <EOL> assert_raises ( HeaderDataError , hdr . get_slice_times ) <EOL> hdr . set_data_shape ( ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:7> ) ) <EOL> assert_raises ( HeaderDataError , hdr . get_slice_times ) <EOL> hdr . set_slice_duration ( <NUM_LIT:0.1> ) <EOL> _stringer = lambda val : val is not None and '<STR_LIT>' % val or None <EOL> _print_me = lambda s : list ( map ( _stringer , s ) ) <EOL> hdr [ '<STR_LIT>' ] = slice_order_codes [ '<STR_LIT>' ] <EOL> assert_equal ( _print_me ( hdr . get_slice_times ( ) ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> assert_equal ( _print_me ( hdr . get_slice_times ( ) ) , <EOL> [ None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ] ) <EOL> hdr [ '<STR_LIT>' ] = slice_order_codes [ '<STR_LIT>' ] <EOL> assert_equal ( _print_me ( hdr . get_slice_times ( ) ) , <EOL> [ None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ] ) <EOL> hdr [ '<STR_LIT>' ] = slice_order_codes [ '<STR_LIT>' ] <EOL> assert_equal ( _print_me ( hdr . get_slice_times ( ) ) , <EOL> [ None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ] ) <EOL> hdr [ '<STR_LIT>' ] = slice_order_codes [ '<STR_LIT>' ] <EOL> assert_equal ( _print_me ( hdr . get_slice_times ( ) ) , <EOL> [ None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ] ) <EOL> hdr [ '<STR_LIT>' ] = slice_order_codes [ '<STR_LIT>' ] <EOL> assert_equal ( _print_me ( hdr . get_slice_times ( ) ) , <EOL> [ None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ] ) <EOL> hdr [ '<STR_LIT>' ] = slice_order_codes [ '<STR_LIT>' ] <EOL> assert_equal ( _print_me ( hdr . get_slice_times ( ) ) , <EOL> [ None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , None ] ) <EOL> hdr = self . header_class ( ) <EOL> hdr . set_dim_info ( slice = <NUM_LIT:2> ) <EOL> times = [ None , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.0> , None ] <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , times ) <EOL> hdr . set_data_shape ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:7> ] ) <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , <EOL> times [ : - <NUM_LIT:1> ] ) <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , <EOL> ( None , ) * len ( times ) ) <EOL> n_mid_times = times [ : ] <EOL> n_mid_times [ <NUM_LIT:3> ] = None <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , <EOL> n_mid_times ) <EOL> funny_times = times [ : ] <EOL> funny_times [ <NUM_LIT:3> ] = <NUM_LIT> <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , <EOL> funny_times ) <EOL> hdr . set_slice_times ( times ) <EOL> assert_equal ( hdr . get_value_label ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:5> ) <EOL> assert_array_almost_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:0.1> ) <EOL> def test_intents ( self ) : <EOL> ehdr = self . header_class ( ) <EOL> ehdr . set_intent ( '<STR_LIT>' , ( <NUM_LIT:10> , ) , name = '<STR_LIT>' ) <EOL> assert_equal ( ehdr . get_intent ( ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT> , ) , '<STR_LIT>' ) ) <EOL> assert_raises ( KeyError , ehdr . set_intent , '<STR_LIT>' ) <EOL> assert_raises ( HeaderDataError , ehdr . set_intent , '<STR_LIT>' , ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> assert_raises ( HeaderDataError , ehdr . set_intent , '<STR_LIT>' , ( <NUM_LIT:10> , ) ) <EOL> ehdr . set_intent ( '<STR_LIT>' ) <EOL> assert_equal ( ( ehdr [ '<STR_LIT>' ] , ehdr [ '<STR_LIT>' ] , ehdr [ '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> assert_equal ( ehdr [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> ehdr . set_intent ( '<STR_LIT>' , ( <NUM_LIT:10> , ) ) <EOL> assert_equal ( ( ehdr [ '<STR_LIT>' ] , ehdr [ '<STR_LIT>' ] ) , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> def test_set_slice_times ( self ) : <EOL> hdr = self . header_class ( ) <EOL> hdr . set_dim_info ( slice = <NUM_LIT:2> ) <EOL> hdr . set_data_shape ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:7> ] ) <EOL> hdr . set_slice_duration ( <NUM_LIT:0.1> ) <EOL> times = [ <NUM_LIT:0> ] * <NUM_LIT:6> <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , times ) <EOL> times = [ None ] * <NUM_LIT:7> <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , times ) <EOL> times = [ None , <NUM_LIT:0> , <NUM_LIT:1> , None , <NUM_LIT:3> , <NUM_LIT:4> , None ] <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , times ) <EOL> times = [ None , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:4> , None ] <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , times ) <EOL> times = [ None , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> , None ] <EOL> assert_raises ( HeaderDataError , hdr . set_slice_times , times ) <EOL> times = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] <EOL> hdr . set_slice_times ( times ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:6> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1.0> ) <EOL> times = [ None , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , None ] <EOL> hdr . set_slice_times ( times ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:5> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1.0> ) <EOL> times = [ None , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT:0> , None ] <EOL> hdr . set_slice_times ( times ) <EOL> assert_true ( np . allclose ( hdr [ '<STR_LIT>' ] , <NUM_LIT:0.1> ) ) <EOL> times = [ None , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> , None ] <EOL> hdr . set_slice_times ( times ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> times = [ None , <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:2> , None ] <EOL> hdr . set_slice_times ( times ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:3> ) <EOL> times = [ None , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:0> , None ] <EOL> hdr . set_slice_times ( times ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:4> ) <EOL> times = [ None , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:4> , None ] <EOL> hdr . set_slice_times ( times ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:5> ) <EOL> times = [ None , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:2> , None ] <EOL> hdr . set_slice_times ( times ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:6> ) <EOL> def test_xyzt_units ( self ) : <EOL> hdr = self . header_class ( ) <EOL> assert_equal ( hdr . get_xyzt_units ( ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> hdr . set_xyzt_units ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert_equal ( hdr . get_xyzt_units ( ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> hdr . set_xyzt_units ( ) <EOL> assert_equal ( hdr . get_xyzt_units ( ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_recoded_fields ( self ) : <EOL> hdr = self . header_class ( ) <EOL> assert_equal ( hdr . get_value_label ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:3> <EOL> assert_equal ( hdr . get_value_label ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> assert_equal ( hdr . get_value_label ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:3> <EOL> assert_equal ( hdr . get_value_label ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> assert_equal ( hdr . get_value_label ( '<STR_LIT>' ) , '<STR_LIT:none>' ) <EOL> hdr . set_intent ( '<STR_LIT>' , ( <NUM_LIT:10> , ) , name = '<STR_LIT>' ) <EOL> assert_equal ( hdr . get_value_label ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> assert_equal ( hdr . get_value_label ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:4> <EOL> assert_equal ( hdr . get_value_label ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> def unshear_44 ( affine ) : <EOL> RZS = affine [ : <NUM_LIT:3> , : <NUM_LIT:3> ] <EOL> zooms = np . sqrt ( np . sum ( RZS * RZS , axis = <NUM_LIT:0> ) ) <EOL> R = RZS / zooms <EOL> P , S , Qs = np . linalg . svd ( R ) <EOL> PR = np . dot ( P , Qs ) <EOL> return from_matvec ( PR * zooms , affine [ : <NUM_LIT:3> , <NUM_LIT:3> ] ) <EOL> class TestNifti1SingleHeader ( TestNifti1PairHeader ) : <EOL> header_class = Nifti1Header <EOL> def test_empty ( self ) : <EOL> tana . TestAnalyzeHeader . test_empty ( self ) <EOL> hdr = self . header_class ( ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , hdr . single_magic ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> def test_binblock_is_file ( self ) : <EOL> hdr = self . header_class ( ) <EOL> str_io = BytesIO ( ) <EOL> hdr . write_to ( str_io ) <EOL> assert_equal ( str_io . getvalue ( ) , hdr . binaryblock + b'<STR_LIT:\x00>' * <NUM_LIT:4> ) <EOL> def test_float128 ( self ) : <EOL> hdr = self . header_class ( ) <EOL> ld_dt = np . dtype ( np . longdouble ) <EOL> if have_binary128 ( ) or ld_dt == np . dtype ( np . float64 ) : <EOL> hdr . set_data_dtype ( np . longdouble ) <EOL> assert_equal ( hdr . get_data_dtype ( ) , ld_dt ) <EOL> else : <EOL> assert_raises ( HeaderDataError , hdr . set_data_dtype , np . longdouble ) <EOL> class TestNifti1Pair ( tana . TestAnalyzeImage , tspm . ImageScalingMixin ) : <EOL> image_class = Nifti1Pair <EOL> supported_np_types = TestNifti1PairHeader . supported_np_types <EOL> def test_none_qsform ( self ) : <EOL> img_klass = self . image_class <EOL> hdr_klass = img_klass . header_class <EOL> shape = ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> data = np . arange ( <NUM_LIT> ) . reshape ( shape ) <EOL> aff = from_matvec ( euler2mat ( <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> ) , [ <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT> ] ) <EOL> for hdr in ( None , hdr_klass ( ) ) : <EOL> img = img_klass ( data , aff , hdr ) <EOL> assert_almost_equal ( img . affine , aff ) <EOL> assert_almost_equal ( img . header . get_sform ( ) , aff ) <EOL> assert_almost_equal ( img . header . get_qform ( ) , aff ) <EOL> hdr = hdr_klass ( ) <EOL> hdr . set_data_shape ( shape ) <EOL> default_aff = hdr . get_best_affine ( ) <EOL> img = img_klass ( data , default_aff , None ) <EOL> assert_almost_equal ( img . header . get_sform ( ) , default_aff ) <EOL> assert_almost_equal ( img . header . get_qform ( ) , default_aff ) <EOL> img = img_klass ( data , None , None ) <EOL> assert_almost_equal ( img . header . get_sform ( ) , np . diag ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ) ) <EOL> assert_almost_equal ( img . header . get_qform ( ) , np . eye ( <NUM_LIT:4> ) ) <EOL> def _qform_rt ( self , img ) : <EOL> hdr = img . header <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:3> <EOL> hdr [ '<STR_LIT>' ] = <NUM_LIT:4> <EOL> for key , value in img . file_map . items ( ) : <EOL> value . fileobj = BytesIO ( ) <EOL> img . to_file_map ( ) <EOL> return img . from_file_map ( img . file_map ) <EOL> def test_qform_cycle ( self ) : <EOL> img_klass = self . image_class <EOL> img = img_klass ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) , None ) <EOL> hdr_back = self . _qform_rt ( img ) . header <EOL> assert_equal ( hdr_back [ '<STR_LIT>' ] , <NUM_LIT:3> ) <EOL> assert_equal ( hdr_back [ '<STR_LIT>' ] , <NUM_LIT:4> ) <EOL> img = img_klass ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) , np . eye ( <NUM_LIT:4> ) ) <EOL> hdr_back = self . _qform_rt ( img ) . header <EOL> assert_equal ( hdr_back [ '<STR_LIT>' ] , <NUM_LIT:3> ) <EOL> assert_equal ( hdr_back [ '<STR_LIT>' ] , <NUM_LIT:4> ) <EOL> img . affine [ <NUM_LIT:0> , <NUM_LIT:0> ] = <NUM_LIT:9> <EOL> img . to_file_map ( ) <EOL> img_back = img . from_file_map ( img . file_map ) <EOL> exp_aff = np . diag ( [ <NUM_LIT:9> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> assert_array_equal ( img_back . affine , exp_aff ) <EOL> hdr_back = img . header <EOL> assert_array_equal ( hdr_back . get_sform ( ) , exp_aff ) <EOL> assert_array_equal ( hdr_back . get_qform ( ) , exp_aff ) <EOL> def test_header_update_affine ( self ) : <EOL> img = self . image_class ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) , np . eye ( <NUM_LIT:4> ) ) <EOL> hdr = img . header <EOL> aff = img . affine <EOL> aff [ : ] = np . diag ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] ) <EOL> hdr . set_qform ( aff , <NUM_LIT:2> ) <EOL> hdr . set_sform ( aff , <NUM_LIT:2> ) <EOL> img . update_header ( ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> def test_set_qform ( self ) : <EOL> img = self . image_class ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) , <EOL> np . diag ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] ) ) <EOL> hdr = img . header <EOL> new_affine = np . diag ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] ) <EOL> assert_array_almost_equal ( img . affine , hdr . get_best_affine ( ) ) <EOL> aff_affine = np . diag ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] ) <EOL> img . affine [ : ] = aff_affine <EOL> assert_array_almost_equal ( img . affine , aff_affine ) <EOL> img . set_qform ( new_affine , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( img . get_qform ( ) , new_affine ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( img . get_qform ( ) , new_affine ) <EOL> qaff , code = img . get_qform ( coded = True ) <EOL> assert_equal ( code , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( qaff , new_affine ) <EOL> assert_array_almost_equal ( img . affine , hdr . get_best_affine ( ) ) <EOL> img . affine [ : ] = aff_affine <EOL> img . set_qform ( new_affine , <NUM_LIT:1> , update_affine = False ) <EOL> assert_array_almost_equal ( img . affine , aff_affine ) <EOL> assert_array_almost_equal ( hdr . get_zooms ( ) , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> img . set_qform ( None ) <EOL> qaff , code = img . get_qform ( coded = True ) <EOL> assert_equal ( ( qaff , code ) , ( None , <NUM_LIT:0> ) ) <EOL> assert_array_almost_equal ( hdr . get_zooms ( ) , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> assert_array_almost_equal ( img . affine , hdr . get_best_affine ( ) ) <EOL> img . set_sform ( None ) <EOL> img . set_qform ( new_affine , <NUM_LIT:1> ) <EOL> qaff , code = img . get_qform ( coded = True ) <EOL> assert_equal ( code , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( img . affine , new_affine ) <EOL> new_affine [ <NUM_LIT:0> , <NUM_LIT:1> ] = <NUM_LIT:2> <EOL> img . set_qform ( new_affine , <NUM_LIT:2> ) <EOL> assert_raises ( HeaderDataError , img . set_qform , new_affine , <NUM_LIT:2> , False ) <EOL> assert_raises ( TypeError , img . get_qform , strange = True ) <EOL> img = self . image_class ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) , None ) <EOL> new_affine = np . eye ( <NUM_LIT:4> ) <EOL> img . set_qform ( new_affine , <NUM_LIT:2> ) <EOL> assert_array_almost_equal ( img . affine , img . header . get_best_affine ( ) ) <EOL> img . set_sform ( None , update_affine = True ) <EOL> assert_array_almost_equal ( img . affine , new_affine ) <EOL> def test_set_sform ( self ) : <EOL> orig_aff = np . diag ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] ) <EOL> img = self . image_class ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) , orig_aff ) <EOL> hdr = img . header <EOL> new_affine = np . diag ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] ) <EOL> qform_affine = np . diag ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] ) <EOL> aff_affine = np . diag ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ] ) <EOL> img . affine [ : ] = aff_affine <EOL> assert_array_almost_equal ( img . affine , aff_affine ) <EOL> assert_equal ( ( hdr [ '<STR_LIT>' ] , hdr [ '<STR_LIT>' ] ) , ( <NUM_LIT:2> , <NUM_LIT:0> ) ) <EOL> img . set_sform ( new_affine , <NUM_LIT:1> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( hdr . get_sform ( ) , new_affine ) <EOL> assert_array_almost_equal ( img . get_sform ( ) , new_affine ) <EOL> saff , code = img . get_sform ( coded = True ) <EOL> assert_equal ( code , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( saff , new_affine ) <EOL> assert_array_almost_equal ( img . affine , hdr . get_best_affine ( ) ) <EOL> img . affine [ : ] = aff_affine <EOL> img . set_sform ( new_affine , <NUM_LIT:1> , update_affine = False ) <EOL> assert_array_almost_equal ( img . affine , aff_affine ) <EOL> assert_array_almost_equal ( img . get_qform ( ) , orig_aff ) <EOL> assert_array_almost_equal ( hdr . get_zooms ( ) , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> img . set_qform ( None ) <EOL> assert_array_almost_equal ( hdr . get_zooms ( ) , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> img . set_qform ( qform_affine , <NUM_LIT:1> ) <EOL> img . set_sform ( new_affine , <NUM_LIT:1> ) <EOL> saff , code = img . get_sform ( coded = True ) <EOL> assert_equal ( code , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( saff , new_affine ) <EOL> assert_array_almost_equal ( img . affine , new_affine ) <EOL> assert_array_almost_equal ( hdr . get_zooms ( ) , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> img . set_sform ( None ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( hdr . get_sform ( ) , saff ) <EOL> assert_array_almost_equal ( img . affine , qform_affine ) <EOL> assert_array_almost_equal ( hdr . get_best_affine ( ) , img . affine ) <EOL> assert_raises ( TypeError , img . get_sform , strange = True ) <EOL> img = self . image_class ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) , None ) <EOL> new_affine = np . eye ( <NUM_LIT:4> ) <EOL> img . set_sform ( new_affine , <NUM_LIT:2> ) <EOL> assert_array_almost_equal ( img . affine , new_affine ) <EOL> def test_hdr_diff ( self ) : <EOL> img = self . image_class ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) , np . eye ( <NUM_LIT:4> ) ) <EOL> ext = dict ( img . files_types ) [ '<STR_LIT:image>' ] <EOL> hdr_len = len ( img . header . binaryblock ) <EOL> img . header [ '<STR_LIT>' ] = hdr_len + <NUM_LIT> <EOL> with InTemporaryDirectory ( ) : <EOL> img . to_filename ( '<STR_LIT>' + ext ) <EOL> def test_load_save ( self ) : <EOL> IC = self . image_class <EOL> img_ext = IC . files_types [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> shape = ( <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> ) <EOL> npt = np . float32 <EOL> data = np . arange ( np . prod ( shape ) , dtype = npt ) . reshape ( shape ) <EOL> affine = np . diag ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> img = IC ( data , affine ) <EOL> assert_equal ( img . header . get_data_offset ( ) , <NUM_LIT:0> ) <EOL> assert_equal ( img . shape , shape ) <EOL> img . set_data_dtype ( npt ) <EOL> img2 = bytesio_round_trip ( img ) <EOL> assert_array_equal ( img2 . get_data ( ) , data ) <EOL> with InTemporaryDirectory ( ) as tmpdir : <EOL> for ext in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> fname = os . path . join ( tmpdir , '<STR_LIT:test>' + img_ext + ext ) <EOL> img . to_filename ( fname ) <EOL> img3 = IC . load ( fname ) <EOL> assert_true ( isinstance ( img3 , img . __class__ ) ) <EOL> assert_array_equal ( img3 . get_data ( ) , data ) <EOL> assert_equal ( img3 . header , img . header ) <EOL> assert_true ( isinstance ( img3 . get_data ( ) , <EOL> np . memmap if ext == '<STR_LIT>' else np . ndarray ) ) <EOL> del img3 <EOL> def test_load_pixdims ( self ) : <EOL> IC = self . image_class <EOL> HC = IC . header_class <EOL> arr = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> qaff = np . diag ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> saff = np . diag ( [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:1> ] ) <EOL> hdr = HC ( ) <EOL> hdr . set_qform ( qaff ) <EOL> assert_array_equal ( hdr . get_qform ( ) , qaff ) <EOL> hdr . set_sform ( saff ) <EOL> assert_array_equal ( hdr . get_sform ( ) , saff ) <EOL> simg = IC ( arr , None , hdr ) <EOL> img_hdr = simg . header <EOL> assert_array_equal ( img_hdr . get_qform ( ) , qaff ) <EOL> assert_array_equal ( img_hdr . get_sform ( ) , saff ) <EOL> assert_array_equal ( img_hdr . get_zooms ( ) , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> re_simg = bytesio_round_trip ( simg ) <EOL> assert_array_equal ( re_simg . get_data ( ) , arr ) <EOL> rimg_hdr = re_simg . header <EOL> assert_array_equal ( rimg_hdr . get_qform ( ) , qaff ) <EOL> assert_array_equal ( rimg_hdr . get_sform ( ) , saff ) <EOL> assert_array_equal ( rimg_hdr . get_zooms ( ) , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> def test_affines_init ( self ) : <EOL> IC = self . image_class <EOL> arr = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> aff = np . diag ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> img = IC ( arr , aff ) <EOL> hdr = img . header <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( hdr [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> assert_array_equal ( hdr . get_zooms ( ) , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> qaff = np . diag ( [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:1> ] ) <EOL> saff = np . diag ( [ <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:1> ] ) <EOL> hdr . set_qform ( qaff , code = '<STR_LIT>' ) <EOL> hdr . set_sform ( saff , code = '<STR_LIT>' ) <EOL> assert_array_equal ( hdr . get_zooms ( ) , [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> img = IC ( arr , aff , hdr ) <EOL> new_hdr = img . header <EOL> assert_equal ( new_hdr [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( new_hdr [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> assert_array_equal ( new_hdr . get_sform ( ) , aff ) <EOL> assert_array_equal ( new_hdr . get_zooms ( ) , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> img = IC ( arr , None , hdr ) <EOL> new_hdr = img . header <EOL> assert_equal ( new_hdr [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> assert_array_equal ( new_hdr . get_qform ( ) , qaff ) <EOL> assert_equal ( new_hdr [ '<STR_LIT>' ] , <NUM_LIT:3> ) <EOL> assert_array_equal ( new_hdr . get_sform ( ) , saff ) <EOL> assert_array_equal ( new_hdr . get_zooms ( ) , [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> def test_read_no_extensions ( self ) : <EOL> IC = self . image_class <EOL> arr = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> img = IC ( arr , np . eye ( <NUM_LIT:4> ) ) <EOL> assert_equal ( len ( img . header . extensions ) , <NUM_LIT:0> ) <EOL> img_rt = bytesio_round_trip ( img ) <EOL> assert_equal ( len ( img_rt . header . extensions ) , <NUM_LIT:0> ) <EOL> img . header . set_data_offset ( <NUM_LIT> ) <EOL> img_rt = bytesio_round_trip ( img ) <EOL> assert_equal ( len ( img_rt . header . extensions ) , <NUM_LIT:0> ) <EOL> def _get_raw_scaling ( self , hdr ) : <EOL> return hdr [ '<STR_LIT>' ] , hdr [ '<STR_LIT>' ] <EOL> def _set_raw_scaling ( self , hdr , slope , inter ) : <EOL> hdr [ '<STR_LIT>' ] = slope <EOL> hdr [ '<STR_LIT>' ] = inter <EOL> def test_write_scaling ( self ) : <EOL> for slope , inter , e_slope , e_inter in ( <EOL> ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) , <EOL> ( np . inf , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) : <EOL> with np . errstate ( invalid = '<STR_LIT:ignore>' ) : <EOL> self . _check_write_scaling ( slope , inter , e_slope , e_inter ) <EOL> class TestNifti1Image ( TestNifti1Pair ) : <EOL> image_class = Nifti1Image <EOL> def test_offset_errors ( self ) : <EOL> IC = self . image_class <EOL> arr = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> img = IC ( arr , np . eye ( <NUM_LIT:4> ) ) <EOL> assert_equal ( img . header . get_data_offset ( ) , <NUM_LIT:0> ) <EOL> img_rt = bytesio_round_trip ( img ) <EOL> assert_equal ( img_rt . header . get_data_offset ( ) , <NUM_LIT:0> ) <EOL> fm = bytesio_filemap ( IC ) <EOL> img . header . set_data_offset ( <NUM_LIT:16> ) <EOL> assert_raises ( HeaderDataError , img . to_file_map , fm ) <EOL> def test_extension_basics ( ) : <EOL> raw = '<STR_LIT>' <EOL> ext = Nifti1Extension ( '<STR_LIT>' , raw ) <EOL> assert_true ( ext . get_sizeondisk ( ) == <NUM_LIT:16> ) <EOL> assert_true ( ext . get_content ( ) == raw ) <EOL> assert_true ( ext . get_code ( ) == <NUM_LIT:6> ) <EOL> def test_ext_eq ( ) : <EOL> ext = Nifti1Extension ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert_true ( ext == ext ) <EOL> assert_false ( ext != ext ) <EOL> ext2 = Nifti1Extension ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert_false ( ext == ext2 ) <EOL> assert_true ( ext != ext2 ) <EOL> def test_extension_codes ( ) : <EOL> for k in extension_codes . keys ( ) : <EOL> Nifti1Extension ( k , '<STR_LIT>' ) <EOL> def test_extension_list ( ) : <EOL> ext_c0 = Nifti1Extensions ( ) <EOL> ext_c1 = Nifti1Extensions ( ) <EOL> assert_equal ( ext_c0 , ext_c1 ) <EOL> ext = Nifti1Extension ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ext_c1 . append ( ext ) <EOL> assert_false ( ext_c0 == ext_c1 ) <EOL> ext_c0 . append ( ext ) <EOL> assert_true ( ext_c0 == ext_c1 ) <EOL> def test_extension_io ( ) : <EOL> bio = BytesIO ( ) <EOL> ext1 = Nifti1Extension ( <NUM_LIT:6> , b'<STR_LIT>' ) <EOL> ext1 . write_to ( bio , False ) <EOL> bio . seek ( <NUM_LIT:0> ) <EOL> ebacks = Nifti1Extensions . from_fileobj ( bio , - <NUM_LIT:1> , False ) <EOL> assert_equal ( len ( ebacks ) , <NUM_LIT:1> ) <EOL> assert_equal ( ext1 , ebacks [ <NUM_LIT:0> ] ) <EOL> exp_dtype = np . dtype ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> bio . seek ( <NUM_LIT:0> ) <EOL> buff = np . ndarray ( shape = ( ) , dtype = exp_dtype , buffer = bio . read ( <NUM_LIT:16> ) ) <EOL> assert_equal ( buff [ '<STR_LIT>' ] , <NUM_LIT:32> ) <EOL> assert_equal ( buff [ '<STR_LIT>' ] , <NUM_LIT:6> ) <EOL> bio . seek ( <NUM_LIT:32> ) <EOL> ext2 = Nifti1Extension ( <NUM_LIT:6> , b'<STR_LIT>' ) <EOL> ext2 . write_to ( bio , False ) <EOL> bio . seek ( <NUM_LIT:0> ) <EOL> ebacks = Nifti1Extensions . from_fileobj ( bio , - <NUM_LIT:1> , False ) <EOL> assert_equal ( len ( ebacks ) , <NUM_LIT:2> ) <EOL> assert_equal ( ext1 , ebacks [ <NUM_LIT:0> ] ) <EOL> assert_equal ( ext2 , ebacks [ <NUM_LIT:1> ] ) <EOL> bio . truncate ( <NUM_LIT:0> ) <EOL> bio . seek ( <NUM_LIT:0> ) <EOL> ext1 . write_to ( bio , False ) <EOL> bio . seek ( <NUM_LIT:0> ) <EOL> start = np . zeros ( ( <NUM_LIT:1> , ) , dtype = exp_dtype ) <EOL> start [ '<STR_LIT>' ] = <NUM_LIT> <EOL> start [ '<STR_LIT>' ] = <NUM_LIT:6> <EOL> bio . write ( start . tostring ( ) ) <EOL> bio . seek ( <NUM_LIT> ) <EOL> ext2 . write_to ( bio , False ) <EOL> bio . seek ( <NUM_LIT:0> ) <EOL> with warnings . catch_warnings ( record = True ) as warns : <EOL> ebacks = Nifti1Extensions . from_fileobj ( bio , - <NUM_LIT:1> , False ) <EOL> assert_equal ( len ( warns ) , <NUM_LIT:1> ) <EOL> assert_equal ( warns [ <NUM_LIT:0> ] . category , UserWarning ) <EOL> assert_equal ( len ( ebacks ) , <NUM_LIT:2> ) <EOL> assert_equal ( ext1 , ebacks [ <NUM_LIT:0> ] ) <EOL> assert_equal ( ext2 , ebacks [ <NUM_LIT:1> ] ) <EOL> def test_nifti_extensions ( ) : <EOL> nim = load ( image_file ) <EOL> hdr = nim . header <EOL> exts_container = hdr . extensions <EOL> assert_equal ( len ( exts_container ) , <NUM_LIT:2> ) <EOL> assert_equal ( exts_container . count ( '<STR_LIT>' ) , <NUM_LIT:2> ) <EOL> assert_equal ( exts_container . count ( '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> assert_equal ( exts_container . get_codes ( ) , [ <NUM_LIT:6> , <NUM_LIT:6> ] ) <EOL> assert_equal ( ( exts_container . get_sizeondisk ( ) ) % <NUM_LIT:16> , <NUM_LIT:0> ) <EOL> assert_equal ( exts_container [ <NUM_LIT:0> ] . get_content ( ) , b'<STR_LIT>' ) <EOL> afniext = Nifti1Extension ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> exts_container . append ( afniext ) <EOL> assert_true ( exts_container . get_codes ( ) == [ <NUM_LIT:6> , <NUM_LIT:6> , <NUM_LIT:4> ] ) <EOL> assert_true ( exts_container . count ( '<STR_LIT>' ) == <NUM_LIT:2> ) <EOL> assert_true ( exts_container . count ( '<STR_LIT>' ) == <NUM_LIT:1> ) <EOL> assert_true ( ( exts_container . get_sizeondisk ( ) ) % <NUM_LIT:16> == <NUM_LIT:0> ) <EOL> del exts_container [ <NUM_LIT:1> ] <EOL> assert_true ( exts_container . get_codes ( ) == [ <NUM_LIT:6> , <NUM_LIT:4> ] ) <EOL> assert_true ( exts_container . count ( '<STR_LIT>' ) == <NUM_LIT:1> ) <EOL> assert_true ( exts_container . count ( '<STR_LIT>' ) == <NUM_LIT:1> ) <EOL> @ dicom_test <EOL> def test_nifti_dicom_extension ( ) : <EOL> nim = load ( image_file ) <EOL> hdr = nim . header <EOL> exts_container = hdr . extensions <EOL> dcmext = Nifti1DicomExtension ( <NUM_LIT:2> , b'<STR_LIT>' ) <EOL> assert_equal ( dcmext . get_content ( ) . __class__ , pydicom . dataset . Dataset ) <EOL> assert_equal ( len ( dcmext . get_content ( ) . values ( ) ) , <NUM_LIT:0> ) <EOL> dcmext = Nifti1DicomExtension ( <NUM_LIT:2> , None ) <EOL> assert_equal ( dcmext . get_content ( ) . __class__ , pydicom . dataset . Dataset ) <EOL> assert_equal ( len ( dcmext . get_content ( ) . values ( ) ) , <NUM_LIT:0> ) <EOL> ds = pydicom . dataset . Dataset ( ) <EOL> ds . add_new ( ( <NUM_LIT> , <NUM_LIT> ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> dcmext = Nifti1DicomExtension ( <NUM_LIT:2> , ds ) <EOL> assert_equal ( dcmext . get_content ( ) . __class__ , pydicom . dataset . Dataset ) <EOL> assert_equal ( len ( dcmext . get_content ( ) . values ( ) ) , <NUM_LIT:1> ) <EOL> assert_equal ( dcmext . get_content ( ) . PatientID , '<STR_LIT>' ) <EOL> dcmbytes_explicit = struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , <EOL> '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) , <NUM_LIT:4> , <EOL> '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) ) <EOL> dcmext = Nifti1DicomExtension ( <NUM_LIT:2> , dcmbytes_explicit ) <EOL> assert_equal ( dcmext . __class__ , Nifti1DicomExtension ) <EOL> assert_equal ( dcmext . _guess_implicit_VR ( ) , False ) <EOL> assert_equal ( dcmext . _is_little_endian , True ) <EOL> assert_equal ( dcmext . get_code ( ) , <NUM_LIT:2> ) <EOL> assert_equal ( dcmext . get_content ( ) . PatientID , '<STR_LIT>' ) <EOL> assert_equal ( len ( dcmext . get_content ( ) . values ( ) ) , <NUM_LIT:1> ) <EOL> assert_equal ( dcmext . _mangle ( dcmext . get_content ( ) ) , dcmbytes_explicit ) <EOL> assert_equal ( dcmext . get_sizeondisk ( ) % <NUM_LIT:16> , <NUM_LIT:0> ) <EOL> dcmbytes_implicit = struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:4> , <EOL> '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) ) <EOL> dcmext = Nifti1DicomExtension ( <NUM_LIT:2> , dcmbytes_implicit ) <EOL> assert_equal ( dcmext . _guess_implicit_VR ( ) , True ) <EOL> assert_equal ( dcmext . get_code ( ) , <NUM_LIT:2> ) <EOL> assert_equal ( dcmext . get_content ( ) . PatientID , '<STR_LIT>' ) <EOL> assert_equal ( len ( dcmext . get_content ( ) . values ( ) ) , <NUM_LIT:1> ) <EOL> assert_equal ( dcmext . _mangle ( dcmext . get_content ( ) ) , dcmbytes_implicit ) <EOL> assert_equal ( dcmext . get_sizeondisk ( ) % <NUM_LIT:16> , <NUM_LIT:0> ) <EOL> dcmbytes_explicit_be = struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , <EOL> '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) , <NUM_LIT:4> , <EOL> '<STR_LIT>' . encode ( '<STR_LIT:utf-8>' ) ) <EOL> hdr_be = Nifti1Header ( endianness = '<STR_LIT:>>' ) <EOL> dcmext = Nifti1DicomExtension ( <NUM_LIT:2> , dcmbytes_explicit_be , parent_hdr = hdr_be ) <EOL> assert_equal ( dcmext . __class__ , Nifti1DicomExtension ) <EOL> assert_equal ( dcmext . _guess_implicit_VR ( ) , False ) <EOL> assert_equal ( dcmext . get_code ( ) , <NUM_LIT:2> ) <EOL> assert_equal ( dcmext . get_content ( ) . PatientID , '<STR_LIT>' ) <EOL> assert_equal ( dcmext . get_content ( ) [ <NUM_LIT> , <NUM_LIT> ] . value , '<STR_LIT>' ) <EOL> assert_equal ( len ( dcmext . get_content ( ) . values ( ) ) , <NUM_LIT:1> ) <EOL> assert_equal ( dcmext . _mangle ( dcmext . get_content ( ) ) , dcmbytes_explicit_be ) <EOL> assert_equal ( dcmext . get_sizeondisk ( ) % <NUM_LIT:16> , <NUM_LIT:0> ) <EOL> dcmext = Nifti1DicomExtension ( <NUM_LIT:2> , ds , parent_hdr = hdr_be ) <EOL> assert_equal ( dcmext . _mangle ( dcmext . get_content ( ) ) , dcmbytes_explicit_be ) <EOL> assert_equal ( exts_container . count ( '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> exts_container . append ( dcmext ) <EOL> assert_equal ( exts_container . count ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> assert_equal ( exts_container . get_codes ( ) , [ <NUM_LIT:6> , <NUM_LIT:6> , <NUM_LIT:2> ] ) <EOL> assert_equal ( dcmext . _mangle ( dcmext . get_content ( ) ) , dcmbytes_explicit_be ) <EOL> assert_equal ( dcmext . get_sizeondisk ( ) % <NUM_LIT:16> , <NUM_LIT:0> ) <EOL> assert_raises ( TypeError , Nifti1DicomExtension , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> class TestNifti1General ( object ) : <EOL> """<STR_LIT>""" <EOL> single_class = Nifti1Image <EOL> pair_class = Nifti1Pair <EOL> module = nifti1 <EOL> example_file = image_file <EOL> def test_loadsave_cycle ( self ) : <EOL> nim = self . module . load ( self . example_file ) <EOL> hdr = nim . header <EOL> exts_container = hdr . extensions <EOL> assert_true ( len ( exts_container ) > <NUM_LIT:0> ) <EOL> lnim = bytesio_round_trip ( nim ) <EOL> hdr = lnim . header <EOL> lexts_container = hdr . extensions <EOL> assert_equal ( exts_container , lexts_container ) <EOL> data = np . ones ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) , dtype = '<STR_LIT>' ) <EOL> img = self . single_class ( data , np . eye ( <NUM_LIT:4> ) ) <EOL> hdr = img . header <EOL> assert_equal ( hdr . get_data_dtype ( ) , np . int16 ) <EOL> assert_array_equal ( hdr . get_slope_inter ( ) , ( None , None ) ) <EOL> hdr . set_slope_inter ( <NUM_LIT:2> , <NUM_LIT:8> ) <EOL> assert_equal ( hdr . get_slope_inter ( ) , ( <NUM_LIT:2> , <NUM_LIT:8> ) ) <EOL> wnim = self . single_class ( data , np . eye ( <NUM_LIT:4> ) , header = hdr ) <EOL> assert_equal ( wnim . get_data_dtype ( ) , np . int16 ) <EOL> assert_equal ( wnim . header . get_slope_inter ( ) , ( None , None ) ) <EOL> wnim . header . set_slope_inter ( <NUM_LIT:2> , <NUM_LIT:8> ) <EOL> assert_equal ( wnim . header . get_slope_inter ( ) , ( <NUM_LIT:2> , <NUM_LIT:8> ) ) <EOL> lnim = bytesio_round_trip ( wnim ) <EOL> assert_equal ( lnim . get_data_dtype ( ) , np . int16 ) <EOL> assert_array_equal ( lnim . get_data ( ) , data * <NUM_LIT> + <NUM_LIT> ) <EOL> assert_equal ( lnim . header . get_slope_inter ( ) , ( None , None ) ) <EOL> assert_equal ( ( lnim . dataobj . slope , lnim . dataobj . inter ) , ( <NUM_LIT:2> , <NUM_LIT:8> ) ) <EOL> def test_load ( self ) : <EOL> arr = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> aff = np . diag ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> simg = self . single_class ( arr , aff ) <EOL> pimg = self . pair_class ( arr , aff ) <EOL> save = self . module . save <EOL> load = self . module . load <EOL> with InTemporaryDirectory ( ) : <EOL> for img in ( simg , pimg ) : <EOL> save ( img , '<STR_LIT>' ) <EOL> assert_array_equal ( arr , load ( '<STR_LIT>' ) . get_data ( ) ) <EOL> save ( simg , '<STR_LIT>' ) <EOL> assert_array_equal ( arr , load ( '<STR_LIT>' ) . get_data ( ) ) <EOL> save ( simg , '<STR_LIT>' ) <EOL> assert_array_equal ( arr , load ( '<STR_LIT>' ) . get_data ( ) ) <EOL> def test_float_int_min_max ( self ) : <EOL> aff = np . eye ( <NUM_LIT:4> ) <EOL> for in_dt in ( np . float32 , np . float64 ) : <EOL> finf = type_info ( in_dt ) <EOL> arr = np . array ( [ finf [ '<STR_LIT>' ] , finf [ '<STR_LIT>' ] ] , dtype = in_dt ) <EOL> for out_dt in IUINT_TYPES : <EOL> img = self . single_class ( arr , aff ) <EOL> img_back = bytesio_round_trip ( img ) <EOL> arr_back_sc = img_back . get_data ( ) <EOL> assert_true ( np . allclose ( arr , arr_back_sc ) ) <EOL> def test_float_int_spread ( self ) : <EOL> powers = np . arange ( - <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:0.5> ) <EOL> arr = np . concatenate ( ( - <NUM_LIT:10> ** powers , <NUM_LIT:10> ** powers ) ) <EOL> aff = np . eye ( <NUM_LIT:4> ) <EOL> for in_dt in ( np . float32 , np . float64 ) : <EOL> arr_t = arr . astype ( in_dt ) <EOL> for out_dt in IUINT_TYPES : <EOL> img = self . single_class ( arr_t , aff ) <EOL> img_back = bytesio_round_trip ( img ) <EOL> arr_back_sc = img_back . get_data ( ) <EOL> slope , inter = img_back . header . get_slope_inter ( ) <EOL> max_miss = rt_err_estimate ( arr_t , arr_back_sc . dtype , slope , <EOL> inter ) <EOL> diff = np . abs ( arr_t - arr_back_sc ) <EOL> rdiff = diff / np . abs ( arr_t ) <EOL> assert_true ( np . all ( ( diff <= max_miss ) | ( rdiff <= <NUM_LIT> ) ) ) <EOL> def test_rt_bias ( self ) : <EOL> rng = np . random . RandomState ( <NUM_LIT> ) <EOL> mu , std , count = <NUM_LIT:100> , <NUM_LIT:10> , <NUM_LIT:100> <EOL> arr = rng . normal ( mu , std , size = ( count , ) ) <EOL> eps = np . finfo ( np . float32 ) . eps <EOL> aff = np . eye ( <NUM_LIT:4> ) <EOL> for in_dt in ( np . float32 , np . float64 ) : <EOL> arr_t = arr . astype ( in_dt ) <EOL> for out_dt in IUINT_TYPES : <EOL> img = self . single_class ( arr_t , aff ) <EOL> img_back = bytesio_round_trip ( img ) <EOL> arr_back_sc = img_back . get_data ( ) <EOL> slope , inter = img_back . header . get_slope_inter ( ) <EOL> bias = np . mean ( arr_t - arr_back_sc ) <EOL> max_miss = rt_err_estimate ( arr_t , arr_back_sc . dtype , slope , <EOL> inter ) <EOL> bias_thresh = np . max ( [ max_miss / np . sqrt ( count ) , eps ] ) <EOL> assert_true ( np . abs ( bias ) < bias_thresh ) <EOL> @ runif_extra_has ( '<STR_LIT>' ) <EOL> def test_large_nifti1 ( ) : <EOL> image_shape = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> img = Nifti1Image ( np . ones ( image_shape , dtype = np . float32 ) , <EOL> affine = np . eye ( <NUM_LIT:4> ) ) <EOL> with InTemporaryDirectory ( ) : <EOL> img . to_filename ( '<STR_LIT>' ) <EOL> del img <EOL> data = load ( '<STR_LIT>' ) . get_data ( ) <EOL> assert_equal ( image_shape , data . shape ) <EOL> n_ones = np . sum ( ( data == <NUM_LIT:1.> ) ) <EOL> assert_equal ( np . prod ( image_shape ) , n_ones ) </s>
