Skip to content

isnan check on NamedTuples with nested vectors fails #898

@penelopeysm

Description

@penelopeysm

Following from #888, this code errors (reported by Christopher Fisher on Slack):

using SequentialSamplingModels
using Turing

@model function model(data; min_rt = minimum(data[2]))
    ν ~ MvNormal(zeros(2), I * 2)
    A ~ truncated(Normal(0.8, 0.4), 0.0, Inf)
    k ~ truncated(Normal(0.2, 0.2), 0.0, Inf)
    τ ~ Uniform(0.0, min_rt)
    data ~ LBA(; ν, A, k, τ)
end

# generate some data
dist = LBA= [3.0, 2.0], A = 0.8, k = 0.2, τ = 0.3)
data = rand(dist, 100)

# estimate parameters
chain = sample(model(data), NUTS(200, 0.65), 100)

errors because sampling from LBA returns a NamedTuple which contains a vector of parameters, and any(isnan, τ) doesn't know how to recurse.

ERROR: MethodError: no method matching isnan(::Vector{Int64})
The function `isnan` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  isnan(::Missing)
   @ Base missing.jl:101
  isnan(::BigFloat)
   @ Base mpfr.jl:1044
  isnan(::ForwardDiff.Dual)
   @ ForwardDiff ~/.julia/packages/ForwardDiff/UBbGT/src/dual.jl:389
  ...

Stacktrace:
  [1] _any(f::typeof(isnan), itr::@NamedTuple{choice::Vector{Int64}, rt::Vector{Float64}}, ::Colon)
    @ Base ./reduce.jl:1237
  [2] any(f::Function, itr::@NamedTuple{choice::Vector{Int64}, rt::Vector{Float64}})
    @ Base ./reduce.jl:1228
  [3] record_pre_tilde_observe!(context::DynamicPPL.DebugUtils.DebugContext{…}, left::@NamedTuple{…}, dist::LNR{…}, varinfo::DynamicPPL.UntypedVarInfo{…})
    @ DynamicPPL.DebugUtils ~/.julia/packages/DynamicPPL/Ka4RU/src/debug_utils.jl:294
  [4] tilde_observe(context::DynamicPPL.DebugUtils.DebugContext{…}, right::LNR{…}, left::@NamedTuple{…}, vi::DynamicPPL.UntypedVarInfo{…})
    @ DynamicPPL.DebugUtils ~/.julia/packages/DynamicPPL/Ka4RU/src/debug_utils.jl:317
  [5] tilde_observe!!(context::DynamicPPL.DebugUtils.DebugContext{…}, right::LNR{…}, left::@NamedTuple{…}, vi::DynamicPPL.UntypedVarInfo{…})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/Ka4RU/src/context_implementations.jl:194
  [6] tilde_observe!!(context::DynamicPPL.DebugUtils.DebugContext{…}, right::LNR{…}, left::@NamedTuple{…}, vname::AbstractPPL.VarName{…}, vi::DynamicPPL.UntypedVarInfo{…})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/Ka4RU/src/context_implementations.jl:176
  [7] macro expansion
    @ ~/.julia/packages/DynamicPPL/Ka4RU/src/compiler.jl:574 [inlined]
  [8] model(__model__::DynamicPPL.Model{…}, __varinfo__::DynamicPPL.UntypedVarInfo{…}, __context__::DynamicPPL.DebugUtils.DebugContext{…}, data::@NamedTuple{…}; min_rt::Float64)
    @ Main ~/.julia/dev/sandbox/turing/run_turing.jl:17
  [9] _evaluate!!(model::DynamicPPL.Model{…}, varinfo::DynamicPPL.UntypedVarInfo{…}, context::DynamicPPL.DebugUtils.DebugContext{…})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/Ka4RU/src/model.jl:936
 [10] evaluate_threadunsafe!!(model::DynamicPPL.Model{…}, varinfo::DynamicPPL.UntypedVarInfo{…}, context::DynamicPPL.DebugUtils.DebugContext{…})
    @ DynamicPPL ~/.julia/packages/DynamicPPL/Ka4RU/src/model.jl:909
 [11] check_model_and_trace(rng::TaskLocalRNG, model::DynamicPPL.Model{…}; varinfo::DynamicPPL.UntypedVarInfo{…}, context::DynamicPPL.SamplingContext{…}, error_on_failure::Bool, kwargs::@Kwargs{})
    @ DynamicPPL.DebugUtils ~/.julia/packages/DynamicPPL/Ka4RU/src/debug_utils.jl:457
 [12] check_model_and_trace(model::DynamicPPL.Model{…}; kwargs::@Kwargs{…})
    @ DynamicPPL.DebugUtils ~/.julia/packages/DynamicPPL/Ka4RU/src/debug_utils.jl:438
 [13] check_model(model::DynamicPPL.Model{…}; kwargs::@Kwargs{…})
    @ DynamicPPL.DebugUtils ~/.julia/packages/DynamicPPL/Ka4RU/src/debug_utils.jl:483
 [14] _check_model(model::DynamicPPL.Model{…})
    @ Turing.Inference ~/.julia/packages/Turing/IXycm/src/mcmc/Inference.jl:253
 [15] _check_model(model::DynamicPPL.Model{…}, alg::NUTS{…})
    @ Turing.Inference ~/.julia/packages/Turing/IXycm/src/mcmc/Inference.jl:256
 [16] sample(rng::TaskLocalRNG, model::DynamicPPL.Model{…}, alg::NUTS{…}, N::Int64; check_model::Bool, kwargs::@Kwargs{})
    @ Turing.Inference ~/.julia/packages/Turing/IXycm/src/mcmc/Inference.jl:277
 [17] sample(rng::TaskLocalRNG, model::DynamicPPL.Model{…}, alg::NUTS{…}, N::Int64)
    @ Turing.Inference ~/.julia/packages/Turing/IXycm/src/mcmc/Inference.jl:269
 [18] sample(model::DynamicPPL.Model{…}, alg::NUTS{…}, N::Int64; kwargs::@Kwargs{})
    @ Turing.Inference ~/.julia/packages/Turing/IXycm/src/mcmc/Inference.jl:266
 [19] sample(model::DynamicPPL.Model{…}, alg::NUTS{…}, N::Int64)
    @ Turing.Inference ~/.julia/packages/Turing/IXycm/src/mcmc/Inference.jl:263
 [20] top-level scope
    @ ~/.julia/dev/sandbox/turing/run_turing.jl:25
Some type information was truncated. Use `show(err)` to see complete types.

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