Skip to content

Commit c340838

Browse files
committed
flipped logic - assume cmdstan is latest, test is CmdStan version is before Major.minor
1 parent b33fe06 commit c340838

6 files changed

Lines changed: 45 additions & 24 deletions

File tree

cmdstanpy/cmdstan_args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from cmdstanpy import _TMPDIR
1414
from cmdstanpy.utils import (
1515
cmdstan_path,
16-
cmdstan_version_at,
16+
cmdstan_version_before,
1717
create_named_text_file,
1818
get_logger,
1919
read_metric,
@@ -818,7 +818,7 @@ def validate(self) -> None:
818818
'Argument "sig_figs" must be an integer between 1 and 18,'
819819
' found {}'.format(self.sig_figs)
820820
)
821-
if not cmdstan_version_at(2, 25):
821+
if cmdstan_version_before(2, 25):
822822
self.sig_figs = None
823823
get_logger().warning(
824824
'Argument "sig_figs" invalid for CmdStan versions < 2.25, '

cmdstanpy/stanfit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
EXTENSION,
4545
check_sampler_csv,
4646
cmdstan_path,
47-
cmdstan_version_at,
47+
cmdstan_version_before,
4848
create_named_text_file,
4949
do_command,
5050
flatten_chains,
@@ -879,7 +879,7 @@ def summary(
879879
dir=_TMPDIR, prefix=tmp_csv_file, suffix='.csv', name_only=True
880880
)
881881
csv_str = '--csv_filename={}'.format(tmp_csv_path)
882-
if not cmdstan_version_at(2, 24):
882+
if cmdstan_version_before(2, 24):
883883
csv_str = '--csv_file={}'.format(tmp_csv_path)
884884
cmd = [
885885
cmd_path,

cmdstanpy/utils.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ def cmdstan_path() -> str:
182182
return cmdstan
183183

184184

185-
def cmdstan_version_at(major: int, minor: int) -> bool:
185+
def cmdstan_version_before(major: int, minor: int) -> bool:
186186
"""
187-
Check that CmdStan version is at or above Major.minor version.
187+
Check that CmdStan version is less than Major.minor version.
188188
Parses version string out of CmdStan makefile variable CMDSTAN_VERSION.
189189
190190
If CmdStan installation is found but cannot parse version from makefile
@@ -200,18 +200,18 @@ def cmdstan_version_at(major: int, minor: int) -> bool:
200200
try:
201201
makefile = os.path.join(cmdstan_path(), 'makefile')
202202
except ValueError:
203-
get_logger().warning(
203+
get_logger().info(
204204
'No CmdStan installation found, '
205-
'cannot check that Cmdstan version is at or above %d.%d.',
205+
'cannot assert version is less than %d.%d.',
206206
major,
207207
minor,
208208
)
209209
return False
210210

211211
if not os.path.exists(makefile):
212-
get_logger().warning(
212+
get_logger().info(
213213
'CmdStan installation %s missing makefile, '
214-
'cannot check that Cmdstan version is at or above %d.%d.',
214+
'cannot assert version is less than %d.%d.',
215215
cmdstan_path(),
216216
major,
217217
minor,
@@ -223,9 +223,9 @@ def cmdstan_version_at(major: int, minor: int) -> bool:
223223

224224
start_idx = contents.find('CMDSTAN_VERSION := ')
225225
if start_idx < 0:
226-
get_logger().warning(
226+
get_logger().info(
227227
'Cannot parse version from makefile: %s,'
228-
'cannot check that Cmdstan version is at or above %d.%d.',
228+
'cannot assert version is less than %d.%d.',
229229
makefile,
230230
major,
231231
minor,
@@ -237,9 +237,9 @@ def cmdstan_version_at(major: int, minor: int) -> bool:
237237

238238
version = contents[start_idx:end_idx]
239239
if version is None or len(version) < 3 or len(version.split('.')) < 2:
240-
get_logger().warning(
240+
get_logger().info(
241241
'Cannot parse version from makefile: %s,'
242-
'cannot check that Cmdstan version is at or above %d.%d.',
242+
'cannot assert version is less than %d.%d.',
243243
makefile,
244244
major,
245245
minor,
@@ -249,7 +249,7 @@ def cmdstan_version_at(major: int, minor: int) -> bool:
249249
splits = version.split('.')
250250
cur_major = int(splits[0])
251251
cur_minor = int(splits[1])
252-
if cur_major > major or (cur_major == major and cur_minor >= minor):
252+
if cur_major < major or (cur_major == major and cur_minor < minor):
253253
return True
254254
return False
255255

test/test_cmdstan_args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
SamplerArgs,
1818
VariationalArgs,
1919
)
20-
from cmdstanpy.utils import cmdstan_version_at
20+
from cmdstanpy.utils import cmdstan_version_before
2121

2222
HERE = os.path.dirname(os.path.abspath(__file__))
2323
DATAFILES_PATH = os.path.join(HERE, 'data')
@@ -576,7 +576,7 @@ def test_args_bad(self):
576576
def test_args_sig_figs(self):
577577
sampler_args = SamplerArgs()
578578
cmdstan_path() # sets os.environ['CMDSTAN']
579-
if not cmdstan_version_at(2, 25):
579+
if cmdstan_version_before(2, 25):
580580
with LogCapture() as log:
581581
logging.getLogger()
582582
CmdStanArgs(

test/test_sample.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from cmdstanpy.cmdstan_args import CmdStanArgs, Method, SamplerArgs
2828
from cmdstanpy.model import CmdStanModel
2929
from cmdstanpy.stanfit import CmdStanMCMC, RunSet, from_csv
30-
from cmdstanpy.utils import EXTENSION, cmdstan_version_at
30+
from cmdstanpy.utils import EXTENSION, cmdstan_version_before
3131

3232
HERE = os.path.dirname(os.path.abspath(__file__))
3333
DATAFILES_PATH = os.path.join(HERE, 'data')
@@ -557,7 +557,7 @@ def test_bernoulli_path_with_space(self):
557557
)
558558

559559
def test_index_bounds_error(self):
560-
if cmdstan_version_at(2, 25) or cmdstan_version_at(2, 26):
560+
if not cmdstan_version_before(2, 27):
561561
oob_stan = os.path.join(DATAFILES_PATH, 'out_of_bounds.stan')
562562
oob_model = CmdStanModel(stan_file=oob_stan)
563563
with self.assertRaises(RuntimeError):
@@ -1496,7 +1496,7 @@ def test_validate(self):
14961496
self.assertEqual(bern_fit.metric_type, 'diag_e')
14971497

14981498
def test_validate_sample_sig_figs(self, stanfile='bernoulli.stan'):
1499-
if cmdstan_version_at(2, 25):
1499+
if not cmdstan_version_before(2, 25):
15001500
stan = os.path.join(DATAFILES_PATH, stanfile)
15011501
bern_model = CmdStanModel(stan_file=stan)
15021502

@@ -1568,7 +1568,7 @@ def test_validate_summary_sig_figs(self):
15681568
beta1_default = format(sum_default.iloc[1, 0], '.18g')
15691569
self.assertTrue(beta1_default.startswith('1.3'))
15701570

1571-
if cmdstan_version_at(2, 25):
1571+
if not cmdstan_version_before(2, 25):
15721572
sum_17 = fit.summary(sig_figs=17)
15731573
beta1_17 = format(sum_17.iloc[1, 0], '.18g')
15741574
self.assertTrue(beta1_17.startswith('1.345767078273'))

test/test_utils.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
TemporaryCopiedFile,
2929
check_sampler_csv,
3030
cmdstan_path,
31-
cmdstan_version_at,
31+
cmdstan_version_before,
3232
do_command,
3333
flatten_chains,
3434
get_latest_cmdstan,
@@ -202,9 +202,30 @@ def test_munge_cmdstan_versions(self):
202202
os.makedirs(os.path.join(tdir, 'cmdstan-2.22.0'))
203203
self.assertEqual(get_latest_cmdstan(tdir), 'cmdstan-2.22.0')
204204

205-
def test_cmdstan_version_at(self):
205+
def test_cmdstan_version_before(self):
206206
cmdstan_path() # sets os.environ['CMDSTAN']
207-
self.assertFalse(cmdstan_version_at(99, 99))
207+
self.assertTrue(cmdstan_version_before(99, 99))
208+
209+
with tempfile.TemporaryDirectory(
210+
prefix="cmdstan_tests", dir=_TMPDIR
211+
) as tmpdir:
212+
tdir = os.path.join(tmpdir, 'tmpdir_xxx')
213+
os.makedirs(tdir)
214+
os.makedirs(os.path.join(tdir, 'cmdstan-2.22.0'))
215+
os.environ['CMDSTAN'] = os.path.join(tdir, 'cmdstan-2.22.0')
216+
with LogCapture() as log:
217+
logging.getLogger()
218+
self.assertFalse(cmdstan_version_before(99, 99))
219+
del os.environ['CMDSTAN']
220+
cmdstan_path()
221+
log.check_present(
222+
(
223+
'cmdstanpy',
224+
'INFO',
225+
'No CmdStan installation found, '
226+
'cannot assert version is less than 99.99.',
227+
)
228+
)
208229

209230

210231
class DataFilesTest(unittest.TestCase):

0 commit comments

Comments
 (0)