Example #1
0
def test_count_touching_neighbors():

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

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

    touch_matrix = cle.generate_touch_matrix(labels)

    neighbor_count_vector = cle.count_touching_neighbors(touch_matrix)

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

    print(a)
    print(b)


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

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

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

    gpu_touch_matrix = cle.generate_touch_matrix(gpu_input)

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

    print(a)
    print(b)

    assert (np.allclose(a, b, 0.001))
def test_average_distance_of_touching_neighbors():

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

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

    centroids = cle.centroids_of_labels(labels)
    distance_matrix = cle.generate_distance_matrix(centroids, centroids)
    touch_matrix = cle.generate_touch_matrix(labels)

    average_distance_of_touching_neighbors = cle.average_distance_of_touching_neighbors(
        distance_matrix, touch_matrix)

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

    print(a)
    print(b)

    assert (np.allclose(a, b, 0.01))
Example #4
0
 def _make_touch_matrix(self, labels, touch_matrix=None):
     if touch_matrix is None:
         import pyclesperanto_prototype as cle
         touch_matrix = cle.generate_touch_matrix(labels)
     return touch_matrix
def mesh_data(gpu_input, sigma: float = 2.0, threshold: float = 300):

    # 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=sigma)

    # 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)

    # 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)

    # Draw connectivity of the cells as a mesh¶
    # We then read out all current positions of detected nuclei as a pointlist to
    # generate a distance matrix of all nuclei towards each other:

    gpu_pointlist = cle.labelled_spots_to_pointlist(gpu_labelled_spots)
    gpu_distance_matrix = cle.generate_distance_matrix(gpu_pointlist,
                                                       gpu_pointlist)
    gpu_touch_matrix = cle.generate_touch_matrix(gpu_labels)

    # touch matrix:
    # set the first column to zero to ignore all spots touching the background
    # (background label 0, first column)
    cle.set_column(gpu_touch_matrix, 0, 0)

    # create memory for the pixelated mesh
    gpu_mesh = cle.create_like(gpu_input)

    cle.touch_matrix_to_mesh(gpu_pointlist, gpu_touch_matrix, gpu_mesh)

    return gpu_mesh