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

Fix distortion reffile schema and unit test #5553

Merged
merged 3 commits into from
Jan 4, 2021
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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ datamodels
- Update Moving Target CHEBY table extension schema for changes to column
definitions in the JWSTKD and SDP [#5558]

- Update distortion reference file schema to have ``meta.instrument.channel``
keyword [#5553]

extract_1d
----------

Expand Down
91 changes: 81 additions & 10 deletions jwst/assign_wcs/tests/test_schemas.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import inspect
import sys

from astropy.modeling import models
from astropy import units as u
from jwst.datamodels import DistortionModel
import pytest

from jwst.datamodels import DistortionModel, ReferenceFileModel
from jwst.datamodels import wcs_ref_models
from jwst.datamodels.wcs_ref_models import _SimpleModel

def test_distortion_schema(tmpdir):
"""Make sure DistortionModel roundtrips"""

def find_all_wcs_ref_models_classes():
clsmembers = inspect.getmembers(sys.modules[wcs_ref_models.__name__], inspect.isclass)
classes = [cls for name,cls in clsmembers if issubclass(cls, ReferenceFileModel)]
classes.remove(_SimpleModel)
return classes


@pytest.fixture
def distortion_model():
"""Create a distortion model that should pass all validation"""
m = models.Shift(1) & models.Shift(2)
dist = DistortionModel(model=m, input_units=u.pixel, output_units=u.arcsec)

dist.meta.reftype = "distortion"
dist.meta.instrument.name = "NIRCAM"
dist.meta.instrument.detector = "NRCA1"
dist.meta.instrument.p_pupil = "F162M|F164N|CLEAR|"
Expand All @@ -16,13 +32,68 @@ def test_distortion_schema(tmpdir):
dist.meta.exposure.type = "NRC_IMAGE"
dist.meta.psubarray = "FULL|SUB64P|SUB160)|SUB160P|SUB320|SUB400P|SUB640|"
dist.meta.subarray.name = "FULL"

# Populate the following so that no validation warnings or errors happen
dist.meta.instrument.module = "A"
dist.meta.instrument.channel = "SHORT"
dist.meta.input_units = u.degree
dist.meta.output_units = u.degree
dist.meta.description = "NIRCam distortion reference file"
dist.meta.author = "Hank the Septopus"
dist.meta.pedigree = "Cleveland"
dist.meta.useafter = "2000-01-01T00:00:00"

return dist


def test_distortion_schema(distortion_model, tmpdir):
"""Make sure DistortionModel roundtrips"""
path = str(tmpdir.join("test_dist.asdf"))
dist = distortion_model
dist.save(path)

with DistortionModel(path) as dist1:
assert dist1.meta.instrument.p_pupil == dist.meta.instrument.p_pupil
assert dist1.meta.instrument.pupil == dist.meta.instrument.pupil
assert dist1.meta.exposure.p_exptype == dist.meta.exposure.p_exptype
assert dist1.meta.exposure.type == dist.meta.exposure.type
assert dist1.meta.psubarray == dist.meta.psubarray
assert dist1.meta.subarray.name == dist.meta.subarray.name
with pytest.warns(None) as report:
with DistortionModel(path) as dist1:
assert dist1.meta.instrument.p_pupil == dist.meta.instrument.p_pupil
assert dist1.meta.instrument.pupil == dist.meta.instrument.pupil
assert dist1.meta.exposure.p_exptype == dist.meta.exposure.p_exptype
assert dist1.meta.exposure.type == dist.meta.exposure.type
assert dist1.meta.psubarray == dist.meta.psubarray
assert dist1.meta.subarray.name == dist.meta.subarray.name
assert len(report) == 0


def test_distortion_strict_validation(distortion_model):
"""Make sure strict validation works"""
distortion_model.validate()


def test_distortion_schema_bad_valueerror(distortion_model):
"""Check that ValueError is raised for ReferenceFile missing items"""
dist = DistortionModel(distortion_model, strict_validation=True)
dist.meta.author = None

with pytest.raises(ValueError):
dist.validate()


def test_distortion_schema_bad_assertionerror(distortion_model):
"""Check that AssertionError is raised for distortion-specific missing items"""
dist = DistortionModel(distortion_model, strict_validation=True)
dist.meta.instrument.channel = None

with pytest.raises(AssertionError):
dist.validate()


@pytest.mark.parametrize("cls", find_all_wcs_ref_models_classes())
def test_simplemodel_subclasses(cls):
"""Test that expected validation errors are raised"""
model = cls()
with pytest.warns(None) as report:
model.validate()
assert len(report) >= 1

model = cls(strict_validation=True)
with pytest.raises((ValueError, KeyError)):
model.validate()
1 change: 1 addition & 0 deletions jwst/datamodels/schemas/distortion.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ allOf:
- $ref: keyword_pupil.schema
- $ref: keyword_ppupil.schema
- $ref: keyword_module.schema
- $ref: keyword_channel.schema
- $ref: keyword_exptype.schema
- $ref: keyword_pexptype.schema
- $ref: subarray.schema
Expand Down
Loading