Skip to content

Commit

Permalink
Fixes for reference file unit removal
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson committed Jan 13, 2025
1 parent 506b979 commit 6d04ced
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 47 deletions.
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 @@ -693,9 +693,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 @@ -704,8 +701,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 @@ -734,9 +731,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 @@ -406,9 +406,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 @@ -421,8 +419,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 All @@ -449,7 +445,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 @@ -508,8 +503,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 @@ -522,7 +515,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 @@ -569,7 +561,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 @@ -603,30 +594,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 @@ -753,7 +729,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
27 changes: 27 additions & 0 deletions tests/test_stnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,33 @@ def test_schema_info():
}


def test_set_pattern_properties():
"""
Regression test for patternProperties not being validated
"""

# This model uses includes a patternProperty
mdl = maker_utils.mk_wfi_img_photom()

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

# Test some valid values (including the rest of the patternProperties)
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
mdl.phot_table.F062.uncertainty = None
mdl.phot_table.F062.pixelareasr = None


# Test that a currently undefined attribute can be assigned using dot notation
# so long as the attribute is defined in the corresponding schema.
def test_node_new_attribute_assignment():
Expand Down

0 comments on commit 6d04ced

Please sign in to comment.