CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cmdstanpy

Python interface to CmdStan that provides comprehensive access to the Stan compiler and all Bayesian inference algorithms.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

installation-setup.mddocs/

Installation and Setup

Functions for installing, configuring, and managing the CmdStan installation that CmdStanPy depends on. CmdStan is the command-line interface to Stan that must be installed separately from CmdStanPy.

Capabilities

Installing CmdStan

Downloads, extracts, and builds CmdStan from GitHub releases, with support for custom versions and build configurations.

def install_cmdstan(
    version=None,
    dir=None,
    overwrite=False,
    compiler=False,
    progress=False,
    verbose=False,
    cores=1,
    interactive=False
):
    """
    Download and install CmdStan.
    
    Parameters:
    - version (str, optional): CmdStan version to install. Defaults to latest stable release
    - dir (str, optional): Directory to install CmdStan. Defaults to user home directory
    - overwrite (bool): Overwrite existing CmdStan installation
    - compiler (bool): Install C++ compiler on Windows (requires internet)
    - progress (bool): Show progress bars during download and build
    - verbose (bool): Show build output during compilation
    - cores (int): Number of cores for parallel compilation
    - interactive (bool): Enable interactive installation mode
    
    Returns:
    None
    
    Raises:
    Exception: If download or build fails
    """

Usage Example:

import cmdstanpy as csp

# Install latest version with defaults
csp.install_cmdstan()

# Install specific version with custom settings
csp.install_cmdstan(
    version="2.32.0", 
    dir="/opt/cmdstan",
    cores=4,
    verbose=True
)

Setting CmdStan Path

Configure the path to an existing CmdStan installation.

def set_cmdstan_path(path):
    """
    Set the path to CmdStan installation.
    
    Parameters:
    - path (str or PathLike): Path to CmdStan installation directory
    
    Returns:
    None
    
    Raises:
    ValueError: If path does not exist or is not a valid CmdStan installation
    """

Usage Example:

import cmdstanpy as csp

# Set path to CmdStan installation
csp.set_cmdstan_path("/usr/local/cmdstan")

# Verify the path was set correctly
print("CmdStan path:", csp.cmdstan_path())

Getting CmdStan Path

Retrieve the currently configured CmdStan installation path.

def cmdstan_path():
    """
    Get the path to CmdStan installation.
    
    Returns:
    str: Path to CmdStan installation directory
    
    Raises:
    ValueError: If CmdStan path is not set or installation not found
    """

Getting CmdStan Version

Get the version of the currently configured CmdStan installation.

def cmdstan_version():
    """
    Get CmdStan version.
    
    Returns:
    str: CmdStan version string (e.g., "2.32.0")
    
    Raises:
    RuntimeError: If CmdStan installation not found or makefile cannot be read
    """

Setting Make Environment

Configure environment variables for the make command used during model compilation.

def set_make_env(make_env):
    """
    Set environment variables for make command.
    
    Parameters:
    - make_env (dict): Dictionary of environment variable names and values
    
    Returns:
    None
    """

Usage Example:

import cmdstanpy as csp

# Set custom C++ compiler flags
csp.set_make_env({
    "CXX": "g++",
    "CXXFLAGS": "-O3 -march=native"
})

Rebuilding CmdStan

Rebuild an existing CmdStan installation, useful after updating compiler settings or system changes.

def rebuild_cmdstan(verbose=False, progress=True, cores=1):
    """
    Rebuild existing CmdStan installation.
    
    Parameters:
    - verbose (bool): Show build output during compilation
    - progress (bool): Show progress bar during build
    - cores (int): Number of cores for parallel compilation
    
    Returns:
    None
    
    Raises:
    RuntimeError: If CmdStan installation not found or build fails
    """

Usage Example:

import cmdstanpy as csp

# Rebuild with maximum parallelism
import multiprocessing
csp.rebuild_cmdstan(cores=multiprocessing.cpu_count(), verbose=True)

Common Setup Patterns

First-Time Setup

import cmdstanpy as csp

# Option 1: Automatic installation (recommended for most users)
csp.install_cmdstan()

# Option 2: Use existing installation
csp.set_cmdstan_path("/path/to/existing/cmdstan")

# Verify setup
print(f"CmdStan {csp.cmdstan_version()} installed at {csp.cmdstan_path()}")

Advanced Configuration

import cmdstanpy as csp

# Set up custom build environment
csp.set_make_env({
    "CXX": "clang++",
    "CXXFLAGS": "-O3 -flto -march=native",
    "LDFLAGS": "-flto"
})

# Install with specific configuration
csp.install_cmdstan(
    version="2.32.0",
    dir="./local-cmdstan", 
    cores=8,
    verbose=True
)

Troubleshooting Setup

import cmdstanpy as csp

# Check current configuration
csp.show_versions()

# Reinstall if needed
csp.install_cmdstan(overwrite=True, verbose=True)

# Rebuild after configuration changes
csp.rebuild_cmdstan(verbose=True)

Install with Tessl CLI

npx tessl i tessl/pypi-cmdstanpy

docs

advanced-variational.md

data-io-utilities.md

generated-quantities.md

index.md

installation-setup.md

mcmc-results.md

model-compilation.md

model-interface.md

optimization-results.md

variational-results.md

tile.json