StrukturaPlikow.R
4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#' Ż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)
}