#' Żeby moje symulacje miały jakikolwiek sens, chciałbym stworzyć strukturę #' porządkująca te symulacje. Tym sposobem, nie będę zawsze musiał się zastanawiać #' we jaki sposób porzadkować pliki. #' Moim celem jest, żeby stworzyć tabelę, w której trzymane będą parametry #' oraz nazwa pliku, w którym symulacje z danymi parametrami będą przechowywane. #' #' Chodzi również o to, żeby tę tabelę można było w prosty sposób uaktualniać dodając #' nowe wiersze, jeśliby doszły nowe pliki. #' #' Tabela powinna mieć także kolumnę ze wskaźnikiem na wersję (w przypadku #' powtórzenia symulacji z tymi samymi parametrami) oraz komentarz, jeśli #' jest jakiś potrzebny. #' library(tidyverse) library(feather) # Struktura ---- struktura <- tibble( # nazwa pliku plik = character(), # nazwa pliku z zawartymi oryginalnymi przesunięciami t0 plik_t0 = character(), # wersja symulacji version = integer(), # częstotliwość transmisji danych fd = numeric(), # dt = 1 / fd dt = numeric(), # częstotliwość włączania konwerterów. fc = numeric(), # odległość pomiędzy kolejnymi włączeniami (pc = 1/fc) pc = numeric(), # duty cycle duty = numeric(), # liczba konwerterów NK = integer(), # liczba rund (powtórzeń symulacji) rounds = integer(), # liczba bitów w ramce N = integer(), # Parametry interferencji # częstotliwość interferencji f = numeric(), # amplituda A = numeric(), # parametr tłumienia lambda = numeric(), # liczba okresów pc trwania sygnału Np = integer(), # tolerancja dla trwania sygnału tol = numeric() ) # write_feather(struktura, "Results/struktura.feather") # base_folder <- "/Volumes/Macbook Pro HDD /Doktorat/SCENT/Simulations/" base_folder <- "/Users/karolniewiadomski/Documents/SCENT/Simulations/" # Funkcje do czytania plików ---- read_structure <- function() { feather::read_feather(file.path(base_folder, "Results/struktura.feather")) } write_structure <- function(base_folder, new_structure) { confirmation <- readline(prompt = "Do you really want to rewrite the current structure table? (y/n)") if(confirmation == 'y') { feather::write_feather(new_structure, file.path(base_folder, 'Results/struktura.feather')) } } #' TODO: make checks whenever possible, #' especially check for the current rows to already exist. update_structure <- function(updated) { old_structure <- feather::read_feather(file.path(base_folder, "Results/struktura.feather")) old_classes <- map(old_structure, class) new_classes <- map(updated, class) classes_equal <- all.equal(old_classes, new_classes) if (class(classes_equal) == "logical") { if (classes_equal) { new_structure <- bind_rows(old_structure, updated) feather::write_feather(new_structure, file.path(base_folder, "Results/struktura.feather")) } } else { stop("Something is missing, check classes of your file.") } } #' TODO: check if it is possible not to read everytime. get_structure_classes <- function() { x <- feather::read_feather(file.path(base_folder, "Results/struktura.feather")) map(x, class) } # Assuming that names are not mixed up. repair_classes <- function(new_structure) { removes <- list("integer" = as.integer, "character" = as.character, "numeric" = as.numeric) classes <- get_structure_classes() nms <- names(new_structure) for( i in seq(1, length(new_structure))) { new_structure[, i] <- mutate_all(new_structure[, i], removes[[classes[[nms[i]]]]]) } return(new_structure) } # Example of usage # new_structure <- list("cos", 1, 123, 1 / 123, 12, 1/234, 0.5, 4, 100, 4) # names(new_structure) <- names(read_structure()) # new_structure <- as_tibble(new_structure) # classes <- get_structure_classes() # # nms <- names(new_structure) # for( i in seq(1, length(new_structure))) { # class(new_structure[, i]) <- classes[[nms[i]]] # } # # update_structure(new_structure) create_file_name <- function(NK, fc, fd, t0 = FALSE, ext = ".rds") { fc <- format(fc, scientific = FALSE) fd <- format(fd, scientific = FALSE) if (t0) { return(stringr::str_interp("nk_$[.3d]{NK}_fc_${fc}_fd_${fd}_t0${ext}") ) } else { return(stringr::str_interp("nk_$[.3d]{NK}_fc_${fc}_fd_${fd}${ext}")) } } # it is not very quick. - could work on that a bit more. convert_file_name <- function(filename) { # Could do with str_exctract_all but this is safer. nk <- str_extract(filename, "nk_([:digit:]+)") fc <- str_extract(filename, "fc_([:digit:]+)") fd <- str_extract(filename, "fd_([:digit:]+)") all_nums <- map(c(nk, fc, fd), function(x) parse_number(str_extract(x, "([:digit:]+)")) ) names(all_nums) <- c("NK", "fc", "fd") return(all_nums) }