@@ -63,16 +63,16 @@ metab_bayes <- function(
6363 # Check data for correct column names & units
6464 dat_list <- mm_validate_data(if (missing(data )) NULL else data , if (missing(data_daily )) NULL else data_daily , " metab_bayes" )
6565 num_discharge_cols <- length(grep(' discharge' , c(names(dat_list $ data ), names(dat_list $ data_daily ))))
66- pool_K600 <- mm_parse_name(specs $ model_name ) $ pool_K600
67- if (xor(num_discharge_cols > 0 , pool_K600 %in% c(' linear' ,' binned' )))
68- stop(' discharge data should be included if & only if pool_K600 indicates hierarchy' )
66+ pool_K600_type <- mm_parse_name(specs $ model_name , expand = TRUE ) $ pool_K600_type
67+ if (xor(num_discharge_cols > 0 , pool_K600_type %in% c(' linear' ,' binned' )))
68+ stop(' discharge data should be included if & only if pool_K600_type indicates hierarchy' )
6969 if (num_discharge_cols > 1 )
7070 stop(' either discharge or discharge.daily may be specified, but not both' )
7171
7272 # Handle discharge. If K600 is a hierarchical function of discharge and
7373 # data$discharge was given, compute daily discharge and store in data.daily
7474 # where it'll be accessible for the user to inspect it after model fitting
75- if ((pool_K600 %in% c(' linear' ,' binned' )) && (' discharge' %in% names(dat_list $ data ))) {
75+ if ((pool_K600_type %in% c(' linear' ,' binned' )) && (' discharge' %in% names(dat_list $ data ))) {
7676 # calculate daily discharge
7777 dailymean <- function (data_ply , data_daily_ply , day_start , day_end , ply_date , ply_validity , timestep_days , ... ) {
7878 data.frame (discharge.daily = if (isTRUE(ply_validity [1 ])) mean(data_ply $ discharge ) else NA )
@@ -111,7 +111,7 @@ metab_bayes <- function(
111111 dat_list $ data_daily $ lnQ.daily <- log(v(dat_list $ data_daily $ discharge.daily ))
112112 }
113113 # If we need discharge bins, compute & store those now, as well
114- if (pool_K600 %in% c(' binned' )) {
114+ if (pool_K600_type %in% c(' binned' )) {
115115 # linear interpolation from node to node, horizontal at the edges
116116 bounds <- c(- Inf , specs $ K600_lnQ_nodes_centers , Inf )
117117 cuts <- cut(dat_list $ data_daily $ lnQ.daily , breaks = bounds , ordered_result = TRUE )
@@ -176,6 +176,7 @@ metab_bayes <- function(
176176 {if (! is.null(. )) setNames(. , ' Compilation' ) else . }
177177 log <- extract_object_list(' log' ) %> % { setNames(. , paste0(' MCMC_' , names(. ))) }
178178 bayes_log <- c(compile_log , log )
179+ bayes_compile_time <- bayes_all_list [[' compile_time' ]]
179180 bayes_mcmc <- extract_object_list(' mcmcfit' )
180181 bayes_mcmc_data <- extract_object_list(' mcmc_data' )
181182 bayes_all <- list (daily = bayes_daily )
@@ -195,10 +196,11 @@ metab_bayes <- function(
195196 . <- ' .dplyr.var'
196197 bayes_log <- bayes_all_list [c(' compile_log' , ' log' )] %> %
197198 setNames(c(' Compilation' ,' MCMC_All_Days' )) %> % { . [! sapply(. , is.null )] }
199+ bayes_compile_time <- bayes_all_list [[' compile_time' ]]
198200 bayes_mcmc <- bayes_all_list $ mcmcfit
199201 bayes_mcmc_data <- bayes_all_list $ mcmc_data
200202 # now a list of dfs, log, warnings, and errors
201- bayes_all <- bayes_all_list [! (names(bayes_all_list ) %in% c(' compile_log' ,' log' ,' mcmcfit' ,' mcmc_data' ))]
203+ bayes_all <- bayes_all_list [! (names(bayes_all_list ) %in% c(' compile_log' ,' compile_time ' , ' log' ,' mcmcfit' ,' mcmc_data' ))]
202204 }
203205 })
204206
@@ -210,7 +212,8 @@ metab_bayes <- function(
210212 log = bayes_log ,
211213 mcmc = bayes_mcmc ,
212214 mcmc_data = bayes_mcmc_data ,
213- fitting_time = fitting_time ,
215+ fitting_time = fitting_time - bayes_compile_time ,
216+ compile_time = bayes_compile_time ,
214217 specs = specs ,
215218 data = dat_list $ data , # keep the units if given
216219 data_daily = dat_list $ data_daily )
@@ -483,7 +486,7 @@ prepdata_bayes <- function(
483486 if (n24 > num_daily_obs ) stop(" day_end - day_start < 24 hours; aborting because daily metabolism could be wrong" )
484487
485488 # parse model name into features for deciding what data to include
486- features <- mm_parse_name(model_name )
489+ features <- mm_parse_name(model_name , expand = TRUE )
487490
488491 # Format the data for Stan. Stan disallows period-separated names, so
489492 # change all the input data to underscore-separated. parameters given in
@@ -501,7 +504,7 @@ prepdata_bayes <- function(
501504 ),
502505
503506 switch (
504- features $ pool_K600 ,
507+ features $ pool_K600_type ,
505508 linear = list (lnQ_daily = data_daily $ lnQ.daily ),
506509 binned = list (
507510 b = length(specs $ K600_lnQ_nodes_centers ),
@@ -533,7 +536,7 @@ prepdata_bayes <- function(
533536
534537 specs [specs $ params_in ]
535538 )
536- if (features $ pool_K600 == ' binned' ) {
539+ if (features $ pool_K600_type == ' binned' ) {
537540 data_list $ K600_lnQ_nodes_meanlog <- array (data_list $ K600_lnQ_nodes_meanlog , dim = data_list $ b )
538541 data_list $ K600_lnQ_nodes_sdlog <- array (data_list $ K600_lnQ_nodes_sdlog , dim = data_list $ b )
539542 }
@@ -604,12 +607,15 @@ runstan_bayes <- function(data_list, model_path, params_out, split_dates, keep_m
604607
605608 # use auto_write=TRUE to recompile if needed, or load from existing .rds file
606609 # without recompiling if possible
610+ compile_time <- system.time({})
607611 mobj_path <- gsub(' .stan$' , ' .stanrds' , model_path )
608612 if (! file.exists(mobj_path ) || file.info(mobj_path )$ mtime < file.info(model_path )$ mtime ) {
609613 if (verbose ) message(" compiling Stan model" )
610- compile_log <- capture.output({
611- stan_mobj <- rstan :: stan_model(file = model_path , auto_write = TRUE )
612- }, type = c(' output' ), split = verbose )
614+ compile_time <- system.time({
615+ compile_log <- capture.output({
616+ stan_mobj <- rstan :: stan_model(file = model_path , auto_write = TRUE )
617+ }, type = c(' output' ), split = verbose )
618+ })
613619 rm(stan_mobj )
614620 gc() # this humble line saves us from many horrible R crashes
615621 autowrite_path <- gsub(' .stan$' , ' .rds' , model_path )
@@ -681,7 +687,10 @@ runstan_bayes <- function(data_list, model_path, params_out, split_dates, keep_m
681687 newlogfiles <- normalizePath(file.path(tempdir(), grep(" _StanProgress.txt" , dir(tempdir()), value = TRUE )))
682688 logfile <- setdiff(newlogfiles , oldlogfiles )
683689 log <- if (length(logfile ) > 0 ) readLines(logfile ) else consolelog
684- stan_out <- c(stan_out , c(list (log = log ), if (exists(' compile_log' )) list (compile_log = compile_log )))
690+ stan_out <- c(stan_out , c(
691+ list (log = log ),
692+ if (exists(' compile_log' )) list (compile_log = compile_log ),
693+ list (compile_time = compile_time )))
685694
686695 return (stan_out )
687696}
@@ -770,7 +779,7 @@ format_mcmc_mat_nosplit <- function(mcmc_mat, data_list_d, data_list_n, keep_mcm
770779setClass(
771780 " metab_bayes" ,
772781 contains = " metab_model" ,
773- slots = c(log = " ANY" , mcmc = " ANY" , mcmc_data = " ANY" )
782+ slots = c(log = " ANY" , mcmc = " ANY" , mcmc_data = " ANY" , compile_time = " ANY " )
774783)
775784
776785# ' Extract any MCMC model objects that were stored with the model
@@ -957,6 +966,7 @@ get_params.metab_bayes <- function(metab_model, date_start=NA, date_end=NA, unce
957966 }
958967 names(metab_model @ fit $ daily ) <- gsub(' _mean$' , ' ' , names(metab_model @ fit $ daily ))
959968 names(metab_model @ fit $ daily ) <- gsub(' _sd$' , ' .sd' , names(metab_model @ fit $ daily ))
969+ names(metab_model @ fit $ daily ) <- gsub(' _50pct$' , ' .median' , names(metab_model @ fit $ daily ))
960970 names(metab_model @ fit $ daily ) <- gsub(' _2.5pct$' , ' .lower' , names(metab_model @ fit $ daily ))
961971 names(metab_model @ fit $ daily ) <- gsub(' _97.5pct$' , ' .upper' , names(metab_model @ fit $ daily ))
962972 # code duplicated in get_params.metab_Kmodel:
@@ -970,6 +980,6 @@ get_params.metab_bayes <- function(metab_model, date_start=NA, date_end=NA, unce
970980 dmsg <- metab_model @ fit $ daily $ errors
971981 metab_model @ fit $ daily $ errors <- ifelse(dmsg == ' ' , omsg , paste(omsg , dmsg , sep = ' ;' ))
972982 }
973- metab_model @ fit <- metab_model @ fit $ daily # SUPER- TEMPORARY we're still converting fit$daily to fit until #247, #229
983+ metab_model @ fit <- metab_model @ fit $ daily # TEMPORARY we're still converting fit$daily to fit until #247, #229
974984 NextMethod()
975985}
0 commit comments