|
| 1 | +.. py:currentmodule:: cmdstanpy |
| 2 | +
|
| 3 | +Controlling Outputs |
| 4 | +=================== |
| 5 | + |
| 6 | +CSV File Outputs |
| 7 | +---------------- |
| 8 | + |
| 9 | +Underlyingly, the CmdStan outputs are a set of per-chain |
| 10 | +`Stan CSV files <https://mc-stan.org/docs/cmdstan-guide/stan-csv.html#mcmc-sampler-csv-output>`__. |
| 11 | +The filenames follow the template '<model_name>-<YYYYMMDDHHMMSS>-<chain_id>' |
| 12 | +plus the file suffix '.csv'. |
| 13 | +CmdStanPy also captures the per-chain console and error messages. |
| 14 | + |
| 15 | +.. ipython:: python |
| 16 | +
|
| 17 | + import os |
| 18 | + from cmdstanpy import CmdStanModel |
| 19 | + stan_file = os.path.join('users-guide', 'examples', 'bernoulli.stan') |
| 20 | + model = CmdStanModel(stan_file=stan_file) |
| 21 | +
|
| 22 | + data_file = os.path.join('users-guide', 'examples', 'bernoulli.data.json') |
| 23 | + fit = model.sample(data=data_file) |
| 24 | +
|
| 25 | + # printing the object reports sampler commands, output files |
| 26 | + print(fit) |
| 27 | +
|
| 28 | +The ``output_dir`` argument is an optional argument which specifies |
| 29 | +the path to the output directory used by CmdStan. |
| 30 | +If this argument is omitted, the output files are written |
| 31 | +to a temporary directory which is deleted when the current Python session is terminated. |
| 32 | + |
| 33 | +.. ipython:: python |
| 34 | +
|
| 35 | + fit = model.sample(data=data_file, output_dir="./outputs/") |
| 36 | +
|
| 37 | + !ls outputs/ |
| 38 | +
|
| 39 | +Alternatively, the :meth:`~CmdStanMCMC.save_csvfiles` function moves the CSV files |
| 40 | +to a specified directory. |
| 41 | + |
| 42 | +.. ipython:: python |
| 43 | +
|
| 44 | + fit = model.sample(data=data_file) |
| 45 | + fit.save_csvfiles(dir='some/path') |
| 46 | +
|
| 47 | + !ls some/path |
| 48 | +
|
| 49 | +.. ipython:: python |
| 50 | + :suppress: |
| 51 | +
|
| 52 | + !rm -rf outputs/ some/path/ |
| 53 | +
|
| 54 | +Logging |
| 55 | +------- |
| 56 | + |
| 57 | +You may notice CmdStanPy can produce a lot of output when it is running: |
| 58 | + |
| 59 | +.. ipython:: python |
| 60 | +
|
| 61 | + fit = model.sample(data=data_file, show_progress=False) |
| 62 | +
|
| 63 | +This output is managed through the built-in :mod:`logging` module. For example, it can be disabled entirely: |
| 64 | + |
| 65 | +.. ipython:: python |
| 66 | +
|
| 67 | + import logging |
| 68 | + cmdstanpy_logger = logging.getLogger("cmdstanpy") |
| 69 | + cmdstanpy_logger.disabled = True |
| 70 | + # look, no output! |
| 71 | + fit = model.sample(data=data_file, show_progress=False) |
| 72 | +
|
| 73 | +Or one can remove the logging handler that CmdStanPy installs by default and install their own for more |
| 74 | +fine-grained control. For example, the following code sends all logs (including the ``DEBUG`` logs, which are hidden by default), |
| 75 | +to a file. |
| 76 | + |
| 77 | +DEBUG logging is useful primarily to developers or when trying to hunt down an issue. |
| 78 | + |
| 79 | +.. ipython:: python |
| 80 | +
|
| 81 | + cmdstanpy_logger.disabled = False |
| 82 | + # remove all existing handlers |
| 83 | + cmdstanpy_logger.handlers = [] |
| 84 | +
|
| 85 | + cmdstanpy_logger.setLevel(logging.DEBUG) |
| 86 | + handler = logging.FileHandler('all.log') |
| 87 | + handler.setLevel(logging.DEBUG) |
| 88 | + handler.setFormatter( |
| 89 | + logging.Formatter( |
| 90 | + '%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| 91 | + "%H:%M:%S", |
| 92 | + ) |
| 93 | + ) |
| 94 | + cmdstanpy_logger.addHandler(handler) |
| 95 | +
|
| 96 | +Now, if we run the model and check the contents of the file, we will see all the possible logging. |
| 97 | + |
| 98 | +.. ipython:: python |
| 99 | +
|
| 100 | + fit = model.sample(data=data_file, show_progress=False) |
| 101 | +
|
| 102 | + with open('all.log','r') as logs: |
| 103 | + for line in logs.readlines(): |
| 104 | + print(line.strip()) |
| 105 | +
|
| 106 | +.. ipython:: python |
| 107 | + :suppress: |
| 108 | +
|
| 109 | + !rm all.log |
0 commit comments