|
| 1 | +"""Tests for Effect ExposureOutput""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from scopesim import UserCommands |
| 8 | +from scopesim.optics.image_plane import ImagePlane |
| 9 | +from scopesim.detector import Detector |
| 10 | +from scopesim.effects.electronic import ExposureOutput |
| 11 | + |
| 12 | +from scopesim.tests.mocks.py_objects.imagehdu_objects import _image_hdu_square |
| 13 | + |
| 14 | +# pylint: disable=missing-class-docstring |
| 15 | +# pylint: disable=missing-function-docstring |
| 16 | + |
| 17 | +def _patched_cmds(exptime=1, dit=None, ndit=None): |
| 18 | + return UserCommands(properties={"!OBS.exptime": exptime, |
| 19 | + "!OBS.dit": dit, |
| 20 | + "!OBS.ndit": ndit}) |
| 21 | + |
| 22 | +@pytest.fixture(name="imageplane", scope="class") |
| 23 | +def fixture_imageplane(): |
| 24 | + """Instantiate an ImagePlane object""" |
| 25 | + implane = ImagePlane(_image_hdu_square().header) |
| 26 | + implane.hdu.data += 1.e5 |
| 27 | + return implane |
| 28 | + |
| 29 | +@pytest.fixture(name="exposureoutput", scope="function") |
| 30 | +def fixture_exposureoutput(): |
| 31 | + """Instantiate an ExposureOutput object""" |
| 32 | + return ExposureOutput(mode="average", dit=1, ndit=4) |
| 33 | + |
| 34 | +@pytest.fixture(name="detector", scope="function") |
| 35 | +def fixture_detector(): |
| 36 | + det = Detector(_image_hdu_square().header) |
| 37 | + det._hdu.data[:] = 1.e5 |
| 38 | + return det |
| 39 | + |
| 40 | +class TestExposureOutput: |
| 41 | + def test_initialises_correctly(self, exposureoutput): |
| 42 | + assert isinstance(exposureoutput, ExposureOutput) |
| 43 | + |
| 44 | + def test_fails_with_unknown_mode(self): |
| 45 | + with pytest.raises(ValueError): |
| 46 | + ExposureOutput(mode="something", dit=1, ndit=4) |
| 47 | + |
| 48 | + def test_fails_without_dit_and_ndit(self): |
| 49 | + with pytest.raises(ValueError): |
| 50 | + ExposureOutput(mode="sum") |
| 51 | + |
| 52 | + def test_works_only_on_detector_base(self, exposureoutput, imageplane): |
| 53 | + assert exposureoutput.apply_to(imageplane) is imageplane |
| 54 | + |
| 55 | + def test_can_set_to_new_mode(self, exposureoutput): |
| 56 | + assert exposureoutput.current_mode == "average" |
| 57 | + exposureoutput.set_mode("sum") |
| 58 | + assert exposureoutput.current_mode == "sum" |
| 59 | + assert exposureoutput.meta["current_mode"] == "sum" |
| 60 | + |
| 61 | + def test_cannot_set_to_unknown_mode(self, exposureoutput): |
| 62 | + old_mode = exposureoutput.current_mode |
| 63 | + exposureoutput.set_mode("something") |
| 64 | + assert exposureoutput.current_mode == old_mode |
| 65 | + |
| 66 | + @pytest.mark.parametrize("dit, ndit", |
| 67 | + [(1., 1), |
| 68 | + (2., 5), |
| 69 | + (3, 36)]) |
| 70 | + def test_applies_average(self, dit, ndit, detector): |
| 71 | + det_mean = detector._hdu.data.mean() |
| 72 | + exposureoutput = ExposureOutput("average", dit=dit, ndit=ndit) |
| 73 | + result = exposureoutput.apply_to(detector) |
| 74 | + assert np.isclose(result._hdu.data.mean(), det_mean / ndit) |
| 75 | + |
| 76 | + @pytest.mark.parametrize("dit, ndit", |
| 77 | + [(1., 1), |
| 78 | + (2., 5), |
| 79 | + (3, 36)]) |
| 80 | + def test_applies_sum(self, dit, ndit, detector): |
| 81 | + det_mean = detector._hdu.data.mean() |
| 82 | + exposureoutput = ExposureOutput("sum", dit=dit, ndit=ndit) |
| 83 | + result = exposureoutput.apply_to(detector) |
| 84 | + assert np.isclose(result._hdu.data.mean(), det_mean) |
0 commit comments