Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def cast_test(M,S,dtype):
M = M.astype(dtype)
S = S.astype(dtype)
W = mahotas.cwatershed(2-S,M)
assert sys.getrefcount(W) == 2
assert np.all(W == np.array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[2, 2, 1, 1],
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]]))
for d in [np.uint8, np.int8, np.uint16, np.int16, np.int32, np.uint32,int]:
surface = distance.max() - distance
if image.volumetric:
footprint = numpy.ones((3, 3, 3))
else:
footprint = numpy.ones((3, 3))
peaks = mahotas.regmax(distance, footprint)
if image.volumetric:
markers, _ = mahotas.label(peaks, numpy.ones((16, 16, 16)))
else:
markers, _ = mahotas.label(peaks, numpy.ones((16, 16)))
expected = mahotas.cwatershed(surface, markers)
expected = expected * binary
expected = skimage.measure.label(expected)
actual = workspace.get_objects("watershed")
actual = actual.segmented
numpy.testing.assert_array_equal(expected, actual)
def test_mix_types():
f = np.zeros((64,64), np.uint16)
f += np.indices(f.shape)[1]**2
f += (np.indices(f.shape)[0]-23)**2
markers = np.zeros((64,64), np.int64)
markers[32,32] = 1
# Below used to force a crash (at least in debug mode)
a,b = mahotas.cwatershed(f, markers, return_lines=1)
seed_mask[outside_brush_mask] = True
seed_mask[frame] = True
# seed_mask[corners] = False
seed_mask[end_points] = False
seeds,n = mh.label(seed_mask)
# remove small regions
sizes = mh.labeled.labeled_size(seeds)
min_seed_size = 5
too_small = np.where(sizes < min_seed_size)
seeds = mh.labeled.remove_regions(seeds, too_small).astype(np.uint8)
#
# run watershed
#
ws = mh.cwatershed(brush_image.max() - brush_image, seeds)
lines_array = np.zeros(ws.shape,dtype=np.uint8)
lines = []
for y in range(ws.shape[0]-1):
for x in range(ws.shape[1]-1):
# print 'looking for', seg_sub_tile[y,x]
if self.lookup_label(seg_sub_tile[y,x]) != self.label_id:
continue
if ws[y,x] != ws[y,x+1]:
lines_array[y,x] = 1
lines.append([ int(bb[0]+x), int(bb[2]+y) ])
if ws[y,x] != ws[y+1,x]:
if x.volumetric:
footprint = numpy.ones(
(self.footprint.value, self.footprint.value, self.footprint.value)
)
else:
footprint = numpy.ones((self.footprint.value, self.footprint.value))
peaks = mahotas.regmax(distance, footprint)
if x.volumetric:
markers, _ = mahotas.label(peaks, numpy.ones((16, 16, 16)))
else:
markers, _ = mahotas.label(peaks, numpy.ones((16, 16)))
y_data = mahotas.cwatershed(surface, markers)
y_data = y_data * x_data
if factor > 1:
y_data = skimage.transform.resize(
y_data, original_shape, mode="edge", order=0, preserve_range=True
)
y_data = numpy.rint(y_data).astype(numpy.uint16)
else:
markers_name = self.markers_name.value
markers = images.get_image(markers_name)
markers_data = markers.pixel_data
# seeds,n = mh.label(brush_boundary_mask)
seeds,n = mh.label(seed_mask)
print n
# remove small regions
sizes = mh.labeled.labeled_size(seeds)
min_seed_size = 5
too_small = np.where(sizes < min_seed_size)
seeds = mh.labeled.remove_regions(seeds, too_small).astype(np.uint8)
#
# run watershed
#
ws = mh.cwatershed(brush_image.max() - brush_image, seeds)
mh.imsave('/tmp/end_points.tif', 50*end_points.astype(np.uint8))
mh.imsave('/tmp/seeds_mask.tif', 50*seed_mask.astype(np.uint8))
mh.imsave('/tmp/seeds.tif', 50*seeds.astype(np.uint8))
mh.imsave('/tmp/ws.tif', 50*ws.astype(np.uint8))
lines_array = np.zeros(ws.shape,dtype=np.uint8)
lines = []
print label_id
# valid_labels = [label_id]
# while label_id in self.__merge_table.values():
# label_id = self.__merge_table.values()[]
# valid_labels.append(label_id)
grad = grad.astype(np.uint8)
# compute seeds
seeds,_ = mh.label(mask)
# remove small regions
sizes = mh.labeled.labeled_size(seeds)
min_seed_size = 5
too_small = np.where(sizes < min_seed_size)
seeds = mh.labeled.remove_regions(seeds, too_small)
#
# run watershed
#
ws = mh.cwatershed(grad, seeds)
lines_array = np.zeros(ws.shape,dtype=np.uint8)
lines = []
for y in range(ws.shape[0]-1):
for x in range(ws.shape[1]-1):
if ws[y,x] != ws[y,x+1]:
lines_array[y,x] = 1
lines.append([x,y])
if ws[y,x] != ws[y+1,x]:
lines_array[y,x] = 1
lines.append([x,y])
import mahotas as mh
from os import path
import numpy as np
from matplotlib import pyplot as plt
nuclear = mh.demos.load('nuclear')
nuclear = nuclear[:,:,0]
nuclear = mh.gaussian_filter(nuclear, 1.)
threshed = (nuclear > nuclear.mean())
distances = mh.stretch(mh.distance(threshed))
Bc = np.ones((9,9))
maxima = mh.morph.regmax(distances, Bc=Bc)
spots,n_spots = mh.label(maxima, Bc=Bc)
surface = (distances.max() - distances)
areas = mh.cwatershed(surface, spots)
areas *= threshed
import random
from matplotlib import colors
from matplotlib import cm
cols = [cm.jet(c) for c in range(0, 256, 4)]
random.shuffle(cols)
cols[0] = (0.,0.,0.,1.)
rmap = colors.ListedColormap(cols)
plt.imshow(areas, cmap=rmap)
plt.show()