Skip to content

Commit

Permalink
#16 added unittest for morphology
Browse files Browse the repository at this point in the history
  • Loading branch information
carljohnsen committed Jun 13, 2022
1 parent 5077259 commit c4c833a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/pybind_kernels/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ OPENCV_INCLUDE=$(shell pkg-config opencv4 --cflags)
OPENCV_LIB=$(shell pkg-config opencv4 --libs)

# Detect if OpenACC can be used
#ifneq (, $(shell which nvc++))
#CXX = nvc++
#CXXFLAGS += -acc=gpu -Minfo=accel -tp=native
#else
#$(info OpenACC compiler nvc++ not found. Compiling without)
#endif
ifneq (, $(shell which nvc++))
CXX = nvc++
CXXFLAGS += -acc=gpu -Minfo=accel -tp=native
else
$(info OpenACC compiler nvc++ not found. Compiling without)
endif

CXXFLAGS += -I../contrib/cpptqdm/ -Iinclude

Expand Down
50 changes: 50 additions & 0 deletions src/pybind_kernels/test/test_morphology.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'''
Unittests for the morphology pybind kernels.
'''
import sys
sys.path.append(sys.path[0]+"/../")
import cpu_seq.morphology as m_cpu_seq
import cpu.morphology as m_cpu
import gpu.morphology as m_gpu
import numpy as np
from scipy import ndimage as ndi
import pytest

# Parameters
implant_dims = 32
cross_width = 8
# TODO if implant_dims doesn't divide by radius, it doesn't work. Except for 2, which also fails.
rs = [4, 8, 16]
impls = [m_cpu_seq, m_cpu, m_gpu]
funcs = [('dilate', ndi.binary_dilation), ('erode', ndi.binary_erosion)]

def sphere(n):
xs = np.linspace(-1,1,n)
return (xs[:,np.newaxis,np.newaxis]**2 + xs[np.newaxis,:,np.newaxis]**2 + xs[np.newaxis,np.newaxis,:]**2) <= 1

@pytest.mark.parametrize('r', rs)
@pytest.mark.parametrize('m', impls)
@pytest.mark.parametrize('op,nd', funcs)
def test_morphology(r, m, op, nd):
implant_mask = np.zeros((implant_dims,implant_dims,implant_dims), dtype=np.uint8)
c = implant_dims // 2
cross_start, cross_end = c - (cross_width // 2), c + (cross_width // 2)

implant_mask[:,cross_start:cross_end,cross_start:cross_end] = True
implant_mask[cross_start:cross_end,:,cross_start:cross_end] = True
implant_mask[cross_start:cross_end,cross_start:cross_end,:] = True

result = np.empty_like(implant_mask)
f = getattr(m, f'{op}_3d_sphere')
f(implant_mask, r, result)

verification = nd(implant_mask, sphere((2*r)+1))

assert np.allclose(verification, result)

if __name__ == '__main__':
for r in rs:
for m in impls:
for op, nd in funcs:
print (f'Testing the {m.__name__} implementation of {op}')
test_morphology(r, m, op, nd)

0 comments on commit c4c833a

Please sign in to comment.