Skip to content

Commit 32da86c

Browse files
committed
the visualisation, model visualisation, diagnostic plots are now running in the main R process. Previously they are run in a background R process. But the overhead of creating a new process and sending the data is too large
1 parent 4b87d86 commit 32da86c

5 files changed

Lines changed: 76 additions & 26 deletions

File tree

OpenStats/R/Backend_History.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ eval_entry_V1_2 <- function(entry, DataModelState,
4040
com = backend_communicator_V1_2
4141
)
4242
res$validate()
43-
res <- res$eval(ResultsState)
44-
get_result(ResultsState)
43+
res$eval(ResultsState)
4544
},
4645
VisualizationModel = {
4746
res <- visualisation_model_V1_2$new(
4847
df = DataModelState$df, DataModelState$formula, entry[["Layer"]]
4948
)
5049
res$validate()
5150
res$eval(ResultsState)
52-
get_result(ResultsState)
5351
},
5452
ApplyFilter = {
5553
res <- apply_filter_V1_2$new(
@@ -147,7 +145,6 @@ eval_entry_V1_2 <- function(entry, DataModelState,
147145
)
148146
res$validate()
149147
res$eval(ResultsState)
150-
get_result(ResultsState)
151148
},
152149
DoseResponse = {
153150
response_type <- entry[["Response type"]]

OpenStats/R/Backend_V1_2_Engine.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ visualisation_V1_2 <- R6::R6Class(
521521
),
522522
promise_result_name = new_result_name,
523523
promise_history_entry = promise_history_entry,
524-
in_background = TRUE, ResultsState
524+
in_background = FALSE, ResultsState
525525
)
526526
},
527527
create_history = function(new_result_name) {
@@ -588,7 +588,7 @@ visualisation_model_V1_2 <- R6::R6Class(
588588
),
589589
promise_result_name = new_name,
590590
promise_history_entry = promise_history_entry,
591-
in_background = TRUE, ResultsState
591+
in_background = FALSE, ResultsState
592592
)
593593
},
594594
warning = function(warn) {
@@ -1320,7 +1320,7 @@ diagnostic_plots_V1_2 <- R6::R6Class(
13201320
args = list(df = self$df, formula = self$formula),
13211321
promise_result_name = new_name,
13221322
promise_history_entry = promise_history_entry,
1323-
in_background = TRUE, ResultsState
1323+
in_background = FALSE, ResultsState
13241324
)
13251325
},
13261326
warning = function(warn) {

OpenStats/R/Server_Visualization.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ visServer <- function(id, DataModelState, ResultsState) {
276276
})
277277

278278
# Plot model
279-
# TODO: add tests for history and backend
280279
plot_model_fct <- function(method) {
281280
print_req(is.data.frame(DataModelState$df), "The dataset is missing")
282281
print_form(DataModelState$formula)
Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
library(ggplot2)
2+
library(Randomization)
13
groups <- read.csv("./Randomization/development/tesstSaAH.csv", sep = ";", header = TRUE)
24
names(groups)[[1L]] <- "ids"
35
groups[, 2:6] <- apply(groups[, 2:6], 2, as.numeric)
@@ -11,28 +13,78 @@ n_blocks <- data.frame(
1113
rep("Gi", 10L)
1214
)
1315
)
14-
n_blocks
1516

16-
result <- Randomization::random_finite_assign(
17-
seed = 1234,
17+
result_mahalanobis <- random_finite_assign(
18+
seed = 42L,
1819
groups = groups[, 2:6],
1920
design = n_blocks,
2021
max_iter = 1000L,
21-
ids = groups$ids,
22+
loss_function = "Mahalanobis",
23+
ids = groups$ids
24+
)
25+
groups_optim_mahalanobis <- groups[result_mahalanobis$unit_id, 2L:6L]
26+
df_optim_mahalanobis <- data.frame(
27+
stack(groups_optim_mahalanobis),
28+
treatment = rep(n_blocks$treatment, 5L),
29+
group = "Mahalanobis"
2230
)
23-
result
2431

25-
df <- data.frame(
26-
treatment = n_blocks$treatment,
27-
groups[result$unit_id, 2:6]
32+
result_default <- random_finite_assign(
33+
seed = 42L,
34+
groups = groups[, 2:6],
35+
design = n_blocks,
36+
max_iter = 1000L,
37+
ids = groups$ids
38+
)
39+
groups_optim_default <- groups[result_default$unit_id, 2L:6L]
40+
df_optim_default <- data.frame(
41+
stack(groups_optim_default),
42+
treatment = rep(n_blocks$treatment, 5L),
43+
group = "Default"
2844
)
29-
means <- tapply(df, df$treatment, function(b) {
30-
b <- c(b[, 2:6]) |> unlist()
31-
mean(b)
32-
})
33-
sds <- tapply(df, df$treatment, function(b) {
34-
b <- c(b[, 2:6]) |> unlist()
35-
sd(b)
36-
})
37-
means
38-
sds
45+
46+
df_orig <- data.frame(
47+
stack(groups[, 2L:6L]),
48+
treatment = rep(n_blocks$treatment, 5L),
49+
group = "origin"
50+
)
51+
52+
df <- Reduce(rbind, list(df_optim_mahalanobis, df_optim_default))
53+
54+
ggplot(data = df, aes(x = treatment, y = values)) +
55+
stat_summary(
56+
data = df, aes(group = group, linetype = group, colour = group),
57+
fun = mean, geom = "line",
58+
position = position_dodge(width = 0.8), color = "black"
59+
) +
60+
stat_summary(
61+
aes(group = group, colour = group),
62+
fun.data = mean_sdl,
63+
fun.args = list(mult = 1), # 1 SD
64+
geom = "errorbar",
65+
width = 0.2,
66+
position = position_dodge(width = 0.8)
67+
) +
68+
facet_wrap(~ ind, scales = "free")
69+
70+
71+
summary <- function(df, fct) {
72+
res <- list()
73+
for (g in unique(df$group)) {
74+
sub1 <- df[df$group == g, ]
75+
temp1 <- c()
76+
for (i in unique(sub1$ind)) {
77+
sub2 <- sub1[sub1$ind == i, ]
78+
temp2 <- c()
79+
for (t in unique(sub2$treatment)) {
80+
sub3 <- sub2[sub2$treatment == t, ]
81+
temp2[[t]] <- fct(sub3$values)
82+
}
83+
temp1[[i]] <- temp2 |> unlist()
84+
}
85+
res[[g]] <- Reduce(rbind, temp1)
86+
}
87+
res
88+
}
89+
summary(df, mean)
90+
summary(df, sd)

development/testing.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
install.packages("OpenStats", type = "source", repos = NULL)
2+
tinytest::test_package("OpenStats")

0 commit comments

Comments
 (0)