def test_jwstgwcs_wrong_tpcorr_type(tpcorr, mock_jwst_wcs): tpwcs.TPCorr = tpcorr wc = tpwcs.JWSTgWCS( mock_jwst_wcs, { 'v2_ref': 123.0, 'v3_ref': 500.0, 'roll_ref': 115.0 }, ) wc.set_correction() p = wc.wcs.pipeline np = [(v[0], V2V3ToDet()) if v[0].name == 'v2v3' else v for v in p] mangled_wc = gwcs.wcs.WCS(np) with pytest.raises(ValueError): wc = tpwcs.JWSTgWCS( mangled_wc, { 'v2_ref': 123.0, 'v3_ref': 500.0, 'roll_ref': 115.0 }, )
def test_jwst_wcs_corr_are_being_combined(tpcorr, mock_jwst_wcs): tpwcs.TPCorr = tpcorr wc = tpwcs.JWSTgWCS(mock_jwst_wcs, { 'v2_ref': 123.0, 'v3_ref': 500.0, 'roll_ref': 115.0 }) matrix1 = np.array([[1.0, 0.2], [-0.3, 1.1]]) shift1 = np.array([5.0, -7.0]) wc.set_correction(matrix=matrix1, shift=shift1) assert 'v2v3corr' in wc.wcs.available_frames matrix2 = np.linalg.inv(matrix1) shift2 = -np.dot(matrix1, shift1) wc = tpwcs.JWSTgWCS(wc.wcs, { 'v2_ref': 123.0, 'v3_ref': 500.0, 'roll_ref': 115.0 }) wc.set_correction(matrix=matrix2, shift=shift2) v2v3idx = [ k for k, n in enumerate(wc.wcs.available_frames) if n == 'v2v3corr' ] assert len(v2v3idx) == 1 tp_corr = wc.wcs.pipeline[v2v3idx[0] - 1][1] assert isinstance(tp_corr, tpwcs.TPCorr) assert np.max(np.abs(tp_corr.matrix - np.identity(2))) < _ATOL assert np.max(np.abs(tp_corr.shift)) < _ATOL
def test_jwstgwcs_bad_pipelines_with_vacorr(): p0 = make_mock_jwst_pipeline(v2ref=0.0, v3ref=0.0, roll=0.0, crpix=[500.0, 512.0], cd=[[1.0e-5, 0.0], [0.0, 1.0e-5]], crval=[12.0, 24.0], enable_vacorr=True) # multiple 'v2v3vacorr' frames: w = gwcs.wcs.WCS(p0) w = tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) p = w.wcs.pipeline p.insert(1, p[2]) w = gwcs.wcs.WCS(p) with pytest.raises(ValueError): tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0})
def test_jwstgwcs_inconsistent_ref(mock_jwst_wcs): wc = tpwcs.JWSTgWCS( mock_jwst_wcs, { 'v2_ref': 123.0, 'v3_ref': 500.0, 'roll_ref': 115.0 }, ) wc.set_correction() with pytest.raises(ValueError): wc = tpwcs.JWSTgWCS( wc.wcs, { 'v2_ref': 124.0, 'v3_ref': 500.0, 'roll_ref': 115.0 }, )
def test_jwstgwcs_detector_to_tanp(inputs): w = make_mock_jwst_wcs(v2ref=0.0, v3ref=0.0, roll=0.0, crpix=[500.0, 512.0], cd=[[1.0e-5, 0.0], [0.0, 1.0e-5]], crval=[12.0, 24.0]) wc = tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) wc.set_correction() x, y, tanp_x, tanp_y = inputs assert np.allclose(wc.det_to_tanp(x, y), (tanp_x, tanp_y), atol=10 * _ATOL) assert np.allclose(wc.tanp_to_det(tanp_x, tanp_y), (x, y), atol=_ATOL)
def test_jwstgwcs_ref_angles_preserved(mock_jwst_wcs): wc = tpwcs.JWSTgWCS( mock_jwst_wcs, { 'v2_ref': 123.0, 'v3_ref': 500.0, 'roll_ref': 115.0 }, ) assert wc.ref_angles['v2_ref'] == 123.0 assert wc.ref_angles['v3_ref'] == 500.0 assert wc.ref_angles['roll_ref'] == 115.0
def test_jwstgwcs_tangent_to_world(inputs): w = make_mock_jwst_wcs(v2ref=0.0, v3ref=0.0, roll=0.0, crpix=[500.0, 512.0], cd=[[1.0e-5, 0.0], [0.0, 1.0e-5]], crval=[12.0, 24.0]) wc = tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) wc.set_correction() tanp_x, tanp_y, ra, dec = inputs assert np.allclose(wc.world_to_tanp(ra, dec), (tanp_x, tanp_y), atol=1000 * _ATOL) assert np.allclose(wc.tanp_to_world(tanp_x, tanp_y), (ra, dec), atol=_ATOL)
def test_jwstgwcs_detector_to_world(inputs): for tpcorr in _TPCORRS: tpwcs.TPCorr = tpcorr w = make_mock_jwst_wcs(v2ref=0.0, v3ref=0.0, roll=0.0, crpix=[500.0, 512.0], cd=[[1.0e-5, 0.0], [0.0, 1.0e-5]], crval=[12.0, 24.0]) wc = tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) wc.set_correction() x, y, ra, dec = inputs assert np.allclose(wc.det_to_world(x, y), (ra, dec), atol=_ATOL) assert np.allclose(wc.world_to_det(ra, dec), (x, y), atol=_ATOL)
def test_jwst_import_failed(monkeypatch): dummy_wcs = gwcs.wcs.WCS(models.Identity(2), 'det', 'world') restore_modules = {} for k in list(sys.modules.keys()): if k.startswith(('jwst')): restore_modules[k] = sys.modules[k] # pragma: no cover sys.modules[k] = None # pragma: no cover elif k.startswith('tweakwcs') or 'tpwcs' in k or 'TPCorr' in k: restore_modules[k] = sys.modules[k] del sys.modules[k] from tweakwcs import tpwcs with pytest.raises(ImportError): tpwcs.JWSTgWCS(dummy_wcs, {}) sys.modules.update(restore_modules)
def test_jwstgwcs_coord_transforms(tpcorr): tpwcs.TPCorr = tpcorr w = make_mock_jwst_wcs(v2ref=0.0, v3ref=0.0, roll=0.0, crpix=[500.0, 512.0], cd=[[1.0e-5, 0.0], [0.0, 1.0e-5]], crval=[12.0, 24.0]) wc = tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) wc.set_correction() assert np.allclose(wc.det_to_world(500, 512), (12, 24), atol=_ATOL) assert np.allclose(wc.world_to_det(12, 24), (500, 512), atol=_ATOL) assert np.allclose(wc.det_to_tanp(500, 512), (0, 0), atol=_ATOL) assert np.allclose(wc.tanp_to_det(0, 0), (500, 512), atol=_ATOL) assert np.allclose(wc.world_to_tanp(12, 24), (0, 0), atol=_ATOL) assert np.allclose(wc.tanp_to_world(0, 0), (12, 24), atol=_ATOL)
def test_jwst_wcs_corr_applied(mock_jwst_wcs): w = make_mock_jwst_wcs(v2ref=123.0, v3ref=500.0, roll=115.0, crpix=[512.0, 512.0], cd=[[1.0e-5, 0.0], [0.0, 1.0e-5]], crval=[82.0, 12.0]) wc = tpwcs.JWSTgWCS(w, { 'v2_ref': 123.0, 'v3_ref': 500.0, 'roll_ref': 115.0 }, meta={}) wc.set_correction(meta={'dummy_meta': None}, dummy_par=1) assert 'v2v3corr' in wc.wcs.available_frames assert 'dummy_meta' in wc.meta
def test_jwstgwcs_bbox(): w = make_mock_jwst_wcs(v2ref=0.0, v3ref=0.0, roll=0.0, crpix=[500.0, 512.0], cd=[[1.0e-5, 0.0], [0.0, 1.0e-5]], crval=[12.0, 24.0]) wc = tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) wc.set_correction() assert np.allclose(wc.bounding_box, ((-0.5, 1024 - 0.5), (-0.5, 2048 - 0.5)), atol=_ATOL) wc._owcs.bounding_box = None assert np.allclose(wc.bounding_box, ((-0.5, 1024 - 0.5), (-0.5, 2048 - 0.5)), atol=_ATOL) wc._owcs.array_shape = None assert wc.bounding_box is None
def test_jwstgwcs_bad_pipelines(tpcorr): tpwcs.TPCorr = tpcorr p0 = make_mock_jwst_pipeline(v2ref=0.0, v3ref=0.0, roll=0.0, crpix=[500.0, 512.0], cd=[[1.0e-5, 0.0], [0.0, 1.0e-5]], crval=[12.0, 24.0]) # no pipeline or empty pipeline: with pytest.raises(ValueError): tpwcs.JWSTgWCS(None, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) # fewer than 3 frames: w = gwcs.wcs.WCS(p0[:2]) with pytest.raises(ValueError): tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) # repeated (any one of the) last two frames: w = gwcs.wcs.WCS(p0 + [p0[-1]]) with pytest.raises(ValueError): tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) w = gwcs.wcs.WCS(p0 + [p0[-2]]) with pytest.raises(ValueError): tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) # multiple 'v2v3' frames: w = gwcs.wcs.WCS(p0) w = tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) p = w.wcs.pipeline p.insert(1, p[1]) w = gwcs.wcs.WCS(p) with pytest.raises(ValueError): tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) # misplaced 'v2v3' frame: w = gwcs.wcs.WCS(p0) w = tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) w.set_correction() p = w.wcs.pipeline del p[0] w = gwcs.wcs.WCS(p) with pytest.raises(ValueError): tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) # multiple 'v2v3corr' frame: w = gwcs.wcs.WCS(p0) w = tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) w.set_correction() p = w.wcs.pipeline p.insert(1, p[-2]) w = gwcs.wcs.WCS(p) with pytest.raises(ValueError): tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0}) # misplaced 'v2v3corr' frame: del p[-2] w = gwcs.wcs.WCS(p) with pytest.raises(ValueError): tpwcs.JWSTgWCS(w, {'v2_ref': 0.0, 'v3_ref': 0.0, 'roll_ref': 0.0})
def test_jwstgwcs_unsupported_wcs(): from tweakwcs import tpwcs dummy_wcs = gwcs.wcs.WCS(models.Identity(2), 'det', 'world') with pytest.raises(ValueError): tpwcs.JWSTgWCS(dummy_wcs, {})