-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted background removal from daophot and removebackground into i…
…ts own class
- Loading branch information
1 parent
bd6e3f6
commit cdd819e
Showing
6 changed files
with
119 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import Tuple | ||
|
||
import numpy as np | ||
from astropy.stats import SigmaClip | ||
|
||
from pyobs.images import Image | ||
|
||
|
||
class _DaoBackgroundRemover: | ||
def __init__(self, sigma: float, box_size: Tuple[int, int], filter_size: Tuple[int, int]): | ||
from photutils.background import MedianBackground | ||
|
||
self._sigma_clip = SigmaClip(sigma=sigma) | ||
self._box_size = box_size | ||
self._filter_size = filter_size | ||
|
||
self._bkg_estimator = MedianBackground() | ||
|
||
def __call__(self, image: Image) -> Image: | ||
background = self._estimate_background(image) | ||
return self._remove_background(image, background) | ||
|
||
def _estimate_background(self, image: Image): | ||
from photutils.background import Background2D | ||
|
||
bkg = Background2D( | ||
image.data, box_size=self._box_size, filter_size=self._filter_size, sigma_clip=self._sigma_clip, | ||
bkg_estimator=self._bkg_estimator, mask=image.mask | ||
) | ||
|
||
return bkg.background | ||
|
||
@staticmethod | ||
def _remove_background(image: Image, background: np.ndarray) -> Image: | ||
output_image = image.copy() | ||
output_image.data = output_image.data - background | ||
return output_image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import numpy as np | ||
import photutils.background | ||
|
||
from pyobs.images import Image | ||
from pyobs.images.processors._daobackgroundremover import _DaoBackgroundRemover | ||
|
||
|
||
def test_init(): | ||
sigma = 1.0 | ||
box_size = (10, 10) | ||
filter_size = (3, 3) | ||
remover = _DaoBackgroundRemover(sigma, box_size, filter_size) | ||
|
||
assert remover._sigma_clip.sigma == sigma | ||
assert remover._box_size == box_size | ||
assert remover._filter_size == filter_size | ||
|
||
|
||
def test_estimate_background_background2d_call(mocker): | ||
sigma = 3.0 | ||
box_size = (1, 1) | ||
filter_size = (3, 3) | ||
spy = mocker.spy(photutils.background.Background2D, "__init__") | ||
remover = _DaoBackgroundRemover(sigma, box_size, filter_size) | ||
|
||
data = np.ones((20, 20)) | ||
mask = np.zeros((20, 20)).astype(bool) | ||
image = Image(data=data, mask=mask) | ||
remover._estimate_background(image) | ||
spy.assert_called_once() | ||
|
||
args = spy.call_args[0] | ||
np.testing.assert_array_equal(args[1], data) | ||
kwargs = spy.call_args.kwargs | ||
assert kwargs["box_size"] == box_size | ||
assert kwargs["filter_size"] == filter_size | ||
assert kwargs["sigma_clip"].sigma == sigma | ||
assert kwargs["bkg_estimator"] == remover._bkg_estimator | ||
np.testing.assert_array_equal(kwargs["mask"], mask) | ||
|
||
|
||
def test_call_const_background(mocker): | ||
sigma = 3.0 | ||
box_size = (1, 1) | ||
filter_size = (3, 3) | ||
|
||
remover = _DaoBackgroundRemover(sigma, box_size, filter_size) | ||
|
||
image = Image(data=np.ones((20, 20))) | ||
output_image = remover(image) | ||
|
||
np.testing.assert_array_equal(output_image.data, np.zeros((20, 20))) |