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

Fixes for reference file unit removal #408

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies = [
"numpy >=1.22",
"astropy >=5.3.0",
# "rad >= 0.21.0",
"rad @ git+https://github.com/spacetelescope/rad.git",
"rad @ git+https://github.com/WilliamJamieson/rad.git@feature/remove_ref_units",
"asdf-standard >=1.1.0",
]
dynamic = ["version"]
Expand Down
10 changes: 2 additions & 8 deletions src/roman_datamodels/maker_utils/_common_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,6 @@ def mk_ref_distoriton_meta(**kwargs):
"""
meta = mk_ref_common("DISTORTION", **kwargs)

meta["input_units"] = kwargs.get("input_units", u.pixel)
meta["output_units"] = kwargs.get("output_units", u.arcsec)

return meta


Expand All @@ -700,8 +697,8 @@ def _mk_ref_photometry_meta(**kwargs):
Create the photometry meta data for pixelarea reference files
"""
meta = {}
meta["pixelarea_steradians"] = kwargs.get("pixelarea_steradians", float(NONUM) * u.sr)
meta["pixelarea_arcsecsq"] = kwargs.get("pixelarea_arcsecsq", float(NONUM) * u.arcsec**2)
meta["pixelarea_steradians"] = kwargs.get("pixelarea_steradians", float(NONUM))
meta["pixelarea_arcsecsq"] = kwargs.get("pixelarea_arcsecsq", float(NONUM))

return meta

Expand Down Expand Up @@ -730,9 +727,6 @@ def mk_ref_units_dn_meta(reftype_, **kwargs):
"""
meta = mk_ref_common(reftype_, **kwargs)

meta["input_units"] = kwargs.get("input_units", u.DN)
meta["output_units"] = kwargs.get("output_units", u.DN)

return meta


Expand Down
23 changes: 9 additions & 14 deletions src/roman_datamodels/maker_utils/_ref_files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import warnings

import numpy as np
from astropy import units as u
from astropy.modeling import models

from roman_datamodels import stnode
Expand Down Expand Up @@ -197,14 +196,10 @@ def mk_dark(*, shape=(2, 4096, 4096), filepath=None, **kwargs):
darkref = stnode.DarkRef()
darkref["meta"] = mk_ref_dark_meta(**kwargs.get("meta", {}))

darkref["data"] = kwargs.get("data", u.Quantity(np.zeros(shape, dtype=np.float32), u.DN, dtype=np.float32))
darkref["data"] = kwargs.get("data", np.zeros(shape, dtype=np.float32))
darkref["dq"] = kwargs.get("dq", np.zeros(shape[1:], dtype=np.uint32))
darkref["dark_slope"] = kwargs.get(
"dark_slope", u.Quantity(np.zeros(shape[1:], dtype=np.float32), u.DN / u.s, dtype=np.float32)
)
darkref["dark_slope_error"] = kwargs.get(
"dark_slope_error", u.Quantity(np.zeros(shape[1:], dtype=np.float32), u.DN / u.s, dtype=np.float32)
)
darkref["dark_slope"] = kwargs.get("dark_slope", np.zeros(shape[1:], dtype=np.float32))
darkref["dark_slope_error"] = kwargs.get("dark_slope_error", np.zeros(shape[1:], dtype=np.float32))

return save_node(darkref, filepath=filepath)

Expand Down Expand Up @@ -291,7 +286,7 @@ def mk_gain(*, shape=(4096, 4096), filepath=None, **kwargs):
gainref = stnode.GainRef()
gainref["meta"] = mk_ref_common("GAIN", **kwargs.get("meta", {}))

gainref["data"] = kwargs.get("data", u.Quantity(np.zeros(shape, dtype=np.float32), u.electron / u.DN, dtype=np.float32))
gainref["data"] = kwargs.get("data", np.zeros(shape, dtype=np.float32))

return save_node(gainref, filepath=filepath)

Expand Down Expand Up @@ -464,11 +459,11 @@ def _mk_phot_table_entry(key, **kwargs):
}
else:
entry = {
"photmjsr": kwargs.get("photmjsr", 1.0e-15 * u.megajansky / u.steradian),
"uncertainty": kwargs.get("uncertainty", 1.0e-16 * u.megajansky / u.steradian),
"photmjsr": kwargs.get("photmjsr", 1.0e-15),
"uncertainty": kwargs.get("uncertainty", 1.0e-16),
}

entry["pixelareasr"] = kwargs.get("pixelareasr", 1.0e-13 * u.steradian)
entry["pixelareasr"] = kwargs.get("pixelareasr", 1.0e-13)

return entry

Expand Down Expand Up @@ -528,7 +523,7 @@ def mk_readnoise(*, shape=(4096, 4096), filepath=None, **kwargs):
readnoiseref = stnode.ReadnoiseRef()
readnoiseref["meta"] = mk_ref_readnoise_meta(**kwargs.get("meta", {}))

readnoiseref["data"] = kwargs.get("data", u.Quantity(np.zeros(shape, dtype=np.float32), u.DN, dtype=np.float32))
readnoiseref["data"] = kwargs.get("data", np.zeros(shape, dtype=np.float32))

return save_node(readnoiseref, filepath=filepath)

Expand Down Expand Up @@ -560,7 +555,7 @@ def mk_saturation(*, shape=(4096, 4096), filepath=None, **kwargs):
saturationref["meta"] = mk_ref_common("SATURATION", **kwargs.get("meta", {}))

saturationref["dq"] = kwargs.get("dq", np.zeros(shape, dtype=np.uint32))
saturationref["data"] = kwargs.get("data", u.Quantity(np.zeros(shape, dtype=np.float32), u.DN, dtype=np.float32))
saturationref["data"] = kwargs.get("data", np.zeros(shape, dtype=np.float32))

return save_node(saturationref, filepath=filepath)

Expand Down
25 changes: 0 additions & 25 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,7 @@ def test_make_dark():
assert dark.data.dtype == np.float32
assert dark.dq.dtype == np.uint32
assert dark.dq.shape == (8, 8)
assert dark.data.unit == u.DN
assert dark.dark_slope.dtype == np.float32
assert dark.dark_slope.unit == u.DN / u.s
assert dark.dark_slope_error.dtype == np.float32
assert dark.dark_slope_error.shape == (8, 8)

Expand All @@ -427,8 +425,6 @@ def test_make_dark():
def test_make_distortion():
distortion = utils.mk_distortion()
assert distortion.meta.reftype == "DISTORTION"
assert distortion["meta"]["input_units"] == u.pixel
assert distortion["meta"]["output_units"] == u.arcsec
assert isinstance(distortion["coordinate_distortion_transform"], Model)

# Test validation
Expand Down Expand Up @@ -456,7 +452,6 @@ def test_make_gain():
gain = utils.mk_gain(shape=(8, 8))
assert gain.meta.reftype == "GAIN"
assert gain.data.dtype == np.float32
assert gain.data.unit == u.electron / u.DN

# Test validation
gain_model = datamodels.GainRefModel(gain)
Expand Down Expand Up @@ -515,8 +510,6 @@ def test_make_mask():
def test_make_pixelarea():
pixearea = utils.mk_pixelarea(shape=(8, 8))
assert pixearea.meta.reftype == "AREA"
assert isinstance(pixearea.meta.photometry.pixelarea_steradians, u.Quantity)
assert isinstance(pixearea.meta.photometry.pixelarea_arcsecsq, u.Quantity)
assert pixearea.data.dtype == np.float32

# Test validation
Expand All @@ -529,7 +522,6 @@ def test_make_readnoise():
readnoise = utils.mk_readnoise(shape=(8, 8))
assert readnoise.meta.reftype == "READNOISE"
assert readnoise.data.dtype == np.float32
assert readnoise.data.unit == u.DN

# Test validation
readnoise_model = datamodels.ReadnoiseRefModel(readnoise)
Expand Down Expand Up @@ -575,7 +567,6 @@ def test_make_saturation():
assert saturation.meta.reftype == "SATURATION"
assert saturation.dq.dtype == np.uint32
assert saturation.data.dtype == np.float32
assert saturation.data.unit == u.DN

# Test validation
saturation_model = datamodels.SaturationRefModel(saturation)
Expand Down Expand Up @@ -609,30 +600,15 @@ def test_make_refpix():
assert refpix.zeta.shape == (8, 8)
assert refpix.alpha.shape == (8, 8)

assert refpix.meta.input_units == u.DN
assert refpix.meta.output_units == u.DN


# WFI Photom tests
def test_make_wfi_img_photom():
wfi_img_photom = utils.mk_wfi_img_photom()

assert wfi_img_photom.meta.reftype == "PHOTOM"
assert isinstance(wfi_img_photom.phot_table.F146.photmjsr, u.Quantity)
assert wfi_img_photom.phot_table.F146.photmjsr.unit == u.megajansky / u.steradian
assert isinstance(wfi_img_photom.phot_table.F184.photmjsr, u.Quantity)

assert isinstance(wfi_img_photom.phot_table.F146.uncertainty, u.Quantity)
assert isinstance(wfi_img_photom.phot_table.F184.uncertainty, u.Quantity)
assert wfi_img_photom.phot_table.F184.uncertainty.unit == u.megajansky / u.steradian

assert isinstance(wfi_img_photom.phot_table.F184.pixelareasr, u.Quantity)
assert isinstance(wfi_img_photom.phot_table.F146.pixelareasr, u.Quantity)
assert wfi_img_photom.phot_table.GRISM.pixelareasr.unit == u.steradian

assert wfi_img_photom.phot_table.PRISM.photmjsr is None
assert wfi_img_photom.phot_table.PRISM.uncertainty is None
assert isinstance(wfi_img_photom.phot_table.PRISM.pixelareasr, u.Quantity)

# Test validation
wfi_img_photom_model = datamodels.WfiImgPhotomRefModel(wfi_img_photom)
Expand Down Expand Up @@ -761,7 +737,6 @@ def test_make_fps():
fps = utils.mk_fps(shape=shape)

assert fps.data.dtype == np.uint16
assert fps.data.unit == u.DN

# Test validation
fps_model = datamodels.FpsModel(fps)
Expand Down
29 changes: 6 additions & 23 deletions tests/test_stnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from contextlib import nullcontext

import asdf
import astropy.units as u
import pytest
from asdf.exceptions import ValidationError

Expand Down Expand Up @@ -171,34 +170,18 @@ def test_set_pattern_properties():
# This model uses includes a patternProperty
mdl = maker_utils.mk_wfi_img_photom()

# This should be invalid because it is not a quantity
with pytest.raises(asdf.ValidationError):
mdl.phot_table.F062.photmjsr = 3.14
with pytest.raises(asdf.ValidationError):
mdl.phot_table.F062.uncertainty = 3.14
with pytest.raises(asdf.ValidationError):
mdl.phot_table.F062.pixelareasr = 3.14

# This is invalid because it is not a scalar
with pytest.raises(asdf.ValidationError):
mdl.phot_table.F062.photmjsr = [37.0] * (u.MJy / u.sr)
with pytest.raises(asdf.ValidationError):
mdl.phot_table.F062.uncertainty = [37.0] * (u.MJy / u.sr)
with pytest.raises(asdf.ValidationError):
mdl.phot_table.F062.pixelareasr = [37.0] * u.sr

# This should be invalid because it has the wrong unit
with pytest.raises(asdf.ValidationError):
mdl.phot_table.F062.photmjsr = 3.14 * u.m
mdl.phot_table.F062.photmjsr = [37.0]
with pytest.raises(asdf.ValidationError):
mdl.phot_table.F062.uncertainty = 3.14 * u.m
mdl.phot_table.F062.uncertainty = [37.0]
with pytest.raises(asdf.ValidationError):
mdl.phot_table.F062.pixelareasr = 3.14 * u.m
mdl.phot_table.F062.pixelareasr = [37.0]

# Test some valid values (including the rest of the patternProperties)
mdl.phot_table.F062.photmjsr = 3.14 * (u.MJy / u.sr)
mdl.phot_table.F062.uncertainty = 0.1 * (u.MJy / u.sr)
mdl.phot_table.F062.pixelareasr = 37.0 * u.sr
mdl.phot_table.F062.photmjsr = 3.14
mdl.phot_table.F062.uncertainty = 0.1
mdl.phot_table.F062.pixelareasr = 37.0

# Test it can be None (including the rest of the patternProperties)
mdl.phot_table.F062.photmjsr = None
Expand Down
Loading