示例#1
0
def test_connected_components_labeling_box_blobs():
    import pyclesperanto_prototype as cle

    from skimage.io import imread, imsave

    # initialize GPU
    cle.select_device("GTX")
    print("Used GPU: " + cle.get_device().name)

    # load data
    image = imread('https://imagej.nih.gov/ij/images/blobs.gif')
    print("Loaded image size: " + str(image.shape))

    # push image to GPU memory
    input = cle.push(image)
    print("Image size in GPU: " + str(input.shape))

    # process the image
    inverted = cle.subtract_image_from_scalar(image, scalar=255)
    blurred = cle.gaussian_blur(inverted, sigma_x=1, sigma_y=1)
    binary = cle.threshold_otsu(blurred)
    labeled = cle.connected_components_labeling_box(binary)

    # The maxmium intensity in a label image corresponds to the number of objects
    num_labels = cle.maximum_of_all_pixels(labeled)

    # print out result
    print("Num objects in the image: " + str(num_labels))

    assert num_labels == 63
示例#2
0
def test_multiply_image_and_coordinate():
    test1 = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2]]))

    reference = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]))

    result = cle.create(test1)
    cle.multiply_image_and_coordinate(test1, result, 0)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)

    assert (np.array_equal(a, b))
def workflow(input: Image, sigma=3, threshold: float = 30) -> Labels:
    if input:
        # push image to GPU memory and show it
        gpu_input = cle.push(input.data)

        # Spot detection
        # After some noise removal/smoothing, we perform a local maximum detection

        # gaussian blur
        gpu_blurred = cle.gaussian_blur(gpu_input,
                                        sigma_x=sigma,
                                        sigma_y=sigma,
                                        sigma_z=0)

        # detect maxima
        gpu_detected_maxima = cle.detect_maxima_box(gpu_blurred)

        # Spot curation
        # Now, we remove spots with values below a certain intensity and label the remaining spots

        # threshold
        gpu_thresholded = cle.greater_constant(gpu_blurred,
                                               constant=threshold * 10)

        # mask
        gpu_masked_spots = cle.mask(gpu_detected_maxima, gpu_thresholded)

        # label spots
        gpu_labelled_spots = cle.connected_components_labeling_box(
            gpu_masked_spots)

        number_of_spots = cle.maximum_of_all_pixels(gpu_labelled_spots)
        print("Number of detected spots: " + str(number_of_spots))

        # Expanding labelled spots
        # Next, we spatially extend the labelled spots by applying a maximum filter.

        # label map closing
        number_of_dilations = 10
        number_of_erosions = 4

        flip = cle.create_like(gpu_labelled_spots)
        flop = cle.create_like(gpu_labelled_spots)
        flag = cle.create([1, 1, 1])
        cle.copy(gpu_labelled_spots, flip)

        for i in range(0, number_of_dilations):
            cle.onlyzero_overwrite_maximum_box(flip, flag, flop)
            cle.onlyzero_overwrite_maximum_diamond(flop, flag, flip)

        flap = cle.greater_constant(flip, constant=1)

        for i in range(0, number_of_erosions):
            cle.erode_box(flap, flop)
            cle.erode_sphere(flop, flap)

        gpu_labels = cle.mask(flip, flap)

        output = cle.pull(gpu_labels)
        return output
def test_threshold_otsu_against_scikit_image():

    # threshold using skimage
    from skimage.data import camera
    from skimage.filters import threshold_otsu
    image = camera()
    thresh = threshold_otsu(image)
    binary = image > thresh

    print(thresh)

    #from skimage import exposure
    #counts, bin_centers = exposure.histogram(image.ravel(), 256, source_range='image')

    #print(str(counts))
    #print(str(bin_centers))


    # threshold in GPU
    import pyclesperanto_prototype as cle
    gpu_image = cle.push(image)
    gpu_binary = cle.threshold_otsu(gpu_image)

    print(str(binary))
    print(str(cle.pull(gpu_binary)))


    # compare
    import numpy as np
    assert(np.allclose(binary, (cle.pull(gpu_binary) > 0)))
示例#5
0
def test_multiply_image_and_scalar():
    test1 = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2]]))

    reference = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [2, 2, 2, 2, 2], [4, 4, 4, 4, 4]]))

    result = cle.create(test1)
    cle.multiply_image_and_scalar(test1, result, 2)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)

    assert (np.array_equal(a, b))
示例#6
0
def _binarize(input1: Layer,
              operation_name: str = cle.threshold_otsu.__name__,
              radius_x: int = 1,
              radius_y: int = 1,
              radius_z: int = 0,
              myself=None):
    if input1 is not None:
        # execute operation
        cle_input1 = cle.push(input1.data)
        output = cle.create_like(cle_input1)
        operation = cle.operation(operation_name)
        # update GUI
        label_parameters(
            operation,
            [myself.gui.radius_x, myself.gui.radius_y, myself.gui.radius_z])
        _call_operation_ignoring_to_many_arguments(
            operation, [cle_input1, output, radius_x, radius_y, radius_z])
        output = cle.pull(output).astype(int)

        # show result in napari
        if not hasattr(myself, 'layer'):
            myself.viewer.add_labels(output, translate=input1.translate)
        else:
            myself.layer.data = output
            myself.layer.contrast_limits = (0, 1)
            myself.layer.name = "Result of " + operation.__name__
            myself.layer.translate = input1.translate
def test_maximum_image_and_scalar():
    test1 = cle.push(
        np.asarray([[0, 3, 4, 5, 0], [0, 2, 1, 6, 0], [0, 0, 8, 7, 0]]))

    reference = cle.push(
        np.asarray([[2, 3, 4, 5, 2], [2, 2, 2, 6, 2], [2, 2, 8, 7, 2]]))

    result = cle.create(test1)
    cle.maximum_image_and_scalar(test1, result, 2)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)

    assert (np.array_equal(a, b))
def test_exclude_labels_with_map_values_equal_to_constant_2d():

    gpu_input = cle.push(np.asarray([[0, 1, 2, 3, 4, 5, 6]]))

    gpu_reference = cle.push(np.asarray([[0, 1, 2, 0, 3, 4, 5]]))

    gpu_output = cle.exclude_labels_with_map_values_equal_to_constant(
        gpu_input, gpu_input, constant=3)

    a = cle.pull(gpu_output)
    b = cle.pull(gpu_reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
示例#9
0
def _denoise(input1: Image,
             operation_name: str = cle.gaussian_blur.__name__,
             x: float = 1,
             y: float = 1,
             z: float = 0,
             myself=None):
    if input1:
        # execute operation
        cle_input = cle.push(input1.data)
        output = cle.create_like(cle_input)
        operation = cle.operation(operation_name)
        # update GUI
        label_parameters(operation, [myself.gui.x, myself.gui.y, myself.gui.z])
        _call_operation_ignoring_to_many_arguments(
            operation, [cle_input, output, x, y, z])
        max_intensity = cle.maximum_of_all_pixels(output)
        if max_intensity == 0:
            max_intensity = 1  # prevent division by zero in vispy
        output = cle.pull(output)

        # show result in napari
        if not hasattr(myself, 'layer'):
            myself.viewer.add_image(output,
                                    colormap=input1.colormap,
                                    translate=input1.translate)
        else:
            myself.layer.data = output
            myself.layer.name = "Result of " + operation.__name__
            myself.layer.contrast_limits = (0, max_intensity)
            myself.layer.translate = input1.translate
def test_resample_downsample_3d():
    test1 = cle.push(
        np.asarray([[[0, 0, 2, 2], [0, 0, 2, 2], [1, 1, 4, 4], [1, 1, 4, 4]],
                    [[0, 0, 2, 2], [0, 0, 2, 2], [1, 1, 4, 4], [1, 1, 4, 4]],
                    [[5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5]],
                    [[5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5]]]))

    reference = cle.push(np.asarray([[[0, 2], [1, 4]], [[5, 5], [5, 5]]]))

    result = cle.resample(test1, factor_x=0.5, factor_y=0.5, factor_z=0.5)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)
    assert (np.array_equal(a, b))
示例#11
0
def _projection(input1: Layer,
                operation_name: str = cle.maximum_z_projection.__name__,
                myself=None):
    if input1 is not None:
        # execute operation
        cle_input1 = cle.push(input1.data)
        output = None
        operation = cle.operation(operation_name)
        output = _call_operation_ignoring_to_many_arguments(
            operation, [cle_input1, output])
        output = cle.pull(output).astype(int)

        # show result in napari
        if not hasattr(myself, 'layer'):
            if isinstance(input1, Labels):
                myself.viewer.add_labels(output,
                                         translate=input1.translate[1:3])
            else:
                myself.viewer.add_image(output,
                                        translate=input1.translate[1:3])
        else:
            myself.layer.data = output
            myself.layer.contrast_limits = input1.contrast_limits
            myself.layer.name = "Result of " + operation.__name__
            myself.layer.translate = input1.translate[1:3]
示例#12
0
def _map(input1: Labels,
         operation_name: str = cle.label_pixel_count_map.__name__,
         n: float = 1,
         myself=None):
    if input1 is not None:
        # execute operation
        cle_input1 = cle.push(input1.data)
        output = cle.create_like(cle_input1)
        operation = cle.operation(operation_name)
        # update GUI
        label_parameters(operation, [myself.gui.n])
        _call_operation_ignoring_to_many_arguments(operation,
                                                   [cle_input1, output, n])
        max_intensity = cle.maximum_of_all_pixels(output)
        if max_intensity == 0:
            max_intensity = 1  # prevent division by zero in vispy
        output = cle.pull(output)

        # show result in napari
        if not hasattr(myself, 'layer'):
            myself.viewer.add_image(output,
                                    colormap='turbo',
                                    interpolation='nearest',
                                    translate=input1.translate)
        else:
            myself.layer.data = output
            myself.layer.name = "Result of " + operation.__name__
            myself.layer.contrast_limits = (0, max_intensity)
            myself.layer.translate = input1.translate
示例#13
0
def _mesh(input1: Labels,
          operation_name: str = cle.draw_mesh_between_touching_labels.__name__,
          n: float = 1,
          myself=None):
    if input1 is not None:
        # execute operation
        cle_input1 = cle.push(input1.data)
        output = cle.create_like(cle_input1)
        operation = cle.operation(operation_name)
        # update GUI
        label_parameters(operation, [myself.gui.n])
        _call_operation_ignoring_to_many_arguments(operation,
                                                   [cle_input1, output, n])
        min_intensity = cle.minimum_of_all_pixels(output)
        max_intensity = cle.maximum_of_all_pixels(output)
        if max_intensity - min_intensity == 0:
            max_intensity = min_intensity + 1  # prevent division by zero in vispy
        output = cle.pull(output)

        # show result in napari
        if not hasattr(myself, 'layer'):
            myself.viewer.add_image(output,
                                    colormap='green',
                                    blending='additive',
                                    translate=input1.translate)
        else:
            myself.layer.data = output
            myself.layer.name = "Result of " + operation.__name__
            myself.layer.contrast_limits = (min_intensity, max_intensity)
            myself.layer.translate = input1.translate
def test_set_column():
    result = cle.push(
        np.asarray([[3, 3, 3, 3, 3], [3, 3, 3, 3, 3], [3, 3, 3, 3, 3],
                    [3, 3, 3, 3, 3], [3, 3, 3, 3, 3]]))

    reference = cle.push(
        np.asarray([[3, 3, 3, 3, 3], [3, 3, 3, 3, 3], [3, 3, 3, 3, 3],
                    [4, 4, 4, 4, 4], [3, 3, 3, 3, 3]]))

    cle.set_column(result, 3, 4)

    print(result)

    a = cle.pull(result)
    b = cle.pull(reference)
    assert (np.allclose(a, b, 0.001))
示例#15
0
def test_label_label_mean_extension_map_2d():

    labels = cle.push(np.asarray([[1, 1, 2], [1, 0, 0], [3, 3, 0]]))

    reference = cle.push(
        np.asarray([[0.65403885, 0.65403885, 0], [0.65403885, 0, 0],
                    [0.5, 0.5, 0]]))

    result = cle.label_mean_extension_map(labels)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)
    print(b)

    assert (np.allclose(a, b, 0.001))
示例#16
0
def test_gradient_y():
    test = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 1, 2, 0, 0], [0, 1, 2, 0, 0],
                    [0, 1, 3, 0, 0], [0, 0, 0, 0, 0]]))

    reference = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [1, 2, -1, -2, 0], [1, 2, -1, -2, 0],
                    [1, 3, -1, -3, 0], [0, 0, 0, 0, 0]]))

    result = cle.create(test)
    cle.gradient_y(test, result)

    a = cle.pull(result)
    b = cle.pull(reference)
    print(a)

    assert (np.array_equal(a, b))
示例#17
0
def test_mean_of_all_pixels_3d():
    test1 = cle.push(
        np.asarray([[[0, 4, 0, 0, 2], [0, 0, 0, 8, 0], [3, 0, 0, 0, 0],
                     [0, 0, 0, 0, 1], [0, 5, 2, 0, 0]]]))

    s = cle.mean_of_all_pixels(test1)

    assert s == 1
def test_read_intensities_from_map():

    intensities = cle.push(np.asarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))

    labels = cle.push(np.asarray([[8, 0, 7], [6, 5, 4], [3, 2, 1]]))

    reference = cle.push(np.asarray([[2, 9, 8, 7, 6, 5, 4, 3, 1]]))

    result = cle.read_intensities_from_map(labels, intensities)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
def test_extend_labels_with_maximum_radius_3d():
    gpu_input = cle.push(np.asarray([
        [
            [0, 0, 0, 0, 0, 2],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 4, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [5, 0, 0, 0, 0, 3],
        ], [
            [1, 0, 0, 0, 0, 2],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [5, 0, 0, 0, 0, 0],
        ]
    ]))

    gpu_reference = cle.push(np.asarray([
        [
            [1, 1, 0, 0, 2, 2],
            [1, 1, 0, 0, 2, 2],
            [0, 0, 4, 4, 4, 0],
            [0, 0, 4, 4, 4, 0],
            [5, 5, 4, 4, 4, 3],
            [5, 5, 0, 0, 3, 3],
        ],[
            [1, 1, 0, 0, 2, 2],
            [1, 1, 0, 0, 2, 2],
            [0, 0, 4, 4, 4, 0],
            [0, 0, 4, 4, 4, 0],
            [5, 5, 4, 4, 4, 3],
            [5, 5, 0, 0, 3, 3],
        ]
    ]))

    gpu_output = cle.extend_labels_with_maximum_radius(gpu_input, radius=1)

    a = cle.pull(gpu_output)
    b = cle.pull(gpu_reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
示例#20
0
def test_sobel():
    test1 = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0],
                    [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]))

    reference = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 1.41, 2, 1.41, 0],
                    [0, 3.16, 2, 3.16, 0], [0, 3.16, 2, 3.16, 0],
                    [0, 1.41, 2, 1.41, 0]]))

    result = cle.create(test1)
    cle.sobel(test1, result)
    a = cle.pull(result)
    print(a)

    b = cle.pull(reference)
    assert (np.allclose(a, b, 0.01))
示例#21
0
def test_read_intensities_from_positions():

    intensities = cle.push(np.asarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))

    pointlist = cle.push(np.asarray([[0, 1, 0], [0, 2, 2]]))

    reference = cle.push(np.asarray([[1, 8, 7]]))

    result = cle.read_intensities_from_positions(pointlist, intensities)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
def test_labelled_spots_to_pointlist():

    gpu_input = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0],
                    [0, 0, 1, 0, 0], [0, 0, 0, 0, 1]]))

    gpu_reference = cle.push(np.asarray([[1, 3, 2, 4], [1, 1, 3, 4]]))

    gpu_output = cle.spots_to_pointlist(gpu_input)

    a = cle.pull(gpu_output)
    b = cle.pull(gpu_reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
def test_label_mean_intensity_map_2d():

    intensity = cle.push(np.asarray([[1, 1, 2], [4, 0, 0], [5, 3, 0]]))

    labels = cle.push(np.asarray([[1, 1, 2], [1, 0, 0], [3, 3, 0]]))

    reference = cle.push(np.asarray([[2, 2, 2], [2, 0, 0], [4, 4, 0]]))

    result = cle.label_mean_intensity_map(intensity, labels)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
def test_dilate_box():
    test = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0],
                    [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]))

    reference = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0],
                    [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]]))

    result = cle.create(test)
    cle.dilate_box(test, result)

    print(result)

    a = cle.pull(result)
    b = cle.pull(reference)
    assert (np.array_equal(a, b))
def test_connected_components_labeling_diamond():

    gpu_input = cle.push(
        np.asarray([[[0, 1, 0, 1], [0, 1, 0, 0], [1, 0, 0, 1]]]))

    gpu_reference = cle.push(
        np.asarray([[[0, 1, 0, 2], [0, 1, 0, 0], [3, 0, 0, 4]]]))

    gpu_output = cle.connected_components_labeling_diamond(gpu_input)

    a = cle.pull_zyx(gpu_output)
    b = cle.pull_zyx(gpu_reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
示例#26
0
def test_sum_of_all_pixels_2d():
    test1 = cle.push(
        np.asarray([[0, 4, 0, 0, 2], [0, 0, 0, 8, 0], [3, 0, 0, 0, 0],
                    [0, 0, 0, 0, 1], [0, 0, 2, 0, 0]]))

    s = cle.sum_of_all_pixels(test1)

    assert s == 20
def test_smaller_or_equal_constant():
    test1 = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 1, 2, 3, 0], [0, 2, 3, 4, 0],
                    [0, 4, 5, 5, 0], [0, 0, 0, 0, 0]]))

    reference = cle.push(
        np.asarray([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 0, 1],
                    [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]]))

    result = cle.create(test1)
    cle.smaller_constant(test1, result, 4)

    print(result)

    a = cle.pull(result)
    b = cle.pull(reference)
    assert (np.array_equal(a, b))
def test_apply_vector_field_2d():
    source = cle.push(
        np.asarray([
            [0, 0, 0, 0, 0],
            [0, 1, 1, 1, 0],
            [0, 1, 2, 1, 0],
            [0, 1, 1, 1, 0],
            [0, 0, 0, 0, 0],
        ]))

    vector_x = cle.push(
        np.asarray([
            [0, 0, 0, 0, 0],
            [0, 0.5, 0.5, 0.5, 0],
            [0, 0.5, 1.0, 0.5, 0],
            [0, 0.5, 0.5, 0.5, 0],
            [0, 0, 0, 0, 0],
        ]))

    vector_y = cle.push(
        np.asarray([
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
        ]))

    reference = cle.push(
        np.asarray([
            [0, 0, 0, 0, 0],
            [0, 1, 1, 0, 0],
            [0, 2, 1, 0, 0],
            [0, 1, 1, 0, 0],
            [0, 0, 0, 0, 0],
        ]))

    result = cle.apply_vector_field(source, vector_x, vector_y)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
示例#29
0
def test_set_ramp_x():
    result = cle.push(
        np.asarray([[[0, 0, 0], [3, 4, 3], [3, 4, 3]],
                    [[3, 4, 3], [3, 4, 3], [3, 4, 3]]]))

    reference = cle.push(
        np.asarray([[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
                    [[1, 1, 1], [1, 1, 1], [1, 1, 1]]]))

    cle.set_ramp_x(result)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)

    assert (np.allclose(a, b, 0.001))
def test_push_np():
    reference = np.asarray([[1, 2], [-3, 4]])

    image = cle.push(reference)

    result = cle.pull(image)

    assert np.allclose(result, reference)