Skip to content

Inf/-Inf/NaN returned as {"__float__": ...} break unlist() and aren't handled by tp_parse_mcmc()/tp_parse_smc() #20

Description

@maribraga

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions