def test_paste(): test1 = cle.push_zyx(np.asarray([ [0, 0, 0, 1], [0, 0, 3, 1], [0, 0, 3, 1], [1, 1, 1, 1] ])) test2 = cle.push_zyx(np.asarray([ [1, 2], ])) reference = cle.push_zyx(np.asarray([ [0, 0, 0, 1], [0, 0, 3, 1], [0, 1, 2, 1], [1, 1, 1, 1] ])) result = cle.create(test1) cle.copy(test1, result) cle.paste(test2, result, 1, 2, 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_copy(): test1 = cle.push(np.asarray([[1, 1], [1, 0]])) test2 = cle.create(test1) cle.copy(test1, test2) print(test2) a = cle.pull(test2) assert (np.min(a) == 0) assert (np.max(a) == 1) assert (np.mean(a) == 0.75)
def test_create_int16(): image = cle.push([[-1, 1.5], [2000, -7.8]]) reference = np.asarray([[-1, 1], [2000, -7]]) target = cle.create(image.shape, dtype=np.int16) cle.copy(image, target) print(target) assert np.allclose(target, reference)
import napari # Start napari viewer with napari.gui_qt(): viewer = napari.Viewer() # Load image image = imread("C:/Users/rober/AppData/Local/Temp/temp1605606091856.tif") # Push temp1605606091856.tif to GPU memory image1 = cle.push_zyx(image) # Copy image2 = cle.create_like(image1) cle.copy(image1, image2) # show result viewer.add_image(cle.pull_zyx(image2), scale=(1.0, 1.0)) # Gaussian Blur2D image3 = cle.create_like(image2) sigma_x = 16.0 sigma_y = 16.0 cle.gaussian_blur(image2, image3, sigma_x, sigma_y) # show result viewer.add_image(cle.pull_zyx(image3), scale=(1.0, 1.0)) # Greater image4 = cle.create_like(image2) cle.greater(image2, image3, image4) # show result
def game_loop(self): """ This function is called at every game iteration. It checks if bullets hit nuclei and redraws the playground """ bullet_radius = 5 # empty playground cle.set(self.playground, 0) # pull nuclei from GPU so that we can access individual pixels nuclei = np.asarray(self.nuclei) # prepare all existing labels in a list. We set them to 0 in case a nucleus was hit labels_to_keep = list(range(0, int(np.max(self.nuclei) + 1))) # future bullets to keep new_bullets = [] for bullet in self.bullets: bullet[1] += 10 # check if a bullet has hit a nucleus try: label = nuclei[int(self.playground.shape[0] - bullet[1]), int(bullet[0] + self.fov_x)] except IndexError: label = 0 if label != 0: # bullet has hit a nucleus labels_to_keep[label] = 0 elif bullet[1] > self.playground.shape[1]: pass # bullet has left the playground else: new_bullets.append(bullet) # draw bullets cle.draw_sphere(self.playground, bullet[0], self.playground.shape[0] - bullet[1], 0, bullet_radius, bullet_radius, 1, 1) self.bullets = new_bullets # only keep cells where the nuclei weren't hit new_label_ids = np.asarray(labels_to_keep) cle.replace_intensities(self.nuclei, new_label_ids, self.nuclei) cle.replace_intensities(self.cells, new_label_ids, self.cells) # make a binary image of areas to keep binary = self.cells > 0 # draw player cle.draw_box(self.playground, self.player_position - 5, self.playground.shape[0] - 20, 0, 10, 20, 1, 2) cle.draw_box(self.playground, self.player_position - 15, self.playground.shape[0] - 10, 0, 30, 10, 1, 2) # collect all layers in a dictionary result = {} for i, image in enumerate(self.images): result["channel" + str(i)] = self.crop_fov( cle.multiply_images(image, binary)) # add segmentation (invisble) and playground result['nuclei'] = self.crop_fov(self.nuclei, self.fov_nuclei) result['cells'] = self.crop_fov(self.cells, self.fov_cells) result['playground'] = cle.copy(self.playground) self.fov_x += self.fov_delta_x if self.fov_x <= 0: self.fov_x = 0 self.fov_delta_x = 1 elif self.fov_x >= self.fov_max_x: self.fov_x = self.fov_max_x self.fov_delta_x = -1 return result
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
import pyclesperanto_prototype as cle from tifffile import imread import numpy as np import matplotlib import matplotlib.pyplot as plt # Load image image = imread("C:/Users/rober/AppData/Local/Temp/temp1645017784179.tif") # Push C4-cell_culture_tom20-bcatenin-dapi-infection-1.tif to GPU memory image_1 = cle.push_zyx(image) # Copy image_2 = cle.create_like(image_1) cle.copy(image_1, image_2) # show result plt.imshow(image_2[46]) plt.show() # Voronoi Otsu Labeling image_3 = cle.create_like(image_2) spot_sigma = 30.0 outline_sigma = 10.0 cle.voronoi_otsu_labeling(image_2, image_3, spot_sigma, outline_sigma) # show result cmap = matplotlib.colors.ListedColormap(np.random.rand(256, 3)) plt.imshow(image_3[49], cmap=cmap) plt.show() # Load image