cmdstanpy 1.2.3__tar.gz → 1.2.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cmdstanpy-1.2.3/cmdstanpy.egg-info → cmdstanpy-1.2.5}/PKG-INFO +1 -1
- cmdstanpy-1.2.5/cmdstanpy/_version.py +3 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/model.py +3 -3
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/stanfit/mcmc.py +13 -10
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/utils/stancsv.py +6 -1
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5/cmdstanpy.egg-info}/PKG-INFO +1 -1
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_model.py +5 -2
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_sample.py +51 -30
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_variational.py +9 -4
- cmdstanpy-1.2.3/cmdstanpy/_version.py +0 -3
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/LICENSE.md +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/MANIFEST.in +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/README.md +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/__init__.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/cmdstan_args.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/compilation.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/install_cmdstan.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/install_cxx_toolchain.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/progress.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/py.typed +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/stanfit/__init__.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/stanfit/gq.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/stanfit/laplace.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/stanfit/metadata.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/stanfit/mle.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/stanfit/pathfinder.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/stanfit/runset.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/stanfit/vb.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/utils/__init__.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/utils/cmdstan.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/utils/command.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/utils/data_munging.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/utils/filesystem.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/utils/json.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy/utils/logging.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy.egg-info/SOURCES.txt +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy.egg-info/dependency_links.txt +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy.egg-info/entry_points.txt +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy.egg-info/requires.txt +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/cmdstanpy.egg-info/top_level.txt +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/pyproject.toml +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/setup.cfg +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_cmdstan_args.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_compiler_opts.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_compliance.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_cxx_installation.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_generate_quantities.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_install_cmdstan.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_laplace.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_log_prob.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_metadata.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_optimize.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_pathfinder.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_runset.py +0 -0
- {cmdstanpy-1.2.3 → cmdstanpy-1.2.5}/test/test_utils.py +0 -0
|
@@ -205,9 +205,9 @@ class CmdStanModel:
|
|
|
205
205
|
self._compiler_options.add_include_path(path)
|
|
206
206
|
|
|
207
207
|
# try to detect models w/out parameters, needed for sampler
|
|
208
|
-
if not cmdstan_version_before(
|
|
209
|
-
2,
|
|
210
|
-
):
|
|
208
|
+
if (not cmdstan_version_before(2, 27)) and cmdstan_version_before(
|
|
209
|
+
2, 36
|
|
210
|
+
):
|
|
211
211
|
try:
|
|
212
212
|
model_info = self.src_info()
|
|
213
213
|
if 'parameters' in model_info:
|
|
@@ -412,7 +412,7 @@ class CmdStanMCMC:
|
|
|
412
412
|
self._step_size[chain] = float(step_size.strip())
|
|
413
413
|
if self._metadata.cmdstan_config['metric'] != 'unit_e':
|
|
414
414
|
line = fd.readline().strip() # metric type
|
|
415
|
-
line = fd.readline().lstrip(' #\t')
|
|
415
|
+
line = fd.readline().lstrip(' #\t').rstrip()
|
|
416
416
|
num_unconstrained_params = len(line.split(','))
|
|
417
417
|
if chain == 0: # can't allocate w/o num params
|
|
418
418
|
if self.metric_type == 'diag_e':
|
|
@@ -429,18 +429,21 @@ class CmdStanMCMC:
|
|
|
429
429
|
),
|
|
430
430
|
dtype=float,
|
|
431
431
|
)
|
|
432
|
-
if
|
|
433
|
-
|
|
434
|
-
self._metric[chain, :] = [float(x) for x in xs]
|
|
435
|
-
else:
|
|
436
|
-
xs = line.split(',')
|
|
437
|
-
self._metric[chain, 0, :] = [float(x) for x in xs]
|
|
438
|
-
for i in range(1, num_unconstrained_params):
|
|
439
|
-
line = fd.readline().lstrip(' #\t').strip()
|
|
432
|
+
if line:
|
|
433
|
+
if self.metric_type == 'diag_e':
|
|
440
434
|
xs = line.split(',')
|
|
441
|
-
self._metric[chain,
|
|
435
|
+
self._metric[chain, :] = [float(x) for x in xs]
|
|
436
|
+
else:
|
|
437
|
+
xs = line.strip().split(',')
|
|
438
|
+
self._metric[chain, 0, :] = [
|
|
442
439
|
float(x) for x in xs
|
|
443
440
|
]
|
|
441
|
+
for i in range(1, num_unconstrained_params):
|
|
442
|
+
line = fd.readline().lstrip(' #\t').rstrip()
|
|
443
|
+
xs = line.split(',')
|
|
444
|
+
self._metric[chain, i, :] = [
|
|
445
|
+
float(x) for x in xs
|
|
446
|
+
]
|
|
444
447
|
else: # unit_e changed in 2.34 to have an extra line
|
|
445
448
|
pos = fd.tell()
|
|
446
449
|
line = fd.readline().strip()
|
|
@@ -193,7 +193,12 @@ def scan_config(fd: TextIO, config_dict: Dict[str, Any], lineno: int) -> int:
|
|
|
193
193
|
try:
|
|
194
194
|
val = float(raw_val)
|
|
195
195
|
except ValueError:
|
|
196
|
-
|
|
196
|
+
if raw_val == "true":
|
|
197
|
+
val = 1
|
|
198
|
+
elif raw_val == "false":
|
|
199
|
+
val = 0
|
|
200
|
+
else:
|
|
201
|
+
val = raw_val
|
|
197
202
|
config_dict[key_val[0].strip()] = val
|
|
198
203
|
cur_pos = fd.tell()
|
|
199
204
|
line = fd.readline().strip()
|
|
@@ -95,10 +95,13 @@ def test_exe_only() -> None:
|
|
|
95
95
|
assert not model2._fixed_param
|
|
96
96
|
|
|
97
97
|
|
|
98
|
-
def
|
|
98
|
+
def test_legacy_fixed_param() -> None:
|
|
99
99
|
stan = os.path.join(DATAFILES_PATH, 'datagen_poisson_glm.stan')
|
|
100
100
|
model = CmdStanModel(stan_file=stan)
|
|
101
|
-
|
|
101
|
+
if cmdstan_version_before(2, 36):
|
|
102
|
+
assert model._fixed_param
|
|
103
|
+
else:
|
|
104
|
+
assert not model._fixed_param
|
|
102
105
|
|
|
103
106
|
|
|
104
107
|
def test_model_pedantic(caplog: pytest.LogCaptureFixture) -> None:
|
|
@@ -642,13 +642,18 @@ def test_fixed_param_good() -> None:
|
|
|
642
642
|
assert datagen_fit.step_size is None
|
|
643
643
|
|
|
644
644
|
|
|
645
|
-
def
|
|
645
|
+
def test_sample_no_params() -> None:
|
|
646
646
|
stan = os.path.join(DATAFILES_PATH, 'datagen_poisson_glm.stan')
|
|
647
647
|
datagen_model = CmdStanModel(stan_file=stan)
|
|
648
648
|
datagen_fit = datagen_model.sample(iter_sampling=100, show_progress=False)
|
|
649
|
-
assert datagen_fit.step_size is None
|
|
650
649
|
summary = datagen_fit.summary()
|
|
651
|
-
|
|
650
|
+
|
|
651
|
+
if cmdstan_version_before(2, 36):
|
|
652
|
+
assert 'lp__' not in list(summary.index)
|
|
653
|
+
assert datagen_fit.step_size is None
|
|
654
|
+
else:
|
|
655
|
+
assert 'lp__' in list(summary.index)
|
|
656
|
+
assert np.isnan(datagen_fit.step_size).all()
|
|
652
657
|
|
|
653
658
|
exe_only = os.path.join(DATAFILES_PATH, 'exe_only')
|
|
654
659
|
shutil.copyfile(datagen_model.exe_file, exe_only)
|
|
@@ -656,9 +661,14 @@ def test_fixed_param_unspecified() -> None:
|
|
|
656
661
|
datagen2_model = CmdStanModel(exe_file=exe_only)
|
|
657
662
|
datagen2_fit = datagen2_model.sample(iter_sampling=200, show_console=True)
|
|
658
663
|
assert datagen2_fit.chains == 4
|
|
659
|
-
assert datagen2_fit.step_size is None
|
|
660
664
|
summary = datagen2_fit.summary()
|
|
661
|
-
|
|
665
|
+
|
|
666
|
+
if cmdstan_version_before(2, 36):
|
|
667
|
+
assert datagen2_fit.step_size is None
|
|
668
|
+
assert 'lp__' not in list(summary.index)
|
|
669
|
+
else:
|
|
670
|
+
assert np.isnan(datagen2_fit.step_size).all()
|
|
671
|
+
assert 'lp__' in list(summary.index)
|
|
662
672
|
|
|
663
673
|
|
|
664
674
|
def test_index_bounds_error() -> None:
|
|
@@ -823,7 +833,7 @@ def test_validate_good_run() -> None:
|
|
|
823
833
|
assert 'Treedepth satisfactory for all transitions.' in diagnostics
|
|
824
834
|
assert 'No divergent transitions found.' in diagnostics
|
|
825
835
|
assert 'E-BFMI satisfactory' in diagnostics
|
|
826
|
-
assert '
|
|
836
|
+
assert 'effective sample size satisfactory' in diagnostics.lower()
|
|
827
837
|
|
|
828
838
|
|
|
829
839
|
def test_validate_big_run() -> None:
|
|
@@ -986,6 +996,12 @@ def test_from_csv_fixed_param() -> None:
|
|
|
986
996
|
assert fixed_param_sample.draws_pd().shape == (100, 88)
|
|
987
997
|
|
|
988
998
|
|
|
999
|
+
def test_from_csv_no_param_hmc() -> None:
|
|
1000
|
+
csv_path = os.path.join(DATAFILES_PATH, 'no_param_hmc_sample.csv')
|
|
1001
|
+
no_parameters_sample = from_csv(path=csv_path)
|
|
1002
|
+
assert no_parameters_sample.draws_pd().shape == (100, 93)
|
|
1003
|
+
|
|
1004
|
+
|
|
989
1005
|
def test_custom_metric() -> None:
|
|
990
1006
|
stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
|
|
991
1007
|
jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
|
|
@@ -1621,33 +1637,18 @@ def test_validate_sample_sig_figs(stanfile='bernoulli.stan'):
|
|
|
1621
1637
|
|
|
1622
1638
|
|
|
1623
1639
|
def test_validate_summary_sig_figs() -> None:
|
|
1624
|
-
# construct CmdStanMCMC from logistic model output
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
seed=12345,
|
|
1633
|
-
data=rdata,
|
|
1634
|
-
output_dir=DATAFILES_PATH,
|
|
1635
|
-
sig_figs=17,
|
|
1636
|
-
method_args=sampler_args,
|
|
1640
|
+
# construct CmdStanMCMC from logistic model output
|
|
1641
|
+
fit = from_csv(
|
|
1642
|
+
[
|
|
1643
|
+
os.path.join(DATAFILES_PATH, 'logistic_output_1.csv'),
|
|
1644
|
+
os.path.join(DATAFILES_PATH, 'logistic_output_2.csv'),
|
|
1645
|
+
os.path.join(DATAFILES_PATH, 'logistic_output_3.csv'),
|
|
1646
|
+
os.path.join(DATAFILES_PATH, 'logistic_output_4.csv'),
|
|
1647
|
+
]
|
|
1637
1648
|
)
|
|
1638
|
-
runset = RunSet(args=cmdstan_args, chains=4)
|
|
1639
|
-
runset._csv_files = [
|
|
1640
|
-
os.path.join(DATAFILES_PATH, 'logistic_output_1.csv'),
|
|
1641
|
-
os.path.join(DATAFILES_PATH, 'logistic_output_2.csv'),
|
|
1642
|
-
os.path.join(DATAFILES_PATH, 'logistic_output_3.csv'),
|
|
1643
|
-
os.path.join(DATAFILES_PATH, 'logistic_output_4.csv'),
|
|
1644
|
-
]
|
|
1645
|
-
retcodes = runset._retcodes
|
|
1646
|
-
for i in range(len(retcodes)):
|
|
1647
|
-
runset._set_retcode(i, 0)
|
|
1648
|
-
fit = CmdStanMCMC(runset)
|
|
1649
1649
|
|
|
1650
1650
|
sum_default = fit.summary()
|
|
1651
|
+
|
|
1651
1652
|
beta1_default = format(sum_default.iloc[1, 0], '.18g')
|
|
1652
1653
|
assert beta1_default.startswith('1.3')
|
|
1653
1654
|
|
|
@@ -2030,6 +2031,26 @@ def test_tuple_data_in() -> None:
|
|
|
2030
2031
|
data_model.sample(data, chains=1, iter_warmup=1, iter_sampling=1)
|
|
2031
2032
|
|
|
2032
2033
|
|
|
2034
|
+
def test_csv_roundtrip():
|
|
2035
|
+
stan = os.path.join(DATAFILES_PATH, 'matrix_var.stan')
|
|
2036
|
+
model = CmdStanModel(stan_file=stan)
|
|
2037
|
+
fit = model.sample(
|
|
2038
|
+
iter_sampling=10, iter_warmup=9, chains=2, save_warmup=True
|
|
2039
|
+
)
|
|
2040
|
+
z = fit.stan_variable(var="z")
|
|
2041
|
+
assert z.shape == (20, 4, 3)
|
|
2042
|
+
z_with_warmup = fit.stan_variable(var="z", inc_warmup=True)
|
|
2043
|
+
assert z_with_warmup.shape == (38, 4, 3)
|
|
2044
|
+
|
|
2045
|
+
# mostly just asserting that from_csv always succeeds
|
|
2046
|
+
# in parsing latest cmdstan headers
|
|
2047
|
+
fit_from_csv = from_csv(fit.runset.csv_files)
|
|
2048
|
+
z_from_csv = fit_from_csv.stan_variable(var="z")
|
|
2049
|
+
assert z_from_csv.shape == (20, 4, 3)
|
|
2050
|
+
z_with_warmup_from_csv = fit.stan_variable(var="z", inc_warmup=True)
|
|
2051
|
+
assert z_with_warmup_from_csv.shape == (38, 4, 3)
|
|
2052
|
+
|
|
2053
|
+
|
|
2033
2054
|
@pytest.mark.order(before="test_no_xarray")
|
|
2034
2055
|
def test_serialization(stanfile='bernoulli.stan'):
|
|
2035
2056
|
# This test must before any test that uses the `without_import` context
|
|
@@ -15,6 +15,7 @@ import pytest
|
|
|
15
15
|
from cmdstanpy.cmdstan_args import CmdStanArgs, VariationalArgs
|
|
16
16
|
from cmdstanpy.model import CmdStanModel
|
|
17
17
|
from cmdstanpy.stanfit import CmdStanVB, RunSet, from_csv
|
|
18
|
+
from cmdstanpy.utils.cmdstan import cmdstan_version_before
|
|
18
19
|
|
|
19
20
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
20
21
|
DATAFILES_PATH = os.path.join(HERE, 'data')
|
|
@@ -150,12 +151,12 @@ def test_variational_good() -> None:
|
|
|
150
151
|
'mu[2]',
|
|
151
152
|
)
|
|
152
153
|
# fixed seed, id=1 by default will give known output values
|
|
153
|
-
assert variational.eta ==
|
|
154
|
+
assert variational.eta == 10
|
|
154
155
|
np.testing.assert_almost_equal(
|
|
155
|
-
variational.variational_params_dict['mu[1]'],
|
|
156
|
+
variational.variational_params_dict['mu[1]'], 302.142, decimal=2
|
|
156
157
|
)
|
|
157
158
|
np.testing.assert_almost_equal(
|
|
158
|
-
variational.variational_params_dict['mu[2]'],
|
|
159
|
+
variational.variational_params_dict['mu[2]'], 361.005, decimal=2
|
|
159
160
|
)
|
|
160
161
|
np.testing.assert_almost_equal(
|
|
161
162
|
variational.variational_params_np[0],
|
|
@@ -177,7 +178,11 @@ def test_variational_eta_small() -> None:
|
|
|
177
178
|
DATAFILES_PATH, 'variational', 'eta_should_be_small.stan'
|
|
178
179
|
)
|
|
179
180
|
model = CmdStanModel(stan_file=stan)
|
|
180
|
-
|
|
181
|
+
if cmdstan_version_before(2, 35):
|
|
182
|
+
seed = 12345
|
|
183
|
+
else:
|
|
184
|
+
seed = 1234
|
|
185
|
+
variational = model.variational(algorithm='meanfield', seed=seed)
|
|
181
186
|
assert variational.column_names == (
|
|
182
187
|
'lp__',
|
|
183
188
|
'log_p__',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|