Skip to content

Commit

Permalink
#12 Moved the gauss_kernel function from 0900 to helper functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
carljohnsen committed Sep 17, 2024
1 parent 054a078 commit 8c82445
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
18 changes: 18 additions & 0 deletions src/lib/py/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@ def generate_cylinder_mask(nx):
rs = np.sqrt(xs[np.newaxis,np.newaxis,:]**2 + xs[np.newaxis,:,np.newaxis]**2)
return rs <= 1

def gauss_kernel(sigma):
radius = round(4.0 * sigma) # stolen from the default scipy parameters
# Deprecated:
#kernel = ndi.filters._gaussian_kernel1d(sigma_voxels, 0, radius).astype(internal_type)

if False:
# Create a 1D Gaussian
x = np.arange(-radius, radius + 1)
kernel = 1.0 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-x**2 / (2 * sigma**2))
return kernel
else:
# Stolen from ndimage
sigma2 = sigma * sigma
x = np.arange(-radius, radius+1)
phi_x = np.exp(-0.5 / sigma2 * x ** 2)
phi_x = phi_x / phi_x.sum()
return phi_x

def generate_cylinder_mask(ny, nx):
'''
Generate a 2D mask of a cylinder with diameter `nx` pixels in the x-dimension and `ny` pixels in the y-dimension.
Expand Down
20 changes: 1 addition & 19 deletions src/processing_steps/0900_generate_gauss_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from scipy import ndimage as ndi
import multiprocessing as mp
from config.paths import hdf5_root, binary_root
from lib.py.helpers import commandline_args, to_int
from lib.py.helpers import commandline_args, gaussian_kernel, to_int
from lib.cpp.gpu.diffusion import diffusion
from lib.cpp.cpu.io import load_slice, write_slice
NA = np.newaxis
Expand All @@ -23,24 +23,6 @@
internal_type = np.float32
result_type = np.uint16

def gauss_kernel(sigma):
radius = round(4.0 * sigma) # stolen from the default scipy parameters
# Deprecated:
#kernel = ndi.filters._gaussian_kernel1d(sigma_voxels, 0, radius).astype(internal_type)

if False:
# Create a 1D Gaussian
x = np.arange(-radius, radius + 1)
kernel = 1.0 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-x**2 / (2 * sigma**2))
return kernel
else:
# Stolen from ndimage
sigma2 = sigma * sigma
x = np.arange(-radius, radius+1)
phi_x = np.exp(-0.5 / sigma2 * x ** 2)
phi_x = phi_x / phi_x.sum()
return phi_x

# sigma is given in physical units, i.e. in micrometers, in order to give scale-invariant results.
if __name__ == '__main__':
sample, scale, sigma, reps, verify, verbose = \
Expand Down

0 comments on commit 8c82445

Please sign in to comment.