Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ Thumbs.db
# Others
.ipynb_checkpoints/
__pycache__/
.python-version
.ccache/
COMPAS_Output*/
output*/
*.coverage
test_artifacts/
build/
.envrc

COMPAS
*.log
Expand Down
38 changes: 15 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
FROM ubuntu:22.04
FROM ubuntu:24.04
COPY --from=ghcr.io/prefix-dev/pixi:0.81.0-noble /usr/local/bin/pixi /bin/pixi

WORKDIR /app/COMPAS

# Prevent interactive prompts during package installation
# Install CA certificates
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y ca-certificates=20260601~24.04.1 --no-install-recommends && rm -rf /var/lib/apt/lists/*

RUN apt-get update && apt-get install -y \
g++ \
libhdf5-serial-dev \
libboost-all-dev \
libgsl-dev \
python3 \
python3-pip \
zip \
&& rm -rf /var/lib/apt/lists/*

# Install Python packages
RUN pip3 install numpy pyyaml
WORKDIR /app/COMPAS

# Copy only the source required to compile COMPAS
COPY src/ src/
# Copy only the source required to compile COMPAS and set up the python environment
COPY --parents src/ compas_python_utils/ scripts/ pyproject.toml pixi.lock README.md ./

RUN mkdir obj bin logs
RUN mkdir logs

ENV COMPAS_ROOT_DIR /app/COMPAS
# Install the locked pixi environment (C++ toolchain + native libs + python deps)
RUN pixi install --locked --environment default

# Compile COMPAS
RUN cd src && make DOCKER_BUILD=1 -j $(nproc)
# Compile COMPAS using the pixi-provided toolchain, with portable (non -march=native)
# optimisation flags suitable for a container image
RUN pixi run --environment docker build

# Run COMPAS
# CMD [ "python", "src/pythonSubmitDefault.py" ]
ENTRYPOINT [ "pixi", "run", "--environment", "docker", "--no-progress", "-q" ]
CMD [ "COMPAS" ]
31 changes: 7 additions & 24 deletions compas_python_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@

import re
from pathlib import Path

try:
from importlib.metadata import PackageNotFoundError, version as package_version
from importlib.metadata import PackageNotFoundError, version
except ImportError: # pragma: no cover
from importlib_metadata import PackageNotFoundError, version as package_version


def _version_from_changelog() -> str:
changelog_path = Path(__file__).resolve().parent.parent / "src" / "changelog.h"
version_match = re.search(
r'VERSION_STRING = ["\']([^"\']+)["\']',
changelog_path.read_text(encoding="utf-8"),
)
if not version_match:
return "0.0.0"
return ".".join(str(int(part)) for part in version_match.group(1).split("."))
from importlib_metadata import PackageNotFoundError, version


__all__ = []

__author__ = "Team COMPAS"
__email__ = "teamcompas@users.noreply.github.com"
__uri__ = "https://github.com/TeamCOMPAS/COMPAS"
__license__ = "MIT"
__description__ = "COMPAS"
__copyright__ = "Copyright 2022 COMPAS developers"
__contributors__ = "https://github.com/TeamCOMPAS/COMPAS/graphs/contributors"

try:
__version__ = package_version("compas-popsynth")
__version__ = version("compas-popsynth")
except PackageNotFoundError:
__version__ = _version_from_changelog()
try:
__version__ = version("compas-python-utils")
except PackageNotFoundError:
__version__ = "0.0.0"
10,728 changes: 10,728 additions & 0 deletions pixi.lock

Large diffs are not rendered by default.

219 changes: 219 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
[project]
name = "compas-python-utils"
authors = [
{ name = "Team COMPAS", email = "teamcompas@users.noreply.github.com" }
]
dynamic = [ "version" ]
description = "COMPAS"
license = "MIT"
readme = "README.md"
requires-python = ">=3.13"
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
]
dependencies = [
"astropy==8.0.1",
"bilby==2.8.1",
"corner==2.2.3",
"gwpy==4.0.1",
"h5py==3.16.0",
"matplotlib==3.11.1",
"numpy==2.5.1",
"pandas==3.0.5",
"pyyaml==6.0.3",
"scipy==1.18.0",
"stroopwafel==1.1.0",
"tqdm==4.67.3",
]

[project.urls]
Homepage = "https://compas.science/"
Documentation = "https://compas.science/docs"
Repository = "https://github.com/TeamCOMPAS/COMPAS"
Changelog = "https://github.com/TeamCOMPAS/COMPAS/blob/dev/src/changelog.h"

[project.optional-dependencies]
gpu = ["cupy"]
docs = [
"sphinx>=7.0.1",
"numpydoc",
"nbsphinx",
"sphinx_rtd_theme>=1.2.2",
"sphinx-tabs",
"sphinx-argparse",
"sphinx-math-dollar>=1.2",
"sphinxcontrib.bibtex",
"linuxdoc>=20210324",
"ipython",
"readthedocs-sphinx-search>=0.1.1b",
"sphinx-togglebutton",
]
full = []

[project.scripts]
compas_h5view = "compas_python_utils.h5view:main"
compas_h5copy = "compas_python_utils.h5copy:main"
compas_h5sample = "compas_python_utils.h5sample:main"
compas_plot_detailed_evolution = "compas_python_utils.detailed_evolution_plotter.plot_detailed_evolution:main"
compas_run = "compas_python_utils.compas_runner:main"
compas_run_submit = "compas_python_utils.preprocessing.runSubmit:main"
compas_sample_stroopwafel = "compas_python_utils.preprocessing.stroopwafelInterface:main"
compas_sample_moe_di_stefano = "compas_python_utils.preprocessing.sampleMoeDiStefano:main"
compas_fast_cosmic_integrator = "compas_python_utils.cosmic_integration.FastCosmicIntegration:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

# Read version from src/changelog.h, stripping leading zeros
[tool.hatch.version]
source = "regex"
path = "src/changelog.h"
pattern = '''.*VERSION_STRING\s*=\s*["\'](?P<version>\d+\.\d+\.\d+)["\']'''


# Explicit, so the Docker build (which COPYs only a subset of the tree, with
# no .git present) packages the same non-.py data files setup.py's
# package_data did, regardless of hatchling's default VCS-aware file
# selection.
[tool.hatch.build.targets.wheel]
packages = ["compas_python_utils"]
include = [
"compas_python_utils/bundled/*",
"compas_python_utils/bundled/*/*.sh",
"compas_python_utils/bundled/*/bin/*",
"compas_python_utils/bundled/*/lib/*",
"compas_python_utils/preprocessing/*.txt",
"compas_python_utils/preprocessing/*.yaml",
"compas_python_utils/detailed_evolution_plotter/van_den_heuvel_figures/*",
"compas_python_utils/cosmic_integration/SNR_Grid*",
]

# ---------------------------------------------------------------------------
# Pixi configuration
# ---------------------------------------------------------------------------
# Pixi solves both the conda-forge (C++ toolchain / native libs) and PyPI
# (python package) dependency graphs together and locks them into pixi.lock.
# Run `pixi lock` once (and commit pixi.lock) after editing this file.

[tool.pixi.workspace]
channels = ["conda-forge"]
platforms = ["linux-64", "linux-aarch64", "osx-arm64", "osx-64"]

# Install this package itself (compas_python_utils) into the environment,
# editable, same role uv.lock + `uv sync` played before.
[tool.pixi.pypi-dependencies]
compas-python-utils = { path = ".", editable = true }

# Native/C++ build dependencies, replacing the apt-get packages
# (g++, libhdf5-serial-dev, libboost-all-dev, libgsl-dev, make, zip).
[tool.pixi.dependencies]
cxx-compiler = ">=1.11.0,<2" # portable g++/clang toolchain
make = ">=4.4.1,<5"
gsl = ">=2.8,<3"
boost-cpp = ">=1.85.0,<2"
hdf5 = ">=2.1.0,<3"
zlib = ">=1.3.1,<2"
zip = ">=3.0,<4"

# Point the Makefile's include/lib search paths at the pixi/conda env prefix
# instead of the system defaults (/include, /lib, /usr/include/hdf5/serial...).
[tool.pixi.activation.env]
COMPAS_ROOT_DIR = "$PIXI_PROJECT_ROOT"
GSLINCDIR = "$CONDA_PREFIX/include"
GSLLIBDIR = "$CONDA_PREFIX/lib"
BOOSTINCDIR = "$CONDA_PREFIX/include"
BOOSTLIBDIR = "$CONDA_PREFIX/lib"
HDF5INCDIR = "$CONDA_PREFIX/include"
HDF5LIBDIR = "$CONDA_PREFIX/lib"
DOCKER_REPO = "teamcompas/compas"
DOCKER_BUILDER = "compas-builder"
DOCKER_MOUNTS = "-v $(pwd)/compas-input:/app/COMPAS/config -v $(pwd)/compas-logs:/app/COMPAS/logs"

### Pixi tasks: helper commands for building & running COMPAS ###

# Print the current package version, as assigned to compas_python_utils
[tool.pixi.tasks.version]
cmd = "python -c 'import compas_python_utils; print(compas_python_utils.__version__)'"

# Build COMPAS, using the number of threads determined to fit within current resources
[tool.pixi.tasks.build]
cmd = "cd src/ && make -j\"$(../scripts/job_limit.sh {{ mem_per_job }})\""
args = [{ arg = "mem_per_job", default = "2" }]

# Clean up the locally-built binary
[tool.pixi.tasks.clean]
cmd = "cd src/ && make clean"

# Build the docker image, which builds the COMPAS binary internally
[tool.pixi.tasks.docker-build]
cmd = """sh -c '
docker builder create --driver docker-container --name $DOCKER_BUILDER >/dev/null 2>&1 || true
docker build --builder $DOCKER_BUILDER --tag $DOCKER_REPO:{{ tag }} --load .
'"""
args = [{ arg = "tag", default = "latest" }]

# Clean up docker artifacts related to this build
[tool.pixi.tasks.docker-clean]
cmd = """sh -c '
IMAGE="$DOCKER_REPO:{{ tag }}"
CONTAINERS=( $(docker ps -aq --filter "ancestor=$IMAGE") )
[ -n "$CONTAINERS" ] && docker container rm -f $CONTAINERS
docker image rm "$IMAGE"
docker image prune -f
docker builder rm $DOCKER_BUILDER
'"""
args = [{ arg = "tag", default = "latest" }]

# Run a shell in the docker image, where you can run COMPAS or explore the filesystem
[tool.pixi.tasks.docker-shell]
cmd = "docker run --rm -it $DOCKER_MOUNTS --entrypoint bash teamcompas/compas"

# Run COMPAS in docker explicitly, with arguments passed from the command line
[tool.pixi.tasks.docker-compas]
cmd = "bash -c 'docker run --rm -it $DOCKER_MOUNTS teamcompas/compas compas \"$@\" --output-path /app/COMPAS/logs' --"

# Try to run COMPAS with a local binary first; if it is not present, run it from the docker image
[tool.pixi.tasks.compas]
cmd = """bash -c 'which COMPAS >/dev/null && \
COMPAS \"$@\" || \
docker run --rm -it $DOCKER_MOUNTS teamcompas/compas compas \"$@\" --output-path /app/COMPAS/logs' --"""


### Optional features ###

# `pixi run -e gpu ...` installs the extra cupy dependency on top of default.
[tool.pixi.feature.gpu.pypi-dependencies]
cupy = "*"

# `pixi run -e docker build` adds DOCKER_BUILD=1 to the environment
[tool.pixi.feature.docker.activation.env]
DOCKER_BUILD = "1"

[tool.pixi.feature.dev.dependencies]
hatch = "*"
pytest = "*"
pytest-cov = "*"
pre-commit = "*"
flake8 = "*"
black = "*"
isort = "*"
coverage-badge = "*"
deepdiff = "*"
jupytext = "*"
# jupyter-autotime = "*"
memory_profiler = "*"
nbconvert = "*"
ipykernel = "*"

[tool.pixi.environments]
default = { features = ["dev"], solve-group = "default" }
gpu = { features = ["gpu"], solve-group = "default" }
docker = { features = ["docker"], solve-group = "default" }
30 changes: 30 additions & 0 deletions scripts/job_limit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# This is a simple script to help determine a reasonable number of parallel jobs
# to run on a machine with CPU or memory limitations. It takes one optional argument:
# a user's estimate of the maximum memory used by the job, for which 2GB is chosen
# if no value is provided.
#
# Currently this is used during docker compilation.

if [ $# -gt 0 ]; then
JOB_MEM_SIZE=$1
else
JOB_MEM_SIZE=2
fi

UNAME_S=$(uname -s)

if [ "$UNAME_S" = "Darwin" ]; then
# macOS has neither `free` nor `nproc`; hw.memsize/hw.ncpu via sysctl are
# the portable equivalents.
TOTAL_MEM_GB=$(($(sysctl -n hw.memsize) / 1024 / 1024 / 1024))
CPU_JOBS=$(sysctl -n hw.ncpu)
else
TOTAL_MEM_GB=$(free -g | awk '/^Mem:/{print $2}')
CPU_JOBS=$(nproc)
fi

MEM_JOBS=$((TOTAL_MEM_GB / JOB_MEM_SIZE))
MEM_JOBS=$((MEM_JOBS < 1 ? 1 : MEM_JOBS))
echo $((MEM_JOBS < CPU_JOBS ? MEM_JOBS : CPU_JOBS))
Loading