Exemple #1
0
def test_generate_proximal_neighbors_matrix():
    positions = cle.push(np.asarray([[1, 1, 4, 4], [1, 4, 4, 1]]))

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

    result = cle.create_like(reference)

    distance_matrix = cle.generate_distance_matrix(positions, positions)

    n_nearest_neighbor_matrix = cle.generate_proximal_neighbors_matrix(
        distance_matrix, min_distance=3, max_distance=3)

    result = cle.touch_matrix_to_mesh(positions, n_nearest_neighbor_matrix,
                                      result)

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

    print(a)
    print(b)

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

    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, 1]]))

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

    max_float = np.finfo(np.float).max

    cle.set_where_x_equals_y(distance_matrix, max_float)
    cle.set_row(distance_matrix, 0, max_float)
    cle.set_column(distance_matrix, 0, max_float)

    n_closest_points = cle.n_closest_points(distance_matrix,
                                            n=1,
                                            ignore_background=False)

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

    print(a)
    print(b)

    assert (np.allclose(a, b, 0.01))
def test_generate_distance_matrix():

    gpu_input = cle.push(np.asarray([

            [0, 0, 0, 0, 0],
            [0, 1, 0, 3, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 2, 0, 0],
            [0, 0, 0, 0, 4]

    ]))

    gpu_reference = cle.push(np.asarray([
            [0.,        0.   ,        0.    ,     0.    ,     0.    ],
            [0.,        0.   ,     2.236068 ,     2.    , 4.2426405 ],
            [0.,    2.236068 ,         0.   , 2.236068  , 2.236068  ],
            [0.,        2.   ,     2.236068 ,     0.    , 3.1622777 ],
            [0.,    4.2426405,     2.236068 , 3.1622777 ,     0.    ]
    ]))



    gpu_pointlist = cle.labelled_spots_to_pointlist(gpu_input)
    gpu_distance_matrix = cle.generate_distance_matrix(gpu_pointlist, gpu_pointlist)

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

    print(a)
    print(b)

    assert (np.allclose(a, b, 0.001))
Exemple #4
0
    def _make_distance_matrix(self, labels, distance_matrix=None):
        if distance_matrix is None:
            import pyclesperanto_prototype as cle
            centroids = cle.centroids_of_labels(labels)
            distance_matrix = cle.generate_distance_matrix(
                centroids, centroids)
            cle.set_column(distance_matrix, 0, 0)
            cle.set_row(distance_matrix, 0, 0)

        return distance_matrix
Exemple #5
0
def test_to_networkx():
    import pyclesperanto_prototype as cle
    labels = cle.push(np.asarray([[0, 1], [2, 3]]))
    # extract centroid positions
    centroids = cle.centroids_of_labels(labels)

    # determine a distance matrix
    distance_matrix = cle.generate_distance_matrix(centroids, centroids)

    # threshold the distance matrix
    adjacency_matrix = cle.generate_proximal_neighbors_matrix(distance_matrix,
                                                              max_distance=50)

    nx_graph = cle.to_networkx(adjacency_matrix, centroids)
    assert len(nx_graph.nodes) == 3
def test_maximum_distance_of_n_shortest_distances():

    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)

    maximum_distance_of_n_far_off_distances = cle.maximum_distance_of_n_shortest_distances(
        distance_matrix, n=1)

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

    print(a)
    print(b)

    assert (np.allclose(a, b, 0.01))
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))
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