<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from os . path import dirname , join as pjoin <EOL> import shutil <EOL> import numpy as np <EOL> from . . import ( Spm99AnalyzeImage , Spm2AnalyzeImage , <EOL> Nifti1Pair , Nifti1Image , <EOL> Nifti2Pair , Nifti2Image ) <EOL> from . . loadsave import load , read_img_data <EOL> from . . filebasedimages import ImageFileError <EOL> from . . tmpdirs import InTemporaryDirectory , TemporaryDirectory <EOL> from . . optpkg import optional_package <EOL> _ , have_scipy , _ = optional_package ( '<STR_LIT>' ) <EOL> from numpy . testing import ( assert_almost_equal , <EOL> assert_array_equal ) <EOL> from nose . tools import ( assert_true , assert_false , assert_raises , <EOL> assert_equal , assert_not_equal ) <EOL> data_path = pjoin ( dirname ( __file__ ) , '<STR_LIT:data>' ) <EOL> def test_read_img_data ( ) : <EOL> for fname in ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) : <EOL> fpath = pjoin ( data_path , fname ) <EOL> img = load ( fpath ) <EOL> data = img . get_data ( ) <EOL> data2 = read_img_data ( img ) <EOL> assert_array_equal ( data , data2 ) <EOL> dao = img . dataobj <EOL> if hasattr ( dao , '<STR_LIT>' ) and hasattr ( img . header , '<STR_LIT>' ) : <EOL> assert_equal ( ( dao . slope , dao . inter ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> assert_array_equal ( read_img_data ( img , prefer = '<STR_LIT>' ) , data ) <EOL> with TemporaryDirectory ( ) as tmpdir : <EOL> up_fpath = pjoin ( tmpdir , fname . upper ( ) ) <EOL> shutil . copyfile ( fpath , up_fpath ) <EOL> img = load ( up_fpath ) <EOL> assert_array_equal ( img . dataobj , data ) <EOL> del img <EOL> def test_read_img_data_nifti ( ) : <EOL> shape = ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> data = np . random . normal ( size = shape ) <EOL> out_dtype = np . dtype ( np . int16 ) <EOL> classes = ( Nifti1Pair , Nifti1Image , Nifti2Pair , Nifti2Image ) <EOL> if have_scipy : <EOL> classes += ( Spm99AnalyzeImage , Spm2AnalyzeImage ) <EOL> with InTemporaryDirectory ( ) : <EOL> for i , img_class in enumerate ( classes ) : <EOL> img = img_class ( data , np . eye ( <NUM_LIT:4> ) ) <EOL> img . set_data_dtype ( out_dtype ) <EOL> assert_raises ( ImageFileError , read_img_data , img ) <EOL> froot = '<STR_LIT>' . format ( i ) <EOL> img . file_map = img . filespec_to_file_map ( froot ) <EOL> assert_raises ( IOError , read_img_data , img ) <EOL> img . to_file_map ( ) <EOL> img_fname = img . file_map [ '<STR_LIT:image>' ] . filename <EOL> img_back = load ( img_fname ) <EOL> data_back = img_back . get_data ( ) <EOL> assert_array_equal ( data_back , read_img_data ( img_back ) ) <EOL> hdr_fname = ( img . file_map [ '<STR_LIT>' ] . filename <EOL> if '<STR_LIT>' in img . file_map else img_fname ) <EOL> with open ( hdr_fname , '<STR_LIT:rb>' ) as fobj : <EOL> hdr_back = img_back . header_class . from_fileobj ( fobj ) <EOL> with open ( img_fname , '<STR_LIT:rb>' ) as fobj : <EOL> scaled_back = hdr_back . data_from_fileobj ( fobj ) <EOL> assert_array_equal ( data_back , scaled_back ) <EOL> with open ( img_fname , '<STR_LIT:rb>' ) as fobj : <EOL> unscaled_back = hdr_back . raw_data_from_fileobj ( fobj ) <EOL> assert_array_equal ( unscaled_back , <EOL> read_img_data ( img_back , prefer = '<STR_LIT>' ) ) <EOL> assert_array_equal ( data_back , read_img_data ( img_back ) ) <EOL> has_inter = hdr_back . has_data_intercept <EOL> old_slope = hdr_back [ '<STR_LIT>' ] <EOL> old_inter = hdr_back [ '<STR_LIT>' ] if has_inter else <NUM_LIT:0> <EOL> est_unscaled = ( data_back - old_inter ) / old_slope <EOL> actual_unscaled = read_img_data ( img_back , prefer = '<STR_LIT>' ) <EOL> assert_almost_equal ( est_unscaled , actual_unscaled ) <EOL> img_back . header [ '<STR_LIT>' ] = <NUM_LIT> <EOL> if has_inter : <EOL> new_inter = <NUM_LIT> <EOL> img_back . header [ '<STR_LIT>' ] = <NUM_LIT> <EOL> else : <EOL> new_inter = <NUM_LIT:0> <EOL> assert_true ( np . allclose ( actual_unscaled * <NUM_LIT> + new_inter , <EOL> read_img_data ( img_back ) ) ) <EOL> assert_array_equal ( actual_unscaled , <EOL> read_img_data ( img_back , prefer = '<STR_LIT>' ) ) <EOL> img . header . set_data_offset ( <NUM_LIT> ) <EOL> del actual_unscaled , unscaled_back <EOL> img . to_file_map ( ) <EOL> with open ( img_fname , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( b'<STR_LIT>' ) <EOL> img_back = load ( img_fname ) <EOL> data_back = img_back . get_data ( ) <EOL> assert_array_equal ( data_back , read_img_data ( img_back ) ) <EOL> img_back . header . set_data_offset ( <NUM_LIT> ) <EOL> exp_offset = np . zeros ( ( data . size , ) , data . dtype ) + old_inter <EOL> exp_offset [ : - <NUM_LIT:1> ] = np . ravel ( data_back , order = '<STR_LIT:F>' ) [ <NUM_LIT:1> : ] <EOL> exp_offset = np . reshape ( exp_offset , shape , order = '<STR_LIT:F>' ) <EOL> assert_array_equal ( exp_offset , read_img_data ( img_back ) ) <EOL> del img , img_back , data_back </s>
