Title: | 'Neuroconductor' Base Package with Helper Functions for 'nifti' Objects |
---|---|
Description: | Base package for 'Neuroconductor', which includes many helper functions that interact with objects of class 'nifti', implemented by package 'oro.nifti', for reading/writing and also other manipulation functions. |
Authors: | John Muschelli [aut, cre] |
Maintainer: | John Muschelli <[email protected]> |
License: | GPL-2 |
Version: | 1.32.4 |
Built: | 2025-01-04 04:27:09 UTC |
Source: | https://github.com/muschellij2/neurobase |
Simple wrapper for subsetting an image with indices, dropping empty dimensions.
applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'nifti' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'character' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'factor' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'list' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'array' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'anlz' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'ANY' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) apply_empty_dim(img, ...)
applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'nifti' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'character' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'factor' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'list' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'array' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'anlz' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) ## S4 method for signature 'ANY' applyEmptyImageDimensions(img, inds, reorient = FALSE, ...) apply_empty_dim(img, ...)
img |
image, nifti object, or array |
inds |
indices of subset from |
reorient |
Should image be reoriented if a filename? |
... |
not used |
Object of class nifti
or array
if nifti
is not supplied
apply_empty_dim
is a shorthand for
applyEmptyImageDimensions
with all the same arguments.
getEmptyImageDimensions
,
dropEmptyImageDimensions
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) inds = getEmptyImageDimensions(nim) inds_arr = getEmptyImageDimensions(arr) testthat::expect_equal(inds, inds_arr) out = applyEmptyImageDimensions(nim, inds = inds) out_arr = applyEmptyImageDimensions(arr, inds = inds) testthat::expect_equal(out_arr, array(out, dim = dim(out))) out = apply_empty_dim(nim, inds = inds) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) inds = getEmptyImageDimensions(nim) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) apply_empty_dim(nim, inds = inds) func = function(...) applyEmptyImageDimensions(..., inds = inds) func(arr) func(nim) func(rnifti) func(timg) func(limg)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) inds = getEmptyImageDimensions(nim) inds_arr = getEmptyImageDimensions(arr) testthat::expect_equal(inds, inds_arr) out = applyEmptyImageDimensions(nim, inds = inds) out_arr = applyEmptyImageDimensions(arr, inds = inds) testthat::expect_equal(out_arr, array(out, dim = dim(out))) out = apply_empty_dim(nim, inds = inds) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) inds = getEmptyImageDimensions(nim) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) apply_empty_dim(nim, inds = inds) func = function(...) applyEmptyImageDimensions(..., inds = inds) func(arr) func(nim) func(rnifti) func(timg) func(limg)
Computes the boxplot of values of an image with the option for a mask.
## S3 method for class 'nifti' boxplot(x, ..., mask) ## S3 method for class 'anlz' boxplot(x, ..., mask)
## S3 method for class 'nifti' boxplot(x, ..., mask) ## S3 method for class 'anlz' boxplot(x, ..., mask)
x |
Object of class |
... |
Arguments passed to |
mask |
object to subset the image. If missing, then all values of the image are plotted. |
Output of boxplot
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 boxplot(img) boxplot(img, mask = mask) boxplot(as.anlz(img))
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 boxplot(img) boxplot(img, mask = mask) boxplot(as.anlz(img))
Helper function for plotting - returns breaks for image
plot function for object of class nifti
breaker(x, zlim, col, breaks = NULL)
breaker(x, zlim, col, breaks = NULL)
x |
Object of class |
zlim |
A user-specified |
col |
colors to be plotted. Only used for |
breaks |
if |
Vector of length 2
If breaks = NULL
, then vector of length(col) + 1
,
otherwise returns breaks
Determine if only values in a mask are 0/1
check_mask(mask, allow.NA = FALSE, allow.array = TRUE)
check_mask(mask, allow.NA = FALSE, allow.array = TRUE)
mask |
Object of class |
allow.NA |
allow NAs in the mask |
allow.array |
if |
Logical indicating if object is binary mask with only 0, 1, and NA
if applicable
arr = array(rbinom(1000, size = 1, prob = 0.2), dim = c(10,10,10)) nim = oro.nifti::nifti(arr) check_mask(nim)
arr = array(rbinom(1000, size = 1, prob = 0.2), dim = c(10,10,10)) nim = oro.nifti::nifti(arr) check_mask(nim)
Determine if only values in a mask are 0/1. Will error otherwise.
check_mask_fail(...)
check_mask_fail(...)
... |
arguments to pass to |
Either will error if conditions not met or an invisible NULL
arr = array(rbinom(1000, size = 1, prob = 0.2), dim = c(10,10,10)) nim = oro.nifti::nifti(arr) check_mask_fail(nim)
arr = array(rbinom(1000, size = 1, prob = 0.2), dim = c(10,10,10)) nim = oro.nifti::nifti(arr) check_mask_fail(nim)
Simple check to see if input is character or of class nifti and read in the header
check_nifti_header(x) ## S4 method for signature 'nifti' check_nifti_header(x) ## S4 method for signature 'character' check_nifti_header(x) ## S4 method for signature 'factor' check_nifti_header(x) ## S4 method for signature 'list' check_nifti_header(x) ## S4 method for signature 'array' check_nifti_header(x) ## S4 method for signature 'anlz' check_nifti_header(x) ## S4 method for signature 'ANY' check_nifti_header(x)
check_nifti_header(x) ## S4 method for signature 'nifti' check_nifti_header(x) ## S4 method for signature 'character' check_nifti_header(x) ## S4 method for signature 'factor' check_nifti_header(x) ## S4 method for signature 'list' check_nifti_header(x) ## S4 method for signature 'array' check_nifti_header(x) ## S4 method for signature 'anlz' check_nifti_header(x) ## S4 method for signature 'ANY' check_nifti_header(x)
x |
character path of image or an object of class nifti, or array |
nifti object or character
John Muschelli [email protected]
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) check_nifti_header(nim) check_nifti_header(as.anlz(nim)) testthat::expect_error(check_nifti_header(arr)) tfile = tempimg(nim) check_nifti_header(tfile) check_nifti_header(RNifti::readNifti(tfile)) check_nifti_header(c(tfile, tfile)) check_nifti_header(list(tfile, tfile)) check_nifti_header(factor(tfile))
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) check_nifti_header(nim) check_nifti_header(as.anlz(nim)) testthat::expect_error(check_nifti_header(arr)) tfile = tempimg(nim) check_nifti_header(tfile) check_nifti_header(RNifti::readNifti(tfile)) check_nifti_header(c(tfile, tfile)) check_nifti_header(list(tfile, tfile)) check_nifti_header(factor(tfile))
Simple check to see if input is character or of class nifti
check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'nifti' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'character' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'factor' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'list' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'array' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = FALSE, ... ) ## S4 method for signature 'anlz' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'ANY' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... )
check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'nifti' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'character' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'factor' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'list' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'array' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = FALSE, ... ) ## S4 method for signature 'anlz' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... ) ## S4 method for signature 'ANY' check_nifti( x, reorient = FALSE, allow.array = FALSE, fast = FALSE, need_header = TRUE, ... )
x |
character path of image or an object of class nifti, or array |
reorient |
(logical) passed to |
allow.array |
(logical) Are array types allowed (TRUE) or should there be an error if the object is not character or class nifti. |
fast |
if |
need_header |
if |
... |
additional arguments to pass to |
nifti object or array if allow.array=TRUE and x is an array
John Muschelli [email protected]
x = nifti() check_nifti(x) set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) check_nifti(nim) check_nifti(as.anlz(nim)) testthat::expect_error(check_nifti(arr, allow.array = FALSE)) tfile = tempimg(nim) check_nifti(c(tfile, tfile)) check_nifti(list(tfile, tfile)) check_nifti(factor(c(tfile, tfile))) check_nifti(RNifti::readNifti(tfile))
x = nifti() check_nifti(x) set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) check_nifti(nim) check_nifti(as.anlz(nim)) testthat::expect_error(check_nifti(arr, allow.array = FALSE)) tfile = tempimg(nim) check_nifti(c(tfile, tfile)) check_nifti(list(tfile, tfile)) check_nifti(factor(c(tfile, tfile))) check_nifti(RNifti::readNifti(tfile))
This function checks if an output filename is not NULL in conjunction whether the user would like to return an image
check_outfile(outfile, retimg, fileext = ".nii.gz")
check_outfile(outfile, retimg, fileext = ".nii.gz")
outfile |
output filename or NULL |
retimg |
Should an image be returned |
fileext |
a non-empty character vector giving the file extension |
Filename of output file or a temporary filename
Ensures the output to be a character filename (or vector) from an input
image or nifti
.
checkimg(file, allow_array = FALSE, ...) ## S4 method for signature 'nifti' checkimg(file, allow_array = FALSE, ...) ## S4 method for signature 'ANY' checkimg(file, allow_array = FALSE, ...) ## S4 method for signature 'character' checkimg(file, allow_array = FALSE, ...) ## S4 method for signature 'list' checkimg(file, allow_array = FALSE, ...)
checkimg(file, allow_array = FALSE, ...) ## S4 method for signature 'nifti' checkimg(file, allow_array = FALSE, ...) ## S4 method for signature 'ANY' checkimg(file, allow_array = FALSE, ...) ## S4 method for signature 'character' checkimg(file, allow_array = FALSE, ...) ## S4 method for signature 'list' checkimg(file, allow_array = FALSE, ...)
file |
character or |
allow_array |
allow arrays to be passed in |
... |
options passed to |
character filename of image or temporary nii, with .nii extension
John Muschelli [email protected]
Ensures the output to be a character filename (or vector) from an input
image or nifti
, but not gzipped and has .nii
extension
checknii(file, ...) ## S4 method for signature 'nifti' checknii(file, ...) ## S4 method for signature 'factor' checknii(file, ...) ## S4 method for signature 'character' checknii(file, ...) ## S4 method for signature 'list' checknii(file, ...) ## S4 method for signature 'ANY' checknii(file, ...) ensure_nii(file, ...)
checknii(file, ...) ## S4 method for signature 'nifti' checknii(file, ...) ## S4 method for signature 'factor' checknii(file, ...) ## S4 method for signature 'character' checknii(file, ...) ## S4 method for signature 'list' checknii(file, ...) ## S4 method for signature 'ANY' checknii(file, ...) ensure_nii(file, ...)
file |
character or |
... |
options passed to |
character filename of image or temporary nii, with .nii extension
John Muschelli [email protected]
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = checknii func(nim) func(rnifti) func(timg) func(limg)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = checknii func(nim) func(rnifti) func(timg) func(limg)
Ensures the output to be a character filename (or vector)
from an input
image or nifti
, to be gzipped and has .nii.gz
extension
checkniigz(file, ...) ## S4 method for signature 'nifti' checkniigz(file, ...) ## S4 method for signature 'ANY' checkniigz(file, ...) ## S4 method for signature 'factor' checkniigz(file, ...) ## S4 method for signature 'character' checkniigz(file, ...) ## S4 method for signature 'list' checkniigz(file, ...) ensure_nii_gz(file, ...)
checkniigz(file, ...) ## S4 method for signature 'nifti' checkniigz(file, ...) ## S4 method for signature 'ANY' checkniigz(file, ...) ## S4 method for signature 'factor' checkniigz(file, ...) ## S4 method for signature 'character' checkniigz(file, ...) ## S4 method for signature 'list' checkniigz(file, ...) ensure_nii_gz(file, ...)
file |
character or |
... |
options passed to |
Character filename of image or temporary nii, with .nii.gz extension
John Muschelli [email protected]
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = checkniigz func(nim) func(rnifti) func(timg) func(limg)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = checkniigz func(nim) func(rnifti) func(timg) func(limg)
Find Center of Gravity of Image, after thresholding
cog(img, thresh = 0, ceil = FALSE, warn = TRUE)
cog(img, thresh = 0, ceil = FALSE, warn = TRUE)
img |
Object of class nifti |
thresh |
threshold for image, will find |
ceil |
Run |
warn |
Produce a warning if the image is empty after thresholding |
Vector of length 3
dims = rep(20, 3) x = array(rnorm(prod(dims)), dim = dims) img = nifti(x, dim= dims, datatype = convert.datatype()$FLOAT32, cal.min = min(x), cal.max = max(x), pixdim = rep(1, 4)) cog(img)
dims = rep(20, 3) x = array(rnorm(prod(dims)), dim = dims) img = nifti(x, dim= dims, datatype = convert.datatype()$FLOAT32, cal.min = min(x), cal.max = max(x), pixdim = rep(1, 4)) cog(img)
Adds a series of colors mapped to a value
colorbar(breaks, col, text.col = "white", labels = TRUE, maxleft = 0.95)
colorbar(breaks, col, text.col = "white", labels = TRUE, maxleft = 0.95)
breaks |
a set of finite numeric breakpoints for the
colours (see |
col |
a list of colors (see |
text.col |
axis and text label color |
labels |
labels for tick marks - see |
maxleft |
Extent the left hand for colorbar |
A plot
Much of this was taken from vertical.image.legend
from
the aqfig
package
Copies slots of a nifti
object to an array. This is useful if you're
subsetting 4D data and getting an array out
copyNIfTIHeader( img, arr, drop_slots = c(".Data", "dim_"), drop = TRUE, onlylast = TRUE, warn = TRUE, ... )
copyNIfTIHeader( img, arr, drop_slots = c(".Data", "dim_"), drop = TRUE, onlylast = TRUE, warn = TRUE, ... )
img |
object of class nifti to copy header |
arr |
array to copy header information |
drop_slots |
Slots not to copy over from header |
drop |
Should |
onlylast |
if |
warn |
if |
... |
arguments to pass to |
Object of class nifti the size of arr
img = nifti(img = array(rnorm(10^4), dim=rep(10, 4)), dim=rep(10, 4), datatype = 16) sub = img[,,,1:3] copyNIfTIHeader(img, sub) sub = img[,,,1, drop=FALSE] copyNIfTIHeader(img, sub) copyNIfTIHeader(img, sub, drop = FALSE)
img = nifti(img = array(rnorm(10^4), dim=rep(10, 4)), dim=rep(10, 4), datatype = 16) sub = img[,,,1:3] copyNIfTIHeader(img, sub) sub = img[,,,1, drop=FALSE] copyNIfTIHeader(img, sub) copyNIfTIHeader(img, sub, drop = FALSE)
Cuts a numeric image into an integer factor, with the option of a mask.
## S3 method for class 'nifti' cut(x, breaks, ..., mask) ## S3 method for class 'anlz' cut(x, ..., mask)
## S3 method for class 'nifti' cut(x, breaks, ..., mask) ## S3 method for class 'anlz' cut(x, ..., mask)
x |
Object of class |
breaks |
either a numeric vector of two or more unique cut points
or a single number (greater than or equal to 2)
giving the number of intervals into which x is to be cut.
Passed to |
... |
Arguments passed to |
mask |
object to subset the image. If missing, then all values of the image are used |
Object of class nifti
with an attribute
of levels
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 cut(img, mask = mask, breaks = 4)
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 cut(img, mask = mask, breaks = 4)
Tries to figure out the correct datatype for image. Useful for image masks - makes them binary if
datatyper( img, type_string = NULL, datatype = NULL, bitpix = NULL, trybyte = TRUE, warn = TRUE )
datatyper( img, type_string = NULL, datatype = NULL, bitpix = NULL, trybyte = TRUE, warn = TRUE )
img |
nifti object (or character of filename) |
type_string |
(NULL) character of datatype and bitpix. Supercedes
both datatype and bitpix. If specified
|
datatype |
(NULL) character of datatype see
|
bitpix |
(NULL) character of bitpix see
|
trybyte |
(logical) Should you try to make a byte (UINT8) if image in c(0, 1)? |
warn |
Should a warning be issued if defaulting to FLOAT32? |
object of type nifti
img = nifti(array(rnorm(10^3, sd = 1000), dim = rep(10, 3))) rimg = round(img) newnii(datatyper(rimg)) rimg = datatyper(rimg, type_string= "FLOAT32")
img = nifti(array(rnorm(10^3, sd = 1000), dim = rep(10, 3))) rimg = round(img) newnii(datatyper(rimg)) rimg = datatyper(rimg, type_string= "FLOAT32")
Computes the density of values of an image with the option for a mask.
## S3 method for class 'nifti' density(x, ..., mask) ## S3 method for class 'anlz' density(x, ..., mask)
## S3 method for class 'nifti' density(x, ..., mask) ## S3 method for class 'anlz' density(x, ..., mask)
x |
Object of class |
... |
Arguments passed to |
mask |
object to subset the image. If missing, then all values of the image are plotted. |
Output of density
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 density(img, mask = mask) density(img) density(as.anlz(img), mask = mask) density(as.anlz(img))
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 density(img, mask = mask) density(img) density(as.anlz(img), mask = mask) density(as.anlz(img))
Simple wrapper to calculate the Dice Coefficient/Similarity Index from a table
dicer(tab, verbose = TRUE)
dicer(tab, verbose = TRUE)
tab |
table or matrix that is 2 by 2 |
verbose |
should the Dice be printed before returned? |
Numeric scalar (one number)
tab = matrix(c(1000, 20, 20, 400), ncol = 2) dicer(tab)
tab = matrix(c(1000, 20, 20, 400), ncol = 2) dicer(tab)
Copy of oro.nifti
's orthographic
function
with some tweaks such as adding L/R designations for left and right
double_ortho( x, y = NULL, col.y = gray(0:64/64), NA.x = TRUE, mfrow = c(2, 4), add = FALSE, ... )
double_ortho( x, y = NULL, col.y = gray(0:64/64), NA.x = TRUE, mfrow = c(2, 4), add = FALSE, ... )
x |
is an object of class nifti or similar. |
y |
is an object of class nifti or similar to be set aside x. |
col.y |
is grayscale (by default). |
NA.x |
Set any values of 0 in |
mfrow |
(numeric) layout of the 3 slices |
add |
Should the y-plot be added or its own plot? Used
in |
... |
other arguments to |
Drops dimensions of an image that has all irrelevant values
dropEmptyImageDimensions( img, value = 0, threshold = 0, other.imgs = NULL, keep_ind = FALSE, reorient = FALSE ) drop_empty_dim( img, value = 0, threshold = 0, other.imgs = NULL, keep_ind = FALSE, reorient = FALSE )
dropEmptyImageDimensions( img, value = 0, threshold = 0, other.imgs = NULL, keep_ind = FALSE, reorient = FALSE ) drop_empty_dim( img, value = 0, threshold = 0, other.imgs = NULL, keep_ind = FALSE, reorient = FALSE )
img |
nifti object |
value |
Value to check against. If zero, then
|
threshold |
Drop dimension if fewer than |
other.imgs |
List of other nifti objects or filenames
to apply the same dropping as |
keep_ind |
keep indices in output. Will return list, even if
|
reorient |
Should image be reoriented if a filename? |
List of output image indices, and other images
if other.imgs
not specified or keep_ind = TRUE
.
Otherwise object of class nifti
drop_empty_dim
is a shorthand for dropEmptyImageDimensions
with all the same arguments. Also, NA
are set to zero.
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) dnim = dropEmptyImageDimensions(nim, keep_ind = TRUE) new_nim = dnim$outimg names(dnim) dnim = dropEmptyImageDimensions(nim, keep_ind = TRUE, other.imgs = nim) dims = rep(10, 4) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) testthat::expect_error( {dnim = dropEmptyImageDimensions(nim, keep_ind = TRUE)} )
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) dnim = dropEmptyImageDimensions(nim, keep_ind = TRUE) new_nim = dnim$outimg names(dnim) dnim = dropEmptyImageDimensions(nim, keep_ind = TRUE, other.imgs = nim) dims = rep(10, 4) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) testthat::expect_error( {dnim = dropEmptyImageDimensions(nim, keep_ind = TRUE)} )
Make a mask of an image that has all irrelevant values
emptyImageDimensionsMask(img, ..., reorient = FALSE) empty_dim_mask(img, ..., reorient = FALSE)
emptyImageDimensionsMask(img, ..., reorient = FALSE) empty_dim_mask(img, ..., reorient = FALSE)
img |
nifti object |
... |
Arguments to be passed to |
reorient |
Should image be reoriented if a filename? |
Object of class nifti
, with binary values
empty_dim_mask
is a shorthand for emptyImageDimensionsMask
with all the same arguments.
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) out = emptyImageDimensionsMask(nim) out_arr = emptyImageDimensionsMask(arr) testthat::expect_equal(out_arr, array(out, dim = dim(out))) out_arr = empty_dim_mask(arr)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) out = emptyImageDimensionsMask(nim) out_arr = emptyImageDimensionsMask(arr) testthat::expect_equal(out_arr, array(out, dim = dim(out))) out_arr = empty_dim_mask(arr)
Forces an array output for manipulation and overall conversion
ensure_array(img)
ensure_array(img)
img |
Image object ( |
Array of same dimensions as image object
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = ensure_array func(arr) func(nim) func(rnifti) func(timg) func(limg[[1]])
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = ensure_array func(arr) func(nim) func(rnifti) func(timg) func(limg[[1]])
Fast Dice Tabulation
fast_dice_tab(x, y) fast_dice(x, y, verbose = FALSE)
fast_dice_tab(x, y) fast_dice(x, y, verbose = FALSE)
x |
A |
y |
A |
verbose |
A logical indicating output |
A table object
library(oro.nifti) set.seed(20161007) dims = rep(10, 3) arr = array(rnorm(10*10*10), dim = dims) nim = oro.nifti::nifti(arr) > -1 fast_dice_tab(nim, nim) fast_dice(nim, nim) == 1
library(oro.nifti) set.seed(20161007) dims = rep(10, 3) arr = array(rnorm(10*10*10), dim = dims) nim = oro.nifti::nifti(arr) > -1 fast_dice_tab(nim, nim) fast_dice(nim, nim) == 1
This function calls the readNifti
function from the
RNifti
package, and then converts the image to a nifti
object
fast_readnii(fname, dtype = TRUE, drop_dim = TRUE)
fast_readnii(fname, dtype = TRUE, drop_dim = TRUE)
fname |
file name of the NIfTI file. |
dtype |
Should |
drop_dim |
Should |
A nifti
object
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) tfile = tempfile(fileext = ".nii.gz") write_nifti(nim, tfile) rimg = fast_readnii(tfile)
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) tfile = tempfile(fileext = ".nii.gz") write_nifti(nim, tfile) rimg = fast_readnii(tfile)
Get the image file extension, either .nii, .hdr, .nii.gz, or .hdr.gz
file_imgext(file, withdot = TRUE)
file_imgext(file, withdot = TRUE)
file |
Vector of character filenames |
withdot |
Should the extension begin with |
Vector of extensions. If withdot = FALSE
, then will
return nii
instead of .nii
Simple wrapper for setting non-finite values to zero
finite_img(img, replace = 0) ## S4 method for signature 'nifti' finite_img(img, replace = 0) ## S4 method for signature 'array' finite_img(img, replace = 0) ## S4 method for signature 'ANY' finite_img(img, replace = 0) ## S4 method for signature 'character' finite_img(img, replace = 0) ## S4 method for signature 'list' finite_img(img, replace = 0)
finite_img(img, replace = 0) ## S4 method for signature 'nifti' finite_img(img, replace = 0) ## S4 method for signature 'array' finite_img(img, replace = 0) ## S4 method for signature 'ANY' finite_img(img, replace = 0) ## S4 method for signature 'character' finite_img(img, replace = 0) ## S4 method for signature 'list' finite_img(img, replace = 0)
img |
character path of image or
an object of class |
replace |
Value to replace non-finite values to |
nifti object
John Muschelli [email protected]
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) finite_img(arr) tfile = tempimg(nim) checkimg(c(tfile, tfile)) checkimg(list(tfile, tfile)) finite_img(list(tfile, tfile)) finite_img(c(tfile, tfile)) img = RNifti::readNifti(tfile) checkimg(img) img[c(5, 6, 7, 8)] = c(NA, NaN, Inf, -Inf) stopifnot(!any(c(is.na(c(finite_img(img))))))
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) finite_img(arr) tfile = tempimg(nim) checkimg(c(tfile, tfile)) checkimg(list(tfile, tfile)) finite_img(list(tfile, tfile)) finite_img(c(tfile, tfile)) img = RNifti::readNifti(tfile) checkimg(img) img[c(5, 6, 7, 8)] = c(NA, NaN, Inf, -Inf) stopifnot(!any(c(is.na(c(finite_img(img))))))
This image will flip x, y, or z direction
flip_img(img, x = FALSE, y = FALSE, z = FALSE, ...)
flip_img(img, x = FALSE, y = FALSE, z = FALSE, ...)
img |
nifti object or character filename |
x |
(logical) Flip x direction |
y |
(logical) Flip y direction |
z |
(logical) Flip z direction |
... |
Arguments passed to |
Object of class nifti
img = random_nifti(rep(15, 3)) flipped = flip_img(img, x = TRUE, y = TRUE, z = TRUE) img = random_nifti(rep(15, 2)) flipped = flip_img(img, x = TRUE) testthat::expect_error(flip_img(img, z= TRUE))
img = random_nifti(rep(15, 3)) flipped = flip_img(img, x = TRUE, y = TRUE, z = TRUE) img = random_nifti(rep(15, 2)) flipped = flip_img(img, x = TRUE) testthat::expect_error(flip_img(img, z= TRUE))
Creates a list of indices of an image that has all irrelevant values
getEmptyImageDimensions(img, value = 0, threshold = 0, reorient = FALSE, ...) get_empty_dim(img, value = 0, threshold = 0, reorient = FALSE, ...)
getEmptyImageDimensions(img, value = 0, threshold = 0, reorient = FALSE, ...) get_empty_dim(img, value = 0, threshold = 0, reorient = FALSE, ...)
img |
nifti object or array |
value |
Value to check against. If zero, then
|
threshold |
Include dimension if fewer than |
reorient |
Should image be reoriented if a filename? |
... |
additional arguments to pass to
|
List of length 3 of indices.
get_empty_dim
is a shorthand for getEmptyImageDimensions
with all the same arguments. Also, NA
are set to zero.
arr = array(rbinom(1000, size = 1, prob = 0.2), dim = c(10,10,10)) arr[,,1] = 0 arr[2:3,,] = 0 getEmptyImageDimensions(arr)
arr = array(rbinom(1000, size = 1, prob = 0.2), dim = c(10,10,10)) arr[,,1] = 0 arr[2:3,,] = 0 getEmptyImageDimensions(arr)
Computes and displays a histogram of the values of an image with the option for a mask.
## S3 method for class 'nifti' hist(x, ..., mask) ## S3 method for class 'anlz' hist(x, ..., mask)
## S3 method for class 'nifti' hist(x, ..., mask) ## S3 method for class 'anlz' hist(x, ..., mask)
x |
Object of class |
... |
Arguments passed to |
mask |
object to subset the image. If missing, then all values of the image are plotted. |
Output of hist
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 hist(img, mask = mask)
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 hist(img, mask = mask)
Creates a matrix, where the voxels are on the rows and images are on the columns
images2matrix(imgs, mask = NULL)
images2matrix(imgs, mask = NULL)
imgs |
Vector of files or list of images (niftiImage, array, or nifti) |
mask |
Binary image to subset the voxels |
Matrix of V by p, where V is the product of the dimensions of one image or the number of voxels in the mask, and p is the number of images
set.seed(5) dims = rep(10, 3) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) imgs = list(nim, arr) mask = nim > 2 mat1 = images2matrix(imgs) mat2 = images2matrix(list(nim, nim)) if (packageVersion("oro.nifti") >= package_version("0.10.2")) { testthat::expect_equal(mat1, mat2) } else { testthat::expect_error(testthat::expect_equal(mat1, mat2)) } mat1 = images2matrix(imgs, mask = mask) mat2 = images2matrix(list(nim, nim), mask)
set.seed(5) dims = rep(10, 3) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) imgs = list(nim, arr) mask = nim > 2 mat1 = images2matrix(imgs) mat2 = images2matrix(list(nim, nim)) if (packageVersion("oro.nifti") >= package_version("0.10.2")) { testthat::expect_equal(mat1, mat2) } else { testthat::expect_error(testthat::expect_equal(mat1, mat2)) } mat1 = images2matrix(imgs, mask = mask) mat2 = images2matrix(list(nim, nim), mask)
Takes in an image and a color scheme, converts that image
into a data.frame
with the data and a color mapping.
img_colour_df(img, zlim = NULL, breaks = NULL, col = gray(0:64/64)) img_color_df(...)
img_colour_df(img, zlim = NULL, breaks = NULL, col = gray(0:64/64)) img_color_df(...)
img |
an object to be coerced to |
zlim |
Limits for the domain of the intensities |
breaks |
Breaks for the intensities to map to colors |
col |
Colors to map intensities |
... |
not used |
A data.frame
with the first columns being the x,y,z (maybe t)
coordinates (named dim
and the dimension number), a value
column that contains the intensity information, and a colour
column
representing the color that voxel maps to
img_color_df
is a duplicate of img_colour_df
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) df = img_colour_df(img) df = img_color_df(img)
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) df = img_colour_df(img) df = img_color_df(img)
Extract image xyz indices (in voxels or millimeters), with the option to append the values
img_indices(img, mask = NULL, add_values = FALSE, units = c("index", "mm"))
img_indices(img, mask = NULL, add_values = FALSE, units = c("index", "mm"))
img |
Object of class |
mask |
Mask to be applied for indices the index |
add_values |
Should the value be column-bound to the matrix |
units |
Should the indices be in xyz-coordinates or millimeters. |
Matrix of 3 columns if add_values = FALSE
or 4 columns,
otherwise.
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) ind = img_indices(nim) ind2 = img_indices(nim, mask = nim > 2) # 3d example set.seed(5) dims = rep(10, 3) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) ind = img_indices(nim) ind2 = img_indices(nim, mask = nim > 2) testthat::expect_equal(colnames(ind2), c("x", "y", "z")) ind2 = img_indices(nim, mask = nim > 2, add_values = TRUE) testthat::expect_equal(colnames(ind2), c("x", "y", "z", "value"))
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) ind = img_indices(nim) ind2 = img_indices(nim, mask = nim > 2) # 3d example set.seed(5) dims = rep(10, 3) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) ind = img_indices(nim) ind2 = img_indices(nim, mask = nim > 2) testthat::expect_equal(colnames(ind2), c("x", "y", "z")) ind2 = img_indices(nim, mask = nim > 2, add_values = TRUE) testthat::expect_equal(colnames(ind2), c("x", "y", "z", "value"))
Turns a a list of 3D images into a 4D time series image
img_list_to_ts(imgs, copy_nifti = TRUE, warn = TRUE)
img_list_to_ts(imgs, copy_nifti = TRUE, warn = TRUE)
imgs |
object of class |
copy_nifti |
Should a |
warn |
Should a warning be printed if object is not class
|
Object of class nifti
If the object is not of class list
,
then the object is returned
Turns a 4D time series image to a Data.frame
img_ts_to_df(imgs, warn = FALSE)
img_ts_to_df(imgs, warn = FALSE)
imgs |
object of class |
warn |
Should a warning be printed if object is not class
|
Matrix of values
Turns a 4D time series image to a list of 3D images
img_ts_to_list(imgs, copy_nifti = TRUE, warn = TRUE)
img_ts_to_list(imgs, copy_nifti = TRUE, warn = TRUE)
imgs |
object of class |
copy_nifti |
Should |
warn |
Should a warning be printed if object is not class
|
List of images
If the object is not of class nifti
or have
4 dimensions, then the object is returned
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) simg = img_ts_to_list(nim) simg_arr = img_ts_to_list(arr) back = img_list_to_ts(simg_arr) slist = lapply(simg, function(x) array(x, dim(x))) testthat::expect_equal(slist, simg_arr) simg_arr = img_ts_to_matrix(arr) simg_arr = img_ts_to_df(arr)
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) simg = img_ts_to_list(nim) simg_arr = img_ts_to_list(arr) back = img_list_to_ts(simg_arr) slist = lapply(simg, function(x) array(x, dim(x))) testthat::expect_equal(slist, simg_arr) simg_arr = img_ts_to_matrix(arr) simg_arr = img_ts_to_df(arr)
Turns a 4D time series image to a Matrix
img_ts_to_matrix(imgs, warn = FALSE)
img_ts_to_matrix(imgs, warn = FALSE)
imgs |
object of class |
warn |
Should a warning be printed if object is not class
|
Matrix of values
Takes an image, masks it by
mask
, and returns an object of class nifti
mask_img(img, mask, allow.NA = TRUE)
mask_img(img, mask, allow.NA = TRUE)
img |
object of class |
mask |
array or object of class |
allow.NA |
allow NAs in the mask |
Object of class nifti with values outside mask set to 0 if mask is 0 and NA if mask is NA and img if mask == 1
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) mask = abs(nim) > 1 masked = mask_img(nim, mask) mask[mask == 0] = NA na_masked = mask_img(nim, mask, allow.NA = TRUE) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) mask = nim > 0 timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = function(...) mask_img(..., mask = mask) func(arr) func(nim) func(rnifti) func(timg) lapply(limg, func)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) mask = abs(nim) > 1 masked = mask_img(nim, mask) mask[mask == 0] = NA na_masked = mask_img(nim, mask, allow.NA = TRUE) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) mask = nim > 0 timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = function(...) mask_img(..., mask = mask) func(arr) func(nim) func(rnifti) func(timg) lapply(limg, func)
Methods that act on the .Data
field in a
NIfTI/ANALYZE image but only on values inside a mask.
mask_vals(object, mask) mask_vals(object, mask) <- value ## S4 replacement method for signature 'nifti' mask_vals(object, mask) <- value ## S4 replacement method for signature 'anlz' mask_vals(object, mask) <- value ## S4 replacement method for signature 'array' mask_vals(object, mask) <- value
mask_vals(object, mask) mask_vals(object, mask) <- value ## S4 replacement method for signature 'nifti' mask_vals(object, mask) <- value ## S4 replacement method for signature 'anlz' mask_vals(object, mask) <- value ## S4 replacement method for signature 'array' mask_vals(object, mask) <- value
object |
is an object of class |
mask |
is an object of class |
value |
is the value to assign to the |
set.seed(2022) img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 1.5 mask_vals(img, mask) testthat::expect_equal(sum(mask_vals(img, mask)), 117.628200302518) mask_vals(img, mask) = rep(4, sum(mask)) mask_vals(img, as(mask, "array")) = rep(4, sum(mask)) mask_vals(as(img, "array"), as(mask, "array")) = rep(4, sum(mask))
set.seed(2022) img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 1.5 mask_vals(img, mask) testthat::expect_equal(sum(mask_vals(img, mask)), 117.628200302518) mask_vals(img, mask) = rep(4, sum(mask)) mask_vals(img, as(mask, "array")) = rep(4, sum(mask)) mask_vals(as(img, "array"), as(mask, "array")) = rep(4, sum(mask))
Simple wrapper for replacing indices with a value
maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'nifti' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'character' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'factor' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'list' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'array' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'anlz' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'ANY' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) mask_empty_dim(img, ...)
maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'nifti' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'character' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'factor' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'list' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'array' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'anlz' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) ## S4 method for signature 'ANY' maskEmptyImageDimensions(img, inds, reorient = FALSE, mask.value = 0, ...) mask_empty_dim(img, ...)
img |
image, nifti object, or array |
inds |
indices of subset from |
reorient |
Should image be reoriented if a filename? |
mask.value |
Value to replace voxels outside the mask. |
... |
not used |
Object of class nifti
or array
if nifti
is not supplied
mask_empty_dim
is a shorthand for
maskEmptyImageDimensions
with all the same arguments.
getEmptyImageDimensions
,
dropEmptyImageDimensions
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) inds = getEmptyImageDimensions(nim) inds_arr = getEmptyImageDimensions(arr) res = maskEmptyImageDimensions(nim, inds = inds, mask.value = NA) res2 = maskEmptyImageDimensions(arr, inds = inds_arr, mask.value = NA) testthat::expect_equal(array(res, dim = dim(res)), array(res2, dim = dim(res2))) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) inds = getEmptyImageDimensions(nim) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) mask_empty_dim(nim, inds = inds) func = function(...) maskEmptyImageDimensions(..., inds = inds) func(arr) func(nim) func(rnifti) func(timg) func(limg)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) inds = getEmptyImageDimensions(nim) inds_arr = getEmptyImageDimensions(arr) res = maskEmptyImageDimensions(nim, inds = inds, mask.value = NA) res2 = maskEmptyImageDimensions(arr, inds = inds_arr, mask.value = NA) testthat::expect_equal(array(res, dim = dim(res)), array(res2, dim = dim(res2))) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) inds = getEmptyImageDimensions(nim) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) mask_empty_dim(nim, inds = inds) func = function(...) maskEmptyImageDimensions(..., inds = inds) func(arr) func(nim) func(rnifti) func(timg) func(limg)
Computes the mean of values of an image with the option for a mask.
## S3 method for class 'nifti' mean(x, ..., mask) ## S3 method for class 'anlz' mean(x, ..., mask)
## S3 method for class 'nifti' mean(x, ..., mask) ## S3 method for class 'anlz' mean(x, ..., mask)
x |
Object of class |
... |
Arguments passed to |
mask |
object to subset the image. If missing, then all values of the image are plotted. |
Output of mean
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 mean(img, mask = mask)
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 mean(img, mask = mask)
Calculates the range of values in an image, then scales the image minimum to 0 and maximum to 1
minmax_img(img) ## S4 method for signature 'nifti' minmax_img(img) ## S4 method for signature 'array' minmax_img(img) ## S4 method for signature 'ANY' minmax_img(img) ## S4 method for signature 'character' minmax_img(img) ## S4 method for signature 'factor' minmax_img(img) ## S4 method for signature 'list' minmax_img(img)
minmax_img(img) ## S4 method for signature 'nifti' minmax_img(img) ## S4 method for signature 'array' minmax_img(img) ## S4 method for signature 'ANY' minmax_img(img) ## S4 method for signature 'character' minmax_img(img) ## S4 method for signature 'factor' minmax_img(img) ## S4 method for signature 'list' minmax_img(img)
img |
character path of image or
an object of class |
A nifti
object (or list of them) or class of
object passed in if not specified
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) mimg = minmax_img(nim) marr = minmax_img(arr) testthat::expect_equal(array(mimg, dim = dim(mimg)), marr) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = minmax_img func(arr) func(nim) func(rnifti) func(timg) func(limg)
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) mimg = minmax_img(nim) marr = minmax_img(arr) testthat::expect_equal(array(mimg, dim = dim(mimg)), marr) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = minmax_img func(arr) func(nim) func(rnifti) func(timg) func(limg)
Creates a multi-row or multi-column plot with image slices and the potential for overlays as well.
multi_overlay( x, y = NULL, z = NULL, w = 1, mask = NULL, col.x = gray(0:64/64), col.y = hotmetal(), zlim.x = NULL, zlim.y = NULL, ybreaks = NULL, plane = c("axial", "coronal", "sagittal"), xlab = "", ylab = "", axes = FALSE, direction = c("horizontal", "vertical"), par.opts = list(oma = c(0, 0, 0, 0), mar = rep(0, 4), bg = "black"), text = NULL, text.x = 0.5, text.y = 1.4, text.cex = 2.5, text.col = "white", main = NULL, main.col = text.col, main.cex = text.cex, NA.x = TRUE, NA.y = TRUE, pdim = NULL, useRaster = TRUE, ... ) multi_overlay_center(x, y = NULL, ...)
multi_overlay( x, y = NULL, z = NULL, w = 1, mask = NULL, col.x = gray(0:64/64), col.y = hotmetal(), zlim.x = NULL, zlim.y = NULL, ybreaks = NULL, plane = c("axial", "coronal", "sagittal"), xlab = "", ylab = "", axes = FALSE, direction = c("horizontal", "vertical"), par.opts = list(oma = c(0, 0, 0, 0), mar = rep(0, 4), bg = "black"), text = NULL, text.x = 0.5, text.y = 1.4, text.cex = 2.5, text.col = "white", main = NULL, main.col = text.col, main.cex = text.cex, NA.x = TRUE, NA.y = TRUE, pdim = NULL, useRaster = TRUE, ... ) multi_overlay_center(x, y = NULL, ...)
x |
List of images of class |
y |
List of images of class |
z |
Slice to display. |
w |
3D volume to display if |
mask |
|
col.x |
Color to display x images |
col.y |
Color to display y images |
zlim.x |
Limits for |
zlim.y |
Limits for |
ybreaks |
(numeric) breaks for y to passed to
|
plane |
the plane of acquisition to be displayed |
xlab |
Label for x-axis |
ylab |
Label for y-axis |
axes |
Should axes be displayed |
direction |
Should images be a row or column? Ignored if
|
par.opts |
Options to pass to |
text |
Text to be displayed |
text.x |
Location of text in x-domain |
text.y |
Location of text in y-domain |
text.cex |
Multiplier for text font |
text.col |
Color for |
main |
Title for each plot |
main.col |
Color for |
main.cex |
Multiplier for text font. Will default to |
NA.x |
Should |
NA.y |
Should |
pdim |
Pixel dimensions if passing in arrays. Will be overridden if
|
useRaster |
if |
... |
Additional arguments to pass to |
set.seed(5) dims = rep(10, 4) arr = array(rnorm(prod(dims)), dim = dims) arr[,,,c(3, 5)] = rpois(1000*2, lambda = 2) nim = oro.nifti::nifti(arr) mask = nim > 2 add_mask = nim[,,,1] > 0 imgs = img_ts_to_list(nim) masks = img_ts_to_list(mask) multi_overlay(imgs, masks) multi_overlay(imgs, masks, main = "hey", direction = "vertical", plane = "coronal") multi_overlay(imgs, masks, mask = add_mask, main = "hey") ## Not run: if (requireNamespace("brainR", quietly = TRUE)) { visits = 1:3 y = paste0("Visit_", visits, ".nii.gz") y = system.file(y, package = "brainR") y = lapply(y, readnii) y = lapply(y, function(r){ pixdim(r) = c(0, rep(1, 3), rep(0, 4)) dropImageDimension(r) }) x = system.file("MNI152_T1_1mm_brain.nii.gz", package = "brainR") x = readnii(x) mask = x >0 x = lapply(visits, function(tmp){ x }) alpha = function(col, alpha = 1) { cols = t(col2rgb(col, alpha = FALSE)/255) rgb(cols, alpha = alpha) } multi_overlay(x, y, col.y = alpha(hotmetal(), 0.5), mask = mask, main = paste0("\n", "Visit ", visits), text = LETTERS[visits], text.x = 0.9, text.y = 0.1, text.cex = 3) } ## End(Not run)
set.seed(5) dims = rep(10, 4) arr = array(rnorm(prod(dims)), dim = dims) arr[,,,c(3, 5)] = rpois(1000*2, lambda = 2) nim = oro.nifti::nifti(arr) mask = nim > 2 add_mask = nim[,,,1] > 0 imgs = img_ts_to_list(nim) masks = img_ts_to_list(mask) multi_overlay(imgs, masks) multi_overlay(imgs, masks, main = "hey", direction = "vertical", plane = "coronal") multi_overlay(imgs, masks, mask = add_mask, main = "hey") ## Not run: if (requireNamespace("brainR", quietly = TRUE)) { visits = 1:3 y = paste0("Visit_", visits, ".nii.gz") y = system.file(y, package = "brainR") y = lapply(y, readnii) y = lapply(y, function(r){ pixdim(r) = c(0, rep(1, 3), rep(0, 4)) dropImageDimension(r) }) x = system.file("MNI152_T1_1mm_brain.nii.gz", package = "brainR") x = readnii(x) mask = x >0 x = lapply(visits, function(tmp){ x }) alpha = function(col, alpha = 1) { cols = t(col2rgb(col, alpha = FALSE)/255) rgb(cols, alpha = alpha) } multi_overlay(x, y, col.y = alpha(hotmetal(), 0.5), mask = mask, main = paste0("\n", "Visit ", visits), text = LETTERS[visits], text.x = 0.9, text.y = 0.1, text.cex = 3) } ## End(Not run)
Resets the slots of a nifti object, usually because an image was loaded, then copied and filled in with new data instead of making a nifti object from scratch. Just a wrapper for smaller functions
newnii(img, ...)
newnii(img, ...)
img |
nifti object (or character of filename) |
... |
arguments to be passed to |
object of type nifti
Make new nifti object by passing in old nifti and array
niftiarr(img, arr)
niftiarr(img, arr)
img |
object of class nifti |
arr |
array to be passed in to .Data slot |
object of class nifti
Quick helper function to strip off .nii or .nii.gz from filename
nii.stub(x, bn = FALSE)
nii.stub(x, bn = FALSE)
x |
character vector of filenames ending in .nii or .nii.gz |
bn |
Take |
A character vector with the same length as x
Reorient an Image to RPI orientation
orient_rpi(file, verbose = TRUE) orient_rpi_file(file, verbose = TRUE) is_rpi_oriented(file, verbose = FALSE)
orient_rpi(file, verbose = TRUE) orient_rpi_file(file, verbose = TRUE) is_rpi_oriented(file, verbose = FALSE)
file |
Object of class |
verbose |
print diagnostic messages |
List of 3 elements
img
: Reoriented image of class nifti
convention
: Convention (Neurological/Radiological) of original image
orientation
: Original image orientations
'orient_rpi' and 'orient_rpi_file' uses 'RNifti' to ensure the reading orientation
lr_fname = system.file( "nifti", "mniLR.nii.gz", package = "oro.nifti") img = readnii(lr_fname) rl_fname = system.file( "nifti", "mniRL.nii.gz", package = "oro.nifti") rl_img = readnii(rl_fname) stopifnot(all(rl_img[nrow(rl_img):1,,] == img)) reor = orient_rpi(rl_fname) stopifnot(all(img == reor$img)) rev = reverse_orient_rpi(reor$img, convention = reor$convention, orientation = reor$orientation) stopifnot(all(rev == rl_img))
lr_fname = system.file( "nifti", "mniLR.nii.gz", package = "oro.nifti") img = readnii(lr_fname) rl_fname = system.file( "nifti", "mniRL.nii.gz", package = "oro.nifti") rl_img = readnii(rl_fname) stopifnot(all(rl_img[nrow(rl_img):1,,] == img)) reor = orient_rpi(rl_fname) stopifnot(all(img == reor$img)) rev = reverse_orient_rpi(reor$img, convention = reor$convention, orientation = reor$orientation) stopifnot(all(rev == rl_img))
Uses ortho2
to plot differences between a predicted binary
image and the assumed ground truth (roi
).
ortho_diff( img, pred, roi, xyz = NULL, cols = c("#56B4E9", "#D55E00", "#009E73"), levels = c("False Negative", "False Positive", "True Positive"), addlegend = TRUE, center = TRUE, leg.cex = 1.5, ... ) multi_overlay_diff( x, pred, roi, z = NULL, cols = c("#56B4E9", "#D55E00", "#009E73"), ... )
ortho_diff( img, pred, roi, xyz = NULL, cols = c("#56B4E9", "#D55E00", "#009E73"), levels = c("False Negative", "False Positive", "True Positive"), addlegend = TRUE, center = TRUE, leg.cex = 1.5, ... ) multi_overlay_diff( x, pred, roi, z = NULL, cols = c("#56B4E9", "#D55E00", "#009E73"), ... )
img |
image to be underlaid |
pred |
binary segmentation (prediction) |
roi |
binary manual segmentation (ground truth) |
xyz |
coordinate for the center of the crosshairs. |
cols |
colors for false negatives/positives |
levels |
labels for false negatives/positives |
addlegend |
add legend, passed to |
center |
run |
leg.cex |
multiplier for legend size |
... |
arguments to be passed to |
x |
List of images of class |
z |
slice to display |
set.seed(5) dims = rep(10, 3) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) roi = nim > 2 pred = nim > 1.5 ortho_diff(nim, pred, roi) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) mask = nim > 2 pred = nim > 1.5 multi_overlay_diff(nim, roi = mask, pred = pred) if (requireNamespace("brainR", quietly = TRUE)) { visits = 1:3 y = paste0("Visit_", visits, ".nii.gz") y = system.file(y, package = "brainR") y = lapply(y, readnii) y = lapply(y, function(r){ pixdim(r) = c(0, rep(1, 3), rep(0, 4)) dropImageDimension(r) }) x = system.file("MNI152_T1_1mm_brain.nii.gz", package = "brainR") x = readnii(x) mask = x > 0 alpha = function(col, alpha = 1) { cols = t(col2rgb(col, alpha = FALSE)/255) rgb(cols, alpha = alpha) } roi = y[[2]] pred = y multi_overlay_diff(x, roi = roi, pred = pred) multi_overlay_diff(x, roi = roi, pred = pred, mask = mask, main = paste0("\n", "Visit ", visits), text = LETTERS[visits], text.x = 0.9, text.y = 0.1, text.cex = 3) }
set.seed(5) dims = rep(10, 3) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) roi = nim > 2 pred = nim > 1.5 ortho_diff(nim, pred, roi) set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) mask = nim > 2 pred = nim > 1.5 multi_overlay_diff(nim, roi = mask, pred = pred) if (requireNamespace("brainR", quietly = TRUE)) { visits = 1:3 y = paste0("Visit_", visits, ".nii.gz") y = system.file(y, package = "brainR") y = lapply(y, readnii) y = lapply(y, function(r){ pixdim(r) = c(0, rep(1, 3), rep(0, 4)) dropImageDimension(r) }) x = system.file("MNI152_T1_1mm_brain.nii.gz", package = "brainR") x = readnii(x) mask = x > 0 alpha = function(col, alpha = 1) { cols = t(col2rgb(col, alpha = FALSE)/255) rgb(cols, alpha = alpha) } roi = y[[2]] pred = y multi_overlay_diff(x, roi = roi, pred = pred) multi_overlay_diff(x, roi = roi, pred = pred, mask = mask, main = paste0("\n", "Visit ", visits), text = LETTERS[visits], text.x = 0.9, text.y = 0.1, text.cex = 3) }
Copy of oro.nifti
's orthographic
function
with some tweaks such as adding L/R designations for left and right
ortho2( x, y = NULL, xyz = NULL, w = 1, col = gray(0:64/64), col.y = oro.nifti::hotmetal(), zlim = NULL, zlim.y = NULL, NA.x = FALSE, NA.y = TRUE, crosshairs = TRUE, col.crosshairs = "red", xlab = "", ylab = "", axes = FALSE, oma = c(0, 0, 0, ifelse(ycolorbar, 5, 0)), mar = rep(0, 4), bg = "black", text = NULL, text.color = "white", text.cex = 2, text.x = 32, text.y = 32, add.orient = FALSE, mfrow = c(2, 2), ybreaks = NULL, breaks = NULL, addlegend = FALSE, leg.x = 32, leg.y = 32, legend, leg.col, leg.title = NULL, leg.cex, window = NULL, ycolorbar = FALSE, clabels = TRUE, add = TRUE, pdim = NULL, useRaster = is.null(y), mask = NULL, ... )
ortho2( x, y = NULL, xyz = NULL, w = 1, col = gray(0:64/64), col.y = oro.nifti::hotmetal(), zlim = NULL, zlim.y = NULL, NA.x = FALSE, NA.y = TRUE, crosshairs = TRUE, col.crosshairs = "red", xlab = "", ylab = "", axes = FALSE, oma = c(0, 0, 0, ifelse(ycolorbar, 5, 0)), mar = rep(0, 4), bg = "black", text = NULL, text.color = "white", text.cex = 2, text.x = 32, text.y = 32, add.orient = FALSE, mfrow = c(2, 2), ybreaks = NULL, breaks = NULL, addlegend = FALSE, leg.x = 32, leg.y = 32, legend, leg.col, leg.title = NULL, leg.cex, window = NULL, ycolorbar = FALSE, clabels = TRUE, add = TRUE, pdim = NULL, useRaster = is.null(y), mask = NULL, ... )
x |
is an object of class nifti or similar. |
y |
is an object of class nifti or similar for the overlay. |
xyz |
is the coordinate for the center of the crosshairs. |
w |
is the time point to be displayed (4D arrays only). |
col |
is grayscale (by default). |
col.y |
is hotmetal (by default). |
zlim |
is the minimum and maximum ‘z’ values passed into image. |
zlim.y |
is the minimum and maximum ‘z’ values passed into image for the overlay. |
NA.x |
Set any values of 0 in |
NA.y |
Set any values of 0 in |
crosshairs |
is a logical value for the presence of crosshairs in all three orthogonal planes (default = TRUE). |
col.crosshairs |
is the color of the crosshairs (default = red). |
xlab |
is set to "" since all margins are set to zero. |
ylab |
is set to "" since all margins are set to zero. |
axes |
is set to FALSE since all margins are set to zero. |
oma |
is the size of the outer margins in the par function. |
mar |
is the number of lines of margin in the par function. |
bg |
is the background color in the par function. |
text |
allows the user to specify text to appear in the fourth (unused) pane. |
text.color |
is the color of the user-specified text (default = “white"). |
text.cex |
is the size of the user-specified text (default = 2). |
text.x |
x coordinate for text |
text.y |
y coordinate for text |
add.orient |
(logical) Add left/right, A/P, etc. orientation |
mfrow |
(numeric) layout of the 3 slices |
ybreaks |
(numeric) breaks for y to passed to
|
breaks |
(numeric) breaks for x to passed to
|
addlegend |
(logical) add legend? |
leg.x |
(numeric) x coordinate for legend |
leg.y |
(numeric) y coordinate for legend |
legend |
(character) legend text |
leg.col |
(character) Colors for legend |
leg.title |
(character) title for legend |
leg.cex |
(numeric) |
window |
(vector) Length-2 vector to limit image to certain range |
ycolorbar |
(logical) Should a colorbar for |
clabels |
Label for colorbar (see |
add |
Should the y-plot be added or its own plot? Used
in |
pdim |
Pixel dimensions if passing in arrays. Will be overridden if
|
useRaster |
logical; if TRUE a bitmap raster is used to
plot the image instead of polygons. Passed to
|
mask |
If a mask is passed, |
... |
other arguments to the image function may be provided here. |
set.seed(10) x = oro.nifti::nifti(array(rnorm(1000), dim = rep(10, 3))) ortho2(x) y = x > 2 mask = x > 2.5 ortho2(x, y) ortho2(x, y, mask = mask, add.orient = TRUE) ortho2(x, y, mask = mask, add.orient = TRUE, add = FALSE) nim = RNifti::asNifti(x, internal = FALSE) ortho2(nim, y, mask = mask) neurobase::ortho2(nim, x, mask = mask, ybreaks = seq(min(x), max(x), length.out = 65), ycolorbar = TRUE) ortho2(nim, y, mask = mask, add = FALSE) arr_x = as.array(x) arr_y = as.array(y) ortho2( arr_x) ortho2( arr_x, arr_y, useRaster = FALSE) set.seed(10) x = oro.nifti::nifti(array(rnorm(10000), dim = rep(10, 4))) y = x > 2 mask = x > 2.5 ortho2(x, y) set.seed(10) x = oro.nifti::nifti(array(rnorm(100), dim = rep(10, 2))) y = x > 2 mask = x > 2.5 ortho2(x, y)
set.seed(10) x = oro.nifti::nifti(array(rnorm(1000), dim = rep(10, 3))) ortho2(x) y = x > 2 mask = x > 2.5 ortho2(x, y) ortho2(x, y, mask = mask, add.orient = TRUE) ortho2(x, y, mask = mask, add.orient = TRUE, add = FALSE) nim = RNifti::asNifti(x, internal = FALSE) ortho2(nim, y, mask = mask) neurobase::ortho2(nim, x, mask = mask, ybreaks = seq(min(x), max(x), length.out = 65), ycolorbar = TRUE) ortho2(nim, y, mask = mask, add = FALSE) arr_x = as.array(x) arr_y = as.array(y) ortho2( arr_x) ortho2( arr_x, arr_y, useRaster = FALSE) set.seed(10) x = oro.nifti::nifti(array(rnorm(10000), dim = rep(10, 4))) y = x > 2 mask = x > 2.5 ortho2(x, y) set.seed(10) x = oro.nifti::nifti(array(rnorm(100), dim = rep(10, 2))) y = x > 2 mask = x > 2.5 ortho2(x, y)
Get image extensions from a filename
parse_img_ext(file)
parse_img_ext(file)
file |
Character filename to parse |
Extension of file
parse_img_ext("blah.nii.gz") parse_img_ext("blah.mnc") parse_img_ext("blah.nii") parse_img_ext("blah") parse_img_ext("blah.img") parse_img_ext("blah.hdr") parse_img_ext("blah.hdr.gz")
parse_img_ext("blah.nii.gz") parse_img_ext("blah.mnc") parse_img_ext("blah.nii") parse_img_ext("blah") parse_img_ext("blah.img") parse_img_ext("blah.hdr") parse_img_ext("blah.hdr.gz")
Creates output image of the quantiles that each voxel is in, after applying the mask
quantile_img(img, mask = NULL, ...)
quantile_img(img, mask = NULL, ...)
img |
Character vector, or object of class |
mask |
Mask to determine cumulative distribution function (cdf) from |
... |
Additional arguments to pass to |
Object of class nifti
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) qimg = quantile_img(nim) qarr = quantile_img(arr) testthat::expect_equal(qarr, array(qimg, dim = dim(qarr))) qimg = quantile_img(nim, mask = nim > 0)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) qimg = quantile_img(nim) qarr = quantile_img(arr) testthat::expect_equal(qarr, array(qimg, dim = dim(qarr))) qimg = quantile_img(nim, mask = nim > 0)
Computes sample quantiles for an image, with the option of a mask.
## S3 method for class 'nifti' quantile(x, ..., mask) ## S3 method for class 'anlz' quantile(x, ..., mask)
## S3 method for class 'nifti' quantile(x, ..., mask) ## S3 method for class 'anlz' quantile(x, ..., mask)
x |
Object of class |
... |
Arguments passed to |
mask |
object to subset the image. If missing, then all values of the image are used |
Output of quantile
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 quantile(img, mask = mask)
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) mask = img > 0 quantile(img, mask = mask)
Create Random 'nifti' object
random_nifti(dim, ...)
random_nifti(dim, ...)
dim |
dimensions for the 'nifti' object |
... |
arguments to send to |
A 'nifti' object
random_nifti(c(10, 10, 2)) random_nifti(c(10, 10))
random_nifti(c(10, 10, 2)) random_nifti(c(10, 10))
Randomize the values within a mask
randomize_mask(img, mask)
randomize_mask(img, mask)
img |
Object of class nifti with values to be randomized |
mask |
Binary mask indicating which values should be randomized. |
Object of class nifti
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) mask = abs(nim) > 1 randomize_mask(nim, mask)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) mask = abs(nim) > 1 randomize_mask(nim, mask)
This function calls the readnii
function after
calling orient_rpi_file
to force RPI orientation.
read_rpi(file, ..., verbose = TRUE)
read_rpi(file, ..., verbose = TRUE)
file |
file name of the NIfTI file. |
... |
Arguments to pass to |
verbose |
print diagnostics, passed to |
'read_rpi' uses 'RNifti' to ensure the reading orientation
This function calls the readNIfTI
function from the
oro.nifti
package, but sets the reorientation to FALSE
by default
readNIfTI2(..., reorient = FALSE) readnii( ..., reorient = FALSE, dtype = TRUE, drop_dim = TRUE, reset_slope = FALSE, warn = FALSE, rm_extensions = TRUE )
readNIfTI2(..., reorient = FALSE) readnii( ..., reorient = FALSE, dtype = TRUE, drop_dim = TRUE, reset_slope = FALSE, warn = FALSE, rm_extensions = TRUE )
... |
Arguments to pass to |
reorient |
Reorientation argument to pass to |
dtype |
Should |
drop_dim |
Should |
reset_slope |
Reset slope/intercept of image |
warn |
Should warnings from |
rm_extensions |
should niftiExtensions be converted to simple nifti objects? |
nifti
object
Wrapper function to take a vector of values and result in a
nifti
object
remake_img(vec, img, mask = NULL, warn = FALSE, ...)
remake_img(vec, img, mask = NULL, warn = FALSE, ...)
vec |
vector of values to be in resulting image |
img |
object of class |
mask |
binary array/ |
warn |
Should a warning be issued if defaulting to FLOAT32? |
... |
additional arguments passed to |
Object of class nifti
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) remake_img(c(nim), nim) mask = nim > 5 vals = nim[mask] vals = sqrt(vals) remake_img(vals, nim, mask = mask)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) remake_img(c(nim), nim) mask = nim > 5 vals = nim[mask] vals = sqrt(vals) remake_img(vals, nim, mask = mask)
This is a simple function that helps with the case where you want to construct a filename (usually for an image) with the same base of the filename, the same directory (default), but things added to the front or end of that base filename, with the same extension.
remap_filename(x, sub_dir = NULL, prefix = "", suffix = "")
remap_filename(x, sub_dir = NULL, prefix = "", suffix = "")
x |
input filename/character vector |
sub_dir |
sub-directory for the new filename. If |
prefix |
string to put in front of base of filename |
suffix |
string to put at the end of base of filename |
Character vector
fname = file.path("/path/to/file", "original.nii.gz") remap_filename(fname, prefix = "preproc_", "_with_gz") fname = "original.nii" remap_filename(fname, prefix = "note_", "_has_directory") remap_filename(c(fname, "other.nii.gz"), prefix = "note_", "_has_directory")
fname = file.path("/path/to/file", "original.nii.gz") remap_filename(fname, prefix = "preproc_", "_with_gz") fname = "original.nii" remap_filename(fname, prefix = "note_", "_has_directory") remap_filename(c(fname, "other.nii.gz"), prefix = "note_", "_has_directory")
This function is the reverse of dropEmptyImageDimensions
.
If dropEmptyImageDimensions
was run, and the output is a list,
usually if keep_ind = TRUE
, this function reverses that.
replace_dropped_dimensions(img, inds, orig.dim)
replace_dropped_dimensions(img, inds, orig.dim)
img |
Object of class |
inds |
List of length 3 of indices from
|
orig.dim |
Original dimension of pre-dropped image. Output image will have dimensions same as this value |
Object of class nifti
## Not run: # nim is an object of class nifti dd = dropEmptyImageDimensions(nim, keep_ind = TRUE) remake = replace_dropped_dimensions(img = dd$outimg, inds = dd$inds, orig.dim = dd$orig.dim) all.equal(nim, remake) ## End(Not run)
## Not run: # nim is an object of class nifti dd = dropEmptyImageDimensions(nim, keep_ind = TRUE) remake = replace_dropped_dimensions(img = dd$outimg, inds = dd$inds, orig.dim = dd$orig.dim) all.equal(nim, remake) ## End(Not run)
Determines values outside the surface of an image and gives a mask back with those values set to a replacement.
replace_outside_surface( img, value = 0, threshold = 0, replace_value = NA, reorient = FALSE )
replace_outside_surface( img, value = 0, threshold = 0, replace_value = NA, reorient = FALSE )
img |
nifti object or array |
value |
Value to check against. If zero, then
|
threshold |
Include dimension if fewer than |
replace_value |
Value to replace those outside the surface. |
reorient |
Should image be reoriented if a filename?
Passed to |
Creates an array of binary values. If img
is a nifti
object, then a nifti
is returned
set.seed(5) dims = rep(10, 3) arr = array(0, dim = dims) arr[ 3:5, 4:6, c(2, 6:8, 5)] = 1 nim = oro.nifti::nifti(arr) out = replace_outside_surface(nim, replace_value = 0) out_arr = replace_outside_surface(arr, replace_value = 0) testthat::expect_equal(out_arr, array(out, dim = dim(out)))
set.seed(5) dims = rep(10, 3) arr = array(0, dim = dims) arr[ 3:5, 4:6, c(2, 6:8, 5)] = 1 nim = oro.nifti::nifti(arr) out = replace_outside_surface(nim, replace_value = 0) out_arr = replace_outside_surface(arr, replace_value = 0) testthat::expect_equal(out_arr, array(out, dim = dim(out)))
Simple wrapper for subsetting an image with indices, dropping empty dimensions.
replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'nifti' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'character' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'factor' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'list' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'array' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'anlz' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'ANY' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) replace_empty_dim(img, ...)
replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'nifti' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'character' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'factor' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'list' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'array' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'anlz' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) ## S4 method for signature 'ANY' replaceEmptyImageDimensions( img, inds, target_dim, value = 0, reorient = FALSE, ... ) replace_empty_dim(img, ...)
img |
image, nifti object, or array |
inds |
indices of subset from |
target_dim |
Original dimension from which the data was subset, the final dimension of the output |
value |
value to replace in the image where outside the indices |
reorient |
Should image be reoriented if a filename? |
... |
not used |
Object of class nifti
or array
if nifti
is not supplied
replace_empty_dim
is a shorthand for
replaceEmptyImageDimensions
with all the same arguments.
getEmptyImageDimensions
,
dropEmptyImageDimensions
dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg, timg)) inds = getEmptyImageDimensions(nim) inds_arr = getEmptyImageDimensions(arr) testthat::expect_equal(inds, inds_arr) out = applyEmptyImageDimensions(nim, inds = inds) result = replaceEmptyImageDimensions(out, inds = inds, target_dim = dim(nim)) testthat::expect_equal(array(result, dim = dim(result)), array(nim, dim = dim(nim))) replace_empty_dim(out, inds = inds, target_dim = dim(nim)) target_dim = dim(nim) arr = array(out, dim = dim(out)) nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = function(...) replaceEmptyImageDimensions(..., target_dim = target_dim, inds = inds) func(arr) func(nim) func(rnifti) func(timg) func(limg)
dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) arr[,,10] = 0 nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg, timg)) inds = getEmptyImageDimensions(nim) inds_arr = getEmptyImageDimensions(arr) testthat::expect_equal(inds, inds_arr) out = applyEmptyImageDimensions(nim, inds = inds) result = replaceEmptyImageDimensions(out, inds = inds, target_dim = dim(nim)) testthat::expect_equal(array(result, dim = dim(result)), array(nim, dim = dim(nim))) replace_empty_dim(out, inds = inds, target_dim = dim(nim)) target_dim = dim(nim) arr = array(out, dim = dim(out)) nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = function(...) replaceEmptyImageDimensions(..., target_dim = target_dim, inds = inds) func(arr) func(nim) func(rnifti) func(timg) func(limg)
Rescales an image to be in certain value range. This was created as sometimes DICOM scale and slope parameters may be inconsistent across sites and the data need to be value restricted
rescale_img( filename, pngname = NULL, write.nii = FALSE, outfile = NULL, min.val = -1024, max.val = 3071, ROIformat = FALSE, drop_dim = TRUE, writer = "dcm2nii", ... )
rescale_img( filename, pngname = NULL, write.nii = FALSE, outfile = NULL, min.val = -1024, max.val = 3071, ROIformat = FALSE, drop_dim = TRUE, writer = "dcm2nii", ... )
filename |
filename of image to be read into R or nifti object |
pngname |
filename of png of histogram of values of image to be made. For no png - set to NULL (default) |
write.nii |
logical - should the image be written. |
outfile |
if |
min.val |
minimum value of image (default -1024 (for CT)). If no thresholding set to -Inf |
max.val |
maximum value of image (default 3071 (for CT)). If no thresholding set to Inf |
ROIformat |
if TRUE, any values $< 0$ will be set to 0 |
drop_dim |
Should |
writer |
character value to add to description slot of NIfTI header |
... |
extra methods to be passed to |
Object of class nifti
img = nifti(array(rnorm(10^3, sd = 1000), dim = rep(10, 3))) outfile = tempfile(fileext = ".nii.gz") pngname = tempfile(fileext = ".png") rescale_img(img, write.nii = TRUE, outfile = outfile, pngname = pngname)
img = nifti(array(rnorm(10^3, sd = 1000), dim = rep(10, 3))) outfile = tempfile(fileext = ".nii.gz") pngname = tempfile(fileext = ".png") rescale_img(img, write.nii = TRUE, outfile = outfile, pngname = pngname)
Reverse Reorientation an Image to RPI orientation
reverse_orient_rpi( file, convention = c("NEUROLOGICAL", "RADIOLOGICAL"), orientation, verbose = TRUE ) reverse_orient_rpi_file( file, convention = c("NEUROLOGICAL", "RADIOLOGICAL"), orientation, verbose = TRUE )
reverse_orient_rpi( file, convention = c("NEUROLOGICAL", "RADIOLOGICAL"), orientation, verbose = TRUE ) reverse_orient_rpi_file( file, convention = c("NEUROLOGICAL", "RADIOLOGICAL"), orientation, verbose = TRUE )
file |
Object of class |
convention |
Convention of original image (usually from |
orientation |
Vector of length 3 from original image
(usually from |
verbose |
print diagnostic messages |
Object of class nifti
'reverse_orient_rpi' and 'reverse_orient_rpi_file' uses 'RNifti' to ensure the reading orientation
Takes an image, finds the quantiles given by
probs
, then sets values outside these values to other
values, as determined by replace
argument passed to
to window_img
robust_window(img, non_zero = FALSE, probs = c(0, 0.999), ..., mask = NULL)
robust_window(img, non_zero = FALSE, probs = c(0, 0.999), ..., mask = NULL)
img |
object of class nifti |
non_zero |
Should zeroes be excluded from the calculation of quantiles? |
probs |
quantiles to constrain the image these define the window sent to |
... |
additional arguments sent to |
mask |
binary image to use to to calculate quantiles |
Object of class nifti with values outside quantiles replaced
by values depending on replace argument passed to window_img
Wrapper to check if multiple objects all have the same dimensions
same_dims(...)
same_dims(...)
... |
Arguments (matrices or arrays) where the dimension will be checked against the first object's dimension |
Logical indicating if all have the same dimensions or not
mat1 = matrix(1:9, ncol = 3) mat2 = matrix(rnorm(9), ncol = 3) mat3 = matrix(rnorm(16), ncol = 4) mat4 = matrix(rnorm(9), ncol = 3) same_dims(mat1, mat2) same_dims(mat1, mat3) same_dims(mat1, mat2, mat4)
mat1 = matrix(1:9, ncol = 3) mat2 = matrix(rnorm(9), ncol = 3) mat3 = matrix(rnorm(16), ncol = 4) mat4 = matrix(rnorm(9), ncol = 3) same_dims(mat1, mat2) same_dims(mat1, mat3) same_dims(mat1, mat2, mat4)
Takes in an image, gets the unique values, then creates a list of binary images for each one of those values.
separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'nifti' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'array' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'ANY' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'factor' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'character' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'list' separate_img(img, levels = NULL, drop_zero = TRUE)
separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'nifti' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'array' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'ANY' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'factor' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'character' separate_img(img, levels = NULL, drop_zero = TRUE) ## S4 method for signature 'list' separate_img(img, levels = NULL, drop_zero = TRUE)
img |
character path of image or
an object of class |
levels |
if |
drop_zero |
Should zeroes be dropped from the labels? Zero usually denotes background or non-interesting voxels |
A nifti
object (or list of them) or class of
object passed in if not specified
Exact equalling is using ==
set.seed(5) dims = rep(10, 3) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) simg = separate_img(nim) simg_arr = separate_img(arr) slist = lapply(simg, function(x) array(x, dim(x))) testthat::expect_equal(slist, simg_arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = separate_img func(arr) func(nim) func(rnifti) func(timg) func(limg)
set.seed(5) dims = rep(10, 3) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) simg = separate_img(nim) simg_arr = separate_img(arr) slist = lapply(simg, function(x) array(x, dim(x))) testthat::expect_equal(slist, simg_arr) rnifti = RNifti::asNifti(nim) timg = tempimg(nim) limg = list(factor(timg), factor(timg)) func = separate_img func(arr) func(nim) func(rnifti) func(timg) func(limg)
Slices a image color data.frame
along the 3 planes (axial,
coronal, sagittal) and returns it in a ggplot-ready format for faceting.
slice_colour_df(img_df, xyz = NULL)
slice_colour_df(img_df, xyz = NULL)
img_df |
an image |
xyz |
coordinates to slice the |
A data.frame
with x and y coordinates, colour, and intensity
values, along with the associated planes that were sliced.
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) df = img_colour_df(img) sliced = slice_colour_df(df, c(5, 5, 4))
img = nifti(array(rnorm(10^3), dim = rep(10, 3))) df = img_colour_df(img) sliced = slice_colour_df(df, c(5, 5, 4))
Subset DTI data based on b-values #'
subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... ) ## S4 method for signature 'nifti' subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... ) ## S4 method for signature 'ANY' subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... ) ## S4 method for signature 'character' subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... ) ## S4 method for signature 'list' subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... )
subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... ) ## S4 method for signature 'nifti' subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... ) ## S4 method for signature 'ANY' subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... ) ## S4 method for signature 'character' subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... ) ## S4 method for signature 'list' subset_dti( img, bvals, bvecs, b_step = 1, maximum = Inf, shells = NULL, verbose = TRUE, ... )
img |
character or |
bvals |
filename of b-values (assuming 1 row) |
bvecs |
filename of b-vectors (assuming 3 rows) |
b_step |
step of b-values to round to |
maximum |
maximum b-value threshold |
shells |
Shells to keep (after rounding) |
verbose |
print diagnostic messages |
... |
options passed to |
List of filenames of image, b-values, and b-vectors that were subsetted.
John Muschelli [email protected]
## Not run: img = "~/Downloads/data.nii.gz" bvals = "~/Downloads/bvals" bvecs = "~/Downloads/bvals" verbose = TRUE b_step = 50 maximum = 1500 shells = NULL sub = subset_dti(img = img, bvals = bvals, bvecs = bvecs, maximum = 1500, b_step = 50) ## End(Not run)
## Not run: img = "~/Downloads/data.nii.gz" bvals = "~/Downloads/bvals" bvecs = "~/Downloads/bvals" verbose = TRUE b_step = 50 maximum = 1500 shells = NULL sub = subset_dti(img = img, bvals = bvals, bvecs = bvecs, maximum = 1500, b_step = 50) ## End(Not run)
Takes in a object of class nifti, writes it
to a temp file, appends
.nii.gz as writenii
adds it.
tempimg( nim, gzipped = TRUE, checknan = TRUE, check_type = FALSE, warn = FALSE, drop_dim = TRUE, dtype = TRUE, ... )
tempimg( nim, gzipped = TRUE, checknan = TRUE, check_type = FALSE, warn = FALSE, drop_dim = TRUE, dtype = TRUE, ... )
nim |
object of class nifti |
gzipped |
Should file be gzipped? Passed to
|
checknan |
Check for NAs or NaNs |
check_type |
Check the datatype for an image. Will run |
warn |
Should warnings be displayed if |
drop_dim |
Should |
dtype |
Should |
... |
Passed to |
filename of output nii.gz
Windows an image to min and max values and also changes cal_max and cal_min parameters
window_img( x, window = c(0, 100), replace = c("window", "missing", "zero"), ... )
window_img( x, window = c(0, 100), replace = c("window", "missing", "zero"), ... )
x |
is either a character name for the image or an object of class nifti |
window |
numeric of length 2 that gives min and max for window |
replace |
either "window" if the any values outside of c(min, max) are set to the min or max or "missing" for these to be set to NA |
... |
not used |
Object of class nifti
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) window_img(nim, window = c(1, 5)) window_img(nim, window = c(1, 5), replace = "missing") tfile = tempimg(nim) window_img(tfile) window_img(as.factor(tfile)) arr = window_img(img_data(nim)) rnim = RNifti::readNifti(tfile) window_img(rnim, window = c(1, 5)) range(window_img(rnim, window = c(1, 5))) window_img(rnim, window = c(1, 5), replace = "missing") range(window_img(rnim, window = c(1, 5), replace = "missing"))
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) window_img(nim, window = c(1, 5)) window_img(nim, window = c(1, 5), replace = "missing") tfile = tempimg(nim) window_img(tfile) window_img(as.factor(tfile)) arr = window_img(img_data(nim)) rnim = RNifti::readNifti(tfile) window_img(rnim, window = c(1, 5)) range(window_img(rnim, window = c(1, 5))) window_img(rnim, window = c(1, 5), replace = "missing") range(window_img(rnim, window = c(1, 5), replace = "missing"))
Writes out NIfTI files for multiple formats. Currently,
for nifti
objects and niftiImage
objects from RNifti
write_nifti(nim, filename, ...)
write_nifti(nim, filename, ...)
nim |
Container for NIfTI Image |
filename |
Filename of image to be written out |
... |
additional arguments, to be passed to |
Output from NIfTI writer
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) tfile = tempfile(fileext = ".nii.gz") write_nifti(nim, tfile) rimg = RNifti::readNifti(tfile) write_nifti(rimg, tfile)
set.seed(5) dims = rep(10, 4) arr = array(rpois(prod(dims), lambda = 2), dim = dims) nim = oro.nifti::nifti(arr) tfile = tempfile(fileext = ".nii.gz") write_nifti(nim, tfile) rimg = RNifti::readNifti(tfile) write_nifti(rimg, tfile)
This function calls the writeNIfTI
function from the
oro.nifti
package, but makes sure to remove .nii
extension and
warnings can be suppressed.
writeNIfTI2(nim, filename, dtype = FALSE, compression = 9, ...) writenii( nim, filename, dtype = TRUE, drop_dim = TRUE, warn = FALSE, compression = 9, rm_extensions = TRUE, ... )
writeNIfTI2(nim, filename, dtype = FALSE, compression = 9, ...) writenii( nim, filename, dtype = TRUE, drop_dim = TRUE, warn = FALSE, compression = 9, rm_extensions = TRUE, ... )
nim |
object of class |
filename |
path to save the NIfTI file. Suffix will be removed |
dtype |
Should |
compression |
compression level for gzipped files. |
... |
Additional arguments passed to |
drop_dim |
Should |
warn |
Should warnings from |
rm_extensions |
should niftiExtensions be converted to simple nifti objects before writing? |
Nothing
While writeNIfTI2
does not run datatyper
as default,
writenii
does. Additional functionality will be added to writenii
likely
but will not to writeNIfTI2
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) tfile = tempfile(fileext = ".nii.gz") timg = writenii(nim, tfile, rm_extensions = TRUE, warn = TRUE) timg = writeNIfTI2(nim, tfile, dtype = TRUE)
set.seed(5) dims = rep(10, 3) arr = array(rnorm(prod(dims)), dim = dims) nim = oro.nifti::nifti(arr) rnifti = RNifti::asNifti(nim) tfile = tempfile(fileext = ".nii.gz") timg = writenii(nim, tfile, rm_extensions = TRUE, warn = TRUE) timg = writeNIfTI2(nim, tfile, dtype = TRUE)
Find Center of Gravity of Image, after thresholding and
take ceiling (wrapper for cog
)
xyz(...)
xyz(...)
... |
Arguments passed to |
Vector of length 3
Just a convenience wrapper for cog(ceil=TRUE)
This function zero pads an image by a certain number of dimensions, usually for convolution
zero_pad(img, kdim, invert = FALSE, pad_value = 0L, ...)
zero_pad(img, kdim, invert = FALSE, pad_value = 0L, ...)
img |
Array or class nifti |
kdim |
Dimensions of kernel |
invert |
(logical) If |
pad_value |
Value to pad the image with. May use other values, such as -1024 for CT data |
... |
Options to |
Object of class nifti
kdim = c(3,3,5) img = array(rnorm(30*30*36), dim = c(30, 30, 36)) pad = zero_pad(img, kdim) back = zero_pad(pad, kdim, invert=TRUE) all.equal(back, img)
kdim = c(3,3,5) img = array(rnorm(30*30*36), dim = c(30, 30, 36)) pad = zero_pad(img, kdim) back = zero_pad(pad, kdim, invert=TRUE) all.equal(back, img)
Helper function for plotting - returns zlim for image
plot function
zlimmer(x, zlim = NULL, computed_range = NULL)
zlimmer(x, zlim = NULL, computed_range = NULL)
x |
Object of class |
zlim |
A user-specified |
computed_range |
If the range of the data was already computed, this can be passed in and will be used if relevant. |
If zlim = NULL
, then vector of length 2, otherwise returns zlim
Standardizes an image either by the axial, sagittal, or coronal slice or whole image
zscore_img( img, mask = NULL, margin = NULL, centrality = c("mean", "median", "trimmed_mean"), variability = c("sd", "iqrdiff", "mad", "maddiff", "iqr", "trimmed_sd"), trim = 0.2, remove.na = TRUE, remove.nan = TRUE, remove.inf = TRUE, remove.val = 0, remask = TRUE )
zscore_img( img, mask = NULL, margin = NULL, centrality = c("mean", "median", "trimmed_mean"), variability = c("sd", "iqrdiff", "mad", "maddiff", "iqr", "trimmed_sd"), trim = 0.2, remove.na = TRUE, remove.nan = TRUE, remove.inf = TRUE, remove.val = 0, remask = TRUE )
img |
character path of image or an object of class nifti |
mask |
character path of mask or an object of class nifti |
margin |
Margin of image to z-score over ( |
centrality |
(character) Measure to center the data, either mean or median |
variability |
(character) Measure to scale the data |
trim |
if centrality is |
remove.na |
(logical) change NAs to remove.val |
remove.nan |
(logical) change NaN to remove.val |
remove.inf |
(logical) change Inf to remove.val |
remove.val |
(logical) value to put the NA/NaN/Inf |
remask |
(logical) Should the image be remasked after normalizing? |
Array of object of class nifti
dim = c(100, 30, 5) img = array(rnorm(prod(dim), mean=4, sd=4), dim=dim) truth2 = img for (i in 1:dim(img)[2]) { truth2[,i,] = (truth2[,i,]- mean(truth2[,i,]))/sd(truth2[,i,]) } truth1 = img for (i in 1:dim(img)[1]) { truth1[i,,] = (truth1[i,,]- mean(truth1[i,,]))/sd(truth1[i,,]) } truth3 = img for (i in 1:dim(img)[3]) { truth3[,,i] = (truth3[,,i]- mean(truth3[,,i]))/sd(truth3[,,i]) } try3 = zscore_img(img, margin=3) stopifnot(all.equal(try3, truth3)) try2 = zscore_img(img, margin=2) stopifnot(all.equal(try2, truth2)) try1 = zscore_img(img, margin=1) stopifnot(all.equal(try1, truth1)) z = zscore_img(img, margin=NULL) ztrim = zscore_img(img, margin=NULL, centrality = "trimmed_mean", variability = "trimmed_sd") z = zscore_img(img, centrality = "median", variability = "iqr") z = zscore_img(img, centrality = "median", variability = "iqrdiff") z = zscore_img(img, centrality = "median", variability = "maddiff")
dim = c(100, 30, 5) img = array(rnorm(prod(dim), mean=4, sd=4), dim=dim) truth2 = img for (i in 1:dim(img)[2]) { truth2[,i,] = (truth2[,i,]- mean(truth2[,i,]))/sd(truth2[,i,]) } truth1 = img for (i in 1:dim(img)[1]) { truth1[i,,] = (truth1[i,,]- mean(truth1[i,,]))/sd(truth1[i,,]) } truth3 = img for (i in 1:dim(img)[3]) { truth3[,,i] = (truth3[,,i]- mean(truth3[,,i]))/sd(truth3[,,i]) } try3 = zscore_img(img, margin=3) stopifnot(all.equal(try3, truth3)) try2 = zscore_img(img, margin=2) stopifnot(all.equal(try2, truth2)) try1 = zscore_img(img, margin=1) stopifnot(all.equal(try1, truth1)) z = zscore_img(img, margin=NULL) ztrim = zscore_img(img, margin=NULL, centrality = "trimmed_mean", variability = "trimmed_sd") z = zscore_img(img, centrality = "median", variability = "iqr") z = zscore_img(img, centrality = "median", variability = "iqrdiff") z = zscore_img(img, centrality = "median", variability = "maddiff")