From 798c539162093d45752ed8e2ec1efa649a9315f0 Mon Sep 17 00:00:00 2001 From: Chengqi Lu <43133404+luchengqi7@users.noreply.github.com> Date: Fri, 22 Sep 2023 11:44:49 +0200 Subject: [PATCH] Update kpi_averaging.R (#63) * Update kpi_averaging.R * Update kpi_averaging.R Some reformating and minor update * Update kpi_averaging.R --- src/main/R/drtAnalysis/kpi_averaging.R | 113 +++++++++++++------------ 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/src/main/R/drtAnalysis/kpi_averaging.R b/src/main/R/drtAnalysis/kpi_averaging.R index e807bae4..f4997782 100644 --- a/src/main/R/drtAnalysis/kpi_averaging.R +++ b/src/main/R/drtAnalysis/kpi_averaging.R @@ -1,17 +1,19 @@ #####libraries#### library(stringr) +library(tidyverse) #####global variables#### -path_to_data <- "runs" - +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) @@ -22,83 +24,88 @@ 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]] = 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_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_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]] = folders_av_averaged_table[[case_name]]/length(folders_seeded[[case_name]]) - + 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]] = 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) } - } - folders_drt_averaged_table[[case_name]] = folders_drt_averaged_table[[case_name]]/length(folders_seeded[[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_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_av_averaged_table[[case_name]] = result_tibble } -print(folders_drt_averaged_table) +print(folders_av_averaged_table) + +#Write averaged av tables#### +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) +} -#####Write averaged drt tables#### -output_dir_name <- "results_kpi_drt" -dir.create(output_dir_name) -for(case_name in names(folders_drt_averaged_table)){ - write.table(folders_drt_averaged_table[[case_name]],paste0("results_kpi_drt/result_drt_",case_name,".tsv"),quote = FALSE,row.names = FALSE) -} \ No newline at end of file