示例#1
0
def test_stardistdata():
    from stardist.models import StarDistData2D
    img, mask = real_image2d()
    s = StarDistData2D([img, img], [mask, mask],
                       batch_size=1, patch_size=(30, 40), n_rays=32, length=1)
    (img,), (prob, dist) = s[0]
    return (img,), (prob, dist), s
示例#2
0
def test_stardistdata_sequence():
    from stardist.models import StarDistData2D
    from csbdeep.utils.tf import keras_import
    Sequence = keras_import('utils', 'Sequence')

    x = np.zeros((100, 100), np.uint16)
    x[10:-10, 10:-10] = 1

    class MyData(Sequence):
        def __init__(self, dtype):
            self.dtype = dtype

        def __getitem__(self, n):
            return ((1 + n) * x).astype(self.dtype)

        def __len__(self):
            return 1000

    X = MyData(np.float32)
    Y = MyData(np.uint16)
    s = StarDistData2D(X,
                       Y,
                       batch_size=1,
                       patch_size=(100, 100),
                       n_rays=32,
                       length=1)
    (img, ), (prob, dist) = s[0]
    return (img, ), (prob, dist), s
示例#3
0
def test_stardistdata_sequence():
    from stardist.models import StarDistData2D

    X = np.zeros((2, 100, 100), np.uint16)
    X[:, 10:-10, 10:-10] = 1

    X, Y = NumpySequence(X), NumpySequence(X.astype(np.uint16))
    s = StarDistData2D(X,
                       Y,
                       batch_size=1,
                       patch_size=(100, 100),
                       n_rays=32,
                       length=1)
    (img, ), (prob, dist) = s[0]
    return (img, ), (prob, dist), s
示例#4
0
def test_stardistdata(shape_completion, n_classes, classes):
    np.random.seed(42)
    from stardist.models import StarDistData2D
    img, mask = real_image2d()
    s = StarDistData2D([img, img], [mask, mask],
                       grid=(2, 2),
                       n_classes=n_classes,
                       classes=classes,
                       shape_completion=shape_completion,
                       b=8,
                       batch_size=1,
                       patch_size=(30, 40),
                       n_rays=32,
                       length=1)
    a, b = s[0]
    return a, b, s
示例#5
0
def test_stardistdata_multithreaded(workers=5):
    np.random.seed(42)
    from stardist.models import StarDistData2D
    from scipy.ndimage import rotate
    from concurrent.futures import ThreadPoolExecutor
    from time import sleep

    def augmenter(x, y):
        deg = int(np.sum(y) % 117)
        print(deg)
        # return x,y
        x = rotate(x, deg, reshape=False, order=0)
        y = rotate(y, deg, reshape=False, order=0)
        # sleep(np.abs(deg)/180)
        return x, y

    n_samples = 4

    _, mask = real_image2d()
    Y = np.stack([mask + i for i in range(n_samples)])
    s = StarDistData2D(Y.astype(np.float32),
                       Y,
                       grid=(1, 1),
                       n_classes=None,
                       augmenter=augmenter,
                       batch_size=1,
                       patch_size=mask.shape,
                       n_rays=32,
                       length=len(Y))

    a1, b1 = tuple(zip(*tuple(s[i] for i in range(n_samples))))

    with ThreadPoolExecutor(max_workers=n_samples) as e:
        a2, b2 = tuple(zip(*tuple(e.map(lambda i: s[i], range(n_samples)))))

    assert all([np.allclose(_r1[0], _r2[0]) for _r1, _r2 in zip(a1, a2)])
    assert all([np.allclose(_r1[0], _r2[0]) for _r1, _r2 in zip(b1, b2)])
    assert all([np.allclose(_r1[1], _r2[1]) for _r1, _r2 in zip(b1, b2)])
    return a2, b2, s