From 95e29c71846096854264c1ff3d3bd059ded4a9ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Brito?= <50997716+antmsbrito@users.noreply.github.com> Date: Wed, 12 Mar 2025 14:45:29 +0000 Subject: [PATCH 01/37] Updating the convolution2D method to accept 3D arrays --- ...nanopyx.core.transform._le_convolution.pyx | 87 ++++---- src/nanopyx/core/transform/_le_convolution.cl | 33 +++ .../core/transform/_le_convolution.pyx | 191 ++++++++++-------- src/nanopyx/core/transform/convolution.py | 100 ++++----- 4 files changed, 237 insertions(+), 174 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_convolution.pyx b/src/mako_templates/nanopyx.core.transform._le_convolution.pyx index 3974ce96..6724a449 100644 --- a/src/mako_templates/nanopyx.core.transform._le_convolution.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_convolution.pyx @@ -13,7 +13,7 @@ from cython.parallel import parallel, prange from libc.math cimport cos, sin from .__interpolation_tools__ import check_image, value2array -from .convolution import check_array, convolution2D_cuda, convolution2D_dask, convolution2D_numba, convolution2D_python, convolution2D_transonic +from .convolution import convolution2D_cuda, convolution2D_dask, convolution2D_numba, convolution2D_python, convolution2D_transonic from ...__liquid_engine__ import LiquidEngine from ...__opencl__ import cl, cl_array, _fastest_device @@ -29,14 +29,14 @@ class Convolution(LiquidEngine): clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) def run(self, image, kernel, run_type=None): - image = check_array(image) + image = check_image(image) return self._run(image, kernel, run_type=run_type) def benchmark(self, image, kernel): return super().benchmark(image, kernel) % for sch in schedulers: - def _run_${sch}(self, float[:,:] image, float[:,:] kernel): + def _run_${sch}(self, float[:,:,:] image, float[:,:] kernel): """ @cpu % if sch!='unthreaded': @@ -44,8 +44,9 @@ class Convolution(LiquidEngine): % endif @cython """ - cdef int nRows = image.shape[0] - cdef int nCols = image.shape[1] + cdef int nFrames = image.shape[0] + cdef int nRows = image.shape[1] + cdef int nCols = image.shape[2] cdef int nRows_kernel = kernel.shape[0] cdef int nCols_kernel = kernel.shape[1] @@ -61,27 +62,28 @@ class Convolution(LiquidEngine): cdef float acc = 0 - conv_out = np.zeros((nRows, nCols), dtype=np.float32) - cdef float[:,:] _conv_out = conv_out + conv_out = np.zeros((nFrames, nRows, nCols), dtype=np.float32) + cdef float[:,:,:] _conv_out = conv_out with nogil: - % if sch=='unthreaded': - for r in range(nRows): - for c in range(nCols): - % elif sch=='threaded': - for r in prange(nRows): - for c in prange(nCols): - % else: - for r in prange(nRows,schedule="${sch.split('_')[1]}"): - for c in prange(nCols,schedule="${sch.split('_')[1]}"): - % endif - acc = 0 - for kr in range(nRows_kernel): - for kc in range(nCols_kernel): - local_row = min(max(r+(kr-center_r),0),nRows-1) - local_col = min(max(c+(kc-center_c),0),nCols-1) - acc = acc + kernel[kr,kc] * image[local_row, local_col] - _conv_out[r,c] = acc + for f in range(nFrames): + % if sch=='unthreaded': + for r in range(nRows): + for c in range(nCols): + % elif sch=='threaded': + for r in prange(nRows): + for c in prange(nCols): + % else: + for r in prange(nRows,schedule="${sch.split('_')[1]}"): + for c in prange(nCols,schedule="${sch.split('_')[1]}"): + % endif + acc = 0 + for kr in range(nRows_kernel): + for kc in range(nCols_kernel): + local_row = min(max(r+(kr-center_r),0),nRows-1) + local_col = min(max(c+(kc-center_c),0),nCols-1) + acc = acc + kernel[kr,kc] * image[f,local_row, local_col] + _conv_out[f,r,c] = acc return conv_out @@ -99,26 +101,33 @@ class Convolution(LiquidEngine): dc = device['device'] cl_queue = cl.CommandQueue(cl_ctx) - image_out = np.zeros((image.shape[0], image.shape[1]), dtype=np.float32) + image_out = np.zeros((image.shape[0], image.shape[1], image.shape[2]), dtype=np.float32) mf = cl.mem_flags - input_image = cl.image_from_array(cl_ctx, image, mode='r') - input_kernel = cl.image_from_array(cl_ctx, kernel, mode='r') - output_opencl = cl.image_from_array(cl_ctx, image_out, mode='w') + input_image = cl.Buffer(cl_ctx, mf.READ_ONLY, image.nbytes) + cl.enqueue_copy(cl_queue, input_image, image).wait() + + input_kernel = cl.Buffer(cl_ctx, mf.READ_ONLY, kernel.nbytes) + cl.enqueue_copy(cl_queue, input_kernel, kernel).wait() + + output_opencl = cl.Buffer(cl_ctx, mf.WRITE_ONLY, image_out.nbytes) + + kernelsize = kernel.shape[0] cl_queue.finish() code = self._get_cl_code("_le_convolution.cl", device['DP']) prg = cl.Program(cl_ctx, code).build() - knl = prg.conv2d + knl = prg.conv2d_2 knl(cl_queue, - (1,image.shape[0], image.shape[1]), - self.get_work_group(device['device'],(1,image.shape[0], image.shape[1])), + (image.shape[0],image.shape[1],image.shape[2]), + None,#self.get_work_group(device['device'],(image.shape[0], image.shape[1], image.shape[2])), input_image, output_opencl, - input_kernel).wait() + input_kernel, + np.int32(kernelsize)).wait() - cl.enqueue_copy(cl_queue, image_out, output_opencl,origin=(0,0), region=(image.shape[0], image.shape[1])).wait() + cl.enqueue_copy(cl_queue, image_out, output_opencl).wait() cl_queue.finish() return image_out @@ -129,12 +138,12 @@ class Convolution(LiquidEngine): """ return convolution2D_python(image, kernel).astype(np.float32) - def _run_transonic(self, image, kernel): - """ - @cpu - @threaded - """ - return convolution2D_transonic(image, kernel).astype(np.float32) + # def _run_transonic(self, image, kernel): + # """ + # @cpu + # @threaded + # """ + # return convolution2D_transonic(image, kernel).astype(np.float32) def _run_dask(self, image, kernel): """ diff --git a/src/nanopyx/core/transform/_le_convolution.cl b/src/nanopyx/core/transform/_le_convolution.cl index 61913d38..f917c62e 100644 --- a/src/nanopyx/core/transform/_le_convolution.cl +++ b/src/nanopyx/core/transform/_le_convolution.cl @@ -25,3 +25,36 @@ conv2d(__read_only image2d_t image_in, __write_only image2d_t image_out, __read_ write_imagef(image_out,image_coords,acc); } +__kernel void +conv2d_2(__global float *image, __global float *image_out, __global float *kernel_array, int kernel_size){ + + int frame = get_global_id(0); + int row = get_global_id(1); + int col = get_global_id(2); + + int nframes = get_global_size(0); + int nrows = get_global_size(1); + int ncols = get_global_size(2); + + int kernel_center = (kernel_size-1)/2; + + float acc = 0; + int localrow; + int localcol; + + for (int kr = 0; kr Date: Wed, 12 Mar 2025 15:31:51 +0000 Subject: [PATCH 02/37] Fixing wrong index to clamp to edge --- src/nanopyx/core/transform/_le_convolution.cl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nanopyx/core/transform/_le_convolution.cl b/src/nanopyx/core/transform/_le_convolution.cl index f917c62e..62380cf6 100644 --- a/src/nanopyx/core/transform/_le_convolution.cl +++ b/src/nanopyx/core/transform/_le_convolution.cl @@ -46,8 +46,8 @@ conv2d_2(__global float *image, __global float *image_out, __global float *kerne for (int kc = 0; kc Date: Wed, 12 Mar 2025 15:45:46 +0000 Subject: [PATCH 03/37] Updating to match new Convolution2D output --- src/nanopyx/core/analysis/_le_channel_registration.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nanopyx/core/analysis/_le_channel_registration.pyx b/src/nanopyx/core/analysis/_le_channel_registration.pyx index 09c6e4c0..682f6fc2 100644 --- a/src/nanopyx/core/analysis/_le_channel_registration.pyx +++ b/src/nanopyx/core/analysis/_le_channel_registration.pyx @@ -85,8 +85,8 @@ def _gaussian_filter(inpimg, _runtype, sigma): knl1 = knl1.astype(np.float32) img1 = conv.run(inpimg,knl1.reshape((len(x1), 1)),run_type=_runtype) - img2 = conv.run(img1,knl1.reshape((1, len(x1))),run_type=_runtype) - + img2 = conv.run(img1,knl1.reshape((1, len(x1))),run_type=_runtype)[0] + return img2 class ChannelRegistrationEstimator(LiquidEngine): From eb2701d5e30ac1fdea993322f48b5985b8bdaa40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Brito?= <50997716+antmsbrito@users.noreply.github.com> Date: Wed, 12 Mar 2025 15:46:09 +0000 Subject: [PATCH 04/37] Updating to match previous new Convolution2D output --- .../nanopyx.core.analysis._le_channel_registration.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mako_templates/nanopyx.core.analysis._le_channel_registration.pyx b/src/mako_templates/nanopyx.core.analysis._le_channel_registration.pyx index ec8db173..7e109e5e 100644 --- a/src/mako_templates/nanopyx.core.analysis._le_channel_registration.pyx +++ b/src/mako_templates/nanopyx.core.analysis._le_channel_registration.pyx @@ -87,8 +87,8 @@ def _gaussian_filter(inpimg, _runtype, sigma): knl1 = knl1.astype(np.float32) img1 = conv.run(inpimg,knl1.reshape((len(x1), 1)),run_type=_runtype) - img2 = conv.run(img1,knl1.reshape((1, len(x1))),run_type=_runtype) - + img2 = conv.run(img1,knl1.reshape((1, len(x1))),run_type=_runtype)[0] + return img2 class ChannelRegistrationEstimator(LiquidEngine): From e748b0023036e83df37fb7159ae07c4d7fae352c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Brito?= <50997716+antmsbrito@users.noreply.github.com> Date: Thu, 13 Mar 2025 10:27:06 +0000 Subject: [PATCH 05/37] Update benchmark signature --- .../_le_convolution/Convolution.yml | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/liquid_benchmarks/_le_convolution/Convolution.yml b/src/liquid_benchmarks/_le_convolution/Convolution.yml index 1c3a22a0..83a19941 100644 --- a/src/liquid_benchmarks/_le_convolution/Convolution.yml +++ b/src/liquid_benchmarks/_le_convolution/Convolution.yml @@ -1,5 +1,5 @@ Cuda: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.0004861999999974387 - 0.0004765000000048758 @@ -22,7 +22,7 @@ Cuda: - 0.00047459999998977764 - 0.0004846000000213735 Dask: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.00937319999999886 - 0.00935059999999055 @@ -45,7 +45,7 @@ Dask: - 0.009444899999976997 - 0.008983199999988756 Numba: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.005503999999987741 - 0.005404399999989096 @@ -68,7 +68,7 @@ Numba: - 0.006126800000004096 - 0.00559069999999906 OpenCL: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.09695740000000796 - 0.0791657999999984 @@ -91,7 +91,7 @@ OpenCL: - 0.07781260000001566 - 0.07502910000002316 Python: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 2.4462038000000064 - 2.4630003999999985 @@ -114,7 +114,7 @@ Python: - 2.3908779999999865 - 2.423667199999983 Threaded: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.000824199999996722 - 0.0008181999999976597 @@ -137,7 +137,7 @@ Threaded: - 0.0007410999999990509 - 0.00074050000000625 Threaded_dynamic: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.0004450999999932037 - 0.00044110000000330274 @@ -160,7 +160,7 @@ Threaded_dynamic: - 0.00042849999999816646 - 0.00042849999999816646 Threaded_guided: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.00045450000000357704 - 0.0004828000000003385 @@ -183,7 +183,7 @@ Threaded_guided: - 0.0004787000000021635 - 0.000453700000008439 Threaded_static: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.0004264999999890051 - 0.0006580000000013797 @@ -206,7 +206,7 @@ Threaded_static: - 0.00047909999997841624 - 0.0004892999999981384 Transonic: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.005411799999990308 - 0.00537510000000907 @@ -229,7 +229,7 @@ Transonic: - 0.005397600000009106 - 0.005529200000012224 Unthreaded: - (['shape(100, 100)', 'shape(23, 23)'], {}): + (['shape(1, 100, 100)', 'shape(23, 23)'], {}): - 5290000 - 0.006530499999996664 - 0.0064456000000063796 From 00501c50b73abe4c7ae5806045bfd40a175c7bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Brito?= <50997716+antmsbrito@users.noreply.github.com> Date: Thu, 13 Mar 2025 10:27:33 +0000 Subject: [PATCH 06/37] Updating input checks for 3D arrays --- .../nanopyx.core.transform._le_convolution.pyx | 13 +++++++------ src/nanopyx/core/transform/_le_convolution.pyx | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_convolution.pyx b/src/mako_templates/nanopyx.core.transform._le_convolution.pyx index 6724a449..607488de 100644 --- a/src/mako_templates/nanopyx.core.transform._le_convolution.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_convolution.pyx @@ -33,6 +33,7 @@ class Convolution(LiquidEngine): return self._run(image, kernel, run_type=run_type) def benchmark(self, image, kernel): + image = check_image(image) return super().benchmark(image, kernel) % for sch in schedulers: @@ -138,12 +139,12 @@ class Convolution(LiquidEngine): """ return convolution2D_python(image, kernel).astype(np.float32) - # def _run_transonic(self, image, kernel): - # """ - # @cpu - # @threaded - # """ - # return convolution2D_transonic(image, kernel).astype(np.float32) + def _run_transonic(self, image, kernel): + """ + @cpu + @threaded + """ + return convolution2D_transonic(image, kernel).astype(np.float32) def _run_dask(self, image, kernel): """ diff --git a/src/nanopyx/core/transform/_le_convolution.pyx b/src/nanopyx/core/transform/_le_convolution.pyx index b6a235f6..df8f622c 100644 --- a/src/nanopyx/core/transform/_le_convolution.pyx +++ b/src/nanopyx/core/transform/_le_convolution.pyx @@ -31,6 +31,7 @@ class Convolution(LiquidEngine): return self._run(image, kernel, run_type=run_type) def benchmark(self, image, kernel): + image = check_image(image) return super().benchmark(image, kernel) def _run_unthreaded(self, float[:,:,:] image, float[:,:] kernel): @@ -287,12 +288,12 @@ class Convolution(LiquidEngine): """ return convolution2D_python(image, kernel).astype(np.float32) - # def _run_transonic(self, image, kernel): - # """ - # @cpu - # @threaded - # """ - # return convolution2D_transonic(image, kernel).astype(np.float32) + def _run_transonic(self, image, kernel): + """ + @cpu + @threaded + """ + return convolution2D_transonic(image, kernel).astype(np.float32) def _run_dask(self, image, kernel): """ From c50ee1bb4325aa8d710598b3999ccc92cf5f09db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Brito?= <50997716+antmsbrito@users.noreply.github.com> Date: Thu, 13 Mar 2025 10:27:58 +0000 Subject: [PATCH 07/37] Updating dask and transonic implementations to accept 3D arrays --- src/nanopyx/core/transform/_transonic.py | 28 ++++++++++++----------- src/nanopyx/core/transform/convolution.py | 6 ++++- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/nanopyx/core/transform/_transonic.py b/src/nanopyx/core/transform/_transonic.py index 75663b06..96212ed5 100644 --- a/src/nanopyx/core/transform/_transonic.py +++ b/src/nanopyx/core/transform/_transonic.py @@ -4,8 +4,9 @@ @jit(backend="numba") def convolution2D(image, kernel): - nRows = image.shape[0] - nCols = image.shape[1] + nFrames = image.shape[0] + nRows = image.shape[1] + nCols = image.shape[2] nRows_kernel = kernel.shape[0] nCols_kernel = kernel.shape[1] @@ -15,16 +16,17 @@ def convolution2D(image, kernel): acc = 0.0 - conv_out = np.zeros((nRows, nCols), dtype=np.float32) - - for r in range(nRows): - for c in range(nCols): - acc = 0 - for kr in range(nRows_kernel): - for kc in range(nCols_kernel): - local_row = min(max(r+(kr-center_r), 0), nRows-1) - local_col = min(max(c+(kc-center_c), 0), nCols-1) - acc = acc + kernel[kr, kc] * image[local_row, local_col] - conv_out[r, c] = acc + conv_out = np.zeros((nFrames, nRows, nCols), dtype=np.float32) + + for f in range(nFrames): + for r in range(nRows): + for c in range(nCols): + acc = 0 + for kr in range(nRows_kernel): + for kc in range(nCols_kernel): + local_row = min(max(r+(kr-center_r), 0), nRows-1) + local_col = min(max(c+(kc-center_c), 0), nCols-1) + acc = acc + kernel[kr, kc] * image[f, local_row, local_col] + conv_out[f, r, c] = acc return conv_out diff --git a/src/nanopyx/core/transform/convolution.py b/src/nanopyx/core/transform/convolution.py index 8c375850..e850dd98 100644 --- a/src/nanopyx/core/transform/convolution.py +++ b/src/nanopyx/core/transform/convolution.py @@ -129,7 +129,11 @@ def convolution2D_numba(image, kernel): def convolution2D_dask(image, kernel): - return np.asarray(dask_convolve(da.from_array(image), da.from_array(kernel.reshape(1,*kernel.shape)))) + + conv_out = np.zeros_like(image) + for i in range(image.shape[0]): + conv_out[i] = dask_convolve(da.from_array(image[i]), da.from_array(kernel)) + return conv_out def convolution2D_cuda(image, kernel): From bdc3070c8289497434333788afc5fcb11c603ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Brito?= <50997716+antmsbrito@users.noreply.github.com> Date: Thu, 13 Mar 2025 10:28:20 +0000 Subject: [PATCH 08/37] Add another test to check if 2D convolution still works fine --- tests/test_conv2d.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/test_conv2d.py b/tests/test_conv2d.py index 023969dd..145284d3 100644 --- a/tests/test_conv2d.py +++ b/tests/test_conv2d.py @@ -5,14 +5,22 @@ def test_conv_small(): - small = np.random.random((100, 100)).astype(np.float32) + small = np.random.random((10,100, 100)).astype(np.float32) kernel = np.ones((23, 23)).astype(np.float32) conv = Convolution() conv.benchmark(small, kernel) def test_convolution2D_numba(): - img = np.random.random((100, 100)).astype(np.float32) + img = np.random.random((10,100, 100)).astype(np.float32) kernel = np.random.random((3, 3)).astype(np.float32) convolution2D_numba(img, kernel) + + +def test_conv_small_2d(): + small = np.random.random((100, 100)).astype(np.float32) + kernel = np.ones((23, 23)).astype(np.float32) + conv = Convolution() + conv.benchmark(small, kernel) + From 1ee10b6be6a833f41d60613b13503c8b08f40d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Brito?= <50997716+antmsbrito@users.noreply.github.com> Date: Fri, 14 Mar 2025 10:11:53 +0000 Subject: [PATCH 09/37] Adding tentative new parameters to RGC (opencl only so far) --- ...nsform._le_radial_gradient_convergence.pyx | 20 ++++++---- .../_le_radial_gradient_convergence.cl | 40 ++++++++++++++----- .../_le_radial_gradient_convergence.pyx | 26 +++++++----- 3 files changed, 58 insertions(+), 28 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx index 3ad05ca6..a3c12416 100644 --- a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx @@ -27,20 +27,20 @@ class RadialGradientConvergence(LiquidEngine): verbose=verbose) - def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, run_type = None): + def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, xyoffset: float = 0, angle: float = 0, run_type = None): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, run_type=run_type) + return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting,xyoffset, angle, run_type=run_type) - def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True): + def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, xyoffset: float = 0, angle: float = 0): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting) + return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): """ @cpu @cython @@ -73,7 +73,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) % for sch in schedulers: - def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): """ @cpu @threaded @@ -111,7 +111,7 @@ class RadialGradientConvergence(LiquidEngine): % endfor - def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, device=None, int mem_div=1): + def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0, device=None, int mem_div=1): """ @gpu """ @@ -135,6 +135,8 @@ class RadialGradientConvergence(LiquidEngine): cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting + cdef float _xyoffset = xyoffset + cdef float _angle = angle # Sizes cdef int nFrames = gradient_col_interp.shape[0] @@ -194,7 +196,9 @@ class RadialGradientConvergence(LiquidEngine): np.float32(tSO), np.float32(tSS), np.float32(_sensitivity), - np.int32(_doIntensityWeighting)).wait() + np.int32(_doIntensityWeighting), + np.float32(_xyoffset), + np.float32(_angle)).wait() # Copy output cl.enqueue_copy(cl_queue, rgc_map[i:i+n_slices,:,:], rgc_map_out).wait() diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index 258e473c..b6a62dbc 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -1,7 +1,9 @@ -float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity); +float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float xyoffset, float angle); double _c_calculate_dw(double distance, double tSS); double _c_calculate_dk(float Gx, float Gy, float dx, float dy, float distance); +float2 _rotation_matrix(float2 point, float angle); + // RGC takes as input the interpolated intensity gradients in the x and y directions // calculate distance weight @@ -19,11 +21,24 @@ double _c_calculate_dk(float Gx, float Gy, float dx, float dy, float distance) { return Dk; } +float2 _rotation_matrix(float2 point, float angle){ + + //xcos - ysin + //xsin + ycos + + float x = point.x; + float y = point.y; + + return (float2)(x*cos(angle) - y*sin(angle), x*sin(angle) + y*cos(angle)); +} + // calculate radial gradient convergence for a single subpixel -float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity) { +float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float xyoffset, float angle) { float vx, vy, Gx, Gy, dx, dy, distance, distanceWeight, GdotR, Dk; + float2 correctedv; + float2 correctedd; float xc = (xM + 0.5) / magnification; float yc = (yM + 0.5) / magnification; @@ -49,15 +64,22 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* distance = sqrt(dx * dx + dy * dy); if (distance != 0 && distance <= tSO) { - Gx = imIntGx[(int)(vy * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)(vx * magnification * Gx_Gy_MAGNIFICATION)]; - Gy = imIntGy[(int)(vy * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)(vx * magnification * Gx_Gy_MAGNIFICATION)]; + + + correctedv = _rotation_matrix((float2)(dy*magnification*Gx_Gy_MAGNIFICATION,dx*magnification*Gx_Gy_MAGNIFICATION), angle); + correctedv = (float2)(correctedv.x + (yc + xyoffset)*magnification*Gx_Gy_MAGNIFICATION, correctedv.y + (xc + xyoffset)*magnification*Gx_Gy_MAGNIFICATION); + + Gx = imIntGx[(int)((correctedv.x) * colsM * Gx_Gy_MAGNIFICATION) + (int)((correctedv.y))]; + Gy = imIntGy[(int)((correctedv.x) * colsM * Gx_Gy_MAGNIFICATION) + (int)((correctedv.y))]; distanceWeight = _c_calculate_dw(distance, tSS); distanceWeightSum += distanceWeight; - GdotR = Gx*dx + Gy*dy; + + correctedd = _rotation_matrix((float2)(dy,dx), angle); + GdotR = Gx*correctedd.y + Gy*correctedd.x; if (GdotR < 0) { - Dk = _c_calculate_dk(Gx, Gy, dx, dy, distance); + Dk = _c_calculate_dk(Gx, Gy, correctedd.y, correctedd.x, distance); RGC += Dk * distanceWeight; } } @@ -78,7 +100,7 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* } - __kernel void calculate_rgc(__global float* imIntGx, __global float* imIntGy, __global float* imInt, __global float* image_out, int nCols, int nRows, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, int doIntensityWeighting) { + __kernel void calculate_rgc(__global float* imIntGx, __global float* imIntGy, __global float* imInt, __global float* image_out, int nCols, int nRows, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, int doIntensityWeighting, float xyoffset, float angle) { // Index of the current pixel int f = get_global_id(0); @@ -95,9 +117,9 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* col = col + magnification*2; if (doIntensityWeighting == 1) { - image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity) * imInt[f * nPixels_out + row * nCols + col]; + image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, xyoffset, angle) * imInt[f * nPixels_out + row * nCols + col]; } else { - image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity); + image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, xyoffset, angle); } } diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx index 0e9d1ab1..78c12817 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx @@ -25,20 +25,20 @@ class RadialGradientConvergence(LiquidEngine): verbose=verbose) - def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, run_type = None): + def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, xyoffset: float = 0, angle: float = 0, run_type = None): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, run_type=run_type) + return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting,xyoffset, angle, run_type=run_type) - def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True): + def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, xyoffset: float = 0, angle: float = 0): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting) + return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): """ @cpu @cython @@ -70,7 +70,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): """ @cpu @threaded @@ -101,7 +101,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): """ @cpu @threaded @@ -132,7 +132,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): """ @cpu @threaded @@ -163,7 +163,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): """ @cpu @threaded @@ -196,7 +196,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) - def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, device=None, int mem_div=1): + def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0, device=None, int mem_div=1): """ @gpu """ @@ -220,6 +220,8 @@ class RadialGradientConvergence(LiquidEngine): cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting + cdef float _xyoffset = xyoffset + cdef float _angle = angle # Sizes cdef int nFrames = gradient_col_interp.shape[0] @@ -279,7 +281,9 @@ class RadialGradientConvergence(LiquidEngine): np.float32(tSO), np.float32(tSS), np.float32(_sensitivity), - np.int32(_doIntensityWeighting)).wait() + np.int32(_doIntensityWeighting), + np.float32(_xyoffset), + np.float32(_angle)).wait() # Copy output cl.enqueue_copy(cl_queue, rgc_map[i:i+n_slices,:,:], rgc_map_out).wait() From 37251afa6dae968cea44ddbfeb03c331aaf5ee50 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Fri, 14 Mar 2025 14:11:42 +0000 Subject: [PATCH 10/37] fixing angle rotation for %2 kernels --- .../_le_radial_gradient_convergence.cl | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index b6a62dbc..fece72e3 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -21,6 +21,14 @@ double _c_calculate_dk(float Gx, float Gy, float dx, float dy, float distance) { return Dk; } +float2 _rotate_vector(float Gx, float Gy, float angle) { + float cos_angle = cos(angle); + float sin_angle = sin(angle); + float rotated_Gx = Gx * cos_angle - Gy * sin_angle; + float rotated_Gy = Gx * sin_angle + Gy * cos_angle; + return (float2)(rotated_Gx, rotated_Gy); +} + float2 _rotation_matrix(float2 point, float angle){ //xcos - ysin @@ -64,22 +72,20 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* distance = sqrt(dx * dx + dy * dy); if (distance != 0 && distance <= tSO) { - + Gx = imIntGx[(int)((vy+xyoffset) * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)((vx+xyoffset) * magnification * Gx_Gy_MAGNIFICATION)]; + Gy = imIntGy[(int)((vy+xyoffset) * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)((vx+xyoffset) * magnification * Gx_Gy_MAGNIFICATION)]; - correctedv = _rotation_matrix((float2)(dy*magnification*Gx_Gy_MAGNIFICATION,dx*magnification*Gx_Gy_MAGNIFICATION), angle); - correctedv = (float2)(correctedv.x + (yc + xyoffset)*magnification*Gx_Gy_MAGNIFICATION, correctedv.y + (xc + xyoffset)*magnification*Gx_Gy_MAGNIFICATION); - - Gx = imIntGx[(int)((correctedv.x) * colsM * Gx_Gy_MAGNIFICATION) + (int)((correctedv.y))]; - Gy = imIntGy[(int)((correctedv.x) * colsM * Gx_Gy_MAGNIFICATION) + (int)((correctedv.y))]; + // Rotate the gradient components + float2 rotatedG = _rotate_vector(Gx, Gy, angle); + Gx = rotatedG.x; + Gy = rotatedG.y; distanceWeight = _c_calculate_dw(distance, tSS); distanceWeightSum += distanceWeight; - - correctedd = _rotation_matrix((float2)(dy,dx), angle); - GdotR = Gx*correctedd.y + Gy*correctedd.x; + GdotR = Gx*dx + Gy*dy; if (GdotR < 0) { - Dk = _c_calculate_dk(Gx, Gy, correctedd.y, correctedd.x, distance); + Dk = _c_calculate_dk(Gx, Gy, dx, dy, distance); RGC += Dk * distanceWeight; } } From aa0cd97ac6bfdadb65a04aa194b64e01ef2915e8 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Thu, 20 Mar 2025 14:47:29 +0000 Subject: [PATCH 11/37] adding missing fixes from branch --- .../_le_radial_gradient_convergence.cl | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index b6a62dbc..112b5cc0 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -21,6 +21,14 @@ double _c_calculate_dk(float Gx, float Gy, float dx, float dy, float distance) { return Dk; } +float2 _rotate_vector(float Gx, float Gy, float angle) { + float cos_angle = cos(angle); + float sin_angle = sin(angle); + float rotated_Gx = Gx * cos_angle - Gy * sin_angle; + float rotated_Gy = Gx * sin_angle + Gy * cos_angle; + return (float2)(rotated_Gx, rotated_Gy); +} + float2 _rotation_matrix(float2 point, float angle){ //xcos - ysin @@ -64,24 +72,24 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* distance = sqrt(dx * dx + dy * dy); if (distance != 0 && distance <= tSO) { - - - correctedv = _rotation_matrix((float2)(dy*magnification*Gx_Gy_MAGNIFICATION,dx*magnification*Gx_Gy_MAGNIFICATION), angle); - correctedv = (float2)(correctedv.x + (yc + xyoffset)*magnification*Gx_Gy_MAGNIFICATION, correctedv.y + (xc + xyoffset)*magnification*Gx_Gy_MAGNIFICATION); + Gx = imIntGx[(int)((vy+xyoffset) * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)((vx+xyoffset) * magnification * Gx_Gy_MAGNIFICATION)]; + Gy = imIntGy[(int)((vy+xyoffset) * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)((vx+xyoffset) * magnification * Gx_Gy_MAGNIFICATION)]; - Gx = imIntGx[(int)((correctedv.x) * colsM * Gx_Gy_MAGNIFICATION) + (int)((correctedv.y))]; - Gy = imIntGy[(int)((correctedv.x) * colsM * Gx_Gy_MAGNIFICATION) + (int)((correctedv.y))]; + // Rotate the gradient components + float2 rotatedG = _rotate_vector(Gx, Gy, angle); + Gx = rotatedG.x; + Gy = rotatedG.y; distanceWeight = _c_calculate_dw(distance, tSS); distanceWeightSum += distanceWeight; - - correctedd = _rotation_matrix((float2)(dy,dx), angle); - GdotR = Gx*correctedd.y + Gy*correctedd.x; - - if (GdotR < 0) { - Dk = _c_calculate_dk(Gx, Gy, correctedd.y, correctedd.x, distance); - RGC += Dk * distanceWeight; - } + GdotR = Gx*dx + Gy*dy; + + // if (GdotR < 0) { + // Dk = _c_calculate_dk(Gx, Gy, dx, dy, distance); + // RGC += Dk * distanceWeight; + // } + Dk = _c_calculate_dk(Gx, Gy, dx, dy, distance); + RGC += Dk * distanceWeight; } } } @@ -93,7 +101,7 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* if (RGC >= 0 && sensitivity > 1) { RGC = pow(RGC, sensitivity); } else if (RGC < 0) { - RGC = 0; + RGC = RGC; } return RGC; @@ -117,7 +125,7 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* col = col + magnification*2; if (doIntensityWeighting == 1) { - image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, xyoffset, angle) * imInt[f * nPixels_out + row * nCols + col]; + image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, xyoffset, angle) * fabs(imInt[f * nPixels_out + row * nCols + col]); } else { image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, xyoffset, angle); From 0a3cb93a2809e7845e9207839890d2f2cc530cfd Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Thu, 20 Mar 2025 15:08:06 +0000 Subject: [PATCH 12/37] removed unrelated changes --- ...nsform._le_radial_gradient_convergence.pyx | 12 +++++---- .../_le_radial_gradient_convergence.cl | 26 +++++++++---------- .../_le_radial_gradient_convergence.pyx | 18 +++++++------ 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx index a3c12416..e166d5f6 100644 --- a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx @@ -27,20 +27,20 @@ class RadialGradientConvergence(LiquidEngine): verbose=verbose) - def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, xyoffset: float = 0, angle: float = 0, run_type = None): + def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting,xyoffset, angle, run_type=run_type) - def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, xyoffset: float = 0, angle: float = 0): + def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @cython @@ -73,7 +73,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) % for sch in schedulers: - def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): + def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -111,7 +111,7 @@ class RadialGradientConvergence(LiquidEngine): % endfor - def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0, device=None, int mem_div=1): + def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): """ @gpu """ @@ -135,6 +135,7 @@ class RadialGradientConvergence(LiquidEngine): cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting + cdef float _offset = offset cdef float _xyoffset = xyoffset cdef float _angle = angle @@ -197,6 +198,7 @@ class RadialGradientConvergence(LiquidEngine): np.float32(tSS), np.float32(_sensitivity), np.int32(_doIntensityWeighting), + np.float32(_offset), np.float32(_xyoffset), np.float32(_angle)).wait() diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index 112b5cc0..fd3ed75a 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -1,4 +1,4 @@ -float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float xyoffset, float angle); +float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float offset, float xyoffset, float angle); double _c_calculate_dw(double distance, double tSS); double _c_calculate_dk(float Gx, float Gy, float dx, float dy, float distance); @@ -42,14 +42,14 @@ float2 _rotation_matrix(float2 point, float angle){ // calculate radial gradient convergence for a single subpixel -float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float xyoffset, float angle) { +float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float offset, float xyoffset, float angle) { float vx, vy, Gx, Gy, dx, dy, distance, distanceWeight, GdotR, Dk; float2 correctedv; float2 correctedd; - float xc = (xM + 0.5) / magnification; - float yc = (yM + 0.5) / magnification; + float xc = (xM + offset) / magnification; + float yc = (yM + offset) / magnification; float RGC = 0; float distanceWeightSum = 0; @@ -84,12 +84,10 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* distanceWeightSum += distanceWeight; GdotR = Gx*dx + Gy*dy; - // if (GdotR < 0) { - // Dk = _c_calculate_dk(Gx, Gy, dx, dy, distance); - // RGC += Dk * distanceWeight; - // } - Dk = _c_calculate_dk(Gx, Gy, dx, dy, distance); - RGC += Dk * distanceWeight; + if (GdotR < 0) { + Dk = _c_calculate_dk(Gx, Gy, dx, dy, distance); + RGC += Dk * distanceWeight; + } } } } @@ -101,14 +99,14 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* if (RGC >= 0 && sensitivity > 1) { RGC = pow(RGC, sensitivity); } else if (RGC < 0) { - RGC = RGC; + RGC = 0; } return RGC; } - __kernel void calculate_rgc(__global float* imIntGx, __global float* imIntGy, __global float* imInt, __global float* image_out, int nCols, int nRows, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, int doIntensityWeighting, float xyoffset, float angle) { + __kernel void calculate_rgc(__global float* imIntGx, __global float* imIntGy, __global float* imInt, __global float* image_out, int nCols, int nRows, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, int doIntensityWeighting, float offset, float xyoffset, float angle) { // Index of the current pixel int f = get_global_id(0); @@ -125,9 +123,9 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* col = col + magnification*2; if (doIntensityWeighting == 1) { - image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, xyoffset, angle) * fabs(imInt[f * nPixels_out + row * nCols + col]); + image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, offset, xyoffset, angle) * imInt[f * nPixels_out + row * nCols + col]; } else { - image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, xyoffset, angle); + image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, offset, xyoffset, angle); } } diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx index 78c12817..4a74de5a 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx @@ -25,20 +25,20 @@ class RadialGradientConvergence(LiquidEngine): verbose=verbose) - def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, xyoffset: float = 0, angle: float = 0, run_type = None): + def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting,xyoffset, angle, run_type=run_type) - def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, xyoffset: float = 0, angle: float = 0): + def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @cython @@ -70,7 +70,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): + def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -101,7 +101,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): + def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -132,7 +132,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): + def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -163,7 +163,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0): + def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -196,7 +196,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) - def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, xyoffset=0, angle=0, device=None, int mem_div=1): + def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): """ @gpu """ @@ -220,6 +220,7 @@ class RadialGradientConvergence(LiquidEngine): cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting + cdef float _offset = offset cdef float _xyoffset = xyoffset cdef float _angle = angle @@ -282,6 +283,7 @@ class RadialGradientConvergence(LiquidEngine): np.float32(tSS), np.float32(_sensitivity), np.int32(_doIntensityWeighting), + np.float32(_offset), np.float32(_xyoffset), np.float32(_angle)).wait() From 051129cc88eeb751ce4977ef8dec536bc76bb94e Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Mon, 24 Mar 2025 13:49:51 +0000 Subject: [PATCH 13/37] adding more updates --- ...nanopyx.core.transform._le_radial_gradient_convergence.pyx | 4 ++-- src/nanopyx/core/transform/_le_radial_gradient_convergence.cl | 4 ++-- .../core/transform/_le_radial_gradient_convergence.pyx | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx index e166d5f6..f1b188a2 100644 --- a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx @@ -31,14 +31,14 @@ class RadialGradientConvergence(LiquidEngine): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting,xyoffset, angle, run_type=run_type) + return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle, run_type=run_type) def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, xyoffset, angle) + return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index fd3ed75a..53305618 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -48,8 +48,8 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* float2 correctedv; float2 correctedd; - float xc = (xM + offset) / magnification; - float yc = (yM + offset) / magnification; + float xc = (float)xM / magnification + offset; // offset in non-magnified space + float yc = (float)yM / magnification + offset; float RGC = 0; float distanceWeightSum = 0; diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx index 4a74de5a..54c01d83 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx @@ -29,14 +29,14 @@ class RadialGradientConvergence(LiquidEngine): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting,xyoffset, angle, run_type=run_type) + return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle, run_type=run_type) def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, xyoffset, angle) + return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ From 8672d347561d75099fe02d4e1f6f590b619c2c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Brito?= <50997716+antmsbrito@users.noreply.github.com> Date: Mon, 24 Mar 2025 13:59:34 +0000 Subject: [PATCH 14/37] Fixing if clauses --- src/nanopyx/core/transform/_le_radial_gradient_convergence.cl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index 53305618..1f11f555 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -61,12 +61,12 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* vy = (int)(Gx_Gy_MAGNIFICATION * yc) + j; vy /= Gx_Gy_MAGNIFICATION; - if (0 < vy && vy <= rowsM - 1) { + if (0 < vy && vy <= (rowsM/magnification) - 1) { for (int i = _start; i < _end; i++) { vx = (int)(Gx_Gy_MAGNIFICATION * xc) + i; vx /= Gx_Gy_MAGNIFICATION; - if (0 < vx && vx <= colsM - 1) { + if (0 < vx && vx <= (colsM/magnification) - 1) { dx = vx - xc; dy = vy - yc; distance = sqrt(dx * dx + dy * dy); From e747f0041e679147660f5cc7e9a0a29a773d0421 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Mon, 24 Mar 2025 14:21:00 +0000 Subject: [PATCH 15/37] another iteration --- ...nsform._le_radial_gradient_convergence.pyx | 20 ++++++------ .../_le_radial_gradient_convergence.cl | 4 +-- .../_le_radial_gradient_convergence.pyx | 32 +++++++++---------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx index f1b188a2..8e96abc4 100644 --- a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx @@ -27,20 +27,20 @@ class RadialGradientConvergence(LiquidEngine): verbose=verbose) - def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): + def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle, run_type=run_type) + return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, grad_magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle, run_type=run_type) - def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) + return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, grad_magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @cython @@ -49,7 +49,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float Gx_Gy_MAGNIFICATION = 2.0 + cdef float Gx_Gy_MAGNIFICATION = grad_magnification cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting @@ -73,7 +73,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) % for sch in schedulers: - def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -83,7 +83,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float Gx_Gy_MAGNIFICATION = 2.0 + cdef float Gx_Gy_MAGNIFICATION = grad_magnification cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting @@ -111,7 +111,7 @@ class RadialGradientConvergence(LiquidEngine): % endfor - def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): + def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): """ @gpu """ @@ -131,7 +131,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float Gx_Gy_MAGNIFICATION = 2.0 + cdef float Gx_Gy_MAGNIFICATION = grad_magnification cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index 53305618..619409ca 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -119,8 +119,8 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* // gradient image dimensions int nPixels_grad = nRows*Gx_Gy_MAGNIFICATION * nCols*Gx_Gy_MAGNIFICATION; - row = row + magnification*2; - col = col + magnification*2; + row = row + magnification*Gx_Gy_MAGNIFICATION; + col = col + magnification*Gx_Gy_MAGNIFICATION; if (doIntensityWeighting == 1) { image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, offset, xyoffset, angle) * imInt[f * nPixels_out + row * nCols + col]; diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx index 54c01d83..7e119b66 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx @@ -25,20 +25,20 @@ class RadialGradientConvergence(LiquidEngine): verbose=verbose) - def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): + def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle, run_type=run_type) + return self._run(gradient_col_interp, gradient_row_interp, image_interp, magnification, grad_magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle, run_type=run_type) - def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def benchmark(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) - return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) + return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, grad_magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @cython @@ -47,7 +47,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float Gx_Gy_MAGNIFICATION = 2.0 + cdef float Gx_Gy_MAGNIFICATION = grad_magnification cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting @@ -70,7 +70,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -80,7 +80,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float Gx_Gy_MAGNIFICATION = 2.0 + cdef float Gx_Gy_MAGNIFICATION = grad_magnification cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting @@ -101,7 +101,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -111,7 +111,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float Gx_Gy_MAGNIFICATION = 2.0 + cdef float Gx_Gy_MAGNIFICATION = grad_magnification cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting @@ -132,7 +132,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -142,7 +142,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float Gx_Gy_MAGNIFICATION = 2.0 + cdef float Gx_Gy_MAGNIFICATION = grad_magnification cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting @@ -163,7 +163,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): """ @cpu @threaded @@ -173,7 +173,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float Gx_Gy_MAGNIFICATION = 2.0 + cdef float Gx_Gy_MAGNIFICATION = grad_magnification cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting @@ -196,7 +196,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) - def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): + def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): """ @gpu """ @@ -216,7 +216,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float Gx_Gy_MAGNIFICATION = 2.0 + cdef float Gx_Gy_MAGNIFICATION = grad_magnification cdef int _magnification = magnification cdef float _sensitivity = sensitivity cdef int _doIntensityWeighting = doIntensityWeighting From e73df52740ce09463432f22f8ec9f64d7b78f085 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Mon, 24 Mar 2025 15:20:48 +0000 Subject: [PATCH 16/37] fixed radius calculation, and edges calculation --- ...e.transform._le_radial_gradient_convergence.pyx | 8 ++++---- .../transform/_le_radial_gradient_convergence.cl | 14 ++++++-------- .../transform/_le_radial_gradient_convergence.pyx | 8 ++++---- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx index 8e96abc4..e9109986 100644 --- a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx @@ -145,10 +145,10 @@ class RadialGradientConvergence(LiquidEngine): cdef int cols_interpolated = (gradient_row_interp.shape[2] / Gx_Gy_MAGNIFICATION) # Grid size of the global work space - lowest_row = _magnification*2 - highest_row = rows_interpolated - _magnification*2 - lowest_col = _magnification*2 - highest_col = cols_interpolated - _magnification*2 + lowest_row = int(radius*2 + 1) # TODO discuss edges calculation + highest_row = int(rows_interpolated - radius*2 - 1) + lowest_col = int(radius*2 + 1) + highest_col = int(cols_interpolated - radius*2 - 1) # Output rgc_map = np.zeros((nFrames, rows_interpolated, cols_interpolated), dtype=np.float32) diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index cf5a502b..8f834cec 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -54,17 +54,15 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* float RGC = 0; float distanceWeightSum = 0; - int _start = -(int)(Gx_Gy_MAGNIFICATION * fwhm); - int _end = (int)(Gx_Gy_MAGNIFICATION * fwhm + 1); + int _start = -(int)(2 * fwhm); //changed to have "Factor Cagança" but with properly iterating over the desired range + int _end = (int)(2 * fwhm + 1); // TODO discuss with Ricardo for (int j = _start; j < _end; j++) { - vy = (int)(Gx_Gy_MAGNIFICATION * yc) + j; - vy /= Gx_Gy_MAGNIFICATION; + vy = yc + j; if (0 < vy && vy <= (rowsM/magnification) - 1) { for (int i = _start; i < _end; i++) { - vx = (int)(Gx_Gy_MAGNIFICATION * xc) + i; - vx /= Gx_Gy_MAGNIFICATION; + vx = xc + i; if (0 < vx && vx <= (colsM/magnification) - 1) { dx = vx - xc; @@ -119,8 +117,8 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* // gradient image dimensions int nPixels_grad = nRows*Gx_Gy_MAGNIFICATION * nCols*Gx_Gy_MAGNIFICATION; - row = row + magnification*Gx_Gy_MAGNIFICATION; - col = col + magnification*Gx_Gy_MAGNIFICATION; + row = row + fwhm*2 + 1; + col = col + fwhm*2 + 1; if (doIntensityWeighting == 1) { image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, offset, xyoffset, angle) * imInt[f * nPixels_out + row * nCols + col]; diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx index 7e119b66..443bc6ba 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx @@ -230,10 +230,10 @@ class RadialGradientConvergence(LiquidEngine): cdef int cols_interpolated = (gradient_row_interp.shape[2] / Gx_Gy_MAGNIFICATION) # Grid size of the global work space - lowest_row = _magnification*2 - highest_row = rows_interpolated - _magnification*2 - lowest_col = _magnification*2 - highest_col = cols_interpolated - _magnification*2 + lowest_row = int(radius*2 + 1) # TODO discuss edges calculation + highest_row = int(rows_interpolated - radius*2 - 1) + lowest_col = int(radius*2 + 1) + highest_col = int(cols_interpolated - radius*2 - 1) # Output rgc_map = np.zeros((nFrames, rows_interpolated, cols_interpolated), dtype=np.float32) From b1ba1b87e6576f539b430541ff36d10466f630d7 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Tue, 1 Apr 2025 09:40:03 +0100 Subject: [PATCH 17/37] updates to esrrf calculation --- .../_c_sr_radial_gradient_convergence.c | 57 ++++++---- .../_c_sr_radial_gradient_convergence.h | 4 +- ...nanopyx.core.transform._le_convolution.pyx | 14 +-- .../nanopyx.core.transform._le_esrrf.pyx | 23 ++-- .../nanopyx.core.transform._le_esrrf3d.pyx | 47 +++++++- ...nsform._le_radial_gradient_convergence.pyx | 14 +-- .../core/transform/_le_convolution.pyx | 22 ++-- src/nanopyx/core/transform/_le_esrrf.pyx | 35 +++--- src/nanopyx/core/transform/_le_esrrf3d.pyx | 107 +++++++++++++++++- .../_le_radial_gradient_convergence.pyx | 32 +++--- 10 files changed, 252 insertions(+), 103 deletions(-) diff --git a/src/include/_c_sr_radial_gradient_convergence.c b/src/include/_c_sr_radial_gradient_convergence.c index d66a04ff..f8e51ef0 100644 --- a/src/include/_c_sr_radial_gradient_convergence.c +++ b/src/include/_c_sr_radial_gradient_convergence.c @@ -14,36 +14,47 @@ double _c_calculate_dk(float Gx, float Gy, float dx, float dy, float distance) { return Dk; } -float _c_calculate_rgc(int xM, int yM, float* imIntGx, float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity) { +void _rotate_vector(float* Gx, float* Gy, float angle) { + float cos_angle = cos(angle); + float sin_angle = sin(angle); + + float original_Gx = *Gx; + float original_Gy = *Gy; + + *Gx = original_Gx * cos_angle - original_Gy * sin_angle; + *Gy = original_Gx * sin_angle + original_Gy * cos_angle; +} + +float _c_calculate_rgc(int xM, int yM, float* imIntGx, float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float offset, float xyoffset, float angle) { float vx, vy, Gx, Gy, dx, dy, distance, distanceWeight, GdotR, Dk; - float xc = (xM + 0.5) / magnification; - float yc = (yM + 0.5) / magnification; + float xc = (float)xM / magnification + offset; // offset in non-magnified space + float yc = (float)yM / magnification + offset; float RGC = 0; float distanceWeightSum = 0; - int _start = -(int)(Gx_Gy_MAGNIFICATION * fwhm); - int _end = (int)(Gx_Gy_MAGNIFICATION * fwhm + 1); + int _start = -(int)(2 * fwhm); + int _end = (int)(2 * fwhm + 1); for (int j = _start; j < _end; j++) { - vy = (int)(Gx_Gy_MAGNIFICATION * yc) + j; - vy /= Gx_Gy_MAGNIFICATION; + vy = yc + j; - if (0 < vy && vy <= rowsM - 1) { + if (0 < vy && vy <= rowsM/magnification - 1) { for (int i = _start; i < _end; i++) { - vx = (int)(Gx_Gy_MAGNIFICATION * xc) + i; - vx /= Gx_Gy_MAGNIFICATION; + vx = xc + i; - if (0 < vx && vx <= colsM - 1) { + if (0 < vx && vx <= colsM/magnification - 1) { dx = vx - xc; dy = vy - yc; distance = sqrt(dx * dx + dy * dy); if (distance != 0 && distance <= tSO) { - Gx = imIntGx[(int)(vy * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)(vx * magnification * Gx_Gy_MAGNIFICATION)]; - Gy = imIntGy[(int)(vy * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)(vx * magnification * Gx_Gy_MAGNIFICATION)]; + Gx = imIntGx[(int)((vy+xyoffset) * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)((vx+xyoffset) * magnification * Gx_Gy_MAGNIFICATION)]; + Gy = imIntGy[(int)((vy+xyoffset) * magnification * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION) + (int)((vx+xyoffset) * magnification * Gx_Gy_MAGNIFICATION)]; + + _rotate_vector(&Gx, &Gy, angle); distanceWeight = _c_calculate_dw(distance, tSS); distanceWeightSum += distanceWeight; @@ -114,31 +125,31 @@ float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIn float vx, vy, vz, Gx, Gy, Gz, dx, dy, dz, dz_real, distance, distance_xy, distance_z, distanceWeight, distanceWeight_xy, distanceWeight_z, GdotR, Dk; - float xc = (xM + 0.5) / magnification_xy; - float yc = (yM + 0.5) / magnification_xy; - float zc = (sliceM + 0.5) / magnification_z; + float xc = (xM) / magnification_xy; + float yc = (yM) / magnification_xy; + float zc = (sliceM) / magnification_z; float RGC = 0; float distanceWeightSum = 0; float distanceWeightSum_xy = 0; float distanceWeightSum_z = 0; - int _start = -(int)(Gx_Gy_MAGNIFICATION * fwhm); - int _end = (int)(Gx_Gy_MAGNIFICATION * fwhm + 1); + int _start = -(int)(2 * fwhm); + int _end = (int)(2 * fwhm + 1); - int _start_z = -(int)(Gz_MAGNIFICATION * fwhm_z); - int _end_z = (int)(Gz_MAGNIFICATION * fwhm_z + 1); + int _start_z = -(int)(2 * fwhm_z); + int _end_z = (int)(2 * fwhm_z + 1); for (int j = _start; j <= _end; j++) { - vy = ((float) ((int) (Gx_Gy_MAGNIFICATION*yc)) + j)/(float) Gx_Gy_MAGNIFICATION; + vy = yc + j; if (0 < vy && vy < rowsM - 1) { for (int i = _start; i <= _end; i++) { - vx = ((float) ((int) (Gx_Gy_MAGNIFICATION*xc)) + i)/(float) Gx_Gy_MAGNIFICATION; + vx = xc + i; if (0 < vx && vx < colsM - 1) { for (int k = _start_z; k <= _end_z; k++) { - vz = ((float) ((int) (Gz_MAGNIFICATION*zc)) + k)/(float) Gz_MAGNIFICATION; + vz = zc + k; if (0 < vz && vz < slicesM - 1) { dx = vx - xc; diff --git a/src/include/_c_sr_radial_gradient_convergence.h b/src/include/_c_sr_radial_gradient_convergence.h index b9793678..0c688674 100644 --- a/src/include/_c_sr_radial_gradient_convergence.h +++ b/src/include/_c_sr_radial_gradient_convergence.h @@ -7,7 +7,9 @@ double _c_calculate_dw(double distance, double tSS); double _c_calculate_dk(float Gx, float Gy, float dx, float dy, float distance); -float _c_calculate_rgc(int xM, int yM, float* imIntGx, float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity); +void _rotate_vector(float* Gx, float* Gy, float angle); + +float _c_calculate_rgc(int xM, int yM, float* imIntGx, float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float offset, float xyoffset, float angle); double _c_calculate_dw3D_isotropic(double distance, double tSS); diff --git a/src/mako_templates/nanopyx.core.transform._le_convolution.pyx b/src/mako_templates/nanopyx.core.transform._le_convolution.pyx index 607488de..dc56d34c 100644 --- a/src/mako_templates/nanopyx.core.transform._le_convolution.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_convolution.pyx @@ -86,7 +86,7 @@ class Convolution(LiquidEngine): acc = acc + kernel[kr,kc] * image[f,local_row, local_col] _conv_out[f,r,c] = acc - return conv_out + return np.squeeze(conv_out) % endfor @@ -131,33 +131,33 @@ class Convolution(LiquidEngine): cl.enqueue_copy(cl_queue, image_out, output_opencl).wait() cl_queue.finish() - return image_out + return np.squeeze(image_out) def _run_python(self, image, kernel): """ @cpu """ - return convolution2D_python(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_python(image, kernel)) def _run_transonic(self, image, kernel): """ @cpu @threaded """ - return convolution2D_transonic(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_transonic(image, kernel)) def _run_dask(self, image, kernel): """ @cpu @threaded """ - return convolution2D_dask(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_dask(image, kernel)) def _run_cuda(self, image, kernel): """ @gpu """ - return convolution2D_cuda(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_cuda(image, kernel)) def _run_numba(self, image, kernel): """ @@ -165,4 +165,4 @@ class Convolution(LiquidEngine): @threaded @numba """ - return convolution2D_numba(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_numba(image, kernel)) diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx index 09710307..4afd1b77 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx @@ -28,15 +28,15 @@ class eSRRF(LiquidEngine): self._designation = "eSRRF_ST" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, run_type=None): + def run(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type=None): image = check_image(image) - return self._run(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle, run_type=run_type) - def benchmark(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True): + def benchmark(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): image = check_image(image) - return super().benchmark(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) + return super().benchmark(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle) - def _run_opencl(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, device=None, mem_div=1): + def _run_opencl(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0, device=None, mem_div=1): """ @gpu """ @@ -140,7 +140,10 @@ class eSRRF(LiquidEngine): np.float32(2 * (radius / 2.355) + 1), np.float32(2 * (radius / 2.355) * (radius / 2.355)), np.float32(sensitivity), - np.int32(doIntensityWeighting)).wait() + np.int32(doIntensityWeighting), + np.float32(offset), + np.float32(xyoffset), + np.float32(angle)).wait() cl.enqueue_copy(cl_queue, output_image[i:i+n_slices,:,:], output_cl).wait() @@ -153,7 +156,7 @@ class eSRRF(LiquidEngine): return output_image % for sch in schedulers: - def _run_${sch}(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_${sch}(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -168,12 +171,12 @@ class eSRRF(LiquidEngine): gradient_col, gradient_row = rbc.run(image, run_type=runtype) gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) return radial_gradients % endfor - def _run_unthreaded(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_unthreaded(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @cython @@ -187,6 +190,6 @@ class eSRRF(LiquidEngine): gradient_col, gradient_row = rbc.run(image, run_type=runtype) gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) return radial_gradients \ No newline at end of file diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx index d46f2979..588edb1b 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx @@ -3,6 +3,7 @@ schedulers = ['threaded','threaded_guided','threaded_dynamic','threaded_static', %># cython: infer_types=True, wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False import numpy as np import math +import time cimport numpy as np from libc.math cimport floor @@ -29,12 +30,6 @@ class eSRRF3D(LiquidEngine): def __init__(self, clear_benchmarks=False, testing=False, verbose=True): self._designation = "eSRRF_3D" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - self._gradients_s_interpolated = None - self._gradients_r_interpolated = None - self._gradients_c_interpolated = None - self.keep_gradients = False - self.keep_interpolated = False - self._img_interpolated = None def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, keep_gradients=False, keep_interpolated = False, run_type=None): self.keep_gradients = keep_gradients @@ -66,6 +61,7 @@ class eSRRF3D(LiquidEngine): % endif @cython """ + time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma @@ -79,6 +75,9 @@ class eSRRF3D(LiquidEngine): cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting + time_1 = time.time() + print("Time 1", time_1 - time_start) + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -90,6 +89,9 @@ class eSRRF3D(LiquidEngine): cdef float rgc_val, zcof + time_2 = time.time() + print("Time 2", time_2 - time_1) + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -116,6 +118,9 @@ class eSRRF3D(LiquidEngine): if self.keep_interpolated: self._img_interpolated = img_dum.copy() + time_3 = time.time() + print("Time 3", time_3 - time_2) + with nogil: for sM in range(0, n_slices_mag): % if sch=="unthreaded": @@ -132,6 +137,11 @@ class eSRRF3D(LiquidEngine): else: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + + + time_4 = time.time() + print("Time 4", time_4 - time_3) + time_2 = time_4 return np.asarray(rgc_map) % endfor @@ -144,3 +154,28 @@ class eSRRF3D(LiquidEngine): def get_interpolated_image(self): return self._img_interpolated + + +# this function loads a binary mask and calculates the edges +def load_binary_mask_and_calculate_edges(mask_path: str): + """ + Loads a binary mask from the given path and calculates its edges. + + Parameters: + mask_path (str): Path to the binary mask file. + + Returns: + np.ndarray: An array representing the edges of the binary mask. + """ + from scipy.ndimage import binary_erosion + + # Load the binary mask + mask = np.load(mask_path) + if mask.dtype != np.bool_: + raise ValueError("The mask must be a binary (boolean) array.") + + # Calculate the edges by subtracting the eroded mask from the original mask + eroded_mask = binary_erosion(mask) + edges = mask & ~eroded_mask + + return edges \ No newline at end of file diff --git a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx index e9109986..132b4ad8 100644 --- a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx @@ -13,7 +13,7 @@ from ...__liquid_engine__ import LiquidEngine from .__interpolation_tools__ import check_image cdef extern from "_c_sr_radial_gradient_convergence.h": - float _c_calculate_rgc(int xM, int yM, float* imIntGx, float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity) nogil + float _c_calculate_rgc(int xM, int yM, float* imIntGx, float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float offset, float xyoffset, float angle) nogil class RadialGradientConvergence(LiquidEngine): """ @@ -40,7 +40,7 @@ class RadialGradientConvergence(LiquidEngine): image_interp = check_image(image_interp) return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, grad_magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @cython @@ -66,14 +66,14 @@ class RadialGradientConvergence(LiquidEngine): for rM in range(_magnification*2, rowsM - _magnification*2): for cM in range(_magnification*2, colsM - _magnification*2): if _doIntensityWeighting: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) * image_interp[f, rM, cM] + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) % for sch in schedulers: - def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -104,9 +104,9 @@ class RadialGradientConvergence(LiquidEngine): % endif for cM in range(_magnification*2, colsM - _magnification*2): if _doIntensityWeighting: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) * image_interp[f, rM, cM] + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) % endfor diff --git a/src/nanopyx/core/transform/_le_convolution.pyx b/src/nanopyx/core/transform/_le_convolution.pyx index df8f622c..9e12a378 100644 --- a/src/nanopyx/core/transform/_le_convolution.pyx +++ b/src/nanopyx/core/transform/_le_convolution.pyx @@ -72,7 +72,7 @@ class Convolution(LiquidEngine): acc = acc + kernel[kr,kc] * image[f,local_row, local_col] _conv_out[f,r,c] = acc - return conv_out + return np.squeeze(conv_out) def _run_threaded(self, float[:,:,:] image, float[:,:] kernel): """ @@ -113,7 +113,7 @@ class Convolution(LiquidEngine): acc = acc + kernel[kr,kc] * image[f,local_row, local_col] _conv_out[f,r,c] = acc - return conv_out + return np.squeeze(conv_out) def _run_threaded_guided(self, float[:,:,:] image, float[:,:] kernel): """ @@ -154,7 +154,7 @@ class Convolution(LiquidEngine): acc = acc + kernel[kr,kc] * image[f,local_row, local_col] _conv_out[f,r,c] = acc - return conv_out + return np.squeeze(conv_out) def _run_threaded_dynamic(self, float[:,:,:] image, float[:,:] kernel): """ @@ -195,7 +195,7 @@ class Convolution(LiquidEngine): acc = acc + kernel[kr,kc] * image[f,local_row, local_col] _conv_out[f,r,c] = acc - return conv_out + return np.squeeze(conv_out) def _run_threaded_static(self, float[:,:,:] image, float[:,:] kernel): """ @@ -236,7 +236,7 @@ class Convolution(LiquidEngine): acc = acc + kernel[kr,kc] * image[f,local_row, local_col] _conv_out[f,r,c] = acc - return conv_out + return np.squeeze(conv_out) def _run_opencl(self, image, kernel, device=None): @@ -280,33 +280,33 @@ class Convolution(LiquidEngine): cl.enqueue_copy(cl_queue, image_out, output_opencl).wait() cl_queue.finish() - return image_out + return np.squeeze(image_out) def _run_python(self, image, kernel): """ @cpu """ - return convolution2D_python(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_python(image, kernel)) def _run_transonic(self, image, kernel): """ @cpu @threaded """ - return convolution2D_transonic(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_transonic(image, kernel)) def _run_dask(self, image, kernel): """ @cpu @threaded """ - return convolution2D_dask(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_dask(image, kernel)) def _run_cuda(self, image, kernel): """ @gpu """ - return convolution2D_cuda(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_cuda(image, kernel)) def _run_numba(self, image, kernel): """ @@ -314,4 +314,4 @@ class Convolution(LiquidEngine): @threaded @numba """ - return convolution2D_numba(image, kernel).astype(np.float32) + return np.squeeze(convolution2D_numba(image, kernel)) diff --git a/src/nanopyx/core/transform/_le_esrrf.pyx b/src/nanopyx/core/transform/_le_esrrf.pyx index df40fa2e..a5021a85 100644 --- a/src/nanopyx/core/transform/_le_esrrf.pyx +++ b/src/nanopyx/core/transform/_le_esrrf.pyx @@ -26,15 +26,15 @@ class eSRRF(LiquidEngine): self._designation = "eSRRF_ST" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, run_type=None): + def run(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type=None): image = check_image(image) - return self._run(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle, run_type=run_type) - def benchmark(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True): + def benchmark(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): image = check_image(image) - return super().benchmark(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) + return super().benchmark(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle) - def _run_opencl(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, device=None, mem_div=1): + def _run_opencl(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0, device=None, mem_div=1): """ @gpu """ @@ -138,7 +138,10 @@ class eSRRF(LiquidEngine): np.float32(2 * (radius / 2.355) + 1), np.float32(2 * (radius / 2.355) * (radius / 2.355)), np.float32(sensitivity), - np.int32(doIntensityWeighting)).wait() + np.int32(doIntensityWeighting), + np.float32(offset), + np.float32(xyoffset), + np.float32(angle)).wait() cl.enqueue_copy(cl_queue, output_image[i:i+n_slices,:,:], output_cl).wait() @@ -150,7 +153,7 @@ class eSRRF(LiquidEngine): return output_image - def _run_threaded(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_threaded(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -165,10 +168,10 @@ class eSRRF(LiquidEngine): gradient_col, gradient_row = rbc.run(image, run_type=runtype) gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) return radial_gradients - def _run_threaded_guided(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_threaded_guided(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -183,10 +186,10 @@ class eSRRF(LiquidEngine): gradient_col, gradient_row = rbc.run(image, run_type=runtype) gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) return radial_gradients - def _run_threaded_dynamic(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_threaded_dynamic(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -201,10 +204,10 @@ class eSRRF(LiquidEngine): gradient_col, gradient_row = rbc.run(image, run_type=runtype) gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) return radial_gradients - def _run_threaded_static(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_threaded_static(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -219,11 +222,11 @@ class eSRRF(LiquidEngine): gradient_col, gradient_row = rbc.run(image, run_type=runtype) gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) return radial_gradients - def _run_unthreaded(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True): + def _run_unthreaded(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @cython @@ -237,6 +240,6 @@ class eSRRF(LiquidEngine): gradient_col, gradient_row = rbc.run(image, run_type=runtype) gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) return radial_gradients \ No newline at end of file diff --git a/src/nanopyx/core/transform/_le_esrrf3d.pyx b/src/nanopyx/core/transform/_le_esrrf3d.pyx index 73177569..ce361545 100644 --- a/src/nanopyx/core/transform/_le_esrrf3d.pyx +++ b/src/nanopyx/core/transform/_le_esrrf3d.pyx @@ -1,6 +1,7 @@ # cython: infer_types=True, wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False import numpy as np import math +import time cimport numpy as np from libc.math cimport floor @@ -27,12 +28,6 @@ class eSRRF3D(LiquidEngine): def __init__(self, clear_benchmarks=False, testing=False, verbose=True): self._designation = "eSRRF_3D" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - self._gradients_s_interpolated = None - self._gradients_r_interpolated = None - self._gradients_c_interpolated = None - self.keep_gradients = False - self.keep_interpolated = False - self._img_interpolated = None def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, keep_gradients=False, keep_interpolated = False, run_type=None): self.keep_gradients = keep_gradients @@ -61,6 +56,7 @@ class eSRRF3D(LiquidEngine): @threaded @cython """ + time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma @@ -74,6 +70,9 @@ class eSRRF3D(LiquidEngine): cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting + time_1 = time.time() + print("Time 1", time_1 - time_start) + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -85,6 +84,9 @@ class eSRRF3D(LiquidEngine): cdef float rgc_val, zcof + time_2 = time.time() + print("Time 2", time_2 - time_1) + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -111,6 +113,9 @@ class eSRRF3D(LiquidEngine): if self.keep_interpolated: self._img_interpolated = img_dum.copy() + time_3 = time.time() + print("Time 3", time_3 - time_2) + with nogil: for sM in range(0, n_slices_mag): for rM in prange(0, n_rows_mag): @@ -121,6 +126,11 @@ class eSRRF3D(LiquidEngine): else: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + + + time_4 = time.time() + print("Time 4", time_4 - time_3) + time_2 = time_4 return np.asarray(rgc_map) def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): @@ -129,6 +139,7 @@ class eSRRF3D(LiquidEngine): @threaded @cython """ + time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma @@ -142,6 +153,9 @@ class eSRRF3D(LiquidEngine): cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting + time_1 = time.time() + print("Time 1", time_1 - time_start) + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -153,6 +167,9 @@ class eSRRF3D(LiquidEngine): cdef float rgc_val, zcof + time_2 = time.time() + print("Time 2", time_2 - time_1) + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -179,6 +196,9 @@ class eSRRF3D(LiquidEngine): if self.keep_interpolated: self._img_interpolated = img_dum.copy() + time_3 = time.time() + print("Time 3", time_3 - time_2) + with nogil: for sM in range(0, n_slices_mag): for rM in prange(0, n_rows_mag, schedule="guided"): @@ -189,6 +209,11 @@ class eSRRF3D(LiquidEngine): else: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + + + time_4 = time.time() + print("Time 4", time_4 - time_3) + time_2 = time_4 return np.asarray(rgc_map) def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): @@ -197,6 +222,7 @@ class eSRRF3D(LiquidEngine): @threaded @cython """ + time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma @@ -210,6 +236,9 @@ class eSRRF3D(LiquidEngine): cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting + time_1 = time.time() + print("Time 1", time_1 - time_start) + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -221,6 +250,9 @@ class eSRRF3D(LiquidEngine): cdef float rgc_val, zcof + time_2 = time.time() + print("Time 2", time_2 - time_1) + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -247,6 +279,9 @@ class eSRRF3D(LiquidEngine): if self.keep_interpolated: self._img_interpolated = img_dum.copy() + time_3 = time.time() + print("Time 3", time_3 - time_2) + with nogil: for sM in range(0, n_slices_mag): for rM in prange(0, n_rows_mag, schedule="dynamic"): @@ -257,6 +292,11 @@ class eSRRF3D(LiquidEngine): else: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + + + time_4 = time.time() + print("Time 4", time_4 - time_3) + time_2 = time_4 return np.asarray(rgc_map) def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): @@ -265,6 +305,7 @@ class eSRRF3D(LiquidEngine): @threaded @cython """ + time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma @@ -278,6 +319,9 @@ class eSRRF3D(LiquidEngine): cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting + time_1 = time.time() + print("Time 1", time_1 - time_start) + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -289,6 +333,9 @@ class eSRRF3D(LiquidEngine): cdef float rgc_val, zcof + time_2 = time.time() + print("Time 2", time_2 - time_1) + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -315,6 +362,9 @@ class eSRRF3D(LiquidEngine): if self.keep_interpolated: self._img_interpolated = img_dum.copy() + time_3 = time.time() + print("Time 3", time_3 - time_2) + with nogil: for sM in range(0, n_slices_mag): for rM in prange(0, n_rows_mag, schedule="static"): @@ -325,6 +375,11 @@ class eSRRF3D(LiquidEngine): else: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + + + time_4 = time.time() + print("Time 4", time_4 - time_3) + time_2 = time_4 return np.asarray(rgc_map) def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): @@ -332,6 +387,7 @@ class eSRRF3D(LiquidEngine): @cpu @cython """ + time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius cdef float tSS = 2 * sigma * sigma @@ -345,6 +401,9 @@ class eSRRF3D(LiquidEngine): cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting + time_1 = time.time() + print("Time 1", time_1 - time_start) + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -356,6 +415,9 @@ class eSRRF3D(LiquidEngine): cdef float rgc_val, zcof + time_2 = time.time() + print("Time 2", time_2 - time_1) + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -382,6 +444,9 @@ class eSRRF3D(LiquidEngine): if self.keep_interpolated: self._img_interpolated = img_dum.copy() + time_3 = time.time() + print("Time 3", time_3 - time_2) + with nogil: for sM in range(0, n_slices_mag): for rM in range(0, n_rows_mag): @@ -392,6 +457,11 @@ class eSRRF3D(LiquidEngine): else: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + + + time_4 = time.time() + print("Time 4", time_4 - time_3) + time_2 = time_4 return np.asarray(rgc_map) @@ -403,3 +473,28 @@ class eSRRF3D(LiquidEngine): def get_interpolated_image(self): return self._img_interpolated + + +# this function loads a binary mask and calculates the edges +def load_binary_mask_and_calculate_edges(mask_path: str): + """ + Loads a binary mask from the given path and calculates its edges. + + Parameters: + mask_path (str): Path to the binary mask file. + + Returns: + np.ndarray: An array representing the edges of the binary mask. + """ + from scipy.ndimage import binary_erosion + + # Load the binary mask + mask = np.load(mask_path) + if mask.dtype != np.bool_: + raise ValueError("The mask must be a binary (boolean) array.") + + # Calculate the edges by subtracting the eroded mask from the original mask + eroded_mask = binary_erosion(mask) + edges = mask & ~eroded_mask + + return edges \ No newline at end of file diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx index 443bc6ba..6a99ebd0 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx @@ -11,7 +11,7 @@ from ...__liquid_engine__ import LiquidEngine from .__interpolation_tools__ import check_image cdef extern from "_c_sr_radial_gradient_convergence.h": - float _c_calculate_rgc(int xM, int yM, float* imIntGx, float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity) nogil + float _c_calculate_rgc(int xM, int yM, float* imIntGx, float* imIntGy, int colsM, int rowsM, int magnification, float Gx_Gy_MAGNIFICATION, float fwhm, float tSO, float tSS, float sensitivity, float offset, float xyoffset, float angle) nogil class RadialGradientConvergence(LiquidEngine): """ @@ -38,7 +38,7 @@ class RadialGradientConvergence(LiquidEngine): image_interp = check_image(image_interp) return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, grad_magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @cython @@ -64,13 +64,13 @@ class RadialGradientConvergence(LiquidEngine): for rM in range(_magnification*2, rowsM - _magnification*2): for cM in range(_magnification*2, colsM - _magnification*2): if _doIntensityWeighting: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) * image_interp[f, rM, cM] + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -97,11 +97,11 @@ class RadialGradientConvergence(LiquidEngine): for rM in prange(_magnification*2, rowsM - _magnification*2): for cM in range(_magnification*2, colsM - _magnification*2): if _doIntensityWeighting: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) * image_interp[f, rM, cM] + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -128,11 +128,11 @@ class RadialGradientConvergence(LiquidEngine): for rM in prange(_magnification*2, rowsM - _magnification*2, schedule="guided"): for cM in range(_magnification*2, colsM - _magnification*2): if _doIntensityWeighting: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) * image_interp[f, rM, cM] + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -159,11 +159,11 @@ class RadialGradientConvergence(LiquidEngine): for rM in prange(_magnification*2, rowsM - _magnification*2, schedule="dynamic"): for cM in range(_magnification*2, colsM - _magnification*2): if _doIntensityWeighting: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) * image_interp[f, rM, cM] + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0): + def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -190,9 +190,9 @@ class RadialGradientConvergence(LiquidEngine): for rM in prange(_magnification*2, rowsM - _magnification*2, schedule="static"): for cM in range(_magnification*2, colsM - _magnification*2): if _doIntensityWeighting: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) * image_interp[f, rM, cM] + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: - rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity) + rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) From f73bf8dd941e6027ad0339b73c4cdcc0a5cb8aa4 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Tue, 1 Apr 2025 15:13:28 +0100 Subject: [PATCH 18/37] fixed channel registration according to new convolution changes --- .../nanopyx.core.analysis._le_channel_registration.pyx | 5 ++--- src/nanopyx/core/analysis/_le_channel_registration.pyx | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/mako_templates/nanopyx.core.analysis._le_channel_registration.pyx b/src/mako_templates/nanopyx.core.analysis._le_channel_registration.pyx index 7e109e5e..2146207a 100644 --- a/src/mako_templates/nanopyx.core.analysis._le_channel_registration.pyx +++ b/src/mako_templates/nanopyx.core.analysis._le_channel_registration.pyx @@ -85,10 +85,9 @@ def _gaussian_filter(inpimg, _runtype, sigma): knl1 = np.exp(-0.5 / (sigma*sigma) * x1 ** 2) knl1 = knl1 / knl1.sum() knl1 = knl1.astype(np.float32) - img1 = conv.run(inpimg,knl1.reshape((len(x1), 1)),run_type=_runtype) - img2 = conv.run(img1,knl1.reshape((1, len(x1))),run_type=_runtype)[0] - + img1 = np.expand_dims(img1, axis=0) + img2 = conv.run(img1,knl1.reshape((1, len(x1))),run_type=_runtype) return img2 class ChannelRegistrationEstimator(LiquidEngine): diff --git a/src/nanopyx/core/analysis/_le_channel_registration.pyx b/src/nanopyx/core/analysis/_le_channel_registration.pyx index 682f6fc2..bbf79d8d 100644 --- a/src/nanopyx/core/analysis/_le_channel_registration.pyx +++ b/src/nanopyx/core/analysis/_le_channel_registration.pyx @@ -83,10 +83,9 @@ def _gaussian_filter(inpimg, _runtype, sigma): knl1 = np.exp(-0.5 / (sigma*sigma) * x1 ** 2) knl1 = knl1 / knl1.sum() knl1 = knl1.astype(np.float32) - img1 = conv.run(inpimg,knl1.reshape((len(x1), 1)),run_type=_runtype) - img2 = conv.run(img1,knl1.reshape((1, len(x1))),run_type=_runtype)[0] - + img1 = np.expand_dims(img1, axis=0) + img2 = conv.run(img1,knl1.reshape((1, len(x1))),run_type=_runtype) return img2 class ChannelRegistrationEstimator(LiquidEngine): From 472fb8940081eea040ef77993f95eae08ffface6 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Tue, 1 Apr 2025 17:00:48 +0100 Subject: [PATCH 19/37] fixed robert's cross in eSRRF cpu runtypes --- .../nanopyx.core.transform._le_esrrf.pyx | 47 +++++---- src/nanopyx/core/transform/_le_esrrf.pyx | 95 +++++++++++-------- 2 files changed, 86 insertions(+), 56 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx index 4afd1b77..179bd516 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx @@ -14,6 +14,7 @@ from .__interpolation_tools__ import check_image, value2array from ...__liquid_engine__ import LiquidEngine from ...__opencl__ import cl, cl_array, _fastest_device +from ._le_convolution import Convolution from ._le_interpolation_catmull_rom import ShiftAndMagnify from ._le_roberts_cross_gradients import GradientRobertsCross from ._le_radial_gradient_convergence import RadialGradientConvergence @@ -28,15 +29,15 @@ class eSRRF(LiquidEngine): self._designation = "eSRRF_ST" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type=None): + def run(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type=None): image = check_image(image) - return self._run(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle, run_type=run_type) + return self._run(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle, run_type=run_type) - def benchmark(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def benchmark(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): image = check_image(image) - return super().benchmark(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle) + return super().benchmark(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle) - def _run_opencl(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0, device=None, mem_div=1): + def _run_opencl(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0, device=None, mem_div=1): """ @gpu """ @@ -112,8 +113,8 @@ class eSRRF(LiquidEngine): col_magnified_gradients_cl, np.float32(0), np.float32(0), - np.float32(magnification*2), - np.float32(magnification*2)).wait() + np.float32(magnification*grad_magnification), + np.float32(magnification*grad_magnification)).wait() cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), @@ -156,7 +157,7 @@ class eSRRF(LiquidEngine): return output_image % for sch in schedulers: - def _run_${sch}(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_${sch}(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -164,32 +165,40 @@ class eSRRF(LiquidEngine): """ runtype = "${sch}".capitalize() crsm = ShiftAndMagnify(verbose=False) - rbc = GradientRobertsCross(verbose=False) + conv = Convolution(verbose=False) rgc = RadialGradientConvergence(verbose=False) + + kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) magnified_image = crsm.run(image, 0, 0, magnification, magnification, run_type=runtype) - gradient_col, gradient_row = rbc.run(image, run_type=runtype) - gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) - gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) + gradient_col = conv.run(image, kernely, run_type=runtype) + gradient_row = conv.run(image, kernelx, run_type=runtype) + gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients % endfor - def _run_unthreaded(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_unthreaded(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @cython """ runtype = "Unthreaded" crsm = ShiftAndMagnify(verbose=False) - rbc = GradientRobertsCross(verbose=False) + conv = Convolution(verbose=False) rgc = RadialGradientConvergence(verbose=False) + + kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) magnified_image = crsm.run(image, 0, 0, magnification, magnification, run_type=runtype) - gradient_col, gradient_row = rbc.run(image, run_type=runtype) - gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) - gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) + gradient_col = conv.run(image, kernely, run_type=runtype) + gradient_row = conv.run(image, kernelx, run_type=runtype) + gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients \ No newline at end of file diff --git a/src/nanopyx/core/transform/_le_esrrf.pyx b/src/nanopyx/core/transform/_le_esrrf.pyx index a5021a85..4420da28 100644 --- a/src/nanopyx/core/transform/_le_esrrf.pyx +++ b/src/nanopyx/core/transform/_le_esrrf.pyx @@ -12,6 +12,7 @@ from .__interpolation_tools__ import check_image, value2array from ...__liquid_engine__ import LiquidEngine from ...__opencl__ import cl, cl_array, _fastest_device +from ._le_convolution import Convolution from ._le_interpolation_catmull_rom import ShiftAndMagnify from ._le_roberts_cross_gradients import GradientRobertsCross from ._le_radial_gradient_convergence import RadialGradientConvergence @@ -26,15 +27,15 @@ class eSRRF(LiquidEngine): self._designation = "eSRRF_ST" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type=None): + def run(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type=None): image = check_image(image) - return self._run(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle, run_type=run_type) + return self._run(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle, run_type=run_type) - def benchmark(self, image, magnification: int = 5, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def benchmark(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): image = check_image(image) - return super().benchmark(image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle) + return super().benchmark(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle) - def _run_opencl(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0, device=None, mem_div=1): + def _run_opencl(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0, device=None, mem_div=1): """ @gpu """ @@ -110,8 +111,8 @@ class eSRRF(LiquidEngine): col_magnified_gradients_cl, np.float32(0), np.float32(0), - np.float32(magnification*2), - np.float32(magnification*2)).wait() + np.float32(magnification*grad_magnification), + np.float32(magnification*grad_magnification)).wait() cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), @@ -153,7 +154,7 @@ class eSRRF(LiquidEngine): return output_image - def _run_threaded(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_threaded(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -161,17 +162,21 @@ class eSRRF(LiquidEngine): """ runtype = "threaded".capitalize() crsm = ShiftAndMagnify(verbose=False) - rbc = GradientRobertsCross(verbose=False) + conv = Convolution(verbose=False) rgc = RadialGradientConvergence(verbose=False) + + kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) magnified_image = crsm.run(image, 0, 0, magnification, magnification, run_type=runtype) - gradient_col, gradient_row = rbc.run(image, run_type=runtype) - gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) - gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) + gradient_col = conv.run(image, kernely, run_type=runtype) + gradient_row = conv.run(image, kernelx, run_type=runtype) + gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients - def _run_threaded_guided(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_threaded_guided(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -179,17 +184,21 @@ class eSRRF(LiquidEngine): """ runtype = "threaded_guided".capitalize() crsm = ShiftAndMagnify(verbose=False) - rbc = GradientRobertsCross(verbose=False) + conv = Convolution(verbose=False) rgc = RadialGradientConvergence(verbose=False) + + kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) magnified_image = crsm.run(image, 0, 0, magnification, magnification, run_type=runtype) - gradient_col, gradient_row = rbc.run(image, run_type=runtype) - gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) - gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) + gradient_col = conv.run(image, kernely, run_type=runtype) + gradient_row = conv.run(image, kernelx, run_type=runtype) + gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients - def _run_threaded_dynamic(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_threaded_dynamic(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -197,17 +206,21 @@ class eSRRF(LiquidEngine): """ runtype = "threaded_dynamic".capitalize() crsm = ShiftAndMagnify(verbose=False) - rbc = GradientRobertsCross(verbose=False) + conv = Convolution(verbose=False) rgc = RadialGradientConvergence(verbose=False) + + kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) magnified_image = crsm.run(image, 0, 0, magnification, magnification, run_type=runtype) - gradient_col, gradient_row = rbc.run(image, run_type=runtype) - gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) - gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) + gradient_col = conv.run(image, kernely, run_type=runtype) + gradient_row = conv.run(image, kernelx, run_type=runtype) + gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients - def _run_threaded_static(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_threaded_static(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @threaded @@ -215,31 +228,39 @@ class eSRRF(LiquidEngine): """ runtype = "threaded_static".capitalize() crsm = ShiftAndMagnify(verbose=False) - rbc = GradientRobertsCross(verbose=False) + conv = Convolution(verbose=False) rgc = RadialGradientConvergence(verbose=False) + + kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) magnified_image = crsm.run(image, 0, 0, magnification, magnification, run_type=runtype) - gradient_col, gradient_row = rbc.run(image, run_type=runtype) - gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) - gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) + gradient_col = conv.run(image, kernely, run_type=runtype) + gradient_row = conv.run(image, kernelx, run_type=runtype) + gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients - def _run_unthreaded(self, image, magnification=5, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_unthreaded(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): """ @cpu @cython """ runtype = "Unthreaded" crsm = ShiftAndMagnify(verbose=False) - rbc = GradientRobertsCross(verbose=False) + conv = Convolution(verbose=False) rgc = RadialGradientConvergence(verbose=False) + + kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) magnified_image = crsm.run(image, 0, 0, magnification, magnification, run_type=runtype) - gradient_col, gradient_row = rbc.run(image, run_type=runtype) - gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*2, magnification*2, run_type=runtype) - gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*2, magnification*2, run_type=runtype) - radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=0, angle=0, run_type=runtype) + gradient_col = conv.run(image, kernely, run_type=runtype) + gradient_row = conv.run(image, kernelx, run_type=runtype) + gradient_col_interp = crsm.run(gradient_col, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + gradient_row_interp = crsm.run(gradient_row, 0, 0, magnification*grad_magnification, magnification*grad_magnification, run_type=runtype) + radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients \ No newline at end of file From f0f84453a3852d3fea9d5fdafc54817adabe415f Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Tue, 1 Apr 2025 17:17:02 +0100 Subject: [PATCH 20/37] added conv as part of eSRRF (BUGGED) --- .../nanopyx.core.transform._le_esrrf.pyx | 42 ++++++++++++------- src/nanopyx/core/transform/_le_esrrf.pyx | 42 ++++++++++++------- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx index 179bd516..1b49d5b3 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx @@ -51,31 +51,38 @@ class eSRRF(LiquidEngine): output_shape = (image.shape[0], int(image.shape[1]*magnification), int(image.shape[2]*magnification)) - # needs input image, first interpolation output, roberts cross output, magnified gradients and output image + # needs input image, 2 conv kernels, first interpolation output, roberts cross output, magnified gradients and output image - total_memory = (3*image[0, :, :].nbytes) + (2*np.zeros((1, output_shape[1], output_shape[2]), dtype=np.float32).nbytes) + (2*np.zeros((1, output_shape[1]*2, output_shape[2]*2), dtype=np.float32).nbytes) + total_memory = (3*image[0, :, :].nbytes) + (2*np.zeros((2,2), dtype=np.float32).nbytes) + (2*np.zeros((1, output_shape[1], output_shape[2]), dtype=np.float32).nbytes) + (2*np.zeros((1, output_shape[1]*grad_magnification, output_shape[2]*grad_magnification), dtype=np.float32).nbytes) output_image = np.zeros(output_shape, dtype=np.float32) max_slices = int((dc.global_mem_size // total_memory)/mem_div) max_slices = self._check_max_slices(image, max_slices) + kernely = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernelx = np.array([[-1, 0], [0, 1]]).astype(np.float32) + mf = cl.mem_flags input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[0:max_slices, :, :].nbytes, dc, max_slices)) output_cl = cl.Buffer(cl_ctx, mf.WRITE_ONLY, self._check_max_buffer_size(np.empty((max_slices, output_shape[1], output_shape[2]), dtype=np.float32).nbytes, dc, max_slices)) magnified_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, output_shape[1], output_shape[2]), dtype=np.float32).nbytes, dc, max_slices)) + col_kernel_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(np.empty((2,2)).nbytes, dc, max_slices)) + row_kernel_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(np.empty((2,2)).nbytes, dc, max_slices)) + cl.enqueue_copy(cl_queue, col_kernel_cl, kernely).wait() + cl.enqueue_copy(cl_queue, row_kernel_cl, kernelx).wait() col_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1], image.shape[2]), dtype=np.float32).nbytes, dc, max_slices)) row_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1], image.shape[2]), dtype=np.float32).nbytes, dc, max_slices)) - col_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1]*magnification*2, image.shape[2]*magnification*2), dtype=np.float32).nbytes, dc, max_slices)) - row_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1]*magnification*2, image.shape[2]*magnification*2), dtype=np.float32).nbytes, dc, max_slices)) + col_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification), dtype=np.float32).nbytes, dc, max_slices)) + row_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification), dtype=np.float32).nbytes, dc, max_slices)) cl.enqueue_copy(cl_queue, input_cl, image[0:max_slices,:,:]).wait() cr_code = self._get_cl_code("_le_interpolation_catmull_rom_.cl", device['DP']) cr_prg = cl.Program(cl_ctx, cr_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) cr_knl = cr_prg.shiftAndMagnify - rc_code = self._get_cl_code("_le_roberts_cross_gradients.cl", device['DP']) - rc_prg = cl.Program(cl_ctx, rc_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) - rc_knl = rc_prg.gradient_roberts_cross + conv_code = self._get_cl_code("_le_convolution.cl", device['DP']) + conv_prg = cl.Program(cl_ctx, conv_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) + conv_knl = conv_prg.conv2d_2 rgc_code = self._get_cl_code("_le_radial_gradient_convergence.cl", device['DP']) rgc_prg = cl.Program(cl_ctx, rgc_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) @@ -97,14 +104,21 @@ class eSRRF(LiquidEngine): np.float32(magnification), np.float32(magnification)).wait() - rc_knl(cl_queue, - (n_slices,), - None, + conv_knl(cl_queue, + (n_slices, image.shape[1], image.shape[2]), + None, input_cl, col_gradients_cl, + col_kernel_cl, + np.int32(2)).wait() + + conv_knl(cl_queue, + (n_slices, image.shape[1], image.shape[2]), + None, + input_cl, row_gradients_cl, - np.int32(image.shape[1]), - np.int32(image.shape[2])).wait() + row_kernel_cl, + np.int32(2)).wait() cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), @@ -123,8 +137,8 @@ class eSRRF(LiquidEngine): row_magnified_gradients_cl, np.float32(0), np.float32(0), - np.float32(magnification*2), - np.float32(magnification*2)).wait() + np.float32(magnification*grad_magnification), + np.float32(magnification*grad_magnification)).wait() rgc_knl(cl_queue, (n_slices, (image.shape[1]*magnification-magnification*2) - magnification*2, image.shape[2]*magnification-magnification*2 - magnification*2), diff --git a/src/nanopyx/core/transform/_le_esrrf.pyx b/src/nanopyx/core/transform/_le_esrrf.pyx index 4420da28..7153ee66 100644 --- a/src/nanopyx/core/transform/_le_esrrf.pyx +++ b/src/nanopyx/core/transform/_le_esrrf.pyx @@ -49,31 +49,38 @@ class eSRRF(LiquidEngine): output_shape = (image.shape[0], int(image.shape[1]*magnification), int(image.shape[2]*magnification)) - # needs input image, first interpolation output, roberts cross output, magnified gradients and output image + # needs input image, 2 conv kernels, first interpolation output, roberts cross output, magnified gradients and output image - total_memory = (3*image[0, :, :].nbytes) + (2*np.zeros((1, output_shape[1], output_shape[2]), dtype=np.float32).nbytes) + (2*np.zeros((1, output_shape[1]*2, output_shape[2]*2), dtype=np.float32).nbytes) + total_memory = (3*image[0, :, :].nbytes) + (2*np.zeros((2,2), dtype=np.float32).nbytes) + (2*np.zeros((1, output_shape[1], output_shape[2]), dtype=np.float32).nbytes) + (2*np.zeros((1, output_shape[1]*grad_magnification, output_shape[2]*grad_magnification), dtype=np.float32).nbytes) output_image = np.zeros(output_shape, dtype=np.float32) max_slices = int((dc.global_mem_size // total_memory)/mem_div) max_slices = self._check_max_slices(image, max_slices) + kernely = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernelx = np.array([[-1, 0], [0, 1]]).astype(np.float32) + mf = cl.mem_flags input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[0:max_slices, :, :].nbytes, dc, max_slices)) output_cl = cl.Buffer(cl_ctx, mf.WRITE_ONLY, self._check_max_buffer_size(np.empty((max_slices, output_shape[1], output_shape[2]), dtype=np.float32).nbytes, dc, max_slices)) magnified_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, output_shape[1], output_shape[2]), dtype=np.float32).nbytes, dc, max_slices)) + col_kernel_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(np.empty((2,2)).nbytes, dc, max_slices)) + row_kernel_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(np.empty((2,2)).nbytes, dc, max_slices)) + cl.enqueue_copy(cl_queue, col_kernel_cl, kernely).wait() + cl.enqueue_copy(cl_queue, row_kernel_cl, kernelx).wait() col_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1], image.shape[2]), dtype=np.float32).nbytes, dc, max_slices)) row_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1], image.shape[2]), dtype=np.float32).nbytes, dc, max_slices)) - col_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1]*magnification*2, image.shape[2]*magnification*2), dtype=np.float32).nbytes, dc, max_slices)) - row_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1]*magnification*2, image.shape[2]*magnification*2), dtype=np.float32).nbytes, dc, max_slices)) + col_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification), dtype=np.float32).nbytes, dc, max_slices)) + row_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(np.empty((max_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification), dtype=np.float32).nbytes, dc, max_slices)) cl.enqueue_copy(cl_queue, input_cl, image[0:max_slices,:,:]).wait() cr_code = self._get_cl_code("_le_interpolation_catmull_rom_.cl", device['DP']) cr_prg = cl.Program(cl_ctx, cr_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) cr_knl = cr_prg.shiftAndMagnify - rc_code = self._get_cl_code("_le_roberts_cross_gradients.cl", device['DP']) - rc_prg = cl.Program(cl_ctx, rc_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) - rc_knl = rc_prg.gradient_roberts_cross + conv_code = self._get_cl_code("_le_convolution.cl", device['DP']) + conv_prg = cl.Program(cl_ctx, conv_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) + conv_knl = conv_prg.conv2d_2 rgc_code = self._get_cl_code("_le_radial_gradient_convergence.cl", device['DP']) rgc_prg = cl.Program(cl_ctx, rgc_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) @@ -95,14 +102,21 @@ class eSRRF(LiquidEngine): np.float32(magnification), np.float32(magnification)).wait() - rc_knl(cl_queue, - (n_slices,), - None, + conv_knl(cl_queue, + (n_slices, image.shape[1], image.shape[2]), + None, input_cl, col_gradients_cl, + col_kernel_cl, + np.int32(2)).wait() + + conv_knl(cl_queue, + (n_slices, image.shape[1], image.shape[2]), + None, + input_cl, row_gradients_cl, - np.int32(image.shape[1]), - np.int32(image.shape[2])).wait() + row_kernel_cl, + np.int32(2)).wait() cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), @@ -121,8 +135,8 @@ class eSRRF(LiquidEngine): row_magnified_gradients_cl, np.float32(0), np.float32(0), - np.float32(magnification*2), - np.float32(magnification*2)).wait() + np.float32(magnification*grad_magnification), + np.float32(magnification*grad_magnification)).wait() rgc_knl(cl_queue, (n_slices, (image.shape[1]*magnification-magnification*2) - magnification*2, image.shape[2]*magnification-magnification*2 - magnification*2), From f44f7c9d4770efcafa4358e4887a1ecf9d0b35fa Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Tue, 1 Apr 2025 19:05:28 +0100 Subject: [PATCH 21/37] further changes and improvements --- .../nanopyx.core.transform._le_esrrf.pyx | 20 +++++++++++------- ...nsform._le_radial_gradient_convergence.pyx | 21 +++++++++++-------- src/nanopyx/core/transform/_le_esrrf.pyx | 20 +++++++++++------- .../_le_radial_gradient_convergence.cl | 4 ++-- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx index 1b49d5b3..0f7624cf 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx @@ -59,8 +59,8 @@ class eSRRF(LiquidEngine): max_slices = int((dc.global_mem_size // total_memory)/mem_div) max_slices = self._check_max_slices(image, max_slices) - kernely = np.array([[0, -1], [1, 0]]).astype(np.float32) - kernelx = np.array([[-1, 0], [0, 1]]).astype(np.float32) + kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) mf = cl.mem_flags input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[0:max_slices, :, :].nbytes, dc, max_slices)) @@ -88,6 +88,12 @@ class eSRRF(LiquidEngine): rgc_prg = cl.Program(cl_ctx, rgc_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) rgc_knl = rgc_prg.calculate_rgc + margin = int(radius*2) + lowest_row = margin # TODO discuss edges calculation + highest_row = output_shape[1] - margin + lowest_col = margin + highest_col = output_shape[2] - margin + for i in range(0, image.shape[0], max_slices): if image.shape[0] - i >= max_slices: n_slices = max_slices @@ -122,7 +128,7 @@ class eSRRF(LiquidEngine): cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification*2, image.shape[2]*magnification*2)), + self.get_work_group(dc, (n_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification)), col_gradients_cl, col_magnified_gradients_cl, np.float32(0), @@ -132,7 +138,7 @@ class eSRRF(LiquidEngine): cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification*2, image.shape[2]*magnification*2)), + self.get_work_group(dc, (n_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification)), row_gradients_cl, row_magnified_gradients_cl, np.float32(0), @@ -141,8 +147,8 @@ class eSRRF(LiquidEngine): np.float32(magnification*grad_magnification)).wait() rgc_knl(cl_queue, - (n_slices, (image.shape[1]*magnification-magnification*2) - magnification*2, image.shape[2]*magnification-magnification*2 - magnification*2), - self.get_work_group(dc, (n_slices, (image.shape[1]*magnification-magnification*2) - magnification*2, image.shape[2]*magnification-magnification*2 - magnification*2)), + (n_slices, highest_row - lowest_row, highest_col - lowest_col), + self.get_work_group(dc, (n_slices, highest_row - lowest_row, highest_col - lowest_col)), col_magnified_gradients_cl, row_magnified_gradients_cl, magnified_cl, @@ -150,7 +156,7 @@ class eSRRF(LiquidEngine): np.int32(image.shape[2]*magnification), np.int32(image.shape[1]*magnification), np.int32(magnification), - np.float32(2), + np.float32(grad_magnification), np.float32(radius), np.float32(2 * (radius / 2.355) + 1), np.float32(2 * (radius / 2.355) * (radius / 2.355)), diff --git a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx index 132b4ad8..786098df 100644 --- a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx @@ -47,6 +47,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm*2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -63,8 +64,8 @@ class RadialGradientConvergence(LiquidEngine): cdef int f, rM, cM with nogil: for f in range(nFrames): - for rM in range(_magnification*2, rowsM - _magnification*2): - for cM in range(_magnification*2, colsM - _magnification*2): + for rM in range(margin, rowsM - margin): + for cM in range(margin, colsM - margin): if _doIntensityWeighting: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: @@ -81,6 +82,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm*2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -98,11 +100,11 @@ class RadialGradientConvergence(LiquidEngine): with nogil: for f in range(nFrames): % if sch=='threaded': - for rM in prange(_magnification*2, rowsM - _magnification*2): + for rM in prange(margin, rowsM - margin): % else: - for rM in prange(_magnification*2, rowsM - _magnification*2, schedule="${sch.split('_')[1]}"): + for rM in prange(margin, rowsM - margin, schedule="${sch.split('_')[1]}"): % endif - for cM in range(_magnification*2, colsM - _magnification*2): + for cM in range(margin, colsM - margin): if _doIntensityWeighting: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: @@ -129,6 +131,7 @@ class RadialGradientConvergence(LiquidEngine): # Parameters cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm*2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -145,10 +148,10 @@ class RadialGradientConvergence(LiquidEngine): cdef int cols_interpolated = (gradient_row_interp.shape[2] / Gx_Gy_MAGNIFICATION) # Grid size of the global work space - lowest_row = int(radius*2 + 1) # TODO discuss edges calculation - highest_row = int(rows_interpolated - radius*2 - 1) - lowest_col = int(radius*2 + 1) - highest_col = int(cols_interpolated - radius*2 - 1) + lowest_row = margin # TODO discuss edges calculation + highest_row = rows_interpolated - margin + lowest_col = margin + highest_col = cols_interpolated - margin # Output rgc_map = np.zeros((nFrames, rows_interpolated, cols_interpolated), dtype=np.float32) diff --git a/src/nanopyx/core/transform/_le_esrrf.pyx b/src/nanopyx/core/transform/_le_esrrf.pyx index 7153ee66..f72d5dac 100644 --- a/src/nanopyx/core/transform/_le_esrrf.pyx +++ b/src/nanopyx/core/transform/_le_esrrf.pyx @@ -57,8 +57,8 @@ class eSRRF(LiquidEngine): max_slices = int((dc.global_mem_size // total_memory)/mem_div) max_slices = self._check_max_slices(image, max_slices) - kernely = np.array([[0, -1], [1, 0]]).astype(np.float32) - kernelx = np.array([[-1, 0], [0, 1]]).astype(np.float32) + kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) mf = cl.mem_flags input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[0:max_slices, :, :].nbytes, dc, max_slices)) @@ -86,6 +86,12 @@ class eSRRF(LiquidEngine): rgc_prg = cl.Program(cl_ctx, rgc_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) rgc_knl = rgc_prg.calculate_rgc + margin = int(radius*2) + lowest_row = margin # TODO discuss edges calculation + highest_row = output_shape[1] - margin + lowest_col = margin + highest_col = output_shape[2] - margin + for i in range(0, image.shape[0], max_slices): if image.shape[0] - i >= max_slices: n_slices = max_slices @@ -120,7 +126,7 @@ class eSRRF(LiquidEngine): cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification*2, image.shape[2]*magnification*2)), + self.get_work_group(dc, (n_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification)), col_gradients_cl, col_magnified_gradients_cl, np.float32(0), @@ -130,7 +136,7 @@ class eSRRF(LiquidEngine): cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification*2, image.shape[2]*magnification*2)), + self.get_work_group(dc, (n_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification)), row_gradients_cl, row_magnified_gradients_cl, np.float32(0), @@ -139,8 +145,8 @@ class eSRRF(LiquidEngine): np.float32(magnification*grad_magnification)).wait() rgc_knl(cl_queue, - (n_slices, (image.shape[1]*magnification-magnification*2) - magnification*2, image.shape[2]*magnification-magnification*2 - magnification*2), - self.get_work_group(dc, (n_slices, (image.shape[1]*magnification-magnification*2) - magnification*2, image.shape[2]*magnification-magnification*2 - magnification*2)), + (n_slices, highest_row - lowest_row, highest_col - lowest_col), + self.get_work_group(dc, (n_slices, highest_row - lowest_row, highest_col - lowest_col)), col_magnified_gradients_cl, row_magnified_gradients_cl, magnified_cl, @@ -148,7 +154,7 @@ class eSRRF(LiquidEngine): np.int32(image.shape[2]*magnification), np.int32(image.shape[1]*magnification), np.int32(magnification), - np.float32(2), + np.float32(grad_magnification), np.float32(radius), np.float32(2 * (radius / 2.355) + 1), np.float32(2 * (radius / 2.355) * (radius / 2.355)), diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index 8f834cec..833c6d6b 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -117,8 +117,8 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* // gradient image dimensions int nPixels_grad = nRows*Gx_Gy_MAGNIFICATION * nCols*Gx_Gy_MAGNIFICATION; - row = row + fwhm*2 + 1; - col = col + fwhm*2 + 1; + row = row + fwhm*2; + col = col + fwhm*2; if (doIntensityWeighting == 1) { image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, offset, xyoffset, angle) * imInt[f * nPixels_out + row * nCols + col]; From ab37f58b711c75945978c5ef7b75eb234c9a313f Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Tue, 1 Apr 2025 19:05:42 +0100 Subject: [PATCH 22/37] furhter changes and improvements --- .../_le_radial_gradient_convergence.pyx | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx index 6a99ebd0..a91edb9c 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx @@ -45,6 +45,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm*2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -61,8 +62,8 @@ class RadialGradientConvergence(LiquidEngine): cdef int f, rM, cM with nogil: for f in range(nFrames): - for rM in range(_magnification*2, rowsM - _magnification*2): - for cM in range(_magnification*2, colsM - _magnification*2): + for rM in range(margin, rowsM - margin): + for cM in range(margin, colsM - margin): if _doIntensityWeighting: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: @@ -78,6 +79,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm*2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -94,8 +96,8 @@ class RadialGradientConvergence(LiquidEngine): cdef int f, rM, cM with nogil: for f in range(nFrames): - for rM in prange(_magnification*2, rowsM - _magnification*2): - for cM in range(_magnification*2, colsM - _magnification*2): + for rM in prange(margin, rowsM - margin): + for cM in range(margin, colsM - margin): if _doIntensityWeighting: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: @@ -109,6 +111,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm*2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -125,8 +128,8 @@ class RadialGradientConvergence(LiquidEngine): cdef int f, rM, cM with nogil: for f in range(nFrames): - for rM in prange(_magnification*2, rowsM - _magnification*2, schedule="guided"): - for cM in range(_magnification*2, colsM - _magnification*2): + for rM in prange(margin, rowsM - margin, schedule="guided"): + for cM in range(margin, colsM - margin): if _doIntensityWeighting: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: @@ -140,6 +143,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm*2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -156,8 +160,8 @@ class RadialGradientConvergence(LiquidEngine): cdef int f, rM, cM with nogil: for f in range(nFrames): - for rM in prange(_magnification*2, rowsM - _magnification*2, schedule="dynamic"): - for cM in range(_magnification*2, colsM - _magnification*2): + for rM in prange(margin, rowsM - margin, schedule="dynamic"): + for cM in range(margin, colsM - margin): if _doIntensityWeighting: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: @@ -171,6 +175,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm*2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -187,8 +192,8 @@ class RadialGradientConvergence(LiquidEngine): cdef int f, rM, cM with nogil: for f in range(nFrames): - for rM in prange(_magnification*2, rowsM - _magnification*2, schedule="static"): - for cM in range(_magnification*2, colsM - _magnification*2): + for rM in prange(margin, rowsM - margin, schedule="static"): + for cM in range(margin, colsM - margin): if _doIntensityWeighting: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) * image_interp[f, rM, cM] else: @@ -214,6 +219,7 @@ class RadialGradientConvergence(LiquidEngine): # Parameters cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm*2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -230,10 +236,10 @@ class RadialGradientConvergence(LiquidEngine): cdef int cols_interpolated = (gradient_row_interp.shape[2] / Gx_Gy_MAGNIFICATION) # Grid size of the global work space - lowest_row = int(radius*2 + 1) # TODO discuss edges calculation - highest_row = int(rows_interpolated - radius*2 - 1) - lowest_col = int(radius*2 + 1) - highest_col = int(cols_interpolated - radius*2 - 1) + lowest_row = margin # TODO discuss edges calculation + highest_row = rows_interpolated - margin + lowest_col = margin + highest_col = cols_interpolated - margin # Output rgc_map = np.zeros((nFrames, rows_interpolated, cols_interpolated), dtype=np.float32) From 32e8e027ba0900cea6821406fcef71d15a7165d3 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Thu, 3 Apr 2025 14:01:52 +0100 Subject: [PATCH 23/37] finished fixing 2D gpu implementation --- .../nanopyx.core.transform._le_esrrf.pyx | 38 +++++++++------- src/nanopyx/core/transform/_le_esrrf.pyx | 44 ++++++++++--------- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx index 0f7624cf..8feb0fa5 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx @@ -29,15 +29,15 @@ class eSRRF(LiquidEngine): self._designation = "eSRRF_ST" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type=None): + def run(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, run_type=None): image = check_image(image) - return self._run(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle, run_type=run_type) + return self._run(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - def benchmark(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def benchmark(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True): image = check_image(image) - return super().benchmark(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle) + return super().benchmark(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) - def _run_opencl(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0, device=None, mem_div=1): + def _run_opencl(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, device=None, mem_div=1): """ @gpu """ @@ -62,6 +62,10 @@ class eSRRF(LiquidEngine): kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) + offset = 0 + xyoffset = -0.5 + angle = np.pi/4 + mf = cl.mem_flags input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[0:max_slices, :, :].nbytes, dc, max_slices)) output_cl = cl.Buffer(cl_ctx, mf.WRITE_ONLY, self._check_max_buffer_size(np.empty((max_slices, output_shape[1], output_shape[2]), dtype=np.float32).nbytes, dc, max_slices)) @@ -102,7 +106,7 @@ class eSRRF(LiquidEngine): cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification), int(image.shape[2]*magnification)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification, image.shape[2]*magnification)), + None, input_cl, magnified_cl, np.float32(0), @@ -127,8 +131,8 @@ class eSRRF(LiquidEngine): np.int32(2)).wait() cr_knl(cl_queue, - (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification)), + (n_slices, int(image.shape[1]*magnification*grad_magnification), int(image.shape[2]*magnification*grad_magnification)), + None, col_gradients_cl, col_magnified_gradients_cl, np.float32(0), @@ -137,8 +141,8 @@ class eSRRF(LiquidEngine): np.float32(magnification*grad_magnification)).wait() cr_knl(cl_queue, - (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification)), + (n_slices, int(image.shape[1]*magnification*grad_magnification), int(image.shape[2]*magnification*grad_magnification)), + None, row_gradients_cl, row_magnified_gradients_cl, np.float32(0), @@ -148,18 +152,18 @@ class eSRRF(LiquidEngine): rgc_knl(cl_queue, (n_slices, highest_row - lowest_row, highest_col - lowest_col), - self.get_work_group(dc, (n_slices, highest_row - lowest_row, highest_col - lowest_col)), + None, col_magnified_gradients_cl, row_magnified_gradients_cl, magnified_cl, output_cl, - np.int32(image.shape[2]*magnification), - np.int32(image.shape[1]*magnification), + np.int32(output_shape[2]), # Ensure correct dimensions + np.int32(output_shape[1]), np.int32(magnification), np.float32(grad_magnification), np.float32(radius), - np.float32(2 * (radius / 2.355) + 1), - np.float32(2 * (radius / 2.355) * (radius / 2.355)), + np.float32(2 * (radius / 2.355) + 1), # Match sigma calculation + np.float32(2 * (radius / 2.355) ** 2), np.float32(sensitivity), np.int32(doIntensityWeighting), np.float32(offset), @@ -177,7 +181,7 @@ class eSRRF(LiquidEngine): return output_image % for sch in schedulers: - def _run_${sch}(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_${sch}(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True): """ @cpu @threaded @@ -201,7 +205,7 @@ class eSRRF(LiquidEngine): return radial_gradients % endfor - def _run_unthreaded(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_unthreaded(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True): """ @cpu @cython diff --git a/src/nanopyx/core/transform/_le_esrrf.pyx b/src/nanopyx/core/transform/_le_esrrf.pyx index f72d5dac..9337cfac 100644 --- a/src/nanopyx/core/transform/_le_esrrf.pyx +++ b/src/nanopyx/core/transform/_le_esrrf.pyx @@ -27,15 +27,15 @@ class eSRRF(LiquidEngine): self._designation = "eSRRF_ST" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type=None): + def run(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, run_type=None): image = check_image(image) - return self._run(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle, run_type=run_type) + return self._run(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - def benchmark(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def benchmark(self, image, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1, doIntensityWeighting: bool = True): image = check_image(image) - return super().benchmark(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=offset, xyoffset=xyoffset, angle=angle) + return super().benchmark(image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) - def _run_opencl(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0, device=None, mem_div=1): + def _run_opencl(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, device=None, mem_div=1): """ @gpu """ @@ -60,6 +60,10 @@ class eSRRF(LiquidEngine): kernelx = np.array([[0, -1], [1, 0]]).astype(np.float32) kernely = np.array([[-1, 0], [0, 1]]).astype(np.float32) + offset = 0 + xyoffset = -0.5 + angle = np.pi/4 + mf = cl.mem_flags input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[0:max_slices, :, :].nbytes, dc, max_slices)) output_cl = cl.Buffer(cl_ctx, mf.WRITE_ONLY, self._check_max_buffer_size(np.empty((max_slices, output_shape[1], output_shape[2]), dtype=np.float32).nbytes, dc, max_slices)) @@ -100,7 +104,7 @@ class eSRRF(LiquidEngine): cr_knl(cl_queue, (n_slices, int(image.shape[1]*magnification), int(image.shape[2]*magnification)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification, image.shape[2]*magnification)), + None, input_cl, magnified_cl, np.float32(0), @@ -125,8 +129,8 @@ class eSRRF(LiquidEngine): np.int32(2)).wait() cr_knl(cl_queue, - (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification)), + (n_slices, int(image.shape[1]*magnification*grad_magnification), int(image.shape[2]*magnification*grad_magnification)), + None, col_gradients_cl, col_magnified_gradients_cl, np.float32(0), @@ -135,8 +139,8 @@ class eSRRF(LiquidEngine): np.float32(magnification*grad_magnification)).wait() cr_knl(cl_queue, - (n_slices, int(image.shape[1]*magnification*2), int(image.shape[2]*magnification*2)), - self.get_work_group(dc, (n_slices, image.shape[1]*magnification*grad_magnification, image.shape[2]*magnification*grad_magnification)), + (n_slices, int(image.shape[1]*magnification*grad_magnification), int(image.shape[2]*magnification*grad_magnification)), + None, row_gradients_cl, row_magnified_gradients_cl, np.float32(0), @@ -146,18 +150,18 @@ class eSRRF(LiquidEngine): rgc_knl(cl_queue, (n_slices, highest_row - lowest_row, highest_col - lowest_col), - self.get_work_group(dc, (n_slices, highest_row - lowest_row, highest_col - lowest_col)), + None, col_magnified_gradients_cl, row_magnified_gradients_cl, magnified_cl, output_cl, - np.int32(image.shape[2]*magnification), - np.int32(image.shape[1]*magnification), + np.int32(output_shape[2]), # Ensure correct dimensions + np.int32(output_shape[1]), np.int32(magnification), np.float32(grad_magnification), np.float32(radius), - np.float32(2 * (radius / 2.355) + 1), - np.float32(2 * (radius / 2.355) * (radius / 2.355)), + np.float32(2 * (radius / 2.355) + 1), # Match sigma calculation + np.float32(2 * (radius / 2.355) ** 2), np.float32(sensitivity), np.int32(doIntensityWeighting), np.float32(offset), @@ -174,7 +178,7 @@ class eSRRF(LiquidEngine): return output_image - def _run_threaded(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_threaded(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True): """ @cpu @threaded @@ -196,7 +200,7 @@ class eSRRF(LiquidEngine): radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients - def _run_threaded_guided(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_threaded_guided(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True): """ @cpu @threaded @@ -218,7 +222,7 @@ class eSRRF(LiquidEngine): radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients - def _run_threaded_dynamic(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_threaded_dynamic(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True): """ @cpu @threaded @@ -240,7 +244,7 @@ class eSRRF(LiquidEngine): radial_gradients = rgc.run(gradient_col_interp, gradient_row_interp, magnified_image, magnification=magnification, grad_magnification=grad_magnification, radius=radius, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, offset=0, xyoffset=-0.5, angle=np.pi/4, run_type=runtype) return radial_gradients - def _run_threaded_static(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_threaded_static(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True): """ @cpu @threaded @@ -263,7 +267,7 @@ class eSRRF(LiquidEngine): return radial_gradients - def _run_unthreaded(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float = 0, xyoffset: float = 0, angle: float = 0): + def _run_unthreaded(self, image, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True): """ @cpu @cython From dc7447e80e5a895a7bb541cdf2289643808ccddb Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Thu, 3 Apr 2025 14:30:38 +0100 Subject: [PATCH 24/37] added verbose option to drift and channel reg --- .../methods/channel_registration/estimator.py | 33 ++++++++++++++----- .../methods/drift_alignment/estimator.py | 20 +++++++---- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/nanopyx/methods/channel_registration/estimator.py b/src/nanopyx/methods/channel_registration/estimator.py index 95d81e16..a2f698cc 100644 --- a/src/nanopyx/methods/channel_registration/estimator.py +++ b/src/nanopyx/methods/channel_registration/estimator.py @@ -3,7 +3,9 @@ from skimage.io import imsave from .corrector import ChannelRegistrationCorrector -from ...core.analysis._le_channel_registration import ChannelRegistrationEstimator as leChannelRegistrationEstimator +from ...core.analysis._le_channel_registration import ( + ChannelRegistrationEstimator as leChannelRegistrationEstimator, +) # this class assumes that the image is a numpy array with shape = [n_channels, height, width] @@ -31,10 +33,11 @@ class ChannelRegistrationEstimator(object): It calculates translation masks for channel alignment and provides options for saving results to files. """ - def __init__(self) -> None: + def __init__(self, verbose=True) -> None: """ Initialize a ChannelRegistrationEstimator instance. """ + self.verbose = verbose self.translation_masks = None def apply_elastic_transform(self, img_stack): @@ -56,8 +59,9 @@ def apply_elastic_transform(self, img_stack): for aligning the channels in the provided image stack based on the stored translation masks. """ corrector = ChannelRegistrationCorrector() - return corrector.align_channels(img_stack, translation_masks=self.translation_masks) - + return corrector.align_channels( + img_stack, translation_masks=self.translation_masks + ) def save_translation_mask(self, path=None): """ @@ -77,7 +81,12 @@ def save_translation_mask(self, path=None): it prompts the user to input a file path and appends "_translation_masks.tif" to it as the default file name. """ if path is None: - path = input("Please provide a filepath to save the translation masks") + "_translation_masks.tif" + path = ( + input( + "Please provide a filepath to save the translation masks" + ) + + "_translation_masks.tif" + ) imsave(path + "_translation_masks.tif", self.translation_masks) @@ -123,11 +132,19 @@ def estimate( """ if ref_channel > img_stack.shape[0]: - print("Reference channel number cannot be bigger than number of channels!") + print( + "Reference channel number cannot be bigger than number of channels!" + ) return None - estimator = leChannelRegistrationEstimator() - self.translation_masks = estimator.run(np.asarray(img_stack, dtype=np.float32), ref_channel, max_shift, blocks_per_axis, min_similarity) + estimator = leChannelRegistrationEstimator(verbose=self.verbose) + self.translation_masks = estimator.run( + np.asarray(img_stack, dtype=np.float32), + ref_channel, + max_shift, + blocks_per_axis, + min_similarity, + ) if save_translation_masks: self.save_translation_mask(path=translation_mask_save_path) diff --git a/src/nanopyx/methods/drift_alignment/estimator.py b/src/nanopyx/methods/drift_alignment/estimator.py index 5945bfda..78fb396a 100644 --- a/src/nanopyx/methods/drift_alignment/estimator.py +++ b/src/nanopyx/methods/drift_alignment/estimator.py @@ -8,7 +8,9 @@ from ...core.utils.timeit import timeit from ...core.analysis.ccm import calculate_ccm from ...core.analysis.rcc import rcc -from ...core.analysis._le_drift_calculator import DriftEstimator as leDriftEstimator +from ...core.analysis._le_drift_calculator import ( + DriftEstimator as leDriftEstimator, +) class DriftEstimator(object): @@ -60,7 +62,7 @@ class DriftEstimator(object): It provides methods for estimating drift using cross-correlation and applying drift correction to an image stack. """ - def __init__(self): + def __init__(self, verbose=True): """ Initialize the `DriftEstimator` object. @@ -73,6 +75,7 @@ def __init__(self): Example: estimator = DriftEstimator() """ + self.verbose = verbose self.estimator_table = DriftEstimatorTable() self.cross_correlation_map = None self.drift_xy = None @@ -109,20 +112,25 @@ def estimate(self, image_array, **kwargs): # x0, y0, x1, y1 correspond to the exact coordinates of the roi to be used or full image dims and should be a tuple if ( - self.estimator_table.params["use_roi"] and self.estimator_table.params["roi"] is not None + self.estimator_table.params["use_roi"] + and self.estimator_table.params["roi"] is not None ): # crops image to roi - print(self.estimator_table.params["use_roi"], self.estimator_table.params["roi"]) + print( + self.estimator_table.params["use_roi"], + self.estimator_table.params["roi"], + ) x0, y0, x1, y1 = tuple(self.estimator_table.params["roi"]) image_arr = image_array[:, y0 : y1 + 1, x0 : x1 + 1] else: image_arr = image_array - estimator = leDriftEstimator() + estimator = leDriftEstimator(verbose=self.verbose) self.estimator_table.drift_table = estimator.run( np.asarray(image_arr, dtype=np.float32), time_averaging=self.estimator_table.params["time_averaging"], max_drift=self.estimator_table.params["max_expected_drift"], - ref_option=self.estimator_table.params["ref_option"]) + ref_option=self.estimator_table.params["ref_option"], + ) if self.estimator_table.params["apply"]: drift_corrector = DriftCorrector() From afc07c50f7f8ae4053e01bc8d2bad22b340d2810 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Fri, 4 Apr 2025 14:26:02 +0100 Subject: [PATCH 25/37] iterative changes to eSRRF 3D --- .../_c_sr_radial_gradient_convergence.c | 23 +- .../_c_sr_radial_gradient_convergence.h | 2 +- .../nanopyx.core.transform._le_esrrf3d.pyx | 164 +++-- src/nanopyx/core/transform/_le_esrrd3d_.cl | 139 +++++ src/nanopyx/core/transform/_le_esrrf3d.pyx | 561 +++++++++++++----- t.ipynb | 124 ++++ test.py 09-40-22-710.py | 208 +++++++ 7 files changed, 1002 insertions(+), 219 deletions(-) create mode 100644 src/nanopyx/core/transform/_le_esrrd3d_.cl create mode 100644 t.ipynb create mode 100644 test.py 09-40-22-710.py diff --git a/src/include/_c_sr_radial_gradient_convergence.c b/src/include/_c_sr_radial_gradient_convergence.c index f8e51ef0..101ed0f2 100644 --- a/src/include/_c_sr_radial_gradient_convergence.c +++ b/src/include/_c_sr_radial_gradient_convergence.c @@ -121,7 +121,7 @@ float _c_get_bound_value(float* im, int slices, int rows, int cols, int s, int r return im[_s * rows * cols + _r * cols + _c]; } -float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) { +float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) { float vx, vy, vz, Gx, Gy, Gz, dx, dy, dz, dz_real, distance, distance_xy, distance_z, distanceWeight, distanceWeight_xy, distanceWeight_z, GdotR, Dk; @@ -137,21 +137,21 @@ float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIn int _start = -(int)(2 * fwhm); int _end = (int)(2 * fwhm + 1); - int _start_z = -(int)(2 * fwhm_z); - int _end_z = (int)(2 * fwhm_z + 1); + int _start_z = -(int)(2 * fwhm); + int _end_z = (int)(2 * fwhm + 1); for (int j = _start; j <= _end; j++) { vy = yc + j; - if (0 < vy && vy < rowsM - 1) { + if (0 < vy && vy < rowsM/magnification_xy - 1) { for (int i = _start; i <= _end; i++) { vx = xc + i; - if (0 < vx && vx < colsM - 1) { + if (0 < vx && vx < colsM/magnification_xy - 1) { for (int k = _start_z; k <= _end_z; k++) { vz = zc + k; - if (0 < vz && vz < slicesM - 1) { + if (0 < vz && vz < slicesM/magnification_z - 1) { dx = vx - xc; dy = vy - yc; dz = vz - zc; @@ -161,9 +161,13 @@ float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIn distance_z = dz_real; if (distance != 0 && distance_xy <= tSO && distance_z <= tSO_z) { - Gx = _c_get_bound_value(imIntGx, slicesM*Gz_MAGNIFICATION, rowsM*Gx_Gy_MAGNIFICATION, colsM*Gx_Gy_MAGNIFICATION, vz * magnification_z * Gz_MAGNIFICATION, magnification_xy * Gx_Gy_MAGNIFICATION * vy, magnification_xy * Gx_Gy_MAGNIFICATION * vx); - Gy = _c_get_bound_value(imIntGy, slicesM*Gz_MAGNIFICATION, rowsM*Gx_Gy_MAGNIFICATION, colsM*Gx_Gy_MAGNIFICATION, vz * magnification_z * Gz_MAGNIFICATION, magnification_xy * Gx_Gy_MAGNIFICATION * vy, magnification_xy * Gx_Gy_MAGNIFICATION * vx); - Gz = _c_get_bound_value(imIntGz, slicesM*Gz_MAGNIFICATION, rowsM*Gx_Gy_MAGNIFICATION, colsM*Gx_Gy_MAGNIFICATION, vz * magnification_z * Gz_MAGNIFICATION, magnification_xy * Gx_Gy_MAGNIFICATION * vy, magnification_xy * Gx_Gy_MAGNIFICATION * vx); + int linear_index = (int)(vz * magnification_z) * rowsM * colsM + + (int)(magnification_xy * vy) * colsM + + (int)(magnification_xy * vx); + + Gx = imIntGx[linear_index]; + Gy = imIntGy[linear_index]; + Gz = imIntGz[linear_index]; // distanceWeight = _c_calculate_dw3D_isotropic(distance, tSS); distanceWeight = _c_calculate_dw3D(distance, distance_xy, distance_z, tSS, tSS_z); @@ -197,6 +201,7 @@ float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIn } return RGC; + //return imIntGy[(int)(zc * rowsM * colsM) + (int)(yc * colsM) + (int)(xc)]; } float _c_calculate_dk_3d(float dx, float dy, float dz, float Gx, float Gy, float Gz, float Gmag, float distance) { diff --git a/src/include/_c_sr_radial_gradient_convergence.h b/src/include/_c_sr_radial_gradient_convergence.h index 0c688674..4d89e987 100644 --- a/src/include/_c_sr_radial_gradient_convergence.h +++ b/src/include/_c_sr_radial_gradient_convergence.h @@ -21,7 +21,7 @@ double _c_calculate_dw_z(double distance_z, double tSS_z); double _c_calculate_dk3D(float Gx, float Gy, float Gz, float dx, float dy, float dz, float distance); -float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity); +float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity); float _c_calculate_rgc_3d(int cM, int rM, int sM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float Gx_Gy_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSS, float sensitivity); diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx index 588edb1b..24c30c98 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx @@ -1,6 +1,6 @@ <%! schedulers = ['threaded','threaded_guided','threaded_dynamic','threaded_static', 'unthreaded'] -%># cython: infer_types=True, wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False +%># cython: infer_types=True, wraparound=True, nonecheck=True, boundscheck=True, cdivision=True, language_level=3, profile=True, autogen_pxd=False import numpy as np import math import time @@ -20,7 +20,7 @@ cdef extern from "_c_gradients.h": void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, int rows, int cols) nogil cdef extern from "_c_sr_radial_gradient_convergence.h": - float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float PSF_voxel_ratio, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil + float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float PSF_voxel_ratio, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil class eSRRF3D(LiquidEngine): """ @@ -31,9 +31,7 @@ class eSRRF3D(LiquidEngine): self._designation = "eSRRF_3D" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, keep_gradients=False, keep_interpolated = False, run_type=None): - self.keep_gradients = keep_gradients - self.keep_interpolated = keep_interpolated + def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, run_type=None): # TODO: complete and check _run inputs, need to complete variables? if image.dtype != np.float32: image = image.astype(np.float32) @@ -70,14 +68,11 @@ class eSRRF3D(LiquidEngine): cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int Gx_Gy_MAGNIFICATION = 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting - time_1 = time.time() - print("Time 1", time_1 - time_start) - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -88,9 +83,6 @@ class eSRRF3D(LiquidEngine): cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - - time_2 = time.time() - print("Time 2", time_2 - time_1) for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -110,17 +102,6 @@ class eSRRF3D(LiquidEngine): gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - if self.keep_gradients: - self._gradients_s_interpolated = gradients_s_interpolated.copy() - self._gradients_r_interpolated = gradients_r_interpolated.copy() - self._gradients_c_interpolated = gradients_c_interpolated.copy() - - if self.keep_interpolated: - self._img_interpolated = img_dum.copy() - - time_3 = time.time() - print("Time 3", time_3 - time_2) - with nogil: for sM in range(0, n_slices_mag): % if sch=="unthreaded": @@ -132,50 +113,123 @@ class eSRRF3D(LiquidEngine): % endif for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val - - - time_4 = time.time() - print("Time 4", time_4 - time_3) - time_2 = time_4 return np.asarray(rgc_map) % endfor - def get_gradients(self): - if self._gradients_c_interpolated is None or self._gradients_r_interpolated is None or self._gradients_s_interpolated is None: - print("Gradients not yet calculated") - else: - return self._gradients_c_interpolated, self._gradients_r_interpolated, self._gradients_s_interpolated - def get_interpolated_image(self): - return self._img_interpolated +class eSRRF3D_v2(LiquidEngine): + """ + eSRRF 3D using the NanoPyx Liquid Engine and running as a single task. + """ + def __init__(self, clear_benchmarks=False, testing=False, verbose=True): + self._designation = "eSRRF_3D_v2" + super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) -# this function loads a binary mask and calculates the edges -def load_binary_mask_and_calculate_edges(mask_path: str): - """ - Loads a binary mask from the given path and calculates its edges. + def run(self, image, magnification_xy: int = 2, magnification_z: int = 2, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, run_type=None): + # TODO: complete and check _run inputs, need to complete variables? + if image.dtype != np.float32: + image = image.astype(np.float32) + if len(image.shape) == 4: + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + elif len(image.shape) == 3: + image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - Parameters: - mask_path (str): Path to the binary mask file. + def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + if image.dtype != np.float32: + image = image.astype(np.float32) + if len(image.shape) == 4: + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio,sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) + elif len(image.shape) == 3: + image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) - Returns: - np.ndarray: An array representing the edges of the binary mask. - """ - from scipy.ndimage import binary_erosion + % for sch in schedulers: + def _run_${sch}(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + """ + @cpu + % if sch!='unthreaded': + @threaded + % endif + @cython + """ - # Load the binary mask - mask = np.load(mask_path) - if mask.dtype != np.bool_: - raise ValueError("The mask must be a binary (boolean) array.") + time_start = time.time() + # calculate all constants + cdef float sigma = radius / 2.355 + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef int _doIntensityWeighting = doIntensityWeighting - # Calculate the edges by subtracting the eroded mask from the original mask - eroded_mask = binary_erosion(mask) - edges = mask & ~eroded_mask + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + n_slices_mag = n_slices * _magnification_z + n_rows_mag = n_rows * _magnification_xy + n_cols_mag = n_cols * _magnification_xy + + # create all necessary arrays + cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] rgc_std + if mode == "std": + rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + + cdef float delta, delta_2, rgc_val + cdef int f_i, sM, rM, cM + for f_i in range(n_frames): + # interpolate frame + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + + # calculate gradients + _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + + # interpolate gradients + # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) - return edges \ No newline at end of file + with nogil: + for sM in range(0, n_slices_mag): + % if sch=="unthreaded": + for rM in range(0, n_rows_mag): + % elif sch=="threaded": + for rM in prange(0, n_rows_mag): + % else: + for rM in prange(0, n_rows_mag, schedule="${sch.split('_')[1]}"): + % endif + for cM in range(0, n_cols_mag): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + if _doIntensityWeighting: + rgc_val = rgc_val * image_interpolated[sM, rM, cM] + if mode == "average": + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) + elif mode == "std": + delta = rgc_val - rgc_avg[sM, rM, cM] + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) + delta_2 = rgc_val - rgc_avg[sM, rM, cM] + rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) + if mode == "std": + rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) + return rgc_std + else: + return np.asarray(rgc_avg) + % endfor \ No newline at end of file diff --git a/src/nanopyx/core/transform/_le_esrrd3d_.cl b/src/nanopyx/core/transform/_le_esrrd3d_.cl new file mode 100644 index 00000000..47598eba --- /dev/null +++ b/src/nanopyx/core/transform/_le_esrrd3d_.cl @@ -0,0 +1,139 @@ +void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, + int rows, int cols) { + float ip0, ip1, ip2, ip3, ip4, ip5, ip6, ip7; + + int z_i, y_i, x_i, z_1, y_1, x_1; + + for (z_i = 0; z_i < slices; z_i++) { + for (y_i = 0; y_i < rows; y_i++) { + for (x_i = 0; x_i < cols; x_i++) { + + z_1 = z_i >= slices - 1 ? slices - 1 : z_i + 1; + y_1 = y_i >= rows - 1 ? rows - 1 : y_i + 1; + x_1 = x_i >= cols - 1 ? cols - 1 : x_i + 1; + ip0 = image[z_i * rows * cols + y_i * cols + x_i]; + ip1 = image[z_i * rows * cols + y_i * cols + x_1]; + ip2 = image[z_i * rows * cols + y_1 * cols + x_i]; + ip3 = image[z_i * rows * cols + y_1 * cols + x_1]; + ip4 = image[z_1 * rows * cols + y_i * cols + x_i]; + ip5 = image[z_1 * rows * cols + y_i * cols + x_1]; + ip6 = image[z_1 * rows * cols + y_1 * cols + x_i]; + ip7 = image[z_1 * rows * cols + y_1 * cols + x_1]; + imGc[z_i * rows* cols + y_i * cols + x_i] = + (ip1 + ip3 + ip5 + ip7 - ip0 - ip2 - ip4 - ip6) / 4; + imGr[z_i * rows* cols + y_i * cols + x_i] = + (ip2 + ip3 + ip6 + ip7 - ip0 - ip1 - ip4 - ip5) / 4; + imGs[z_i * rows* cols + y_i * cols + x_i] = + (ip4 + ip5 + ip6 + ip7 - ip0 - ip1 - ip2 - ip3) / 4; + } + } + } +} + +double _c_calculate_dw3D(double distance, double distance_xy, double distance_z,double tSS, double tSS_z) { + float D_weight_xy, D_weight_z, D_weight; + D_weight_xy = (exp((-distance_xy * distance_xy) / tSS)); + D_weight_z = (exp((-distance_z * distance_z) / tSS_z)); + D_weight = distance * (D_weight_xy * D_weight_z); + return pow(D_weight, 4); +} + +double _c_calculate_dw_xy(double distance_xy, double tSS) { + return pow((distance_xy * exp((-distance_xy * distance_xy) / tSS)), 4); +} + +double _c_calculate_dw_z(double distance_z, double tSS_z) { + return pow((distance_z * exp((-distance_z * distance_z) / tSS_z)), 4); +} + +double _c_calculate_dk3D(float Gx, float Gy, float Gz, float dx, float dy, float dz, float distance) { + float Dk = sqrt((Gy * dz - Gz * dy) * (Gy * dz - Gz * dy) + (Gz * dx - Gx * dz) * (Gz * dx - Gx * dz) + (Gx * dy - Gy * dx) * (Gx * dy - Gy * dx)) / sqrt(Gx * Gx + Gy * Gy + Gz * Gz); + if (isnan(Dk)) { + Dk = distance; + } + Dk = 1 - Dk / distance; + return Dk; +} + +float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) { + + float vx, vy, vz, Gx, Gy, Gz, dx, dy, dz, dz_real, distance, distance_xy, distance_z, distanceWeight, distanceWeight_xy, distanceWeight_z, GdotR, Dk; + + float xc = (xM) / magnification_xy; + float yc = (yM) / magnification_xy; + float zc = (sliceM) / magnification_z; + + float RGC = 0; + float distanceWeightSum = 0; + float distanceWeightSum_xy = 0; + float distanceWeightSum_z = 0; + + int _start = -(int)(2 * fwhm); + int _end = (int)(2 * fwhm + 1); + + int _start_z = -(int)(2 * fwhm_z); + int _end_z = (int)(2 * fwhm_z + 1); + + for (int j = _start; j <= _end; j++) { + vy = yc + j; + + if (0 < vy && vy < rowsM/magnification_xy - 1) { + for (int i = _start; i <= _end; i++) { + vx = xc + i; + + if (0 < vx && vx < colsM/magnification_xy - 1) { + for (int k = _start_z; k <= _end_z; k++) { + vz = zc + k; + + if (0 < vz && vz < slicesM/magnification_z - 1) { + dx = vx - xc; + dy = vy - yc; + dz = vz - zc; + dz_real = dz * ratio_px; // This has been already divided by magnification_z + distance = sqrt(dx * dx + dy * dy + dz_real * dz_real); + distance_xy = sqrt(dx * dx + dy * dy); + distance_z = dz_real; + + if (distance != 0 && distance_xy <= tSO && distance_z <= tSO_z) { + int linear_index = (int)(vz * magnification_z * Gz_MAGNIFICATION) * rowsM * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION + + (int)(magnification_xy * Gx_Gy_MAGNIFICATION * vy) * colsM * Gx_Gy_MAGNIFICATION + + (int)(magnification_xy * Gx_Gy_MAGNIFICATION * vx); + + Gx = imIntGx[linear_index]; + Gy = imIntGy[linear_index]; + Gz = imIntGz[linear_index]; + + // distanceWeight = _c_calculate_dw3D_isotropic(distance, tSS); + distanceWeight = _c_calculate_dw3D(distance, distance_xy, distance_z, tSS, tSS_z); + + // distanceWeight_xy = _c_calculate_dw_xy(distance_xy, tSS); + // distanceWeight_z = _c_calculate_dw_z(distance_z, tSS_z); + + distanceWeightSum += distanceWeight; + // distanceWeightSum_xy += distanceWeight_xy; + // distanceWeightSum_z += distanceWeight_z; + GdotR = Gx*dx + Gy*dy + Gz*dz_real; + + if (GdotR < 0) { + Dk = _c_calculate_dk3D(Gx, Gy, Gz, dx, dy, dz_real, distance); + RGC += (Dk * distanceWeight); + } + } + } + } + } + } + } + } + + RGC /= distanceWeightSum; + + if (RGC >= 0) { + RGC = pow(RGC, sensitivity); + } else { + RGC = 0; + } + + return RGC; +} + diff --git a/src/nanopyx/core/transform/_le_esrrf3d.pyx b/src/nanopyx/core/transform/_le_esrrf3d.pyx index ce361545..3d36bb18 100644 --- a/src/nanopyx/core/transform/_le_esrrf3d.pyx +++ b/src/nanopyx/core/transform/_le_esrrf3d.pyx @@ -1,4 +1,4 @@ -# cython: infer_types=True, wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False +# cython: infer_types=True, wraparound=True, nonecheck=True, boundscheck=True, cdivision=True, language_level=3, profile=True, autogen_pxd=False import numpy as np import math import time @@ -18,7 +18,7 @@ cdef extern from "_c_gradients.h": void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, int rows, int cols) nogil cdef extern from "_c_sr_radial_gradient_convergence.h": - float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float PSF_voxel_ratio, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil + float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float PSF_voxel_ratio, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil class eSRRF3D(LiquidEngine): """ @@ -29,9 +29,7 @@ class eSRRF3D(LiquidEngine): self._designation = "eSRRF_3D" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, keep_gradients=False, keep_interpolated = False, run_type=None): - self.keep_gradients = keep_gradients - self.keep_interpolated = keep_interpolated + def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, run_type=None): # TODO: complete and check _run inputs, need to complete variables? if image.dtype != np.float32: image = image.astype(np.float32) @@ -65,14 +63,11 @@ class eSRRF3D(LiquidEngine): cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int Gx_Gy_MAGNIFICATION = 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting - time_1 = time.time() - print("Time 1", time_1 - time_start) - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -83,9 +78,6 @@ class eSRRF3D(LiquidEngine): cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - - time_2 = time.time() - print("Time 2", time_2 - time_1) for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -105,32 +97,16 @@ class eSRRF3D(LiquidEngine): gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - if self.keep_gradients: - self._gradients_s_interpolated = gradients_s_interpolated.copy() - self._gradients_r_interpolated = gradients_r_interpolated.copy() - self._gradients_c_interpolated = gradients_c_interpolated.copy() - - if self.keep_interpolated: - self._img_interpolated = img_dum.copy() - - time_3 = time.time() - print("Time 3", time_3 - time_2) - with nogil: for sM in range(0, n_slices_mag): for rM in prange(0, n_rows_mag): for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val - - - time_4 = time.time() - print("Time 4", time_4 - time_3) - time_2 = time_4 return np.asarray(rgc_map) def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): @@ -148,14 +124,11 @@ class eSRRF3D(LiquidEngine): cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int Gx_Gy_MAGNIFICATION = 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting - time_1 = time.time() - print("Time 1", time_1 - time_start) - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -166,9 +139,6 @@ class eSRRF3D(LiquidEngine): cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - - time_2 = time.time() - print("Time 2", time_2 - time_1) for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -188,32 +158,16 @@ class eSRRF3D(LiquidEngine): gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - if self.keep_gradients: - self._gradients_s_interpolated = gradients_s_interpolated.copy() - self._gradients_r_interpolated = gradients_r_interpolated.copy() - self._gradients_c_interpolated = gradients_c_interpolated.copy() - - if self.keep_interpolated: - self._img_interpolated = img_dum.copy() - - time_3 = time.time() - print("Time 3", time_3 - time_2) - with nogil: for sM in range(0, n_slices_mag): for rM in prange(0, n_rows_mag, schedule="guided"): for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val - - - time_4 = time.time() - print("Time 4", time_4 - time_3) - time_2 = time_4 return np.asarray(rgc_map) def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): @@ -231,14 +185,11 @@ class eSRRF3D(LiquidEngine): cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int Gx_Gy_MAGNIFICATION = 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting - time_1 = time.time() - print("Time 1", time_1 - time_start) - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -249,9 +200,6 @@ class eSRRF3D(LiquidEngine): cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - - time_2 = time.time() - print("Time 2", time_2 - time_1) for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -271,32 +219,16 @@ class eSRRF3D(LiquidEngine): gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - if self.keep_gradients: - self._gradients_s_interpolated = gradients_s_interpolated.copy() - self._gradients_r_interpolated = gradients_r_interpolated.copy() - self._gradients_c_interpolated = gradients_c_interpolated.copy() - - if self.keep_interpolated: - self._img_interpolated = img_dum.copy() - - time_3 = time.time() - print("Time 3", time_3 - time_2) - with nogil: for sM in range(0, n_slices_mag): for rM in prange(0, n_rows_mag, schedule="dynamic"): for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val - - - time_4 = time.time() - print("Time 4", time_4 - time_3) - time_2 = time_4 return np.asarray(rgc_map) def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): @@ -314,14 +246,11 @@ class eSRRF3D(LiquidEngine): cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int Gx_Gy_MAGNIFICATION = 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting - time_1 = time.time() - print("Time 1", time_1 - time_start) - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -332,9 +261,6 @@ class eSRRF3D(LiquidEngine): cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - - time_2 = time.time() - print("Time 2", time_2 - time_1) for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -354,32 +280,16 @@ class eSRRF3D(LiquidEngine): gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - if self.keep_gradients: - self._gradients_s_interpolated = gradients_s_interpolated.copy() - self._gradients_r_interpolated = gradients_r_interpolated.copy() - self._gradients_c_interpolated = gradients_c_interpolated.copy() - - if self.keep_interpolated: - self._img_interpolated = img_dum.copy() - - time_3 = time.time() - print("Time 3", time_3 - time_2) - with nogil: for sM in range(0, n_slices_mag): for rM in prange(0, n_rows_mag, schedule="static"): for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val - - - time_4 = time.time() - print("Time 4", time_4 - time_3) - time_2 = time_4 return np.asarray(rgc_map) def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): @@ -396,14 +306,11 @@ class eSRRF3D(LiquidEngine): cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int Gx_Gy_MAGNIFICATION = 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z cdef int _doIntensityWeighting = doIntensityWeighting - time_1 = time.time() - print("Time 1", time_1 - time_start) - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] @@ -414,9 +321,6 @@ class eSRRF3D(LiquidEngine): cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - - time_2 = time.time() - print("Time 2", time_2 - time_1) for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -436,65 +340,414 @@ class eSRRF3D(LiquidEngine): gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - if self.keep_gradients: - self._gradients_s_interpolated = gradients_s_interpolated.copy() - self._gradients_r_interpolated = gradients_r_interpolated.copy() - self._gradients_c_interpolated = gradients_c_interpolated.copy() - - if self.keep_interpolated: - self._img_interpolated = img_dum.copy() - - time_3 = time.time() - print("Time 3", time_3 - time_2) - with nogil: for sM in range(0, n_slices_mag): for rM in range(0, n_rows_mag): for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val - - - time_4 = time.time() - print("Time 4", time_4 - time_3) - time_2 = time_4 return np.asarray(rgc_map) - def get_gradients(self): - if self._gradients_c_interpolated is None or self._gradients_r_interpolated is None or self._gradients_s_interpolated is None: - print("Gradients not yet calculated") + +class eSRRF3D_v2(LiquidEngine): + """ + eSRRF 3D using the NanoPyx Liquid Engine and running as a single task. + """ + + def __init__(self, clear_benchmarks=False, testing=False, verbose=True): + self._designation = "eSRRF_3D_v2" + super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) + + def run(self, image, magnification_xy: int = 2, magnification_z: int = 2, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, run_type=None): + # TODO: complete and check _run inputs, need to complete variables? + if image.dtype != np.float32: + image = image.astype(np.float32) + if len(image.shape) == 4: + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + elif len(image.shape) == 3: + image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + + def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + if image.dtype != np.float32: + image = image.astype(np.float32) + if len(image.shape) == 4: + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio,sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) + elif len(image.shape) == 3: + image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) + + def _run_threaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + """ + @cpu + @threaded + @cython + """ + + time_start = time.time() + # calculate all constants + cdef float sigma = radius / 2.355 + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef int _doIntensityWeighting = doIntensityWeighting + + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + n_slices_mag = n_slices * _magnification_z + n_rows_mag = n_rows * _magnification_xy + n_cols_mag = n_cols * _magnification_xy + + time_1 = time.time() - time_start + print("Time 1", time_1) + # create all necessary arrays + cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] rgc_std + if mode == "std": + rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + + cdef float delta, delta_2, rgc_val + cdef int f_i, sM, rM, cM + for f_i in range(n_frames): + # interpolate frame + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + + # calculate gradients + _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + + # interpolate gradients + # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + + with nogil: + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag): + for cM in range(0, n_cols_mag): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + if _doIntensityWeighting: + rgc_val = rgc_val * image_interpolated[sM, rM, cM] + if mode == "average": + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) + elif mode == "std": + delta = rgc_val - rgc_avg[sM, rM, cM] + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) + delta_2 = rgc_val - rgc_avg[sM, rM, cM] + rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) + if mode == "std": + rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) + return rgc_std + else: + return np.asarray(rgc_avg) + def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + """ + @cpu + @threaded + @cython + """ + + time_start = time.time() + # calculate all constants + cdef float sigma = radius / 2.355 + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef int _doIntensityWeighting = doIntensityWeighting + + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + n_slices_mag = n_slices * _magnification_z + n_rows_mag = n_rows * _magnification_xy + n_cols_mag = n_cols * _magnification_xy + + time_1 = time.time() - time_start + print("Time 1", time_1) + # create all necessary arrays + cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] rgc_std + if mode == "std": + rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + + cdef float delta, delta_2, rgc_val + cdef int f_i, sM, rM, cM + for f_i in range(n_frames): + # interpolate frame + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + + # calculate gradients + _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + + # interpolate gradients + # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + + with nogil: + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag, schedule="guided"): + for cM in range(0, n_cols_mag): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + if _doIntensityWeighting: + rgc_val = rgc_val * image_interpolated[sM, rM, cM] + if mode == "average": + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) + elif mode == "std": + delta = rgc_val - rgc_avg[sM, rM, cM] + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) + delta_2 = rgc_val - rgc_avg[sM, rM, cM] + rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) + if mode == "std": + rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) + return rgc_std else: - return self._gradients_c_interpolated, self._gradients_r_interpolated, self._gradients_s_interpolated + return np.asarray(rgc_avg) + def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + """ + @cpu + @threaded + @cython + """ - def get_interpolated_image(self): - return self._img_interpolated + time_start = time.time() + # calculate all constants + cdef float sigma = radius / 2.355 + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef int _doIntensityWeighting = doIntensityWeighting + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + n_slices_mag = n_slices * _magnification_z + n_rows_mag = n_rows * _magnification_xy + n_cols_mag = n_cols * _magnification_xy + + time_1 = time.time() - time_start + print("Time 1", time_1) + # create all necessary arrays + cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] rgc_std + if mode == "std": + rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + + cdef float delta, delta_2, rgc_val + cdef int f_i, sM, rM, cM + for f_i in range(n_frames): + # interpolate frame + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + + # calculate gradients + _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + + # interpolate gradients + # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) -# this function loads a binary mask and calculates the edges -def load_binary_mask_and_calculate_edges(mask_path: str): - """ - Loads a binary mask from the given path and calculates its edges. + with nogil: + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag, schedule="dynamic"): + for cM in range(0, n_cols_mag): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + if _doIntensityWeighting: + rgc_val = rgc_val * image_interpolated[sM, rM, cM] + if mode == "average": + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) + elif mode == "std": + delta = rgc_val - rgc_avg[sM, rM, cM] + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) + delta_2 = rgc_val - rgc_avg[sM, rM, cM] + rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) + if mode == "std": + rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) + return rgc_std + else: + return np.asarray(rgc_avg) + def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + """ + @cpu + @threaded + @cython + """ - Parameters: - mask_path (str): Path to the binary mask file. + time_start = time.time() + # calculate all constants + cdef float sigma = radius / 2.355 + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef int _doIntensityWeighting = doIntensityWeighting - Returns: - np.ndarray: An array representing the edges of the binary mask. - """ - from scipy.ndimage import binary_erosion + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + n_slices_mag = n_slices * _magnification_z + n_rows_mag = n_rows * _magnification_xy + n_cols_mag = n_cols * _magnification_xy + + time_1 = time.time() - time_start + print("Time 1", time_1) + # create all necessary arrays + cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] rgc_std + if mode == "std": + rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + + cdef float delta, delta_2, rgc_val + cdef int f_i, sM, rM, cM + for f_i in range(n_frames): + # interpolate frame + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + + # calculate gradients + _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + + # interpolate gradients + # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + + with nogil: + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag, schedule="static"): + for cM in range(0, n_cols_mag): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + if _doIntensityWeighting: + rgc_val = rgc_val * image_interpolated[sM, rM, cM] + if mode == "average": + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) + elif mode == "std": + delta = rgc_val - rgc_avg[sM, rM, cM] + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) + delta_2 = rgc_val - rgc_avg[sM, rM, cM] + rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) + if mode == "std": + rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) + return rgc_std + else: + return np.asarray(rgc_avg) + def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + """ + @cpu + @cython + """ - # Load the binary mask - mask = np.load(mask_path) - if mask.dtype != np.bool_: - raise ValueError("The mask must be a binary (boolean) array.") + time_start = time.time() + # calculate all constants + cdef float sigma = radius / 2.355 + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef int _doIntensityWeighting = doIntensityWeighting - # Calculate the edges by subtracting the eroded mask from the original mask - eroded_mask = binary_erosion(mask) - edges = mask & ~eroded_mask + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + n_slices_mag = n_slices * _magnification_z + n_rows_mag = n_rows * _magnification_xy + n_cols_mag = n_cols * _magnification_xy + + time_1 = time.time() - time_start + print("Time 1", time_1) + # create all necessary arrays + cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] rgc_std + if mode == "std": + rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) + cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) + + cdef float delta, delta_2, rgc_val + cdef int f_i, sM, rM, cM + for f_i in range(n_frames): + # interpolate frame + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + + # calculate gradients + _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + + # interpolate gradients + # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) - return edges \ No newline at end of file + with nogil: + for sM in range(0, n_slices_mag): + for rM in range(0, n_rows_mag): + for cM in range(0, n_cols_mag): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + if _doIntensityWeighting: + rgc_val = rgc_val * image_interpolated[sM, rM, cM] + if mode == "average": + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) + elif mode == "std": + delta = rgc_val - rgc_avg[sM, rM, cM] + rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) + delta_2 = rgc_val - rgc_avg[sM, rM, cM] + rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) + if mode == "std": + rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) + return rgc_std + else: + return np.asarray(rgc_avg) diff --git a/t.ipynb b/t.ipynb new file mode 100644 index 00000000..da85acdb --- /dev/null +++ b/t.ipynb @@ -0,0 +1,124 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from tifffile import imread\n", + "tetra = imread(\"/Users/bsaraiva/Code/NanoPyx/tetrahedron_emitters.tiff\")\n", + "#cross_single_emitters.tiff\n", + "cross = imread(\"/Users/bsaraiva/Code/NanoPyx/cross_single_emitters.tiff\")\n", + "big = imread(\"/Users/bsaraiva/Code/NanoPyx/3D_stack_maxproject_frames.tif\")\n", + "\n", + "bigger = np.random.random((100, 80, 300, 300)).astype(np.float32)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Agent: eSRRF_3D using threaded ran in 23.232891875028145 seconds\n", + "Time 1 0.0\n", + "Agent: eSRRF_3D_v2 using threaded ran in 7.750940249999985 seconds\n" + ] + } + ], + "source": [ + "from nanopyx.core.transform._le_esrrf3d import eSRRF3D, eSRRF3D_v2\n", + "from stackview import slice\n", + "\n", + "img_to_use = cross\n", + "calc = eSRRF3D()\n", + "#calc_v2 = eSRRF3D_v2()\n", + "out = calc.run(img_to_use, magnification_xy=2, magnification_z=2, PSF_voxel_ratio=4, doIntensityWeighting=False, run_type=\"threaded\")\n", + "#out_v2 = calc_v2.run(img_to_use, magnification_xy=2, magnification_z=2, PSF_voxel_ratio=4, doIntensityWeighting=False, run_type=\"threaded\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "4b18e0af2b374fc48eb5764c26db8def", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(VBox(children=(VBox(children=(HBox(children=(VBox(children=(ImageWidget(height=160, width=160),…" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "slice(np.mean(out, axis=0), zoom_factor=2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "a0f47d88b1e84eb78ee7070a156da0c0", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(VBox(children=(VBox(children=(HBox(children=(VBox(children=(ImageWidget(height=160, width=160),…" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#slice(out_v2, zoom_factor=2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "aiobio_dev", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.11" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/test.py 09-40-22-710.py b/test.py 09-40-22-710.py new file mode 100644 index 00000000..3a62d7d1 --- /dev/null +++ b/test.py 09-40-22-710.py @@ -0,0 +1,208 @@ +import numpy as np +import tifffile + +from nanopyx.core.transform import ( + RadialGradientConvergence, + GradientRobertsCross, + eSRRF_ST, + CRShiftAndMagnify, + Convolution2D, +) + +from matplotlib import pyplot as plt + + +def sobel(img): + + kernely = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]).astype(np.float32) + kernelx = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]).astype(np.float32) + + gx = Convolution2D().run(img, kernelx, run_type="OpenCL") + gy = Convolution2D().run(img, kernely, run_type="OpenCL") + + return gx, gy + + +def testingkernels(img): + + kernelx = np.array([[0, 0, 0], [-1, 0, 1], [0, 0, 0]]).astype(np.float32) + kernely = np.array([[0, -1, 0], [0, 0, 0], [0, 1, 0]]).astype(np.float32) + + print(kernelx, kernely) + + gx = Convolution2D().run(img, kernelx, run_type="OpenCL") + gy = Convolution2D().run(img, kernely, run_type="OpenCL") + + return gx, gy + + +def robertscross(img): + + kernely = np.array([[0, -1], [1, 0]]).astype(np.float32) + kernelx = np.array([[-1, 0], [0, 1]]).astype(np.float32) + + print(kernelx, kernely) + + gx = Convolution2D().run(img, kernelx, run_type="OpenCL") + gy = Convolution2D().run(img, kernely, run_type="OpenCL") + + return gx, gy + + +if __name__ == "__main__": + + # Load image + img = tifffile.imread( + "/Users/bsaraiva/Code/NanoPyx/Frames_camera_resampled_10FPS.tif", + key=range(1000), + ).astype(np.float32) + + mag = 4 + grad_mag = 1 + radius = 1.5 + + # # Calculate gradients + # gx, gy = GradientRobertsCross().run(img, run_type="OpenCL") + + # Magnify image 5x + imgmag = CRShiftAndMagnify().run(img, 0, 0, mag, mag, run_type="OpenCL") + + # # Magnify gradients 10x + # gxmag = CRShiftAndMagnify().run( + # gx, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" + # ) + # gymag = CRShiftAndMagnify().run( + # gy, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" + # ) + + # # Calculate radial gradient convergence + # rgc_old = RadialGradientConvergence().run( + # gxmag, + # gymag, + # imgmag, + # doIntensityWeighting=True, + # magnification=mag, + # grad_magnification=grad_mag, + # radius=radius, + # offset=0.0, + # xyoffset=0, + # angle=0, + # sensitivity=1, + # run_type="opencl", + # ) + + # Now do the exact same but calculate the gradients using finite differences + gx, gy = testingkernels(img) + gxmag = CRShiftAndMagnify().run( + gx, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" + ) + gymag = CRShiftAndMagnify().run( + gy, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" + ) + rgc_fd_iw = RadialGradientConvergence().run( + gxmag, + gymag, + imgmag, + doIntensityWeighting=True, + magnification=mag, + grad_magnification=grad_mag, + radius=radius, + offset=0, + xyoffset=0, + angle=0, + sensitivity=1, + run_type="opencl", + ) + + rgc_fd = RadialGradientConvergence().run( + gxmag, + gymag, + imgmag, + doIntensityWeighting=False, + magnification=mag, + grad_magnification=grad_mag, + radius=radius, + offset=0, + xyoffset=0, + angle=0, + sensitivity=1, + run_type="opencl", + ) + + # Now do the exact same but calculate the gradients using finite differences + # gx, gy = testingkernels(img) + # gxmag = CRShiftAndMagnify().run( + # gx, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" + # ) + # gymag = CRShiftAndMagnify().run( + # gy, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" + # ) + # rgc_fd_05 = RadialGradientConvergence().run( + # gxmag, + # gymag, + # imgmag, + # doIntensityWeighting=True, + # offset=0.5, + # xyoffset=0, + # angle=0, + # sensitivity=1, + # run_type="opencl", + # ) + + # Now do the exact same but calculate the gradients using a robertscross + gx, gy = robertscross(img) + gxmag = CRShiftAndMagnify().run( + gx, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" + ) + gymag = CRShiftAndMagnify().run( + gy, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" + ) + + offset = 0 + xyoffset = -0.5 + angle = np.pi / 4 + + rgc_rc = RadialGradientConvergence().run( + gxmag, + gymag, + imgmag, + doIntensityWeighting=False, + magnification=mag, + grad_magnification=grad_mag, + radius=radius, + offset=offset, + xyoffset=xyoffset, + angle=angle, + sensitivity=1, + run_type="opencl", + ) + + rgc_rc_iw = RadialGradientConvergence().run( + gxmag, + gymag, + imgmag, + doIntensityWeighting=True, + magnification=mag, + grad_magnification=grad_mag, + radius=radius, + offset=offset, + xyoffset=xyoffset, + angle=angle, + sensitivity=1, + run_type="opencl", + ) + + fig, ax = plt.subplots(1, 6) + ax[0].imshow(np.mean(imgmag, axis=0), cmap="gray") + ax[0].set_title("Image mag") + ax[1].imshow(np.mean(rgc_rc, axis=0), cmap="gray") + ax[1].set_title("RGC rc") + ax[2].imshow(np.mean(rgc_rc_iw, axis=0), cmap="gray") + ax[2].set_title("RGC rc iw") + ax[3].imshow(np.mean(rgc_fd, axis=0), cmap="gray") + ax[3].set_title("RGC fd") + ax[4].imshow(np.mean(rgc_fd_iw, axis=0), cmap="gray") + ax[4].set_title("RGC fd iw") + ax[5].imshow(np.mean(rgc_rc - rgc_fd, axis=0), cmap="gray") + ax[5].set_title("RGC fd - RGC fd") + plt.show() From 2b2af85213690776f53f49b371cf43e176505a26 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Mon, 7 Apr 2025 12:30:33 +0100 Subject: [PATCH 26/37] update --- .../nanopyx.core.transform._le_esrrf3d.pyx | 48 ++-- src/nanopyx/core/transform/_le_esrrf3d.pyx | 212 ++++++++++-------- test.py 09-40-22-710.py | 208 ----------------- 3 files changed, 143 insertions(+), 325 deletions(-) delete mode 100644 test.py 09-40-22-710.py diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx index 24c30c98..12ffb123 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx @@ -1,6 +1,6 @@ <%! schedulers = ['threaded','threaded_guided','threaded_dynamic','threaded_static', 'unthreaded'] -%># cython: infer_types=True, wraparound=True, nonecheck=True, boundscheck=True, cdivision=True, language_level=3, profile=True, autogen_pxd=False +%># cython: infer_types=True, wraparound=True, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False import numpy as np import math import time @@ -62,6 +62,7 @@ class eSRRF3D(LiquidEngine): time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm * 2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -79,11 +80,11 @@ class eSRRF3D(LiquidEngine): cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -103,15 +104,15 @@ class eSRRF3D(LiquidEngine): gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): + for sM in range(margin, n_slices_mag-margin): % if sch=="unthreaded": - for rM in range(0, n_rows_mag): + for rM in range(margin, n_rows_mag-margin): % elif sch=="threaded": - for rM in prange(0, n_rows_mag): + for rM in prange(margin, n_rows_mag-margin): % else: - for rM in prange(0, n_rows_mag, schedule="${sch.split('_')[1]}"): + for rM in prange(margin, n_rows_mag-margin, schedule="${sch.split('_')[1]}"): % endif - for cM in range(0, n_cols_mag): + for cM in range(margin, n_cols_mag-margin): if _doIntensityWeighting: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] @@ -119,6 +120,8 @@ class eSRRF3D(LiquidEngine): rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + if f == 0: + print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) return np.asarray(rgc_map) % endfor @@ -164,6 +167,8 @@ class eSRRF3D_v2(LiquidEngine): time_start = time.time() # calculate all constants cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -192,32 +197,33 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM + for f_i in range(n_frames): # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) - # calculate gradients - _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + with nogil: + # calculate gradients + _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) # interpolate gradients - # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): + for sM in range(margin, n_slices_mag-margin): % if sch=="unthreaded": - for rM in range(0, n_rows_mag): + for rM in range(margin, n_rows_mag-margin): % elif sch=="threaded": - for rM in prange(0, n_rows_mag): + for rM in prange(margin, n_rows_mag-margin): % else: - for rM in prange(0, n_rows_mag, schedule="${sch.split('_')[1]}"): + for rM in prange(margin, n_rows_mag-margin, schedule="${sch.split('_')[1]}"): % endif - for cM in range(0, n_cols_mag): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + for cM in range(margin, n_cols_mag-margin): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": diff --git a/src/nanopyx/core/transform/_le_esrrf3d.pyx b/src/nanopyx/core/transform/_le_esrrf3d.pyx index 3d36bb18..753275f2 100644 --- a/src/nanopyx/core/transform/_le_esrrf3d.pyx +++ b/src/nanopyx/core/transform/_le_esrrf3d.pyx @@ -1,4 +1,4 @@ -# cython: infer_types=True, wraparound=True, nonecheck=True, boundscheck=True, cdivision=True, language_level=3, profile=True, autogen_pxd=False +# cython: infer_types=True, wraparound=True, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False import numpy as np import math import time @@ -57,6 +57,7 @@ class eSRRF3D(LiquidEngine): time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm * 2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -74,11 +75,11 @@ class eSRRF3D(LiquidEngine): cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -98,9 +99,9 @@ class eSRRF3D(LiquidEngine): gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in prange(0, n_rows_mag): - for cM in range(0, n_cols_mag): + for sM in range(margin, n_slices_mag-margin): + for rM in prange(margin, n_rows_mag-margin): + for cM in range(margin, n_cols_mag-margin): if _doIntensityWeighting: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] @@ -108,6 +109,8 @@ class eSRRF3D(LiquidEngine): rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + if f == 0: + print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) return np.asarray(rgc_map) def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @@ -118,6 +121,7 @@ class eSRRF3D(LiquidEngine): time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm * 2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -135,11 +139,11 @@ class eSRRF3D(LiquidEngine): cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -159,9 +163,9 @@ class eSRRF3D(LiquidEngine): gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in prange(0, n_rows_mag, schedule="guided"): - for cM in range(0, n_cols_mag): + for sM in range(margin, n_slices_mag-margin): + for rM in prange(margin, n_rows_mag-margin, schedule="guided"): + for cM in range(margin, n_cols_mag-margin): if _doIntensityWeighting: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] @@ -169,6 +173,8 @@ class eSRRF3D(LiquidEngine): rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + if f == 0: + print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) return np.asarray(rgc_map) def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @@ -179,6 +185,7 @@ class eSRRF3D(LiquidEngine): time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm * 2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -196,11 +203,11 @@ class eSRRF3D(LiquidEngine): cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -220,9 +227,9 @@ class eSRRF3D(LiquidEngine): gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in prange(0, n_rows_mag, schedule="dynamic"): - for cM in range(0, n_cols_mag): + for sM in range(margin, n_slices_mag-margin): + for rM in prange(margin, n_rows_mag-margin, schedule="dynamic"): + for cM in range(margin, n_cols_mag-margin): if _doIntensityWeighting: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] @@ -230,6 +237,8 @@ class eSRRF3D(LiquidEngine): rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + if f == 0: + print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) return np.asarray(rgc_map) def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @@ -240,6 +249,7 @@ class eSRRF3D(LiquidEngine): time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm * 2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -257,11 +267,11 @@ class eSRRF3D(LiquidEngine): cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -281,9 +291,9 @@ class eSRRF3D(LiquidEngine): gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in prange(0, n_rows_mag, schedule="static"): - for cM in range(0, n_cols_mag): + for sM in range(margin, n_slices_mag-margin): + for rM in prange(margin, n_rows_mag-margin, schedule="static"): + for cM in range(margin, n_cols_mag-margin): if _doIntensityWeighting: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] @@ -291,6 +301,8 @@ class eSRRF3D(LiquidEngine): rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + if f == 0: + print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) return np.asarray(rgc_map) def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @@ -300,6 +312,7 @@ class eSRRF3D(LiquidEngine): time_start = time.time() cdef float sigma = radius / 2.355 cdef float fwhm = radius + cdef int margin = int(fwhm * 2) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -317,11 +330,11 @@ class eSRRF3D(LiquidEngine): cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 cdef float rgc_val, zcof - + for f in range(n_frames): image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) @@ -341,9 +354,9 @@ class eSRRF3D(LiquidEngine): gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in range(0, n_rows_mag): - for cM in range(0, n_cols_mag): + for sM in range(margin, n_slices_mag-margin): + for rM in range(margin, n_rows_mag-margin): + for cM in range(margin, n_cols_mag-margin): if _doIntensityWeighting: rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] @@ -351,6 +364,8 @@ class eSRRF3D(LiquidEngine): rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) rgc_map[f, sM, rM, cM] = rgc_val + if f == 0: + print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) return np.asarray(rgc_map) @@ -392,6 +407,8 @@ class eSRRF3D_v2(LiquidEngine): time_start = time.time() # calculate all constants cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -407,9 +424,7 @@ class eSRRF3D_v2(LiquidEngine): n_slices_mag = n_slices * _magnification_z n_rows_mag = n_rows * _magnification_xy n_cols_mag = n_cols * _magnification_xy - - time_1 = time.time() - time_start - print("Time 1", time_1) + # create all necessary arrays cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] rgc_std @@ -422,26 +437,27 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM + print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) for f_i in range(n_frames): # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) - # calculate gradients - _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + with nogil: + # calculate gradients + _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) # interpolate gradients - # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in prange(0, n_rows_mag): - for cM in range(0, n_cols_mag): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(margin, n_slices_mag-margin): + for rM in prange(margin, n_rows_mag-margin): + for cM in range(margin, n_cols_mag-margin): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": @@ -466,6 +482,8 @@ class eSRRF3D_v2(LiquidEngine): time_start = time.time() # calculate all constants cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -481,9 +499,7 @@ class eSRRF3D_v2(LiquidEngine): n_slices_mag = n_slices * _magnification_z n_rows_mag = n_rows * _magnification_xy n_cols_mag = n_cols * _magnification_xy - - time_1 = time.time() - time_start - print("Time 1", time_1) + # create all necessary arrays cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] rgc_std @@ -496,26 +512,27 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM + print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) for f_i in range(n_frames): # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) - # calculate gradients - _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + with nogil: + # calculate gradients + _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) # interpolate gradients - # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in prange(0, n_rows_mag, schedule="guided"): - for cM in range(0, n_cols_mag): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(margin, n_slices_mag-margin): + for rM in prange(margin, n_rows_mag-margin, schedule="guided"): + for cM in range(margin, n_cols_mag-margin): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": @@ -540,6 +557,8 @@ class eSRRF3D_v2(LiquidEngine): time_start = time.time() # calculate all constants cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -555,9 +574,7 @@ class eSRRF3D_v2(LiquidEngine): n_slices_mag = n_slices * _magnification_z n_rows_mag = n_rows * _magnification_xy n_cols_mag = n_cols * _magnification_xy - - time_1 = time.time() - time_start - print("Time 1", time_1) + # create all necessary arrays cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] rgc_std @@ -570,26 +587,27 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM + print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) for f_i in range(n_frames): # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) - # calculate gradients - _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + with nogil: + # calculate gradients + _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) # interpolate gradients - # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in prange(0, n_rows_mag, schedule="dynamic"): - for cM in range(0, n_cols_mag): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(margin, n_slices_mag-margin): + for rM in prange(margin, n_rows_mag-margin, schedule="dynamic"): + for cM in range(margin, n_cols_mag-margin): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": @@ -614,6 +632,8 @@ class eSRRF3D_v2(LiquidEngine): time_start = time.time() # calculate all constants cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -629,9 +649,7 @@ class eSRRF3D_v2(LiquidEngine): n_slices_mag = n_slices * _magnification_z n_rows_mag = n_rows * _magnification_xy n_cols_mag = n_cols * _magnification_xy - - time_1 = time.time() - time_start - print("Time 1", time_1) + # create all necessary arrays cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] rgc_std @@ -644,26 +662,27 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM + print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) for f_i in range(n_frames): # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) - # calculate gradients - _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + with nogil: + # calculate gradients + _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) # interpolate gradients - # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in prange(0, n_rows_mag, schedule="static"): - for cM in range(0, n_cols_mag): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(margin, n_slices_mag-margin): + for rM in prange(margin, n_rows_mag-margin, schedule="static"): + for cM in range(margin, n_cols_mag-margin): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": @@ -687,6 +706,8 @@ class eSRRF3D_v2(LiquidEngine): time_start = time.time() # calculate all constants cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account @@ -702,9 +723,7 @@ class eSRRF3D_v2(LiquidEngine): n_slices_mag = n_slices * _magnification_z n_rows_mag = n_rows * _magnification_xy n_cols_mag = n_cols * _magnification_xy - - time_1 = time.time() - time_start - print("Time 1", time_1) + # create all necessary arrays cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] rgc_std @@ -717,26 +736,27 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM + print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) for f_i in range(n_frames): # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], magnification_xy, _magnification_z) + image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) - # calculate gradients - _c_gradient_3d(&image_interpolated[0, 0, 0], &gradients_col_mag[0, 0, 0], &gradients_row_mag[0, 0, 0], &gradients_slices_mag[0, 0, 0], n_slices_mag, n_rows_mag, n_cols_mag) + with nogil: + # calculate gradients + _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) # interpolate gradients - # gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - # gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - # gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) + gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) + gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) with nogil: - for sM in range(0, n_slices_mag): - for rM in range(0, n_rows_mag): - for cM in range(0, n_cols_mag): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, radius, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(margin, n_slices_mag-margin): + for rM in range(margin, n_rows_mag-margin): + for cM in range(margin, n_cols_mag-margin): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": diff --git a/test.py 09-40-22-710.py b/test.py 09-40-22-710.py deleted file mode 100644 index 3a62d7d1..00000000 --- a/test.py 09-40-22-710.py +++ /dev/null @@ -1,208 +0,0 @@ -import numpy as np -import tifffile - -from nanopyx.core.transform import ( - RadialGradientConvergence, - GradientRobertsCross, - eSRRF_ST, - CRShiftAndMagnify, - Convolution2D, -) - -from matplotlib import pyplot as plt - - -def sobel(img): - - kernely = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]).astype(np.float32) - kernelx = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]).astype(np.float32) - - gx = Convolution2D().run(img, kernelx, run_type="OpenCL") - gy = Convolution2D().run(img, kernely, run_type="OpenCL") - - return gx, gy - - -def testingkernels(img): - - kernelx = np.array([[0, 0, 0], [-1, 0, 1], [0, 0, 0]]).astype(np.float32) - kernely = np.array([[0, -1, 0], [0, 0, 0], [0, 1, 0]]).astype(np.float32) - - print(kernelx, kernely) - - gx = Convolution2D().run(img, kernelx, run_type="OpenCL") - gy = Convolution2D().run(img, kernely, run_type="OpenCL") - - return gx, gy - - -def robertscross(img): - - kernely = np.array([[0, -1], [1, 0]]).astype(np.float32) - kernelx = np.array([[-1, 0], [0, 1]]).astype(np.float32) - - print(kernelx, kernely) - - gx = Convolution2D().run(img, kernelx, run_type="OpenCL") - gy = Convolution2D().run(img, kernely, run_type="OpenCL") - - return gx, gy - - -if __name__ == "__main__": - - # Load image - img = tifffile.imread( - "/Users/bsaraiva/Code/NanoPyx/Frames_camera_resampled_10FPS.tif", - key=range(1000), - ).astype(np.float32) - - mag = 4 - grad_mag = 1 - radius = 1.5 - - # # Calculate gradients - # gx, gy = GradientRobertsCross().run(img, run_type="OpenCL") - - # Magnify image 5x - imgmag = CRShiftAndMagnify().run(img, 0, 0, mag, mag, run_type="OpenCL") - - # # Magnify gradients 10x - # gxmag = CRShiftAndMagnify().run( - # gx, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" - # ) - # gymag = CRShiftAndMagnify().run( - # gy, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" - # ) - - # # Calculate radial gradient convergence - # rgc_old = RadialGradientConvergence().run( - # gxmag, - # gymag, - # imgmag, - # doIntensityWeighting=True, - # magnification=mag, - # grad_magnification=grad_mag, - # radius=radius, - # offset=0.0, - # xyoffset=0, - # angle=0, - # sensitivity=1, - # run_type="opencl", - # ) - - # Now do the exact same but calculate the gradients using finite differences - gx, gy = testingkernels(img) - gxmag = CRShiftAndMagnify().run( - gx, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" - ) - gymag = CRShiftAndMagnify().run( - gy, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" - ) - rgc_fd_iw = RadialGradientConvergence().run( - gxmag, - gymag, - imgmag, - doIntensityWeighting=True, - magnification=mag, - grad_magnification=grad_mag, - radius=radius, - offset=0, - xyoffset=0, - angle=0, - sensitivity=1, - run_type="opencl", - ) - - rgc_fd = RadialGradientConvergence().run( - gxmag, - gymag, - imgmag, - doIntensityWeighting=False, - magnification=mag, - grad_magnification=grad_mag, - radius=radius, - offset=0, - xyoffset=0, - angle=0, - sensitivity=1, - run_type="opencl", - ) - - # Now do the exact same but calculate the gradients using finite differences - # gx, gy = testingkernels(img) - # gxmag = CRShiftAndMagnify().run( - # gx, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" - # ) - # gymag = CRShiftAndMagnify().run( - # gy, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" - # ) - # rgc_fd_05 = RadialGradientConvergence().run( - # gxmag, - # gymag, - # imgmag, - # doIntensityWeighting=True, - # offset=0.5, - # xyoffset=0, - # angle=0, - # sensitivity=1, - # run_type="opencl", - # ) - - # Now do the exact same but calculate the gradients using a robertscross - gx, gy = robertscross(img) - gxmag = CRShiftAndMagnify().run( - gx, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" - ) - gymag = CRShiftAndMagnify().run( - gy, 0, 0, mag * grad_mag, mag * grad_mag, run_type="OpenCL" - ) - - offset = 0 - xyoffset = -0.5 - angle = np.pi / 4 - - rgc_rc = RadialGradientConvergence().run( - gxmag, - gymag, - imgmag, - doIntensityWeighting=False, - magnification=mag, - grad_magnification=grad_mag, - radius=radius, - offset=offset, - xyoffset=xyoffset, - angle=angle, - sensitivity=1, - run_type="opencl", - ) - - rgc_rc_iw = RadialGradientConvergence().run( - gxmag, - gymag, - imgmag, - doIntensityWeighting=True, - magnification=mag, - grad_magnification=grad_mag, - radius=radius, - offset=offset, - xyoffset=xyoffset, - angle=angle, - sensitivity=1, - run_type="opencl", - ) - - fig, ax = plt.subplots(1, 6) - ax[0].imshow(np.mean(imgmag, axis=0), cmap="gray") - ax[0].set_title("Image mag") - ax[1].imshow(np.mean(rgc_rc, axis=0), cmap="gray") - ax[1].set_title("RGC rc") - ax[2].imshow(np.mean(rgc_rc_iw, axis=0), cmap="gray") - ax[2].set_title("RGC rc iw") - ax[3].imshow(np.mean(rgc_fd, axis=0), cmap="gray") - ax[3].set_title("RGC fd") - ax[4].imshow(np.mean(rgc_fd_iw, axis=0), cmap="gray") - ax[4].set_title("RGC fd iw") - ax[5].imshow(np.mean(rgc_rc - rgc_fd, axis=0), cmap="gray") - ax[5].set_title("RGC fd - RGC fd") - plt.show() From 2c5b4f1fbb9a255f702ca71f55cd761c002c9138 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Mon, 7 Apr 2025 14:48:25 +0100 Subject: [PATCH 27/37] udpated run function --- src/nanopyx/methods/esrrf_3d/run.py | 54 ++++++++++++++++++----------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/nanopyx/methods/esrrf_3d/run.py b/src/nanopyx/methods/esrrf_3d/run.py index ab6c63d6..0d3dd4b3 100644 --- a/src/nanopyx/methods/esrrf_3d/run.py +++ b/src/nanopyx/methods/esrrf_3d/run.py @@ -1,27 +1,41 @@ from ...core.transform._le_esrrf3d import eSRRF3D -from ...core.transform.sr_temporal_correlations import calculate_eSRRF3d_temporal_correlations +from ...core.transform.sr_temporal_correlations import ( + calculate_eSRRF3d_temporal_correlations, +) -def run_esrrf3d(img,correlation="AVG", framewindow=5, rollingoverlap=2, **kwargs): + +def run_esrrf3d( + img, + mode="average", + magnification_xy=2, + magnification_z=2, + radius=1.5, + sensitivity=1, + voxel_ratio=4, + doIntensityWeighting=True, + **kwargs, +): """ - Calculate the eSRRF3D temporal correlations for the given image. + Calculate the eSRRF3D temporal correlations for the given 3D image. - Args: - img: The input 3D image. - magnification_xy: The magnification factor for the x and y axes. - magnification_z: The magnification factor for the z axis. - radius: The radius for the xy plane. - radius_z: The radius for the z axis. - sensitivity: The sensitivity for the calculation. - doIntensityWeighting: Whether to perform intensity weighting. - run_type: The type of the run. - keep_gradients: Whether to keep the gradients. - keep_interpolated: Whether to keep the interpolated values. - correlation: The type of correlation to use. - framewindow: The window size for frame. - rollingoverlap: The overlap size for rolling. + img (ndarray): The input 4D image. + mode (str, optional): The mode of time projection, default is "average", other option is "std". + magnification_xy (float, optional): The magnification factor for the x and y axes. Default is 2. + magnification_z (float, optional): The magnification factor for the z axis. Default is 2. + radius (float, optional): The radius for the xy plane. Default is 1.5. + sensitivity (float, optional): The sensitivity for the calculation. Default is 1. + voxel_ratio (float, optional): The ratio of voxel dimensions (z to xy). Default is 4. + doIntensityWeighting (bool, optional): Whether to perform intensity weighting. Default is True. + **kwargs: Additional parameters for the eSRRF3D calculation, including: + - radius_z (float, optional): The radius for the z axis. + - run_type (str, optional): The type of the run. + - keep_gradients (bool, optional): Whether to keep the gradients. + - keep_interpolated (bool, optional): Whether to keep the interpolated values. + - correlation (str, optional): The type of correlation to use. + - framewindow (int, optional): The window size for frames. + - rollingoverlap (int, optional): The overlap size for rolling. - Returns: - The calculated eSRRF3D temporal correlations. + ndarray: The calculated eSRRF3D temporal correlations. """ esrrf_calculator = eSRRF3D() - return calculate_eSRRF3d_temporal_correlations(esrrf_calculator.run(img, **kwargs), correlation=correlation, framewindow=framewindow, rollingoverlap=rollingoverlap) \ No newline at end of file + return esrrf_calculator.run(img, **kwargs) From df8f94443d3be473478538f329dd948f1b937228 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Mon, 7 Apr 2025 14:48:49 +0100 Subject: [PATCH 28/37] updated var name --- .../nanopyx.core.transform._le_esrrf3d.pyx | 126 +----- src/nanopyx/core/transform/_le_esrrf3d.pyx | 413 ++---------------- 2 files changed, 43 insertions(+), 496 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx index 12ffb123..25722ab0 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx @@ -20,7 +20,7 @@ cdef extern from "_c_gradients.h": void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, int rows, int cols) nogil cdef extern from "_c_sr_radial_gradient_convergence.h": - float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float PSF_voxel_ratio, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil + float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float voxel_ratio, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil class eSRRF3D(LiquidEngine): """ @@ -31,131 +31,27 @@ class eSRRF3D(LiquidEngine): self._designation = "eSRRF_3D" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, run_type=None): + def run(self, image, magnification_xy: int = 2, magnification_z: int = 2, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, run_type=None): # TODO: complete and check _run inputs, need to complete variables? if image.dtype != np.float32: image = image.astype(np.float32) if len(image.shape) == 4: - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) elif len(image.shape) == 3: image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): + def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): if image.dtype != np.float32: image = image.astype(np.float32) if len(image.shape) == 4: - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio,sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio,sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) elif len(image.shape) == 3: image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) % for sch in schedulers: - def _run_${sch}(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): - """ - @cpu - % if sch!='unthreaded': - @threaded - % endif - @cython - """ - time_start = time.time() - cdef float sigma = radius / 2.355 - cdef float fwhm = radius - cdef int margin = int(fwhm * 2) - cdef float tSS = 2 * sigma * sigma - cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account - cdef float fwhm_z = radius * PSF_voxel_ratio - cdef float tSS_z = 2 * sigma_z * sigma_z - cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 1 - cdef int _magnification_xy = magnification_xy - cdef int _magnification_z = magnification_z - cdef int _doIntensityWeighting = doIntensityWeighting - - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum - n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - - cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) - - cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - - cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 - - cdef float rgc_val, zcof - - for f in range(n_frames): - image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) - - n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] - - img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) - n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] - - gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - with nogil: - _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) - - gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) - gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) - gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - - with nogil: - for sM in range(margin, n_slices_mag-margin): - % if sch=="unthreaded": - for rM in range(margin, n_rows_mag-margin): - % elif sch=="threaded": - for rM in prange(margin, n_rows_mag-margin): - % else: - for rM in prange(margin, n_rows_mag-margin, schedule="${sch.split('_')[1]}"): - % endif - for cM in range(margin, n_cols_mag-margin): - if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] - else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val - - if f == 0: - print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) - return np.asarray(rgc_map) - % endfor - - -class eSRRF3D_v2(LiquidEngine): - """ - eSRRF 3D using the NanoPyx Liquid Engine and running as a single task. - """ - - def __init__(self, clear_benchmarks=False, testing=False, verbose=True): - self._designation = "eSRRF_3D_v2" - super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - - def run(self, image, magnification_xy: int = 2, magnification_z: int = 2, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, run_type=None): - # TODO: complete and check _run inputs, need to complete variables? - if image.dtype != np.float32: - image = image.astype(np.float32) - if len(image.shape) == 4: - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - elif len(image.shape) == 3: - image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - - def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): - if image.dtype != np.float32: - image = image.astype(np.float32) - if len(image.shape) == 4: - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio,sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) - elif len(image.shape) == 3: - image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) - - % for sch in schedulers: - def _run_${sch}(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def _run_${sch}(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): """ @cpu % if sch!='unthreaded': @@ -171,12 +67,12 @@ class eSRRF3D_v2(LiquidEngine): cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag @@ -223,7 +119,7 @@ class eSRRF3D_v2(LiquidEngine): for rM in prange(margin, n_rows_mag-margin, schedule="${sch.split('_')[1]}"): % endif for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": diff --git a/src/nanopyx/core/transform/_le_esrrf3d.pyx b/src/nanopyx/core/transform/_le_esrrf3d.pyx index 753275f2..1d591e46 100644 --- a/src/nanopyx/core/transform/_le_esrrf3d.pyx +++ b/src/nanopyx/core/transform/_le_esrrf3d.pyx @@ -18,7 +18,7 @@ cdef extern from "_c_gradients.h": void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, int rows, int cols) nogil cdef extern from "_c_sr_radial_gradient_convergence.h": - float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float PSF_voxel_ratio, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil + float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float voxel_ratio, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil class eSRRF3D(LiquidEngine): """ @@ -29,375 +29,26 @@ class eSRRF3D(LiquidEngine): self._designation = "eSRRF_3D" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, run_type=None): + def run(self, image, magnification_xy: int = 2, magnification_z: int = 2, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, run_type=None): # TODO: complete and check _run inputs, need to complete variables? if image.dtype != np.float32: image = image.astype(np.float32) if len(image.shape) == 4: - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) elif len(image.shape) == 3: image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): + def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): if image.dtype != np.float32: image = image.astype(np.float32) if len(image.shape) == 4: - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio,sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio,sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) elif len(image.shape) == 3: image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) - def _run_threaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): - """ - @cpu - @threaded - @cython - """ - time_start = time.time() - cdef float sigma = radius / 2.355 - cdef float fwhm = radius - cdef int margin = int(fwhm * 2) - cdef float tSS = 2 * sigma * sigma - cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account - cdef float fwhm_z = radius * PSF_voxel_ratio - cdef float tSS_z = 2 * sigma_z * sigma_z - cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 1 - cdef int _magnification_xy = magnification_xy - cdef int _magnification_z = magnification_z - cdef int _doIntensityWeighting = doIntensityWeighting - - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum - n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - - cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) - - cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - - cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 - - cdef float rgc_val, zcof - - for f in range(n_frames): - image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) - - n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] - - img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) - n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] - - gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - with nogil: - _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) - - gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) - gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) - gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - - with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in prange(margin, n_rows_mag-margin): - for cM in range(margin, n_cols_mag-margin): - if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] - else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val - - if f == 0: - print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) - return np.asarray(rgc_map) - def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): - """ - @cpu - @threaded - @cython - """ - time_start = time.time() - cdef float sigma = radius / 2.355 - cdef float fwhm = radius - cdef int margin = int(fwhm * 2) - cdef float tSS = 2 * sigma * sigma - cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account - cdef float fwhm_z = radius * PSF_voxel_ratio - cdef float tSS_z = 2 * sigma_z * sigma_z - cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 1 - cdef int _magnification_xy = magnification_xy - cdef int _magnification_z = magnification_z - cdef int _doIntensityWeighting = doIntensityWeighting - - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum - n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - - cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) - - cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - - cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 - - cdef float rgc_val, zcof - - for f in range(n_frames): - image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) - - n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] - - img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) - n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] - - gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - with nogil: - _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) - - gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) - gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) - gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - - with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in prange(margin, n_rows_mag-margin, schedule="guided"): - for cM in range(margin, n_cols_mag-margin): - if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] - else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val - - if f == 0: - print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) - return np.asarray(rgc_map) - def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): - """ - @cpu - @threaded - @cython - """ - time_start = time.time() - cdef float sigma = radius / 2.355 - cdef float fwhm = radius - cdef int margin = int(fwhm * 2) - cdef float tSS = 2 * sigma * sigma - cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account - cdef float fwhm_z = radius * PSF_voxel_ratio - cdef float tSS_z = 2 * sigma_z * sigma_z - cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 1 - cdef int _magnification_xy = magnification_xy - cdef int _magnification_z = magnification_z - cdef int _doIntensityWeighting = doIntensityWeighting - - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum - n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - - cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) - - cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - - cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 - - cdef float rgc_val, zcof - - for f in range(n_frames): - image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) - - n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] - - img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) - n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] - - gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - with nogil: - _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) - - gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) - gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) - gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - - with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in prange(margin, n_rows_mag-margin, schedule="dynamic"): - for cM in range(margin, n_cols_mag-margin): - if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] - else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val - - if f == 0: - print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) - return np.asarray(rgc_map) - def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): - """ - @cpu - @threaded - @cython - """ - time_start = time.time() - cdef float sigma = radius / 2.355 - cdef float fwhm = radius - cdef int margin = int(fwhm * 2) - cdef float tSS = 2 * sigma * sigma - cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account - cdef float fwhm_z = radius * PSF_voxel_ratio - cdef float tSS_z = 2 * sigma_z * sigma_z - cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 1 - cdef int _magnification_xy = magnification_xy - cdef int _magnification_z = magnification_z - cdef int _doIntensityWeighting = doIntensityWeighting - - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum - n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - - cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) - - cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - - cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 - - cdef float rgc_val, zcof - - for f in range(n_frames): - image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) - - n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] - - img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) - n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] - - gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - with nogil: - _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) - - gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) - gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) - gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - - with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in prange(margin, n_rows_mag-margin, schedule="static"): - for cM in range(margin, n_cols_mag-margin): - if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] - else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val - - if f == 0: - print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) - return np.asarray(rgc_map) - def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): - """ - @cpu - @cython - """ - time_start = time.time() - cdef float sigma = radius / 2.355 - cdef float fwhm = radius - cdef int margin = int(fwhm * 2) - cdef float tSS = 2 * sigma * sigma - cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account - cdef float fwhm_z = radius * PSF_voxel_ratio - cdef float tSS_z = 2 * sigma_z * sigma_z - cdef float tSO_z = 2 * sigma_z + 1 - cdef int Gx_Gy_MAGNIFICATION = 1 - cdef int _magnification_xy = magnification_xy - cdef int _magnification_z = magnification_z - cdef int _doIntensityWeighting = doIntensityWeighting - - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum - n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - - cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) - - cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum - - cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 - - cdef float rgc_val, zcof - - for f in range(n_frames): - image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) - - n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] - - img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) - n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] - - gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) - with nogil: - _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) - - gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) - gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) - gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - - with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in range(margin, n_rows_mag-margin): - for cM in range(margin, n_cols_mag-margin): - if _doIntensityWeighting: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] - else: - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) - rgc_map[f, sM, rM, cM] = rgc_val - - if f == 0: - print(image.shape, image_interpolated.shape,gradients_c.shape, gradients_r.shape, gradients_s.shape, gradients_c_interpolated.shape,gradients_r_interpolated.shape, gradients_s_interpolated.shape) - return np.asarray(rgc_map) - - -class eSRRF3D_v2(LiquidEngine): - """ - eSRRF 3D using the NanoPyx Liquid Engine and running as a single task. - """ - - def __init__(self, clear_benchmarks=False, testing=False, verbose=True): - self._designation = "eSRRF_3D_v2" - super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - - def run(self, image, magnification_xy: int = 2, magnification_z: int = 2, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, run_type=None): - # TODO: complete and check _run inputs, need to complete variables? - if image.dtype != np.float32: - image = image.astype(np.float32) - if len(image.shape) == 4: - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - elif len(image.shape) == 3: - image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - - def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): - if image.dtype != np.float32: - image = image.astype(np.float32) - if len(image.shape) == 4: - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio,sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) - elif len(image.shape) == 3: - image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) - - def _run_threaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def _run_threaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): """ @cpu @threaded @@ -411,12 +62,12 @@ class eSRRF3D_v2(LiquidEngine): cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag @@ -439,7 +90,7 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM - print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) + for f_i in range(n_frames): # interpolate frame image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) @@ -457,7 +108,7 @@ class eSRRF3D_v2(LiquidEngine): for sM in range(margin, n_slices_mag-margin): for rM in prange(margin, n_rows_mag-margin): for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": @@ -472,7 +123,7 @@ class eSRRF3D_v2(LiquidEngine): return rgc_std else: return np.asarray(rgc_avg) - def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): """ @cpu @threaded @@ -486,12 +137,12 @@ class eSRRF3D_v2(LiquidEngine): cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag @@ -514,7 +165,7 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM - print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) + for f_i in range(n_frames): # interpolate frame image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) @@ -532,7 +183,7 @@ class eSRRF3D_v2(LiquidEngine): for sM in range(margin, n_slices_mag-margin): for rM in prange(margin, n_rows_mag-margin, schedule="guided"): for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": @@ -547,7 +198,7 @@ class eSRRF3D_v2(LiquidEngine): return rgc_std else: return np.asarray(rgc_avg) - def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): """ @cpu @threaded @@ -561,12 +212,12 @@ class eSRRF3D_v2(LiquidEngine): cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag @@ -589,7 +240,7 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM - print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) + for f_i in range(n_frames): # interpolate frame image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) @@ -607,7 +258,7 @@ class eSRRF3D_v2(LiquidEngine): for sM in range(margin, n_slices_mag-margin): for rM in prange(margin, n_rows_mag-margin, schedule="dynamic"): for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": @@ -622,7 +273,7 @@ class eSRRF3D_v2(LiquidEngine): return rgc_std else: return np.asarray(rgc_avg) - def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): """ @cpu @threaded @@ -636,12 +287,12 @@ class eSRRF3D_v2(LiquidEngine): cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag @@ -664,7 +315,7 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM - print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) + for f_i in range(n_frames): # interpolate frame image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) @@ -682,7 +333,7 @@ class eSRRF3D_v2(LiquidEngine): for sM in range(margin, n_slices_mag-margin): for rM in prange(margin, n_rows_mag-margin, schedule="static"): for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": @@ -697,7 +348,7 @@ class eSRRF3D_v2(LiquidEngine): return rgc_std else: return np.asarray(rgc_avg) - def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): """ @cpu @cython @@ -710,12 +361,12 @@ class eSRRF3D_v2(LiquidEngine): cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _PSF_voxel_ratio = PSF_voxel_ratio + cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag @@ -738,7 +389,7 @@ class eSRRF3D_v2(LiquidEngine): cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) cdef float delta, delta_2, rgc_val cdef int f_i, sM, rM, cM - print(image.shape, image_interpolated.shape, gradients_col.shape, gradients_row.shape, gradients_slices.shape, gradients_col_mag.shape, gradients_row_mag.shape, gradients_slices_mag.shape) + for f_i in range(n_frames): # interpolate frame image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) @@ -756,7 +407,7 @@ class eSRRF3D_v2(LiquidEngine): for sM in range(margin, n_slices_mag-margin): for rM in range(margin, n_rows_mag-margin): for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _PSF_voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) if _doIntensityWeighting: rgc_val = rgc_val * image_interpolated[sM, rM, cM] if mode == "average": From 0dc76a075cbab906c11c5f5c76f220c38a891e56 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Mon, 7 Apr 2025 14:59:44 +0100 Subject: [PATCH 29/37] fixed typo --- src/nanopyx/core/transform/{_le_esrrd3d_.cl => _le_esrrf3d_.cl} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/nanopyx/core/transform/{_le_esrrd3d_.cl => _le_esrrf3d_.cl} (100%) diff --git a/src/nanopyx/core/transform/_le_esrrd3d_.cl b/src/nanopyx/core/transform/_le_esrrf3d_.cl similarity index 100% rename from src/nanopyx/core/transform/_le_esrrd3d_.cl rename to src/nanopyx/core/transform/_le_esrrf3d_.cl From d32feabe154e6d28d0dbdcf005077fae91fcca83 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Mon, 7 Apr 2025 15:58:57 +0100 Subject: [PATCH 30/37] wrote python side of opencl runtype --- .../nanopyx.core.transform._le_esrrf3d.pyx | 156 +++++++++++++++++- src/nanopyx/core/transform/_le_esrrf3d.pyx | 154 +++++++++++++++++ 2 files changed, 309 insertions(+), 1 deletion(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx index 25722ab0..ec3a66ed 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx @@ -15,6 +15,7 @@ from .sr_temporal_correlations import calculate_eSRRF_temporal_correlations from ._interpolation import interpolate_3d, interpolate_3d_zlinear from ._le_interpolation_catmull_rom import ShiftAndMagnify from ...__liquid_engine__ import LiquidEngine +from ...__opencl__ import cl, cl_array, _fastest_device cdef extern from "_c_gradients.h": void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, int rows, int cols) nogil @@ -134,4 +135,157 @@ class eSRRF3D(LiquidEngine): return rgc_std else: return np.asarray(rgc_avg) - % endfor \ No newline at end of file + % endfor + + def _run_opencl(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, device=None, mem_div=1): + """ + @gpu + """ + + cl_ctx = cl.Context([device['device']]) + dc = device['device'] + cl_queue = cl.CommandQueue(cl_ctx) + + output_shape = (image.shape[1] * magnification_z, image.shape[2] * magnification_xy, image.shape[3] * magnification_xy) + + output_image = np.zeros(output_shape, dtype=np.float32) + tmp_slice = np.zeros(output_shape, dtype=np.float32) + + #max_slices = int((dc.global_mem_size - (3 * (image[0, :, :, :].nbytes) + 4 * (output_image.nbytes)) // (image[0, :, :, :].nbytes)) // mem_div) + #max_slices = self._check_max_slices(image, max_slices) + + max_slices = 1 + + mf = cl.mem_flags + + # create cl buffer for input image + input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[:, :, :, :].nbytes, dc, max_slices)) + cl.enqueue_copy(cl_queue, input_cl, image[:,:,:, :]).wait() + + # create magnified input image buffer + magnified_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + + # create gradients buffers + col_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) + row_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) + z_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) + + # create magnified gradients buffers + col_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + row_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + z_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + + # create the output buffer + output_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + + esrrf3d_cl_code = self._get_cl_code("esrrf3d.cl", device["DP"]) + esrrf3d_cl_prg = cl.Program(cl_ctx, esrrf3d_cl_code).build(options=["-cl-fast-relaxed-math", "-cl-mad-enable"]) + interp_knl = esrrf3d_cl_prg.interpolate_3d + grads_knl = esrrf3d_cl_prg.gradients_3d + rgc_knl = esrrf3d_cl_prg.calculate_rgc3D + + margin = int(radius*2) + lowest_row = margin # TODO discuss edges calculation + highest_row = output_shape[1] - margin + lowest_col = margin + highest_col = output_shape[2] - margin + + for f in range(image.shape[0]): + + interp_knl( + cl_queue, + (image.shape[1], image.shape[2], image.shape[3]), + None, + input_cl, + magnified_cl, + np.int32(magnification_xy), + np.int32(magnification_z), + np.int32(f) + ).wait() + + + grads_knl( + cl_queue, + (image.shape[1], image.shape[2], image.shape[3]), + None, + input_cl, + col_gradients_cl, + row_gradients_cl, + z_gradients_cl, + np.int32(f) + ).wait() + + interp_knl( + cl_queue, + (output_shape[0], output_shape[1], output_shape[2]), + None, + col_gradients_cl, + col_magnified_gradients_cl, + np.int32(magnification_xy), + np.int32(magnification_z), + np.int32(f) + ).wait() + + interp_knl( + cl_queue, + (output_shape[0], output_shape[1], output_shape[2]), + None, + row_gradients_cl, + row_magnified_gradients_cl, + np.int32(magnification_xy), + np.int32(magnification_z), + np.int32(f) + ).wait() + + interp_knl( + cl_queue, + (output_shape[0], output_shape[1], output_shape[2]), + None, + z_gradients_cl, + z_magnified_gradients_cl, + np.int32(magnification_xy), + np.int32(magnification_z), + np.int32(f) + ).wait() + + rgc_knl( + cl_queue, + (output_shape[0], output_shape[1], output_shape[2]), + None, + col_magnified_gradients_cl, + row_magnified_gradients_cl, + z_magnified_gradients_cl, + magnified_cl, + output_cl, + np.int32(output_shape[1]), + np.int32(output_shape[2]), + np.int32(output_shape[0]), + np.int32(magnification_xy), + np.int32(magnification_z), + np.float32(voxel_ratio), + np.float32(radius), + np.float32(2 * (radius/2.355) + 1), + np.float32(2 * (radius*voxel_ratio/2.355) + 1), + np.float32(2 * (radius/2.355) * (radius/2.355)), + np.float32(2 * (radius*voxel_ratio/2.355) * (radius*voxel_ratio/2.355)), + np.int32(sensitivity), + np.int32(doIntensityWeighting) + + ).wait() + + cl.enqueue_copy(cl_queue, tmp_slice, output_cl).wait() + + if mode == "average": + output_image = output_image + (tmp_slice - output_image) / (f + 1) + elif mode == "std": + delta = tmp_slice - output_image + output_image = output_image + (delta) / (f + 1) + delta_2 = tmp_slice - output_image + output_image = output_image + (delta * delta_2) + + + if mode == "std": + output_image = np.sqrt(np.asarray(output_image) / image.shape[0]) + return output_image + else: + return np.asarray(output_image) \ No newline at end of file diff --git a/src/nanopyx/core/transform/_le_esrrf3d.pyx b/src/nanopyx/core/transform/_le_esrrf3d.pyx index 1d591e46..4d338108 100644 --- a/src/nanopyx/core/transform/_le_esrrf3d.pyx +++ b/src/nanopyx/core/transform/_le_esrrf3d.pyx @@ -13,6 +13,7 @@ from .sr_temporal_correlations import calculate_eSRRF_temporal_correlations from ._interpolation import interpolate_3d, interpolate_3d_zlinear from ._le_interpolation_catmull_rom import ShiftAndMagnify from ...__liquid_engine__ import LiquidEngine +from ...__opencl__ import cl, cl_array, _fastest_device cdef extern from "_c_gradients.h": void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, int rows, int cols) nogil @@ -422,3 +423,156 @@ class eSRRF3D(LiquidEngine): return rgc_std else: return np.asarray(rgc_avg) + + def _run_opencl(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, device=None, mem_div=1): + """ + @gpu + """ + + cl_ctx = cl.Context([device['device']]) + dc = device['device'] + cl_queue = cl.CommandQueue(cl_ctx) + + output_shape = (image.shape[1] * magnification_z, image.shape[2] * magnification_xy, image.shape[3] * magnification_xy) + + output_image = np.zeros(output_shape, dtype=np.float32) + tmp_slice = np.zeros(output_shape, dtype=np.float32) + + #max_slices = int((dc.global_mem_size - (3 * (image[0, :, :, :].nbytes) + 4 * (output_image.nbytes)) // (image[0, :, :, :].nbytes)) // mem_div) + #max_slices = self._check_max_slices(image, max_slices) + + max_slices = 1 + + mf = cl.mem_flags + + # create cl buffer for input image + input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[:, :, :, :].nbytes, dc, max_slices)) + cl.enqueue_copy(cl_queue, input_cl, image[:,:,:, :]).wait() + + # create magnified input image buffer + magnified_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + + # create gradients buffers + col_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) + row_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) + z_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) + + # create magnified gradients buffers + col_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + row_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + z_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + + # create the output buffer + output_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) + + esrrf3d_cl_code = self._get_cl_code("esrrf3d.cl", device["DP"]) + esrrf3d_cl_prg = cl.Program(cl_ctx, esrrf3d_cl_code).build(options=["-cl-fast-relaxed-math", "-cl-mad-enable"]) + interp_knl = esrrf3d_cl_prg.interpolate_3d + grads_knl = esrrf3d_cl_prg.gradients_3d + rgc_knl = esrrf3d_cl_prg.calculate_rgc3D + + margin = int(radius*2) + lowest_row = margin # TODO discuss edges calculation + highest_row = output_shape[1] - margin + lowest_col = margin + highest_col = output_shape[2] - margin + + for f in range(image.shape[0]): + + interp_knl( + cl_queue, + (image.shape[1], image.shape[2], image.shape[3]), + None, + input_cl, + magnified_cl, + np.int32(magnification_xy), + np.int32(magnification_z), + np.int32(f) + ).wait() + + + grads_knl( + cl_queue, + (image.shape[1], image.shape[2], image.shape[3]), + None, + input_cl, + col_gradients_cl, + row_gradients_cl, + z_gradients_cl, + np.int32(f) + ).wait() + + interp_knl( + cl_queue, + (output_shape[0], output_shape[1], output_shape[2]), + None, + col_gradients_cl, + col_magnified_gradients_cl, + np.int32(magnification_xy), + np.int32(magnification_z), + np.int32(f) + ).wait() + + interp_knl( + cl_queue, + (output_shape[0], output_shape[1], output_shape[2]), + None, + row_gradients_cl, + row_magnified_gradients_cl, + np.int32(magnification_xy), + np.int32(magnification_z), + np.int32(f) + ).wait() + + interp_knl( + cl_queue, + (output_shape[0], output_shape[1], output_shape[2]), + None, + z_gradients_cl, + z_magnified_gradients_cl, + np.int32(magnification_xy), + np.int32(magnification_z), + np.int32(f) + ).wait() + + rgc_knl( + cl_queue, + (output_shape[0], output_shape[1], output_shape[2]), + None, + col_magnified_gradients_cl, + row_magnified_gradients_cl, + z_magnified_gradients_cl, + magnified_cl, + output_cl, + np.int32(output_shape[1]), + np.int32(output_shape[2]), + np.int32(output_shape[0]), + np.int32(magnification_xy), + np.int32(magnification_z), + np.float32(voxel_ratio), + np.float32(radius), + np.float32(2 * (radius/2.355) + 1), + np.float32(2 * (radius*voxel_ratio/2.355) + 1), + np.float32(2 * (radius/2.355) * (radius/2.355)), + np.float32(2 * (radius*voxel_ratio/2.355) * (radius*voxel_ratio/2.355)), + np.int32(sensitivity), + np.int32(doIntensityWeighting) + + ).wait() + + cl.enqueue_copy(cl_queue, tmp_slice, output_cl).wait() + + if mode == "average": + output_image = output_image + (tmp_slice - output_image) / (f + 1) + elif mode == "std": + delta = tmp_slice - output_image + output_image = output_image + (delta) / (f + 1) + delta_2 = tmp_slice - output_image + output_image = output_image + (delta * delta_2) + + + if mode == "std": + output_image = np.sqrt(np.asarray(output_image) / image.shape[0]) + return output_image + else: + return np.asarray(output_image) \ No newline at end of file From 8a410c67dac4341b1944972a86268b5247a7804a Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Tue, 8 Apr 2025 16:41:46 +0100 Subject: [PATCH 31/37] bugfixing --- .../nanopyx.core.transform._le_esrrf.pyx | 2 +- .../nanopyx.core.transform._le_esrrf3d.pyx | 23 ++++++++------ ...nsform._le_radial_gradient_convergence.pyx | 14 ++++----- src/nanopyx/core/transform/_le_esrrf.pyx | 2 +- src/nanopyx/core/transform/_le_esrrf3d.pyx | 23 ++++++++------ src/nanopyx/core/transform/_le_esrrf3d_.cl | 11 ++++++- .../_le_radial_gradient_convergence.cl | 4 +-- .../_le_radial_gradient_convergence.pyx | 30 +++++++++++-------- 8 files changed, 66 insertions(+), 43 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx index 8feb0fa5..d1c5b98e 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf.pyx @@ -92,7 +92,7 @@ class eSRRF(LiquidEngine): rgc_prg = cl.Program(cl_ctx, rgc_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) rgc_knl = rgc_prg.calculate_rgc - margin = int(radius*2) + margin = int(radius*2*magnification) lowest_row = margin # TODO discuss edges calculation highest_row = output_shape[1] - margin lowest_col = margin diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx index ec3a66ed..35692613 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx @@ -212,7 +212,10 @@ class eSRRF3D(LiquidEngine): col_gradients_cl, row_gradients_cl, z_gradients_cl, - np.int32(f) + np.int32(f), + np.int32(image.shape[1]), + np.int32(image.shape[2]), + np.int32(image.shape[3]), ).wait() interp_knl( @@ -256,7 +259,7 @@ class eSRRF3D(LiquidEngine): row_magnified_gradients_cl, z_magnified_gradients_cl, magnified_cl, - output_cl, + tmp_slice, np.int32(output_shape[1]), np.int32(output_shape[2]), np.int32(output_shape[0]), @@ -273,16 +276,18 @@ class eSRRF3D(LiquidEngine): ).wait() - cl.enqueue_copy(cl_queue, tmp_slice, output_cl).wait() - + + # TODO change tmp_slice to cl arrays if mode == "average": - output_image = output_image + (tmp_slice - output_image) / (f + 1) + output_cl = output_cl + (tmp_slice - output_cl) / (f + 1) elif mode == "std": - delta = tmp_slice - output_image - output_image = output_image + (delta) / (f + 1) - delta_2 = tmp_slice - output_image - output_image = output_image + (delta * delta_2) + delta = tmp_slice - output_cl + output_cl = output_cl + (delta) / (f + 1) + delta_2 = tmp_slice - output_cl + output_cl = output_cl + (delta * delta_2) + + cl.enqueue_copy(cl_queue, output_image, output_cl).wait() if mode == "std": output_image = np.sqrt(np.asarray(output_image) / image.shape[0]) diff --git a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx index 786098df..fe64b25a 100644 --- a/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_radial_gradient_convergence.pyx @@ -27,7 +27,7 @@ class RadialGradientConvergence(LiquidEngine): verbose=verbose) - def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): + def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, grad_magnification: int = 1, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) @@ -40,14 +40,14 @@ class RadialGradientConvergence(LiquidEngine): image_interp = check_image(image_interp) return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, grad_magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=1, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @cython """ cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(fwhm*2) + cdef int margin = int(fwhm*2) * magnification cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -74,7 +74,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) % for sch in schedulers: - def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): + def _run_${sch}(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=1, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -82,7 +82,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(fwhm*2) + cdef int margin = int(fwhm*2) * magnification cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -113,7 +113,7 @@ class RadialGradientConvergence(LiquidEngine): % endfor - def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): + def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, grad_magnification=1, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): """ @gpu """ @@ -131,7 +131,7 @@ class RadialGradientConvergence(LiquidEngine): # Parameters cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(fwhm*2) + cdef int margin = int(fwhm*2) * magnification cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification diff --git a/src/nanopyx/core/transform/_le_esrrf.pyx b/src/nanopyx/core/transform/_le_esrrf.pyx index 9337cfac..860ab942 100644 --- a/src/nanopyx/core/transform/_le_esrrf.pyx +++ b/src/nanopyx/core/transform/_le_esrrf.pyx @@ -90,7 +90,7 @@ class eSRRF(LiquidEngine): rgc_prg = cl.Program(cl_ctx, rgc_code).build(options=["-cl-mad-enable -cl-fast-relaxed-math"]) rgc_knl = rgc_prg.calculate_rgc - margin = int(radius*2) + margin = int(radius*2*magnification) lowest_row = margin # TODO discuss edges calculation highest_row = output_shape[1] - margin lowest_col = margin diff --git a/src/nanopyx/core/transform/_le_esrrf3d.pyx b/src/nanopyx/core/transform/_le_esrrf3d.pyx index 4d338108..1feeff9a 100644 --- a/src/nanopyx/core/transform/_le_esrrf3d.pyx +++ b/src/nanopyx/core/transform/_le_esrrf3d.pyx @@ -499,7 +499,10 @@ class eSRRF3D(LiquidEngine): col_gradients_cl, row_gradients_cl, z_gradients_cl, - np.int32(f) + np.int32(f), + np.int32(image.shape[1]), + np.int32(image.shape[2]), + np.int32(image.shape[3]), ).wait() interp_knl( @@ -543,7 +546,7 @@ class eSRRF3D(LiquidEngine): row_magnified_gradients_cl, z_magnified_gradients_cl, magnified_cl, - output_cl, + tmp_slice, np.int32(output_shape[1]), np.int32(output_shape[2]), np.int32(output_shape[0]), @@ -560,16 +563,18 @@ class eSRRF3D(LiquidEngine): ).wait() - cl.enqueue_copy(cl_queue, tmp_slice, output_cl).wait() - + + # TODO change tmp_slice to cl arrays if mode == "average": - output_image = output_image + (tmp_slice - output_image) / (f + 1) + output_cl = output_cl + (tmp_slice - output_cl) / (f + 1) elif mode == "std": - delta = tmp_slice - output_image - output_image = output_image + (delta) / (f + 1) - delta_2 = tmp_slice - output_image - output_image = output_image + (delta * delta_2) + delta = tmp_slice - output_cl + output_cl = output_cl + (delta) / (f + 1) + delta_2 = tmp_slice - output_cl + output_cl = output_cl + (delta * delta_2) + + cl.enqueue_copy(cl_queue, output_image, output_cl).wait() if mode == "std": output_image = np.sqrt(np.asarray(output_image) / image.shape[0]) diff --git a/src/nanopyx/core/transform/_le_esrrf3d_.cl b/src/nanopyx/core/transform/_le_esrrf3d_.cl index 47598eba..2f39bedd 100644 --- a/src/nanopyx/core/transform/_le_esrrf3d_.cl +++ b/src/nanopyx/core/transform/_le_esrrf3d_.cl @@ -1,4 +1,4 @@ -void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, +void _c_gradient_3d(__global float* image, __global float* imGc, __global float* imGr, __global float* imGs, int slices, int rows, int cols) { float ip0, ip1, ip2, ip3, ip4, ip5, ip6, ip7; @@ -137,3 +137,12 @@ float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIn return RGC; } + +__kernel void interpolate_3d() + + +__kernel void gradients_3d(__global float* image, __global float* imGc, __global float* imGr, __global float* imGs, int f, int slices, int rows, int cols) { + _c_gradient_3d(&image[f * slices * rows * cols], &imGc[f * slices * rows * cols], &imGr[f * slices * rows * cols], &imGs[f * slices * rows * cols], slices, rows, cols); +} + +__kernel void calculate_rgc3D() \ No newline at end of file diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl index 833c6d6b..746b34ef 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.cl @@ -117,8 +117,8 @@ float _c_calculate_rgc(int xM, int yM, __global float* imIntGx, __global float* // gradient image dimensions int nPixels_grad = nRows*Gx_Gy_MAGNIFICATION * nCols*Gx_Gy_MAGNIFICATION; - row = row + fwhm*2; - col = col + fwhm*2; + row = row + fwhm*2*magnification; + col = col + fwhm*2*magnification; if (doIntensityWeighting == 1) { image_out[f * nPixels_out + row * nCols + col] = _c_calculate_rgc(col, row, &imIntGx[f * nPixels_grad], &imIntGy[f * nPixels_grad], nCols, nRows, magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, sensitivity, offset, xyoffset, angle) * imInt[f * nPixels_out + row * nCols + col]; diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx index a91edb9c..eb8221fb 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx @@ -25,7 +25,7 @@ class RadialGradientConvergence(LiquidEngine): verbose=verbose) - def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, grad_magnification: int = 2, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): + def run(self, gradient_col_interp, gradient_row_interp, image_interp, magnification: int = 5, grad_magnification: int = 1, radius: float = 1.5, sensitivity: float = 1 , doIntensityWeighting: bool = True, offset: float = 0, xyoffset: float = 0, angle: float = 0, run_type = None): gradient_col_interp = check_image(gradient_col_interp) gradient_row_interp = check_image(gradient_row_interp) image_interp = check_image(image_interp) @@ -38,14 +38,14 @@ class RadialGradientConvergence(LiquidEngine): image_interp = check_image(image_interp) return super().benchmark(gradient_col_interp, gradient_row_interp, image_interp, magnification, grad_magnification, radius, sensitivity, doIntensityWeighting, offset, xyoffset, angle) - def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): + def _run_unthreaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=1, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @cython """ cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(fwhm*2) + cdef int margin = int(fwhm*2) * magnification cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -71,7 +71,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): + def _run_threaded(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=1, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -79,7 +79,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(fwhm*2) + cdef int margin = int(fwhm*2) * magnification cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -93,6 +93,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float [:,:,:] rgc_map = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) + print("Here 2") cdef int f, rM, cM with nogil: for f in range(nFrames): @@ -103,7 +104,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): + def _run_threaded_guided(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=1, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -111,7 +112,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(fwhm*2) + cdef int margin = int(fwhm*2) * magnification cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -125,6 +126,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float [:,:,:] rgc_map = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) + print("Here 2") cdef int f, rM, cM with nogil: for f in range(nFrames): @@ -135,7 +137,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): + def _run_threaded_dynamic(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=1, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -143,7 +145,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(fwhm*2) + cdef int margin = int(fwhm*2) * magnification cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -157,6 +159,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float [:,:,:] rgc_map = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) + print("Here 2") cdef int f, rM, cM with nogil: for f in range(nFrames): @@ -167,7 +170,7 @@ class RadialGradientConvergence(LiquidEngine): else: rgc_map[f, rM, cM] = _c_calculate_rgc(cM, rM, &gradient_col_interp[f,0,0], &gradient_row_interp[f,0,0], colsM, rowsM, _magnification, Gx_Gy_MAGNIFICATION, fwhm, tSO, tSS, _sensitivity, offset, xyoffset, angle) return np.asarray(rgc_map,dtype=np.float32) - def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): + def _run_threaded_static(self, float[:,:,:] gradient_col_interp, float[:,:,:] gradient_row_interp, float[:,:,:] image_interp, magnification=5, grad_magnification=1, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset: float =0, xyoffset: float =0, angle: float =0): """ @cpu @threaded @@ -175,7 +178,7 @@ class RadialGradientConvergence(LiquidEngine): """ cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(fwhm*2) + cdef int margin = int(fwhm*2) * magnification cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification @@ -189,6 +192,7 @@ class RadialGradientConvergence(LiquidEngine): cdef float [:,:,:] rgc_map = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) + print("Here 2") cdef int f, rM, cM with nogil: for f in range(nFrames): @@ -201,7 +205,7 @@ class RadialGradientConvergence(LiquidEngine): return np.asarray(rgc_map,dtype=np.float32) - def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, grad_magnification=2, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): + def _run_opencl(self, gradient_col_interp, gradient_row_interp, image_interp, magnification=5, grad_magnification=1, radius=1.5, sensitivity=1, doIntensityWeighting=True, offset=0, xyoffset=0, angle=0, device=None, int mem_div=1): """ @gpu """ @@ -219,7 +223,7 @@ class RadialGradientConvergence(LiquidEngine): # Parameters cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(fwhm*2) + cdef int margin = int(fwhm*2) * magnification cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 cdef float Gx_Gy_MAGNIFICATION = grad_magnification From 33d1cf20830741ec1f0f198f7671749ccf66cc6c Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Wed, 9 Apr 2025 11:57:58 +0100 Subject: [PATCH 32/37] reverting 3d changes --- .../_c_sr_radial_gradient_convergence.c | 39 ++++++++----------- .../_c_sr_radial_gradient_convergence.h | 3 +- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/include/_c_sr_radial_gradient_convergence.c b/src/include/_c_sr_radial_gradient_convergence.c index 101ed0f2..5f6850c3 100644 --- a/src/include/_c_sr_radial_gradient_convergence.c +++ b/src/include/_c_sr_radial_gradient_convergence.c @@ -121,37 +121,37 @@ float _c_get_bound_value(float* im, int slices, int rows, int cols, int s, int r return im[_s * rows * cols + _r * cols + _c]; } -float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) { +float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) { float vx, vy, vz, Gx, Gy, Gz, dx, dy, dz, dz_real, distance, distance_xy, distance_z, distanceWeight, distanceWeight_xy, distanceWeight_z, GdotR, Dk; - float xc = (xM) / magnification_xy; - float yc = (yM) / magnification_xy; - float zc = (sliceM) / magnification_z; + float xc = (xM + 0.5) / magnification_xy; + float yc = (yM + 0.5) / magnification_xy; + float zc = (sliceM + 0.5) / magnification_z; float RGC = 0; float distanceWeightSum = 0; float distanceWeightSum_xy = 0; float distanceWeightSum_z = 0; - int _start = -(int)(2 * fwhm); - int _end = (int)(2 * fwhm + 1); + int _start = -(int)(Gx_Gy_MAGNIFICATION * fwhm); + int _end = (int)(Gx_Gy_MAGNIFICATION * fwhm + 1); - int _start_z = -(int)(2 * fwhm); - int _end_z = (int)(2 * fwhm + 1); + int _start_z = -(int)(Gz_MAGNIFICATION * fwhm_z); + int _end_z = (int)(Gz_MAGNIFICATION * fwhm_z + 1); for (int j = _start; j <= _end; j++) { - vy = yc + j; + vy = ((float) ((int) (Gx_Gy_MAGNIFICATION*yc)) + j)/(float) Gx_Gy_MAGNIFICATION; - if (0 < vy && vy < rowsM/magnification_xy - 1) { + if (0 < vy && vy < rowsM - 1) { for (int i = _start; i <= _end; i++) { - vx = xc + i; + vx = ((float) ((int) (Gx_Gy_MAGNIFICATION*xc)) + i)/(float) Gx_Gy_MAGNIFICATION; - if (0 < vx && vx < colsM/magnification_xy - 1) { + if (0 < vx && vx < colsM - 1) { for (int k = _start_z; k <= _end_z; k++) { - vz = zc + k; + vz = ((float) ((int) (Gz_MAGNIFICATION*zc)) + k)/(float) Gz_MAGNIFICATION; - if (0 < vz && vz < slicesM/magnification_z - 1) { + if (0 < vz && vz < slicesM - 1) { dx = vx - xc; dy = vy - yc; dz = vz - zc; @@ -161,13 +161,9 @@ float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIn distance_z = dz_real; if (distance != 0 && distance_xy <= tSO && distance_z <= tSO_z) { - int linear_index = (int)(vz * magnification_z) * rowsM * colsM + - (int)(magnification_xy * vy) * colsM + - (int)(magnification_xy * vx); - - Gx = imIntGx[linear_index]; - Gy = imIntGy[linear_index]; - Gz = imIntGz[linear_index]; + Gx = _c_get_bound_value(imIntGx, slicesM*Gz_MAGNIFICATION, rowsM*Gx_Gy_MAGNIFICATION, colsM*Gx_Gy_MAGNIFICATION, vz * magnification_z * Gz_MAGNIFICATION, magnification_xy * Gx_Gy_MAGNIFICATION * vy, magnification_xy * Gx_Gy_MAGNIFICATION * vx); + Gy = _c_get_bound_value(imIntGy, slicesM*Gz_MAGNIFICATION, rowsM*Gx_Gy_MAGNIFICATION, colsM*Gx_Gy_MAGNIFICATION, vz * magnification_z * Gz_MAGNIFICATION, magnification_xy * Gx_Gy_MAGNIFICATION * vy, magnification_xy * Gx_Gy_MAGNIFICATION * vx); + Gz = _c_get_bound_value(imIntGz, slicesM*Gz_MAGNIFICATION, rowsM*Gx_Gy_MAGNIFICATION, colsM*Gx_Gy_MAGNIFICATION, vz * magnification_z * Gz_MAGNIFICATION, magnification_xy * Gx_Gy_MAGNIFICATION * vy, magnification_xy * Gx_Gy_MAGNIFICATION * vx); // distanceWeight = _c_calculate_dw3D_isotropic(distance, tSS); distanceWeight = _c_calculate_dw3D(distance, distance_xy, distance_z, tSS, tSS_z); @@ -201,7 +197,6 @@ float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIn } return RGC; - //return imIntGy[(int)(zc * rowsM * colsM) + (int)(yc * colsM) + (int)(xc)]; } float _c_calculate_dk_3d(float dx, float dy, float dz, float Gx, float Gy, float Gz, float Gmag, float distance) { diff --git a/src/include/_c_sr_radial_gradient_convergence.h b/src/include/_c_sr_radial_gradient_convergence.h index 4d89e987..60d48153 100644 --- a/src/include/_c_sr_radial_gradient_convergence.h +++ b/src/include/_c_sr_radial_gradient_convergence.h @@ -21,7 +21,8 @@ double _c_calculate_dw_z(double distance_z, double tSS_z); double _c_calculate_dk3D(float Gx, float Gy, float Gz, float dx, float dy, float dz, float distance); -float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity); +float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity); + float _c_calculate_rgc_3d(int cM, int rM, int sM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float Gx_Gy_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSS, float sensitivity); From fe7c80f2d924a1f34f11e00e9942028561b1e2ea Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Wed, 9 Apr 2025 11:59:09 +0100 Subject: [PATCH 33/37] reverting 3d changes --- .../nanopyx.core.transform._le_esrrf3d.pyx | 563 +++++++++++------- 1 file changed, 336 insertions(+), 227 deletions(-) diff --git a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx index 35692613..78cb801c 100644 --- a/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx +++ b/src/mako_templates/nanopyx.core.transform._le_esrrf3d.pyx @@ -1,9 +1,6 @@ -<%! -schedulers = ['threaded','threaded_guided','threaded_dynamic','threaded_static', 'unthreaded'] -%># cython: infer_types=True, wraparound=True, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False +# cython: infer_types=True, wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False import numpy as np import math -import time cimport numpy as np from libc.math cimport floor @@ -15,13 +12,12 @@ from .sr_temporal_correlations import calculate_eSRRF_temporal_correlations from ._interpolation import interpolate_3d, interpolate_3d_zlinear from ._le_interpolation_catmull_rom import ShiftAndMagnify from ...__liquid_engine__ import LiquidEngine -from ...__opencl__ import cl, cl_array, _fastest_device cdef extern from "_c_gradients.h": void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, int rows, int cols) nogil cdef extern from "_c_sr_radial_gradient_convergence.h": - float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float voxel_ratio, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil + float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float PSF_voxel_ratio, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil class eSRRF3D(LiquidEngine): """ @@ -31,266 +27,379 @@ class eSRRF3D(LiquidEngine): def __init__(self, clear_benchmarks=False, testing=False, verbose=True): self._designation = "eSRRF_3D" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - - def run(self, image, magnification_xy: int = 2, magnification_z: int = 2, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, run_type=None): + self._gradients_s_interpolated = None + self._gradients_r_interpolated = None + self._gradients_c_interpolated = None + self.keep_gradients = False + self.keep_interpolated = False + self._img_interpolated = None + + def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, keep_gradients=False, keep_interpolated = False, run_type=None): + self.keep_gradients = keep_gradients + self.keep_interpolated = keep_interpolated # TODO: complete and check _run inputs, need to complete variables? if image.dtype != np.float32: image = image.astype(np.float32) if len(image.shape) == 4: - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) elif len(image.shape) == 3: image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): if image.dtype != np.float32: image = image.astype(np.float32) if len(image.shape) == 4: - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio,sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio,sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) elif len(image.shape) == 3: image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) - % for sch in schedulers: - def _run_${sch}(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def _run_threaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @cpu - % if sch!='unthreaded': @threaded - % endif @cython """ + cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef int _doIntensityWeighting = doIntensityWeighting + + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) + + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof + + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) - time_start = time.time() - # calculate all constants + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + with nogil: + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) + + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) + + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() + + with nogil: + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag): + for cM in range(0, n_cols_mag): + if _doIntensityWeighting: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) + def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): + """ + @cpu + @threaded + @cython + """ cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - n_slices_mag = n_slices * _magnification_z - n_rows_mag = n_rows * _magnification_xy - n_cols_mag = n_cols * _magnification_xy + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) + + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof - # create all necessary arrays - cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] rgc_std - if mode == "std": - rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val - cdef int f_i, sM, rM, cM - - for f_i in range(n_frames): - # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) + + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) with nogil: - # calculate gradients - _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) + + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - # interpolate gradients - gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() with nogil: - for sM in range(margin, n_slices_mag-margin): - % if sch=="unthreaded": - for rM in range(margin, n_rows_mag-margin): - % elif sch=="threaded": - for rM in prange(margin, n_rows_mag-margin): - % else: - for rM in prange(margin, n_rows_mag-margin, schedule="${sch.split('_')[1]}"): - % endif - for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag, schedule="guided"): + for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = rgc_val * image_interpolated[sM, rM, cM] - if mode == "average": - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) - elif mode == "std": - delta = rgc_val - rgc_avg[sM, rM, cM] - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) - delta_2 = rgc_val - rgc_avg[sM, rM, cM] - rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) - if mode == "std": - rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) - return rgc_std - else: - return np.asarray(rgc_avg) - % endfor + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) + def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): + """ + @cpu + @threaded + @cython + """ + cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef int _doIntensityWeighting = doIntensityWeighting + + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) + + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof + + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) + + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + with nogil: + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) + + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - def _run_opencl(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, device=None, mem_div=1): + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() + + with nogil: + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag, schedule="dynamic"): + for cM in range(0, n_cols_mag): + if _doIntensityWeighting: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) + def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ - @gpu + @cpu + @threaded + @cython """ + cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef int _doIntensityWeighting = doIntensityWeighting - cl_ctx = cl.Context([device['device']]) - dc = device['device'] - cl_queue = cl.CommandQueue(cl_ctx) - - output_shape = (image.shape[1] * magnification_z, image.shape[2] * magnification_xy, image.shape[3] * magnification_xy) - - output_image = np.zeros(output_shape, dtype=np.float32) - tmp_slice = np.zeros(output_shape, dtype=np.float32) - - #max_slices = int((dc.global_mem_size - (3 * (image[0, :, :, :].nbytes) + 4 * (output_image.nbytes)) // (image[0, :, :, :].nbytes)) // mem_div) - #max_slices = self._check_max_slices(image, max_slices) - - max_slices = 1 - - mf = cl.mem_flags - - # create cl buffer for input image - input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[:, :, :, :].nbytes, dc, max_slices)) - cl.enqueue_copy(cl_queue, input_cl, image[:,:,:, :]).wait() - - # create magnified input image buffer - magnified_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - - # create gradients buffers - col_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) - row_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) - z_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) - - # create magnified gradients buffers - col_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - row_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - z_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - - # create the output buffer - output_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - - esrrf3d_cl_code = self._get_cl_code("esrrf3d.cl", device["DP"]) - esrrf3d_cl_prg = cl.Program(cl_ctx, esrrf3d_cl_code).build(options=["-cl-fast-relaxed-math", "-cl-mad-enable"]) - interp_knl = esrrf3d_cl_prg.interpolate_3d - grads_knl = esrrf3d_cl_prg.gradients_3d - rgc_knl = esrrf3d_cl_prg.calculate_rgc3D - - margin = int(radius*2) - lowest_row = margin # TODO discuss edges calculation - highest_row = output_shape[1] - margin - lowest_col = margin - highest_col = output_shape[2] - margin - - for f in range(image.shape[0]): - - interp_knl( - cl_queue, - (image.shape[1], image.shape[2], image.shape[3]), - None, - input_cl, - magnified_cl, - np.int32(magnification_xy), - np.int32(magnification_z), - np.int32(f) - ).wait() - - - grads_knl( - cl_queue, - (image.shape[1], image.shape[2], image.shape[3]), - None, - input_cl, - col_gradients_cl, - row_gradients_cl, - z_gradients_cl, - np.int32(f), - np.int32(image.shape[1]), - np.int32(image.shape[2]), - np.int32(image.shape[3]), - ).wait() - - interp_knl( - cl_queue, - (output_shape[0], output_shape[1], output_shape[2]), - None, - col_gradients_cl, - col_magnified_gradients_cl, - np.int32(magnification_xy), - np.int32(magnification_z), - np.int32(f) - ).wait() - - interp_knl( - cl_queue, - (output_shape[0], output_shape[1], output_shape[2]), - None, - row_gradients_cl, - row_magnified_gradients_cl, - np.int32(magnification_xy), - np.int32(magnification_z), - np.int32(f) - ).wait() - - interp_knl( - cl_queue, - (output_shape[0], output_shape[1], output_shape[2]), - None, - z_gradients_cl, - z_magnified_gradients_cl, - np.int32(magnification_xy), - np.int32(magnification_z), - np.int32(f) - ).wait() - - rgc_knl( - cl_queue, - (output_shape[0], output_shape[1], output_shape[2]), - None, - col_magnified_gradients_cl, - row_magnified_gradients_cl, - z_magnified_gradients_cl, - magnified_cl, - tmp_slice, - np.int32(output_shape[1]), - np.int32(output_shape[2]), - np.int32(output_shape[0]), - np.int32(magnification_xy), - np.int32(magnification_z), - np.float32(voxel_ratio), - np.float32(radius), - np.float32(2 * (radius/2.355) + 1), - np.float32(2 * (radius*voxel_ratio/2.355) + 1), - np.float32(2 * (radius/2.355) * (radius/2.355)), - np.float32(2 * (radius*voxel_ratio/2.355) * (radius*voxel_ratio/2.355)), - np.int32(sensitivity), - np.int32(doIntensityWeighting) - - ).wait() - - - # TODO change tmp_slice to cl arrays - if mode == "average": - output_cl = output_cl + (tmp_slice - output_cl) / (f + 1) - elif mode == "std": - delta = tmp_slice - output_cl - output_cl = output_cl + (delta) / (f + 1) - delta_2 = tmp_slice - output_cl - output_cl = output_cl + (delta * delta_2) - - - cl.enqueue_copy(cl_queue, output_image, output_cl).wait() - - if mode == "std": - output_image = np.sqrt(np.asarray(output_image) / image.shape[0]) - return output_image + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) + + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof + + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) + + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + with nogil: + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) + + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) + + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() + + with nogil: + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag, schedule="static"): + for cM in range(0, n_cols_mag): + if _doIntensityWeighting: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) + def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): + """ + @cpu + @cython + """ + cdef float sigma = radius / 2.355 + cdef float fwhm = radius + cdef float tSS = 2 * sigma * sigma + cdef float tSO = 2 * sigma + 1 + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio + cdef float tSS_z = 2 * sigma_z * sigma_z + cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 + cdef int _magnification_xy = magnification_xy + cdef int _magnification_z = magnification_z + cdef int _doIntensityWeighting = doIntensityWeighting + + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum + n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) + + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof + + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) + + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + with nogil: + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) + + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) + + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() + + with nogil: + for sM in range(0, n_slices_mag): + for rM in range(0, n_rows_mag): + for cM in range(0, n_cols_mag): + if _doIntensityWeighting: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) + + def get_gradients(self): + if self._gradients_c_interpolated is None or self._gradients_r_interpolated is None or self._gradients_s_interpolated is None: + print("Gradients not yet calculated") else: - return np.asarray(output_image) \ No newline at end of file + return self._gradients_c_interpolated, self._gradients_r_interpolated, self._gradients_s_interpolated + + def get_interpolated_image(self): + return self._img_interpolated \ No newline at end of file From d5560f48fbd23ecf0c643725153e98bef2a199b9 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Wed, 9 Apr 2025 12:03:15 +0100 Subject: [PATCH 34/37] reverting 3d changes --- src/nanopyx/methods/esrrf_3d/run.py | 52 +++++++++++++---------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/src/nanopyx/methods/esrrf_3d/run.py b/src/nanopyx/methods/esrrf_3d/run.py index 0d3dd4b3..4ac718b7 100644 --- a/src/nanopyx/methods/esrrf_3d/run.py +++ b/src/nanopyx/methods/esrrf_3d/run.py @@ -5,37 +5,33 @@ def run_esrrf3d( - img, - mode="average", - magnification_xy=2, - magnification_z=2, - radius=1.5, - sensitivity=1, - voxel_ratio=4, - doIntensityWeighting=True, - **kwargs, + img, correlation="AVG", framewindow=5, rollingoverlap=2, **kwargs ): """ - Calculate the eSRRF3D temporal correlations for the given 3D image. + Calculate the eSRRF3D temporal correlations for the given image. - img (ndarray): The input 4D image. - mode (str, optional): The mode of time projection, default is "average", other option is "std". - magnification_xy (float, optional): The magnification factor for the x and y axes. Default is 2. - magnification_z (float, optional): The magnification factor for the z axis. Default is 2. - radius (float, optional): The radius for the xy plane. Default is 1.5. - sensitivity (float, optional): The sensitivity for the calculation. Default is 1. - voxel_ratio (float, optional): The ratio of voxel dimensions (z to xy). Default is 4. - doIntensityWeighting (bool, optional): Whether to perform intensity weighting. Default is True. - **kwargs: Additional parameters for the eSRRF3D calculation, including: - - radius_z (float, optional): The radius for the z axis. - - run_type (str, optional): The type of the run. - - keep_gradients (bool, optional): Whether to keep the gradients. - - keep_interpolated (bool, optional): Whether to keep the interpolated values. - - correlation (str, optional): The type of correlation to use. - - framewindow (int, optional): The window size for frames. - - rollingoverlap (int, optional): The overlap size for rolling. + Args: + img: The input 3D image. + magnification_xy: The magnification factor for the x and y axes. + magnification_z: The magnification factor for the z axis. + radius: The radius for the xy plane. + radius_z: The radius for the z axis. + sensitivity: The sensitivity for the calculation. + doIntensityWeighting: Whether to perform intensity weighting. + run_type: The type of the run. + keep_gradients: Whether to keep the gradients. + keep_interpolated: Whether to keep the interpolated values. + correlation: The type of correlation to use. + framewindow: The window size for frame. + rollingoverlap: The overlap size for rolling. - ndarray: The calculated eSRRF3D temporal correlations. + Returns: + The calculated eSRRF3D temporal correlations. """ esrrf_calculator = eSRRF3D() - return esrrf_calculator.run(img, **kwargs) + return calculate_eSRRF3d_temporal_correlations( + esrrf_calculator.run(img, **kwargs), + correlation=correlation, + framewindow=framewindow, + rollingoverlap=rollingoverlap, + ) From 5e75f976dc30f428c7de3f91a77b8cc23cf3cd5f Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Wed, 9 Apr 2025 12:03:28 +0100 Subject: [PATCH 35/37] removing prints --- .../core/transform/_le_radial_gradient_convergence.pyx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx index eb8221fb..f12e82d3 100644 --- a/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx +++ b/src/nanopyx/core/transform/_le_radial_gradient_convergence.pyx @@ -93,7 +93,6 @@ class RadialGradientConvergence(LiquidEngine): cdef float [:,:,:] rgc_map = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) - print("Here 2") cdef int f, rM, cM with nogil: for f in range(nFrames): @@ -126,7 +125,6 @@ class RadialGradientConvergence(LiquidEngine): cdef float [:,:,:] rgc_map = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) - print("Here 2") cdef int f, rM, cM with nogil: for f in range(nFrames): @@ -159,7 +157,6 @@ class RadialGradientConvergence(LiquidEngine): cdef float [:,:,:] rgc_map = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) - print("Here 2") cdef int f, rM, cM with nogil: for f in range(nFrames): @@ -192,7 +189,6 @@ class RadialGradientConvergence(LiquidEngine): cdef float [:,:,:] rgc_map = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) - print("Here 2") cdef int f, rM, cM with nogil: for f in range(nFrames): From 5f6267268652a6b0dc2be6ddd814f7b84c563f16 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Wed, 9 Apr 2025 12:04:24 +0100 Subject: [PATCH 36/37] reverting 3d changes --- src/nanopyx/core/transform/_le_esrrf3d.pyx | 676 ++++++++------------- 1 file changed, 249 insertions(+), 427 deletions(-) diff --git a/src/nanopyx/core/transform/_le_esrrf3d.pyx b/src/nanopyx/core/transform/_le_esrrf3d.pyx index 1feeff9a..78cb801c 100644 --- a/src/nanopyx/core/transform/_le_esrrf3d.pyx +++ b/src/nanopyx/core/transform/_le_esrrf3d.pyx @@ -1,7 +1,6 @@ -# cython: infer_types=True, wraparound=True, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False +# cython: infer_types=True, wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3, profile=False, autogen_pxd=False import numpy as np import math -import time cimport numpy as np from libc.math cimport floor @@ -13,13 +12,12 @@ from .sr_temporal_correlations import calculate_eSRRF_temporal_correlations from ._interpolation import interpolate_3d, interpolate_3d_zlinear from ._le_interpolation_catmull_rom import ShiftAndMagnify from ...__liquid_engine__ import LiquidEngine -from ...__opencl__ import cl, cl_array, _fastest_device cdef extern from "_c_gradients.h": void _c_gradient_3d(float* image, float* imGc, float* imGr, float* imGs, int slices, int rows, int cols) nogil cdef extern from "_c_sr_radial_gradient_convergence.h": - float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float voxel_ratio, float fwhm, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil + float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float PSF_voxel_ratio, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) nogil class eSRRF3D(LiquidEngine): """ @@ -29,555 +27,379 @@ class eSRRF3D(LiquidEngine): def __init__(self, clear_benchmarks=False, testing=False, verbose=True): self._designation = "eSRRF_3D" super().__init__(clear_benchmarks=clear_benchmarks, testing=testing, verbose=verbose) - - def run(self, image, magnification_xy: int = 2, magnification_z: int = 2, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, run_type=None): + self._gradients_s_interpolated = None + self._gradients_r_interpolated = None + self._gradients_c_interpolated = None + self.keep_gradients = False + self.keep_interpolated = False + self._img_interpolated = None + + def run(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True, keep_gradients=False, keep_interpolated = False, run_type=None): + self.keep_gradients = keep_gradients + self.keep_interpolated = keep_interpolated # TODO: complete and check _run inputs, need to complete variables? if image.dtype != np.float32: image = image.astype(np.float32) if len(image.shape) == 4: - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) elif len(image.shape) == 3: image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting, run_type=run_type) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting, run_type=run_type) - def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def benchmark(self, image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): if image.dtype != np.float32: image = image.astype(np.float32) if len(image.shape) == 4: - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio,sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) + return self._run(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio,sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) elif len(image.shape) == 3: image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) - return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, voxel_ratio=voxel_ratio, sensitivity=sensitivity, mode=mode, doIntensityWeighting=doIntensityWeighting) + return super().benchmark(image, magnification_xy=magnification_xy, magnification_z=magnification_z, radius=radius, PSF_voxel_ratio=PSF_voxel_ratio, sensitivity=sensitivity, doIntensityWeighting=doIntensityWeighting) - def _run_threaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + def _run_threaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @cpu @threaded @cython """ - - time_start = time.time() - # calculate all constants cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - n_slices_mag = n_slices * _magnification_z - n_rows_mag = n_rows * _magnification_xy - n_cols_mag = n_cols * _magnification_xy + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) + + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof - # create all necessary arrays - cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] rgc_std - if mode == "std": - rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val - cdef int f_i, sM, rM, cM - - for f_i in range(n_frames): - # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) + + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) with nogil: - # calculate gradients - _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) - # interpolate gradients - gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) + + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in prange(margin, n_rows_mag-margin): - for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag): + for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = rgc_val * image_interpolated[sM, rM, cM] - if mode == "average": - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) - elif mode == "std": - delta = rgc_val - rgc_avg[sM, rM, cM] - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) - delta_2 = rgc_val - rgc_avg[sM, rM, cM] - rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) - if mode == "std": - rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) - return rgc_std - else: - return np.asarray(rgc_avg) - def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) + def _run_threaded_guided(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @cpu @threaded @cython """ - - time_start = time.time() - # calculate all constants cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - n_slices_mag = n_slices * _magnification_z - n_rows_mag = n_rows * _magnification_xy - n_cols_mag = n_cols * _magnification_xy + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) + + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof - # create all necessary arrays - cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] rgc_std - if mode == "std": - rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val - cdef int f_i, sM, rM, cM - - for f_i in range(n_frames): - # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) + + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) with nogil: - # calculate gradients - _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) - # interpolate gradients - gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) + + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in prange(margin, n_rows_mag-margin, schedule="guided"): - for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag, schedule="guided"): + for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = rgc_val * image_interpolated[sM, rM, cM] - if mode == "average": - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) - elif mode == "std": - delta = rgc_val - rgc_avg[sM, rM, cM] - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) - delta_2 = rgc_val - rgc_avg[sM, rM, cM] - rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) - if mode == "std": - rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) - return rgc_std - else: - return np.asarray(rgc_avg) - def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) + def _run_threaded_dynamic(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @cpu @threaded @cython """ - - time_start = time.time() - # calculate all constants cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - n_slices_mag = n_slices * _magnification_z - n_rows_mag = n_rows * _magnification_xy - n_cols_mag = n_cols * _magnification_xy + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) - # create all necessary arrays - cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] rgc_std - if mode == "std": - rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val - cdef int f_i, sM, rM, cM - - for f_i in range(n_frames): - # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof + + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) + + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) with nogil: - # calculate gradients - _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) - # interpolate gradients - gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) + + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in prange(margin, n_rows_mag-margin, schedule="dynamic"): - for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag, schedule="dynamic"): + for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = rgc_val * image_interpolated[sM, rM, cM] - if mode == "average": - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) - elif mode == "std": - delta = rgc_val - rgc_avg[sM, rM, cM] - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) - delta_2 = rgc_val - rgc_avg[sM, rM, cM] - rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) - if mode == "std": - rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) - return rgc_std - else: - return np.asarray(rgc_avg) - def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) + def _run_threaded_static(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @cpu @threaded @cython """ - - time_start = time.time() - # calculate all constants cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - n_slices_mag = n_slices * _magnification_z - n_rows_mag = n_rows * _magnification_xy - n_cols_mag = n_cols * _magnification_xy + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) - # create all necessary arrays - cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] rgc_std - if mode == "std": - rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val - cdef int f_i, sM, rM, cM - - for f_i in range(n_frames): - # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof + + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) + + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) with nogil: - # calculate gradients - _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) + + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - # interpolate gradients - gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in prange(margin, n_rows_mag-margin, schedule="static"): - for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(0, n_slices_mag): + for rM in prange(0, n_rows_mag, schedule="static"): + for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = rgc_val * image_interpolated[sM, rM, cM] - if mode == "average": - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) - elif mode == "std": - delta = rgc_val - rgc_avg[sM, rM, cM] - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) - delta_2 = rgc_val - rgc_avg[sM, rM, cM] - rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) - if mode == "std": - rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) - return rgc_std - else: - return np.asarray(rgc_avg) - def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True): + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) + def _run_unthreaded(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, PSF_voxel_ratio: float = 4.0, sensitivity: float = 1, doIntensityWeighting: bool = True): """ @cpu @cython """ - - time_start = time.time() - # calculate all constants cdef float sigma = radius / 2.355 cdef float fwhm = radius - cdef int margin = int(2 * radius) cdef float tSS = 2 * sigma * sigma cdef float tSO = 2 * sigma + 1 - cdef float sigma_z = radius * voxel_ratio / 2.355 # Taking voxel size into account + cdef float sigma_z = radius * PSF_voxel_ratio / 2.355 # Taking voxel size into account + cdef float fwhm_z = radius * PSF_voxel_ratio cdef float tSS_z = 2 * sigma_z * sigma_z cdef float tSO_z = 2 * sigma_z + 1 + cdef int Gx_Gy_MAGNIFICATION = 2 cdef int _magnification_xy = magnification_xy cdef int _magnification_z = magnification_z - cdef float _voxel_ratio = voxel_ratio cdef int _doIntensityWeighting = doIntensityWeighting - cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag, n_rows_mag, n_cols_mag + cdef int n_frames, n_slices, n_rows, n_cols, n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum n_frames, n_slices, n_rows, n_cols = image.shape[0], image.shape[1], image.shape[2], image.shape[3] - n_slices_mag = n_slices * _magnification_z - n_rows_mag = n_rows * _magnification_xy - n_cols_mag = n_cols * _magnification_xy + + cdef float[:, :, :, :] rgc_map = np.zeros((n_frames, n_slices*magnification_z, n_rows*magnification_xy, n_cols*magnification_xy), dtype=np.float32) + + cdef float[:, :, :] image_interpolated, gradients_s, gradients_r, gradients_c, gradients_s_interpolated, gradients_r_interpolated, gradients_c_interpolated, padded, img_dum + + cdef int f, n_slices_mag, n_rows_mag, n_cols_mag, sM, rM, cM, z0 + + cdef float rgc_val, zcof - # create all necessary arrays - cdef float[:, :, :] rgc_avg = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] rgc_std - if mode == "std": - rgc_std = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] image_interpolated = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_col = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_row = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_slices = np.zeros((n_slices, n_rows, n_cols), dtype=np.float32) - cdef float[:, :, :] gradients_col_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_row_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float[:, :, :] gradients_slices_mag = np.zeros((n_slices_mag, n_rows_mag, n_cols_mag), dtype=np.float32) - cdef float delta, delta_2, rgc_val - cdef int f_i, sM, rM, cM - - for f_i in range(n_frames): - # interpolate frame - image_interpolated = interpolate_3d_zlinear(image[f_i,:,:,:], _magnification_xy, _magnification_z) + for f in range(n_frames): + image_interpolated = interpolate_3d_zlinear(image[f,:,:,:], _magnification_xy, _magnification_z) + + n_slices_mag, n_rows_mag, n_cols_mag = image_interpolated.shape[0], image_interpolated.shape[1], image_interpolated.shape[2] + img_dum = interpolate_3d_zlinear(image[f], Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION) + n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum = img_dum.shape[0], img_dum.shape[1], img_dum.shape[2] + + gradients_c = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_r = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) + gradients_s = np.zeros((n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum), dtype=np.float32) with nogil: - # calculate gradients - _c_gradient_3d(&image[f_i, 0, 0, 0], &gradients_col[0, 0, 0], &gradients_row[0, 0, 0], &gradients_slices[0, 0, 0], n_slices, n_rows, n_cols) + _c_gradient_3d(&img_dum[0, 0, 0], &gradients_c[0, 0, 0], &gradients_r[0, 0, 0], &gradients_s[0, 0, 0], n_slices_mag_dum, n_rows_mag_dum, n_cols_mag_dum) + + gradients_s_interpolated = interpolate_3d_zlinear(gradients_s, _magnification_xy, _magnification_z) + gradients_r_interpolated = interpolate_3d_zlinear(gradients_r, _magnification_xy, _magnification_z) + gradients_c_interpolated = interpolate_3d_zlinear(gradients_c, _magnification_xy, _magnification_z) - # interpolate gradients - gradients_slices_mag = interpolate_3d_zlinear(gradients_slices, _magnification_xy, _magnification_z) - gradients_row_mag = interpolate_3d_zlinear(gradients_row, _magnification_xy, _magnification_z) - gradients_col_mag = interpolate_3d_zlinear(gradients_col, _magnification_xy, _magnification_z) + if self.keep_gradients: + self._gradients_s_interpolated = gradients_s_interpolated.copy() + self._gradients_r_interpolated = gradients_r_interpolated.copy() + self._gradients_c_interpolated = gradients_c_interpolated.copy() + + if self.keep_interpolated: + self._img_interpolated = img_dum.copy() with nogil: - for sM in range(margin, n_slices_mag-margin): - for rM in range(margin, n_rows_mag-margin): - for cM in range(margin, n_cols_mag-margin): - rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_col_mag[0,0,0], &gradients_row_mag[0,0,0], &gradients_slices_mag[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, _voxel_ratio, fwhm, tSO, tSO_z, tSS, tSS_z, sensitivity) + for sM in range(0, n_slices_mag): + for rM in range(0, n_rows_mag): + for cM in range(0, n_cols_mag): if _doIntensityWeighting: - rgc_val = rgc_val * image_interpolated[sM, rM, cM] - if mode == "average": - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (rgc_val - rgc_avg[sM, rM, cM]) / (f_i + 1) - elif mode == "std": - delta = rgc_val - rgc_avg[sM, rM, cM] - rgc_avg[sM, rM, cM] = rgc_avg[sM, rM, cM] + (delta) / (f_i + 1) - delta_2 = rgc_val - rgc_avg[sM, rM, cM] - rgc_std[sM, rM, cM] = rgc_std[sM, rM, cM] + (delta * delta_2) - if mode == "std": - rgc_std = np.sqrt(np.asarray(rgc_std) / n_frames) - return rgc_std - else: - return np.asarray(rgc_avg) - - def _run_opencl(self, float[:,:,:,:] image, magnification_xy: int = 5, magnification_z: int = 5, radius: float = 1.5, voxel_ratio: float = 4.0, sensitivity: float = 1, mode: str = "average", doIntensityWeighting: bool = True, device=None, mem_div=1): - """ - @gpu - """ + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val * image_interpolated[sM, rM, cM] + else: + rgc_val = _c_calculate_rgc3D(cM, rM, sM, &gradients_c_interpolated[0,0,0], &gradients_r_interpolated[0,0,0], &gradients_s_interpolated[0,0,0], n_cols_mag, n_rows_mag, n_slices_mag, _magnification_xy, _magnification_z, PSF_voxel_ratio, Gx_Gy_MAGNIFICATION, Gx_Gy_MAGNIFICATION, fwhm, fwhm_z, tSO, tSO_z, tSS, tSS_z, sensitivity) + rgc_map[f, sM, rM, cM] = rgc_val + + return np.asarray(rgc_map) - cl_ctx = cl.Context([device['device']]) - dc = device['device'] - cl_queue = cl.CommandQueue(cl_ctx) - - output_shape = (image.shape[1] * magnification_z, image.shape[2] * magnification_xy, image.shape[3] * magnification_xy) - - output_image = np.zeros(output_shape, dtype=np.float32) - tmp_slice = np.zeros(output_shape, dtype=np.float32) - - #max_slices = int((dc.global_mem_size - (3 * (image[0, :, :, :].nbytes) + 4 * (output_image.nbytes)) // (image[0, :, :, :].nbytes)) // mem_div) - #max_slices = self._check_max_slices(image, max_slices) - - max_slices = 1 - - mf = cl.mem_flags - - # create cl buffer for input image - input_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(image[:, :, :, :].nbytes, dc, max_slices)) - cl.enqueue_copy(cl_queue, input_cl, image[:,:,:, :]).wait() - - # create magnified input image buffer - magnified_cl = cl.Buffer(cl_ctx, mf.READ_ONLY, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - - # create gradients buffers - col_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) - row_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) - z_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(image[0, :, :, :].nbytes, dc, max_slices)) - - # create magnified gradients buffers - col_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - row_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - z_magnified_gradients_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - - # create the output buffer - output_cl = cl.Buffer(cl_ctx, mf.READ_WRITE, self._check_max_buffer_size(output_image.nbytes, dc, max_slices)) - - esrrf3d_cl_code = self._get_cl_code("esrrf3d.cl", device["DP"]) - esrrf3d_cl_prg = cl.Program(cl_ctx, esrrf3d_cl_code).build(options=["-cl-fast-relaxed-math", "-cl-mad-enable"]) - interp_knl = esrrf3d_cl_prg.interpolate_3d - grads_knl = esrrf3d_cl_prg.gradients_3d - rgc_knl = esrrf3d_cl_prg.calculate_rgc3D - - margin = int(radius*2) - lowest_row = margin # TODO discuss edges calculation - highest_row = output_shape[1] - margin - lowest_col = margin - highest_col = output_shape[2] - margin - - for f in range(image.shape[0]): - - interp_knl( - cl_queue, - (image.shape[1], image.shape[2], image.shape[3]), - None, - input_cl, - magnified_cl, - np.int32(magnification_xy), - np.int32(magnification_z), - np.int32(f) - ).wait() - - - grads_knl( - cl_queue, - (image.shape[1], image.shape[2], image.shape[3]), - None, - input_cl, - col_gradients_cl, - row_gradients_cl, - z_gradients_cl, - np.int32(f), - np.int32(image.shape[1]), - np.int32(image.shape[2]), - np.int32(image.shape[3]), - ).wait() - - interp_knl( - cl_queue, - (output_shape[0], output_shape[1], output_shape[2]), - None, - col_gradients_cl, - col_magnified_gradients_cl, - np.int32(magnification_xy), - np.int32(magnification_z), - np.int32(f) - ).wait() - - interp_knl( - cl_queue, - (output_shape[0], output_shape[1], output_shape[2]), - None, - row_gradients_cl, - row_magnified_gradients_cl, - np.int32(magnification_xy), - np.int32(magnification_z), - np.int32(f) - ).wait() - - interp_knl( - cl_queue, - (output_shape[0], output_shape[1], output_shape[2]), - None, - z_gradients_cl, - z_magnified_gradients_cl, - np.int32(magnification_xy), - np.int32(magnification_z), - np.int32(f) - ).wait() - - rgc_knl( - cl_queue, - (output_shape[0], output_shape[1], output_shape[2]), - None, - col_magnified_gradients_cl, - row_magnified_gradients_cl, - z_magnified_gradients_cl, - magnified_cl, - tmp_slice, - np.int32(output_shape[1]), - np.int32(output_shape[2]), - np.int32(output_shape[0]), - np.int32(magnification_xy), - np.int32(magnification_z), - np.float32(voxel_ratio), - np.float32(radius), - np.float32(2 * (radius/2.355) + 1), - np.float32(2 * (radius*voxel_ratio/2.355) + 1), - np.float32(2 * (radius/2.355) * (radius/2.355)), - np.float32(2 * (radius*voxel_ratio/2.355) * (radius*voxel_ratio/2.355)), - np.int32(sensitivity), - np.int32(doIntensityWeighting) - - ).wait() - - - # TODO change tmp_slice to cl arrays - if mode == "average": - output_cl = output_cl + (tmp_slice - output_cl) / (f + 1) - elif mode == "std": - delta = tmp_slice - output_cl - output_cl = output_cl + (delta) / (f + 1) - delta_2 = tmp_slice - output_cl - output_cl = output_cl + (delta * delta_2) - - - cl.enqueue_copy(cl_queue, output_image, output_cl).wait() - - if mode == "std": - output_image = np.sqrt(np.asarray(output_image) / image.shape[0]) - return output_image + def get_gradients(self): + if self._gradients_c_interpolated is None or self._gradients_r_interpolated is None or self._gradients_s_interpolated is None: + print("Gradients not yet calculated") else: - return np.asarray(output_image) \ No newline at end of file + return self._gradients_c_interpolated, self._gradients_r_interpolated, self._gradients_s_interpolated + + def get_interpolated_image(self): + return self._img_interpolated \ No newline at end of file From f2cb770e8ae46de33b49f09de19d70913b925625 Mon Sep 17 00:00:00 2001 From: Bruno Manuel Santos Saraiva Date: Wed, 9 Apr 2025 12:15:38 +0100 Subject: [PATCH 37/37] removing unwanted files --- src/nanopyx/core/transform/_le_esrrf3d_.cl | 148 --------------------- t.ipynb | 124 ----------------- 2 files changed, 272 deletions(-) delete mode 100644 src/nanopyx/core/transform/_le_esrrf3d_.cl delete mode 100644 t.ipynb diff --git a/src/nanopyx/core/transform/_le_esrrf3d_.cl b/src/nanopyx/core/transform/_le_esrrf3d_.cl deleted file mode 100644 index 2f39bedd..00000000 --- a/src/nanopyx/core/transform/_le_esrrf3d_.cl +++ /dev/null @@ -1,148 +0,0 @@ -void _c_gradient_3d(__global float* image, __global float* imGc, __global float* imGr, __global float* imGs, int slices, - int rows, int cols) { - float ip0, ip1, ip2, ip3, ip4, ip5, ip6, ip7; - - int z_i, y_i, x_i, z_1, y_1, x_1; - - for (z_i = 0; z_i < slices; z_i++) { - for (y_i = 0; y_i < rows; y_i++) { - for (x_i = 0; x_i < cols; x_i++) { - - z_1 = z_i >= slices - 1 ? slices - 1 : z_i + 1; - y_1 = y_i >= rows - 1 ? rows - 1 : y_i + 1; - x_1 = x_i >= cols - 1 ? cols - 1 : x_i + 1; - ip0 = image[z_i * rows * cols + y_i * cols + x_i]; - ip1 = image[z_i * rows * cols + y_i * cols + x_1]; - ip2 = image[z_i * rows * cols + y_1 * cols + x_i]; - ip3 = image[z_i * rows * cols + y_1 * cols + x_1]; - ip4 = image[z_1 * rows * cols + y_i * cols + x_i]; - ip5 = image[z_1 * rows * cols + y_i * cols + x_1]; - ip6 = image[z_1 * rows * cols + y_1 * cols + x_i]; - ip7 = image[z_1 * rows * cols + y_1 * cols + x_1]; - imGc[z_i * rows* cols + y_i * cols + x_i] = - (ip1 + ip3 + ip5 + ip7 - ip0 - ip2 - ip4 - ip6) / 4; - imGr[z_i * rows* cols + y_i * cols + x_i] = - (ip2 + ip3 + ip6 + ip7 - ip0 - ip1 - ip4 - ip5) / 4; - imGs[z_i * rows* cols + y_i * cols + x_i] = - (ip4 + ip5 + ip6 + ip7 - ip0 - ip1 - ip2 - ip3) / 4; - } - } - } -} - -double _c_calculate_dw3D(double distance, double distance_xy, double distance_z,double tSS, double tSS_z) { - float D_weight_xy, D_weight_z, D_weight; - D_weight_xy = (exp((-distance_xy * distance_xy) / tSS)); - D_weight_z = (exp((-distance_z * distance_z) / tSS_z)); - D_weight = distance * (D_weight_xy * D_weight_z); - return pow(D_weight, 4); -} - -double _c_calculate_dw_xy(double distance_xy, double tSS) { - return pow((distance_xy * exp((-distance_xy * distance_xy) / tSS)), 4); -} - -double _c_calculate_dw_z(double distance_z, double tSS_z) { - return pow((distance_z * exp((-distance_z * distance_z) / tSS_z)), 4); -} - -double _c_calculate_dk3D(float Gx, float Gy, float Gz, float dx, float dy, float dz, float distance) { - float Dk = sqrt((Gy * dz - Gz * dy) * (Gy * dz - Gz * dy) + (Gz * dx - Gx * dz) * (Gz * dx - Gx * dz) + (Gx * dy - Gy * dx) * (Gx * dy - Gy * dx)) / sqrt(Gx * Gx + Gy * Gy + Gz * Gz); - if (isnan(Dk)) { - Dk = distance; - } - Dk = 1 - Dk / distance; - return Dk; -} - -float _c_calculate_rgc3D(int xM, int yM, int sliceM, float* imIntGx, float* imIntGy, float* imIntGz, int colsM, int rowsM, int slicesM, int magnification_xy, int magnification_z, float ratio_px, float Gx_Gy_MAGNIFICATION, float Gz_MAGNIFICATION, float fwhm, float fwhm_z, float tSO, float tSO_z, float tSS, float tSS_z, float sensitivity) { - - float vx, vy, vz, Gx, Gy, Gz, dx, dy, dz, dz_real, distance, distance_xy, distance_z, distanceWeight, distanceWeight_xy, distanceWeight_z, GdotR, Dk; - - float xc = (xM) / magnification_xy; - float yc = (yM) / magnification_xy; - float zc = (sliceM) / magnification_z; - - float RGC = 0; - float distanceWeightSum = 0; - float distanceWeightSum_xy = 0; - float distanceWeightSum_z = 0; - - int _start = -(int)(2 * fwhm); - int _end = (int)(2 * fwhm + 1); - - int _start_z = -(int)(2 * fwhm_z); - int _end_z = (int)(2 * fwhm_z + 1); - - for (int j = _start; j <= _end; j++) { - vy = yc + j; - - if (0 < vy && vy < rowsM/magnification_xy - 1) { - for (int i = _start; i <= _end; i++) { - vx = xc + i; - - if (0 < vx && vx < colsM/magnification_xy - 1) { - for (int k = _start_z; k <= _end_z; k++) { - vz = zc + k; - - if (0 < vz && vz < slicesM/magnification_z - 1) { - dx = vx - xc; - dy = vy - yc; - dz = vz - zc; - dz_real = dz * ratio_px; // This has been already divided by magnification_z - distance = sqrt(dx * dx + dy * dy + dz_real * dz_real); - distance_xy = sqrt(dx * dx + dy * dy); - distance_z = dz_real; - - if (distance != 0 && distance_xy <= tSO && distance_z <= tSO_z) { - int linear_index = (int)(vz * magnification_z * Gz_MAGNIFICATION) * rowsM * Gx_Gy_MAGNIFICATION * colsM * Gx_Gy_MAGNIFICATION + - (int)(magnification_xy * Gx_Gy_MAGNIFICATION * vy) * colsM * Gx_Gy_MAGNIFICATION + - (int)(magnification_xy * Gx_Gy_MAGNIFICATION * vx); - - Gx = imIntGx[linear_index]; - Gy = imIntGy[linear_index]; - Gz = imIntGz[linear_index]; - - // distanceWeight = _c_calculate_dw3D_isotropic(distance, tSS); - distanceWeight = _c_calculate_dw3D(distance, distance_xy, distance_z, tSS, tSS_z); - - // distanceWeight_xy = _c_calculate_dw_xy(distance_xy, tSS); - // distanceWeight_z = _c_calculate_dw_z(distance_z, tSS_z); - - distanceWeightSum += distanceWeight; - // distanceWeightSum_xy += distanceWeight_xy; - // distanceWeightSum_z += distanceWeight_z; - GdotR = Gx*dx + Gy*dy + Gz*dz_real; - - if (GdotR < 0) { - Dk = _c_calculate_dk3D(Gx, Gy, Gz, dx, dy, dz_real, distance); - RGC += (Dk * distanceWeight); - } - } - } - } - } - } - } - } - - RGC /= distanceWeightSum; - - if (RGC >= 0) { - RGC = pow(RGC, sensitivity); - } else { - RGC = 0; - } - - return RGC; -} - - -__kernel void interpolate_3d() - - -__kernel void gradients_3d(__global float* image, __global float* imGc, __global float* imGr, __global float* imGs, int f, int slices, int rows, int cols) { - _c_gradient_3d(&image[f * slices * rows * cols], &imGc[f * slices * rows * cols], &imGr[f * slices * rows * cols], &imGs[f * slices * rows * cols], slices, rows, cols); -} - -__kernel void calculate_rgc3D() \ No newline at end of file diff --git a/t.ipynb b/t.ipynb deleted file mode 100644 index da85acdb..00000000 --- a/t.ipynb +++ /dev/null @@ -1,124 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import numpy as np\n", - "from tifffile import imread\n", - "tetra = imread(\"/Users/bsaraiva/Code/NanoPyx/tetrahedron_emitters.tiff\")\n", - "#cross_single_emitters.tiff\n", - "cross = imread(\"/Users/bsaraiva/Code/NanoPyx/cross_single_emitters.tiff\")\n", - "big = imread(\"/Users/bsaraiva/Code/NanoPyx/3D_stack_maxproject_frames.tif\")\n", - "\n", - "bigger = np.random.random((100, 80, 300, 300)).astype(np.float32)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Agent: eSRRF_3D using threaded ran in 23.232891875028145 seconds\n", - "Time 1 0.0\n", - "Agent: eSRRF_3D_v2 using threaded ran in 7.750940249999985 seconds\n" - ] - } - ], - "source": [ - "from nanopyx.core.transform._le_esrrf3d import eSRRF3D, eSRRF3D_v2\n", - "from stackview import slice\n", - "\n", - "img_to_use = cross\n", - "calc = eSRRF3D()\n", - "#calc_v2 = eSRRF3D_v2()\n", - "out = calc.run(img_to_use, magnification_xy=2, magnification_z=2, PSF_voxel_ratio=4, doIntensityWeighting=False, run_type=\"threaded\")\n", - "#out_v2 = calc_v2.run(img_to_use, magnification_xy=2, magnification_z=2, PSF_voxel_ratio=4, doIntensityWeighting=False, run_type=\"threaded\")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "4b18e0af2b374fc48eb5764c26db8def", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "HBox(children=(VBox(children=(VBox(children=(HBox(children=(VBox(children=(ImageWidget(height=160, width=160),…" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "slice(np.mean(out, axis=0), zoom_factor=2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "a0f47d88b1e84eb78ee7070a156da0c0", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "HBox(children=(VBox(children=(VBox(children=(HBox(children=(VBox(children=(ImageWidget(height=160, width=160),…" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "#slice(out_v2, zoom_factor=2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "aiobio_dev", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.11" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}