prtools 1.3.1__tar.gz → 1.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {prtools-1.3.1/prtools.egg-info → prtools-1.4.0}/PKG-INFO +1 -1
- {prtools-1.3.1 → prtools-1.4.0}/prtools/__init__.py +14 -3
- {prtools-1.3.1 → prtools-1.4.0}/prtools/array.py +5 -4
- prtools-1.3.1/prtools/_backend.py → prtools-1.4.0/prtools/backend/__init__.py +42 -10
- {prtools-1.3.1 → prtools-1.4.0}/prtools/backend/_base.py +1 -1
- prtools-1.4.0/prtools/backend/_jax.py +49 -0
- prtools-1.3.1/prtools/backend/numpy.py → prtools-1.4.0/prtools/backend/_numpy.py +3 -3
- prtools-1.4.0/prtools/convolve.py +348 -0
- {prtools-1.3.1 → prtools-1.4.0}/prtools/cost.py +1 -1
- {prtools-1.3.1 → prtools-1.4.0}/prtools/fourier.py +2 -5
- prtools-1.4.0/prtools/jax.py +133 -0
- {prtools-1.3.1 → prtools-1.4.0}/prtools/misc.py +2 -2
- {prtools-1.3.1 → prtools-1.4.0}/prtools/segmented.py +1 -1
- {prtools-1.3.1 → prtools-1.4.0}/prtools/shape.py +1 -1
- {prtools-1.3.1 → prtools-1.4.0}/prtools/stats.py +1 -1
- {prtools-1.3.1 → prtools-1.4.0}/prtools/zernike.py +92 -31
- {prtools-1.3.1 → prtools-1.4.0/prtools.egg-info}/PKG-INFO +1 -1
- {prtools-1.3.1 → prtools-1.4.0}/prtools.egg-info/SOURCES.txt +6 -4
- prtools-1.4.0/tests/test_array.py +67 -0
- prtools-1.4.0/tests/test_fourier.py +95 -0
- prtools-1.3.1/prtools/backend/__init__.py +0 -19
- prtools-1.3.1/prtools/backend/jax.py +0 -130
- prtools-1.3.1/tests/test_array.py +0 -45
- {prtools-1.3.1 → prtools-1.4.0}/LICENSE +0 -0
- {prtools-1.3.1 → prtools-1.4.0}/NOTICE +0 -0
- {prtools-1.3.1 → prtools-1.4.0}/README.rst +0 -0
- {prtools-1.3.1 → prtools-1.4.0}/prtools/sparse.py +0 -0
- {prtools-1.3.1 → prtools-1.4.0}/prtools.egg-info/dependency_links.txt +0 -0
- {prtools-1.3.1 → prtools-1.4.0}/prtools.egg-info/requires.txt +0 -0
- {prtools-1.3.1 → prtools-1.4.0}/prtools.egg-info/top_level.txt +0 -0
- {prtools-1.3.1 → prtools-1.4.0}/pyproject.toml +0 -0
- {prtools-1.3.1 → prtools-1.4.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
__version__ = '1.
|
|
1
|
+
__version__ = '1.4.0'
|
|
2
2
|
|
|
3
|
-
from prtools.
|
|
3
|
+
from prtools.backend import __backend__, use
|
|
4
4
|
|
|
5
5
|
from prtools.array import (
|
|
6
6
|
centroid,
|
|
@@ -15,10 +15,22 @@ from prtools.array import (
|
|
|
15
15
|
medfix
|
|
16
16
|
)
|
|
17
17
|
|
|
18
|
+
from prtools.convolve import (
|
|
19
|
+
fftconv,
|
|
20
|
+
gauss,
|
|
21
|
+
gauss_blur,
|
|
22
|
+
gauss_kernel,
|
|
23
|
+
pixel_kernel,
|
|
24
|
+
pixelate,
|
|
25
|
+
sinc,
|
|
26
|
+
)
|
|
27
|
+
|
|
18
28
|
from prtools.cost import sserror
|
|
19
29
|
|
|
20
30
|
from prtools.fourier import dft2, idft2
|
|
21
31
|
|
|
32
|
+
import prtools.jax
|
|
33
|
+
|
|
22
34
|
from prtools.misc import (
|
|
23
35
|
calcpsf,
|
|
24
36
|
translation_defocus,
|
|
@@ -36,7 +48,6 @@ from prtools.shape import (
|
|
|
36
48
|
hexagon,
|
|
37
49
|
rectangle,
|
|
38
50
|
spider,
|
|
39
|
-
gauss,
|
|
40
51
|
sin,
|
|
41
52
|
waffle
|
|
42
53
|
)
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import warnings
|
|
2
2
|
|
|
3
3
|
from prtools import __backend__
|
|
4
|
-
from prtools.
|
|
5
|
-
from prtools.
|
|
4
|
+
from prtools.backend import numpy as np
|
|
5
|
+
from prtools.backend import scipy
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
def centroid(a, where=None, kind='absolute', indexing='ij'):
|
|
@@ -265,8 +265,9 @@ def boundary(x, threshold=0):
|
|
|
265
265
|
rows = np.any(x, axis=1)
|
|
266
266
|
cols = np.any(x, axis=0)
|
|
267
267
|
|
|
268
|
-
|
|
269
|
-
|
|
268
|
+
idx = np.array((0, -1)) # https://github.com/andykee/prtools/issues/6
|
|
269
|
+
rmin, rmax = np.where(rows)[0][idx]
|
|
270
|
+
cmin, cmax = np.where(cols)[0][idx]
|
|
270
271
|
|
|
271
272
|
return rmin, rmax, cmin, cmax
|
|
272
273
|
|
|
@@ -1,12 +1,22 @@
|
|
|
1
|
+
import importlib
|
|
1
2
|
import sys
|
|
2
3
|
import types
|
|
3
4
|
|
|
4
|
-
from
|
|
5
|
-
from prtools.backend._base import _BackendLibrary
|
|
5
|
+
from ._base import BackendLibrary
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
"""
|
|
8
|
+
def backend_available(name):
|
|
9
|
+
"""Return True if backend library is available"""
|
|
10
|
+
try:
|
|
11
|
+
importlib.import_module(name)
|
|
12
|
+
except ImportError:
|
|
13
|
+
return False
|
|
14
|
+
else:
|
|
15
|
+
return True
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class BackendName:
|
|
19
|
+
"""Helper class for changing the backend name - needed since strings are
|
|
10
20
|
immutable.
|
|
11
21
|
"""
|
|
12
22
|
|
|
@@ -23,10 +33,32 @@ class _BackendName:
|
|
|
23
33
|
return self.name == other
|
|
24
34
|
|
|
25
35
|
|
|
26
|
-
class
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
36
|
+
class BackendRegistry:
|
|
37
|
+
|
|
38
|
+
_BUILTIN_BACKEND = ['numpy', 'jax']
|
|
39
|
+
|
|
40
|
+
def _get_backend_module(self, backend):
|
|
41
|
+
return importlib.import_module(f'._{backend}', 'prtools.backend')
|
|
42
|
+
|
|
43
|
+
def load_numpy(self, backend):
|
|
44
|
+
backend = self._get_backend_module(backend)
|
|
45
|
+
return backend.Numpy()
|
|
46
|
+
|
|
47
|
+
def load_scipy(self, backend):
|
|
48
|
+
return self._get_backend_module(backend).Scipy()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
registry = BackendRegistry()
|
|
52
|
+
|
|
53
|
+
class Backend(types.ModuleType):
|
|
54
|
+
|
|
55
|
+
numpy = BackendLibrary(None)
|
|
56
|
+
scipy = BackendLibrary(None)
|
|
57
|
+
|
|
58
|
+
__backend__ = BackendName(None)
|
|
59
|
+
|
|
60
|
+
JAX_AVAILABLE = backend_available('jax') & backend_available('optax')
|
|
61
|
+
NUMPY_AVAILABLE = backend_available('numpy')
|
|
30
62
|
|
|
31
63
|
@classmethod
|
|
32
64
|
def use(cls, backend):
|
|
@@ -67,7 +99,7 @@ class _Backend(types.ModuleType):
|
|
|
67
99
|
|
|
68
100
|
|
|
69
101
|
# use NumPy by default
|
|
70
|
-
|
|
102
|
+
Backend.use('numpy')
|
|
71
103
|
|
|
72
104
|
# https://stackoverflow.com/a/72911884
|
|
73
|
-
sys.modules[__name__].__class__ =
|
|
105
|
+
sys.modules[__name__].__class__ = Backend
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import importlib
|
|
2
|
+
|
|
3
|
+
from ._base import BackendLibrary
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Numpy(BackendLibrary):
|
|
7
|
+
def __init__(self):
|
|
8
|
+
super().__init__(importlib.import_module('jax.numpy'))
|
|
9
|
+
|
|
10
|
+
def broadcast_to(self, array, shape):
|
|
11
|
+
# jax numpy.broadcast_to expects an array input
|
|
12
|
+
array = self.module.asarray(array)
|
|
13
|
+
return self.module.broadcast_to(array, shape)
|
|
14
|
+
|
|
15
|
+
def divide(self, a, b, out=None):
|
|
16
|
+
# jax.numpy.divide doesn't support the `out` parameter so we
|
|
17
|
+
# ignore it
|
|
18
|
+
return self.module.divide(a, b)
|
|
19
|
+
|
|
20
|
+
def dot(self, a, b, out=None):
|
|
21
|
+
# jax.numpy.dot doesn't support the `out` parameter so we ignore it
|
|
22
|
+
return self.module.dot(a, b)
|
|
23
|
+
|
|
24
|
+
def floor(self, x, *args, **kwargs):
|
|
25
|
+
# jax numpy.floor expects a scalar or array input. It also doesn't
|
|
26
|
+
# support the `out` parameter
|
|
27
|
+
kwargs.pop('out', None)
|
|
28
|
+
x = self.module.asarray(x)
|
|
29
|
+
return self.module.floor(x, *args, **kwargs)
|
|
30
|
+
|
|
31
|
+
def max(self, a, *args, **kwargs):
|
|
32
|
+
# jax numpy.max expects an array input
|
|
33
|
+
array = self.module.asarray(a)
|
|
34
|
+
return self.module.max(array, *args, **kwargs)
|
|
35
|
+
|
|
36
|
+
def multiply(self, a, b, out=None):
|
|
37
|
+
# jax.numpy.multiply doesn't support the `out` parameter so we
|
|
38
|
+
# ignore it
|
|
39
|
+
return self.module.multiply(a, b)
|
|
40
|
+
|
|
41
|
+
def sum(self, a, *args, **kwargs):
|
|
42
|
+
kwargs.pop('out', None)
|
|
43
|
+
a = self.module.asarray(a)
|
|
44
|
+
return self.module.sum(a, *args, **kwargs)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Scipy(BackendLibrary):
|
|
48
|
+
def __init__(self):
|
|
49
|
+
super().__init__(importlib.import_module('jax.scipy'))
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
from ._base import
|
|
1
|
+
from ._base import BackendLibrary
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
class Numpy(
|
|
4
|
+
class Numpy(BackendLibrary):
|
|
5
5
|
def __init__(self):
|
|
6
6
|
import numpy
|
|
7
7
|
super().__init__(numpy)
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
class Scipy(
|
|
10
|
+
class Scipy(BackendLibrary):
|
|
11
11
|
def __init__(self):
|
|
12
12
|
import scipy
|
|
13
13
|
super().__init__(scipy)
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
from prtools.backend import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def fftconv(array, kernel, normalize_kernel=True, fft_array=True,
|
|
5
|
+
fft_kernel=True):
|
|
6
|
+
r"""Convolve an array with a kernel using the FFT.
|
|
7
|
+
|
|
8
|
+
The colvolution is computed as
|
|
9
|
+
|
|
10
|
+
.. math::
|
|
11
|
+
|
|
12
|
+
u*v = \mathcal{F}^{-1}\left\{\mathcal{F}\{u\}\cdot\mathcal{F}\{v\}\right\}
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
array : array_like
|
|
17
|
+
Array to be convolved with `kernel`.
|
|
18
|
+
kernel : array_like
|
|
19
|
+
Convolution kernel. Should have the same shape as `array`.
|
|
20
|
+
normalize_kernel : bool, optional
|
|
21
|
+
If True (default), kernel is normalized so that `kernel =
|
|
22
|
+
kernel / np.sum(kernel)`.
|
|
23
|
+
fft_array : bool, optional
|
|
24
|
+
If True (default), the array is assumed to be provided in the spatial
|
|
25
|
+
domain. If False, the array is assumed to be provided in the frequency
|
|
26
|
+
domain.
|
|
27
|
+
fft_kernel : bool, optional
|
|
28
|
+
If True (default), the kernel is assumed to be provided in the spatial
|
|
29
|
+
domain. If False, the kernel is assumed to be provided in the frequency
|
|
30
|
+
domain.
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
-------
|
|
34
|
+
ndarray
|
|
35
|
+
|
|
36
|
+
See also
|
|
37
|
+
--------
|
|
38
|
+
:func:`~prtools.gauss_blur`
|
|
39
|
+
:func:`~prtools.pixelate`
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
a = np.fft.fft2(array) if fft_array else array
|
|
43
|
+
k = np.fft.fft2(array) if fft_kernel else kernel
|
|
44
|
+
|
|
45
|
+
if normalize_kernel:
|
|
46
|
+
k /= np.sum(k)
|
|
47
|
+
|
|
48
|
+
return np.fft.ifft2(a*k).real
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def gauss_blur(img, sigma, oversample=1):
|
|
52
|
+
"""Blur an image using a Gaussian filter using the FFT.
|
|
53
|
+
|
|
54
|
+
Parameters
|
|
55
|
+
----------
|
|
56
|
+
img : array_like
|
|
57
|
+
Array to be blurred
|
|
58
|
+
sigma : float or (2,) array_like
|
|
59
|
+
Standard deviation of Gaussian. Providing two values allows for
|
|
60
|
+
non-symmetric Gaussian interpreted as `(sigma_row, sigma_col)`
|
|
61
|
+
oversample : float, optional
|
|
62
|
+
Oversampling factor of `img`. Default is 1.
|
|
63
|
+
|
|
64
|
+
Returns
|
|
65
|
+
-------
|
|
66
|
+
ndarray
|
|
67
|
+
|
|
68
|
+
Examples
|
|
69
|
+
--------
|
|
70
|
+
.. plot::
|
|
71
|
+
:include-source:
|
|
72
|
+
:context: reset
|
|
73
|
+
:scale: 50
|
|
74
|
+
|
|
75
|
+
>>> img = prtools.circle((256,256), 100)
|
|
76
|
+
>>> img_blurred = prtools.gauss_blur(img, sigma=3)
|
|
77
|
+
>>> fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(5,2))
|
|
78
|
+
>>> ax[0].imshow(img, cmap='gray')
|
|
79
|
+
>>> ax[0].set_title('Input image')
|
|
80
|
+
>>> ax[1].imshow(img_blurred, cmap='gray')
|
|
81
|
+
>>> ax[1].set_title('Blurred image')
|
|
82
|
+
"""
|
|
83
|
+
img = np.asarray(img)
|
|
84
|
+
kernel = gauss_kernel(img.shape, sigma, oversample, fftshift=False)
|
|
85
|
+
return fftconv(img, kernel, normalize_kernel=True, fft_array=True,
|
|
86
|
+
fft_kernel=False)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def pixelate(img, oversample=1):
|
|
90
|
+
"""Apply the aperture effects of an idealized square pixel on a discretely
|
|
91
|
+
sampled image using the FFT.
|
|
92
|
+
|
|
93
|
+
Parameters
|
|
94
|
+
----------
|
|
95
|
+
img : array_like
|
|
96
|
+
Input image
|
|
97
|
+
oversample : float, optional
|
|
98
|
+
Oversampling factor of `img`. Default is 1.
|
|
99
|
+
|
|
100
|
+
Returns
|
|
101
|
+
-------
|
|
102
|
+
out : ndarray
|
|
103
|
+
Image with pixel sampling effects applied.
|
|
104
|
+
|
|
105
|
+
Notes
|
|
106
|
+
-----
|
|
107
|
+
To avoid the introduction of numerical artifacts, this function should be
|
|
108
|
+
performed on data that is at least 2x oversampled.
|
|
109
|
+
|
|
110
|
+
Examples
|
|
111
|
+
--------
|
|
112
|
+
.. plot::
|
|
113
|
+
:include-source:
|
|
114
|
+
:context: reset
|
|
115
|
+
:scale: 50
|
|
116
|
+
|
|
117
|
+
>>> img = prtools.circle((17,17), 5)
|
|
118
|
+
>>> img_pixelate = prtools.pixelate(img)
|
|
119
|
+
>>> fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(5,2))
|
|
120
|
+
>>> ax[0].imshow(img, cmap='gray')
|
|
121
|
+
>>> ax[0].set_title('Input image')
|
|
122
|
+
>>> ax[1].imshow(img_pixelate, cmap='gray')
|
|
123
|
+
>>> ax[1].set_title('Pixelated image')
|
|
124
|
+
|
|
125
|
+
Note that the pixelation process preserves image power:
|
|
126
|
+
|
|
127
|
+
.. code:: pycon
|
|
128
|
+
|
|
129
|
+
>>> print(np.sum(img), np.sum(img_pixelate))
|
|
130
|
+
64.0, 64.0
|
|
131
|
+
"""
|
|
132
|
+
img = np.asarray(img)
|
|
133
|
+
kernel = pixel_kernel(img.shape, oversample=oversample, fftshift=False)
|
|
134
|
+
return fftconv(img, kernel, normalize_kernel=False, fft_array=True,
|
|
135
|
+
fft_kernel=False)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def gauss(x1, x2, sigma, oversample=1, indexing='ij', normalize=False):
|
|
139
|
+
"""2D Gaussian function
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
x1, x2 : array_like
|
|
144
|
+
1-D arrays representing the grid coordinates
|
|
145
|
+
sigma : float or (2,) array_like
|
|
146
|
+
Standard deviation of Gaussian. Providing two values allows for
|
|
147
|
+
non-symmetric Gaussian interpreted as `(sigma_row, sigma_col)`
|
|
148
|
+
oversample : float, optional
|
|
149
|
+
Oversampling factor. Defailt is 1.
|
|
150
|
+
indexing : {'ij', 'xy'}, optional
|
|
151
|
+
Matrix ('ij', default) or cartesian ('xy') indexing of output.
|
|
152
|
+
normalize : bool, optional
|
|
153
|
+
If True, the output is normalized such that its sum is equal to 1.
|
|
154
|
+
If False (default), the output has max equal to one.
|
|
155
|
+
|
|
156
|
+
Returns
|
|
157
|
+
-------
|
|
158
|
+
ndarray
|
|
159
|
+
|
|
160
|
+
Examples
|
|
161
|
+
--------
|
|
162
|
+
.. plot::
|
|
163
|
+
:include-source:
|
|
164
|
+
:context: reset
|
|
165
|
+
:scale: 50
|
|
166
|
+
|
|
167
|
+
>>> r = np.arange(-128,128)
|
|
168
|
+
>>> c = np.arange(-128,128)
|
|
169
|
+
>>> g = prtools.gauss(r, c, sigma=20)
|
|
170
|
+
>>> plt.imshow(g, cmap='gray')
|
|
171
|
+
"""
|
|
172
|
+
xx1, xx2 = np.meshgrid(x1, x2, indexing=indexing)
|
|
173
|
+
sigma = np.broadcast_to(sigma, (2,)) / oversample
|
|
174
|
+
g = np.exp(-((xx1**2/(2*sigma[0]**2)) + (xx2**2/(2*sigma[1]**2))))
|
|
175
|
+
if normalize:
|
|
176
|
+
g = g / (2*np.pi * np.prod(sigma))
|
|
177
|
+
return g
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def sinc(x1, x2, indexing='ij'):
|
|
181
|
+
r"""Normalized 2D sinc function
|
|
182
|
+
|
|
183
|
+
Parameters
|
|
184
|
+
----------
|
|
185
|
+
x1, x2 : array_like
|
|
186
|
+
1-D arrays representing the grid coordinates
|
|
187
|
+
indexing : {'ij', 'xy'}, optional
|
|
188
|
+
Matrix ('ij', default) or cartesian ('xy') indexing of output.
|
|
189
|
+
normalize : bool, optional
|
|
190
|
+
If True, the output is normalized such that its sum is equal to 1.
|
|
191
|
+
If False (default), the output has max equal to one.
|
|
192
|
+
|
|
193
|
+
Returns
|
|
194
|
+
-------
|
|
195
|
+
ndarray
|
|
196
|
+
|
|
197
|
+
See also
|
|
198
|
+
--------
|
|
199
|
+
:func:`~prtools.pixel_kernel`
|
|
200
|
+
|
|
201
|
+
Notes
|
|
202
|
+
-----
|
|
203
|
+
This function uses ``numpy.sinc``, which computes the sinc function as
|
|
204
|
+
|
|
205
|
+
.. math::
|
|
206
|
+
\mbox{sinc}(x) = \frac{\sin \pi x}{\pi x}
|
|
207
|
+
|
|
208
|
+
Examples
|
|
209
|
+
--------
|
|
210
|
+
.. plot::
|
|
211
|
+
:include-source:
|
|
212
|
+
:context: reset
|
|
213
|
+
:scale: 50
|
|
214
|
+
|
|
215
|
+
>>> r = np.linspace(-3,3,512)
|
|
216
|
+
>>> c = np.linspace(-3,3,512)
|
|
217
|
+
>>> s = prtools.sinc(r, c)
|
|
218
|
+
>>> plt.imshow(s, cmap='gray', extent=[r.min(), r.max(), c.min(), c.max()])
|
|
219
|
+
"""
|
|
220
|
+
xx1, xx2 = np.meshgrid(x1, x2, indexing=indexing)
|
|
221
|
+
return np.sinc(xx1) * np.sinc(xx2)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def gauss_kernel(shape, sigma, oversample=1, fftshift=True):
|
|
225
|
+
"""2D Gaussian filter kernel
|
|
226
|
+
|
|
227
|
+
This function returns the transfer function of a normalized 2D Gaussian
|
|
228
|
+
function.
|
|
229
|
+
|
|
230
|
+
Parameters
|
|
231
|
+
----------
|
|
232
|
+
shape : int or tuple of int
|
|
233
|
+
Shape of the kernel
|
|
234
|
+
sigma : float
|
|
235
|
+
Standard deviation of Gaussian. Providing two values allows for
|
|
236
|
+
non-symmetric Gaussian interpreted as `(sigma_row, sigma_col)`
|
|
237
|
+
oversample : float, optional
|
|
238
|
+
Oversampling factor to represent in the kernel. Default is 1.
|
|
239
|
+
fftshift : bool, optional
|
|
240
|
+
If True (default), the kernel is FFT-shifted before it is returned.
|
|
241
|
+
|
|
242
|
+
Returns
|
|
243
|
+
-------
|
|
244
|
+
ndarray
|
|
245
|
+
|
|
246
|
+
See also
|
|
247
|
+
--------
|
|
248
|
+
:func:`~prtools.fftconv`
|
|
249
|
+
:func:`~prtools.gauss_blur`
|
|
250
|
+
:func:`~prtools.gauss`
|
|
251
|
+
|
|
252
|
+
Examples
|
|
253
|
+
--------
|
|
254
|
+
.. plot::
|
|
255
|
+
:include-source:
|
|
256
|
+
:context: reset
|
|
257
|
+
:scale: 50
|
|
258
|
+
|
|
259
|
+
>>> blur_kernel = prtools.gauss_kernel((256,256), 1)
|
|
260
|
+
>>> plt.imshow(blur_kernel, cmap='gray')
|
|
261
|
+
|
|
262
|
+
Note this is functionally equivalent to
|
|
263
|
+
|
|
264
|
+
.. plot::
|
|
265
|
+
:include-source:
|
|
266
|
+
:context: reset
|
|
267
|
+
:scale: 50
|
|
268
|
+
|
|
269
|
+
>>> r = np.fft.fftfreq(256)
|
|
270
|
+
>>> c = np.fft.fftfreq(256)
|
|
271
|
+
>>> sigma = 1
|
|
272
|
+
>>> blur_kernel = np.fft.fftshift(prtools.gauss(r, c, 1/(2*np.pi*sigma)))
|
|
273
|
+
>>> plt.imshow(blur_kernel, cmap='gray')
|
|
274
|
+
"""
|
|
275
|
+
|
|
276
|
+
shape = np.broadcast_to(shape, (2,))
|
|
277
|
+
sigma = np.broadcast_to(sigma, (2,))
|
|
278
|
+
|
|
279
|
+
x1 = np.fft.fftfreq(shape[0])
|
|
280
|
+
x2 = np.fft.fftfreq(shape[1])
|
|
281
|
+
|
|
282
|
+
k = gauss(x1, x2, 1/(2*np.pi*sigma), oversample, indexing='ij',
|
|
283
|
+
normalize=False)
|
|
284
|
+
|
|
285
|
+
if fftshift:
|
|
286
|
+
return np.fft.fftshift(k)
|
|
287
|
+
else:
|
|
288
|
+
return k
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def pixel_kernel(shape, oversample=1, fftshift=True):
|
|
292
|
+
r"""2D pixel MTF filter kernel
|
|
293
|
+
|
|
294
|
+
This function returns a normalized 2D sinc function sized to represent
|
|
295
|
+
the transfer function of an idealized square pixel.
|
|
296
|
+
|
|
297
|
+
Parameters
|
|
298
|
+
----------
|
|
299
|
+
shape : int or tuple of int
|
|
300
|
+
Shape of the kernel
|
|
301
|
+
oversample : float, optional
|
|
302
|
+
Oversampling factor to represent in the kernel. Default is 1.
|
|
303
|
+
fftshift : bool, optional
|
|
304
|
+
If True (default), the kernel is FFT-shifted before it is returned.
|
|
305
|
+
|
|
306
|
+
Returns
|
|
307
|
+
-------
|
|
308
|
+
ndarray
|
|
309
|
+
|
|
310
|
+
See also
|
|
311
|
+
--------
|
|
312
|
+
:func:`~prtools.fftconv`
|
|
313
|
+
:func:`~prtools.pixelate`
|
|
314
|
+
:func:`~prtools.sinc`
|
|
315
|
+
|
|
316
|
+
Examples
|
|
317
|
+
--------
|
|
318
|
+
.. plot::
|
|
319
|
+
:include-source:
|
|
320
|
+
:context: reset
|
|
321
|
+
:scale: 50
|
|
322
|
+
|
|
323
|
+
>>> pixel_mtf = prtools.pixel_kernel((256,256), 1)
|
|
324
|
+
>>> plt.imshow(pixel_mtf, cmap='gray', vmin=0)
|
|
325
|
+
|
|
326
|
+
Note this is functionally equivalent to
|
|
327
|
+
|
|
328
|
+
.. plot::
|
|
329
|
+
:include-source:
|
|
330
|
+
:context: reset
|
|
331
|
+
:scale: 50
|
|
332
|
+
|
|
333
|
+
>>> r = np.fft.fftfreq(256)
|
|
334
|
+
>>> c = np.fft.fftfreq(256)
|
|
335
|
+
>>> pixel_mtf = np.fft.fftshift(np.abs(prtools.sinc(r, c)))
|
|
336
|
+
>>> plt.imshow(pixel_mtf, cmap='gray', vmin=0)
|
|
337
|
+
"""
|
|
338
|
+
shape = np.broadcast_to(shape, (2,))
|
|
339
|
+
|
|
340
|
+
x1 = np.fft.fftfreq(shape[0]) / oversample
|
|
341
|
+
x2 = np.fft.fftfreq(shape[1]) / oversample
|
|
342
|
+
|
|
343
|
+
k = np.abs(sinc(x1, x2, indexing='ij'))
|
|
344
|
+
|
|
345
|
+
if fftshift:
|
|
346
|
+
return np.fft.fftshift(k)
|
|
347
|
+
else:
|
|
348
|
+
return k
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import functools
|
|
2
|
-
|
|
3
1
|
from prtools import __backend__
|
|
4
|
-
from prtools.
|
|
2
|
+
from prtools.backend import numpy as np
|
|
5
3
|
|
|
6
4
|
|
|
7
5
|
def dft2(f, alpha, shape=None, shift=(0, 0), offset=(0, 0), unitary=True,
|
|
@@ -76,7 +74,7 @@ def dft2(f, alpha, shape=None, shift=(0, 0), offset=(0, 0), unitary=True,
|
|
|
76
74
|
References
|
|
77
75
|
----------
|
|
78
76
|
[1] Soummer, et. al. Fast computation of Lyot-style coronagraph
|
|
79
|
-
|
|
77
|
+
propagation (2007)
|
|
80
78
|
"""
|
|
81
79
|
return _dftcore(f, alpha, shape, shift, offset, unitary, out, forward=True)
|
|
82
80
|
|
|
@@ -137,7 +135,6 @@ def _idft2_matrices(m, n, M, N, alphar, alphac, shiftr, shiftc, offsetr, offsetc
|
|
|
137
135
|
return E1, E2
|
|
138
136
|
|
|
139
137
|
|
|
140
|
-
@functools.lru_cache(maxsize=32)
|
|
141
138
|
def _dft2_coords(m, n, M, N):
|
|
142
139
|
# R and S are (r,c) coordinates in the (m x n) input plane f
|
|
143
140
|
# V and U are (r,c) coordinates in the (M x N) output plane F
|