Skip to content

Commit

Permalink
Moving dense_basis install to an optional dependency (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlovell authored Feb 12, 2025
2 parents 60542de + daef245 commit fd8020b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
# is not set up properly and depends on numpy and cython, the former
# we depend on, the latter we need to install)
pip install cython
WITH_OPENMP=1 pip install .[bluetides,eagle,illustris]
WITH_OPENMP=1 pip install .[bluetides,eagle,illustris,dense_basis]
# Output the compilation report so it can be viewed in an action log
cat build_synth.log
- uses: astral-sh/ruff-action@v1 # Lint with Ruff
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install wheel
pip install -e .[docs]
pip install -e .[docs,dense_basis]
sudo apt install pandoc
- name: Download test data
run: |
Expand Down
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ dependencies = [
"tqdm",
"requests",
"pathos",
"dense_basis@git+https://github.com/kartheikiyer/dense_basis",
"statsmodels",
]

Expand Down Expand Up @@ -136,6 +135,13 @@ illustris = [
"illustris_python @ git+https://github.com/illustristng/illustris_python.git@master",
]

# Optional dependency for using dense_basis (note that we
# currently have to use the main branch since the PyPi version
# hasn't been updated with a fix we required)
dense_basis = [
"dense_basis@git+https://github.com/kartheikiyer/dense_basis",
]


# Project urls
[project.urls]
Expand Down
29 changes: 19 additions & 10 deletions src/synthesizer/parametric/sf_hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@
from synthesizer.utils.stats import weighted_mean, weighted_median
from synthesizer.warnings import warn

# This import is in quarantine because it insists on printing
# "Starting dense_basis" to the console on import! It can stay here until
# a PR is raised to fix this.
original_stdout = sys.stdout
sys.stdout = io.StringIO()
import dense_basis as db

sys.stdout = original_stdout

# Define a list of the available parametrisations
parametrisations = (
"Constant",
Expand Down Expand Up @@ -750,7 +741,7 @@ def _convert_db_to_sfh(
self, interpolator="gp_george", min_age=5, max_age=10.3
):
"""
Convert dense basis representation to a binned SFH
Convert dense basis representation to a binned SFH.
Args:
interpolator (string)
Expand All @@ -762,6 +753,24 @@ def _convert_db_to_sfh(
max_age (float)
maximum age of SFH grid
"""
# Attempt to import dense_basis, if missing they need to
# install the optional dependency
try:
# This import is in quarantine because it insists on printing
# "Starting dense_basis" to the console on import! It can stay here
# until a PR is raised to fix this.
original_stdout = sys.stdout
sys.stdout = io.StringIO()
import dense_basis as db

sys.stdout = original_stdout
except ImportError:
raise exceptions.UnmetDependency(
"dense_basis not found. Please install Synthesizer with "
" dense_basis support by running `pip install "
".[dense_basis]`"
)

# Convert the dense basis tuple arguments to sfh in mass fraction units
tempsfh, temptime = db.tuple_to_sfh(
self.db_tuple, self.redshift, interpolator=interpolator, vb=False
Expand Down

0 comments on commit fd8020b

Please sign in to comment.