Environment
- treepplr 0.14.0
- tpplc 0.4
Issue
When a model produces a non-finite value (e.g. a -Inf log-weight from a weight 0.0 statement, or a NaN in a degenerate normConst), tp_run()'s parsed JSON represents it not as a plain R numeric but as a nested list:
out[[1]]$weights[[1]]
#> $`__float__`
#> [1] "-inf"
(similarly "inf" and "nan" show up elsewhere, e.g. in normConst).
Nothing downstream handles this:
- Plain
unlist() on a weights list containing a mix of ordinary numbers and one of these wrapped values either throws Error in round(w, 6) : non-numeric argument to mathematical function or silently coerces the whole vector to character, depending on what's mixed in.
tp_parse_mcmc() and tp_parse_smc() don't unwrap these either — normal numeric fields come through fine, but a field that happens to be -Inf for some draws will break naive as.numeric() conversion downstream (the string "-inf" isn't itself a problem for as.numeric(), but the fact that it arrives as a one-element named list rather than a bare value is what breaks unlist()-based flattening).
Repro
w <- list(list(`__float__` = "-inf"), -6.89, -5.28)
unlist(w)
#> Error in round(...) situations aside, this just silently produces a character vector:
#> "list(`__float__` = \"-inf\")" ... etc. depending on context -- inconsistent, fragile either way.
Suggested fix
A small exported/internal helper that recursively unwraps {"__float__": "-inf"|"inf"|"nan"} into the corresponding R -Inf/Inf/NaN, applied automatically inside tp_parse_mcmc(), tp_parse_smc(), and ideally also as a documented public utility (e.g. tp_unwrap_float()) for anyone working with the raw tp_run() output directly. This came up in normal use (a model with weight 0.0 for rejected branch samples), not an edge case.
Environment
Issue
When a model produces a non-finite value (e.g. a
-Inflog-weight from aweight 0.0statement, or aNaNin a degeneratenormConst),tp_run()'s parsed JSON represents it not as a plain R numeric but as a nested list:(similarly
"inf"and"nan"show up elsewhere, e.g. innormConst).Nothing downstream handles this:
unlist()on aweightslist containing a mix of ordinary numbers and one of these wrapped values either throwsError in round(w, 6) : non-numeric argument to mathematical functionor silently coerces the whole vector to character, depending on what's mixed in.tp_parse_mcmc()andtp_parse_smc()don't unwrap these either — normal numeric fields come through fine, but a field that happens to be-Inffor some draws will break naiveas.numeric()conversion downstream (the string"-inf"isn't itself a problem foras.numeric(), but the fact that it arrives as a one-element named list rather than a bare value is what breaksunlist()-based flattening).Repro
Suggested fix
A small exported/internal helper that recursively unwraps
{"__float__": "-inf"|"inf"|"nan"}into the corresponding R-Inf/Inf/NaN, applied automatically insidetp_parse_mcmc(),tp_parse_smc(), and ideally also as a documented public utility (e.g.tp_unwrap_float()) for anyone working with the rawtp_run()output directly. This came up in normal use (a model withweight 0.0for rejected branch samples), not an edge case.