Skip to content

Commit

Permalink
Merge pull request #174 from sot/dyn-bgd-min-anchor-stars
Browse files Browse the repository at this point in the history
Require minimum number of anchor stars for dyn bgd bonus
  • Loading branch information
taldcroft authored Jul 6, 2022
2 parents f48f000 + 7f933db commit e5e3a1f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
14 changes: 12 additions & 2 deletions sparkles/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
CACHE = {}
FILEDIR = Path(__file__).parent

# Minimum number of "anchor stars" that are always evaluated *without* the bonus
# from dynamic background when dyn_bgd_n_faint > 0. This is mostly to avoid the
# situation where 4 stars are selected and 2 are faint bonus stars. In this case
# there would be only 2 anchor stars that ensure good tracking even without
# dyn bgd.
MIN_DYN_BGD_ANCHOR_STARS = 3


def main(sys_args=None):
"""Command line interface to preview_load()"""
Expand Down Expand Up @@ -555,10 +562,13 @@ def __init__(self, *args, **kwargs):
t_ccds = np.full_like(mags, self.guides.t_ccd)
if self.dyn_bgd_n_faint > 0:
# Apply the dynamic background t_ccd bonus to the
# dyn_bgd_n_faint faintest stars. See:
# dyn_bgd_n_faint faintest stars, ensuring that at least
# MIN_DYN_BGD_ANCHOR_STARS are evaluated without the bonus. See:
# https://nbviewer.org/urls/cxc.harvard.edu/mta/ASPECT/ipynb/misc/guide-count-dyn-bgd.ipynb
mags = np.sort(mags)
t_ccds[-self.dyn_bgd_n_faint:] += self.dyn_bgd_dt_ccd
n_faint = min(self.dyn_bgd_n_faint, len(t_ccds))
idx_bonus = max(len(t_ccds) - n_faint, MIN_DYN_BGD_ANCHOR_STARS)
t_ccds[idx_bonus:] += self.dyn_bgd_dt_ccd

self.guide_count = guide_count(mags, t_ccds)
if self.is_ER:
Expand Down
25 changes: 17 additions & 8 deletions sparkles/tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,34 @@ def test_n_guide_mon_check_atypical_request():
'category': 'caution'}]


def test_guide_count_dyn_bgd_bonus():
vals = [
(5, 4.07, 4.66), # n_guide, legacy_guide_count, dyn_bdg_guide_count
(4, 3.25, 3.60), # 4 stars, legacy and dyn bdg guide counts closer together
(3, 2.42, 2.42), # 3 stars, legacy and dyn bdg equal (no bonus stars)
]


@pytest.mark.parametrize('vals', vals)
def test_guide_count_dyn_bgd_bonus(vals):
n_guide, leg_guide_count, dyn_guide_count = vals
stars = StarsTable.empty()

stars.add_fake_constellation(mag=np.linspace(10, 10.2, 5),
size=2000, n_stars=5)
stars.add_fake_constellation(mag=np.linspace(10, 10.2, n_guide),
size=2000, n_stars=n_guide)

aca_leg = get_aca_catalog(**STD_INFO, dark=DARK40, stars=stars, dyn_bgd_n_faint=0)
aca_dyn = get_aca_catalog(**STD_INFO, dark=DARK40, stars=stars, dyn_bgd_n_faint=2,
dyn_bgd_dt_ccd=-4.0)
# Same catalog but with different attributes
assert len(aca_leg.guides) == 5
assert len(aca_dyn.guides) == 5
assert len(aca_leg.guides) == n_guide
assert len(aca_dyn.guides) == n_guide
assert np.all(aca_leg.guides['mag'] == aca_dyn.guides['mag'])

acar_leg = ACAReviewTable(aca_leg)
acar_dyn = ACAReviewTable(aca_dyn)
# Computed guide counts are different and more with dyn_bgd_n_faint=2
assert np.isclose(acar_leg.guide_count, 4.07, rtol=0, atol=0.1)
assert np.isclose(acar_dyn.guide_count, 4.66, rtol=0, atol=0.1)
# Computed guide counts without / with dyn_bgd_n_faint=2
assert np.isclose(acar_leg.guide_count, leg_guide_count, rtol=0, atol=0.1)
assert np.isclose(acar_dyn.guide_count, dyn_guide_count, rtol=0, atol=0.1)


def test_n_guide_too_few_guide_or_mon():
Expand Down

0 comments on commit e5e3a1f

Please sign in to comment.