diff --git a/.github/workflows/CI-build.yaml b/.github/workflows/CI-build.yaml new file mode 100644 index 0000000..f6146c9 --- /dev/null +++ b/.github/workflows/CI-build.yaml @@ -0,0 +1,57 @@ +name: Build and upload wheels + +on: + push: + branches: + - '*' + pull_request: + branches: + - '*' + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-2019, ubuntu-18.04, macos-10.15] + python-version: [3.6, 3.7, 3.8, 3.9] + + steps: + - uses: actions/checkout@v1 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + pip install wheel setuptools + pip install -r requirements.txt + + - name: Build wheel + run: python setup.py bdist_wheel + + - name: Install software + run: pip install --find-links=${{github.workspace}}/dist/ SimpleCRF + + - name: Test import + run: python -c "import denseCRF, maxflow" + + - name: Install deps for demos + run: pip install SimpleITK pillow matplotlib + + - name: Test maxflow demo + run: cd ${{github.workspace}}/examples && python demo_maxflow.py 0 + + - name: Test denseCRF demo + run: cd ${{github.workspace}}/examples && python demo_densecrf.py 1 + + - name: Test denseCRF 3D demo + run: cd ${{github.workspace}}/examples && python demo_densecrf.py 1 + + - name: Upload Python wheel + uses: actions/upload-artifact@v2 + with: + name: Python wheel + path: ${{github.workspace}}/dist/SimpleCRF-*.whl + if-no-files-found: error diff --git a/examples/demo_densecrf.py b/examples/demo_densecrf.py index fd60ff9..684f01f 100755 --- a/examples/demo_densecrf.py +++ b/examples/demo_densecrf.py @@ -2,6 +2,7 @@ import numpy as np from PIL import Image import matplotlib.pyplot as plt +import sys def densecrf(I, P, param): """ @@ -72,10 +73,11 @@ def demo_densecrf1(): param = (w1, alpha, beta, w2, gamma, it) lab = densecrf(Iq, prob, param) lab = Image.fromarray(lab*255) + fig = plt.figure() plt.subplot(1,3,1); plt.axis('off'); plt.imshow(I); plt.title('input image') plt.subplot(1,3,2); plt.axis('off'); plt.imshow(L); plt.title('initial label') plt.subplot(1,3,3); plt.axis('off'); plt.imshow(lab); plt.title('after dense CRF') - plt.show() + fig.savefig('./demo_result.png') def demo_densecrf2(): I = Image.open('../dependency/densecrf/examples/im3.ppm') @@ -99,21 +101,22 @@ def demo_densecrf2(): lab = densecrf(Iq, prob, param) lab = colorize_label_map(lab, color_list) lab = Image.fromarray(lab) + fig = plt.figure() plt.subplot(1,3,1); plt.axis('off'); plt.imshow(I); plt.title('input image') plt.subplot(1,3,2); plt.axis('off'); plt.imshow(L); plt.title('initial label') plt.subplot(1,3,3); plt.axis('off'); plt.imshow(lab); plt.title('after dense CRF') - plt.show() + fig.savefig('./demo_result.png') if __name__ == "__main__": print("example list") print(" 0 -- Dense CRF example for a binary segmentation") print(" 1 -- Dense CRF example for a multi-class segmentation") - print("please enter the index of an example:") - method = input() - method = "{0:}".format(method) + if len(sys.argv) == 1: + raise ValueError("Please, provide an argument.") + method = sys.argv[1] if(method == '0'): demo_densecrf1() elif(method == '1'): demo_densecrf2() else: - print("invalid number : {0:}".format(method)) \ No newline at end of file + raise ValueError("Invalid number: " + method) diff --git a/examples/demo_densecrf3d.py b/examples/demo_densecrf3d.py index fd8fa5a..5cb4648 100755 --- a/examples/demo_densecrf3d.py +++ b/examples/demo_densecrf3d.py @@ -3,6 +3,7 @@ import numpy as np from PIL import Image import matplotlib.pyplot as plt +import sys def densecrf3d(I, P, param): """ @@ -91,14 +92,12 @@ def demo_densecrf3d_2(): print("example list") print(" 0 -- 3D Dense CRF example for single-modal segmentation") print(" 1 -- 3D Dense CRF example for multi-modal segmentation") - print("please enter the index of an example:") - method = input() - method = "{0:}".format(method) + if len(sys.argv) == 1: + raise ValueError("Please, provide an argument.") + method = sys.argv[1] if(method == '0'): demo_densecrf3d_1() elif(method == '1'): demo_densecrf3d_2() else: - print("invalid number : {0:}".format(method)) - - + raise ValueError("Invalid number: " + method) diff --git a/examples/demo_maxflow.py b/examples/demo_maxflow.py index 2a02928..4eda2be 100755 --- a/examples/demo_maxflow.py +++ b/examples/demo_maxflow.py @@ -1,9 +1,9 @@ - import numpy as np import SimpleITK as sitk import maxflow from PIL import Image import matplotlib.pyplot as plt +import sys def maxflow2d(I, P, param): """ @@ -96,10 +96,11 @@ def demo_maxflow(): param = (lamda, sigma) lab = maxflow2d(Iq, Prob, param) + fig = plt.figure() plt.subplot(1,3,1); plt.axis('off'); plt.imshow(I); plt.title('input image') plt.subplot(1,3,2); plt.axis('off'); plt.imshow(fP); plt.title('initial \n segmentation') plt.subplot(1,3,3); plt.axis('off'); plt.imshow(lab); plt.title('CRF result') - plt.show() + fig.savefig('./demo_result.png') def demo_interactive_maxflow(): I = Image.open('../data/brain.png') @@ -121,10 +122,11 @@ def demo_interactive_maxflow(): param = (lamda, sigma) lab = interactive_maxflow2d(Iq, Prob, Seed, param) + fig = plt.figure() plt.subplot(1,3,1); plt.axis('off'); plt.imshow(I); plt.title('input image') plt.subplot(1,3,2); plt.axis('off'); plt.imshow(fP); plt.title('initial \n segmentation') plt.subplot(1,3,3); plt.axis('off'); plt.imshow(lab); plt.title('CRF result') - plt.show() + fig.savefig('./demo_result.png') def demo_maxflow3d(): img_name = "../data/2013_12_1_img.nii.gz" @@ -188,9 +190,9 @@ def test_interactive_max_flow3d(): print(" 1 -- 2D max flow with interactions") print(" 2 -- 3D max flow without interactions") print(" 3 -- 3D max flow with interactions") - print("please enter the index of an example:") - method = input() - method = "{0:}".format(method) + if len(sys.argv) == 1: + raise ValueError("Please, provide an argument.") + method = sys.argv[1] if(method == '0'): demo_maxflow() elif(method == '1'): @@ -200,4 +202,4 @@ def test_interactive_max_flow3d(): elif(method == '3'): test_interactive_max_flow3d() else: - print("invalid number : {0:}".format(method)) + raise ValueError("Invalid number: " + method) diff --git a/setup.py b/setup.py index de5eb04..e68a502 100755 --- a/setup.py +++ b/setup.py @@ -25,7 +25,8 @@ def finalize_options(self): 'maxflow_python/util.cpp', 'dependency/maxflow-v3.0/graph.cpp', 'dependency/maxflow-v3.0/maxflow.cpp', - maxflow_source]) + maxflow_source], + py_limited_api=False) module_name2 = 'denseCRF' densecrf_source = "densecrf_python/wrap2D_py{0:}.cpp".format(py_version) @@ -45,7 +46,8 @@ def finalize_options(self): './dependency/densecrf/src/util.cpp', './dependency/densecrf/external/liblbfgs/lib/lbfgs.c', densecrf_source, - ]) + ], + py_limited_api=False) module_name3 = 'denseCRF3D' densecrf_source = "densecrf_python/wrap3D_py{0:}.cpp".format(py_version) @@ -65,13 +67,14 @@ def finalize_options(self): './dependency/densecrf3d/src/util.cpp', './dependency/densecrf/external/liblbfgs/lib/lbfgs.c', densecrf_source, - ]) + ], + py_limited_api=False) # Get the summary description = 'An open-source toolkit for conditional random field (CRF) and dense CRF' # Get the long description -if(sys.version[0] == '2'): +if (sys.version[0] == '2'): import io with io.open('README.md', 'r', encoding='utf-8') as f: long_description = f.read() @@ -92,13 +95,16 @@ def finalize_options(self): ext_modules = [module1, module2, module3], classifiers=[ 'License :: OSI Approved :: BSD License', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3 :: Only', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', ], python_requires = '>=3.6', cmdclass={'build_ext': build_ext},) -# to build, run python stup.py build or python setup.py build_ext --inplace +# to build, run python setup.py build or python setup.py build_ext --inplace # to install, run python setup.py install