Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up Camera-related tests and add a name property #253

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 31 additions & 48 deletions stellarphot/photometry/tests/test_photometry.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather like this approach of having a ZERO_CAMERA and then just copying it and changing the attributes. Cleaner test code!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😀 thanks!

Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,28 @@

SHIFT_TOLERANCE = 6
FWHM_ESTIMATE = 5

# A camera with not unreasonable settings
FAKE_CAMERA = Camera(
data_unit=u.adu,
gain=1.0 * u.electron / u.adu,
name="test camera",
read_noise=0 * u.electron,
dark_current=0.1 * u.electron / u.second,
pixel_scale=1 * u.arcsec / u.pixel,
max_data_value=40000 * u.adu,
)

# Camera with no read noise or dark currnet
ZERO_CAMERA = Camera(
data_unit=u.adu,
gain=1.0 * u.electron / u.adu,
name="test camera",
read_noise=0 * u.electron,
dark_current=0.0 * u.electron / u.second,
pixel_scale=1 * u.arcsec / u.pixel,
max_data_value=40000 * u.adu,
)
FAKE_OBS = EarthLocation(lat=0 * u.deg, lon=0 * u.deg, height=0 * u.m)
COORDS2USE = "pixel"

Expand All @@ -57,14 +71,8 @@ def test_calc_noise_source_only(gain, aperture_area):
expected = np.sqrt(gain * counts)

# Create camera instance
camera = Camera(
data_unit=u.adu,
gain=gain * u.electron / u.adu,
read_noise=0 * u.electron,
dark_current=0 * u.electron / u.second,
pixel_scale=1 * u.arcsec / u.pixel,
max_data_value=40000 * u.adu,
)
camera = ZERO_CAMERA.copy()
camera.gain = gain * camera.gain.unit

np.testing.assert_allclose(
calculate_noise(camera, counts=counts, aperture_area=aperture_area), expected
Expand All @@ -80,14 +88,10 @@ def test_calc_noise_dark_only(gain, aperture_area):
exposure = 20

# Create camera instance
camera = Camera(
data_unit=u.adu,
gain=gain * u.electron / u.adu,
read_noise=0 * u.electron,
dark_current=dark_current * u.electron / u.second,
pixel_scale=1 * u.arcsec / u.pixel,
max_data_value=40000 * u.adu,
)
camera = ZERO_CAMERA.copy()
# Set gain and dark current to values for test
camera.dark_current = dark_current * camera.dark_current.unit
camera.gain = gain * camera.gain.unit

expected = np.sqrt(dark_current * aperture_area * exposure)

Expand All @@ -106,14 +110,9 @@ def test_calc_read_noise_only(gain, aperture_area):
expected = np.sqrt(aperture_area * read_noise**2)

# Create camera instance
camera = Camera(
data_unit=u.adu,
gain=gain * u.electron / u.adu,
read_noise=read_noise * u.electron,
dark_current=0 * u.electron / u.second,
pixel_scale=1 * u.arcsec / u.pixel,
max_data_value=40000 * u.adu,
)
camera = ZERO_CAMERA.copy()
camera.read_noise = read_noise * camera.read_noise.unit
camera.gain = gain * camera.gain.unit

np.testing.assert_allclose(
calculate_noise(camera, aperture_area=aperture_area), expected
Expand All @@ -128,14 +127,8 @@ def test_calc_sky_only(gain, aperture_area):
expected = np.sqrt(gain * aperture_area * sky)

# Create camera instance
camera = Camera(
data_unit=u.adu,
gain=gain * u.electron / u.adu,
read_noise=0 * u.electron,
dark_current=0 * u.electron / u.second,
pixel_scale=1 * u.arcsec / u.pixel,
max_data_value=40000 * u.adu,
)
camera = ZERO_CAMERA.copy()
camera.gain = gain * camera.gain.unit

np.testing.assert_allclose(
calculate_noise(camera, aperture_area=aperture_area, sky_per_pix=sky), expected
Expand All @@ -153,14 +146,8 @@ def test_annulus_area_term():
expected = np.sqrt(gain * aperture_area * (1 + aperture_area / annulus_area) * sky)

# Create camera instance
camera = Camera(
data_unit=u.adu,
gain=gain * u.electron / u.adu,
read_noise=0 * u.electron,
dark_current=0 * u.electron / u.second,
pixel_scale=1 * u.arcsec / u.pixel,
max_data_value=40000 * u.adu,
)
camera = ZERO_CAMERA.copy()
camera.gain = gain * camera.gain.unit

np.testing.assert_allclose(
calculate_noise(
Expand Down Expand Up @@ -189,14 +176,10 @@ def test_calc_noise_messy_case(digit, expected):
read_noise = 12

# Create camera instance
camera = Camera(
data_unit=u.adu,
gain=gain * u.electron / u.adu,
read_noise=read_noise * u.electron,
dark_current=dark_current * u.electron / u.second,
pixel_scale=1 * u.arcsec / u.pixel,
max_data_value=40000 * u.adu,
)
camera = ZERO_CAMERA.copy()
camera.gain = gain * camera.gain.unit
camera.dark_current = dark_current * camera.dark_current.unit
camera.read_noise = read_noise * camera.read_noise.unit

np.testing.assert_allclose(
calculate_noise(
Expand Down
12 changes: 12 additions & 0 deletions stellarphot/settings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class Camera(BaseModel):
The gain of the camera in units such the product of `gain`
times the image data has units equal to that of the `read_noise`.

name : str
The name of the camera; can be anything that helps the user identify
the camera.

read_noise : `astropy.units.Quantity`
The read noise of the camera with units.

Expand All @@ -53,6 +57,10 @@ class Camera(BaseModel):
The gain of the camera in units such the product of `gain`
times the image data has units equal to that of the `read_noise`.

name : str
The name of the camera; can be anything that helps the user identify
the camera.

read_noise : `astropy.units.Quantity`
The read noise of the camera with units.

Expand All @@ -78,6 +86,7 @@ class Camera(BaseModel):
>>> from stellarphot.settings import Camera
>>> camera = Camera(data_unit="adu",
... gain=1.0 * u.electron / u.adu,
... name="test camera",
... read_noise=1.0 * u.electron,
... dark_current=0.01 * u.electron / u.second,
... pixel_scale=0.563 * u.arcsec / u.pixel,
Expand All @@ -86,6 +95,8 @@ class Camera(BaseModel):
Unit("adu")
>>> camera.gain
<Quantity 1. electron / adu>
>>> camera.name
'test camera'
>>> camera.read_noise
<Quantity 1. electron>
>>> camera.dark_current
Expand All @@ -103,6 +114,7 @@ class Camera(BaseModel):
description="unit should be consistent with data and read noise",
examples=["1.0 electron / adu"],
)
name: str
read_noise: QuantityType = Field(
description="unit should be consistent with dark current",
examples=["10.0 electron"],
Expand Down
Loading