Skip to content

Commit c7bdc3f

Browse files
authored
Error on NaN's in check_model (#888)
* Error on NaN's * Add a test
1 parent 019e41b commit c7bdc3f

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

HISTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# DynamicPPL Changelog
22

3+
## 0.35.7
4+
5+
`check_model_and_trace` now errors if any NaN's are encountered when evaluating the model.
6+
37
## 0.35.6
48

59
Fixed the implementation of `.~`, such that running a model with it no longer requires DynamicPPL itself to be loaded.

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DynamicPPL"
22
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
3-
version = "0.35.6"
3+
version = "0.35.7"
44

55
[deps]
66
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"

src/debug_utils.jl

+14-14
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,11 @@ function record_varname!(context::DebugContext, varname::VarName, dist)
231231
end
232232
end
233233

234-
# tilde
235-
_isassigned(x::AbstractArray, i) = isassigned(x, i)
236-
# HACK(torfjelde): Julia v1.7 only supports `isassigned(::AbstractArray, ::Int...)`.
237-
# TODO(torfjelde): Determine exactly in which version this change was introduced.
238-
if VERSION < v"v1.9.0-alpha1"
239-
_isassigned(x::AbstractArray, inds::Tuple) = isassigned(x, inds...)
240-
_isassigned(x::AbstractArray, idx::CartesianIndex) = _isassigned(x, Tuple(idx))
241-
end
242-
243234
_has_missings(x) = ismissing(x)
244235
function _has_missings(x::AbstractArray)
245236
# Can't just use `any` because `x` might contain `undef`.
246237
for i in eachindex(x)
247-
if _isassigned(x, i) && _has_missings(x[i])
238+
if isassigned(x, i) && _has_missings(x[i])
248239
return true
249240
end
250241
end
@@ -293,9 +284,18 @@ function record_pre_tilde_observe!(context::DebugContext, left, dist, varinfo)
293284
# Check for `missing`s; these should not end up here.
294285
if _has_missings(left)
295286
error(
296-
"Encountered missing value(s) in observe!\n" *
297-
"Remember that using `missing` to de-condition a variable is only " *
298-
"supported for univariate distributions, not for $dist",
287+
"Encountered `missing` value(s) on the left-hand side" *
288+
" of an observe statement. Using `missing` to de-condition" *
289+
" a variable is only supported for univariate distributions," *
290+
" not for $dist.",
291+
)
292+
end
293+
# Check for NaN's as well
294+
if any(isnan, left)
295+
error(
296+
"Encountered a NaN value on the left-hand side of an" *
297+
" observe statement; this may indicate that your data" *
298+
" contain NaN values.",
299299
)
300300
end
301301
end
@@ -474,7 +474,7 @@ end
474474
475475
Check that `model` is valid, warning about any potential issues.
476476
477-
See [`check_model_and_trace`](@ref) for more details on supported keword arguments
477+
See [`check_model_and_trace`](@ref) for more details on supported keyword arguments
478478
and details of which types of checks are performed.
479479
480480
# Returns

test/debug_utils.jl

+11
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@
123123
end
124124
end
125125

126+
@testset "NaN in data" begin
127+
@model function demo_nan_in_data(x)
128+
a ~ Normal()
129+
for i in eachindex(x)
130+
x[i] ~ Normal(a)
131+
end
132+
end
133+
model = demo_nan_in_data([1.0, NaN])
134+
@test_throws ErrorException check_model(model; error_on_failure=true)
135+
end
136+
126137
@testset "incorrect use of condition" begin
127138
@testset "missing in multivariate" begin
128139
@model function demo_missing_in_multivariate(x)

0 commit comments

Comments
 (0)