Concatenated Premises for Presentation \[ lim_{\epsilon \to 0} A - \epsilon \\ \min(\epsilon) \text{ when standard deviation in period is minimized: } \\ \min( \sqrt{ \frac{1}{n}\sum_{i = 1}^n \max_{\text{local}}V_T(t)_{i} - \max_{\text{local}}V_T(t)_{i+1} - \mu(\max_{\text{local}}V_T(t)_{i} - \max_{\text{local}}V_T(t)_{i+1}))^2})\\ \text{and when:} \\ lim_{coh \to 1}coh(B(t),P_{\gamma}(t)) \\ \text{ ie. coherence between }P_{\gamma}(t) \text{ and }B(t) \text{ is maximized} \\ \text{where:} \\ B(t) = \text{breath time-series} \text{ & } .2Hz \leq f(B(t)) \leq .35Hz\ (f = \text{frequency})\\ BPM_a = \text{Actual breaths in a minute} \\ BPM_r = \text{Number of breaths reported in a minute} \\ P_{\gamma}(t) = \text{gamma power time-series} \\ A = \text{attention: }\frac{60s - \frac{60s}{BPM_{a}} * \mid BPM_{r} - BPM_{a} \mid}{60s} \\ \text{attention standardized with reference to a consistent measure of time} \\ \text{ to account for differences in rate of breathing} \\ \epsilon =\text{Error} \\ \text{ie a perfect attention score is 100 \& }\epsilon \text{ is the error resulting from actual attention score} \\ V_T = \text{Breath tidal volume} \] Coherence Assumption \[ lim_{\epsilon \to 0 } A_{ext} - \epsilon \\ lim_{coh \to 1} coh(t) = coh(B(t),P_{\gamma}(t)) \\ A_{ext} = \text{exteroceptive attention: }\frac{60s - \frac{60s}{CC_{a} } * \mid CC_{r} - CC_{a} \mid}{60s} \\ \text{ where: } \\ CC_r = \text{reported color changes} \\ CC_a = \text{actual color changes} \\ f(B(t)) = .2Hz \leq f(B(t)) \leq .35Hz \\ \text{ attention standardized with reference to a consistent measure of time} \\ \epsilon = \text{Error, ie a perfect attention score is 100 }\epsilon \text{ is the error resulting from actual attention score} \\ \] Consistency of Period assumption \[ lim_{\epsilon \to 0} A - \epsilon \\ \min(\epsilon) \text{ when standard deviation in period is minimized: } \\ \min( \sqrt{ \frac{1}{n}\sum_{i = 1}^n \max_{\text{local}}V_T(t)_{i} - \max_{\text{local}}V_T(t)_{i+1} - \mu(\max_{\text{local}}V_T(t)_{i} - \max_{\text{local}}V_T(t)_{i+1}))^2})\\ \text{where:} \\ B(t) = \text{breath time-series} \text{ & } .2Hz \leq f(B(t)) \leq .35Hz\ (f = \text{frequency}) \\ V_T = \text{Breath tidal volume} \]
Read Respiratory
Resp <- read.delim("~/Northeastern/Spring 2019/HINF6500 Predictive Analytics/Project/Data/sub-032439_55%/Physio-sub-032439/sub-032439_ses-01_task-rest_acq-AP_run-01_recording-resp_physio.tsv",
header = FALSE)
Resp %<>% .[, -3]
Resp[, 2] <- seq(from = 0, length.out = nrow(Resp), by = 0.001)
names(Resp) <- c("Pressure", "Time")
ggplot(Resp[1:60000, ], aes(x = Time, y = Pressure)) + geom_line() + geom_vline(xintercept = Resp[HDA::find_peaks(Resp[1:60000,
1], m = 850), 2]) + labs(title = "Respiratory Raw", subtitle = "", caption = "",
x = "Time (s)", y = "Pressure (?)") + theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))
Read EEG Data
fbands <- read_csv("~/MATLAB/HighPass.csv", col_names = F)
rn <- read.csv("~/MATLAB/Highpass_rownames.csv", header = F, as.is = T) %>% .[1,
, drop = T] %>% unlist
time <- read.csv("~/MATLAB/Highpass_time.csv", header = F) %>% t %>% .[, 1, drop = T]
Bwaves <- lapply(fbands[, 1, drop = T], rn = rn, time = time, verbose = F, function(x,
rn, time, verbose) {
p <- paste0("~/MATLAB/Highpass_", x, ".csv")
if (verbose == T)
print(p)
out <- read_csv(p, col_names = F) %>% data.table::transpose()
if (verbose == T)
print(ncol(out))
colnames(out) <- rn
if (verbose == T)
print(nrow(out))
out$Time <- time
out %<>% select(Time, everything())
return(out)
})
names(Bwaves) <- fbands[, 1, drop = T]
Graph EEG Data
Bwaves$gamma1[, c("Time", "Fp1")] %>% ggplot(data = ., mapping = aes(x = Time, y = Fp1)) +
geom_line() + scale_x_continuous(limits = c(0, 5)) + scale_y_continuous(labels = function(x) format(x,
scientific = TRUE)) + labs(title = "Raw EEG", subtitle = "", caption = "", x = "Time (s)",
y = "Power Amplitude (?)") + theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5))
Respiratory Smoothing and Visualization
Resp_ksmooth <- with(Resp[1:60000, ], {
ksmooth(Time, Pressure, x.points = Time, kernel = "box", bandwidth = 0.25)
}) %>% as.data.frame
ggplot(Resp_ksmooth, aes(x = x, y = y)) + geom_line() + geom_vline(xintercept = Resp_ksmooth[HDA::find_peaks(Resp_ksmooth[,
2], m = 850), 1])
Resp_loess <- loess(Pressure ~ Time, Resp[1:60000, ], span = 0.1)
Resp_loess_df <- data.frame(y = predict(Resp_loess), x = Resp_loess[["x"]][, 1])
ggplot(Resp_loess_df, aes(x = x, y = y)) + geom_line() + geom_vline(xintercept = Resp_loess_df[HDA::find_peaks(Resp_loess_df[,
"y"], m = 850), "x"])
ggplot(Resp_ksmooth, aes(x = x, y = y)) + geom_line() + geom_vline(xintercept = Resp_ksmooth[HDA::find_peaks(Resp_loess_df[,
"y"], m = 850), "x"])
Resp_ksmooth250hz <- Resp_ksmooth[seq(1, 60000, 4), ]
ggplot(Resp_ksmooth250hz, aes(x = x, y = y)) + geom_line()
Resp_sma <- QuantTools::sma(Resp_ksmooth250hz$y, n = 250)
Resp_sma[1:249] <- Resp_loess_df$y[seq(1, 996, 4)]
Resp_sma <- cbind.data.frame(y = scale(Resp_sma) %>% .[, 1], x = Resp_ksmooth250hz$x)
Resp_sma$y %<>% pracma::detrend() %>% .[, 1]
ggplot(Resp_sma, aes(x = x, y = y)) + geom_line() + theme_light() + labs(title = "Respiratory Signal - 1st 60s",
subtitle = "Detrended and Smoothed", caption = "ksmooth and sma applied", x = "Time (s)",
y = "Normalized Pressure") + theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5))
Simulate Respiratory Impulse
## ----------------------- Mon Apr 08 13:23:44 2019 ------------------------#
## TODO: Take the derivative of the respiratory signal to capture flow rate. From
## flow rate determine the breath impulse.
library(pspline)
out <- predict(pspline::sm.spline(Resp_sma$x, Resp_sma$y), Resp_sma$x, 1)[, 1]
kout <- ksmooth(Resp_sma$x, out, kernel = "normal")
ind <- out > qnorm(0.999, mean(out), sd(out))
out[100:15000] <- QuantTools::sma(out, 100)[100:15000]
out[1:99] <- kout$y[1:99]
ind <- out > qnorm(0.999, mean(out), sd(out))
out[ind] <- ksmooth(Resp_sma$x, out, kernel = "normal", bandwidth = 0.8)$y[ind]
Resp_sma$dydt <- out
# ----------------------- Mon Apr 08 16:37:10 2019 ------------------------# Is
# the breath impulse the integral of dy/dt?
pos.neg <- rle(Resp_sma$dydt > 0) %>% unclass() %>% as.data.frame() %>% mutate(end = cumsum(lengths),
start = c(1, lag(end)[-1] + 1)) %>% extract(c(1, 2, 4, 3))
Resp_sma <- lapply(apply(pos.neg[pos.neg$values == T, ], 1, function(r) {
seq(r["start"], r["end"])
}), df = Resp_sma, function(ind, df) {
df[ind, ]
}) %>% lapply(function(df) {
df %>% mutate(cs = cumsum(dydt))
}) %>% do.call("rbind", .) %>% right_join(Resp_sma, by = c("x", "y", "dydt"))
Resp_sma$cs[is.na(Resp_sma$cs)] <- 0
Resp_sma$cs %<>% scale(center = 0) %>% .[, 1]
ggplot(Resp_sma, aes(x = x, y = y)) + geom_line() + geom_line(aes(y = dydt), color = HDA::ggColor(2)[1]) +
geom_line(aes(y = cs), color = HDA::ggColor(2)[2], size = 1) + theme_light() +
labs(title = "Respiratory Signal - 1st 60s", subtitle = latex2exp::TeX("Flow Rate $(\\frac{dB}{dt})$ in Red, Tidal Volume $(\\int \\frac{dB}{dt} > 0)$ in blue"),
caption = latex2exp::TeX("ksmooth(bw=.8) and sma(100) applied to $(\\frac{dy}{dt})$"),
x = "Time (s)", y = "Normalized Value") + theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)) + guides(color = guide_legend())
Translate Respiratory signal to Frequency Domain
Frequencies of smoothed breath signal at Specific Intervals
(Resp_freq <- seewave::dfreq(Resp_sma$y, f = 250, ovlp = 50, wl = 5000, wn = "bartlett",
plot = F, fftw = T, threshold = 25) %>% as.data.frame %>% setNames(c("Time (s)",
"Frequency (Hz)")))
Time (s) | Frequency (Hz) |
---|---|
0 | 0.00035 |
15 | 0.00030 |
30 | 0.00030 |
45 | 0.00035 |
60 | 0.00035 |
grDevices::png("../Resp_freq.png", width = 800, height = 600, units = "px", type = "cairo-png")
seewave::dfreq(Resp_sma$y, f = 250, ovlp = 50, wl = 5000, wn = "bartlett", fftw = T,
threshold = 25, ylim = c(0, 0.0005), main = "Time-Frequency Plot of Respiration")
dev.off()
## png
## 2
Spectrograph of smoothed breath signal
Resp_spectro <- seewave::spectro(Resp_sma$y, f = 250, w1 = 5000, ovlp = 50, wn = "bartlett",
flim = c(0, 0.002), norm = T, cont = T)
Compute Standard Deviation of the Period
ggplot(Resp_sma, aes(x = x, y = y)) + geom_line() + theme_light() + labs(title = "Respiratory Signal - 1st 60s",
subtitle = "Detrended and Smoothed", caption = "ksmooth and sma applied", x = "Time (s)",
y = "Normalized Pressure") + theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
geom_vline(xintercept = Resp_sma[HDA::find_peaks(Resp_sma[, "y"], m = 250), "x"])
## [1] 0.1677472
Model EEG based on Respiratory Impulse
# ----------------------- Mon Apr 08 13:27:29 2019 ------------------------#
# Using the Respiratory impulse, model a threshold triggered exponential feature
# of an EEG signal according to what one would expect. -----------------------
# Wed Apr 10 13:37:43 2019 ------------------------# Hippocampus
Fp1 <- Bwaves$gamma1[, c("Time", "Fp1")]
Fp1$Fp1 <- scale(Fp1$Fp1, center = F)[, 1]
Fp1$Fp1[Fp1$Fp1 > qnorm(0.9, mean(Fp1$Fp1), sd(Fp1$Fp1))] <- mean(Fp1$Fp1)
Fp1$Hippocampus <- Fp1$Fp1 * Resp_sma$y
Fp1$Breath <- Resp_sma$y
Hippocampus_gg <- reshape2::melt(Fp1, measure.vars = c("Hippocampus", "Breath")) %>%
rename(Signal = variable) %>% ggplot(., aes(x = Time, y = value, color = Signal)) +
geom_line() + geom_line() + labs(title = "Expected Hippocampal EEG Signal", subtitle = "EEG Proportional to Smoothed Breath Signal",
caption = "", x = "Time (s)", y = "Amplitude") + theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))
plotly::ggplotly(Hippocampus_gg)
ggsave("../Images/Hippocampus.png", type = "cairo", width = 16/1.5, height = 9/1.5,
units = "in")
# ----------------------- Wed Apr 10 13:37:36 2019 ------------------------# OB
pos.neg <- rle(sign(Resp_sma$cs)) %>% unclass() %>% as.data.frame() %>% mutate(end = cumsum(lengths),
start = c(1, lag(end)[-1] + 1)) %>% extract(c(1, 2, 4, 3))
Resp_sma <- lapply(apply(pos.neg[pos.neg$values == 1, ], 1, function(r) {
seq(r["start"], r["end"])
}), df = Resp_sma, function(ind, df) {
df[ind, ]
}) %>% lapply(function(df) {
df %>% mutate(thrs = qnorm(0.8, mean(cs), sd(cs)))
}) %>% do.call("rbind", .) %>% right_join(Resp_sma, by = c("x", "y", "dydt", "cs"))
Resp_EEG <- left_join(Fp1 %>% mutate_at(vars(Time), funs(round(., 3))), Resp_sma %>%
mutate_at(vars(x), funs(round(., 3))), by = c(Time = "x"))
Resp_EEG$thrs[is.na(Resp_EEG$thrs)] <- 0
OBEEG <- function(cs, thrs, Fp1) {
if (cs > thrs) {
out <- Fp1 * cs
} else {
out <- Fp1
}
return(out)
}
Resp_EEG %<>% rowwise %>% mutate(OB = OBEEG(cs, thrs, Fp1))
OB_gg <- Resp_EEG %>% rename(`Tidal Volume` = cs) %>% reshape2::melt(., measure.vars = c("Tidal Volume",
"OB")) %>% rename(Signal = variable) %>% ggplot(mapping = aes(x = Time, y = value,
color = Signal)) + geom_line() + geom_line() + labs(title = "Expected Olfactory Bulb EEG Signal",
subtitle = "OB = EEG * Tidal Volume when TV > 80%", caption = "OB EEG signal \n multiplicative (EEG * Breath volume @ 80% local quantile threshold)",
x = "Time (s)", y = "Amplitude") + scale_color_manual(values = c(OB = HDA::ggColor(2)[1],
`Tidal Volume` = HDA::ggColor(2)[2])) + theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))
plotly::ggplotly(OB_gg)
Hippocampal Coherence
# ----------------------- Sat Apr 20 16:52:55 2019 ------------------------#
# Ensure coherence is appropriately calculated at the frequency of the
# respiratory signal.
tags$h4("Coherence between smoothed breathing signal and synthesized hippocampus signal")
grDevices::png("../Images/Sliding Coherence-Breathing & Synthesized Hippocampus.png",
width = 800, height = 600, units = "px", type = "cairo-png")
seewave::ccoh(Fp1$Breath, Fp1$Hippocampus, f = 250, wl = 5000, ovlp = 50, main = "Sliding Coherence: Breathing & Synthesized Hippocampus",
flim = c(0.0003, 0.00035))
dev.off()
## png
## 2
tags$strong(paste0("Mean coherence of synthesized hippocampus signal at respiratory frequency: ",
seewave::ccoh(Fp1$Breath, Fp1$Hippocampus, f = 250, wl = 5000, ovlp = 50, flim = c(0.0003,
0.00035), plot = F)$coh %>% mean %>% round(3) %>% assign("Hip_ccoh", ., envir = globalenv())))
Hip_Coh_t.test <- seewave::coh(Fp1$Breath, Fp1$Hippocampus, f = 250, main = "Gamma1 Coherence",
plot = F) %>% assign("Hip_Coh", ., envir = globalenv()) %>% subset(subset = .[,
1] <= 0.00035 & .[, 1] >= 0.0003) %>% .[, 2] %>% t.test(., Hip_Coh[, 2])
Hip_Coh_t.test$data.name <- "Coherence at .30 - .35 Hz"
grDevices::png("../Images/Coherence-Breath & Synthesized Hippocampus.png", width = 800,
height = 600, units = "px", type = "cairo-png")
seewave::coh(Fp1$Breath, Fp1$Hippocampus, f = 250, main = "Breath & Synthesized Hippocampus Coherence")
mtext("Frequency Domain \n Breath: .3 - .35 Hz", 3, cex = 0.8)
text(x = 0.0005, y = 0.95, labels = paste0(Hip_Coh_t.test$data.name, "\n ", Hip_ccoh,
" *", Hip_Coh_t.test$p.value %>% HDA::p.txt()), adj = 0)
dev.off()
## png
## 2
Olfactory Bulb Coherence
# ----------------------- Tue Apr 23 14:04:29 2019 ------------------------# OB
# Coherence
tags$h4("Coherence between smoothed breathing signal and synthesized olfactory bulb signal")
grDevices::png("../Images/Sliding Coherence-Breathing & Synthesized OB.png", width = 800,
height = 600, units = "px", type = "cairo-png")
seewave::ccoh(Fp1$Breath, Resp_EEG$OB, f = 250, wl = 5000, ovlp = 50, main = "Sliding Coherence: Breathing & Synthesized OB",
flim = c(0.0003, 0.00035))
text(x = 30, y = 0.0003, labels = "Breath: .3 - .35 Hz")
dev.off()
## png
## 2
tags$strong(paste0("Mean coherence of synthesized hippocampus signal at respiratory frequency: ",
seewave::ccoh(Fp1$Breath, Resp_EEG$OB, f = 250, wl = 5000, ovlp = 50, flim = c(0.0003,
0.00035), plot = F)$coh %>% mean %>% round(3) %>% assign("BreathOB_ccoh",
., envir = globalenv())))
OB_Coh_t.test <- seewave::coh(Fp1$Breath, Resp_EEG$OB, f = 250, main = "Breath & OB",
plot = F) %>% assign("OB_Coh", ., envir = globalenv()) %>% subset(subset = .[,
1] <= 0.00035 & .[, 1] >= 0.0003) %>% .[, 2] %>% t.test(., OB_Coh[, 2])
OB_Coh_t.test$data.name <- "OB Coherence at .30 - .35 Hz"
grDevices::png("../Images/Coherence-Breathing & Synthesized OB.png", width = 800,
height = 600, units = "px", type = "cairo-png")
seewave::coh(Fp1$Breath, Resp_EEG$OB, f = 250, main = "Breath & Synthesized OB Coherence")
mtext("Frequency Domain \n Breath: .3 - .35 Hz", 3, cex = 0.8)
text(x = 0.0005, y = qnorm(0.999, mean(OB_Coh[, 2]), sd(OB_Coh[, 2])), labels = paste0(OB_Coh_t.test$data.name,
"\n ", BreathOB_ccoh, " *", OB_Coh_t.test$p.value %>% HDA::p.txt()), adj = 0)
dev.off()
## png
## 2
Coherence with Fp1 Signal
# ----------------------- Tue Apr 23 14:03:53 2019 ------------------------#
# Coherence between raw Fp1 and smoothed breathing signal
tags$h4("Coherence between smoothed breathing signal and Raw Fp1 signal")
grDevices::png("../Images/Sliding Coherence-Gamma1 & Smoothed Breathing Signal.png",
width = 800, height = 600, units = "px", type = "cairo-png")
seewave::ccoh(Fp1$Breath, Bwaves$gamma1$Fp1, f = 250, wl = 5000, ovlp = 50, main = "Sliding Coherence: Gamma1 & Smoothed Breathing Signal",
sub = "Frequency-time Domain \n Gamma1: 30-59 Hz", flim = c(0.0003, 0.00035))
text(x = 30, y = 0.0003, labels = "Breath: .3 - .35 Hz")
dev.off()
## png
## 2
tags$strong(paste0("Mean coherence of Fp1 Gamma1 power over time signal at respiratory frequency: ",
seewave::ccoh(Fp1$Breath, Bwaves$gamma1$Fp1, f = 250, wl = 5000, ovlp = 50, flim = c(0.0003,
0.00035), plot = F)$coh %>% mean %>% round(3) %>% assign("BreathFp1_ccoh",
., envir = globalenv())))
Fp1_Coh_t.test <- seewave::coh(Fp1$Breath, Bwaves$gamma1$Fp1, f = 250, main = "Breath & Faw Fp1",
plot = F) %>% assign("Fp1_Coh", ., envir = globalenv()) %>% subset(subset = .[,
1] <= 0.00035 & .[, 1] >= 0.0003) %>% .[, 2] %>% t.test(., Fp1_Coh[, 2])
Fp1_Coh_t.test$data.name <- "Fp1 Raw Signal Coherence \n at .30 - .35 Hz"
grDevices::png("../Images/Coherence-Gamma1 & Smoothed Breathing Signal.png", width = 800,
height = 600, units = "px", type = "cairo-png")
seewave::coh(Fp1$Breath, Bwaves$gamma1$Fp1, f = 250, main = "Breath & Raw Fp1 Signal Coherence")
mtext("Frequency Domain \n Breath: .3 - .35 Hz", 3, cex = 0.8)
text(x = 0.0005, y = 0.95, labels = paste0(Fp1_Coh_t.test$data.name, "\n ", BreathFp1_ccoh,
" *", Fp1_Coh_t.test$p.value %>% HDA::p.txt()), adj = 0)
dev.off()
## png
## 2
Synthesize Signal and test Coherence
# ----------------------- Wed Apr 03 19:33:20 2019 ------------------------#
# Generate Signal and test Coherence
approxData <- data.frame(y1 = seewave::synth(f = 250, cf = 16, d = 60)[, 1], y2 = seewave::synth(f = 250,
cf = 50, d = 60)[, 1])
seewave::coh(approxData$y1, approxData$y2, f = 250, main = "16 & 50 Hz Coherence")
{
approxData$y1 * approxData$y2
} %>% plot(type = "l", xlim = c(0, 10), main = "Compound 16 & 50Hz")
Visualize Synthesized Waves
ggplot(data = approxData, mapping = aes(x = time, y = y1)) + geom_line(color = HDA::ggColor(2)[1],
, alpha = 0.75) + geom_line(aes(y = y2), color = HDA::ggColor(2)[2], alpha = 0.75) +
scale_x_continuous(limits = c(0, 10)) + labs(title = "Synthesized 16 & 50 Hz Waves",
subtitle = "", caption = "", x = "Amplitude", y = "Time (s)") + theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))
\[\begin{align} I_{ext} &= C_m \frac{dV_m}{dt} + I_{ion}\\ & = C_m \frac{dV_m}{dt} + g_{Na} m^3 h(V-E_{Na}) + g_K n^4 (V - E_K) + g_L (V - E_L) \end{align}\] \[\begin{align} C \frac{dV}{dt} & = I - g_{Na} m^3 h(V-E_{Na}) - g_K n^4 (V - E_K) -g_L (V - E_L)\\ \frac{dm}{dt} & = a_m(V) (1 - m) - b_m(V)m\\ \frac{dh}{dt} & = a_h(V)(1 - h) - b_h(V)h\\ \frac{dn}{dt} & = a_n(V) (1 - n) - b_n(V)n\\ a_m(V) & = 0.1(V+40)/(1 - \exp(-(V+40)/10))\\ b_m(V) & = 4 \exp(-(V + 65)/18)\\ a_h(V) & = 0.07 \exp(-(V+65)/20)\\ b_h(V) & = 1/(1 + \exp(- (V + 35)/10))\\ a_n(V) & = 0.01 (V + 55)/(1 - \exp(-(V+55)/10))\\ b_n(V) & = 0.125 \exp(-(V + 65)/80) \end{align}\] Model Hodgkin-Huxley Neuron
library(simecol)
HH <- simecol::odeModel(
main = function(time, init, parms) {
with(as.list(c(init, parms)),{
am <- function(v) 0.1*(v+40)/(1-exp(-(v+40)/10))
bm <- function(v) 4*exp(-(v+65)/18)
ah <- function(v) 0.07*exp(-(v+65)/20)
bh <- function(v) 1/(1+exp(-(v+35)/10))
an <- function(v) 0.01*(v+55)/(1-exp(-(v+55)/10))
bn <- function(v) 0.125*exp(-(v+65)/80)
dv <- (I - gna*h*(v-Ena)*m^3-gk*(v-Ek)*n^4-gl*(v-El))/C
dm <- am(v)*(1-m)-bm(v)*m
dh <- ah(v)*(1-h)-bh(v)*h
dn <- an(v)*(1-n)-bn(v)*n
return(list(c(dv, dm, dh, dn)))
})
},
## Set parameters
parms = c(Ena=50, Ek=-77, El=-54.4, gna=120, gk=36, gl=0.3, C=1, I=0),
## Set integrations times
times = c(from=0, to=40, by = 0.25),
## Set initial state
init = c(v=-65, m=0.052, h=0.596, n=0.317),
solver = "lsoda"
)
HH_sim <- simecol::sim(HH)
grDevices::png("../Images/GatingVariables.png", width = 800, height = 600, units = "px", type = "cairo-png")
plot(HH_sim)
par(cex.main = 3)
dev.off()
## png
## 2
Input Stimulus
times(HH)["to"] <- 100
## Stimulus
I <- c(2, 5, 5.97, 5.975, 6.2, 6.5)
I_sims <- do.call("rbind", lapply(I, function(i) {
parms(HH)["I"] <- i
HH <- sim(HH)
cbind(I = paste("I =", i), out(HH))
}))
## Plot the various experiments with lattice
library(latticeExtra)
grDevices::png("../Images/InputCurrents.png", width = 800, height = 600, units = "px",
type = "cairo-png")
asTheEconomist(xyplot(v ~ time | factor(I), data = I_sims, type = "l", as.table = TRUE,
main = "Hodgkin-Huxley model with varying external stimulus"), xlab = "time (ms)",
ylab = "mV", par.settings = list(cex.main = 5))
dev.off()
## png
## 2
Sensitivity Analysis with Sodium
grDevices::png("../Images/SodiumConductance.png", width = 800, height = 600, units = "px",
type = "cairo-png")
Ena <- rep(50, 6) #c(50, 51, 52, 53, 54, 55)
gNa <- seq(120, 135, by = 3)
Na_sims <- do.call("rbind", purrr::map2(Ena, gNa, function(.x, .y) {
parms(HH)["Ena"] <- .x
parms(HH)["gna"] <- .y
HH <- sim(HH)
cbind(Ena = paste("Ena =", .x), gNa = paste("gNa =", .y), out(HH))
}))
asTheEconomist(xyplot(v ~ time | factor(gNa), data = Na_sims, type = "l", as.table = TRUE,
main = "Hodgkin-Huxley model with increasing Sodium conductance"), xlab = "time (ms)",
ylab = "mV", par.settings = list(cex.main = 5))
dev.off()
## png
## 2
grDevices::png("../Images/SodiumPotential.png", width = 800, height = 600, units = "px",
type = "cairo-png")
Ena <- c(50, 51, 52, 53, 54, 55)
gNa <- rep(120, 6)
Na_sims <- do.call("rbind", purrr::map2(Ena, gNa, function(.x, .y) {
parms(HH)["Ena"] <- .x
parms(HH)["gna"] <- .y
HH <- sim(HH)
cbind(Ena = paste("Ena =", .x), gNa = paste("gNa =", .y), out(HH))
}))
asTheEconomist(xyplot(v ~ time | factor(Ena), data = Na_sims, type = "l", as.table = TRUE,
main = "Hodgkin-Huxley model with increasing Sodium potential"), xlab = "time (ms)",
ylab = "mV", par.settings = list(cex.main = 5))
dev.off()
## png
## 2
grDevices::png("../Images/SodiumCandP.png", width = 800, height = 600, units = "px",
type = "cairo-png")
Ena <- c(50, 51, 52, 53, 54, 55)
gNa <- seq(120, 135, by = 3)
Na_sims <- do.call("rbind", purrr::map2(Ena, gNa, function(.x, .y) {
parms(HH)["Ena"] <- .x
parms(HH)["gna"] <- .y
HH <- sim(HH)
cbind(Ena = paste("Ena =", .x), gNa = paste("gNa =", .y), out(HH))
}))
asTheEconomist(xyplot(v ~ time | factor(gNa), data = Na_sims, type = "l", as.table = TRUE,
main = "Hodgkin-Huxley model with increasing Sodium potential and conductance"),
xlab = "time (ms)", ylab = "mV", par.settings = list(cex.main = 5))
dev.off()
## png
## 2
Sensitivity Analysis with Sodium Conductance
grDevices::png("../Images/SodiumPotential.png", width = 800, height = 600, units = "px",
type = "cairo-png")
Ena <- c(50, 51, 52, 55, 62)
Na_sims <- do.call("rbind", lapply(Ena, function(i) {
parms(HH)["Ena"] <- i
HH <- sim(HH)
cbind(Ena = paste("Ena =", i), out(HH))
}))
asTheEconomist(xyplot(v ~ time | factor(Ena), data = Na_sims, type = "l", as.table = TRUE,
main = "Hodgkin-Huxley model with varying external stimulus"), xlab = "time (ms)",
ylab = "mV", par.settings = list(cex.main = 5))
dev.off()
## png
## 2
Load Test of Attentional Performance
# ----------------------- Mon Apr 01 13:38:27 2019 ------------------------# Load
# scores from TAP - Test of Attentional Performance: Alertness subtest
tap <- read_csv("Behavioural_Data_MPILMBB_LEMON/TAP-Alertness.csv")
tap_info <- read_lines("Behavioural_Data_MPILMBB_LEMON/TAP-Alertness_info.txt")
tap_info <- tap_info[!nchar(tap_info) < 1]
RemGL <- function(x) {
# Remove greater than and less than symbols
if (str_detect(x, "\\<")) {
x <- gsub("<", "", x) %>% as.numeric(x) %>% {
. - 0.5
}
} else if (str_detect(x, "\\>")) {
x <- gsub(">", "", x) %>% as.numeric(x) %>% {
. + 0.5
}
} else {
as.numeric(x)
}
return(x)
}
tap_total <- tap %>% select(X1, TAP_A_7, TAP_A_12) %>% na.omit
tap_total$TAP_A_7 %<>% sapply(RemGL)
tap_total$TAP_A_12 %<>% sapply(RemGL)
tap_total %<>% rowwise %>% mutate_at(vars(TAP_A_7, TAP_A_12), funs(as.numeric))
tap_total$TAP_total <- rowMeans(select(tap_total, starts_with("TAP")), na.rm = T)
tap_total %<>% arrange(desc(TAP_total))
tap_quartiles <- c(`100%` = tap_total[1, 1, drop = T], `75%` = tap_total %>% filter(TAP_total ==
75) %>% .[1, 1, drop = T], `55%` = "sub-032439", `25%` = tap_total %>% filter(TAP_total ==
25) %>% .[1, 1, drop = T], `0%` = tap_total %>% filter(TAP_total < 2) %>% .[2,
1, drop = T])