Skip to content

Commit fd721ff

Browse files
committed
Update docs, check version in model constructor
1 parent 94d96cc commit fd721ff

File tree

4 files changed

+49
-38
lines changed

4 files changed

+49
-38
lines changed

cmdstanpy/model.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
import io
44
import os
5-
import platform
65
import re
76
import shutil
87
import subprocess
98
import sys
109
import tempfile
1110
import threading
12-
from collections import OrderedDict
1311
from concurrent.futures import ThreadPoolExecutor
1412
from io import StringIO
1513
from multiprocessing import cpu_count
@@ -41,12 +39,8 @@
4139
RunSet,
4240
from_csv,
4341
)
44-
from cmdstanpy.utils import (
45-
cmdstan_path,
46-
cmdstan_version_before,
47-
do_command,
48-
get_logger,
49-
)
42+
from cmdstanpy.utils import do_command, get_logger
43+
from cmdstanpy.utils.cmdstan import cmdstan_version_before, windows_tbb_path
5044
from cmdstanpy.utils.filesystem import (
5145
temp_inits,
5246
temp_metrics,
@@ -118,6 +112,8 @@ def __init__(
118112

119113
self._fixed_param = False
120114

115+
windows_tbb_path()
116+
121117
if exe_file is not None:
122118
self._exe_file = os.path.realpath(os.path.expanduser(exe_file))
123119
if not os.path.exists(self._exe_file):
@@ -169,26 +165,21 @@ def __init__(
169165
if 'parameters' in model_info:
170166
self._fixed_param |= len(model_info['parameters']) == 0
171167

172-
if platform.system() == 'Windows':
173-
try:
174-
do_command(['where.exe', 'tbb.dll'], fd_out=None)
175-
except RuntimeError:
176-
# Add tbb to the $PATH on Windows
177-
libtbb = os.environ.get('STAN_TBB')
178-
if libtbb is None:
179-
libtbb = os.path.join(
180-
cmdstan_path(), 'stan', 'lib', 'stan_math', 'lib', 'tbb'
181-
)
182-
get_logger().debug("Adding TBB (%s) to PATH", libtbb)
183-
os.environ['PATH'] = ';'.join(
184-
list(
185-
OrderedDict.fromkeys(
186-
[libtbb] + os.environ.get('PATH', '').split(';')
187-
)
188-
)
189-
)
190-
else:
191-
get_logger().debug("TBB already found in load path")
168+
# check CmdStan version compatibility
169+
exe_info = None
170+
try:
171+
exe_info = self.exe_info()
172+
# pylint: disable=broad-except
173+
except Exception as e:
174+
get_logger().warning(
175+
'Could not get exe info for model %s, error: %s',
176+
self._name,
177+
str(e),
178+
)
179+
if cmdstan_version_before(2, 35, exe_info):
180+
raise RuntimeError(
181+
"This version of CmdStanPy requires CmdStan 2.35 or higher."
182+
)
192183

193184
def __repr__(self) -> str:
194185
return (

cmdstanpy/utils/cmdstan.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from tqdm.auto import tqdm
1313

1414
from cmdstanpy import _DOT_CMDSTAN
15+
from cmdstanpy.utils.command import do_command
1516

1617
from .. import progress as progbar
1718
from .logging import get_logger
@@ -431,6 +432,29 @@ def cxx_toolchain_path(
431432
return compiler_path, tool_path
432433

433434

435+
def windows_tbb_path() -> None:
436+
if platform.system() == 'Windows':
437+
try:
438+
do_command(['where.exe', 'tbb.dll'], fd_out=None)
439+
except RuntimeError:
440+
# Add tbb to the $PATH on Windows
441+
libtbb = os.environ.get('STAN_TBB')
442+
if libtbb is None:
443+
libtbb = os.path.join(
444+
cmdstan_path(), 'stan', 'lib', 'stan_math', 'lib', 'tbb'
445+
)
446+
get_logger().debug("Adding TBB (%s) to PATH", libtbb)
447+
os.environ['PATH'] = ';'.join(
448+
list(
449+
OrderedDict.fromkeys(
450+
[libtbb] + os.environ.get('PATH', '').split(';')
451+
)
452+
)
453+
)
454+
else:
455+
get_logger().debug("TBB already found in load path")
456+
457+
434458
def install_cmdstan(
435459
version: str | None = None,
436460
dir: str | None = None,

cmdstanpy_tutorial.ipynb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@
7979
"or whichever name you used for your environment (following `-n`\n",
8080
"above).\n",
8181
"\n",
82-
"Note that CmdStan is only available on conda for versions\n",
83-
"2.27.0 and newer. If you require an older version, you must use\n",
84-
"one of the following methods to install it. If you require a\n",
85-
"version of CmdStan *newer* than 2.27.0, but not the latest,\n",
86-
"you can install it in the standard conda way by specifying\n",
87-
"`cmdstan==VERSION` in the install command.\n",
8882
"\n",
8983
"#### Pip (non-Conda) users \n",
9084
"\n",

docsrc/installation.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
Installation
22
============
33

4-
CmdStanPy is a pure-Python3 package which wraps CmdStan,
4+
CmdStanPy is a pure-Python package which wraps CmdStan,
55
the command-line interface to Stan which is written in C++.
6-
Therefore, in addition to Python3,
7-
CmdStanPy requires a modern C++ toolchain in order to build and run Stan models.
8-
There are several ways to install CmdStanPy and the underlying CmdStan components.
6+
7+
Therefore, CmdStanPy requires a modern C++ toolchain in order to build and run
8+
Stan models, as well as a recent version of CmdStan (released within the year
9+
prior to the release of the CmdStanPy version being used). There are several
10+
ways to install CmdStanPy and the underlying CmdStan components.
911

1012
* You can download CmdStanPy, CmdStan, and the C++ toolchain from conda-forge.
1113

0 commit comments

Comments
 (0)