Skip to content
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

Scan string true and false in scan_config #764

Merged
merged 3 commits into from
Jun 17, 2024
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
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
Loading