Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update predictpbpk.R #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions R/predictpbpk.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
predictpbpk <- function(dataset, rawModel, additionalInfo){

n_comp <- length(additionalInfo$predictedFeatures) - 1
feats <- colnames(dataset$dataEntry[,2])
# feats <- colnames(dataset$dataEntry)
# Get feature keys (a key number that points to the url)
feat.keys <- dataset$features$key
# Get feature names (actual name)
feat.names <- dataset$features$names
# Convert names from a factor list to a vector of characters
feat.names <- as.vector(unlist(lapply(feat.names, as.character)))
# Create a dataframe that includes the feature key and the corresponding name
key.match <- data.frame(cbind(feat.keys, feat.names), stringAsFactors = FALSE)

# Initialize a dataframe with as many rows as the number of values per feature
rows_data <- length(dataset$dataEntry$values[,2])
df <- data.frame(matrix(0, ncol = 0, nrow = rows_data))
for(i in feats){
fe <- additionalInfo$independentFeatures[i][[i]]

for(key in feat.keys){
# For each key (feature) get the vector of values (of length 'row_data')
feval <- dataset$dataEntry$values[i][,1]
df[fe] <- feval
# Name the column with the corresponding name that is connected with the key
df[key.match[key.match$feat.keys == i, 2]] <- feval
}

mod <- unserialize(base64_dec(rawModel))
Expand All @@ -23,9 +33,10 @@ predictpbpk <- function(dataset, rawModel, additionalInfo){
# predFeat <- additionalInfo$predictedFeatures[1][[1]]
predFeat <- additionalInfo$predictedFeatures

initial_concentration = rep(0, n_comp)
for(i in 1:length(initial_concentration)){
con = paste("y", i, sep="")
comp <- additionalInfo$comp
initial_concentration <- rep(0,length(comp))
for(i in length(comp)){
con = paste("C0_", comp[i], sep="")
initial_concentration[i] = df[[con]]
}
weight = df$weight
Expand All @@ -37,22 +48,22 @@ predictpbpk <- function(dataset, rawModel, additionalInfo){
sample_time <- c(0, 5/60, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 6, 8, 10, 12, 24, 36, 48, 72) # in hours
solution <- ode(y = initial_concentration, times = sample_time, func = odemodel, parms = params)

comp_names <- rep(0, n_comp + 1)
comp_short <- rep(0, n_comp + 1)
pred_names <- rep(0, n_comp + 1)
for(i in 1:length(comp_names)){
comp_names[i] <- additionalInfo$predictedFeatures[[i]]
comp_short[i] <- as.integer(strsplit(additionalInfo$predictedFeatures[[i]], "_")[[1]])
pred_names[i] <- names(additionalInfo$predictedFeatures)[i]
}
comp_all <- data.frame(cbind(comp_names, comp_short, pred_names))
comp_all_s <- comp_all[order(comp_short),][,1]
#comp_names <- rep(0, n_comp + 1)
#comp_short <- rep(0, n_comp + 1)
#pred_names <- rep(0, n_comp + 1)
#for(i in 1:length(comp_names)){
# comp_names[i] <- additionalInfo$predictedFeatures[[i]]
# comp_short[i] <- as.integer(strsplit(additionalInfo$predictedFeatures[[i]], "_")[[1]])
# pred_names[i] <- names(additionalInfo$predictedFeatures)[i]
#}
#comp_all <- data.frame(cbind(comp_names, comp_short, pred_names))
#comp_all_s <- comp_all[order(comp_short),][,1]



for(i in 1:dim(solution)[1]){
prediction<- data.frame(t(solution[i,]))
colnames(prediction)<- comp_all_s
colnames(prediction)<- c("time", comp)
if(i==1){lh_preds<- list(unbox(prediction))
}else{
lh_preds[[i]]<- unbox(prediction)
Expand Down