Skip to content

Commit

Permalink
Add tests for when all observations/stars are cut.
Browse files Browse the repository at this point in the history
  • Loading branch information
erykoff committed Feb 14, 2022
1 parent 23926ba commit deb0995
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 7 deletions.
16 changes: 16 additions & 0 deletions python/lsst/pipe/tasks/isolatedStarAssociation.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,17 @@ def run(self, skymap, tract, source_table_ref_dict):
# Do primary matching
primary_star_cat = self._match_primary_stars(primary_bands, star_obs_cat)

if len(primary_star_cat) == 0:
return pipeBase.Struct(star_obs_cat=np.zeros(0, star_obs_cat.dtype),
star_cat=np.zeros(0, primary_star_cat.dtype))

# Remove neighbors
primary_star_cat = self._remove_neighbors(primary_star_cat)

if len(primary_star_cat) == 0:
return pipeBase.Struct(star_obs_cat=np.zeros(0, star_obs_cat.dtype),
star_cat=np.zeros(0, primary_star_cat.dtype))

# Crop to inner tract region
inner_tract_ids = skymap.findTractIdArray(primary_star_cat[self.config.ra_column],
primary_star_cat[self.config.dec_column],
Expand All @@ -251,6 +259,10 @@ def run(self, skymap, tract, source_table_ref_dict):

primary_star_cat = primary_star_cat[use]

if len(primary_star_cat) == 0:
return pipeBase.Struct(star_obs_cat=np.zeros(0, star_obs_cat.dtype),
star_cat=np.zeros(0, primary_star_cat.dtype))

# Set the unique ids.
primary_star_cat['isolated_star_id'] = self._compute_unique_ids(skymap,
tract,
Expand Down Expand Up @@ -417,6 +429,10 @@ def _match_primary_stars(self, primary_bands, star_obs_cat):
primary_star_cat = np.append(primary_star_cat, band_cat)
self.log.info('Found %d primary stars in %s band.', len(band_cat), primary_band)

# If everything was cut, we still want the correct datatype.
if primary_star_cat is None:
primary_star_cat = np.zeros(0, dtype=dtype)

return primary_star_cat

def _remove_neighbors(self, primary_star_cat):
Expand Down
102 changes: 95 additions & 7 deletions tests/test_isolatedStarAssociation.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,27 @@ def _make_skymap(self):
skymap_config.pixelScale = 0.168
return lsst.skymap.ringsSkyMap.RingsSkyMap(skymap_config)

def _make_simdata(self, tract):
"""Make simulated data tables and references."""
def _make_simdata(self,
tract,
only_neighbors=False,
only_out_of_tract=False,
only_out_of_inner_tract=False):
"""Make simulated data tables and references.
Parameters
----------
only_neighbors : `bool`, optional
Only put in neighbors.
only_out_of_tract : `bool`, optional
All stars are out of the tract.
only_out_of_inner_tract : `bool`, optional
All stars are out of the inner tract.
Returns
-------
data_refs : `list` [`MockSourceTableReference`]
List of mock references.
"""
np.random.seed(12345)

n_visit_per_band = 5
Expand Down Expand Up @@ -149,8 +168,23 @@ def _make_simdata(self, tract):
ra_just = ra_just_i
dec_just = dec_just_i

star_ra = np.concatenate((ra_both, ra_neighbor, ra_just))
star_dec = np.concatenate((dec_both, dec_neighbor, dec_just))
if only_neighbors:
star_ra = np.concatenate(([ra_both[n_star_both//2]], ra_neighbor))
star_dec = np.concatenate(([dec_both[n_star_both//2]], dec_neighbor))
else:
star_ra = np.concatenate((ra_both, ra_neighbor, ra_just))
star_dec = np.concatenate((dec_both, dec_neighbor, dec_just))

if only_out_of_tract:
poly = self.skymap[self.tract].outer_sky_polygon
use = ~poly.contains(np.deg2rad(star_ra), np.deg2rad(star_dec))
star_ra = star_ra[use]
star_dec = star_dec[use]
elif only_out_of_inner_tract:
inner_tract_ids = self.skymap.findTractIdArray(star_ra, star_dec, degrees=True)
use = (inner_tract_ids != self.tract)
star_ra = star_ra[use]
star_dec = star_dec[use]

nstar = len(star_ra)

Expand Down Expand Up @@ -182,8 +216,12 @@ def _make_simdata(self, tract):
self.n_visit_per_band = n_visit_per_band
self.n_star_both = n_star_both
self.n_star_just_one = n_star_just_one
self.star_ras = np.concatenate((ra_both, ra_just_r, ra_just_i, ra_neighbor))
self.star_decs = np.concatenate((dec_both, dec_just_r, dec_just_i, dec_neighbor))
if only_neighbors:
self.star_ras = np.concatenate(([ra_both[n_star_both//2]], ra_neighbor))
self.star_decs = np.concatenate(([dec_both[n_star_both//2]], dec_neighbor))
else:
self.star_ras = np.concatenate((ra_both, ra_just_r, ra_just_i, ra_neighbor))
self.star_decs = np.concatenate((dec_both, dec_just_r, dec_just_i, dec_neighbor))

return data_refs

Expand Down Expand Up @@ -295,7 +333,6 @@ def test_make_all_star_obs(self):

def test_run_isolated_star_association_task(self):
"""Test running the full task."""

struct = self.isolatedStarAssociationTask.run(self.skymap,
self.tract,
self.data_ref_dict)
Expand Down Expand Up @@ -343,6 +380,57 @@ def test_run_isolated_star_association_task(self):
1./3600.)
self.assertEqual(len(idx[0]), star_cat[f'nobs_{band}'][i])

def test_run_task_all_neighbors(self):
"""Test running the task when all the stars are rejected as neighbors."""
data_refs = self._make_simdata(self.tract, only_neighbors=True)
data_ref_dict = {visit: data_ref for visit, data_ref in zip(self.visits,
data_refs)}

struct = self.isolatedStarAssociationTask.run(self.skymap,
self.tract,
data_ref_dict)

# These should ber zero length.
self.assertEqual(len(struct.star_obs_cat), 0)
self.assertEqual(len(struct.star_cat), 0)
# And spot-check a couple of expected fields to make sure they have the right type.
self.assertTrue('physical_filter' in struct.star_obs_cat.dtype.names)
self.assertTrue('nobs_i' in struct.star_cat.dtype.names)

def test_run_task_all_out_of_tract(self):
"""Test running the task when all the observations are out of the tract."""
data_refs = self._make_simdata(self.tract, only_out_of_tract=True)
data_ref_dict = {visit: data_ref for visit, data_ref in zip(self.visits,
data_refs)}

struct = self.isolatedStarAssociationTask.run(self.skymap,
self.tract,
data_ref_dict)

# These should ber zero length.
self.assertEqual(len(struct.star_obs_cat), 0)
self.assertEqual(len(struct.star_cat), 0)
# And spot-check a couple of expected fields to make sure they have the right type.
self.assertTrue('physical_filter' in struct.star_obs_cat.dtype.names)
self.assertTrue('nobs_i' in struct.star_cat.dtype.names)

def test_run_task_all_out_of_inner_tract(self):
"""Test running the task when all the observations are out of the inner tract."""
data_refs = self._make_simdata(self.tract, only_out_of_inner_tract=True)
data_ref_dict = {visit: data_ref for visit, data_ref in zip(self.visits,
data_refs)}

struct = self.isolatedStarAssociationTask.run(self.skymap,
self.tract,
data_ref_dict)

# These should ber zero length.
self.assertEqual(len(struct.star_obs_cat), 0)
self.assertEqual(len(struct.star_cat), 0)
# And spot-check a couple of expected fields to make sure they have the right type.
self.assertTrue('physical_filter' in struct.star_obs_cat.dtype.names)
self.assertTrue('nobs_i' in struct.star_cat.dtype.names)


class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
pass
Expand Down

0 comments on commit deb0995

Please sign in to comment.