forked from ImperialCollegeLondon/covid19model
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreprocess-stanfit.r
69 lines (62 loc) · 2.09 KB
/
reprocess-stanfit.r
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
library(optparse)
library(data.table)
source("utils/log-and-process.r")
# Commandline options and parsing
parser <- OptionParser()
parser <- add_option(parser, c("-D", "--directory"), default=NULL,
help="Load all fits in directory X")
parser <- add_option(parser, c("-a","--all"), action="store_true",
help="Loads all fits in directory './'")
parser <- add_option(parser, c("-p","--pattern"), default="",
help="Finds files which match the specified pattern.")
cmdoptions <- parse_args(parser, args = commandArgs(trailingOnly = TRUE),
positional_arguments = TRUE)
if(!is.null(cmdoptions$options$all)) {
cmdoptions$options$directory = "./"
}
if(is.null(cmdoptions$options$pattern)) {
cmdoptions$options$pattern = ""
}
if(length(cmdoptions$args) > 0) {
stanfit_files = cmdoptions$args
if (!is.null(cmdoptions$options$directory)){
message("WARNING: Ignoring directory as files specified on command line.")
}
} else if (!is.null(cmdoptions$options$directory)
|| !is.null(cmdoptions$options$pattern)) {
if (is.null(cmdoptions$options$directory)){
cmdoptions$options$directory = './'
}
stanfit_files = list.files(
path=cmdoptions$options$directory,
pattern=paste0(".*", cmdoptions$options$pattern, ".*-stanfit.Rdata$"),
recursive = TRUE,
full.names = TRUE
)
} else {
message("WARNING: no file specified")
stanfit_files <- c()
}
print(stanfit_files)
error_log = data.frame(
"error"=rep("", length(stanfit_files)),
"file"=stanfit_files,
stringsAsFactors=F
)
i = 0
for (stanfit_file in stanfit_files) {
i = i+ 1
err_mark = tryCatch({
process_stanfit_file(stanfit_file)
}, error = function(e) {
message(paste("Processing Error: ",e))
return(paste(e))
})
if (!is.null(err_mark)){
error_log$error[i] = err_mark
}
}
message("______________________________________________")
message("________________ERROR_LOG_____________________")
message("______________________________________________")
print(error_log)