/
run_dir.R
142 lines (112 loc) · 2.87 KB
/
run_dir.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
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
#' Current run directory
#'
#' Returns the current training run directory. If a training run is
#' not currently active (see [is_run_active()]) then the current
#' working directory is returned.
#'
#' @return Active run direcotry (or current working directory as a fallback)
#'
#' @export
run_dir <- function() {
# do we already have a run_dir?
if (is_run_active()) {
.globals$run_dir$path
# no run_dir currently established
} else {
getwd()
}
}
#' Check for an active training run
#'
#' @return `TRUE` if a training tun is currently active
#'
#' @export
is_run_active <- function() {
!is.null(.globals$run_dir$path)
}
#' Create a unique run directory
#'
#' Create a new uniquely named run directory within the specified `runs_dir`.
#'
#' The directory name will be a timestamp (in GMT time). If a duplicate name is
#' generated then the function will wait long enough to return a unique one.
#'
#' @inheritParams ls_runs
#' @param seconds_scale Decimal scale for the seconds component of the
#' timestamp. Defaults to 0 which results in only the rounded seconds value
#' being used in the timestamp. Specify larger numbers to include a decimal
#' component (useful if you need to create many unique run directories at the
#' same time).
#'
#' @export
unique_run_dir <- function(runs_dir = getOption("tfruns.runs_dir", "runs"),
seconds_scale = 0) {
# determine seconds format
if (seconds_scale == 0)
seconds_format <- "S"
else
seconds_format <- paste0("OS", seconds_scale)
# loop while waiting to create a unique run directory
while(TRUE) {
run_dir <- file.path(
runs_dir,
paste0(strftime(
Sys.time(),
format = paste0("%Y-%m-%dT%H-%M-%", seconds_format, "Z"),
tz = "GMT")
)
)
if (!file.exists(run_dir)) {
dir.create(run_dir, recursive = TRUE)
return(run_dir)
}
# sleep for an appropriate interval before trying again
else {
sleep_for <- (1 / 10^seconds_scale) * 0.5
Sys.sleep(sleep_for)
}
}
}
#' Extract run directory from an object
#'
#' @param x Object to extract run directory from
#'
#' @return Run directory path(s)
#'
#' @keywords internal
#'
#' @export
as_run_dir <- function(x) {
UseMethod("as_run_dir")
}
#' @export
as_run_dir.character <- function(x) {
if (all(utils::file_test("-d", x)))
x
else {
runs_dir <- getOption("tfruns.runs_dir", "runs")
run_dir <- file.path(runs_dir, x)
if (all(utils::file_test("-d", run_dir)))
run_dir
else
x
}
}
#' @export
as_run_dir.tfruns_run <- function(x) {
x$run_dir
}
#' @export
as_run_dir.list <- function(x) {
if (!is.null(x$run_dir))
x$run_dir
else
stop("List does not contain a run_dir")
}
#' @export
as_run_dir.data.frame <- function(x) {
if (!is.null(x$run_dir))
x$run_dir
else
stop("Data frame does not contain a run_dir")
}