Skip to content

Commit

Permalink
Merge pull request #459 from AstarVienna/kl/round_psfs
Browse files Browse the repository at this point in the history
Added function to round the edges of square PSF kernels
  • Loading branch information
astronomyk authored Aug 27, 2024
2 parents 2d11b6a + 1a4b2e9 commit f995db0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
15 changes: 15 additions & 0 deletions scopesim/effects/psfs/psf_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, **kwargs):
"bkg_width": -1,
"wave_key": "WAVE0",
"normalise_kernel": True,
"rounded_edges": True,
"rotational_blur_angle": 0,
"report_plot_include": True,
"report_table_include": False,
Expand Down Expand Up @@ -70,6 +71,11 @@ def apply_to(self, obj, **kwargs):
# makes a copy of kernel
kernel = rotational_blur(kernel, rot_blur_angle)

# Round the edges of kernels so that the silly square stars
# don't appear anymore
if self.meta.get("rounded_edges", False) and kernel.ndim == 2:
kernel = self._round_kernel_edges(kernel)

# normalise psf kernel KERNEL SHOULD BE normalised within get_kernel()
# if from_currsys(self.meta["normalise_kernel"], self.cmds):
# kernel /= np.sum(kernel)
Expand Down Expand Up @@ -115,6 +121,15 @@ def get_kernel(self, obj):
self.kernel = np.ones((1, 1))
return self.kernel

@staticmethod
def _round_kernel_edges(kernel: np.ndarray) -> np.ndarray:
x, y = np.array(kernel.shape) // 2
threshold = min(kernel[x, 0], kernel[x, -1],
kernel[0, y], kernel[-1, y])
kernel[kernel < threshold] = 0.
# TODO: maybe masked array here?
return kernel

def plot(self, obj=None, **kwargs):
fig, axes = figure_factory()

Expand Down
14 changes: 6 additions & 8 deletions scopesim/effects/psfs/semianalytical.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""Currently only contains the AnisoCADO connection."""
from warnings import warn

import numpy as np
from scipy.interpolate import RectBivariateSpline
Expand Down Expand Up @@ -121,8 +122,6 @@ def get_kernel(self, fov):

self._kernel = self._psf_object.psf_latest
self._kernel /= np.sum(self._kernel)
if self.meta["rounded_edges"]:
self._round_kernel_edges()

return self._kernel

Expand All @@ -136,15 +135,14 @@ def remake_kernel(self, x):
[um] if float
"""
warn("The 'remake_kernel' method was unused and thus deprecated and "
"will be removed in a future release. If you are using this "
"method, pleas let us know by creating an issue at: "
"https://github.com/AstarVienna/ScopeSim/issues",
DeprecationWarning, stacklevel=2)
self._kernel = None
return self.get_kernel(x)

def _round_kernel_edges(self):
y, x = np.array(self._kernel.shape).astype(int) // 2
threshold = np.min([self._kernel[y, 0], self._kernel[y, -1],
self._kernel[0, x], self._kernel[-1, x]])
self._kernel[self._kernel < threshold] = 0.

@property
def wavelength(self):
# FIXME: expensive property...
Expand Down
6 changes: 3 additions & 3 deletions scopesim/tests/tests_effects/test_PSF.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def test_returns_rotated_kernel_array_has_same_sum(self, angle):

# With blur
implane = basic_image_plane()
implane.data[75,75] = 1
implane.data[75, 75] = 1

psf = PSF(rotational_blur_angle=angle)
psf = PSF(rotational_blur_angle=angle, rounded_edges=False)
psf.kernel = basic_kernel()
implane = psf.apply_to(implane)

Expand Down Expand Up @@ -157,7 +157,7 @@ def test_convolves_with_3D_cube(self):
plt.subplot(231)
plt.imshow(implane.data[1, :, :])

psf = PSF(rotational_blur_angle=15, bkg_width=5)
psf = PSF(rotational_blur_angle=15, bkg_width=5, rounded_edges=False)
psf.kernel = basic_kernel(n=63)
implane = psf.apply_to(implane)

Expand Down

0 comments on commit f995db0

Please sign in to comment.