knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

tidyneuro

R build status Codecov test coverage

There are a lot of packages in the R ecosystem focused on several aspects of Neuroimage processing, many of them listed in the CRAN Task View: Medical Imaging section: oro.nifti, oro.dicom, RNifti, fmri, divest (among others) for reading/writing of images; ANTsR, fslr for more advanced processing methods; RNiftyReg specialized in registration, mritc focused in image segmentation. Many more, for DTI, fMRI, molecular imaging, and visualization are available in CRAN.

However, to get a complete Neuroimaging pipeline working, it is common to rely on several different packages, and, in general, no single package can offer all the functionalities desired. Tracking the requirements and dependencies of a pipeline is time-consuming, since the user has to manually find, for each function used, the packages it depends on.

In addition, the most typical form of building a pipeline in R is via scripting. Once the needed functions are defined or imported from a third-party package, one or more scripts are written in order to get all processes done.

But scripts are error-prone, difficult to trace and debug if the script is large, and require some level of customization when one tries to run the script in a different environment (computer, laboratory...). These cons make us think of defining self-contained workflows, easily shareable, which can perform the same operations as a script but with greater legibility and traceability.

The goal of this package is to provide certain features to manage the situations before:

Installation

You can install the development version from the GitHub repo with:

# install.packages("remotes")
remotes::install_github("neuroimaginador/tidyneuro")

The tidyneuro approach

suppressPackageStartupMessages(library(tidyneuro))
##%######################################################%##
#                                                          #
####                Auxiliary functions                 ####
#                                                          #
##%######################################################%##

# - register to a template (MNI152 in case of structural imaging)
# Returns the transformation, as declared in the ANTsRCore package
register_to_template <- function(image, template) {
  #...
}

# Returns the image reoriented in the template space
get_img_in_template <- function(tx) {
  #...
}

# Returns the affine transformation
get_tx_to_template <- function(tx) {
  #...
}

# Returns the scale parameter as the determinant of the affine transformation
get_scale_parameter <- function(M) {
  #...
}

template <- file.path(".", "resources",
                      "volumetry", "MNI152_T1_1mm.nii.gz")

# - bias field correction, just a simple wrapper around
# n4BiasFieldCorrection
bfc <- function(img) {
  #...
}

# - brain extraction
# Uses MALF technique with a "brain extraction" dataset
# Returns the brain mask
make_brain_extraction <- function(image) {
  #...
}

# - segmentation (GM, WM an CSF)
# Uses FSL's FAST tool to find a segmentation into GM, WM and CSF
# Returns the segmentation as a labelled image with labels 0 (background),
# 1 (CSF), 2 (GM) and 3 (WM)
get_segmentation <- function(img, mask = NULL) {
  #...
}

count_by_ROI <- function(img) {
  #...
}


##%######################################################%##
#                                                          #
####                   Volumetry flow                   ####
#                                                          #
##%######################################################%##

volumetry_flow <- workflow(name = "volumetry",
                             work_dir = "~/flow/volumetry",
                             inputs = "T1")

volumetry_flow  %>% 
  step(register_to_template,
      inputs = "T1",
      output = c("T1 (MNI)", "Affine transform (Native -> MNI)"),
      template = template) %>% 
  step(get_scale_parameter,
      inputs = "Affine transform (Native -> MNI)",
      output = "Scale parameter")

volumetry_flow %>% 
  step(bfc,
       inputs = "T1 (MNI)",
       output = "Bias field corrected T1")

volumetry_flow %>% 
  step(make_brain_extraction,
       inputs = "Bias field corrected T1",
       output = "Brain mask")

volumetry_flow %>% 
  step(function(A, B) {A * B},
       inputs = c("Bias field corrected T1", "Brain mask"),
       output = "Skull-stripped T1")

volumetry_flow %>% 
  step(get_segmentation,
       inputs = "Skull-stripped T1",
       output = "Segmented T1")

volumetry_flow %>% 
  step(count_by_ROI,
       inputs = "Brain mask",
       output = "Brain volume (MNI)")

volumetry_flow %>% 
  step(function(A, B) {A * B},
       inputs = c("Brain volume (MNI)", "Scale parameter"),
       output = "Brain volume")

volumetry_flow %>% 
  step(count_by_ROI,
       inputs = "Segmented T1",
       output = "Tissue volume (MNI)")

volumetry_flow %>% 
  step(function(A, B) {A * B},
       inputs = c("Tissue volume (MNI)", "Scale parameter"),
       output = "Tissue volume")

In tidyneuro, a workflow is an ordered collection of processes that convert inputs into outputs, such that the output of a process may be the input to another one.

By defining the appropriate functions, one can model the pipeline in the correct order and obtain a flow as the one depicted in the following figure.

f <- function(a, b) {a + b}

my_flow <- workflow(name = "test", 
                    inputs = c("Input1", "Input2"))

my_flow %>% 
  step(f, inputs = c("Input1", "Input2"), output = "Output1") %>% 
  inputs("Input3") %>% 
  step(f, inputs = c("Input2", "Input3"), output = "Output2") %>% 
  step(f, inputs = c("Input1", "Input3"), output = "Output3") %>% 
  step(f, inputs = c("Output3", "Input3"), output = "Output4")

my_flow %>% plot()

In this case, we have 3 inputs and several outputs. The arrows indicate which inputs are used to compute each of the other outputs.

Example

This is a basic example which shows you how to solve a common problem, the computation of tissue (gray matter, white matter and cephalospinal fluid) volume:

library(tidyneuro)
## basic example code
volumetry_flow %>% plot()

Code of Conduct

Please note that the tidyneuro project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.



neuroimaginador/tidyneuro documentation built on Jan. 1, 2021, 11:44 a.m.