Skip to content

Commit

Permalink
WIP installs without cmdstanpy however also does not fail when using …
Browse files Browse the repository at this point in the history
…pseudobatch[error_propagation]
  • Loading branch information
viktorht committed May 16, 2024
1 parent d6571d8 commit 0e8b171
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 205 deletions.
19 changes: 19 additions & 0 deletions build_testing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# remove the old virtual environment if it exists
rm -rf .venv-docker

# Create a new virtual environment
python -m venv .venv-docker

# Activate the virtual environment
source .venv-docker/bin/activate

# Install the current directory
pip install -e '.[development]'

# Run the tests
pytest

# install error_propagation
pip install '.[error_propagation]'
100 changes: 54 additions & 46 deletions pseudobatch/error_propagation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,57 @@
import shutil
import warnings
from pathlib import Path
import cmdstanpy
from pseudobatch.error_propagation.error_propagation import (
run_error_propagation,
)

STAN_FILES_FOLDER = Path(__file__).parent / "stan"
CMDSTAN_VERSION = "2.31.0"


# on Windows specifically, we should point cmdstanpy to the repackaged
# CmdStan if it exists. This lets cmdstanpy handle the TBB path for us.
local_cmdstan = STAN_FILES_FOLDER / f"cmdstan-{CMDSTAN_VERSION}"
if local_cmdstan.exists():
cmdstanpy.set_cmdstan_path(str(local_cmdstan.resolve()))


def load_stan_model(name: str) -> cmdstanpy.CmdStanModel:
"""
Try to load precompiled Stan models. If that fails,
compile them.
"""
try:
model = cmdstanpy.CmdStanModel(
exe_file=STAN_FILES_FOLDER / f"{name}.exe",
stan_file=STAN_FILES_FOLDER / f"{name}.stan",
compile=False,
)
except ValueError:
warnings.warn(f"Failed to load pre-built model '{name}.exe', compiling")
model = cmdstanpy.CmdStanModel(
stan_file=STAN_FILES_FOLDER / f"{name}.stan",
stanc_options={"O1": True},
)
shutil.copy(
model.exe_file, # type: ignore
STAN_FILES_FOLDER / f"{name}.exe",
)

return model


ERROR_PROPAGATION = load_stan_model("error_propagation")


# example: just print the info of the model
print(ERROR_PROPAGATION.exe_info())
from importlib.metadata import distribution

try:
distribution("cmdstanpy")
import cmdstanpy

cmdstanpy_installed = True
except:
cmdstanpy_installed = False

if cmdstanpy_installed:
from pseudobatch.error_propagation.error_propagation import (
run_error_propagation,
)

STAN_FILES_FOLDER = Path(__file__).parent / "stan"
CMDSTAN_VERSION = "2.31.0"

# on Windows specifically, we should point cmdstanpy to the repackaged
# CmdStan if it exists. This lets cmdstanpy handle the TBB path for us.
local_cmdstan = STAN_FILES_FOLDER / f"cmdstan-{CMDSTAN_VERSION}"
if local_cmdstan.exists():
cmdstanpy.set_cmdstan_path(str(local_cmdstan.resolve()))

def load_stan_model(name: str) -> cmdstanpy.CmdStanModel:
"""
Try to load precompiled Stan models. If that fails,
compile them.
"""
try:
model = cmdstanpy.CmdStanModel(
exe_file=STAN_FILES_FOLDER / f"{name}.exe",
stan_file=STAN_FILES_FOLDER / f"{name}.stan",
compile=False,
)
except ValueError:
warnings.warn(
f"Failed to load pre-built model '{name}.exe', compiling"
)
model = cmdstanpy.CmdStanModel(
stan_file=STAN_FILES_FOLDER / f"{name}.stan",
stanc_options={"O1": True},
)
shutil.copy(
model.exe_file, # type: ignore
STAN_FILES_FOLDER / f"{name}.exe",
)

return model

ERROR_PROPAGATION = load_stan_model("error_propagation")

# example: just print the info of the model
print(ERROR_PROPAGATION.exe_info())
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools", "wheel", "cmdstanpy>=1.0.7"]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[tool.isort]
Expand Down
Loading

0 comments on commit 0e8b171

Please sign in to comment.