Skip to content

Commit

Permalink
Merge pull request #621 from ANTsX/nanobuild-v2
Browse files Browse the repository at this point in the history
ENH: move c++ wrapping from pybind11 to nanobind
  • Loading branch information
Nicholas Cullen, PhD authored May 14, 2024
2 parents 150e6f6 + b3c95f2 commit 0903c42
Show file tree
Hide file tree
Showing 140 changed files with 3,181 additions and 3,900 deletions.
122 changes: 40 additions & 82 deletions .github/workflows/ci-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,93 +11,51 @@ on:

env:
# Updates or changes to this, or the runner OS or arch will invalidate the cache
python_version: '3.10' # Python version to use for testing - update when needed
python_version: "3.10" # Python version to use for testing - update when needed

jobs:
test:
runs-on: ubuntu-20.04
# Default shell needs to be bash for conda
# https://github.com/conda-incubator/setup-miniconda?tab=readme-ov-file#important
defaults:
run:
shell: bash -el {0}
run:
shell: bash -el {0}
steps:
- name: Setup Miniconda
uses: conda-incubator/setup-miniconda@030178870c779d9e5e1b4e563269f3aa69b04081 # v3.0.3, devs recommend using hash
with:
miniconda-version: "latest"
python-version: ${{ env.python_version }}
auto-update-conda: true
activate-environment: "test-env"

- name: Configure Conda to use only .tar.bz2
run: |
conda config --set use_only_tar_bz2 true
- name: Checkout code
uses: actions/checkout@v4 # Checkout PR code to 'antspy-pr'
with:
path: antspy-pr

- name: Load conda environment from cache if available
id: cache-env
uses: actions/cache@v4
with:
path: ~/conda-env.tar.bz2
key: >-
${{ runner.os }}-conda-${{ env.python_version }}-${{ hashFiles('antspy-pr/ants/environment.yml',
'antspy-pr/ants/requirements.txt', 'antspy-pr/ants/setup.py', 'antspy-pr/scripts/configure_ITK.sh',
'antspy-pr/scripts/configure_ANTsPy.sh', 'antspy-pr/ants/lib/*') }}
- name: Unpack cached environment
if: steps.cache-env.outputs.cache-hit == 'true'
run: |
mkdir -p ${CONDA}/envs/antspy-env
tar -xjf ~/conda-env.tar.bz2 -C ${CONDA}/envs/antspy-env
conda activate antspy-env
conda-unpack
- name: Install dependencies and ANTsPy from PR
if: steps.cache-env.outputs.cache-hit != 'true'
run: |
conda create -n antspy-env python=${{ env.python_version }} -y
conda activate antspy-env
conda install -c conda-forge conda-pack coverage
conda info
pip install ./antspy-pr
conda list
antspy-pr/tests/run_tests.sh -c
conda pack -n antspy-env -o ~/conda-env.tar.bz2
- name: Cache Conda environment
if: steps.cache-env.outputs.cache-hit != 'true'
uses: actions/cache@v4
with:
path: ~/conda-env.tar.bz2
key: >-
${{ runner.os }}-conda-${{ env.python_version }}-${{ hashFiles('antspy-pr/ants/environment.yml',
'antspy-pr/ants/requirements.txt', 'antspy-pr/ants/setup.py', 'antspy-pr/scripts/configure_ITK.sh',
'antspy-pr/scripts/configure_ANTsPy.sh', 'antspy-pr/ants/lib/*') }}) }}
- name: Replace installed ANTsPy with PR code
if: steps.cache-env.outputs.cache-hit == 'true'
run: |
conda activate antspy-env
ANTS_SITE_PACKAGES="${CONDA}/envs/antspy-env/lib/python${{ env.python_version }}/site-packages/ants"
for d in contrib core learn registration segmentation utils viz; do
rm -rf $ANTS_SITE_PACKAGES/$d;
cp -r antspy-pr/ants/$d $ANTS_SITE_PACKAGES/$d;
done
find $ANTS_SITE_PACKAGES -name '__pycache__' -exec rm -rf {} +
- name: Run tests
if: steps.cache-env.outputs.cache-hit == 'true'
run: |
conda activate antspy-env
bash antspy-pr/tests/run_tests.sh -c
- name: Coveralls
uses: coverallsapp/github-action@v2
with:
files: antspy-pr/tests/coverage.xml

- name: Setup Miniconda
uses: conda-incubator/setup-miniconda@030178870c779d9e5e1b4e563269f3aa69b04081 # v3.0.3, devs recommend using hash
with:
miniconda-version: "latest"
python-version: ${{ env.python_version }}
auto-update-conda: true
activate-environment: "test-env"

- name: Configure Conda to use only .tar.bz2
run: |
conda config --set use_only_tar_bz2 true
- name: Checkout code
uses: actions/checkout@v4 # Checkout PR code to 'antspy-pr'
with:
path: antspy-pr

- name: Install dependencies and ANTsPy from PR
run: |
conda create -n antspy-env python=${{ env.python_version }} -y
conda activate antspy-env
conda install -c conda-forge conda-pack coverage
conda info
pip install ./antspy-pr
conda list
antspy-pr/tests/run_tests.sh -c
conda pack -n antspy-env -o ~/conda-env.tar.bz2
- name: Run tests
run: |
conda activate antspy-env
bash antspy-pr/tests/run_tests.sh -c
- name: Coveralls
uses: coverallsapp/github-action@v2
with:
files: antspy-pr/tests/coverage.xml
90 changes: 90 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"files.associations": {
"iosfwd": "cpp",
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__functional_base": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tuple": "cpp",
"algorithm": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"complex": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"exception": "cpp",
"fstream": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"locale": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"utility": "cpp",
"vector": "cpp",
"random": "cpp",
"__tree": "cpp",
"any": "cpp",
"cfenv": "cpp",
"condition_variable": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"future": "cpp",
"list": "cpp",
"map": "cpp",
"numeric": "cpp",
"queue": "cpp",
"regex": "cpp",
"set": "cpp",
"unordered_set": "cpp",
"valarray": "cpp",
"variant": "cpp",
"*.in": "cpp",
"*.inc": "cpp",
"*.tmpl": "cpp"
}
}
72 changes: 72 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
cmake_minimum_required(VERSION 3.16.3...3.26)

project(ants LANGUAGES CXX)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Try to import all Python components potentially needed by nanobind
find_package(Python 3.8
REQUIRED COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)

# Import nanobind through CMake's find_package mechanism
find_package(nanobind CONFIG REQUIRED)

# TODO: make this run only if ITK + ANTs are not already built
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
execute_process(COMMAND cmd /c ${PROJECT_SOURCE_DIR}/scripts/configure_ITK.bat)
execute_process(COMMAND cmd /c ${PROJECT_SOURCE_DIR}/scripts/configure_ANTs.bat)
else()
execute_process(COMMAND bash ${PROJECT_SOURCE_DIR}/scripts/configure_ITK.sh)
execute_process(COMMAND bash ${PROJECT_SOURCE_DIR}/scripts/configure_ANTs.sh)
endif()

# ITK
set(ITK_DIR "./itkbuild")
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

# ANTS
add_library(antsUtilities STATIC src/antscore/antsUtilities.cxx src/antscore/antsCommandLineOption.cxx src/antscore/antsCommandLineParser.cxx src/antscore/ReadWriteData.cxx src/antscore/ANTsVersion.cxx)
add_library(registrationUtilities STATIC src/antscore/antsRegistrationTemplateHeader.cxx
src/antscore/antsRegistration2DDouble.cxx src/antscore/antsRegistration2DFloat.cxx
src/antscore/antsRegistration3DDouble.cxx src/antscore/antsRegistration3DFloat.cxx
src/antscore/antsRegistration4DDouble.cxx src/antscore/antsRegistration4DFloat.cxx)


add_library(imageMathUtilities STATIC src/antscore/ImageMathHelper2D.cxx src/antscore/ImageMathHelper3D.cxx src/antscore/ImageMathHelper4D.cxx)

# this may not be needed
target_link_libraries(antsUtilities ${ITK_LIBRARIES})
target_link_libraries(registrationUtilities ${ITK_LIBRARIES})
target_link_libraries(imageMathUtilities ${ITK_LIBRARIES})

nanobind_add_module(
lib
STABLE_ABI
NB_STATIC
src/main.cpp
src/antscore/antsAffineInitializer.cxx
src/antscore/antsApplyTransforms.cxx
src/antscore/antsApplyTransformsToPoints.cxx
src/antscore/antsJointFusion.cxx
src/antscore/antsRegistration.cxx
src/antscore/Atropos.cxx
src/antscore/AverageAffineTransform.cxx
src/antscore/AverageAffineTransformNoRigid.cxx
src/antscore/CreateJacobianDeterminantImage.cxx
src/antscore/DenoiseImage.cxx
src/antscore/iMath.cxx
src/antscore/KellyKapowski.cxx
src/antscore/LabelClustersUniquely.cxx
src/antscore/LabelGeometryMeasures.cxx
src/antscore/N3BiasFieldCorrection.cxx
src/antscore/N4BiasFieldCorrection.cxx
src/antscore/ResampleImage.cxx
src/antscore/ThresholdImage.cxx
src/antscore/TileImages.cxx
)
target_link_libraries(lib PRIVATE ${ITK_LIBRARIES} antsUtilities registrationUtilities imageMathUtilities)

# Install directive for scikit-build-core
install(TARGETS lib LIBRARY DESTINATION ants)
Loading

0 comments on commit 0903c42

Please sign in to comment.