Skip to content

Commit

Permalink
Plotting slices have correct margins with partial plot
Browse files Browse the repository at this point in the history
  • Loading branch information
dipterix committed Jul 11, 2024
1 parent 4bd0352 commit bae88cc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changes since last CRAN release
* `7198a191 (HEAD -> master)` [_`dipterix`_]: Allow users to drag and drop value tables
* `10ac71ad (origin/master, origin/HEAD)` [_`dipterix`_]: Allow users to hide crosshairs
* `ef1e293d (HEAD -> master)` [_`dipterix`_]: Plotting slices have correct margins with partial plot
* `4bd0352c (origin/master, origin/HEAD)` [_`dipterix`_]: Allow users to drag and drop value tables
* `10ac71ad` [_`dipterix`_]: Allow users to hide crosshairs
* `05836f94` [_`dipterix`_]: Allow `shiny` app to change current color map via proxy
* `fbca42f4` [_`dipterix`_]: Allows drag-drop electrode color files; Added `set_electrode_data` to brain proxy class, allowing `shiny` applications to change the electrode data, set color palettes, and set value ranges in the same call
* `a14cd3f5` [_`dipterix`_]: Allow masks to be added to `T1`; Added `D99v2` for monkey brain; Added demo code; Export `GLTF` for `datacube2`
Expand Down
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: Your Advanced 3D Brain Visualization
Version: 1.1.0.9024
Version: 1.1.0.9025
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
9 changes: 8 additions & 1 deletion R/class_brain.R
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ Brain2 <- R6::R6Class(
graphics::points(0, 0, pch = 20, col = electrode_color,
cex = electrode_size)
},
device_init = NULL,
save_to = NULL, one_plot = is.null(save_to), width = 12, height = 4) {
# DIPSAUS DEBUG START
# self <- raveio::rave_brain('demo/DemoSubject')
Expand Down Expand Up @@ -1006,7 +1007,7 @@ Brain2 <- R6::R6Class(

if(length(save_to) == 1 && isTRUE(is.character(save_to))) {
if(endsWith(tolower(save_to), "png")) {
save_to <- sprintf("%s-%%04.png",
save_to <- sprintf("%s-%%04d.png",
gsub("[%0-9d]{0,}\\.png$", replacement = "",
save_to, ignore.case = TRUE))
grDevices::png(filename = save_to, width = width * 72,
Expand All @@ -1019,8 +1020,14 @@ Brain2 <- R6::R6Class(
title = "RAVE Slice Plots", bg = "black")
}
on.exit({ grDevices::dev.off() })

}
if(is.function(device_init)) {
device_init()
}



if( one_plot ) {
plot_idx <- list(plot_idx)
}
Expand Down
55 changes: 38 additions & 17 deletions R/plot_volume-slices.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ plot_slices <- function(
stop("`volume` must be character or threeBrain.volume")
}

pal <- grDevices::colorRampPalette(col)(256)

more_args <- list(...)
more_args$axes <- FALSE
more_args$asp <- 1
more_args$col <- pal
more_args$zlim <- zlim
more_args$useRaster <- TRUE
more_args$main <- ''
more_args$xlab <- ""
more_args$ylab <- ""
default_add <- isTRUE(more_args$add)
# more_args$add <- FALSE

canvas_ratio <- 3
n_plots <- 3
if( default_add ) {
# assuming length(which) == 1 and you want to add to plot
canvas_ratio <- 1
n_plots <- 1
} else {
if(length(which) > 0) {
canvas_ratio <- 1
n_plots <- length(which)
}
}


overlays <- lapply(seq_along(overlays), function(ii) {
item <- overlays[[ ii ]]
default_color <- col2hexStr(DEFAULT_COLOR_DISCRETE[[(ii - 1L) %% length(DEFAULT_COLOR_DISCRETE) + 1L]], alpha = overlay_alpha)
Expand Down Expand Up @@ -145,36 +173,28 @@ plot_slices <- function(
main <- ""
}
main <- rep(main, ceiling(npts / length(main)))
pal <- grDevices::colorRampPalette(col)(256)

x <- seq(-127.5, 127.5, by = abs(pixel_width * zoom)) / zoom
nx <- length(x)

pos <- rbind(t(as.matrix(expand.grid(x, x, KEEP.OUT.ATTRS = FALSE))), 0, 1)

more_args <- list(...)
more_args$axes <- FALSE
more_args$asp <- 1
more_args$col <- pal
more_args$zlim <- zlim
more_args$useRaster <- TRUE
more_args$main <- ''
more_args$xlab <- ""
more_args$ylab <- ""
more_args$x <- x
more_args$y <- x
default_add <- isTRUE(more_args$add)
# more_args$add <- FALSE

pos <- rbind(t(as.matrix(expand.grid(x, x, KEEP.OUT.ATTRS = FALSE))), 0, 1)

oldpar <- graphics::par(no.readonly = TRUE)

if(!length(nc) || is.na(nc[[1]])) {
nc <- grDevices::n2mfrow(npts, asp = 1/3)[[2]]
nc <- grDevices::n2mfrow(npts, asp = 1/n_plots)[[2]]
} else {
nc <- nc[[1]]
}
nc <- min(max(round(nc), 1), npts)
nr <- ceiling(npts / nc)

padding_left <- 0
padding_top <- 0

if(!length(which)) {
if( title_position == "left") {
lmat <- matrix(seq_len(nr * nc), ncol = nc, byrow = FALSE)
Expand All @@ -187,6 +207,7 @@ plot_slices <- function(
lmat,
widths = rep(c(graphics::lcm(0.8), 1, 1, 1), times = nc)
)
padding_left <- 0.8
} else {
lmat <- matrix(seq_len(nr * nc), ncol = nc, byrow = TRUE)
lmat <- apply(lmat, 2, function(l) {
Expand All @@ -199,6 +220,7 @@ plot_slices <- function(
lmat,
heights = rep(c(graphics::lcm(0.8), 1), times = nc)
)
padding_top <- 0.8
}

graphics::par(
Expand All @@ -211,10 +233,9 @@ plot_slices <- function(
on.exit({ do.call(graphics::par, oldpar) })
}


# Calculate plt
pin <- graphics::par("din")
pin[[1]] <- (pin[[1]] - 0.8 / 2.54) / nc / 3
pin[[1]] <- (pin[[1]] - padding_left / 2.54) / nc / canvas_ratio
pin[[2]] <- pin[[2]] / nr
if(pin[[1]] > pin[[2]]) {
ratio <- pin[[2]] / pin[[1]]
Expand Down

0 comments on commit bae88cc

Please sign in to comment.