Simple wrapper for setting non-finite values to zero

finite_img(img, replace = 0)

# S4 method for nifti
finite_img(img, replace = 0)

# S4 method for array
finite_img(img, replace = 0)

# S4 method for ANY
finite_img(img, replace = 0)

# S4 method for character
finite_img(img, replace = 0)

# S4 method for list
finite_img(img, replace = 0)

Arguments

img

character path of image or an object of class nifti, or list of images

replace

Value to replace non-finite values to

Value

nifti object

Author

John Muschelli muschellij2@gmail.com

Examples

set.seed(5)
dims = rep(10, 3)
arr = array(rpois(prod(dims), lambda = 2), dim = dims)
arr[c(5, 6, 7, 8)] = c(NA, NaN, Inf, -Inf)
nim = nifti(arr)
finite_img(nim)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
finite_img(arr)
#> , , 1
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    1    1    4    2    3    1    2    3    4     3
#>  [2,]    3    2    3    0    1    2    1    3    3     1
#>  [3,]    4    1    1    1    3    4    4    1    1     1
#>  [4,]    1    2    1    0    2    5    6    3    0     1
#>  [5,]    0    1    1    0    4    4    6    0    2     1
#>  [6,]    0    1    2    2    2    1    4    4    0     3
#>  [7,]    0    1    2    2    3    1    4    0    5     1
#>  [8,]    0    4    5    2    0    1    1    3    3     2
#>  [9,]    5    2    1    1    3    1    4    2    1     2
#> [10,]    0    3    5    1    2    1    3    1    2     2
#> 
#> , , 2
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    4    1    0    1    2    2    3    1    3     2
#>  [2,]    3    2    5    1    1    2    1    4    3     3
#>  [3,]    2    3    3    0    2    3    0    6    1     1
#>  [4,]    4    1    4    3    4    0    4    0    2     4
#>  [5,]    3    2    1    1    2    2    3    1    3     1
#>  [6,]    1    1    0    0    3    3    2    4    0     1
#>  [7,]    1    2    3    0    2    2    2    2    3     2
#>  [8,]    2    0    3    2    2    1    1    5    1     2
#>  [9,]    2    2    1    1    2    1    3    1    3     2
#> [10,]    1    1    1    2    3    3    5    0    1     2
#> 
#> , , 3
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    0    3    1    3    4    2    4    2    1     1
#>  [2,]    0    3    2    0    1    0    2    1    1     2
#>  [3,]    4    2    5    0    3    1    0    2    2     2
#>  [4,]    3    2    1    4    6    1    1    1    1     2
#>  [5,]    3    0    6    3    0    4    0    1    6     2
#>  [6,]    2    1    3    1    2    0    0    1    1     2
#>  [7,]    2    1    2    2    0    0    2    3    3     0
#>  [8,]    1    0    3    0    0    4    1    1    3     0
#>  [9,]    2    4    0    2    3    0    3    2    1     1
#> [10,]    1    0    1    3    1    2    2    3    4     4
#> 
#> , , 4
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    3    4    0    1    0    2    6    1    3     4
#>  [2,]    1    1    3    0    1    1    3    1    1     3
#>  [3,]    3    1    6    2    2    4    1    2    2     3
#>  [4,]    1    2    1    5    1    2    1    3    1     3
#>  [5,]    1    4    2    2    0    1    3    1    1     2
#>  [6,]    5    2    0    1    1    2    4    2    2     5
#>  [7,]    3    1    5    2    0    3    3    3    2     1
#>  [8,]    0    0    2    2    2    0    5    1    4     4
#>  [9,]    1    2    1    1    2    1    0    0    2     3
#> [10,]    3    0    3    1    4    0    2    3    4     1
#> 
#> , , 5
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    2    2    4    1    2    2    1    5    2     1
#>  [2,]    1    2    2    2    3    4    0    6    0     2
#>  [3,]    1    3    3    2    2    0    3    4    2     3
#>  [4,]    0    0    1    3    1    2    3    3    4     4
#>  [5,]    3    3    3    4    5    1    1    1    2     2
#>  [6,]    1    2    1    3    0    2    5    3    2     1
#>  [7,]    1    1    1    3    1    1    1    1    4     0
#>  [8,]    0    3    2    2    0    1    2    2    2     5
#>  [9,]    2    3    3    3    0    0    1    4    0     2
#> [10,]    2    3    1    1    0    0    5    3    2     1
#> 
#> , , 6
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    1    0    0    6    1    2    2    1    2     1
#>  [2,]    1    0    2    4    0    1    3    3    2     2
#>  [3,]    1    1    2    3    1    0    1    4    3     3
#>  [4,]    2    1    3    2    4    0    2    4    1     3
#>  [5,]    2    1    3    1    1    2    1    2    3     3
#>  [6,]    0    1    1    2    2    2    5    2    5     3
#>  [7,]    1    3    1    0    4    2    3    5    2     2
#>  [8,]    2    0    1    2    4    2    2    1    1     2
#>  [9,]    0    2    2    1    5    2    1    1    2     2
#> [10,]    4    0    3    1    3    5    3    1    3     1
#> 
#> , , 7
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    4    1    1    4    1    0    0    1    0     1
#>  [2,]    1    1    0    3    3    2    2    1    2     0
#>  [3,]    2    4    4    4    4    3    2    0    0     1
#>  [4,]    4    5    1    1    2    5    1    1    3     1
#>  [5,]    5    1    3    1    2    2    2    2    0     0
#>  [6,]    2    2    1    1    4    0    1    3    1     2
#>  [7,]    2    2    1    0    3    0    1    3    3     2
#>  [8,]    2    4    4    3    1    4    2    1    3     2
#>  [9,]    0    3    2    2    1    1    3    2    1     4
#> [10,]    2    4    3    0    0    2    4    5    2     3
#> 
#> , , 8
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    0    3    1    4    2    4    5    4    1     0
#>  [2,]    6    3    3    1    2    2    2    4    1     1
#>  [3,]    2    5    5    1    3    1    4    3    2     1
#>  [4,]    1    0    2    5    2    2    0    1    2     2
#>  [5,]    3    0    2    2    0    4    3    1    1     3
#>  [6,]    1    0    1    3    2    0    2    4    2     1
#>  [7,]    1    2    3    3    3    3    3    1    3     0
#>  [8,]    5    1    2    4    3    2    2    2    3     2
#>  [9,]    2    2    1    2    1    1    5    1    2     1
#> [10,]    2    0    1    2    3    5    7    0    0     2
#> 
#> , , 9
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    1    1    3    6    2    2    1    3    1     2
#>  [2,]    2    0    2    2    2    4    2    2    2     2
#>  [3,]    3    1    1    1    5    3    0    4    0     4
#>  [4,]    0    2    1    0    0    0    2    1    2     2
#>  [5,]    0    5    1    2    2    0    2    5    2     1
#>  [6,]    1    0    4    2    4    2    0    3    2     3
#>  [7,]    1    1    3    2    0    3    2    3    3     5
#>  [8,]    3    3    2    2    3    1    1    2    1     3
#>  [9,]    2    0    2    0    2    3    5    3    3     2
#> [10,]    0    4    1    2    5    0    4    2    2     0
#> 
#> , , 10
#> 
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    3    2    1    3    4    4    2    1    1     2
#>  [2,]    3    1    3    3    2    1    1    1    2     4
#>  [3,]    1    0    0    3    1    2    1    1    1     1
#>  [4,]    3    2    3    1    1    1    4    2    1     3
#>  [5,]    2    3    2    0    0    2    1    1    1     4
#>  [6,]    2    2    4    0    3    3    1    4    2     3
#>  [7,]    2    0    0    3    1    3    2    3    1     2
#>  [8,]    1    0    5    1    2    0    1    4    2     0
#>  [9,]    2    2    5    1    1    3    0    2    4     0
#> [10,]    4    2    1    3    3    2    1    1    4     1
#> 
tfile = tempimg(nim)
#> Warning: NAs and NaNs in image file, replacing with zeros
#> Warning: NAs introduced by coercion to integer range
checkimg(c(tfile, tfile))
#>   /tmp/RtmpBb1B9G/file16b731fb2bee4.nii.gz 
#> "/tmp/RtmpBb1B9G/file16b731fb2bee4.nii.gz" 
#>   /tmp/RtmpBb1B9G/file16b731fb2bee4.nii.gz 
#> "/tmp/RtmpBb1B9G/file16b731fb2bee4.nii.gz" 
checkimg(list(tfile, tfile))
#> [1] "/tmp/RtmpBb1B9G/file16b731fb2bee4.nii.gz"
#> [2] "/tmp/RtmpBb1B9G/file16b731fb2bee4.nii.gz"
finite_img(list(tfile, tfile))
#> Warning: NAs introduced by coercion to integer range
#> Warning: NAs introduced by coercion to integer range
#> [[1]]
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 16 (FLOAT32)
#>   Bits per Pixel  : 32
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
#> 
#> [[2]]
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 16 (FLOAT32)
#>   Bits per Pixel  : 32
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
#> 
finite_img(c(tfile, tfile))
#> Warning: NAs introduced by coercion to integer range
#> Warning: NAs introduced by coercion to integer range
#> [[1]]
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 16 (FLOAT32)
#>   Bits per Pixel  : 32
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
#> 
#> [[2]]
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 16 (FLOAT32)
#>   Bits per Pixel  : 32
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
#> 
img = RNifti::readNifti(tfile)
checkimg(img)
#> [1] "/tmp/RtmpBb1B9G/file16b7357445518.nii.gz"
img[c(5, 6, 7, 8)] = c(NA, NaN, Inf, -Inf)
stopifnot(!any(c(is.na(c(finite_img(img))))))