Skip to content

Commit

Permalink
These tests now all pass
Browse files Browse the repository at this point in the history
Some contained wrong assumptions in the scaling factor.

reset=False is needed when opt.cmds is hacked before the readout call.
  • Loading branch information
teutoburg committed Nov 5, 2024
1 parent 44bdf17 commit 30ffb5e
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions scopesim/tests/test_basic_instrument/test_basic_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,16 @@ def test_obs_dict(self, obs, dit, ndit, factor, quant):
o_dit, o_ndit = opt.cmds["!OBS.dit"], opt.cmds["!OBS.ndit"]
opt.cmds["!OBS.dit"] = dit
opt.cmds["!OBS.ndit"] = ndit
kwarged = int(opt.readout()[0][1].data.sum())
kwarged = int(opt.readout(reset=False)[0][1].data.sum())
assert quanteff._should_apply() == quant
opt.cmds["!OBS.dit"] = o_dit
opt.cmds["!OBS.ndit"] = o_ndit
# Quantization results in ~4% loss, which is fine:
assert pytest.approx(kwarged / default, rel=.05) == factor

@pytest.mark.parametrize(("dit", "ndit", "factor", "quant"),
[pytest.param(20, 1, 2, True, marks=pytest.mark.xfail(reason="S.E. doesn't get kwargs without A.E..")),
pytest.param(10, 3, 3, False, marks=pytest.mark.xfail(reason="S.E. doesn't get kwargs without A.E..")),
[(20, 1, 2, True),
(10, 3, 3, False),
(None, None, 1, True)])
def test_kwargs_override_obs_dict(self, obs, dit, ndit, factor, quant):
"""This should prioritize kwargs and fallback to !OBS."""
Expand All @@ -267,69 +267,71 @@ def test_kwargs_override_obs_dict(self, obs, dit, ndit, factor, quant):
assert pytest.approx(kwarged / default, rel=.05) == factor

@pytest.mark.parametrize(("dit", "ndit", "factor", "quant"),
[pytest.param(20, 1, 2, True, marks=pytest.mark.xfail(reason="S.E. doesn't get kwargs without A.E..")),
pytest.param(10, 3, 3, False, marks=pytest.mark.xfail(reason="S.E. doesn't get kwargs without A.E..")),
pytest.param(None, None, 1, True, marks=pytest.mark.xfail(reason="A.E. dosen't use dit, ndit from !OBS if those are None in kwargs."))])
[(20, 1, 2, True),
(10, 3, 3, False),
(None, None, 1, True)])
def test_kwargs_override_obs_dict_also_with_autoexp(
self, obs_aeq, dit, ndit, factor, quant):
"""This should prioritize dit, ndit from kwargs.
Lacking those, dit and ndit from !OBS should be used over exptime.
"""
opt, default, quanteff = obs_aeq
kwarged = int(opt.readout(dit=dit, ndit=ndit)[0][1].data.sum())
assert int(kwarged / default) == factor
kwarged = int(opt.readout(dit=dit, ndit=ndit, reset=False)[0][1].data.sum())
assert quanteff._should_apply() == quant
# Quantization results in ~4% loss, which is fine:
assert pytest.approx(kwarged / default, rel=.05) == factor

@pytest.mark.parametrize(("exptime", "factor"),
[(20, 2), (30, 3),
pytest.param(None, 6, marks=pytest.mark.xfail(reason="A.E. doesn't understand None yet."))])
[(20, 2), (30, 3), (None, 6)])
def test_autoexp(self, obs_aeq, exptime, factor):
"""This should prioritize kwargs and fallback to !OBS."""
opt, default, quanteff = obs_aeq
# This should work with patch.dict, but doesn't :(
o_dit, o_ndit = opt.cmds["!OBS.dit"], opt.cmds["!OBS.ndit"]
opt.cmds["!OBS.dit"] = None
opt.cmds["!OBS.ndit"] = None
kwarged = int(opt.readout(exptime=exptime)[0][1].data.sum())
kwarged = int(opt.readout(exptime=exptime, reset=False)[0][1].data.sum())
assert not quanteff._should_apply()
opt.cmds["!OBS.dit"] = o_dit
opt.cmds["!OBS.ndit"] = o_ndit
assert int(kwarged / default) == factor
# Quantization results in ~4% loss, which is fine:
assert pytest.approx(kwarged / default, rel=.05) == factor

@pytest.mark.parametrize(("exptime", "factor", "quant"),
[(30, 3, False),
(None, 1, True)])
[(30, 3, False), (None, 1, True)])
def test_autoexp_overrides_obs_dict(self, obs_aeq, exptime, factor, quant):
"""This should prioritize kwargs and use dit, ndit when None."""
opt, default, quanteff = obs_aeq
kwarged = int(opt.readout(exptime=exptime)[0][1].data.sum())
kwarged = int(opt.readout(exptime=exptime, reset=False)[0][1].data.sum())
assert quanteff._should_apply() == quant
# Quantization results in ~4% loss, which is fine:
assert pytest.approx(kwarged / default, rel=.05) == factor

@pytest.mark.parametrize(("dit", "ndit", "factor", "quant"),
[pytest.param(90, 1, 90, True, marks=pytest.mark.xfail(reason="S.E. doesn't get kwargs.")),
pytest.param(2, 90, 180, False, marks=pytest.mark.xfail(reason="S.E. doesn't get kwargs."))])
[(90, 1, 9, True), (2, 90, 18, False)])
def test_ditndit_in_kwargs_while_also_having_autoexp(
self, obs_aeq, dit, ndit, factor, quant):
"""This should prioritize dit, ndit from kwargs."""
opt, default, quanteff = obs_aeq
kwarged = int(opt.readout(dit=dit, ndit=ndit)[0][1].data.sum())
assert int(kwarged / default) == factor
kwarged = int(opt.readout(dit=dit, ndit=ndit, reset=False)[0][1].data.sum())
assert quanteff._should_apply() == quant
# Quantization results in ~4% loss, which is fine:
assert pytest.approx(kwarged / default, rel=.05) == factor

@pytest.mark.parametrize(("dit", "ndit", "exptime", "factor", "quant"),
[pytest.param(90, 1, None, 90, True, marks=pytest.mark.xfail(reason="S.E. doesn't get kwargs.")),
pytest.param(2, 90, 20, 180, False, marks=pytest.mark.xfail(reason="S.E. doesn't get kwargs."))])
[(90, 1, None, 9, True),
(2, 90, 20, 18, False)])
def test_ditndit_in_kwargs_while_also_having_autoexp_and_exptime(
self, obs_aeq, dit, ndit, exptime, factor, quant):
"""This should prioritize dit, ndit from kwargs and ignore exptime."""
opt, default, quanteff = obs_aeq
kwarged = int(opt.readout(exptime=exptime,
dit=dit, ndit=ndit)[0][1].data.sum())
assert int(kwarged / default) == factor
dit=dit, ndit=ndit,
reset=False)[0][1].data.sum())
assert quanteff._should_apply() == quant
# Quantization results in ~4% loss, which is fine:
assert pytest.approx(kwarged / default, rel=.05) == factor

def test_throws_for_no_anything(self, obs):
"""No specification whatsoever, so throw error."""
Expand All @@ -340,7 +342,7 @@ def test_throws_for_no_anything(self, obs):
opt.cmds["!OBS.dit"] = None
opt.cmds["!OBS.ndit"] = None
with pytest.raises(ValueError):
opt.readout()
opt.readout(reset=False)
opt.cmds["!OBS.dit"] = o_dit
opt.cmds["!OBS.ndit"] = o_ndit

Expand All @@ -349,7 +351,7 @@ def test_throws_for_no_ditndit_no_autoexp_kwargs(self, obs):
opt, default, quanteff = obs
opt.cmds["!OBS.exptime"] = None
with pytest.raises(ValueError):
opt.readout(exptime=60)
opt.readout(exptime=60, reset=False)

def test_throws_for_no_ditndit_no_autoexp_obs(self, obs):
"""This should fallback to !OBS.exptime, but fail w/o AutoExp."""
Expand All @@ -360,6 +362,6 @@ def test_throws_for_no_ditndit_no_autoexp_obs(self, obs):
opt.cmds["!OBS.dit"] = None
opt.cmds["!OBS.ndit"] = None
with pytest.raises(ValueError):
opt.readout()
opt.readout(reset=False)
opt.cmds["!OBS.dit"] = o_dit
opt.cmds["!OBS.ndit"] = o_ndit

0 comments on commit 30ffb5e

Please sign in to comment.