Skip to content

Commit dd95ba7

Browse files
authored
Merge pull request #764 from stan-dev/bugfix/textual_true_false
Scan string true and false in scan_config
2 parents ec5cabc + 4fb78da commit dd95ba7

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

cmdstanpy/utils/stancsv.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ def scan_config(fd: TextIO, config_dict: Dict[str, Any], lineno: int) -> int:
193193
try:
194194
val = float(raw_val)
195195
except ValueError:
196-
val = raw_val
196+
if raw_val == "true":
197+
val = 1
198+
elif raw_val == "false":
199+
val = 0
200+
else:
201+
val = raw_val
197202
config_dict[key_val[0].strip()] = val
198203
cur_pos = fd.tell()
199204
line = fd.readline().strip()

docsrc/changes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ What's New
77

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

10+
CmdStanPy 1.2.4
11+
---------------
12+
13+
- Fixed a bug in `from_csv` which prevented reading files created by CmdStan 2.35.0+
14+
15+
Reminder: The next non-bugfix release of CmdStanPy will be version 2.0, which will remove all existing deprecations.
1016

1117
CmdStanPy 1.2.3
1218
---------------

test/test_sample.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,26 @@ def test_tuple_data_in() -> None:
20302030
data_model.sample(data, chains=1, iter_warmup=1, iter_sampling=1)
20312031

20322032

2033+
def test_csv_roundtrip():
2034+
stan = os.path.join(DATAFILES_PATH, 'matrix_var.stan')
2035+
model = CmdStanModel(stan_file=stan)
2036+
fit = model.sample(
2037+
iter_sampling=10, iter_warmup=9, chains=2, save_warmup=True
2038+
)
2039+
z = fit.stan_variable(var="z")
2040+
assert z.shape == (20, 4, 3)
2041+
z_with_warmup = fit.stan_variable(var="z", inc_warmup=True)
2042+
assert z_with_warmup.shape == (38, 4, 3)
2043+
2044+
# mostly just asserting that from_csv always succeeds
2045+
# in parsing latest cmdstan headers
2046+
fit_from_csv = from_csv(fit.runset.csv_files)
2047+
z_from_csv = fit_from_csv.stan_variable(var="z")
2048+
assert z_from_csv.shape == (20, 4, 3)
2049+
z_with_warmup_from_csv = fit.stan_variable(var="z", inc_warmup=True)
2050+
assert z_with_warmup_from_csv.shape == (38, 4, 3)
2051+
2052+
20332053
@pytest.mark.order(before="test_no_xarray")
20342054
def test_serialization(stanfile='bernoulli.stan'):
20352055
# This test must before any test that uses the `without_import` context

0 commit comments

Comments
 (0)