From 714543fb6a422a41de5466e1050951274ee8cbca Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Fri, 20 Nov 2026 17:53:12 +0530 Subject: [PATCH] Allow nonnumeric observations in model checks --- HISTORY.md | 2 ++ src/debug_utils.jl | 2 +- test/debug_utils.jl | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 80f6e9d65d..fa0015ed98 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,7 @@ # 0.43.0 (unreleased) + - `check_model` no longer throws an `isnan` MethodError for nonnumeric observations such as symbols. + `VarNamedTuple` records indexed random variables in same-type tuple templates ([#1539](https://github.com/TuringLang/DynamicPPL.jl/pull/1539)). `fix` and `condition` now apply same-type tuple values to indexed variables: for `x = (0.0, 0.0); x[1] ~ Normal()`, `fix(model; x=(1.0, 2.0))` previously had no effect and now fixes `x[1]`. Mixed-type tuple values, such as `x=(1.0, 2)`, throw an `ArgumentError`, as does indexing into one stored in a `VarNamedTuple` ([#1539](https://github.com/TuringLang/DynamicPPL.jl/pull/1539)). diff --git a/src/debug_utils.jl b/src/debug_utils.jl index fa0d36375d..7b6cf530d2 100644 --- a/src/debug_utils.jl +++ b/src/debug_utils.jl @@ -71,7 +71,7 @@ Check if `x` is `NaN`, or contains any `NaN` values. """ _has_nans(x::NamedTuple) = any(_has_nans, x) _has_nans(x::AbstractArray) = any(_has_nans, x) -_has_nans(x) = isnan(x) +_has_nans(x) = applicable(isnan, x) && isnan(x) _has_nans(::Missing) = false function DynamicPPL.accumulate_assume!!( diff --git a/test/debug_utils.jl b/test/debug_utils.jl index 551cf0323f..5f944e9f30 100644 --- a/test/debug_utils.jl +++ b/test/debug_utils.jl @@ -8,6 +8,9 @@ using DynamicPPL, Distributions, Test using LinearAlgebra: I using Random: Xoshiro +struct NaNObservation end +Base.isnan(::NaNObservation) = true + function test_model_fails_check(model) issuccess = check_model(model) @test !issuccess @@ -122,6 +125,14 @@ end @test_throws ErrorException check_model(m; error_on_failure=true) end + @testset "non-numeric data" begin + @test !DynamicPPL.DebugUtils._has_nans(( + label=:right, values=["left", missing, 1.0] + )) + @test DynamicPPL.DebugUtils._has_nans((label=:right, values=["left", missing, NaN])) + @test DynamicPPL.DebugUtils._has_nans((label=:right, value=NaNObservation())) + end + @testset "incorrect use of condition" begin @testset "missing in multivariate" begin @model function demo_missing_in_multivariate(x)