Skip to content

Commit

Permalink
Merge pull request #764 from stan-dev/bugfix/textual_true_false
Browse files Browse the repository at this point in the history
Scan string true and false in scan_config
  • Loading branch information
WardBrian authored Jun 17, 2024
2 parents ec5cabc + 4fb78da commit dd95ba7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmdstanpy/utils/stancsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ def scan_config(fd: TextIO, config_dict: Dict[str, Any], lineno: int) -> int:
try:
val = float(raw_val)
except ValueError:
val = raw_val
if raw_val == "true":
val = 1
elif raw_val == "false":
val = 0
else:
val = raw_val
config_dict[key_val[0].strip()] = val
cur_pos = fd.tell()
line = fd.readline().strip()
Expand Down
6 changes: 6 additions & 0 deletions docsrc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ What's New

For full changes, see the `Releases page <https://github.com/stan-dev/cmdstanpy/releases>`_ on GitHub.

CmdStanPy 1.2.4
---------------

- Fixed a bug in `from_csv` which prevented reading files created by CmdStan 2.35.0+

Reminder: The next non-bugfix release of CmdStanPy will be version 2.0, which will remove all existing deprecations.

CmdStanPy 1.2.3
---------------
Expand Down
20 changes: 20 additions & 0 deletions test/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,26 @@ def test_tuple_data_in() -> None:
data_model.sample(data, chains=1, iter_warmup=1, iter_sampling=1)


def test_csv_roundtrip():
stan = os.path.join(DATAFILES_PATH, 'matrix_var.stan')
model = CmdStanModel(stan_file=stan)
fit = model.sample(
iter_sampling=10, iter_warmup=9, chains=2, save_warmup=True
)
z = fit.stan_variable(var="z")
assert z.shape == (20, 4, 3)
z_with_warmup = fit.stan_variable(var="z", inc_warmup=True)
assert z_with_warmup.shape == (38, 4, 3)

# mostly just asserting that from_csv always succeeds
# in parsing latest cmdstan headers
fit_from_csv = from_csv(fit.runset.csv_files)
z_from_csv = fit_from_csv.stan_variable(var="z")
assert z_from_csv.shape == (20, 4, 3)
z_with_warmup_from_csv = fit.stan_variable(var="z", inc_warmup=True)
assert z_with_warmup_from_csv.shape == (38, 4, 3)


@pytest.mark.order(before="test_no_xarray")
def test_serialization(stanfile='bernoulli.stan'):
# This test must before any test that uses the `without_import` context
Expand Down

0 comments on commit dd95ba7

Please sign in to comment.