Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_morphology_fft_dilate_2D(self):
im = self.im[:, :, 50]
truth = spim.binary_dilation(im, structure=disk(3))
test = ps.tools.fftmorphology(im, strel=disk(3), mode='dilation')
assert sp.all(truth == test)
def test_morphology_fft_dilate_3D(self):
im = self.im
truth = spim.binary_dilation(im, structure=ball(3))
test = ps.tools.fftmorphology(im, strel=ball(3), mode='dilation')
assert sp.all(truth == test)
def test_morphology_fft_closing_3D(self):
im = self.im
truth = spim.binary_closing(im, structure=ball(3))
test = ps.tools.fftmorphology(im, strel=ball(3), mode='closing')
assert sp.all(truth == test)
def test_morphology_fft_closing_2D(self):
im = self.im[:, :, 50]
truth = spim.binary_closing(im, structure=disk(3))
test = ps.tools.fftmorphology(im, strel=disk(3), mode='closing')
assert sp.all(truth == test)
# bothered to figure it out programmatically right now
# TODO: Ideally the spheres should be added periodically
print(78*'―')
print('RSA: Adding spheres of size ' + str(radius))
d2 = len(im.shape) == 2
mrad = 2*radius
if d2:
im_strel = ps_disk(radius)
mask_strel = ps_disk(mrad)
else:
im_strel = ps_ball(radius)
mask_strel = ps_ball(mrad)
if sp.any(im > 0):
# Dilate existing objects by im_strel to remove pixels near them
# from consideration for sphere placement
mask = ps.tools.fftmorphology(im > 0, im_strel > 0, mode='dilate')
mask = mask.astype(int)
else:
mask = sp.zeros_like(im)
if mode == 'contained':
mask = _remove_edge(mask, radius)
elif mode == 'extended':
pass
elif mode == 'periodic':
raise Exception('Periodic edges are not implemented yet')
else:
raise Exception('Unrecognized mode: ' + mode)
vf = im.sum()/im.size
free_spots = sp.argwhere(mask == 0)
i = 0
while vf <= volume_fraction and len(free_spots) > 0:
choice = sp.random.randint(0, len(free_spots), size=1)
strel = ps_disk
else:
strel = ps_ball
if mode == 'mio':
pw = int(sp.floor(dt.max()))
impad = sp.pad(im, mode='symmetric', pad_width=pw)
inletspad = sp.pad(inlets, mode='symmetric', pad_width=pw)
inlets = sp.where(inletspad)
# sizes = sp.unique(sp.around(sizes, decimals=0).astype(int))[-1::-1]
imresults = sp.zeros(sp.shape(impad))
for r in tqdm(sizes):
imtemp = fftmorphology(impad, strel(r), mode='erosion')
if access_limited:
imtemp = trim_disconnected_blobs(imtemp, inlets)
imtemp = fftmorphology(imtemp, strel(r), mode='dilation')
if sp.any(imtemp):
imresults[(imresults == 0)*imtemp] = r
imresults = extract_subsection(imresults, shape=im.shape)
elif mode == 'dt':
inlets = sp.where(inlets)
imresults = sp.zeros(sp.shape(im))
for r in tqdm(sizes):
imtemp = dt >= r
if access_limited:
imtemp = trim_disconnected_blobs(imtemp, inlets)
if sp.any(imtemp):
imtemp = spim.distance_transform_edt(~imtemp) < r
imresults[(imresults == 0)*imtemp] = r
elif mode == 'hybrid':
inlets = sp.where(inlets)
imresults = sp.zeros(sp.shape(im))
sizes = sp.unique(sizes)[-1::-1]
if im.ndim == 2:
strel = ps_disk
else:
strel = ps_ball
if mode == 'mio':
pw = int(sp.floor(dt.max()))
impad = sp.pad(im, mode='symmetric', pad_width=pw)
inletspad = sp.pad(inlets, mode='symmetric', pad_width=pw)
inlets = sp.where(inletspad)
# sizes = sp.unique(sp.around(sizes, decimals=0).astype(int))[-1::-1]
imresults = sp.zeros(sp.shape(impad))
for r in tqdm(sizes):
imtemp = fftmorphology(impad, strel(r), mode='erosion')
if access_limited:
imtemp = trim_disconnected_blobs(imtemp, inlets)
imtemp = fftmorphology(imtemp, strel(r), mode='dilation')
if sp.any(imtemp):
imresults[(imresults == 0)*imtemp] = r
imresults = extract_subsection(imresults, shape=im.shape)
elif mode == 'dt':
inlets = sp.where(inlets)
imresults = sp.zeros(sp.shape(im))
for r in tqdm(sizes):
imtemp = dt >= r
if access_limited:
imtemp = trim_disconnected_blobs(imtemp, inlets)
if sp.any(imtemp):
imtemp = spim.distance_transform_edt(~imtemp) < r
imresults[(imresults == 0)*imtemp] = r