Skip to content

Commit a4069e0

Browse files
options enhancements and docs
1 parent 0a16d3d commit a4069e0

9 files changed

Lines changed: 210 additions & 38 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: macpan2
22
Title: Fast and Flexible Compartmental Modelling
3-
Version: 3.3.0
3+
Version: 3.3.1
44
Authors@R: c(
55
person("Steve Walker", email="swalk@mcmaster.ca", role=c("cre", "aut")),
66
person("Weiguang Guan", role="aut"),

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ S3method(mp_optimized_spec,TMBCalibrator)
8686
S3method(mp_optimizer_output,TMBCalibrator)
8787
S3method(mp_parameterization,TMBCalibrator)
8888
S3method(mp_parameterization,TMBSimulator)
89+
S3method(mp_print_obj_fn,TMBCalibrator)
90+
S3method(mp_print_obj_fn,TMBSimulator)
8991
S3method(mp_reduce,TMBModelSpec)
9092
S3method(mp_reference,Index)
9193
S3method(mp_reference,Ledger)
@@ -297,6 +299,7 @@ export(mp_positions)
297299
export(mp_print_after)
298300
export(mp_print_before)
299301
export(mp_print_during)
302+
export(mp_print_obj_fn)
300303
export(mp_print_spec)
301304
export(mp_rbf)
302305
export(mp_read_rds)

R/lists.R

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,36 @@ melt_matrix = function(x, zeros_are_blank = TRUE) {
8787
data.frame(row = row, col = col, value = as.vector(x))
8888
}
8989

90-
melt_default_matrix_list = function(x, zeros_are_blank = TRUE, simplify_as_scalars = FALSE) {
90+
melt_default_matrix_list = function(x
91+
, zeros_are_blank = TRUE
92+
, suppress_collapse_for_scalars = FALSE
93+
, force_collapse_for_scalars = FALSE
94+
) {
95+
if (suppress_collapse_for_scalars & force_collapse_for_scalars) stop("Developer error")
9196
if (length(x) == 0L) return(empty_frame("matrix", "row", "col", "value"))
9297
f = (x
9398
|> lapply(melt_matrix, zeros_are_blank)
9499
|> bind_rows(.id = "matrix")
95100
)
96-
if (simplify_as_scalars) f = rm_no_info_traj_cols(f)
101+
102+
## sorry ---
103+
if (force_collapse_for_scalars) f = rm_no_info_default_cols(f, collapse_default = TRUE)
104+
if (!suppress_collapse_for_scalars) f = rm_no_info_default_cols(f)
97105

98106
rownames(f) = NULL
99107
f
100108
}
101109

102-
rm_no_info_traj_cols = function(x
110+
111+
rm_no_info_default_cols = function(x
103112
, matrix_col_name = "quantity"
113+
, collapse_default = getOption("macpan2_collapse_default")
114+
) {
115+
rm_no_info_traj_cols(x, matrix_col_name, collapse_default)
116+
}
117+
118+
rm_no_info_traj_cols = function(x
119+
, matrix_col_name = "variable"
104120
, collapse_traj = getOption("macpan2_collapse_traj")
105121
) {
106122

R/mp_tmb_calibrator.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ print.TMBCalibrator = function(x, ...) {
261261
cat("---------------------\n")
262262
msg("Objective function:\n") |> cat()
263263
cat("---------------------\n")
264-
cat(deparse1(x$simulator$tmb_model$obj_fn$formula_list()[[1L]], width.cutoff = 500L))
265-
cat("\n")
264+
mp_print_obj_fn(x)
266265
hist = x$simulator$optimization_history
267266
if (hist$opt_attempted()) {
268267
cat("\n---------------------\n")
@@ -1517,7 +1516,10 @@ TMBPar.ParArg = function(par
15171516

15181517
self$random_frame = function() {
15191518
pf = (self$spec$default[self$par_ranef]
1520-
|> melt_default_matrix_list(FALSE)
1519+
|> melt_default_matrix_list(
1520+
zeros_are_blank = FALSE
1521+
, suppress_collapse_for_scalars = TRUE
1522+
)
15211523
|> rename_synonyms(mat = "matrix", default = "value")
15221524
)
15231525
bind_rows(pf, self$tv$tv_random_frame())
@@ -1597,7 +1599,10 @@ TMBPar.character = function(par
15971599
}
15981600
self$params_frame = function() {
15991601
pf = (self$spec$default[self$par]
1600-
|> melt_default_matrix_list(FALSE)
1602+
|> melt_default_matrix_list(
1603+
zeros_are_blank = FALSE
1604+
, suppress_collapse_for_scalars = TRUE
1605+
)
16011606
|> rename_synonyms(mat = "matrix", default = "value")
16021607
)
16031608
bind_rows(pf

R/mp_tmb_model_spec.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ defaults_printer = function(x) {
405405
cat("---------------------\n")
406406
msg("Default values:\n") |> cat()
407407
print(
408-
melt_default_matrix_list(x$default, simplify_as_scalars = TRUE)
408+
melt_default_matrix_list(x$default, force_collapse_for_scalars = TRUE)
409409
, row.names = FALSE
410410
)
411411
cat("---------------------\n")

R/tmb_model.R

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ mp_default_list = function(model, include_all = FALSE) {
288288

289289
#' @export
290290
mp_default.TMBModelSpec = function(model, include_all = FALSE) {
291-
melt_default_matrix_list(mp_default_list(model, include_all))
291+
lst = mp_default_list(model, include_all)
292+
melt_default_matrix_list(lst)
292293
}
293294

294295
#' @export
@@ -303,7 +304,8 @@ mp_default_list.TMBModelSpec = function(model, include_all = FALSE) {
303304

304305
#' @export
305306
mp_default.TMBSimulator = function(model, include_all = FALSE) {
306-
melt_default_matrix_list(mp_default_list(model, include_all))
307+
lst = mp_default_list(model, include_all)
308+
melt_default_matrix_list(lst)
307309
}
308310

309311
#' @export
@@ -458,6 +460,33 @@ mp_final_list.TMBSimulator = function(model) {
458460
mp_final(model) |> cast_default_matrix_list()
459461
}
460462

463+
464+
#' Print Objective Function
465+
#'
466+
#' @param model Model object with an objective function, probably a
467+
#' calibrator produced using \code{\link{mp_tmb_calibrator}}.
468+
#'
469+
#' @return Called to print the objective function for humans to read.
470+
#' Invisibly returns the underlying objective function object that is
471+
#' of limited utility.
472+
#'
473+
#' @export
474+
mp_print_obj_fn = function(model) UseMethod("mp_print_obj_fn")
475+
476+
#' @export
477+
mp_print_obj_fn.TMBSimulator = function(model) {
478+
obj_fn = model$tmb_model$obj_fn
479+
obj_formula = obj_fn$formula_list()[[1L]]
480+
obj_str = deparse1(obj_formula, width.cutoff = 500L)
481+
cat(obj_str)
482+
cat("\n")
483+
invisible(obj_fn)
484+
}
485+
486+
#' @export
487+
mp_print_obj_fn.TMBCalibrator = function(model) mp_print_obj_fn(model$simulator)
488+
489+
461490
#' Simulate Dynamical Model Trajectories
462491
#'
463492
#' Return simulations of the trajectory of the output
@@ -550,7 +579,7 @@ mp_trajectory.TMBSimulator = function(model, include_initial = FALSE) {
550579
if (length(macro) > 1L) macro = macro[[1L]]
551580
if (length(macro) < 1L) macro = "simulate"
552581
traj = model[[macro]](.phases = phases) |> reset_rownames()
553-
rm_no_info_traj_cols(traj, "matrix")
582+
rm_no_info_traj_cols(traj)
554583
}
555584

556585
#' @export
@@ -599,7 +628,7 @@ trajectory_par_util = function(simulator
599628
phases = trajectory_phases_util(include_initial, include_final)
600629
vector = trajectory_vec_util(simulator, parameter_updates, value_column_name)
601630
traj = simulator$simulate(vector, .phases = phases)
602-
rm_no_info_traj_cols(traj, "matrix")
631+
rm_no_info_traj_cols(traj)
603632
}
604633

605634
trajectory_rep_util = function(n, simulator
@@ -766,7 +795,7 @@ mp_trajectory_sd.TMBSimulator = function(model
766795
vars = intersect(c("value", "conf.low", "conf.high"), names(r))
767796
r = backtrans(r, vars, "matrix", "sd", "value")
768797
}
769-
rm_no_info_traj_cols(r, "matrix")
798+
rm_no_info_traj_cols(r)
770799
}
771800

772801
#' @export
@@ -782,7 +811,7 @@ mp_trajectory_sd.TMBCalibrator = function(model
782811
, back_transform
783812
)
784813
traj$time = model$time_steps_obj$internal_to_external(traj$time)
785-
rm_no_info_traj_cols(traj, "matrix")
814+
rm_no_info_traj_cols(traj)
786815
}
787816

788817
#' @export
@@ -792,7 +821,7 @@ mp_trajectory_ensemble.TMBSimulator = function(model
792821
) {
793822
best_pars = get_last_best_par(model$ad_fun())
794823
traj = model$report_ensemble(best_pars, .n = n, .probs = probs)
795-
rm_no_info_traj_cols(traj, "matrix")
824+
rm_no_info_traj_cols(traj)
796825
}
797826

798827
#' @export
@@ -878,7 +907,7 @@ mp_trajectory_sim.TMBSimulator = function(model
878907
, n
879908
, probs = c(0.025, 0.25, 0.5, 0.75, 0.975)
880909
) {
881-
r = model$simulate() |> rm_no_info_traj_cols("matrix")
910+
r = model$simulate() |> rm_no_info_traj_cols()
882911
r = r[, names(r) != "value", drop = FALSE]
883912
rr = (n
884913
|> replicate(model$simulate_values())

R/zzz.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
mp_future = function() {
2+
options(
3+
macpan2_collapse_traj = TRUE
4+
, macpan2_collapse_coef = TRUE
5+
, macpan2_collapse_default = TRUE
6+
)
7+
}
8+
19
#' @importFrom tools R_user_dir
210
.onLoad <- function(lib, pkg) {
311

@@ -12,6 +20,7 @@
1220
, macpan2_verbose = FALSE
1321
, macpan2_collapse_traj = FALSE
1422
, macpan2_collapse_coef = FALSE
23+
, macpan2_collapse_default = FALSE
1524
, macpan2_default_loss = c("clamped_poisson", "poisson", "sum_of_squares", "neg_bin")
1625
, macpan2_tmb_type = NULL
1726
, macpan2_tmb_check = TRUE

0 commit comments

Comments
 (0)