Skip to content

Commit

Permalink
Added plot_slices to brain class
Browse files Browse the repository at this point in the history
  • Loading branch information
dipterix committed Dec 20, 2023
1 parent 58c6633 commit 77a28c2
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: threeBrain
Type: Package
Title: 3D Brain Visualization
Version: 1.0.1.9020
Version: 1.0.1.9021
Authors@R: c(
person("Zhengjia", "Wang", email = "[email protected]", role = c("aut", "cre", "cph")),
person("John", "Magnotti", email = "[email protected]", role = c("aut", "res")),
Expand Down
107 changes: 107 additions & 0 deletions R/class_brain.R
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,113 @@ Brain2 <- R6::R6Class(
...
)

},

plot_electrodes_on_slices = function(
electrodes_to_plot = "all", volume = NULL, elec_table = NULL,
zoom = 1, electrode_color = "green", electrode_size = 2, ...,
decoration = function(i, j) {
graphics::points(0, 0, pch = 20, col = electrode_color,
cex = electrode_size)
},
save_to = NULL, one_plot = is.null(save_to), width = 12, height = 4) {
# DIPSAUS DEBUG START
# self <- raveio::rave_brain('demo/DemoSubject')
# private <- self$private

# Load electrode table
if(!is.data.frame(elec_table)) {
elec_table <- self$electrodes$raw_table
}

if(!is.data.frame(elec_table) || !nrow(elec_table)) {
stop("Electrode table not specified")
}
tkr_ras <- as.matrix(elec_table[, c("Coord_x", "Coord_y", "Coord_z")])
invalids <- rowSums(tkr_ras^2) == 0
if(all(invalids)) {
stop("All electrodes are invalid. Cannot plot slices.")
}

if(is.character(electrodes_to_plot)) {
electrodes_to_plot <- elec_table$Electrode
}
plot_idx <- elec_table$Electrode %in% electrodes_to_plot & !invalids
if(!any(plot_idx)) {
stop("All specified electrodes to plot are invalid. Please double-check input `electrodes_to_plot`")
}
plot_idx <- which(plot_idx)

# transform electrode from tkrRAS (fs coord_sys) to scanner ras (T1 scanner)
scanner_ras <- self$electrodes$apply_transform_points(
tkr_ras,
from = "tkrRAS",
to = "scannerRAS"
)
scanner_ras[invalids, ] <- 0



# load up volume and adjust brightness
adjust_brightness <- TRUE
if(is.null(volume)) {
volume <- self$volumes$T1$object$group$group_data$volume_data$absolute_path
adjust_brightness <- FALSE
}
if(!inherits(volume, "threeBrain.volume")) {
volume <- read_volume(volume)
}
if( adjust_brightness ) {
qt <- quantile(volume$data, c(0, 0.95), na.rm = TRUE)
volume$data[] <- (volume$data - qt[[1]]) / (qt[[2]] - qt[[1]]) * 255
volume$data[volume$data > 255] <- 255
}

# img_height <- 480
# png(filename = file.path(subject$imaging_path, "snapshots%03d.png"), width = 3*img_height, height = img_height, bg = "black")

if(length(save_to) == 1 && isTRUE(is.character(save_to))) {
if(endsWith(tolower(save_to), "png")) {
save_to <- sprintf("%s-%%04.png",
gsub("[%0-9d]{0,}\\.png$", replacement = "",
save_to, ignore.case = TRUE))
grDevices::png(filename = save_to, width = width * 72,
height = height * 72, bg = "black")
} else {
save_to <- sprintf("%s.pdf",
gsub("\\.pdf", "", save_to, ignore.case = TRUE))
grDevices::pdf(save_to, width = width, height = height,
useDingbats = FALSE, onefile = TRUE,
title = "RAVE Slice Plots", bg = "black")
}
on.exit({ grDevices::dev.off() })
}

if( one_plot ) {
plot_idx <- list(plot_idx)
}
progress <- dipsaus::progress2("Plotting slices",
max = length(plot_idx) + 1,
shiny_auto_close = TRUE)
for(ii in plot_idx) {
progress$inc(detail = sprintf("Generating graphs for electrode %s", dipsaus::deparse_svec(ii)))
plot_slices(
volume,
positions = scanner_ras[ii,],
main = sprintf('%s (Ch=%.0f,ScanRAS=%.1f,%.1f,%.1f)',
elec_table$Label[ii],
elec_table$Electrode[ii],
scanner_ras[ii, 1],
scanner_ras[ii, 2],
scanner_ras[ii, 3]),
pixel_width = 1,
zoom = zoom,
fun = decoration,
...
)
}
progress$inc(detail = "Closing graphic device")
# dev.off()
}

),
Expand Down
48 changes: 36 additions & 12 deletions R/plot_volume-slices.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
#' (no normalize) or a numeric vector of length two
#' @param zlim image plot value range, default is identical to \code{normalize}
#' @param main image titles
#' @param title_position title position; choices are \code{"left"} or \code{"top"}
#' @param ... additional arguments passing into \code{\link[graphics]{image}}
#' @returns Nothing
#' @export
plot_slices <- function(
volume, transform = NULL, positions = NULL, zoom = 1, pixel_width = 0.5,
col = c("black", "white"), normalize = NULL, zclip = NULL,
zlim = normalize, main = "", fun = NULL, nc = NA, ...) {
zlim = normalize, main = "", title_position = c("left", "top"),
fun = NULL, nc = NA, ...) {
# DIPSAUS DEBUG START
# volume <- "~/rave_data/raw_dir/YAB/rave-imaging/fs/mri/brain.finalsurfs.mgz"
# list2env(list(transform = NULL, positions = NULL, zoom = 1, pixel_width = 0.5,
Expand All @@ -33,6 +35,9 @@ plot_slices <- function(
# fun <- NULL
# positions = rnorm(12)
# nc <- 1
# title_position <- "top"

title_position <- match.arg(title_position)

if( is.character(volume) ) {
volume <- read_volume(volume)
Expand Down Expand Up @@ -118,16 +123,31 @@ plot_slices <- function(
}
nc <- min(max(round(nc), 1), npts)
nr <- ceiling(npts / nc)
lmat <- matrix(seq_len(nr * nc), ncol = nc, byrow = FALSE)
lmat <- t(apply(lmat, 1, function(l) {
l <- (l - 1) * 4
as.vector(rbind(l + 1, l + 2, l + 3, l + 4))
}))
dim(lmat) <- c(nr, nc * 4)
graphics::layout(
lmat,
widths = rep(c(graphics::lcm(0.8), 1, 1, 1), times = nc)
)
if( title_position == "left") {
lmat <- matrix(seq_len(nr * nc), ncol = nc, byrow = FALSE)
lmat <- t(apply(lmat, 1, function(l) {
l <- (l - 1) * 4
as.vector(rbind(l + 1, l + 2, l + 3, l + 4))
}))
dim(lmat) <- c(nr, nc * 4)
graphics::layout(
lmat,
widths = rep(c(graphics::lcm(0.8), 1, 1, 1), times = nc)
)
} else {
lmat <- matrix(seq_len(nr * nc), ncol = nc, byrow = TRUE)
lmat <- apply(lmat, 2, function(l) {
l <- (l - 1) * 4
c(rep(l + 1, each = 3), t(outer(l, c(2,3,4), FUN = "+")))
})
dim(lmat) <- c(nr * 3, nc * 2)
lmat <- t(lmat)
graphics::layout(
lmat,
heights = rep(c(graphics::lcm(0.8), 1), times = nc)
)
}

graphics::par(
bg = pal[[1]],
fg = pal[[length(pal)]],
Expand Down Expand Up @@ -172,7 +192,11 @@ plot_slices <- function(

adjust_plt(reset = TRUE)
graphics::plot.new()
graphics::mtext(side = 4, line = -1.5, text = main[[ii]], las = 0)
if(title_position == "top") {
graphics::mtext(side = 1, line = 0, text = main[[ii]], las = 0)
} else {
graphics::mtext(side = 4, line = -1.5, text = main[[ii]], las = 0)
}

# Axial
# translate x transform_inv x translate^-1 x Norig
Expand Down
3 changes: 3 additions & 0 deletions man/plot_slices.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77a28c2

Please sign in to comment.