Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _py_star_dist3D(img, rays, grid=(1,1,1)):
grid = _normalize_grid(grid,3)
img = img.astype(np.uint16, copy=False)
dst_shape = tuple(s // a for a, s in zip(grid, img.shape)) + (len(rays),)
dst = np.empty(dst_shape, np.float32)
dzs, dys, dxs = rays.vertices.T
for i in range(dst_shape[0]):
for j in range(dst_shape[1]):
for k in range(dst_shape[2]):
value = img[i * grid[0], j * grid[1], k * grid[2]]
if value == 0:
dst[i, j, k] = 0
else:
for n, (dz, dy, dx) in enumerate(zip(dzs, dys, dxs)):
x, y, z = np.float32(0), np.float32(0), np.float32(0)
def test_nms(n_rays, nms_thresh):
dist = 10*np.ones((33,44,55,n_rays))
prob = np.random.uniform(0,1,dist.shape[:3])
rays = Rays_GoldenSpiral(dist.shape[-1])
points, probi, disti = non_maximum_suppression_3d(dist, prob, rays,
prob_thresh=0.9,
nms_thresh = nms_thresh,
verbose=True)
return points, rays, disti
def test_types(img, n_rays, grid):
mode = "cpp"
rays = Rays_GoldenSpiral(n_rays)
gt = star_dist3D(img, rays=rays, grid=grid, mode=mode)
for dtype in (np.int8, np.int16, np.int32,
np.uint8, np.uint16, np.uint32):
x = star_dist3D(img.astype(dtype), rays=rays, grid=grid, mode=mode)
print("test_stardist3D (mode {mode}) for shape {img.shape} and type {dtype}".format(mode =mode, img = img, dtype = dtype))
check_similar(gt, x)
def test_types_gpu(img, n_rays, grid):
mode = "opencl"
rays = Rays_GoldenSpiral(n_rays)
gt = star_dist3D(img, rays=rays, grid=grid, mode=mode)
for dtype in (np.int8, np.int16, np.int32,
np.uint8, np.uint16, np.uint32):
x = star_dist3D(img.astype(dtype), rays=rays, grid=grid, mode=mode)
print("test_stardist3D (mode {mode}) for shape {img.shape} and type {dtype}".format(mode =mode, img = img, dtype = dtype))
check_similar(gt, x)
def test_label():
n_rays = 32
dist = 20*np.ones((1,n_rays))
rays = Rays_GoldenSpiral(dist.shape[-1])
points = [[20,20,20]]
return polyhedron_to_label(dist,points, rays, shape = (33,44,55))
@pytest.mark.gpu
@pytest.mark.parametrize('img', (real_image3d()[1], random_image((33, 44, 55))))
@pytest.mark.parametrize('n_rays', (4, 16, 32))
@pytest.mark.parametrize('grid', ((1,1,1),(1,2,4)))
def test_cpu_gpu(img, n_rays, grid):
rays = Rays_GoldenSpiral(n_rays)
s_cpp = star_dist3D(img, rays=rays, grid=grid, mode="cpp")
s_ocl = star_dist3D(img, rays=rays, grid=grid, mode="opencl")
check_similar(s_cpp, s_ocl)
if __name__ == '__main__':
from utils import circle_image
rays = Rays_GoldenSpiral(4)
lbl = circle_image((64,) * 3)
a = star_dist3D(lbl, rays=rays, grid=(1, 2, 2), mode="cpp")
b = star_dist3D(lbl, rays=rays, grid=(1, 2, 2), mode="opencl")
print(np.amax(np.abs(a - b)))
def test_cpu_gpu(img, n_rays, grid):
rays = Rays_GoldenSpiral(n_rays)
s_cpp = star_dist3D(img, rays=rays, grid=grid, mode="cpp")
s_ocl = star_dist3D(img, rays=rays, grid=grid, mode="opencl")
check_similar(s_cpp, s_ocl)
def test_types_gpu(img, n_rays):
mode = "opencl"
gt = star_dist(img, n_rays=n_rays, mode=mode)
for dtype in (np.int8, np.int16, np.int32,
np.uint8, np.uint16, np.uint32):
x = star_dist(img.astype(dtype), n_rays=n_rays, mode=mode)
print("test_stardist2D with mode {mode} for shape {img.shape} and type {dtype}".format(mode =mode, img = img, dtype = dtype))
check_similar(gt, x)
def test_acc(img):
prob = edt_prob(img)
dist = star_dist(img, n_rays=32, mode="cpp")
coord = dist_to_coord(dist)
points = non_maximum_suppression(coord, prob, prob_thresh=0.4)
img2 = polygons_to_label(coord, prob, points, shape=img.shape)
m = matching(img, img2)
acc = m.accuracy
print("accuracy {acc:.2f}".format(acc=acc))
assert acc > 0.9
def test_types(img, n_rays):
mode = "cpp"
gt = star_dist(img, n_rays=n_rays, mode=mode)
for dtype in (np.int8, np.int16, np.int32,
np.uint8, np.uint16, np.uint32):
x = star_dist(img.astype(dtype), n_rays=n_rays, mode=mode)
print("test_stardist2D (mode {mode}) for shape {img.shape} and type {dtype}".format(mode =mode, img = img, dtype = dtype))
check_similar(gt, x)