Skip to content

Commit 7c1a548

Browse files
committed
Merge branch 'global_analysis'
2 parents 4f81368 + 4b197a0 commit 7c1a548

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2943
-2890
lines changed

Paper/GlobalAnalysis/10Runs.RData

1.34 MB
Binary file not shown.

Paper/GlobalAnalysis/Full.csv

Lines changed: 18 additions & 0 deletions
Large diffs are not rendered by default.
134 KB
Loading

Paper/GlobalAnalysis/Result.RData

1.58 MB
Binary file not shown.
221 KB
Loading
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
load("./Paper/GlobalAnalysis/10Runs.RData")
2+
names(res[[1]])
3+
4+
ms <- lapply(seq_len(length(res)), \(i) {
5+
res[[i]][["metrices"]]
6+
})
7+
ms
8+
# Signal plots
9+
# ====================================================================
10+
states <- lapply(seq_len(length(res)), \(i) {
11+
elem <- res[[i]]
12+
temp <- elem[["data"]]
13+
temp <- temp[, 1:13] # Remove free dye and host-dye
14+
temp$run <- i
15+
temp
16+
})
17+
states <- Reduce(rbind, states)
18+
19+
wls <- c(470, 486, 512, 524, 532, 550)
20+
names(states)[2:7] <- paste("Measured", wls)
21+
names(states)[8:13] <- paste("Simulated", wls)
22+
23+
states <- data.frame(
24+
stack(states[, 2:13]),
25+
run = rep(states$run, 12),
26+
"Guest" = rep(states[[1]], 12)
27+
)
28+
states$group <- gsub("[0-9]| ", "", states$ind)
29+
states$signal_wavelengths <- as.numeric(gsub("[A-z]| ", "", states$ind))
30+
states$signal_names <- sprintf("Signal at %s nm", states$signal_wavelengths)
31+
32+
library(ggplot2)
33+
34+
p <- ggplot(data = states) +
35+
geom_boxplot(
36+
data = states[states$group == "Simulated", ],
37+
aes(x = Guest, y = values, group = Guest)
38+
) +
39+
geom_point(
40+
data = states[states$group == "Measured", ],
41+
aes(x = Guest, y = values, colour = "Measured")
42+
) +
43+
facet_wrap(~ signal_names, scales = "free") +
44+
labs(x = "Total Guest Measured [M]", y = NULL, colour = NULL)
45+
p
46+
47+
ggsave(p,
48+
file = "./Paper/GlobalAnalysis/SignalPlot.png",
49+
width = 10,
50+
height = 12.5
51+
)
52+
53+
# Parameter plots
54+
# ====================================================================
55+
params <- lapply(seq_len(length(res)), \(i) {
56+
res[[i]][["parameter"]]
57+
})
58+
params <- Reduce(rbind, params)
59+
60+
p_Ka <- ggplot(data = params[, 1, drop = FALSE],
61+
aes(y = .data[["Ka(HG) [1/M]"]])) +
62+
geom_boxplot() +
63+
theme(
64+
legend.title = element_blank(),
65+
axis.text = element_text(size = 14),
66+
axis.title = element_text(size = 18),
67+
legend.position = "bottom",
68+
legend.key.size = unit(0.6, "cm"),
69+
legend.key = element_rect(fill = "white"),
70+
axis.text.x = element_blank(),
71+
axis.ticks.x = element_blank()
72+
)
73+
74+
params <- stack(params[, 2:19])
75+
params$signal <- gsub(".*(Nr\\.[0-9]+).*", "\\1", params$ind)
76+
params$signal <- gsub("Nr.", "", params$signal) |> as.numeric()
77+
wls <- c(470, 486, 512, 524, 532, 550)
78+
params$signal <- vapply(params$signal, function(i) {
79+
wls[i]
80+
}, numeric(1))
81+
params$parameter <- sub("^.*?(I)", "\\1", params$ind)
82+
head(params)
83+
tail(params)
84+
params
85+
86+
p <- ggplot(data = params, aes(x = signal, y = values, group = signal)) +
87+
geom_boxplot() +
88+
facet_wrap(~ parameter, scales = "free") +
89+
labs(x = "Signal [nm]", y = NULL) +
90+
theme(
91+
legend.title = element_blank(),
92+
axis.text = element_text(size = 14),
93+
axis.title = element_text(size = 18),
94+
legend.position = "bottom",
95+
legend.key.size = unit(0.6, "cm"),
96+
legend.key = element_rect(fill = "white"),
97+
strip.text = element_text(size = 16)
98+
)
99+
100+
ggsave(p,
101+
file = "./Paper/GlobalAnalysis/ParameterPlots.png",
102+
width = 10,
103+
height = 12.5
104+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
library(ggplot2)
2+
library(corrplot)
3+
library(RColorBrewer)
4+
5+
dfh <- read.csv("./Paper/GlobalAnalysis/Full.csv", header = TRUE)
6+
wl_cols <- grep("^X\\d+$", names(dfh), value = TRUE)
7+
wl_num <- as.numeric(gsub("X", "", wl_cols))
8+
X <- as.matrix(dfh[wl_cols])
9+
M <- cor(X)
10+
corrplot(
11+
M,
12+
type = "upper", order = "hclust",
13+
col = brewer.pal(n=8, name="RdYlBu"),
14+
tl.col = "black",
15+
tl.cex = 0.6
16+
)

Paper/GlobalAnalysis/global.R

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
library(tsf)
2+
library(parallel)
3+
4+
num_cores <- detectCores() - 3
5+
seeds <- c(376033, 945211, 478950, 429370, 78735, 268225, 324905, 359876, 868834, 584371)
6+
7+
pp <- function(res, wls) {
8+
ps <- vapply(res$parameter, function(x) {
9+
formatC(x[[1]], format = "e", digits = 2)
10+
}, character(1))
11+
n <- names(ps)[1]
12+
cat(paste0(n, " ", ps[[1]]), "\n")
13+
temp <- as.data.frame(matrix(ps[-1], nrow = 3))
14+
names(temp) <- wls
15+
row.names(temp) <- c("I(0)", "I(HD) [1/M]", "I(D) [1/M]")
16+
print(temp)
17+
invisible(NULL)
18+
}
19+
20+
df <- read.csv("./Paper/GlobalAnalysis/Full.csv", header = TRUE)
21+
wls <- paste("X", c(470, 486, 512, 524, 532, 550), sep = "")
22+
df <- df[, c("var", wls)]
23+
24+
ida <- function(df, seeds) {
25+
additionalParameters <- c(
26+
host = 4.0*10^-6,
27+
dye = 6.0*10^-6,
28+
kHD = 1.7*10^7
29+
)
30+
lowerBounds <- c(Ka = 1000,
31+
c(I0 = 0, IHD = 10^6, ID = 10^5), # 470
32+
c(I0 = 0, IHD = 10^6, ID = 10^5), # 486
33+
c(I0 = 0, IHD = 10^6, ID = 10^5), # 512
34+
c(I0 = 0, IHD = 10^6, ID = 10^5), # 524
35+
c(I0 = 0, IHD = 10^6, ID = 10^5), # 532
36+
c(I0 = 0, IHD = 10^6, ID = 10^5) # 550
37+
)
38+
upperBounds <- c(Ka = 10^10,
39+
c(I0 = 10^2, IHD = 10^10, ID = 10^9), # 470
40+
c(I0 = 10^2, IHD = 10^10, ID = 10^9), # 486
41+
c(I0 = 10^2, IHD = 10^10, ID = 10^9), # 512
42+
c(I0 = 10^2, IHD = 10^10, ID = 10^9), # 524
43+
c(I0 = 10^2, IHD = 10^10, ID = 10^9), # 532
44+
c(I0 = 10^2, IHD = 10^10, ID = 10^9) # 550
45+
)
46+
mclapply(seeds, function(seed) {
47+
opti(
48+
case = "ida",
49+
lowerBounds = lowerBounds,
50+
upperBounds = upperBounds,
51+
path = df,
52+
seed = seed,
53+
ngen = 5000,
54+
npop = 40,
55+
errorThreshold = -Inf,
56+
additionalParameters = additionalParameters,
57+
add_info = as.character(seed)
58+
)
59+
}, mc.cores = num_cores, mc.silent = TRUE, mc.preschedule = FALSE)
60+
}
61+
62+
res <- ida(df, seeds)
63+
save(res, file = "./Paper/GlobalAnalysis/10Runs.RData")

Paper/LocationSpread/Analyse.R

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
setwd("./Paper/LocationSpread")
2+
source("loadParams.R")
3+
source("DistributionFitting.R")
4+
source("Bootstrapping.R")
5+
source("Plotting.R")
6+
source("JDKBootstrapping.R")
7+
source("CalcValues.R")
8+
9+
kdjoint_smr <- jdk_smr(p_ida)
10+
set.seed(1234)
11+
# kdjoint <- jdk(p_ida, n_boot = 1000)
12+
# save(kdjoint, file = "kdjoint_bootstrapped10000.RData")
13+
distris <- c("lognorm", "exp", "norm", "norm")
14+
15+
data <- lapply(1:4, function(x) {
16+
res <- list()
17+
18+
bins <- hist(p_ida[, x], plot = FALSE)$breaks
19+
density <- hist(p_ida[, x], plot = FALSE)$density
20+
mind <- min(density)
21+
maxd <- max(density)
22+
res[["RawData"]] <- list(p_ida[, x], bins)
23+
24+
median_ci <- calc_location_ci_bootstrap(median, p_ida[, x])
25+
median_iqr <- median_iqr(p_ida[, x])
26+
mean_ci <- calc_location_ci_bootstrap(mean, p_ida[, x])
27+
kde_ci <- modus_ci_hdr(p_ida[, x])
28+
29+
set.seed(1234)
30+
fd <- fit_distri(p_ida[, x], distris[x])
31+
df <- fd$df
32+
df$linetype <- "Fitted distribution"
33+
res[["Labels"]] <- fd
34+
35+
kde_ci$kd$linetype <- "Kernel density"
36+
df <- rbind(df, kde_ci$kd)
37+
res[["Density"]] <- df
38+
jk <- kdjoint_smr$df[[x]]
39+
ref_peak <- max(df$y[df$linetype == "Kernel density"], na.rm = TRUE)
40+
s <- ref_peak / max(jk$y, na.rm = TRUE)
41+
jk$y <- jk$y * s
42+
res[["JointKernelDensity"]] <- jk
43+
44+
location_error <- data.frame(
45+
x = c( mean_ci$location, median_ci$location, kde_ci$mode,
46+
kdjoint_smr$mode[[x]], kdjoint$mode[[x]], median_iqr[1]),
47+
xmin = c( mean_ci$lower_ci, median_ci$lower_ci, kde_ci$lower_ci,
48+
kdjoint_smr$lower_ci[[x]], kdjoint$lower_ci[[x]], median_iqr[2]),
49+
xmax = c( mean_ci$upper_ci, median_ci$upper_ci, kde_ci$upper_ci,
50+
kdjoint_smr$upper_ci[[x]], kdjoint$upper_ci[[x]], median_iqr[3]),
51+
type = c(
52+
"Mean",
53+
"Median",
54+
"Mode (KD)",
55+
"Mode (JDK)",
56+
"Mode (JDK boot.)",
57+
"Median IQR"
58+
),
59+
y = seq(mind, maxd, length.out = 6)
60+
)
61+
res[["LocationError"]] <- location_error
62+
res
63+
})
64+
65+
data[[4]][["Labels"]]
66+
67+
source("Plotting.R")
68+
plots <- lapply(1:4, function(x) {
69+
data <- data[[x]]
70+
p <- plot_raw_data(data[["RawData"]][[1]], data[["RawData"]][[2]])
71+
# p <- add_labels(p, data[["Labels"]])
72+
p <- add_density(p, data[["Density"]])
73+
p <- add_joint_kernel_density(p, data[["JointKernelDensity"]])
74+
p <- add_metrices(p, data[["LocationError"]])
75+
p
76+
})
77+
78+
legend <- get_legend(plots[[1]])
79+
plots <- lapply(plots, function(x) {
80+
x + theme(legend.position = "none")
81+
})
82+
plot_grid <- plot_grid(
83+
plotlist = plots, nrow = 4,
84+
labels = c("A", "B", "C", "D"), label_size = 28
85+
)
86+
final_plot <- plot_grid(
87+
plot_grid, legend,
88+
ncol = 1,
89+
rel_heights = c(1, 0.075)
90+
)
91+
final_plot
92+
93+
ggsave(final_plot,
94+
bg = "white",
95+
file = "LocationEstimation_1000nboot.png",
96+
width = 24,
97+
height = 16
98+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Median IQR
2+
# ========================================
3+
median_iqr <- function(x) {
4+
c(median(x), quantile(x, 0.25), quantile(x, 0.75))
5+
}
6+
7+
# Bootstrap method for calculating CI for the median
8+
# ========================================
9+
calc_location_ci_bootstrap <- function(
10+
location_fct,
11+
data, n_iter = 10000,
12+
conf_level = 0.95) {
13+
locations <- numeric(n_iter)
14+
for (i in 1:n_iter) {
15+
sample_data <- sample(
16+
data,
17+
size = length(data),
18+
replace = TRUE
19+
)
20+
locations[i] <- location_fct(sample_data)
21+
}
22+
lower_ci <- quantile(locations, (1 - conf_level) / 2)
23+
upper_ci <- quantile(locations, 1 - (1 - conf_level) / 2)
24+
# Confidence that true location is lying there
25+
return(
26+
list(
27+
location = location_fct(data),
28+
lower_ci = lower_ci, upper_ci = upper_ci
29+
)
30+
)
31+
}
32+
33+
# Kernel density estimation
34+
# ========================================
35+
# Draws 10000 times of HDR to estimate CIs
36+
modus_ci_hdr <- function(data) {
37+
res <- density(data)
38+
mode <- res$x[which.max(res$y)]
39+
40+
n_iter <- 10000
41+
modes <- numeric(n_iter)
42+
for (i in 1:n_iter) {
43+
sample_data <- sample(
44+
data,
45+
size = length(data),
46+
replace = TRUE
47+
)
48+
res <- density(sample_data)
49+
modes[i] <- res$x[which.max(res$y)]
50+
}
51+
lower_ci <- quantile(modes, 0.025)
52+
upper_ci <- quantile(modes, 0.975)
53+
res <- data.frame(x = res$x, y = res$y)
54+
return(
55+
list(
56+
kd = res,
57+
mode = mode,
58+
lower_ci = lower_ci, upper_ci = upper_ci
59+
)
60+
)
61+
}

0 commit comments

Comments
 (0)