Skip to content

Commit

Permalink
update website, and minor code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
craddm committed Jul 6, 2021
1 parent 84b1df8 commit 5bedb36
Show file tree
Hide file tree
Showing 105 changed files with 1,111 additions and 433 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: eegUtils
Type: Package
Title: Utilities for Electroencephalographic (EEG) Analysis
Version: 0.6.2.9000
Version: 0.6.3
Date: 2021-06-08
Authors@R: c(
person("Matt", "Craddock", role = c("aut", "cre", "cph"), email = "[email protected]"),
Expand Down
2 changes: 1 addition & 1 deletion R/tfr_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ fft_n <- function(signal, n) {
} else {
signal_n <- signal[1:n]
}
fft(signal_n)
stats::fft(signal_n)
} else {
if (nrow(signal) < n) {
signal_n <- matrix(0,
Expand Down
260 changes: 143 additions & 117 deletions R/view_ica.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,87 +16,93 @@ view_ica <- function(data) {
}

ui <-
navbarPage("ICA viewer",
tabPanel("Topographies",
plotOutput("comp_topos",
dblclick = "topo_click"
)),
tabPanel("Timecourses",
plotOutput("ica_butters",
hover = "butter_click",
brush = brushOpts(
id = "butter_brush",
resetOnNew = TRUE
),
dblclick = "butter_dbl"),
verbatimTextOutput("info")),
tabPanel("PSDs",
plotOutput("ica_psd",
hover = "psd_click",
brush = brushOpts(
id = "psd_brush",
resetOnNew = TRUE
),
dblclick = "psd_dbl"),
verbatimTextOutput("psd_info")),
tabPanel("Individual",
sidebarLayout(
sidebarPanel(selectInput("comp_no",
"Component:",
channel_names(data)),
width = 3),
mainPanel(
fluidRow(
column(
width = 6,
plotOutput("indiv_topo")
),
column(
width = 6,
plotOutput("indiv_erpim"))
),
fluidRow(
column(
width = 6,
plotOutput("indiv_psd")
),
column(
width = 6,
plotOutput("indiv_tc")
)
),
width = 9
)
)
)
)
navbarPage(
"ICA viewer",
tabPanel(
"Topographies",
plotOutput("comp_topos",
dblclick = "topo_click")
),
tabPanel(
"Timecourses",
plotOutput(
"ica_butters",
hover = "butter_click",
brush = brushOpts(id = "butter_brush",
resetOnNew = TRUE),
dblclick = "butter_dbl"
),
verbatimTextOutput("info"),
tags$p(span("Hover over lines to see component details.")),
tags$p("To zoom, drag-click to highlight where you want to zoom, then double-click to zoom. Double-click again to zoom back out.")

),
tabPanel(
"PSDs",
plotOutput(
"ica_psd",
hover = "psd_click",
brush = brushOpts(id = "psd_brush",
resetOnNew = TRUE),
dblclick = "psd_dbl"
),
verbatimTextOutput("psd_info"),
tags$p(span("Hover over lines to see component details.")),
tags$p("To zoom, drag-click to highlight where you want to zoom, then double-click to zoom. Double-click again to zoom back out.")
),
tabPanel("Individual",
sidebarLayout(
sidebarPanel(selectInput(
"comp_no",
"Component:",
channel_names(data)
),
width = 3),
mainPanel(fluidRow(
column(width = 6,
plotOutput("indiv_topo")),
column(width = 6,
plotOutput("indiv_erpim"))
),
fluidRow(
column(width = 6,
plotOutput("indiv_psd")),
column(width = 6,
plotOutput("indiv_tc"))
),
width = 9)
))
)

server <- function(input,
output,
session) {

ranges <- reactiveValues(x = NULL,
y = NULL)
b_ranges <- reactiveValues(x = NULL,
y = NULL)

ica_erps <- as.data.frame(eeg_average(data),
long = TRUE)
ica_erps <-
as.data.frame(eeg_average(data),
long = TRUE)

ica_erps <- dplyr::group_by(ica_erps,
electrode,
time)
ica_erps <-
dplyr::group_by(ica_erps,
electrode,
time)

ica_erps <- dplyr::summarise(ica_erps,
amplitude = mean(amplitude))

psd_ica <- compute_psd(data,
verbose = FALSE)

psd_ica <- tidyr::pivot_longer(psd_ica,
cols = channel_names(data),
names_to = "component",
values_to = "power")
psd_ica <- tidyr::pivot_longer(
psd_ica,
cols = channel_names(data),
names_to = "component",
values_to = "power"
)

psd_ica <- dplyr::group_by(psd_ica,
component,
Expand All @@ -105,72 +111,88 @@ view_ica <- function(data) {
power = mean(10 * log10(power)))

output$comp_topos <- shiny::renderPlot(
ica_topos(data),
height = function() {
.8 * session$clientData$output_comp_topos_width
ica_topos(data),
height = function() {
.8 * session$clientData$output_comp_topos_width
}
)

output$ica_butters <- renderPlot(
plot_butterfly(data) +
coord_cartesian(xlim = b_ranges$x,
ylim = b_ranges$y)
)

output$ica_psd <- renderPlot({
ggplot(psd_ica,
aes(x = frequency,
y = power,
colour = component)) +
geom_line() +
theme_bw() +
coord_cartesian(xlim = ranges$x,
ylim = ranges$y,
expand = FALSE)}
)
output$ica_butters <-
renderPlot(
plot_butterfly(data) +
coord_cartesian(xlim = b_ranges$x,
ylim = b_ranges$y))

output$ica_psd <-
renderPlot({
ggplot(psd_ica,
aes(x = frequency,
y = power,
colour = component)) +
geom_line() +
theme_bw() +
coord_cartesian(xlim = ranges$x,
ylim = ranges$y,
expand = FALSE)
})

output$info <- renderPrint({
shiny::nearPoints(ica_erps,
input$butter_click,
threshold = 10,
maxpoints = 1)
as.data.frame(
shiny::nearPoints(ica_erps,
input$butter_click,
threshold = 20,
maxpoints = 1)
)
})

output$psd_info <- renderPrint({
shiny::nearPoints(psd_ica,
input$psd_click,
threshold = 20,
maxpoints = 1)
as.data.frame(
shiny::nearPoints(psd_ica,
input$psd_click,
threshold = 20,
maxpoints = 1)
)
})

output$indiv_topo <- renderCachedPlot({
topoplot(data, input$comp_no)
}, cacheKeyExpr = {input$comp_no})
topoplot(data,
input$comp_no,
verbose = FALSE)
}, cacheKeyExpr = {
input$comp_no
})

output$indiv_erpim <- renderCachedPlot({
erp_image(data,
input$comp_no)
}, cacheKeyExpr = {input$comp_no})
}, cacheKeyExpr = {
input$comp_no
})

output$indiv_tc <- renderCachedPlot({
plot_timecourse(data,
input$comp_no)
},
cacheKeyExpr = {input$comp_no})
cacheKeyExpr = {
input$comp_no
})

output$indiv_psd <- renderCachedPlot({
tmp_psd <-
compute_psd(select(data,
input$comp_no),
n_fft = data$srate,
noverlap = 0,
verbose = FALSE)
compute_psd(
select(data,
input$comp_no),
n_fft = data$srate,
noverlap = 0,
verbose = FALSE
)

tmp_psd <- dplyr::rename(tmp_psd,
power = 2)
tmp_psd <- dplyr::filter(tmp_psd,
frequency >= 3,
frequency <= 50)

ggplot(tmp_psd,
aes(x = frequency,
y = 10 * log10((power)))) +
Expand All @@ -184,15 +206,16 @@ view_ica <- function(data) {
y = "Power (dB)") +
coord_cartesian(expand = FALSE)
},
cacheKeyExpr = {input$comp_no},
cacheKeyExpr = {
input$comp_no
},
res = 96)

observeEvent(input$psd_dbl, {
brush <- input$psd_brush
if (!is.null(brush)) {
ranges$x <- c(brush$xmin, brush$xmax)
ranges$y <- c(brush$ymin, brush$ymax)

} else {
ranges$x <- NULL
ranges$y <- NULL
Expand All @@ -203,10 +226,9 @@ view_ica <- function(data) {
brush <- input$butter_brush
if (!is.null(brush)) {
b_ranges$x <- c(brush$xmin,
brush$xmax)
brush$xmax)
b_ranges$y <- c(brush$ymin,
brush$ymax)

brush$ymax)
} else {
b_ranges$x <- NULL
b_ranges$y <- NULL
Expand All @@ -223,23 +245,27 @@ view_ica <- function(data) {
ica_topos <- function(data) {
ggplot2::ggplot(get_scalpmap(data,
grid_res = 80),
aes(x = x,
y = y,
fill = scale(fill),
z = scale(fill))) +
aes(
x = x,
y = y,
fill = scale(fill),
z = scale(fill)
)) +
geom_raster(interpolate = TRUE) +
geom_head(data = channels(data),
mapping = aes(fill = NULL,
z = NULL)) +
geom_contour(aes(linetype = stat(level) < 0),
bins = 6,
colour = "black",
size = rel(0.8)) +
facet_wrap(~component) +
geom_contour(
aes(linetype = stat(level) < 0),
bins = 6,
colour = "black",
size = rel(0.8)
) +
facet_wrap( ~ component) +
theme_void() +
scale_fill_distiller(palette = "RdBu",
limits = c(-3, 3),
oob = scales::squish) +
limits = c(-3, 3),
oob = scales::squish) +
theme(legend.position = "none") +
coord_fixed()
}
2 changes: 1 addition & 1 deletion docs/404.html

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

2 changes: 1 addition & 1 deletion docs/CODE_OF_CONDUCT.html

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

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

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

Loading

0 comments on commit 5bedb36

Please sign in to comment.