Skip to content

Error on NaN's in check_model #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# DynamicPPL Changelog

## 0.35.7

`check_model_and_trace` now errors if any NaN's are encountered when evaluating the model.

## 0.35.6

Fixed the implementation of `.~`, such that running a model with it no longer requires DynamicPPL itself to be loaded.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.35.6"
version = "0.35.7"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
28 changes: 14 additions & 14 deletions src/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,11 @@
end
end

# tilde
_isassigned(x::AbstractArray, i) = isassigned(x, i)
# HACK(torfjelde): Julia v1.7 only supports `isassigned(::AbstractArray, ::Int...)`.
# TODO(torfjelde): Determine exactly in which version this change was introduced.
if VERSION < v"v1.9.0-alpha1"
_isassigned(x::AbstractArray, inds::Tuple) = isassigned(x, inds...)
_isassigned(x::AbstractArray, idx::CartesianIndex) = _isassigned(x, Tuple(idx))
end

_has_missings(x) = ismissing(x)
function _has_missings(x::AbstractArray)
# Can't just use `any` because `x` might contain `undef`.
for i in eachindex(x)
if _isassigned(x, i) && _has_missings(x[i])
if isassigned(x, i) && _has_missings(x[i])

Check warning on line 238 in src/debug_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/debug_utils.jl#L238

Added line #L238 was not covered by tests
return true
end
end
Expand Down Expand Up @@ -293,9 +284,18 @@
# Check for `missing`s; these should not end up here.
if _has_missings(left)
error(
"Encountered missing value(s) in observe!\n" *
"Remember that using `missing` to de-condition a variable is only " *
"supported for univariate distributions, not for $dist",
"Encountered `missing` value(s) on the left-hand side" *
" of an observe statement. Using `missing` to de-condition" *
" a variable is only supported for univariate distributions," *
" not for $dist.",
)
end
# Check for NaN's as well
if any(isnan, left)
error(

Check warning on line 295 in src/debug_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/debug_utils.jl#L295

Added line #L295 was not covered by tests
"Encountered a NaN value on the left-hand side of an" *
" observe statement; this may indicate that your data" *
" contain NaN values.",
)
end
end
Expand Down Expand Up @@ -474,7 +474,7 @@

Check that `model` is valid, warning about any potential issues.

See [`check_model_and_trace`](@ref) for more details on supported keword arguments
See [`check_model_and_trace`](@ref) for more details on supported keyword arguments
and details of which types of checks are performed.

# Returns
Expand Down
11 changes: 11 additions & 0 deletions test/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@
end
end

@testset "NaN in data" begin
@model function demo_nan_in_data(x)
a ~ Normal()
for i in eachindex(x)
x[i] ~ Normal(a)
end
end
model = demo_nan_in_data([1.0, NaN])
@test_throws ErrorException check_model(model; error_on_failure=true)
end

@testset "incorrect use of condition" begin
@testset "missing in multivariate" begin
@model function demo_missing_in_multivariate(x)
Expand Down
Loading