Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
simeks committed Oct 2, 2019
2 parents 4d2620c + 865b0a8 commit dc3c59d
Show file tree
Hide file tree
Showing 23 changed files with 908 additions and 148 deletions.
29 changes: 19 additions & 10 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,27 @@ build:
matrix:
fast_finish: true

before_build:
- cmd: if "%PLATFORM%"=="x86" call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat"
- cmd: if "%PLATFORM%"=="x64" call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
- mkdir build
- cd build
environment:
matrix:
- PYTHON: "C:\\Python36-x64"

install:
- git submodule update --init --recursive
- set PATH=%PYTHON%;%PYTHON%\Scripts;%PATH%
- pip install -r requirements.txt

- cmd: if "%PLATFORM%"=="x86" set GENERATOR="Visual Studio 15 2017"
- cmd: if "%PLATFORM%"=="x64" set GENERATOR="Visual Studio 15 2017 Win64"
- cmd: cmake .. -DSTK_BUILD_TESTS=1 -DSTK_WARNINGS_ARE_ERRORS=1 -DCMAKE_BUILD_TYPE=%CONFIGURATION% -G%GENERATOR%
before_build:
- mkdir build
- cd build
- cmake .. -DCMAKE_BUILD_TYPE=%CONFIGURATION% -DCMAKE_GENERATOR_PLATFORM=%PLATFORM% -DSTK_BUILD_TESTS=1 -DSTK_WARNINGS_ARE_ERRORS=1

build_script:
- cmd: cmake --build . --config %CONFIGURATION%
- cmake --build . --config %CONFIGURATION%
- cd ..
- python setup.py install -DCMAKE_GENERATOR_PLATFORM=%PLATFORM%

test_script:
- ctest --output-on-failure
- cd build
- ctest --output-on-failure
- cd ..
- python -m unittest discover test
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
build_debug/
build_vs/
build/
dist/
*.egg-info
__pycache__
.env/
.vscode/
.ycm_extra_conf.py
.ycm_extra_conf.py
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "third_party/pybind11"]
path = third_party/pybind11
url = https://github.com/pybind/pybind11.git
59 changes: 39 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,56 @@
language: cpp
language: python
python:
- 3.6

matrix:
fast_finish: true
include:
- os: linux
- name: trusty
os: linux
dist: trusty
- os: linux
env:
- PYTHON_EXECUTABLE=~/virtualenv/python3.6/bin/python
- name: xenial
os: linux
dist: xenial
- os: linux
env:
- PYTHON_EXECUTABLE=~/virtualenv/python3.6/bin/python
- name: g++8
os: linux
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- ubuntu-toolchain-r-test
packages:
- g++-6
env:
- MATRIX_EVAL="CC=gcc-6 && CXX=g++-6"
- os: linux
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-7
env:
- MATRIX_EVAL="CC=gcc-7 && CXX=g++-7"
- g++-8
before_install:
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 90
- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 90
env:
- PYTHON_EXECUTABLE=~/virtualenv/python3.6/bin/python
- name: osx
os: osx
language: generic
before_install:
- brew install llvm libomp
- python3 -m pip install virtualenv
- virtualenv venv -p python3
- source venv/bin/activate
env:
- PYTHON_EXECUTABLE=../venv/bin/python
- HOMEBREW_NO_AUTO_UPDATE=1
- CC=/usr/local/opt/llvm/bin/clang
- CXX=/usr/local/opt/llvm/bin/clang++

install:
- pip install -r requirements.txt

script:
- mkdir build
- cd build
- cmake .. -DCMAKE_BUILD_TYPE=Release -DSTK_BUILD_TESTS=1 -DSTK_WARNINGS_ARE_ERRORS=1
- cmake .. -DCMAKE_BUILD_TYPE=Release -DSTK_BUILD_TESTS=1
-DSTK_BUILD_PYTHON_WRAPPER=ON -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE
- cmake --build . --config Release
- ctest --output-on-failure
- ctest --output-on-failure
- cd ..
- python setup.py install
- python -m unittest discover test
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.8)

option(STK_BUILD_EXAMPLES "Build examples" ON)
option(STK_BUILD_TESTS "Build unit tests" ON)
option(STK_BUILD_PYTHON_WRAPPER "Build Python wrapper" OFF)
option(STK_USE_CUDA "Enables CUDA support" OFF)
option(STK_WARNINGS_ARE_ERRORS "Warnings are treated as errors" OFF)
option(STK_BUILD_WITH_DEBUG_INFO "Includes debug info in release builds" OFF)
Expand Down Expand Up @@ -56,7 +57,9 @@ if (MSVC)
set(EXTRA_FLAGS "${extra_flags} /fp:fast")
endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(EXTRA_FLAGS "-Wall -Wextra -pedantic -Wno-missing-field-initializers")
set(EXTRA_FLAGS "-Wall -Wextra -pedantic -Wno-missing-field-initializers -Wno-attributes")

# -Wno-attributes thanks to pybind11

if (STK_WARNINGS_ARE_ERRORS)
set(EXTRA_FLAGS "${extra_flags} -Werror")
Expand Down Expand Up @@ -88,6 +91,7 @@ endif()
if (STK_USE_CUDA)
add_definitions(-DSTK_USE_CUDA)
enable_language(CUDA)
set(CMAKE_CUDA_STANDARD 11)

if (STK_ENABLE_FAST_MATH)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use_fast_math")
Expand All @@ -109,10 +113,11 @@ set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${EXTRA_FLAGS_RELEASE}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EXTRA_LINK_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${EXTRA_LINK_FLAGS_RELEASE}")

include_directories(src)

# TODO: Determine QNANHIBIT for NrrdIO
set(QNANHIBIT 1)


if(NOT WIN32)
find_package(ZLIB QUIET)
find_package(NIFTI QUIET)
Expand Down
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
recursive-include src *
recursive-include third_party *
include cmake/*
include CMakeLists.txt
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,37 @@ CMake was not compatible with the one required by CUDA, it is possible to
specify a different executable with `-DCMAKE_CUDA_FLAGS="-ccbin gcc-XX"`, where
`gcc-XX` is a version of `gcc` compatible with your CUDA version.

# Python API

A minimalistic Python API is also provided.

## Install

```bash
python setup.py install
```

## Example usage

```python
import stk
import numpy as np

# Create volume directly from numpy
vol = stk.Volume(np.zeros((5,5,5)).astype(np.float32), spacing=(2,2,2))
# or read volume from file
vol = stk.read_volume('test.nrrd')

# Modify data (numpy array points to the volume data)
data = np.array(vol, copy=False)
data[0:10] = 0.0

# Access meta data
vol.origin = (2, 2, 2)
vol.spacing = (3, 3, 3)

# Write volume
stk.write_volume('test-out.nrrd', vol)
```


1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numpy
91 changes: 91 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import os
import sys
import platform
import subprocess

from pprint import pprint

from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext

with open("README.md", encoding="utf-8") as f:
readme = f.read()

# Parse command line flags
flags = {k: 'OFF' for k in ['--debug', '--use-cuda', '--use-itk']}
for flag in flags.keys():
if flag in sys.argv:
flags[flag] = 'ON'
sys.argv.remove(flag)

# Command line flags forwarded to CMake
cmake_cmd_args = []
for f in sys.argv:
if f.startswith('-D'):
cmake_cmd_args.append(f)

for f in cmake_cmd_args:
sys.argv.remove(f)


class CMakeExtension(Extension):
def __init__(self, name, cmake_lists_dir=''):
Extension.__init__(self, name, sources=[])
self.cmake_lists_dir = os.path.abspath(cmake_lists_dir)


class CMakeBuild(build_ext):
def run(self):
try:
subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError('Cannot find CMake executable')

for ext in self.extensions:
self.build_extension(ext)

def build_extension(self, ext):

extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cfg = 'Debug' if flags['--debug'] == 'ON' else 'Release'
build_args = ['--config', cfg]

cmake_args = [
'-DSTK_BUILD_PYTHON_WRAPPER=ON',
'-DSTK_BUILD_TESTS=OFF',
'-DSTK_BUILD_WITH_DEBUG_INFO=%s' % ('ON' if cfg == 'Debug' else 'OFF'),
'-DCMAKE_BUILD_TYPE=%s' % cfg,
'-DSTK_USE_CUDA=%s' % flags['--use-cuda'],
'-DSTK_ITK_BRIDGE=%s' % flags['--use-itk'],
'-DPYTHON_EXECUTABLE=' + sys.executable,
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
]

if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]

cmake_args += cmake_cmd_args
pprint(cmake_args)

if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)

subprocess.check_call(['cmake', ext.cmake_lists_dir] + cmake_args, cwd=self.build_temp)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)


setup(
name='python-stk',
version='0.4',
author='Simon Ekström',
author_email='',
description='',
long_description=readme,
long_description_content_type='text/markdown',
install_requires=['numpy'],
packages=['stk'],
ext_modules=[CMakeExtension('_stk', '.')],
cmdclass={'build_ext': CMakeBuild},
zip_safe=False,
)

Loading

0 comments on commit dc3c59d

Please sign in to comment.