svank / remove_starfield

Python code for estimating and removing a starfield from a set of celestial images
https://svank.github.io/remove_starfield/
GNU General Public License v3.0
2 stars 2 forks source link

Getting NANs at boundaries #6

Open s0larish opened 4 months ago

s0larish commented 4 months ago

Test was failing again after the update. However, when boundary_mode='ignore' was included in line 283 of starfield.py, the tests passed with flying colors.

svank commented 4 months ago

What problem are you having? And how does this change things? That boundary mode isn't having any effect for me.

s0larish commented 4 months ago

We are using the following test for our module:

`import unittest import os import pathlib from glob import glob from pkg_resources import iter_entry_points

Third party imports

import numpy as np import pytest from astropy.wcs import WCS from remove_starfield import Starfield

punchbowl imports

from punchbowl.data import NormalizedMetadata, PUNCHData from punchbowl.level3.starfield_remove import ( generate_starfield_background, subtract_starfield_background, subtract_starfield_background_task)

@pytest.fixture() def one_data(shape: tuple = (128, 128)) -> PUNCHData: """ Generate some random data for testing """ data = np.ones(shape)

wcs = WCS(naxis=2)
wcs.wcs.ctype = "HPLN-AZP", "HPLT-AZP"
wcs.wcs.cunit = "deg", "deg"
wcs.wcs.cdelt = 0.02, 0.02
wcs.wcs.crpix = 1024, 1024
wcs.wcs.crval = 0, 24.75

meta = NormalizedMetadata(
    {"TYPECODE": "CS", "OBSCODE": "M", "LEVEL": "3", "OBSRVTRY": "0", "DATE-OBS": "2024-04-08T18:40:00"})
return PUNCHData(data=data, wcs=wcs, meta=meta)

@pytest.fixture() def zero_starfield_data(shape: tuple = (256, 256)) -> Starfield: """ Generate some random data for testing """ starfield = np.zeros(shape)

wcs = WCS(naxis=2)
wcs.wcs.ctype = "HPLN-AZP", "HPLT-AZP"
wcs.wcs.cunit = "deg", "deg"
wcs.wcs.cdelt = 0.02, 0.02
wcs.wcs.crpix = 1024, 1024
wcs.wcs.crval = 0, 24.75

# meta = NormalizedMetadata(
#     {"TYPECODE": "CS", "OBSCODE": "M", "LEVEL": "3", "OBSRVTRY": "0", "DATE-OBS": "2024-04-08T18:40:00"})
return Starfield(starfield=starfield, wcs=wcs)

def test_basic_subtraction(one_data: PUNCHData, zero_starfield_data: Starfield) -> None: """

"""
subtraction_starfield = subtract_starfield_background(one_data, zero_starfield_data)
subtraction_punchdata = PUNCHData(data=subtraction_starfield.data, wcs=subtraction_starfield.wcs, meta=subtraction_starfield.meta)
assert isinstance(subtraction_punchdata, PUNCHData)
assert np.allclose(subtraction_punchdata.data, 1)

` and getting the error as in screenshot when the modification is not included in Lin 283 of starfield.py

Screenshot 2024-05-14 at 9 54 43 AM Screenshot 2024-05-14 at 9 57 23 AM

svank commented 4 months ago

Hm, it looks like you're generating a data array with its edges along the edge of the starfield array, and the edges of the starfield come out as NaN from the reprojection because of the boundary mode. Since your image array is already smaller than the starfield array, if you center the image in the starfield (or put it anywhere but right along the edge), you shouldn't get any NaNs.

Changing the boundary mode to ignore would have the effect of sort of blurring out the edge of the starfield by a pixel or two, which would risk mis-representing the photometry for those edge pixels. I don't think this is likely to come up in real-world usage unless the starfield has been set to be too small, but I think leaving the boundary mode as strict is the safer choice.