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 test_flip(): test = cle.push_zyx(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_zyx(np.asarray([ [0, 0, 0, 0, 0], [0, 0, 2, 1, 0], [0, 0, 2, 1, 0], [0, 0, 3, 1, 0], [0, 0, 0, 0, 0] ])) result = cle.create(test) cle.flip(test, result, True, False, False) print(result) a = cle.pull_zyx(result) b = cle.pull_zyx(reference) assert (np.array_equal(a, b))
def test_gradient_z(): test = cle.push_zyx( 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]], [[0, 0, 0, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [0, 0, 0, 0, 0]], [[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]]])) reference = cle.push_zyx( np.asarray([[[0, 0, 0, 0, 0], [1, 0, 0, -1, 0], [1, 0, 0, -1, 0], [1, 0, 0, -1, 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, 0], [0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0], [-1, 0, 0, 1, 0], [-1, 0, 0, 1, 0], [-1, 0, 0, 1, 0], [0, 0, 0, 0, 0]]])) result = cle.create(test) cle.gradient_z(test, result) a = cle.pull_zyx(result) b = cle.pull_zyx(reference) print(a) assert (np.array_equal(a, b))
def test_minimum_of_masked_pixels(): np_input = np.asarray([[[1, 2, 3, 10], [4, 5, 6, 11], [7, 8, 9, 12]], [[1, 2, 3, 13], [4, 5, 6, 14], [7, 8, 9, 15]]]) np_mask = np.asarray([[[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0]], [[0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]]) gpu_input = cle.push_zyx(np_input) gpu_mask = cle.push_zyx(np_mask) print("gpu_input") print(gpu_input) print("gpu_mask") print(gpu_mask) result = cle.minimum_of_masked_pixels(gpu_input, gpu_mask) print(result) assert (result == 2) gpu_input = cle.push(np_input) gpu_mask = cle.push(np_mask) result = cle.minimum_of_masked_pixels(gpu_input, gpu_mask) print(result) assert (result == 2)
def test_dilate_box_slice_by_slice(): test = cle.push_zyx( 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]], [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 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, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]])) reference = cle.push_zyx( 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]], [[0, 0, 0, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [0, 0, 0, 0, 0]], [[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_slice_by_slice(test, result) a = cle.pull_zyx(result) b = cle.pull_zyx(reference) print(a) assert (np.array_equal(a, b))
def test_minimum_of_masked_pixels_mini_x(): np_input = np.asarray([[1, 2, 3, 4]]) np_mask = np.asarray([[0, 1, 1, 0]]) gpu_input = cle.push_zyx(np_input) gpu_mask = cle.push_zyx(np_mask) result = cle.minimum_of_masked_pixels(gpu_input, gpu_mask) print(result) assert (result == 2)
def test_minimum_of_masked_pixels_mini_y(): np_input = np.asarray([[1], [2], [3], [4]]) np_mask = np.asarray([[0], [1], [1], [0]]) gpu_input = cle.push_zyx(np_input) gpu_mask = cle.push_zyx(np_mask) result = cle.minimum_of_masked_pixels(gpu_input, gpu_mask) print(result) assert (result == 2)
def combine(input1: Image, input2: Image, operation: Combine) -> Image: if input1 is not None: cle_input1 = cle.push_zyx(input1.data) cle_input2 = cle.push_zyx(input2.data) output = cle.create_like(cle_input1) operation(cle_input1, cle_input2, 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_crop(): test1 = cle.push_zyx( np.asarray([[0, 0, 0, 1], [0, 0, 3, 1], [0, 0, 3, 1], [1, 1, 1, 1]])) reference = cle.push_zyx(np.asarray([[0, 0, 0], [0, 0, 3], [0, 0, 3]])) result = cle.create(reference) cle.crop(test1, result, 0, 0) a = cle.pull(result) b = cle.pull(reference) print(a) assert (np.array_equal(a, b))
def test_maximum_x_projection_of_pointlist(): positions_and_values = cle.push_zyx( np.asarray([[0, 0, 2, 3, 5], [0, 1, 3, 2, 6]])) reference = cle.push_zyx(np.asarray([[5], [6]])) result = cle.maximum_x_projection(positions_and_values) a = cle.pull_zyx(result) b = cle.pull_zyx(reference) print(a) print(b) assert (np.array_equal(a, b))
def test_multiply_image_and_coordinate(): test1 = cle.push_zyx( np.asarray([[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2]])) reference = cle.push_zyx( 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_zyx(result) b = cle.pull_zyx(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_zyx(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_zyx(gpu_labels) return output
def test_histogram(): test = cle.push_zyx(np.asarray([[1, 2, 4, 4, 2, 3], [3, 3, 4, 4, 5, 5]])) ref_histogram = [1, 2, 3, 4, 2] my_histogram = cle.histogram(test, num_bins=5) print(my_histogram) a = cle.pull(my_histogram) assert (np.allclose(a, ref_histogram))
def clij_filter(input: Image, sigma=2, threshold: float = 300) -> Image: if input: # push image to GPU memory and show it gpu_input = cle.push_zyx(input.data) gpu_output = mesh_data(gpu_input, sigma, threshold) output = cle.pull_zyx(gpu_output) return output
def block_enum(source, blocksize): flagged_indices = cle.push_zyx(source) max_label = source.shape[1] - 1 block_sums = cle.create([1, int((int(max_label) + 1) / blocksize) + 1]) cle.sum_reduction_x(flagged_indices, block_sums, blocksize) # distribute new numbers new_indices = cle.create([1, int(max_label) + 1]) cle.block_enumerate(flagged_indices, block_sums, new_indices, blocksize) return cle.pull_zyx(new_indices)
def process_image(input: Image, sigma: float = 5) -> Labels: if input: # push image to GPU input = cle.push_zyx(input.data) # process the mage blurred = cle.gaussian_blur(input, sigma_x=sigma, sigma_y=sigma) binary = cle.threshold_otsu(blurred) labels = cle.connected_components_labeling_box(binary) # pull result back output = cle.pull_zyx(labels) return output
def test_maximum_of_all_pixels(): np_input = np.asarray([[[1, 2, 3, 10], [4, 5, 6, 11], [7, 8, 9, 12]], [[1, 2, 3, 13], [4, 5, 6, 14], [7, 8, 9, 15]]]) gpu_input = cle.push_zyx(np_input) result = cle.maximum_of_all_pixels(gpu_input) print(result) assert (result == 15) gpu_input = cle.push(np_input) result = cle.maximum_of_all_pixels(gpu_input) print(result) assert (result == 15)
def test_subtract_image_from_scalar(): test1 = cle.push_zyx(np.asarray([ [0, 0], [1, 1], [2, 2] ])) reference = cle.push_zyx(np.asarray([ [5, 5], [4, 4], [3, 3] ])) result = cle.create(test1) cle.subtract_image_from_scalar(test1, result, 5) a = cle.pull_zyx(result) b = cle.pull_zyx(reference) print(a) assert (np.array_equal(a, b))
def test_center_of_mass(): test = cle.push_zyx(np.asarray([ [0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [0, 0, 0, 0, 0] ])) c = cle.center_of_mass(test) print(c) assert (np.array_equal(c, [2, 1, 0]))
def test_copy_slice_mini_x(): np_input = np.asarray([[1, 2, 3, 4]]) gpu_input = cle.push_zyx(np_input) gpu_output = cle.create((1, 1, 4)) cle.copy_slice(gpu_input, gpu_output, 0) print(gpu_output) a = cle.pull(gpu_output) assert (np.min(a) == 1) assert (np.max(a) == 4) assert (np.mean(a) == 2.5)
def test_multiply_image_and_scalar(): test1 = cle.push_zyx(np.asarray([ [0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2] ])) reference = cle.push_zyx(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_zyx(result) b = cle.pull_zyx(reference) print(a) assert (np.array_equal(a, b))
def test_draw_line(): reference = cle.push_zyx( np.asarray([[2, 2, 0, 0, 0], [2, 2, 2, 0, 0], [0, 2, 2, 2, 0], [0, 0, 2, 2, 2], [0, 0, 0, 2, 2]])) result = cle.create((5, 5)) cle.set(result, 0) cle.draw_line(result, 1, 1, 0, 4, 4, 0, 1, 2) print(result) a = cle.pull(result) b = cle.pull(reference) 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_copy_slice_to3d_with_one_slice_zyx(): test1 = cle.push_zyx(np.asarray([[3, 4, 6], [4, 5, 2]])) print(test1) print("shape test1 " + str(test1.shape)) test2 = cle.create((1, 2, 3)) print("shape test2 " + str(test2.shape)) print(test2) cle.copy_slice(test1, test2, 0) print(test2) a = cle.pull(test2) assert (np.min(a) == 2) assert (np.max(a) == 6) assert (np.mean(a) == 4)
def test_replace_intensities(): test1 = cle.push( np.asarray([[0, 0, 0, 0, 0], [0, 1, 2, 3, 0], [0, 2, 3, 4, 0], [0, 4, 4, 5, 0], [0, 0, 0, 0, 0]])) test2 = cle.push_zyx(np.asarray([[0, 9, 8, 7, 6, 5]])) reference = cle.push( np.asarray([[0, 0, 0, 0, 0], [0, 9, 8, 7, 0], [0, 8, 7, 6, 0], [0, 6, 6, 5, 0], [0, 0, 0, 0, 0]])) result = cle.create(test1) cle.replace_intensities(test1, test2, result) print(result) a = cle.pull(result) b = cle.pull(reference) assert (np.allclose(a, b, 0.001))
# Stay tuned and check out http://clesperanto.net to learn more. # Generator version: 0.6.1.2 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))
output = cle.pull_zyx(gpu_output) return output # use of magic_gui also passes an attribute to clij_operation "called" # def print_shape(image): # print('Output image shape ', image.shape) gui = clij_filter.Gui() viewer.window.add_dock_widget(gui) # if a layer gets added or removed, refresh the dropdown choices viewer.layers.events.changed.connect( lambda x: gui.refresh_choices("input")) # clij_operation.called.connect(print_shape) gpu_input = cle.push_zyx(image) import cProfile #cProfile.run('mesh_data(gpu_input, 2, 300)') import time time_stamp = time.time() gpu_output = mesh_data(gpu_input, 2, 300) print("First round took " + str((time.time() - time_stamp) * 1000) + " ms") time_stamp = time.time() gpu_output = mesh_data(gpu_input, 2, 300) print("Second round took " + str((time.time() - time_stamp) * 1000) + " ms")
# Generator version: 0.4.0.8 import pyclesperanto_prototype as cle 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))
width = 512 height = 1024 depth = 71 voxel_size = [3, 0.6934, 0.6934] #image = np.empty((71, 1024, 512), np.uint16) import beetlesafari as bs img_arr = bs.imread_raw(filename, width, height, depth) import pyclesperanto_prototype as cle print("Shape before resampling: " + str(img_arr.shape)) buffer = cle.push_zyx(img_arr) resampled = cle.resample(buffer, factor_x=voxel_size[2], factor_y=voxel_size[1], factor_z=voxel_size[0]) img_arr = cle.pull_zyx(resampled) print("Shape after resampling: " + str(img_arr.shape)) # print(img_arr) # Start up napari import napari with napari.gui_qt(): viewer = napari.Viewer() viewer.add_image(img_arr, name='Tribolium')
cle.select_device('RTX') 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()