Skip to content

Commit

Permalink
Merge 00190de into 5030ee9
Browse files Browse the repository at this point in the history
  • Loading branch information
robelgeda committed Sep 18, 2019
2 parents 5030ee9 + 00190de commit 4cbe4f9
Show file tree
Hide file tree
Showing 3 changed files with 319 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ install:

before_script:
# Get WebbPSF data files (just a subset of the full 250 MB!) and set up environment variable
- wget https://stsci.box.com/shared/static/8j5svnqr6r7drwyvssg58ne7gt440b3p.gz -O /tmp/minimal-webbpsf-data.tar.gz
- wget https://stsci.box.com/shared/static/1lp219kcya4paab0epyp1g94rg9tzsn8.gz -O /tmp/minimal-webbpsf-data.tar.gz
- tar -xzvf /tmp/minimal-webbpsf-data.tar.gz
- export WEBBPSF_PATH=$PWD/webbpsf-data

Expand Down
120 changes: 99 additions & 21 deletions webbpsf/tests/test_wfirst.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os
import pytest
from webbpsf import wfirst
from numpy import allclose

MASKED_FLAG = "COLD_PUPIL"
UNMASKED_FLAG = "UNMASKED"
AUTO_FLAG = "AUTO"

def test_WFI_psf():
"""
Expand All @@ -20,7 +24,6 @@ def test_WFI_filters():
wi.filter = filter
wi.calc_psf(fov_pixels=4, oversample=1, nlambda=3)


def test_aberration_detector_position_setter():
detector = wfirst.FieldDependentAberration(4096, 4096)

Expand All @@ -46,6 +49,65 @@ def test_aberration_detector_position_setter():
assert detector._field_position == valid_pos, 'Setting field position through setter did not ' \
'update private `_field_position` value'

def test_WFI_pupil_controller():
wfi = wfirst.WFI()

for detector in wfi.detector_list:
wfi.detector = detector

detector_cropped = detector[:3] + str(int((detector[3:]))) # example "SCA01" -> "SCA1"

unmasked_pupil_path = os.path.join(wfi._pupil_controller._pupil_basepath,
'{}_rim_mask.fits.gz'.format(detector_cropped))

masked_pupil_path = os.path.join(wfi._pupil_controller._pupil_basepath,
'{}_full_mask.fits.gz'.format(detector_cropped))

assert os.path.isfile(unmasked_pupil_path), "Pupil file missing {}".format(unmasked_pupil_path)
assert os.path.isfile(masked_pupil_path), "Pupil file missing {}".format(masked_pupil_path)

# Test detector change was successful
assert wfi.detector == detector, "WFI detector was not set correctly"
assert wfi._unmasked_pupil_path == unmasked_pupil_path, "unmasked_pupil_path was not set correctly"
assert wfi._masked_pupil_path == masked_pupil_path, "masked_pupil_path was not set correctly"
assert wfi.pupil in [unmasked_pupil_path, masked_pupil_path], "pupil was not set correctly"

# Test mask overriding
wfi.pupil_mask = MASKED_FLAG
assert wfi.pupil == masked_pupil_path, "pupil was not set correctly"
assert wfi._pupil_controller.auto_pupil is False, "auto_pupil is active after user override"
assert wfi._pupil_controller._pupil_mask == wfi.pupil_mask, "pupil mask was not set correctly"

wfi.pupil_mask = UNMASKED_FLAG
assert wfi.pupil == unmasked_pupil_path, "pupil was not set correctly"
assert wfi._pupil_controller.auto_pupil is False, "auto_pupil is active after user override"
assert wfi._pupil_controller._pupil_mask == wfi.pupil_mask, "pupil mask was not set correctly"

wfi.pupil_mask = AUTO_FLAG
assert wfi._pupil_controller.auto_pupil is True, "auto_pupil is inactive after mask is set to AUTO"
assert wfi._pupil_controller._pupil_mask == wfi.pupil_mask, "pupil mask was not set correctly"

# Test filters
for filter in wfi.filter_list:
wfi.filter = filter
if filter in wfi._pupil_controller._masked_filters:
assert wfi.pupil == masked_pupil_path, \
"Pupil did not set to correct value according to filter {}".format(filter)
else:
assert wfi.pupil == unmasked_pupil_path, \
"Pupil did not set to correct value according to filter {}".format(filter)

# Test calculating a single PSF
wfi = wfirst.WFI()
wfi.detector = detector
valid_pos = (4000, 1000)
wfi.detector_position = valid_pos
wfi.pupil_mask = "COLD_PUPIL"
assert wfi.pupil == masked_pupil_path, "Pupil did not set to correct value according to override"
wfi.calc_psf(fov_pixels=4)
assert wfi.pupil == masked_pupil_path, "Pupil did not set to correct value according to override"


def test_WFI_detector_position_setter():
wfi = wfirst.WFI()
wfi.detector = 'SCA01'
Expand Down Expand Up @@ -74,56 +136,72 @@ def autopupil():
"""Helper to trigger pupil selection in testing"""
wavelengths, _ = wfi._get_weights()
wfi._validate_config(wavelengths=wavelengths)
wfi.filter = 'Z087'
wfi.filter = 'F087'
autopupil()
assert wfi.pupil == wfi._unmasked_pupil_path, "WFI did not select unmasked pupil for Z087"
wfi.filter = 'H158'
assert wfi.pupil == wfi._unmasked_pupil_path, "WFI did not select unmasked pupil for F087"
wfi.filter = 'F184'
autopupil()
assert wfi.pupil == wfi._masked_pupil_path, "WFI did not select masked pupil for H158"
wfi.filter = 'Z087'
assert wfi.pupil == wfi._masked_pupil_path, "WFI did not select masked pupil for F158"
wfi.filter = 'F087'
autopupil()
assert wfi.pupil == wfi._unmasked_pupil_path, "WFI did not re-select unmasked pupil for Z087"
assert wfi.pupil == wfi._unmasked_pupil_path, "WFI did not re-select unmasked pupil for F087"

def _test_filter_pupil(filter_name, expected_pupil):
wfi.filter = 'Z087'
wfi.filter = 'F087'
autopupil()
wfi.filter = filter_name
autopupil()
assert wfi.pupil == expected_pupil, "Expected pupil {} " \
"for filter {}".format(filter_name, expected_pupil)

_test_filter_pupil('Y106', wfi._unmasked_pupil_path)
_test_filter_pupil('J129', wfi._unmasked_pupil_path)
_test_filter_pupil('R062', wfi._unmasked_pupil_path)
_test_filter_pupil('F106', wfi._unmasked_pupil_path)
_test_filter_pupil('F129', wfi._unmasked_pupil_path)
_test_filter_pupil('F062', wfi._unmasked_pupil_path)
_test_filter_pupil('F158', wfi._unmasked_pupil_path)
_test_filter_pupil('F146', wfi._unmasked_pupil_path)

_test_filter_pupil('H158', wfi._masked_pupil_path)
_test_filter_pupil('F184', wfi._masked_pupil_path)
_test_filter_pupil('W149', wfi._masked_pupil_path)

def test_WFI_limits_interpolation_range():
wfi = wfirst.WFI()
det = wfi._detectors['SCA01']
det.get_aberration_terms(1.29e-6)
det.field_position = (0, 0)
with pytest.raises(RuntimeError) as excinfo:
det.get_aberration_terms(1.29e-6)
assert 'out-of-bounds field point' in str(excinfo.value), (
det.get_aberration_terms(1.29e-6)

with pytest.raises(ValueError) as excinfo:
det.field_position = (500000, 0)
assert 'Requested pixel_x position' in str(excinfo.value), (
"FieldDependentAberration did not error on out-of-bounds field point"
)
with pytest.raises(RuntimeError) as excinfo:
det.get_aberration_terms(1.29e-6)
assert 'out-of-bounds field point' in str(excinfo.value), (

with pytest.raises(ValueError) as excinfo:
det.field_position = (-1, 0)
assert 'Requested pixel_x position' in str(excinfo.value), (
"FieldDependentAberration did not error on out-of-bounds field point"
)

with pytest.raises(ValueError) as excinfo:
det.field_position = (0, 500000)
assert 'Requested pixel_y position' in str(excinfo.value), (
"FieldDependentAberration did not error on out-of-bounds field point"
)

with pytest.raises(ValueError) as excinfo:
det.field_position = (0, -1)
assert 'Requested pixel_y position' in str(excinfo.value), (
"FieldDependentAberration did not error on out-of-bounds field point"
)

det.field_position = (2048, 2048)

# Test the get_aberration_terms function uses approximated wavelength when
# called with an out-of-bound wavelength.
assert allclose(det.get_aberration_terms(5e-6), det.get_aberration_terms(2e-6)), (
assert allclose(det.get_aberration_terms(2.0e-6), det.get_aberration_terms(2.5e-6)), (
"Aberration outside wavelength range did not return closest value."
)

assert allclose(det.get_aberration_terms(1e-7), det.get_aberration_terms(0.76e-6)), (
assert allclose(det.get_aberration_terms(0.48e-6), det.get_aberration_terms(0.40e-6)), (
"Aberration outside wavelength range did not return closest value."
)

Expand Down

0 comments on commit 4cbe4f9

Please sign in to comment.