Skip to content

Commit

Permalink
fix: Correctly handle inf and nan values
Browse files Browse the repository at this point in the history
Correctly handle inf and nan values. Fall back on the standard library's
`json` module in the rare event that JSON lines contain nan or inf
values.
  • Loading branch information
riddell-stan authored and ahartikainen committed Nov 19, 2020
1 parent 6d9d0b2 commit bbb493d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions stan/fit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Sequence

import numpy as np
Expand Down Expand Up @@ -64,10 +65,14 @@ def __init__(
for chain_index, stan_output in zip(range(self.num_chains), self.stan_outputs):
draw_index = 0
for line in stan_output.splitlines():
msg = parser.parse(line)
try:
msg = parser.parse(line)
except ValueError:
# Occurs when draws contain an nan or infinity. simdjson cannot parse such values.
msg = json.loads(line)
if msg["topic"] == "sample":
# Ignore sample message which is mixed together with proper draws.
if not isinstance(msg["values"], simdjson.Object):
if not isinstance(msg["values"], (simdjson.Object, dict)):
continue

# for the first draw: collect sample and sampler parameter names.
Expand Down
4 changes: 4 additions & 0 deletions stan/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def is_iteration_or_elapsed_time_logger_message(msg: simdjson.Object):
for stan_output in stan_outputs:
assert isinstance(stan_output, bytes)
for line in stan_output.splitlines():
# Do not attempt to parse non-logger messages. Draws could contain nan or inf values.
# simdjson cannot parse lines containing such values.
if b'"logger"' not in line:
continue
msg = parser.parse(line)
if is_nonempty_logger_message(msg) and not is_iteration_or_elapsed_time_logger_message(msg):
nonstandard_logger_messages.append(msg.as_dict())
Expand Down

0 comments on commit bbb493d

Please sign in to comment.