<s> import nibabel <EOL> import numpy as np <EOL> from numpy . testing import assert_array_equal <EOL> from nilearn . input_data import NiftiSpheresMasker <EOL> from nilearn . _utils . testing import assert_raises_regex <EOL> def test_seed_extraction ( ) : <EOL> data = np . random . random ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:5> ) ) <EOL> img = nibabel . Nifti1Image ( data , np . eye ( <NUM_LIT:4> ) ) <EOL> masker = NiftiSpheresMasker ( [ ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ] ) <EOL> masker . fit ( ) <EOL> s = masker . transform ( img ) <EOL> assert_array_equal ( s [ : , <NUM_LIT:0> ] , data [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> def test_sphere_extraction ( ) : <EOL> data = np . random . random ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:5> ) ) <EOL> img = nibabel . Nifti1Image ( data , np . eye ( <NUM_LIT:4> ) ) <EOL> masker = NiftiSpheresMasker ( [ ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ] , radius = <NUM_LIT:1> ) <EOL> masker . fit ( ) <EOL> s = masker . transform ( img ) <EOL> mask = np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) , dtype = np . bool ) <EOL> mask [ : , <NUM_LIT:1> , <NUM_LIT:1> ] = True <EOL> mask [ <NUM_LIT:1> , : , <NUM_LIT:1> ] = True <EOL> mask [ <NUM_LIT:1> , <NUM_LIT:1> , : ] = True <EOL> assert_array_equal ( s [ : , <NUM_LIT:0> ] , np . mean ( data [ mask ] , axis = <NUM_LIT:0> ) ) <EOL> mask_img = np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> mask_img [ <NUM_LIT:1> , : , : ] = <NUM_LIT:1> <EOL> mask_img = nibabel . Nifti1Image ( mask_img , np . eye ( <NUM_LIT:4> ) ) <EOL> masker = NiftiSpheresMasker ( [ ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ] , radius = <NUM_LIT:1> , mask_img = mask_img ) <EOL> masker . fit ( ) <EOL> s = masker . transform ( img ) <EOL> assert_array_equal ( s [ : , <NUM_LIT:0> ] , <EOL> np . mean ( data [ np . logical_and ( mask , mask_img . get_data ( ) ) ] , <EOL> axis = <NUM_LIT:0> ) ) <EOL> def test_anisotropic_sphere_extraction ( ) : <EOL> data = np . random . random ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:5> ) ) <EOL> affine = np . eye ( <NUM_LIT:4> ) <EOL> affine [ <NUM_LIT:0> , <NUM_LIT:0> ] = <NUM_LIT:2> <EOL> affine [ <NUM_LIT:2> , <NUM_LIT:2> ] = <NUM_LIT:2> <EOL> img = nibabel . Nifti1Image ( data , affine ) <EOL> masker = NiftiSpheresMasker ( [ ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ) ] , radius = <NUM_LIT:1> ) <EOL> masker . fit ( ) <EOL> s = masker . transform ( img ) <EOL> mask = np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) , dtype = np . bool ) <EOL> mask [ <NUM_LIT:1> , : , <NUM_LIT:1> ] = True <EOL> assert_array_equal ( s [ : , <NUM_LIT:0> ] , np . mean ( data [ mask ] , axis = <NUM_LIT:0> ) ) <EOL> mask_img = np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> mask_img [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> affine_2 = affine . copy ( ) <EOL> affine_2 [ <NUM_LIT:0> , <NUM_LIT:0> ] = <NUM_LIT:4> <EOL> mask_img = nibabel . Nifti1Image ( mask_img , affine = affine_2 ) <EOL> masker = NiftiSpheresMasker ( [ ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ) ] , radius = <NUM_LIT:1> , mask_img = mask_img ) <EOL> masker . fit ( ) <EOL> s = masker . transform ( img ) <EOL> assert_array_equal ( s [ : , <NUM_LIT:0> ] , data [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> def test_errors ( ) : <EOL> masker = NiftiSpheresMasker ( ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , radius = <NUM_LIT> ) <EOL> assert_raises_regex ( ValueError , '<STR_LIT>' , masker . fit ) <EOL> def test_nifti_spheres_masker_overlap ( ) : <EOL> affine = np . eye ( <NUM_LIT:4> ) <EOL> shape = ( <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> data = np . random . random ( shape + ( <NUM_LIT:5> , ) ) <EOL> fmri_img = nibabel . Nifti1Image ( data , affine ) <EOL> seeds = [ ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) ] <EOL> overlapping_masker = NiftiSpheresMasker ( seeds , radius = <NUM_LIT:1> , <EOL> allow_overlap = True ) <EOL> overlapping_masker . fit_transform ( fmri_img ) <EOL> overlapping_masker = NiftiSpheresMasker ( seeds , radius = <NUM_LIT:2> , <EOL> allow_overlap = True ) <EOL> overlapping_masker . fit_transform ( fmri_img ) <EOL> noverlapping_masker = NiftiSpheresMasker ( seeds , radius = <NUM_LIT:1> , <EOL> allow_overlap = False ) <EOL> noverlapping_masker . fit_transform ( fmri_img ) <EOL> noverlapping_masker = NiftiSpheresMasker ( seeds , radius = <NUM_LIT:2> , <EOL> allow_overlap = False ) <EOL> assert_raises_regex ( ValueError , '<STR_LIT>' , <EOL> noverlapping_masker . fit_transform , fmri_img ) <EOL> def test_small_radius ( ) : <EOL> affine = np . eye ( <NUM_LIT:4> ) <EOL> shape = ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> data = np . random . random ( shape ) <EOL> mask = np . zeros ( shape ) <EOL> mask [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> mask [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> affine = np . eye ( <NUM_LIT:4> ) * <NUM_LIT> <EOL> seed = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> masker = NiftiSpheresMasker ( [ seed ] , radius = <NUM_LIT:0.1> , <EOL> mask_img = nibabel . Nifti1Image ( mask , affine ) ) <EOL> masker . fit_transform ( nibabel . Nifti1Image ( data , affine ) ) <EOL> mask [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> mask [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> masker = NiftiSpheresMasker ( [ seed ] , radius = <NUM_LIT:0.1> , <EOL> mask_img = nibabel . Nifti1Image ( mask , affine ) ) <EOL> assert_raises_regex ( ValueError , '<STR_LIT>' , <EOL> masker . fit_transform , <EOL> nibabel . Nifti1Image ( data , affine ) ) <EOL> masker = NiftiSpheresMasker ( [ seed ] , radius = <NUM_LIT> , <EOL> mask_img = nibabel . Nifti1Image ( mask , affine ) ) <EOL> masker . fit_transform ( nibabel . Nifti1Image ( data , affine ) ) </s>
