Skip to content

Commit

Permalink
minor code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
craddm committed Jul 2, 2021
1 parent ba4842e commit 84b1df8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
41 changes: 32 additions & 9 deletions R/epoch_images.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,23 @@ erp_image.data.frame <- function(data,
}

if (!is.null(time_lim)) {
data <- filter(data,
time > time_lim[1],
time < time_lim[2])
data <-
dplyr::filter(
data,
time > time_lim[1],
time < time_lim[2]
)
}



if (all(electrode %in% data$electrode)) {
sel_elec <- electrode
data <-
dplyr::filter(
data,
electrode %in% sel_elec
)
create_erpimage(data,
electrode = electrode,
smoothing = smoothing,
Expand Down Expand Up @@ -132,8 +143,8 @@ erp_image.eeg_ICA <- function(data,
na.rm = TRUE,
...) {

if (!component %in% names(data$signals)) {
stop("Specified component not found.")
if (length(component) > 1) {
stop("Only one `component` should be supplied")
}
data <- select_elecs(data,
component = component)
Expand Down Expand Up @@ -362,9 +373,17 @@ erp_raster <- function(data,
clim = NULL,
interpolate = FALSE) {

if (inherits(data,
"eeg_tfr")) {
stop("Not currently implemented for `eeg_tfr` objects.")
if (
inherits(
data, c("eeg_tfr", "eeg_ICA")
)
) {
stop(
paste(
"Not currently implemented for objects of class",
class(data)[[1]]
)
)
}

if (!is.null(time_lim)){
Expand All @@ -383,7 +402,10 @@ erp_raster <- function(data,
data$time)
data <- lapply(data,
Matrix::colMeans)
data <- as.data.frame(do.call(rbind, data))
data <- as.data.frame(
do.call(rbind,
data)
)
data <- tidyr::gather(data,
electrode,
amplitude,
Expand All @@ -393,6 +415,7 @@ erp_raster <- function(data,
clim <- c(min(data$amplitude),
max(data$amplitude))
}

ggplot2::ggplot(data,
aes(x = time,
y = electrode,
Expand Down
44 changes: 30 additions & 14 deletions R/erp_scalp.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ erp_scalp <- function(data,
montage = NULL) {

chan_info <- NULL

if (is.eeg_epochs(data) & is.null(montage)) {
chan_info <- channels(data)
data <- eeg_average(data)
data <- as.data.frame(data,
long = TRUE)
} else if (is.eeg_epochs(data)) {
data <- eeg_average(data)
data <- as.data.frame(data,
long = TRUE)
}
Expand Down Expand Up @@ -79,12 +82,15 @@ erp_scalp <- function(data,
}

plotfun <- function(x) {
plot <- ggplot(data = x,
aes_(as.name(time),
as.name(amplitude)))
plot <- plot +
plot <-
ggplot2::ggplot(data = x,
aes_(as.name(time),
as.name(amplitude)))
plot <-
plot +
facet_wrap("electrodefacet")
plot <- plot +
plot <-
plot +
coord_cartesian(ylim = c(minAmp, maxAmp),
xlim = c(minTime, maxTime))
plot <- plot +
Expand All @@ -109,18 +115,25 @@ erp_scalp <- function(data,

data$electrodefacet <- data[, electrode]
data <- tidyr::nest(tibble::as_tibble(data),
data = c(time, amplitude, electrodefacet))
data <- dplyr::mutate(data, plot = map(data, plotfun))
data <- dplyr::select(data, -data)
data = c(time,
amplitude,
electrodefacet))
data <- dplyr::mutate(data,
plot = map(data, plotfun))
data <- dplyr::select(data,
-data)

# Get default electrode locations from pkg internal data
if (is.null(chan_info)){
data <- electrode_locations(data,
drop = TRUE,
montage = montage)
} else {
data <- dplyr::left_join(data, chan_info)
data <- dplyr::filter(data, !(is.na(x) | is.na(y)))
data <- dplyr::left_join(data,
chan_info,
by = "electrode")
data <- dplyr::filter(data,
!(is.na(x) | is.na(y)))
}

max_x <- max(abs(min(data$x, na.rm = TRUE)),
Expand All @@ -132,10 +145,13 @@ erp_scalp <- function(data,

panel_size <- floor(sqrt(plot_area / nrow(data)))

coords_space <- data.frame(x = c(-(max_x + panel_size),
max_x + panel_size),
y = c(-(max_y + panel_size),
max_y + panel_size))
coords_space <-
data.frame(
x = c(-(max_x + panel_size),
max_x + panel_size),
y = c(-(max_y + panel_size),
max_y + panel_size)
)

p <- ggplot(coords_space,
aes(x, y)) +
Expand Down

0 comments on commit 84b1df8

Please sign in to comment.