From 1be29f029fc0705aec40a2bf4e4ebad098dadb48 Mon Sep 17 00:00:00 2001 From: Leon Merten Lohse <llohse@uni-goettingen.de> Date: Thu, 12 Dec 2024 13:47:44 +0100 Subject: [PATCH 1/3] remove unused padding functions --- src/hotopy/holo/_util.py | 45 ------------------- src/hotopy/image/_stats.py | 29 +++++++++++- src/hotopy/image/_transforms.py | 79 +-------------------------------- 3 files changed, 28 insertions(+), 125 deletions(-) diff --git a/src/hotopy/holo/_util.py b/src/hotopy/holo/_util.py index f4d1bc2..36180bc 100644 --- a/src/hotopy/holo/_util.py +++ b/src/hotopy/holo/_util.py @@ -2,7 +2,6 @@ Author: Jens Lucht """ -from math import ceil, floor from itertools import combinations from scipy.fft import fft2, ifft2 @@ -19,50 +18,6 @@ def _freq_filter2(x, filter): return np.real(ifft2(fft2(x) * filter)) -def padwidth(imshape, padshape): - """ - Difference of padded shape and image shape - """ - return (np.asarray(padshape) - np.asarray(imshape)).astype(int) - - -def symmetric_padding(imshape, padding): - """ - Shape of image when padded with padding - """ - # padding = [0, *padwidth(imshape, padshape)] # do not pad in 1st dim in stack of holograms - return [[floor(p / 2), ceil(p / 2)] for p in padding] - - -def crops(shape): - """ - Crop array to given shape - - Parameters - ---------- - shape : shape[ tuple, sequence ] - Shape of unpadded, i.e. original, data, which shape should be recovered. - - Returns - ------- - Tuple of slices similar to np.s_ which can be directly used for array indexing. - Example: - arr[crops((2,2)] returns arr[0:2, 0:2] - """ - return tuple([slice(0, dim) for dim in shape]) - - -def croppad(padding): - """ - Crops padding in [(before, after), ...] per axis manner. - - See for example np.pad. - """ - return tuple( - [slice(before if before else None, -after if after else None) for before, after in padding] - ) - - def check_fresnel_number( images, fresnel_number_expected, betadelta=0.0, num_minima=10, labels=None, ax=None ): diff --git a/src/hotopy/image/_stats.py b/src/hotopy/image/_stats.py index 1741196..3967f97 100644 --- a/src/hotopy/image/_stats.py +++ b/src/hotopy/image/_stats.py @@ -3,7 +3,6 @@ from scipy.stats import binned_statistic from scipy.fft import fftn from ..utils import fftfreqn -from ._transforms import crop_quadratic from . import ndwindow @@ -30,6 +29,32 @@ def radial_average(a): return binned_statistic(r.ravel(), a.ravel(), bins=n, range=(0, n)) +def _crop_center(a, out_shape): + """ + Crops `a` symmetrically around the center to given size `size`. + + Parameters: + a: image or ndarray to crop + out_shape: output shape + + Returns: + array crop around the center with shape `size`. + """ + in_center = [dim // 2 for dim in a.shape] + out_center = [si // 2 for si in out_shape] + slices = [slice(ci - si, ci + si) for ci, si in zip(in_center, out_center)] + + return a[slices] + + +def _crop_quadratic(a): + """ + Crops input a to maximal length quadratic shape. + """ + size = tuple([min(a.shape)] * a.ndim) + return _crop_center(a, size) + + def radial_power_spectrum(im, window=("kaiser", 8)): """ Radial averaged power spectrum density of image im. @@ -53,7 +78,7 @@ def radial_power_spectrum(im, window=("kaiser", 8)): ------- >>> psd, freq, binn = radial_power_spectrum(im, ("kaiser", 8)) """ - im = crop_quadratic(im) + im = _crop_quadratic(im) if window is not None: im = im * ndwindow(window, im.shape) diff --git a/src/hotopy/image/_transforms.py b/src/hotopy/image/_transforms.py index c35c053..a90b098 100644 --- a/src/hotopy/image/_transforms.py +++ b/src/hotopy/image/_transforms.py @@ -1,55 +1,9 @@ -from itertools import repeat -from numpy import ones, diag, asarray, ascontiguousarray, zeros +from numpy import ones, diag, asarray, ascontiguousarray from numpy import linalg from scipy.ndimage import affine_transform, shift as ndshift -def center_slices(dims, size, dtype=int): - """ - Symmetrically slices around center of input dims. - - Parameters - ---------- - dims: tuple, shape-like - Shape of input array - size: tuple, shape-like - Shape of output array. Needs to be smaller than `dims`. - dtype: type - datatype of indices - - """ - if not len(dims) == len(size): - raise ValueError(f"In- and output dimensions do not match: {len(dims)} != {len(size)}") - center = [dim // 2 for dim in dims] - size = [si // 2 for si in size] - - slices = [slice(ci - si, ci + si) for ci, si in zip(center, size)] - return tuple(slices) - - -def crop_center(a, size): - """ - Crops `a` symmetrically around the center to given size `size`. - - Parameters: - a: image or ndarray to crop - size: output size - - Returns: - array crop around the center with shape `size`. - """ - return a[center_slices(a.shape, size)] - - -def crop_quadratic(a): - """ - Crops input a to maximal length quadratic shape. - """ - size = repeat(min(a.shape), a.ndim) - return crop_center(a, tuple(size)) - - def imscale(input, scale, center=True, mode="nearest", **kwargs): """ Scales an image using an affine transformation. @@ -215,34 +169,3 @@ def imshiftscale(input, shift, scale, center=True, offset=None, mode="nearest", inv = linalg.inv(op) return affine_transform(input, inv, mode=mode, **kwargs) - - -def imembed_zero(im, dims, caxis=-1): - """ - Embeds a given image into larger zero-padded image. - """ - im = asarray(im) - imdims = im.shape - - # check for channel axis - channel_axis = im.ndim == len(dims) + 1 - - if channel_axis: - new_dims = (*tuple(dims), im.shape[caxis]) - new_dims = tuple( - asarray(new_dims).swapaxes(-1, caxis) # ensure channel axis at correct position - ) - else: - new_dims = dims - - # determine center slice for each dim to put original image in - center_slice = tuple( - slice(dims[dim] // 2 - imdims[dim] // 2, dims[dim] // 2 + (imdims[dim] - 1) // 2 + 1) - for dim in range(len(dims)) - ) - - # embed into zeros - embed = zeros(new_dims, dtype=im.dtype) - embed[center_slice] = im - - return embed -- GitLab From 43e313fae887d2aa2bda47f1e10cd8ba74619828 Mon Sep 17 00:00:00 2001 From: Leon Merten Lohse <llohse@uni-goettingen.de> Date: Thu, 12 Dec 2024 13:52:40 +0100 Subject: [PATCH 2/3] ruff format --- src/hotopy/image/_transforms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hotopy/image/_transforms.py b/src/hotopy/image/_transforms.py index a90b098..a68258d 100644 --- a/src/hotopy/image/_transforms.py +++ b/src/hotopy/image/_transforms.py @@ -1,4 +1,3 @@ - from numpy import ones, diag, asarray, ascontiguousarray from numpy import linalg from scipy.ndimage import affine_transform, shift as ndshift -- GitLab From 5eadd1a08d39480f7128f1fe9050570fcc11573f Mon Sep 17 00:00:00 2001 From: Leon Merten Lohse <llohse@uni-goettingen.de> Date: Thu, 12 Dec 2024 13:55:44 +0100 Subject: [PATCH 3/3] remove link to removed padding functions --- src/hotopy/image/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/hotopy/image/__init__.py b/src/hotopy/image/__init__.py index 1ab037b..d635eff 100644 --- a/src/hotopy/image/__init__.py +++ b/src/hotopy/image/__init__.py @@ -19,8 +19,6 @@ Transformation .. autosummary:: :toctree: generated/ - crop_center - crop_quadratic imscale imshift imshiftscale -- GitLab