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_point_index_list_to_touch_matrix(): positions = cle.push(np.asarray([ [1, 1, 4, 4], [1, 4, 4, 1] ])) index_list = cle.push(np.asarray([ [2, 3, 4, 1], [3,-1,-1,-1] ])) reference = cle.push(np.asarray([ [0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0], [0, 1, 1, 0, 1, 0], [0, 1, 0, 1, 1, 0], [0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0] ])) result = cle.create_like(reference) touch_matrix = cle.point_index_list_to_touch_matrix(index_list) print(touch_matrix) result = cle.touch_matrix_to_mesh(positions, touch_matrix, result) a = cle.pull(result) b = cle.pull(reference) print(a) print(b) assert (np.array_equal(a, b))
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_merge_touching_labels(): gpu_input = cle.push(np.asarray([ [ [1, 1, 0, 0, 0], [0, 2, 2, 0, 3], [0, 0, 2, 0, 3], ] ])) gpu_output = cle.create_like(gpu_input) gpu_reference = cle.push(np.asarray([ [ [1, 1, 0, 0, 0], [0, 1, 1, 0, 2], [0, 0, 1, 0, 2], ] ])) cle.merge_touching_labels(gpu_input, gpu_output) a = cle.pull(gpu_output) b = cle.pull(gpu_reference) print(a) print(b) assert (np.array_equal(a, b))
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_write_values_to_positions_2d(): positions_and_values = cle.push(np.asarray([ [0, 0, 2, 3, 5], [0, 1, 3, 2, 6], [8, 7, 6, 5, 4] ])) reference = cle.push(np.asarray([ [8, 0, 0, 0, 0, 0], [7, 0, 0, 0, 0, 0], [0, 0, 0, 5, 0, 0], [0, 0, 6, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 4] ])) result = cle.create_like(reference) cle.set(result, 0) result = cle.write_values_to_positions(positions_and_values, result) a = cle.pull(result) b = cle.pull(reference) print(a) print(b) assert (np.array_equal(a, b))
def test_relabel_sequential(): gpu_input = cle.push(np.asarray([ [ [1, 2, 3], [1, 6, 6], [7, 8, 9] ] ])) gpu_output = cle.create_like(gpu_input) gpu_reference = cle.push(np.asarray([ [ [1, 2, 3], [1, 4, 4], [5, 6, 7] ] ])) cle.relabel_sequential(gpu_input, gpu_output) a = cle.pull(gpu_output) b = cle.pull(gpu_reference) print(a) print(b) assert (np.array_equal(a, b))
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 _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
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 _combine(input1: Layer, input2: Layer = None, operation_name: str = cle.binary_and.__name__, myself=None): if input1 is not None: if (input2 is None): input2 = input1 # execute operation cle_input1 = cle.push(input1.data) cle_input2 = cle.push(input2.data) output = cle.create_like(cle_input1) operation = cle.operation(operation_name) _call_operation_ignoring_to_many_arguments( operation, [cle_input1, cle_input2, output]) 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_detect_minima_box(): gpu_input = cle.push(np.asarray([ [6, 6, 6, 6, 6], [5, 6, 6, 4, 6], [5, 4, 5, 3, 4], [6, 5, 6, 4, 6], [6, 6, 6, 6, 6] ])) gpu_output = cle.create_like(gpu_input) gpu_reference = cle.push(np.asarray([ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ])) cle.detect_minima_box(gpu_input, gpu_output) a = cle.pull(gpu_output) b = cle.pull(gpu_reference) print(a) print(b) assert (np.array_equal(a, b))
def test_create_2d(): size = [2, 3] image = cle.create(size) assert (image.shape[0] == 2) assert (image.shape[1] == 3) image2 = cle.create_like(image) assert (image2.shape[0] == 2) assert (image2.shape[1] == 3)
def label(input1: Image, operation: Label) -> Image: if input1 is not None: cle_input1 = cle.push_zyx(input1.data) output = cle.create_like(cle_input1) operation(cle_input1, output) output = cle.pull_zyx(output) # workaround to cause a auto-contrast in the viewer after returning the result if Gui.global_last_filter_applied is not None: viewer.layers.remove_selected() Gui.global_last_filter_applied = operation return output
def test_create_3d(): size = [2, 3, 4] image = create(size) assert (image.shape[0] == 2) assert (image.shape[1] == 3) assert (image.shape[2] == 4) image2 = create_like(image) assert (image2.shape[0] == 2) assert (image2.shape[1] == 3) assert (image2.shape[2] == 4)
def test_add_image_and_scalar(): data = np.arange(100).reshape(10, 10) # push an array to the GPU flip = cle.push(data.T) assert flip.shape == (10, 10) assert isinstance(flip, cl.array.Array) # create memory for the output flop = cle.create_like(data) assert flop.shape == (10, 10) assert isinstance(flop, cl.array.Array) # add a constant to all pixels cle.add_image_and_scalar(flip, flop, 100.0) # Note the transposition! np.testing.assert_allclose(data + 100, flop.get().T)
def test_close_index_gaps_in_label_maps(): gpu_input = cle.push(np.asarray([[[1, 2, 3], [1, 6, 6], [7, 8, 9]]])) gpu_output = cle.create_like(gpu_input) gpu_reference = cle.push(np.asarray([[[1, 2, 3], [1, 4, 4], [5, 6, 7]]])) cle.close_index_gaps_in_label_map(gpu_input, gpu_output) a = cle.pull_zyx(gpu_output) b = cle.pull_zyx(gpu_reference) print(a) print(b) assert (np.array_equal(a, b))
def filter(input: Image, operation: Filter, x: float = 1, y: float = 1, z: float = 0) -> Image: if input: cle_input = cle.push_zyx(input.data) output = cle.create_like(cle_input) operation(cle_input, output, x, y, z) output = cle.pull_zyx(output) # workaround to cause a auto-contrast in the viewer after returning the result if Gui.global_last_filter_applied is not None: viewer.layers.remove_selected() Gui.global_last_filter_applied = operation return output
def test_detect_maxima_box(): gpu_input = cle.push( np.asarray([[0, 0, 0, 0, 0], [1, 0, 0, 2, 0], [1, 2, 1, 3, 2], [0, 1, 0, 2, 0], [0, 0, 0, 0, 0]])) gpu_output = cle.create_like(gpu_input) gpu_reference = cle.push( np.asarray([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])) cle.detect_maxima_box(gpu_input, gpu_output) a = cle.pull(gpu_output) b = cle.pull(gpu_reference) print(a) print(b) assert (np.array_equal(a, b))
def _label( input1: Layer, operation_name: str = cle.connected_components_labeling_box.__name__, a: float = 2, b: float = 2, myself=None): if input1 is not None: # execute operation cle_input1 = cle.push(input1.data) operation = cle.operation(operation_name) # update GUI label_parameters(operation, [myself.gui.a, myself.gui.b]) output = cle.create_like(cle_input1) _call_operation_ignoring_to_many_arguments(operation, [cle_input1, output, a, b]) 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.name = "Result of " + operation.__name__ myself.layer.translate = input1.translate
def _label_processing( input1: Labels, operation_name: str = cle.exclude_labels_on_edges.__name__, min: float = 0, max: float = 100, 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.min, myself.gui.max]) _call_operation_ignoring_to_many_arguments( operation, [cle_input1, output, min, max]) 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.name = "Result of " + operation.__name__ myself.layer.translate = input1.translate
def predict(self, labels, image=None): """ Parameters ---------- labels: label image image: intensity image Returns ------- label image representing a semantic segmentation: pixel intensities represent label class """ import pyclesperanto_prototype as cle labels = cle.push(labels) selected_features, gt = self._make_features( self.classifier.feature_specification, labels, None, image) output = cle.create_like(selected_features[0].shape) parameters = {} for i, f in enumerate(selected_features): parameters['in' + str(i)] = cle.push(f) parameters['out'] = output cle.execute(None, self.classifier.opencl_file, "predict", selected_features[0].shape, parameters) # set background to zero cle.set_column(output, 0, 0) result_labels = cle.create_labels_like(labels) cle.replace_intensities(labels, output, result_labels) return result_labels
from tifffile import imread 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)
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()
def generate_feature_stack( image, features_specification: Union[str, PredefinedFeatureSet] = None): """ Creates a feature stack from a given image. Parameters ---------- image : ndarray 2D or 3D image to generate a feature stack from features_specification : str or PredefinedFeatureSet a space-separated list of features, e.g. original gaussian=4 sobel_of_gaussian=4 or a PredefinedFeatureSet Returns ------- a list of OCLarray images """ image = cle.push(image) # default features if features_specification is None: blurred = cle.gaussian_blur(image, sigma_x=2, sigma_y=2, sigma_z=2) edges = cle.sobel(blurred) stack = [image, blurred, edges] return stack if isinstance(features_specification, PredefinedFeatureSet): features_specification = features_specification.value while " " in features_specification: features_specification = features_specification.replace(" ", " ") while "\t" in features_specification: features_specification = features_specification.replace("\t", " ") features_specs = features_specification.split(" ") generated_features = {} result_features = [] for spec in features_specs: if spec.lower() == 'original': generated_features['original'] = image result_features.append(image) elif "=" in spec: temp = spec.split("=") operation = temp[0] numeric_parameter = float(temp[1]) if not hasattr(cle, operation) and "_of_" in operation: temp = operation.split("_of_") outer_operation = temp[0] inner_operation = temp[1] if (inner_operation + "=" + str(numeric_parameter) ) not in generated_features.keys(): new_image = cle.create_like(image) _apply_operation(inner_operation, image, new_image, numeric_parameter) generated_features[inner_operation + "=" + str(numeric_parameter)] = new_image if (operation + "=" + str(numeric_parameter) ) not in generated_features.keys(): new_image2 = cle.create_like(image) _apply_operation( outer_operation, generated_features[inner_operation + "=" + str(numeric_parameter)], new_image2, numeric_parameter) generated_features[operation + "=" + str(numeric_parameter)] = new_image2 else: if (operation + "=" + str(numeric_parameter)) not in generated_features: new_image = cle.create_like(image) _apply_operation(operation, image, new_image, numeric_parameter) generated_features[operation + "=" + str(numeric_parameter)] = new_image result_features.append(generated_features[operation + "=" + str(numeric_parameter)]) return result_features
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
cle.set_wait_for_kernel_finish(True) # config num_iterations = 10 num_tests = 10 # initialize GPU print("Used GPU: " + cle.get_device().name) # generate data; 50 MB image = np.random.random([50, 1024, 1024]) print("Image size: " + str(image.shape)) # push image to GPU memory flip = cle.push_zyx(image) flop = cle.create_like(flip) for j in range(0, num_tests): start = time.time() for i in range(0, num_iterations): cle.maximum_sphere(flip, flop, 3, 3, 0) cle.minimum_sphere(flop, flip, 3, 3, 0) end = time.time() print("Flip-flop took " + str(end - start) + "s") for j in range(0, num_tests): start = time.time()
IJ.run("Close All") # Meghan's multilevel segmentation approach. Gamma -> 3DGaussian -> Otsu -> Dilate -> Fill Holes -> Erode # 3D Gaussian with 1 voxel size - "Image3Dthresh" # Raw data - Otsu threshold -> Divide by standard deviation of image. image3D = cle.imread( '/archive/MIL/marciano/20201211_CapillaryLooping/cropped/wt/1_CH00_000000.tif' ) cle.imshow(image3D, 'raw data', True) # Gamma Correction inside_gamma = 0.5 image3Dblurred1 = cle.create_like(image3D) image3Dblurred1 = cle.gamma_correction(image3D, image3Dblurred1, inside_gamma) # Otsu Threshold image3Dthresh = cle.create_like(image3Dblurred1) image3Dthresh = cle.threshold_otsu(image3Dblurred1, image3Dthresh) # Dilation, Fill Holes, Erosion # Dilation Occurs 4x. image3Ddilated1 = cle.create_like(image3Dthresh) image3Ddilated1 = cle.dilate_sphere(image3Dthresh, image3Ddilated1) image3Ddilated2 = cle.create_like(image3Ddilated1) image3Ddilated2 = cle.dilate_sphere(image3Ddilated1, image3Ddilated2) image3Ddilated3 = cle.create_like(image3Ddilated2)