Skip to content

Commit

Permalink
Update kpi_averaging.R
Browse files Browse the repository at this point in the history
Some reformating and minor update
  • Loading branch information
luchengqi7 committed Sep 20, 2023
1 parent 8f6d3a8 commit 73558fb
Showing 1 changed file with 45 additions and 61 deletions.
106 changes: 45 additions & 61 deletions src/main/R/drtAnalysis/kpi_averaging.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#####libraries####
library(stringr)
library(tidyverse)

#####global variables####
path_to_data <- "output-folder (specific case with different seeds)"
path_to_data <- "path/to/data (folder of a specific case, with different seeds)"
stats = c("mean","median","sd" ,"max", "min")

##### Collect all folder names####

folders_list<-list.files(path_to_data,full.names = TRUE)

folders_seeded = list()

for(i in 1:length(folders_list)){
if (endsWith(folders_list[i],".tsv")){
next
}

case_name = tail(str_split(folders_list[i],"-")[[1]],n = 1)

Expand All @@ -22,109 +24,91 @@ for(i in 1:length(folders_list)){
}
}

#####Reading and averaging av tables#####


folders_av_averaged_table = list()
#########################################
#####Reading and averaging drt tables####
folders_drt_averaged_table = list()

for(case_name in names(folders_seeded)){



for(folder in folders_seeded[[case_name]]){
files_list<- list.files(paste0(folder,"/analysis-drt-service-quality"),full.names = TRUE)
av_KPI_file <- files_list[grepl(pattern = "av_KPI.tsv",files_list)]
cat("processing ",av_KPI_file," \r\n")
av_KPI_table <- read.delim(av_KPI_file)
drt_KPI_file <- files_list[grepl(pattern = "drt_KPI.tsv",files_list)]
cat("processing ",drt_KPI_file," \r\n")
drt_KPI_table <- read.delim(drt_KPI_file)

if(!case_name %in% names(folders_av_averaged_table)){
folders_av_averaged_table[[case_name]] = av_KPI_table
if(!case_name %in% names(folders_drt_averaged_table)){
folders_drt_averaged_table[[case_name]] = drt_KPI_table
}else{
folders_av_averaged_table[[case_name]] = rbind(folders_av_averaged_table[[case_name]],av_KPI_table)
folders_drt_averaged_table[[case_name]] = rbind(folders_drt_averaged_table[[case_name]],drt_KPI_table)
}

}



tbl_colnames = c("stat",colnames(folders_av_averaged_table[[case_name]]))
tbl_colnames = c("stat",colnames(folders_drt_averaged_table[[case_name]]))
result_tibble = tbl_colnames %>% purrr::map_dfc(setNames, object = list(numeric()))
for(stat in stats){
func = get(stat)
new_row = c(stat)
for(column in colnames(folders_av_averaged_table[[case_name]])){
new_row = append(new_row,func(folders_av_averaged_table[[case_name]][[column]]))
for(column in colnames(folders_drt_averaged_table[[case_name]])){
new_row = append(new_row,func(folders_drt_averaged_table[[case_name]][[column]]))
}

result_tibble = rbind(result_tibble,new_row)
}
colnames(result_tibble) = tbl_colnames

folders_av_averaged_table[[case_name]] = result_tibble
folders_drt_averaged_table[[case_name]] = result_tibble

}

print(folders_av_averaged_table)

#####Write averaged av tables####

dir_output_name <- "results_kpi_av"
dir.create(dir_output_name)
print(folders_drt_averaged_table)

for(case_name in names(folders_av_averaged_table)){
write.table(folders_av_averaged_table[[case_name]],paste0("results_kpi_av/result_av_",case_name,".tsv"),quote = FALSE,row.names = FALSE)
#Write averaged drt tables####
for(case_name in names(folders_drt_averaged_table)){
write.table(folders_drt_averaged_table[[case_name]],paste0(path_to_data, "/kpi_summary_drt_", case_name, ".tsv"),quote = FALSE,row.names = FALSE)
}



#####Reading and averaging drt tables####



folders_drt_averaged_table = list()

#########################################
#####Reading and averaging av tables#####
folders_av_averaged_table = list()
for(case_name in names(folders_seeded)){



for(folder in folders_seeded[[case_name]]){
files_list<- list.files(paste0(folder,"/analysis-drt-service-quality"),full.names = TRUE)
drt_KPI_file <- files_list[grepl(pattern = "drt_KPI.tsv",files_list)]
cat("processing ",drt_KPI_file," \r\n")
drt_KPI_table <- read.delim(drt_KPI_file)
av_KPI_file <- files_list[grepl(pattern = "av_KPI.tsv",files_list)]
cat("processing ",av_KPI_file," \r\n")
av_KPI_table <- read.delim(av_KPI_file)

if(!case_name %in% names(folders_drt_averaged_table)){
folders_drt_averaged_table[[case_name]] = drt_KPI_table
if(!case_name %in% names(folders_av_averaged_table)){
folders_av_averaged_table[[case_name]] = av_KPI_table
}else{
folders_drt_averaged_table[[case_name]] = rbind(folders_drt_averaged_table[[case_name]],drt_KPI_table)
folders_av_averaged_table[[case_name]] = rbind(folders_av_averaged_table[[case_name]],av_KPI_table)
}

}

tbl_colnames = c("stat",colnames(folders_drt_averaged_table[[case_name]]))
tbl_colnames = c("stat",colnames(folders_av_averaged_table[[case_name]]))
result_tibble = tbl_colnames %>% purrr::map_dfc(setNames, object = list(numeric()))
for(stat in stats){
func = get(stat)
new_row = c(stat)
for(column in colnames(folders_drt_averaged_table[[case_name]])){
new_row = append(new_row,func(folders_drt_averaged_table[[case_name]][[column]]))
for(column in colnames(folders_av_averaged_table[[case_name]])){
new_row = append(new_row,func(folders_av_averaged_table[[case_name]][[column]]))
}

result_tibble = rbind(result_tibble,new_row)
}
colnames(result_tibble) = tbl_colnames

folders_drt_averaged_table[[case_name]] = result_tibble

folders_av_averaged_table[[case_name]] = result_tibble
}

print(folders_drt_averaged_table)

#####Write averaged drt tables####
print(folders_av_averaged_table)

output_dir_name <- "results_kpi_drt"
dir.create(output_dir_name)
#####Write averaged av tables####
dir_output_name <- "results_kpi_av"
dir.create(dir_output_name)

for(case_name in names(folders_drt_averaged_table)){
write.table(folders_drt_averaged_table[[case_name]],paste0(paste(path_to_data,"result_drt_", sep="/"),case_name,".tsv"),quote = FALSE,row.names = FALSE)
for(case_name in names(folders_av_averaged_table)){
write.table(folders_av_averaged_table[[case_name]],paste0(path_to_data, "/kpi_summary_av_",case_name,".tsv"),quote = FALSE,row.names = FALSE)
}



0 comments on commit 73558fb

Please sign in to comment.