-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from biosustain/42-make-pseudobatch-installati…
…on-independent-of-cmdstan 42 make pseudobatch installation independent of cmdstan
- Loading branch information
Showing
15 changed files
with
289 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,8 @@ | ||
import shutil | ||
import warnings | ||
from pathlib import Path | ||
from .import_from_excel import process_excel_template | ||
# from .data_correction import pseudobatch_transform_multiple, pseudobatch_transform, pseudobatch_transform_pandas, accumulated_dilution_factor, convert_volumetric_rates_from_pseudo_to_real, pseudobatch_transform_pandas_by_group | ||
import cmdstanpy | ||
|
||
from pseudobatch.data_correction import ( | ||
pseudobatch_transform, | ||
pseudobatch_transform_multiple, | ||
pseudobatch_transform_pandas, | ||
hypothetical_concentration, | ||
metabolised_amount, | ||
) | ||
from pseudobatch.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 .import_from_excel import process_excel_template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import shutil | ||
import warnings | ||
from pathlib import Path | ||
from importlib.metadata import distribution | ||
|
||
# The error propagation module requires to be installed separately. | ||
# Cmdstanpy is here used as an indication of whether the error | ||
# propagation module is installed or not. If cmdstanpy is installed, | ||
# the error propagation module will be loaded. | ||
try: | ||
distribution("cmdstanpy") | ||
import cmdstanpy | ||
|
||
cmdstanpy_installed = True | ||
except: | ||
cmdstanpy_installed = False | ||
raise ImportError( | ||
"cmdstanpy is not installed. To use the error propagation module, " | ||
"please install cmdstanpy by running 'pip install cmdstanpy'. " | ||
"Then install the CmdStan binaries by running 'cmdstanpy.install_cmdstan()'. " | ||
"Finally, install the remaining dependencies for the error propagation module " | ||
"by running 'pip install pseudobatch[error_propagation]'." | ||
) | ||
|
||
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.