Skip to content

Commit 2bf094c

Browse files
committed
Improve progress printing
1 parent c9b6fe8 commit 2bf094c

7 files changed

+31
-12
lines changed

NEWS.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# shar 1.1
22
* Improvements
33
* Use energy_df to get energy for printing if available
4-
+ Updated tests
4+
* Updated tests
5+
* More stable progress printing
56

67
# shar 1.0.1
78
* Improvements

R/print.rd_mar.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ print.rd_mar <- function(x,
7373

7474
mean_energy <- round(mean(vapply(x$energy_df, function(x) {
7575

76-
tail(x, n = 1)[, 2]
76+
utils::tail(x, n = 1)[, 2]
7777
}, FUN.VALUE = numeric(1))), digits = digits)
7878
}
7979

R/print.rd_pat.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ print.rd_pat <- function(x,
7777

7878
mean_energy <- round(mean(vapply(x$energy_df, function(x) {
7979

80-
tail(x, n = 1)[, 2]
80+
utils::tail(x, n = 1)[, 2]
8181
}, FUN.VALUE = numeric(1))), digits = digits)
8282
}
8383

R/reconstruct_pattern_cluster.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ reconstruct_pattern_cluster <- function(pattern,
308308
if (plot) {
309309

310310
# https://support.rstudio.com/hc/en-us/community/posts/200661917-Graph-does-not-update-until-loop-completion
311-
Sys.sleep(0.1)
311+
Sys.sleep(0.01)
312312

313313
graphics::plot(x = pcf_observed[[1]], y = pcf_observed[[3]],
314314
type = "l", col = "black",
@@ -337,8 +337,12 @@ reconstruct_pattern_cluster <- function(pattern,
337337
# print progress
338338
if (verbose) {
339339

340+
if (!plot) {
341+
Sys.sleep(0.01)
342+
}
343+
340344
message("\r> Progress: n_random: ", current_pattern, "/", n_random,
341-
" || max_runs: ", i, "/", max_runs,
345+
" || max_runs: ", floor(i / max_runs * 100), "%",
342346
" || energy = ", round(energy_current, 5), "\t\t",
343347
appendLF = FALSE)
344348
}

R/reconstruct_pattern_hetero.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ reconstruct_pattern_hetero <- function(pattern,
280280
if (plot) {
281281

282282
# https://support.rstudio.com/hc/en-us/community/posts/200661917-Graph-does-not-update-until-loop-completion
283-
Sys.sleep(0.1)
283+
Sys.sleep(0.01)
284284

285285
graphics::plot(x = pcf_observed[[1]], y = pcf_observed[[3]],
286286
type = "l", col = "black",
@@ -309,8 +309,12 @@ reconstruct_pattern_hetero <- function(pattern,
309309
# print progress
310310
if (verbose) {
311311

312+
if (!plot) {
313+
Sys.sleep(0.01)
314+
}
315+
312316
message("\r> Progress: n_random: ", current_pattern, "/", n_random,
313-
" || max_runs: ", i, "/", max_runs,
317+
" || max_runs: ", floor(i / max_runs * 100), "%",
314318
" || energy = ", round(energy_current, 5), "\t\t",
315319
appendLF = FALSE)
316320
}

R/reconstruct_pattern_homo.R

+8-3
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,14 @@ reconstruct_pattern_homo <- function(pattern,
274274
if (plot) {
275275

276276
# https://support.rstudio.com/hc/en-us/community/posts/200661917-Graph-does-not-update-until-loop-completion
277-
Sys.sleep(0.1)
277+
Sys.sleep(0.01)
278278

279279
graphics::plot(x = pcf_observed[[1]], y = pcf_observed[[3]],
280280
type = "l", col = "black",
281281
xlab = "r", ylab = "g(r)")
282282

283-
graphics::lines(x = pcf_relocated[[1]], y = pcf_relocated[[3]], col = "red")
283+
graphics::lines(x = pcf_relocated[[1]], y = pcf_relocated[[3]],
284+
col = "red")
284285

285286
graphics::legend("topright",
286287
legend = c("observed", "reconstructed"),
@@ -303,8 +304,12 @@ reconstruct_pattern_homo <- function(pattern,
303304
# print progress
304305
if (verbose) {
305306

307+
if (!plot) {
308+
Sys.sleep(0.01)
309+
}
310+
306311
message("\r> Progress: n_random: ", current_pattern, "/", n_random,
307-
" || max_runs: ", i, "/", max_runs,
312+
" || max_runs: ", floor(i / max_runs * 100), "%",
308313
" || energy = ", round(energy_current, 5), "\t\t",
309314
appendLF = FALSE)
310315
}

R/reconstruct_pattern_marks.R

+7-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ reconstruct_pattern_marks <- function(pattern,
207207
if (plot) {
208208

209209
# https://support.rstudio.com/hc/en-us/community/posts/200661917-Graph-does-not-update-until-loop-completion
210-
Sys.sleep(0.1)
210+
Sys.sleep(0.01)
211211

212212
graphics::plot(x = kmmr_observed[[1]], y = kmmr_observed[[3]],
213213
type = "l", col = "black",
@@ -235,8 +235,13 @@ reconstruct_pattern_marks <- function(pattern,
235235

236236
# print progress
237237
if (verbose) {
238+
239+
if (!plot) {
240+
Sys.sleep(0.01)
241+
}
242+
238243
message("\r> Progress: n_random: ", current_pattern, "/", n_random,
239-
" || max_runs: ", i, "/", max_runs,
244+
" || max_runs: ", floor(i / max_runs * 100), "%",
240245
" || energy = ", round(energy_current, 5), "\t\t",
241246
appendLF = FALSE)
242247
}

0 commit comments

Comments
 (0)