Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dfsp-spirit committed Jan 23, 2023
2 parents f6ab0aa + d96e968 commit fe1ddfc
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 25 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ jobs:
if: runner.os != 'Windows'
run: export RGL_USE_NULL=true

- name: Install dependencies

- name: Install curl on Windows
if: runner.os == 'Windows'
run: install.packages("curl")
shell: Rscript {0}

name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
Expand Down
56 changes: 52 additions & 4 deletions R/cbar.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# colorbar functions

#' @title Draw coloredbar into background of current plot.
#' @title Draw colorebar into background of current plot.
#'
#' @description Requires a rgl 3d visualisation to be open that already contains a rendered object. Uses \code{\link{bgplot3d}} to add a colorbar in the background of the plot using \code{\link[fields]{image.plot}}. Experimental.
#'
Expand All @@ -18,6 +18,7 @@
#' @importFrom fields image.plot
#' @keywords internal
draw.colorbar <- function(coloredmeshes, horizontal=FALSE, ...) {

if(! is.list(coloredmeshes)) {
stop("Parameter 'coloredmeshes' must be a list.");
}
Expand Down Expand Up @@ -55,7 +56,20 @@ draw.colorbar <- function(coloredmeshes, horizontal=FALSE, ...) {
}
num_col = ifelse(is.null(makecmap_options$n), 100L, makecmap_options$n);

rgl::bgplot3d({op = graphics::par(mar = rep(0.1, 4)); plot.new(); fields::image.plot(add=T, legend.only = TRUE, zlim = zlim, col = makecmap_options$colFn(num_col), horizontal = horizontal, ...); graphics::par(op);});
# Check whether to plot in log10 scale.
if('base' %in% names(makecmap_options)) {
num_ticks_default = 5L;
if (as.integer(makecmap_options$base) == 10L) {
ticks = squash::prettyLog(combined_data_range, n=num_ticks_default);
} else {
stop(sprintf(" - Invalid 'base' value in 'makecmap_options', only 10 is supported for log 10 scale. (Do not set it at all for linear scale.)\n"));
}
axis.args = list('at'=log(ticks), 'labels'=ticks);
rgl::bgplot3d({op = graphics::par(mar = rep(0.1, 4)); plot.new(); fields::image.plot(add=T, legend.only = TRUE, zlim = log(zlim), axis.args=axis.args, col = makecmap_options$colFn(num_col), horizontal = horizontal, ...); graphics::par(op);});
} else {
# Plot linear scale.
rgl::bgplot3d({op = graphics::par(mar = rep(0.1, 4)); plot.new(); fields::image.plot(add=T, legend.only = TRUE, zlim = zlim, col = makecmap_options$colFn(num_col), horizontal = horizontal, ...); graphics::par(op);});
}
} else {
warning("Requested to draw colorbar, but meshes do not contain the required metadata. Skipping.");
}
Expand All @@ -78,6 +92,8 @@ draw.colorbar <- function(coloredmeshes, horizontal=FALSE, ...) {
#'
#' @param trim_png logical, whether to trim the output PNG image using image magick, i.e., remove everything but the foreground. Ignored unless an output PNG image is actually written (see 'png_options') and the 'magick' package is installed.
#'
#' @param log_break: logical, scalar int, or vector of ints. Whether to use log10 scale for plotting the cbar. If logical and TRUE, uses log scale with default number (=5) ticks auto-computed from the data. If a single integer N, uses N ticks auto-computed from the data instead. If a numeric vector, uses the supplied values in the vector as ticks, note that they must be on a `log(data)` scale. If the 'makecmap_options' stored in the passed 'coloredmeshes' contain a 'base' value of 10, log 10 is assumed (with the default 5 ticks), even if this parameter is left at its default value, logical FALSE.
#'
#' @note If you increase the output resolution of the colorbar (using 'png_options'), you will have to increase the font sizes as well (using 'image.plot_extra_options'), otherwise the axis and legend labels will be hard to read.
#'
#' @examples
Expand All @@ -104,15 +120,15 @@ draw.colorbar <- function(coloredmeshes, horizontal=FALSE, ...) {
#' @importFrom grDevices png pdf dev.off
#' @importFrom utils modifyList
#' @export
coloredmesh.plot.colorbar.separate <- function(coloredmeshes, show=FALSE, image.plot_extra_options = list(horizontal=FALSE, 'legend.cex'=1.8, 'legend.width'=2, 'legend.mar' = 12, 'axis.args'=list('cex.axis'=5.0)), png_options=list('filename'='fsbrain_cbar.png', 'width'=1400, 'height'=1400, 'bg'='#FFFFFF00'), silent=FALSE, trim_png=TRUE) {
coloredmesh.plot.colorbar.separate <- function(coloredmeshes, show=FALSE, image.plot_extra_options = list(horizontal=FALSE, 'legend.cex'=1.8, 'legend.width'=2, 'legend.mar' = 12, 'axis.args'=list('cex.axis'=5.0)), png_options=list('filename'='fsbrain_cbar.png', 'width'=1400, 'height'=1400, 'bg'='#FFFFFF00'), silent=FALSE, trim_png=TRUE, log_breaks=FALSE) {

if(length(coloredmeshes) < 1) {
message("Requested to draw separate colorbar, but mesh list is empty. Skipping.");
return(invisible(NULL));
}


makecmap_options = coloredmeshes.get.md(coloredmeshes, 'makecmap_options');
print(makecmap_options);
if(is.null(makecmap_options)) {
warning("Requested to draw colorbar, but meshes contain no 'makecmap_options' metadata, falling back to defaults.");
makecmap_options = mkco.seq();
Expand Down Expand Up @@ -145,6 +161,38 @@ coloredmesh.plot.colorbar.separate <- function(coloredmeshes, show=FALSE, image.

image.plot_options_internal = list(legend.only=TRUE, zlim = zlim, col = makecmap_options$colFn(num_col), add=TRUE, graphics.reset=TRUE);
image.plot_options = modifyList(image.plot_options_internal, image.plot_extra_options);

# Enable plotting log-scale color bar.
# NOTE: Use 'area' instead of 'thickness' as PVD when testing this with the code from the example, thickness has a min of 0 (won't work with log).
if((is.logical(log_breaks) && log_breaks) || is.numeric(log_breaks) || 'base' %in% names(makecmap_options)) {
num_ticks_default = 5L;
if(is.logical(log_breaks) && log_breaks) {
log_breaks = num_ticks_default; # If simply 'True', use default number of breaks = 5.
}

if(is.numeric(log_breaks) && length(log_breaks) == 1L) { # If a single number, treat it as the *number* of ticks to create.
num_break_labels = as.integer(log_breaks);
ticks = squash::prettyLog(combined_data_range, n=num_break_labels);
} else if(is.numeric(log_breaks) && length(log_breaks) > 1L) { # If a vector, treat the values in there as the desired ticks.
ticks = log_breaks;
} else {
if (as.integer(makecmap_options$base) == 10L) {
ticks = squash::prettyLog(combined_data_range, n=num_ticks_default);
} else {
stop(sprintf(" - Invalid 'base' value in 'makecmap_options', only 10 is supported for log 10 scale. (Do not set it at all for linear scale.)"));
}

}

tick_axis_args = list('at'=log(ticks), 'labels'=ticks)
if ("axis.args" %in% names(image.plot_options)) {
image.plot_options$axis.args = modifyList(image.plot_options$axis.args, tick_axis_args);
} else {
image.plot_options$axis.args = tick_axis_args;
}
image.plot_options$zlim = log(image.plot_options$zlim);
}

if(show) {
plot.new();
do.call(fields::image.plot, image.plot_options);
Expand Down
12 changes: 9 additions & 3 deletions R/optdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ download_fsaverage <- function(accept_freesurfer_license=FALSE) {
c(base_path_fsaverage, 'surf', 'lh.curv'),
c(base_path_fsaverage, 'surf', 'rh.curv'),
c(base_path_fsaverage, 'ext', 'FreeSurferColorLUT.txt'),
c(base_path_fsaverage, 'LICENSE')
c(base_path_fsaverage, 'LICENSE'),
c(base_path_fsaverage, 'surf', 'lh.sphere'),
c(base_path_fsaverage, 'surf', 'rh.sphere')
);


Expand All @@ -265,7 +267,9 @@ download_fsaverage <- function(accept_freesurfer_license=FALSE) {
'3e81598a5ac0546443ec37d0ac477c80',
'76ad91d2488de081392313ad5a87fafb',
'a3735566ef949bd4d7ed303837cc5e77', # color LUT
'b39610adfe02fdce2ad9d30797c567b3' # LICENSE
'b39610adfe02fdce2ad9d30797c567b3', # LICENSE
'9bcc318b66c4da5e479b33e9451ec5e1',
'9bcc318b66c4da5e479b33e9451ec5e1' # rh.sphere
);


Expand All @@ -287,7 +291,9 @@ download_fsaverage <- function(accept_freesurfer_license=FALSE) {
'surf/lh.curv',
'surf/rh.curv',
'ext/FreeSurferColorLUT.txt',
'LICENSE'
'LICENSE',
'surf/lh.sphere',
'surf/rh.sphere'
);
ext_urls = paste(ext_url_subject_part_fsaverage, ext_url_parts_each_subject, sep='');
base_url = 'http://rcmd.org/projects/nitestdata/';
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ The documentation can be accessed from within an R session after you have loaded
* [basic fsbrain example notebook](https://htmlpreview.github.io/?https://github.com/dfsp-spirit/fsbrain/blob/develop/web/Rmd_web_examples/examples.html): Live visualization of subject data
* [advanced fsbrain example notebook](https://htmlpreview.github.io/?https://github.com/dfsp-spirit/fsbrain/blob/develop/web/Rmd_web_examples/examples_adv.html): Plotting group data
* [export API fsbrain example notebook](https://htmlpreview.github.io/?https://github.com/dfsp-spirit/fsbrain/blob/develop/web/Rmd_web_examples/examples_export.html): Exporting publication-ready plots
* A detailed vignette with explanations and examples for the functions of the package is included, run `browseVignettes("fsbrain")` to see the vignettes. You can also open the vignette directly:
* learn how to load and visualize surface-based neuroimaging data and results: `vignette("fsbrain")` [read online at CRAN](https://cran.r-project.org/web/packages/fsbrain/vignettes/fsbrain.html)
* learn how to load and visualize volume-based neuroimaging data and results: `vignette("fsbrain_vol")` [read online at CRAN](https://cran.r-project.org/web/packages/fsbrain/vignettes/fsbrain_vol.html)
* read the fsbrain FAQ: `vignette("fsbrain_faq")` [read online at CRAN](https://cran.r-project.org/web/packages/fsbrain/vignettes/fsbrain_faq.html)


* Detailed vignettes with explanations and examples for the functions of the package is included, run `browseVignettes("fsbrain")` to see the vignettes. You can also open the vignette directly:
* How to load and visualize surface-based neuroimaging data: `vignette("fsbrain")` or: [read online at CRAN](https://cran.r-project.org/web/packages/fsbrain/vignettes/fsbrain.html)
* How to load and visualize volume-based neuroimaging data: `vignette("fsbrain_vol")` or: [read online at CRAN](https://cran.r-project.org/web/packages/fsbrain/vignettes/fsbrain_vol.html)
* The fsbrain FAQ: `vignette("fsbrain_faq")` or: [read online at CRAN](https://cran.r-project.org/web/packages/fsbrain/vignettes/fsbrain_faq.html)


* Help for a specific function can be accessed in the usual R manner: `?<function>`, where you replace `<function>` with a function name. Like this: `?group.morph.native`.
* Run `example(<function>)` to see a live demo that uses the function `<function>`. Like this: `example(group.morph.native)`.
* The [unit tests](./tests/testthat/) that come with this package are essentially a list of examples that illustrate how to use the functions.
Expand Down
2 changes: 1 addition & 1 deletion docker/README_DEV_DOCKER.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To create a new version of the image on docker.io, one needs to build the new im
1) login to docker.io:

```
sudo docker login -u "dfspspirit" --password-stdin docker.io
docker login -u "dfspspirit" docker.io
```

2) tag the image with the dockerhub user/repo and a version, e.g.:
Expand Down
67 changes: 67 additions & 0 deletions docker/fsbrain0.5.3/R4.1.0/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Docker build for distributing an fsbrain container.
# This file is part of fsbrain, https://github.com/dfsp-spirit/fsbrain
#
# See the very bottom of this script for info on how to run it.

FROM rocker/r-ver:4.1.0

# Shell setup:
WORKDIR /root

##### Install tools and system dependencies #####
RUN apt-get update && apt-get install -y apt-utils
RUN apt-get update && apt-get install -y \
build-essential \
git \
libcurl4-openssl-dev \
libfreetype6-dev \
libgfortran4 \
libgl1-mesa-dev \
libglu1-mesa-dev \
libmagick++-dev \
libssh-dev \
libx11-dev \
libxml2-dev \
mesa-common-dev \
xvfb \
libtk8.6 \
libfontconfig1-dev \
libharfbuzz-dev \
libfribidi-dev \
&& rm -rf /var/lib/apt/lists/*

##### Install required R packages #####

## Get fsbrain dependencies.
RUN R -e "install.packages('devtools', repos = 'http://cran.rstudio.com/')"

## We want freesurferformats with all optional dependencies for full file format support.
RUN R -e "devtools::install_version('freesurferformats', version = '0.1.17', dependencies=TRUE, repos = 'http://cran.rstudio.com')"

RUN R -e "install.packages('rgl', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('viridis', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('RColorBrewer', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('misc3d', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('igraph', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('magick', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('Rvcg', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('pracma', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('reshape', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('squash', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('data.table', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('methods', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('fields', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('pkgfilecache', repos = 'http://cran.rstudio.com/')"

## Now get fsbrain
RUN R -e "devtools::install_version('fsbrain', version = '0.5.3', dependencies=TRUE, repos = 'http://cran.rstudio.com')"

## Create folders for mapping. You should write output to /home/output in the R session.
RUN mkdir /home/input
RUN mkdir /home/output

## Run R in xvfb with 2k resolution.
CMD cd /home/input \
&& xvfb-run --server-args="-screen 0, 2560x1440x24" R

## Now run your analyses and write the results/images to /home/output.
62 changes: 62 additions & 0 deletions docker/fsbrain0.5.3dev/R4.1.0/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Docker build for distributing an fsbrain container.
# This file is part of fsbrain, https://github.com/dfsp-spirit/fsbrain
#
# See the very bottom of this script for info on how to run it.

FROM rocker/r-ver:4.1.0

# Shell setup:
WORKDIR /root

##### Install tools and system dependencies #####
RUN apt-get update && apt-get install -y \
build-essential \
git \
libcurl4-openssl-dev \
libfreetype6-dev \
libgfortran4 \
libgl1-mesa-dev \
libglu1-mesa-dev \
libmagick++-dev \
libssh-dev \
libx11-dev \
libxml2-dev \
mesa-common-dev \
xvfb \
&& rm -rf /var/lib/apt/lists/*

##### Install required R packages #####

## Get fsbrain dependencies.
RUN R -e "install.packages('devtools', repos = 'http://cran.rstudio.com/')"

## We want freesurferformats with all optional dependencies for full file format support.
RUN R -e "devtools::install_version('freesurferformats', version = '0.1.17', dependencies=TRUE, repos = 'http://cran.rstudio.com')"

RUN R -e "install.packages('rgl', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('viridis', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('RColorBrewer', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('misc3d', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('igraph', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('magick', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('Rvcg', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('pracma', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('reshape', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('squash', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('data.table', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('methods', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('fields', repos = 'http://cran.rstudio.com/')"
RUN R -e "install.packages('pkgfilecache', repos = 'http://cran.rstudio.com/')"

## Now get fsbrain
RUN R -e "devtools::install_github('dfsp-spirit/fsbrain', ref='c8213a5eb4ba59c48754736644a3035117a6478a')"

## Create folders for mapping. You should write output to /home/output in the R session.
RUN mkdir /home/input
RUN mkdir /home/output

## Run R in xvfb with 2k resolution.
CMD cd /home/input \
&& xvfb-run --server-args="-screen 0, 2560x1440x24" R

## Now run your analyses and write the results/images to /home/output.
Loading

0 comments on commit fe1ddfc

Please sign in to comment.