Mercurial > repos > siwaa > redelac_stics_s
comparison redelacSticsSimulator.R @ 7:2586f7dbcde1 draft
"planemo upload for repository https://forgemia.inra.fr/redelac/redelac-toolbox/-/tree/af2d1fcb63a2ae897e02f11e82cc76c4f6e8ca5e/tools/REDELACSticsSimulator commit af2d1fcb63a2ae897e02f11e82cc76c4f6e8ca5e-dirty"
author | siwaa |
---|---|
date | Mon, 02 Oct 2023 13:51:01 +0000 |
parents | 41f652e6616f |
children | 65c00476f417 |
comparison
equal
deleted
inserted
replaced
6:41f652e6616f | 7:2586f7dbcde1 |
---|---|
1 library(SticsRFiles) | 1 library(SticsRFiles) |
2 library(SticsOnR) | 2 library(SticsOnR) |
3 library(parallel) | 3 library(parallel) |
4 library(doParallel) | 4 library(doParallel) |
5 library(readxl) | 5 library(readxl) |
6 library(tidyr) | |
7 library(data.table) | |
8 library(readr) | |
9 library(dplyr) | |
10 library(stringr) | |
11 library(readr) | |
6 | 12 |
7 sessionInfo() | 13 sessionInfo() |
14 | |
15 args = commandArgs(trailingOnly = TRUE) | |
16 | |
17 parallel <- (args[1] == "parallel") | |
18 nbSlots <- as.integer(args[2]) | |
19 scenario <- args[3] | |
8 | 20 |
9 workspace <- paste0(getwd(), "/WS") | 21 workspace <- paste0(getwd(), "/WS") |
10 javastics_path <- getwd() | 22 javastics_path <- getwd() |
11 default_config_path <- paste0(javastics_path, "/config") | 23 default_config_path <- paste0(javastics_path, "/config") |
12 | 24 |
13 txt_path <- paste0(getwd(), "/WS/txt_files") | 25 txt_path <- paste0(getwd(), "/WS/txt_files") |
14 | 26 |
15 # List of successive USMs | 27 # List of successive USMs |
16 successive_usms_file <- ("successionPlan.csv") | 28 successive_usms_file <- ("successionPlan.csv") |
17 successive_usms_tab <- read.csv(file = file.path(workspace, successive_usms_file), sep =";") | 29 successive_usms_tab <- |
18 | 30 read.csv(file = file.path(workspace, successive_usms_file), |
19 args = commandArgs(trailingOnly = TRUE) | 31 sep = ";") |
20 | 32 |
21 parallel <- (args[1] == "parallel") | 33 # from succesions to simulate retrieving upcSol Cases |
22 nbSlots <- as.integer(args[2]) | 34 |
35 successRef <- successive_usms_tab["Treatment"] | |
36 | |
37 successRef <- successRef %>% | |
38 separate( | |
39 col = Treatment, | |
40 into = c("upc", "sol", "systeme", "rotation", "tete_rotation", "horizon"), | |
41 sep = "-", | |
42 remove = FALSE | |
43 ) | |
44 | |
45 successRef$upcSol <- paste0(successRef$upc, "-", successRef$sol) | |
46 | |
47 casesUpcSol <- unique(successRef$upcSol) | |
48 | |
49 systeme <- successRef$systeme[1] | |
50 rotation <- successRef$rotation[1] | |
23 | 51 |
24 if (parallel) { | 52 if (parallel) { |
25 cl <- makeCluster(nbSlots) | 53 cl <- makeCluster(nbSlots) |
54 print("Inside R, info on the cluster:") | |
26 print(cl) | 55 print(cl) |
27 registerDoParallel(cl) | 56 registerDoParallel(cl) |
28 `%dordopar%` <- `%dopar%` | 57 `%dordopar%` <- `%dopar%` |
29 } else { | 58 } else { |
30 `%dordopar%` <- `%do%` | 59 `%dordopar%` <- `%do%` |
31 } | 60 } |
32 | 61 |
33 result <- foreach(i=1:nrow(successive_usms_tab), | 62 successive_usms_tab_split <- list() |
34 .packages = c("foreach", "SticsRFiles", "SticsOnR") ) %dordopar% { | 63 |
35 | 64 for (j in 1:length(casesUpcSol)) { |
36 nusms <- as.numeric(successive_usms_tab[i,2]) | 65 splited <- strsplit(casesUpcSol[j], split = "-")[[1]] |
37 | 66 |
38 vec <- as.character(successive_usms_tab[i,c(3:(2+nusms))]) | 67 upc <- splited[1] |
39 | 68 |
40 list_successive_usms <- list(vec) | 69 sol <- splited[2] |
41 | 70 |
42 sim_options <- stics_wrapper_options(javastics = javastics_path, | 71 successive_usms_tab_split[[j]] <- |
43 stics_exe = paste0(javastics_path,"/bin/stics_modulo"), | 72 successive_usms_tab[successRef$upcSol == casesUpcSol[j],] |
44 workspace = txt_path, | |
45 verbose = TRUE, | |
46 successive = list_successive_usms) | |
47 list_usms <- vec | |
48 | |
49 return(stics_wrapper(model_options = sim_options, situation = list_usms)) | |
50 | |
51 } | 73 } |
52 | 74 |
53 if (parallel) stopCluster(cl) | 75 result <- foreach(j = 1:length(casesUpcSol)) %:% |
54 | 76 foreach( |
55 for (i in 1:nrow(successive_usms_tab)) { #i=1 | 77 i = 1:nrow(successive_usms_tab_split[[j]]), |
56 if (i == 1) { | 78 .packages = c("foreach", "SticsRFiles", "SticsOnR") |
57 simPCP <- result[[i]] | 79 ) %dordopar% { |
58 } else { | 80 if (parallel) |
59 simPCP$sim_list <- c(simPCP$sim_list, result[[i]]$sim_list) | 81 sink("./logPaRedelac.txt", append = TRUE) |
82 | |
83 nusms <- as.numeric(successive_usms_tab_split[[j]][i, 2]) | |
84 | |
85 vec <- | |
86 as.character(successive_usms_tab_split[[j]][i, c(3:(2 + nusms))]) | |
87 | |
88 list_successive_usms <- list(vec) | |
89 | |
90 sim_options <- stics_wrapper_options( | |
91 javastics = javastics_path, | |
92 stics_exe = paste0(javastics_path, "/bin/stics_modulo"), | |
93 workspace = txt_path, | |
94 verbose = TRUE, | |
95 successive = list_successive_usms | |
96 ) | |
97 | |
98 list_usms <- vec | |
99 | |
100 cat(paste0("-->", successive_usms_tab_split[[j]][i, 1], "launched\n")) | |
101 | |
102 return(stics_wrapper(model_options = sim_options, situation = list_usms)) | |
103 | |
60 } | 104 } |
61 | 105 |
106 if (parallel) { | |
107 stopCluster(cl) | |
108 sink() | |
109 cat(read_file("./logPaRedelac.txt")) | |
62 } | 110 } |
63 | 111 |
64 save(simPCP, file = "results.RData") | 112 for (j in 1:length(casesUpcSol)) { |
113 splited <- strsplit(casesUpcSol[j], split = "-")[[1]] | |
114 | |
115 upc <- splited[1] | |
116 | |
117 sol <- splited[2] | |
118 | |
119 for (i in 1:nrow(successive_usms_tab_split[[j]])) { | |
120 #i=1 | |
121 if (i == 1) { | |
122 simPCP <- result[[j]][[i]] | |
123 } else { | |
124 simPCP$sim_list <- c(simPCP$sim_list, result[[j]][[i]]$sim_list) | |
125 } | |
126 | |
127 } | |
128 | |
129 #agregation des sorties dans un dataframe | |
130 sim_df <- | |
131 rbindlist(simPCP$sim_list, use.names = TRUE, idcol = "usm") | |
132 | |
133 #ajout des colonnes explicatives + de l'horizon temporel | |
134 sim_df <- sim_df %>% | |
135 separate( | |
136 col = usm, | |
137 into = c( | |
138 "upc", | |
139 "sol", | |
140 "systeme", | |
141 "rotation", | |
142 "tete_rotation", | |
143 "horizon", | |
144 "annee", | |
145 "culture" | |
146 ), | |
147 sep = "-", | |
148 remove = FALSE | |
149 ) | |
150 | |
151 #sauvegarde sous la forme d'un fichier rds | |
152 | |
153 if (dir.exists(paste0( | |
154 workspace, | |
155 "/analyse_simulations_STICS/", | |
156 systeme, | |
157 "/", | |
158 rotation | |
159 )) == FALSE) { | |
160 dir.create( | |
161 paste0( | |
162 workspace, | |
163 "/analyse_simulations_STICS/", | |
164 systeme, | |
165 "/", | |
166 rotation | |
167 ), | |
168 recursive = TRUE | |
169 ) | |
170 } | |
171 | |
172 saveRDS( | |
173 sim_df, | |
174 paste0( | |
175 workspace, | |
176 "/analyse_simulations_STICS/", | |
177 systeme, | |
178 "/", | |
179 rotation, | |
180 "/outputs-", | |
181 rotation, | |
182 "-", | |
183 upc, | |
184 "-", | |
185 sol, | |
186 "-", | |
187 scenario, | |
188 ".rds" | |
189 ) | |
190 ) | |
191 | |
192 successive_usms_tab_split[[j]]$Treatment <- NULL | |
193 successive_usms_tab_split[[j]]$Number_USM <- NULL | |
194 | |
195 list_usms <- | |
196 unlist(unname(as.list( | |
197 transpose(successive_usms_tab_split[[j]]) | |
198 ))) | |
199 list_usms <- list_usms[!is.na(list_usms)] | |
200 list_usms <- list_usms[list_usms != ''] | |
201 | |
202 list_usms_yr1 <- successive_usms_tab_split[[j]]$USM_1 | |
203 | |
204 for (i in 1:length(list_usms)) { | |
205 if (i == 1) { | |
206 #ajout d'une condition sur l'existence du fichier "mod_rapport.sti" pour la concatenation | |
207 #en attendant que toutes les simulations tournent correctement | |
208 #if(exists(file.path(txt_path, list_usms[i], "mod_rapport.sti"))==TRUE){ | |
209 rap_1 <- | |
210 read_delim(file.path(txt_path, list_usms[i], "mod_rapport.sti"), | |
211 col_names = TRUE) | |
212 names_col <- colnames(rap_1) | |
213 | |
214 rap_string <- | |
215 read_lines(file = file.path(txt_path, list_usms[i], "mod_rapport.sti"), | |
216 skip = 1) | |
217 | |
218 rap <- as_tibble(rap_string) %>% | |
219 separate(col = value, | |
220 sep = ";", | |
221 into = names_col) %>% | |
222 mutate(USM = list_usms[i]) | |
223 # } | |
224 | |
225 } else { | |
226 #if (list_usms[i] %in% list_usms_yr1) { | |
227 #ajout d'une condition sur l'existence du fichier "mod_rapport.sti" pour la concatenation | |
228 #en attendant que toutes les simulations tournent correctement | |
229 #if(exists(file.path(txt_path, list_usms[i], "mod_rapport.sti"))==TRUE){ | |
230 rap_string <- | |
231 read_lines(file = file.path(txt_path, list_usms[i], "mod_rapport.sti"), | |
232 skip = 1,)#} | |
233 | |
234 # } else { | |
235 #ajout d'une condition sur l'existence du fichier "mod_rapport.sti" pour la concatenation | |
236 #en attendant que toutes les simulations tournent correctement | |
237 #if(exists(file.path(txt_path, list_usms[i], "mod_rapport.sti"))==TRUE){ | |
238 # rap_string <- | |
239 # read_file(file = file.path(txt_path, list_usms[i], "mod_rapport.sti"))#} | |
240 # } | |
241 rap_i <- as_tibble(rap_string) %>% | |
242 separate(col = value, | |
243 sep = ";", | |
244 into = names_col) %>% | |
245 mutate(USM = list_usms[i]) | |
246 | |
247 rap <- bind_rows(rap, rap_i) | |
248 | |
249 } | |
250 | |
251 } | |
252 rm(rap_1, names_col, rap_string, rap_i) | |
253 | |
254 rap <- rap %>% | |
255 select(USM, everything()) %>% | |
256 mutate(P_usm = str_replace_all(P_usm, " ", "")) %>% | |
257 mutate(wlieu = str_replace_all(wlieu, " ", "")) %>% | |
258 mutate(stade = str_replace_all(stade, " ", "")) %>% | |
259 mutate(nomversion = str_replace_all(nomversion, " ", "")) %>% | |
260 mutate(across(3:9, as.numeric)) %>% | |
261 mutate(across(13:ncol(rap), as.numeric)) %>% | |
262 #separate(col=P_usm,into=c("upc","sol","systeme","rotation","tete_rotation","horizon","annee","culture"),sep="-",remove = FALSE) | |
263 separate( | |
264 col = USM, | |
265 into = c( | |
266 "upc", | |
267 "sol", | |
268 "systeme", | |
269 "rotation", | |
270 "tete_rotation", | |
271 "horizon", | |
272 "annee", | |
273 "culture" | |
274 ), | |
275 sep = "-", | |
276 remove = FALSE | |
277 ) | |
278 | |
279 write_delim( | |
280 x = rap, | |
281 file = paste0( | |
282 workspace, | |
283 "/analyse_simulations_STICS/", | |
284 systeme, | |
285 "/", | |
286 rotation, | |
287 "/report-", | |
288 rotation, | |
289 "-", | |
290 upc, | |
291 "-", | |
292 sol, | |
293 "-", | |
294 scenario, | |
295 ".csv" | |
296 ), | |
297 delim = ";" | |
298 ) | |
299 | |
300 } |