diff --git a/R/R/compile.R b/R/R/compile.R index 5948da1b..8d5b414a 100644 --- a/R/R/compile.R +++ b/R/R/compile.R @@ -43,7 +43,7 @@ get_bridgestan_path <- function() { tryCatch({ verify_bridgestan_path(path) }, error = function(e) { - print(paste0("Bridgestan not found at location specified by $BRIDGESTAN ", + print(paste0("BridgeStan not found at location specified by $BRIDGESTAN ", "environment variable, downloading version ", packageVersion("bridgestan"), " to ", path)) get_bridgestan_src() diff --git a/R/R/download.R b/R/R/download.R index 72cc21ef..2991d141 100644 --- a/R/R/download.R +++ b/R/R/download.R @@ -10,7 +10,7 @@ get_bridgestan_src <- function() { dir.create(HOME_BRIDGESTAN, showWarnings = FALSE, recursive = TRUE) temp <- tempfile() - err_text <- paste("Failed to download Bridgestan", current_version, "from github.com.") + err_text <- paste("Failed to download BridgeStan", current_version, "from github.com.") for (i in 1:RETRIES) { tryCatch({ download.file(url, destfile = temp, mode = "wb", quiet = TRUE, method = "auto") diff --git a/python/bridgestan/compile.py b/python/bridgestan/compile.py index fd930b2a..527c68b0 100644 --- a/python/bridgestan/compile.py +++ b/python/bridgestan/compile.py @@ -46,7 +46,7 @@ def set_bridgestan_path(path: Union[str, os.PathLike]) -> None: os.environ["BRIDGESTAN"] = path -def get_bridgestan_path(): +def get_bridgestan_path() -> str: """ Get the path to BridgeStan. @@ -66,14 +66,14 @@ def get_bridgestan_path(): verify_bridgestan_path(path) except ValueError: print( - "Bridgestan not found at location specified by $BRIDGESTAN " + "BridgeStan not found at location specified by $BRIDGESTAN " f"environment variable, downloading version {__version__} to {path}" ) get_bridgestan_src() num_files = len(list(HOME_BRIDGESTAN.iterdir())) if num_files >= 5: warnings.warn( - f"Found {num_files} different versions of Bridgestan in {HOME_BRIDGESTAN}. " + f"Found {num_files} different versions of BridgeStan in {HOME_BRIDGESTAN}. " "Consider deleting old versions to save space." ) print("Done!") @@ -81,7 +81,7 @@ def get_bridgestan_path(): return path -def generate_so_name(model: Path): +def generate_so_name(model: Path) -> Path: name = model.stem return model.with_stem(f"{name}_model").with_suffix(".so") @@ -138,7 +138,7 @@ def compile_model( return output -def windows_dll_path_setup(): +def windows_dll_path_setup() -> None: """Add tbb.dll to %PATH% on Windows.""" global WINDOWS_PATH_SET if IS_WINDOWS and not WINDOWS_PATH_SET: @@ -180,7 +180,6 @@ def windows_dll_path_setup(): os.path.dirname(out.stdout.decode().splitlines()[0]) ) os.add_dll_directory(mingw_dir) - WINDOWS_PATH_SET &= True except: # no default location warnings.warn( diff --git a/python/bridgestan/download.py b/python/bridgestan/download.py index 1bf0a1cd..1dbdeefe 100644 --- a/python/bridgestan/download.py +++ b/python/bridgestan/download.py @@ -12,7 +12,7 @@ RETRIES = 5 -def get_bridgestan_src(): +def get_bridgestan_src() -> None: """ Download and unzip the BridgeStan source distribution for this version @@ -24,7 +24,7 @@ def get_bridgestan_src(): ) HOME_BRIDGESTAN.mkdir(exist_ok=True) - err_text = f"Failed to download Bridgestan {__version__} from github.com." + err_text = f"Failed to download BridgeStan {__version__} from github.com." for i in range(1, 1 + RETRIES): try: file_tmp, _ = urllib.request.urlretrieve(url, filename=None) diff --git a/python/bridgestan/model.py b/python/bridgestan/model.py index a12f25c7..d1a69bd5 100644 --- a/python/bridgestan/model.py +++ b/python/bridgestan/model.py @@ -134,11 +134,13 @@ def __init__( if capture_stan_prints: self._set_print_callback(_print_callback, None) - err = ctypes.pointer(ctypes.c_char_p()) - self.model = self._construct(str.encode(self.data), self.seed, err) + err = ctypes.c_char_p() + self.model = self._construct( + str.encode(self.data), self.seed, ctypes.byref(err) + ) if not self.model: - raise self._handle_error(err.contents, "bs_model_construct") + raise self._handle_error(err, "bs_model_construct") if self.model_version() != __version_info__: warnings.warn( @@ -398,7 +400,7 @@ def param_constrain( "Error: out must be same size as number of constrained parameters" ) - err = ctypes.pointer(ctypes.c_char_p()) + err = ctypes.c_char_p() rc = self._param_constrain( self.model, @@ -407,11 +409,11 @@ def param_constrain( theta_unc, out, rng_ptr, - err, + ctypes.byref(err), ) if rc: - raise self._handle_error(err.contents, "param_constrain") + raise self._handle_error(err, "param_constrain") return out def new_rng(self, seed) -> "StanRNG": @@ -447,10 +449,10 @@ def param_unconstrain( raise ValueError( f"out size = {out.size} != unconstrained params size = {dims}" ) - err = ctypes.pointer(ctypes.c_char_p()) - rc = self._param_unconstrain(self.model, theta, out, err) + err = ctypes.c_char_p() + rc = self._param_unconstrain(self.model, theta, out, ctypes.byref(err)) if rc: - raise self._handle_error(err.contents, "param_unconstrain") + raise self._handle_error(err, "param_unconstrain") return out def param_unconstrain_json( @@ -479,10 +481,10 @@ def param_unconstrain_json( f"out size = {out.size} != unconstrained params size = {dims}" ) chars = theta_json.encode("UTF-8") - err = ctypes.pointer(ctypes.c_char_p()) - rc = self._param_unconstrain_json(self.model, chars, out, err) + err = ctypes.c_char_p() + rc = self._param_unconstrain_json(self.model, chars, out, ctypes.byref(err)) if rc: - raise self._handle_error(err.contents, "param_unconstrain_json") + raise self._handle_error(err, "param_unconstrain_json") return out def log_density( @@ -505,14 +507,19 @@ def log_density( :return: The log density. :raises RuntimeError: If the C++ Stan model throws an exception. """ - lp = ctypes.pointer(ctypes.c_double()) - err = ctypes.pointer(ctypes.c_char_p()) + lp = ctypes.c_double() + err = ctypes.c_char_p() rc = self._log_density( - self.model, int(propto), int(jacobian), theta_unc, lp, err + self.model, + int(propto), + int(jacobian), + theta_unc, + ctypes.byref(lp), + ctypes.byref(err), ) if rc: - raise self._handle_error(err.contents, "log_density") - return lp.contents.value + raise self._handle_error(err, "log_density") + return lp.value def log_density_gradient( self, @@ -547,14 +554,20 @@ def log_density_gradient( out = np.zeros(shape=dims) elif out.size != dims: raise ValueError(f"out size = {out.size} != params size = {dims}") - lp = ctypes.pointer(ctypes.c_double()) - err = ctypes.pointer(ctypes.c_char_p()) + lp = ctypes.c_double() + err = ctypes.c_char_p() rc = self._log_density_gradient( - self.model, int(propto), int(jacobian), theta_unc, lp, out, err + self.model, + int(propto), + int(jacobian), + theta_unc, + ctypes.byref(lp), + out, + ctypes.byref(err), ) if rc: - raise self._handle_error(err.contents, "log_density_gradient") - return lp.contents.value, out + raise self._handle_error(err, "log_density_gradient") + return lp.value, out def log_density_hessian( self, @@ -602,22 +615,22 @@ def log_density_hessian( raise ValueError( f"out_hess size = {out_hess.size} != params size^2 = {hess_size}" ) - lp = ctypes.pointer(ctypes.c_double()) - err = ctypes.pointer(ctypes.c_char_p()) + lp = ctypes.c_double() + err = ctypes.c_char_p() rc = self._log_density_hessian( self.model, int(propto), int(jacobian), theta_unc, - lp, + ctypes.byref(lp), out_grad, out_hess, - err, + ctypes.byref(err), ) if rc: - raise self._handle_error(err.contents, "log_density_hessian") + raise self._handle_error(err, "log_density_hessian") out_hess = out_hess.reshape(dims, dims) - return lp.contents.value, out_grad, out_hess + return lp.value, out_grad, out_hess def log_density_hessian_vector_product( self, @@ -650,15 +663,22 @@ def log_density_hessian_vector_product( out = np.zeros(shape=dims) elif out.size != dims: raise ValueError(f"out size = {out.size} != params size = {dims}") - lp = ctypes.pointer(ctypes.c_double()) - err = ctypes.pointer(ctypes.c_char_p()) + lp = ctypes.c_double() + err = ctypes.c_char_p() rc = self._log_density_hvp( - self.model, int(propto), int(jacobian), theta_unc, v, lp, out, err + self.model, + int(propto), + int(jacobian), + theta_unc, + v, + ctypes.byref(lp), + out, + ctypes.byref(err), ) if rc: - raise self._handle_error(err.contents, "log_density_hessian_vector_product") + raise self._handle_error(err, "log_density_hessian_vector_product") - return lp.contents.value, out + return lp.value, out def _handle_error(self, err: ctypes.c_char_p, method: str) -> Exception: """