Controlling Outputs#
File Outputs#
CmdStanPy’s inference methods produce a bundle of files containing draws or
estimates, the run configuration, and other method-specific information. When
sampling with CmdStanModel.sample(), the primary outputs are a set of
per-chain Stan CSV files.
Their filenames generally follow the pattern
<model_name>-<YYYYMMDDHHMMSS>_<chain_id>.csv.
Each CmdStan process also writes a configuration JSON file that records the
arguments passed to it. CmdStanPy uses the configuration and CSV files to
faithfully reconstruct a fit object such as CmdStanMCMC. CmdStanPy
also captures console messages in text files. Depending on the method and its
options, the bundle can include metric, diagnostic, and profiling files.
In [1]: import os
In [2]: from cmdstanpy import CmdStanModel
In [3]: stan_file = os.path.join('users-guide', 'examples', 'bernoulli.stan')
In [4]: model = CmdStanModel(stan_file=stan_file)
In [5]: data_file = os.path.join('users-guide', 'examples', 'bernoulli.data.json')
In [6]: fit = model.sample(data=data_file)
INFO:cmdstanpy:CmdStan start processing
INFO:cmdstanpy:CmdStan done processing.
# printing the object reports sampler commands, output files
In [7]: print(fit)
CmdStanMCMC: model=bernoulli_model chains=4 method=sample algorithm=hmc
csv_files:
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_1.csv
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_2.csv
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_3.csv
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_4.csv
config_files:
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_1_config.json
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_2_config.json
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_3_config.json
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_4_config.json
output_files:
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_stdout_1.txt
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_stdout_2.txt
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_stdout_3.txt
/tmp/tmpxnuvtsr9/bernoullib_hv6vvc/bernoulli-20261117171316_stdout_4.txt
The output_dir argument is an optional argument which specifies
the path to the output directory used by CmdStan. If this argument is omitted,
the output files are written to a temporary directory which is deleted when the
current Python session is terminated.
In [8]: fit = model.sample(data=data_file, output_dir="./outputs/")
INFO:cmdstanpy:created output directory: /home/runner/work/cmdstanpy/cmdstanpy/docsrc/outputs
INFO:cmdstanpy:CmdStan start processing
INFO:cmdstanpy:CmdStan done processing.
In [9]: !ls outputs/
bernoulli-20261117171316_1.csv bernoulli-20261117171316_3_metric.json
bernoulli-20261117171316_1_config.json bernoulli-20261117171316_4.csv
bernoulli-20261117171316_1_metric.json bernoulli-20261117171316_4_config.json
bernoulli-20261117171316_2.csv bernoulli-20261117171316_4_metric.json
bernoulli-20261117171316_2_config.json bernoulli-20261117171316_stdout_1.txt
bernoulli-20261117171316_2_metric.json bernoulli-20261117171316_stdout_2.txt
bernoulli-20261117171316_3.csv bernoulli-20261117171316_stdout_3.txt
bernoulli-20261117171316_3_config.json bernoulli-20261117171316_stdout_4.txt
Alternatively, the save_output_files() method moves the
fit’s output files to a specified directory.
In [10]: fit = model.sample(data=data_file)
INFO:cmdstanpy:CmdStan start processing
INFO:cmdstanpy:CmdStan done processing.
In [11]: fit.save_output_files(dir='some/path')
In [12]: !ls some/path
bernoulli-20261117171316_1.csv bernoulli-20261117171316_3_metric.json
bernoulli-20261117171316_1_config.json bernoulli-20261117171316_4.csv
bernoulli-20261117171316_1_metric.json bernoulli-20261117171316_4_config.json
bernoulli-20261117171316_2.csv bernoulli-20261117171316_4_metric.json
bernoulli-20261117171316_2_config.json bernoulli-20261117171316_stdout_1.txt
bernoulli-20261117171316_2_metric.json bernoulli-20261117171316_stdout_2.txt
bernoulli-20261117171316_3.csv bernoulli-20261117171316_stdout_3.txt
bernoulli-20261117171316_3_config.json bernoulli-20261117171316_stdout_4.txt
Logging#
You may notice CmdStanPy can produce a lot of output when it is running:
In [13]: fit = model.sample(data=data_file, show_progress=False)
INFO:cmdstanpy:CmdStan start processing
INFO:cmdstanpy:Chain [1] start processing
INFO:cmdstanpy:Chain [2] start processing
INFO:cmdstanpy:Chain [3] start processing
INFO:cmdstanpy:Chain [4] start processing
INFO:cmdstanpy:Chain [2] done processing
INFO:cmdstanpy:Chain [3] done processing
INFO:cmdstanpy:Chain [4] done processing
INFO:cmdstanpy:Chain [1] done processing
This output is managed through the built-in logging module. For
convenience, CmdStanPy provides the module-level cmdstanpy.enable_logging()
and cmdstanpy.disable_logging() functions to simplify logging management.
For example, it can be disabled entirely:
In [14]: import cmdstanpy
In [15]: cmdstanpy.disable_logging()
Out[15]:
# look, no output!
In [16]: fit = model.sample(data=data_file, show_progress=False)
We can re-enable this by calling enable_logging():
In [17]: cmdstanpy.enable_logging()
Out[17]:
In [18]: fit = model.sample(data=data_file, show_progress=False)
INFO:cmdstanpy:CmdStan start processing
INFO:cmdstanpy:Chain [1] start processing
INFO:cmdstanpy:Chain [2] start processing
INFO:cmdstanpy:Chain [3] start processing
INFO:cmdstanpy:Chain [4] start processing
INFO:cmdstanpy:Chain [3] done processing
INFO:cmdstanpy:Chain [1] done processing
INFO:cmdstanpy:Chain [2] done processing
INFO:cmdstanpy:Chain [4] done processing
These functions also work as context managers for more local control:
In [19]: with cmdstanpy.disable_logging():
....: fit = model.sample(data=data_file, show_progress=False)
....:
An explicitly configured level is preserved when CmdStanPy initializes its logger. For example, to show errors but suppress warnings, set the level before using a model:
import logging
logging.getLogger("cmdstanpy").setLevel(logging.ERROR)
For more fine-grained control, one can interact with the underlying logging
library directly. For example, the following code installs a custom handler
that sends all logs (including the DEBUG logs, which are hidden by
default), to a file.
DEBUG logging is useful primarily to developers or when trying to hunt down an issue.
In [20]: import logging
In [21]: cmdstanpy_logger = logging.getLogger("cmdstanpy")
In [22]: cmdstanpy_logger.disabled = False
# remove all existing handlers
In [23]: cmdstanpy_logger.handlers = []
In [24]: cmdstanpy_logger.setLevel(logging.DEBUG)
In [25]: handler = logging.FileHandler('all.log')
In [26]: handler.setLevel(logging.DEBUG)
In [27]: handler.setFormatter(
....: logging.Formatter(
....: '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
....: "%H:%M:%S",
....: )
....: )
....:
In [28]: cmdstanpy_logger.addHandler(handler)
Now, if we run the model and check the contents of the file, we will see all the possible logging.
In [29]: fit = model.sample(data=data_file, show_progress=False)
In [30]: with open('all.log','r') as logs:
....: for line in logs.readlines():
....: print(line.strip())
....:
17:13:17 - cmdstanpy - DEBUG - cmd: /home/runner/work/cmdstanpy/cmdstanpy/docsrc/users-guide/examples/bernoulli info
cwd: None
17:13:17 - cmdstanpy - INFO - CmdStan start processing
17:13:17 - cmdstanpy - DEBUG - idx 0
17:13:17 - cmdstanpy - DEBUG - idx 1
17:13:17 - cmdstanpy - DEBUG - running CmdStan, num_threads: 1
17:13:17 - cmdstanpy - DEBUG - CmdStan args: ['/home/runner/work/cmdstanpy/cmdstanpy/docsrc/users-guide/examples/bernoulli', 'id=1', 'random', 'seed=72521', 'data', 'file=users-guide/examples/bernoulli.data.json', 'output', 'file=/tmp/tmpxnuvtsr9/bernoullituua3ob8/bernoulli-20261117171317_1.csv', 'save_cmdstan_config=1', 'method=sample', 'algorithm=hmc', 'adapt', 'engaged=1', 'save_metric=1']
17:13:17 - cmdstanpy - INFO - Chain [1] start processing
17:13:17 - cmdstanpy - DEBUG - idx 2
17:13:17 - cmdstanpy - DEBUG - running CmdStan, num_threads: 1
17:13:17 - cmdstanpy - DEBUG - CmdStan args: ['/home/runner/work/cmdstanpy/cmdstanpy/docsrc/users-guide/examples/bernoulli', 'id=3', 'random', 'seed=72521', 'data', 'file=users-guide/examples/bernoulli.data.json', 'output', 'file=/tmp/tmpxnuvtsr9/bernoullituua3ob8/bernoulli-20261117171317_3.csv', 'save_cmdstan_config=1', 'method=sample', 'algorithm=hmc', 'adapt', 'engaged=1', 'save_metric=1']
17:13:17 - cmdstanpy - INFO - Chain [3] start processing
17:13:17 - cmdstanpy - DEBUG - idx 3
17:13:17 - cmdstanpy - DEBUG - running CmdStan, num_threads: 1
17:13:17 - cmdstanpy - DEBUG - CmdStan args: ['/home/runner/work/cmdstanpy/cmdstanpy/docsrc/users-guide/examples/bernoulli', 'id=4', 'random', 'seed=72521', 'data', 'file=users-guide/examples/bernoulli.data.json', 'output', 'file=/tmp/tmpxnuvtsr9/bernoullituua3ob8/bernoulli-20261117171317_4.csv', 'save_cmdstan_config=1', 'method=sample', 'algorithm=hmc', 'adapt', 'engaged=1', 'save_metric=1']
17:13:17 - cmdstanpy - INFO - Chain [4] start processing
17:13:17 - cmdstanpy - DEBUG - running CmdStan, num_threads: 1
17:13:17 - cmdstanpy - DEBUG - CmdStan args: ['/home/runner/work/cmdstanpy/cmdstanpy/docsrc/users-guide/examples/bernoulli', 'id=2', 'random', 'seed=72521', 'data', 'file=users-guide/examples/bernoulli.data.json', 'output', 'file=/tmp/tmpxnuvtsr9/bernoullituua3ob8/bernoulli-20261117171317_2.csv', 'save_cmdstan_config=1', 'method=sample', 'algorithm=hmc', 'adapt', 'engaged=1', 'save_metric=1']
17:13:17 - cmdstanpy - INFO - Chain [2] start processing
17:13:17 - cmdstanpy - INFO - Chain [1] done processing
17:13:17 - cmdstanpy - INFO - Chain [4] done processing
17:13:17 - cmdstanpy - INFO - Chain [3] done processing
17:13:17 - cmdstanpy - INFO - Chain [2] done processing
17:13:17 - cmdstanpy - DEBUG - runset
RunSet: chains=4, chain_ids=[1, 2, 3, 4], num_processes=4
cmd (chain 1):
['/home/runner/work/cmdstanpy/cmdstanpy/docsrc/users-guide/examples/bernoulli', 'id=1', 'random', 'seed=72521', 'data', 'file=users-guide/examples/bernoulli.data.json', 'output', 'file=/tmp/tmpxnuvtsr9/bernoullituua3ob8/bernoulli-20261117171317_1.csv', 'save_cmdstan_config=1', 'method=sample', 'algorithm=hmc', 'adapt', 'engaged=1', 'save_metric=1']
retcodes=[0, 0, 0, 0]
per-chain output files (showing chain 1 only):
csv_file:
/tmp/tmpxnuvtsr9/bernoullituua3ob8/bernoulli-20261117171317_1.csv
console_msgs (if any):
/tmp/tmpxnuvtsr9/bernoullituua3ob8/bernoulli-20261117171317_stdout_1.txt
config_files:
/tmp/tmpxnuvtsr9/bernoullituua3ob8/bernoulli-20261117171317_1_config.json
17:13:17 - cmdstanpy - DEBUG - Chain 1 console:
method = sample (Default)
sample
num_samples = 1000 (Default)
num_warmup = 1000 (Default)
save_warmup = false (Default)
thin = 1 (Default)
adapt
engaged = true (Default)
gamma = 0.05 (Default)
delta = 0.8 (Default)
kappa = 0.75 (Default)
t0 = 10 (Default)
init_buffer = 75 (Default)
term_buffer = 50 (Default)
window = 25 (Default)
save_metric = true
algorithm = hmc (Default)
hmc
engine = nuts (Default)
nuts
max_depth = 10 (Default)
metric = diag_e (Default)
metric_file = (Default)
stepsize = 1 (Default)
stepsize_jitter = 0 (Default)
num_chains = 1 (Default)
id = 1 (Default)
data
file = users-guide/examples/bernoulli.data.json
init = 2 (Default)
random
seed = 72521
output
file = /tmp/tmpxnuvtsr9/bernoullituua3ob8/bernoulli-20261117171317_1.csv
diagnostic_file = (Default)
refresh = 100 (Default)
sig_figs = 8 (Default)
profile_file = profile.csv (Default)
save_cmdstan_config = true
num_threads = 1 (Default)
Gradient evaluation took 3e-06 seconds
1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
Adjust your expectations accordingly!
Iteration: 1 / 2000 [ 0%] (Warmup)
Iteration: 100 / 2000 [ 5%] (Warmup)
Iteration: 200 / 2000 [ 10%] (Warmup)
Iteration: 300 / 2000 [ 15%] (Warmup)
Iteration: 400 / 2000 [ 20%] (Warmup)
Iteration: 500 / 2000 [ 25%] (Warmup)
Iteration: 600 / 2000 [ 30%] (Warmup)
Iteration: 700 / 2000 [ 35%] (Warmup)
Iteration: 800 / 2000 [ 40%] (Warmup)
Iteration: 900 / 2000 [ 45%] (Warmup)
Iteration: 1000 / 2000 [ 50%] (Warmup)
Iteration: 1001 / 2000 [ 50%] (Sampling)
Iteration: 1100 / 2000 [ 55%] (Sampling)
Iteration: 1200 / 2000 [ 60%] (Sampling)
Iteration: 1300 / 2000 [ 65%] (Sampling)
Iteration: 1400 / 2000 [ 70%] (Sampling)
Iteration: 1500 / 2000 [ 75%] (Sampling)
Iteration: 1600 / 2000 [ 80%] (Sampling)
Iteration: 1700 / 2000 [ 85%] (Sampling)
Iteration: 1800 / 2000 [ 90%] (Sampling)
Iteration: 1900 / 2000 [ 95%] (Sampling)
Iteration: 2000 / 2000 [100%] (Sampling)
Elapsed Time: 0.003 seconds (Warm-up)
0.009 seconds (Sampling)
0.012 seconds (Total)