22__all__ = [
'IsolatedStarAssociationConnections',
23 'IsolatedStarAssociationConfig',
24 'IsolatedStarAssociationTask']
28from smatch.matcher
import Matcher
37 dimensions=(
'instrument',
'tract',
'skymap',),
39 source_table_visit = pipeBase.connectionTypes.Input(
40 doc=
'Source table in parquet format, per visit',
41 name=
'sourceTable_visit',
42 storageClass=
'DataFrame',
43 dimensions=(
'instrument',
'visit'),
47 skymap = pipeBase.connectionTypes.Input(
48 doc=
"Input definition of geometry/bbox and projection/wcs for warped exposures",
49 name=BaseSkyMap.SKYMAP_DATASET_TYPE_NAME,
50 storageClass=
'SkyMap',
51 dimensions=(
'skymap',),
53 isolated_star_sources = pipeBase.connectionTypes.Output(
54 doc=
'Catalog of individual sources for the isolated stars',
55 name=
'isolated_star_sources',
56 storageClass=
'DataFrame',
57 dimensions=(
'instrument',
'tract',
'skymap'),
59 isolated_star_cat = pipeBase.connectionTypes.Output(
60 doc=
'Catalog of isolated star positions',
61 name=
'isolated_star_cat',
62 storageClass=
'DataFrame',
63 dimensions=(
'instrument',
'tract',
'skymap'),
68 pipelineConnections=IsolatedStarAssociationConnections):
69 """Configuration for IsolatedStarAssociationTask."""
71 inst_flux_field = pexConfig.Field(
72 doc=(
'Full name of instFlux field to use for s/n selection and persistence. '
73 'The associated flag will be implicity included in bad_flags. '
74 'Note that this is expected to end in ``instFlux``.'),
76 default=
'apFlux_12_0_instFlux',
78 match_radius = pexConfig.Field(
79 doc=
'Match radius (arcseconds)',
83 isolation_radius = pexConfig.Field(
84 doc=(
'Isolation radius (arcseconds). Any stars with average centroids '
85 'within this radius of another star will be rejected from the final '
86 'catalog. This radius should be at least 2x match_radius.'),
90 band_order = pexConfig.ListField(
91 doc=((
'Ordered list of bands to use for matching/storage. '
92 'Any bands not listed will not be matched.')),
94 default=[
'i',
'z',
'r',
'g',
'y',
'u'],
96 id_column = pexConfig.Field(
97 doc=
'Name of column with source id.',
101 ra_column = pexConfig.Field(
102 doc=
'Name of column with right ascension.',
106 dec_column = pexConfig.Field(
107 doc=
'Name of column with declination.',
111 physical_filter_column = pexConfig.Field(
112 doc=
'Name of column with physical filter name',
114 default=
'physical_filter',
116 band_column = pexConfig.Field(
117 doc=
'Name of column with band name',
121 extra_columns = pexConfig.ListField(
122 doc=
'Extra names of columns to read and persist (beyond instFlux and error).',
126 'apFlux_17_0_instFlux',
127 'apFlux_17_0_instFluxErr',
129 'localBackground_instFlux',
130 'localBackground_flag']
132 source_selector = sourceSelectorRegistry.makeField(
133 doc=
'How to select sources. Under normal usage this should not be changed.',
141 source_selector.setDefaults()
143 source_selector.doFlags =
True
144 source_selector.doUnresolved =
True
145 source_selector.doSignalToNoise =
True
146 source_selector.doIsolated =
True
147 source_selector.doRequireFiniteRaDec =
True
148 source_selector.doRequirePrimary =
True
150 source_selector.signalToNoise.minimum = 10.0
151 source_selector.signalToNoise.maximum = 1000.0
155 source_selector.flags.bad = [
'pixelFlags_edge',
156 'pixelFlags_interpolatedCenter',
157 'pixelFlags_saturatedCenter',
158 'pixelFlags_crCenter',
160 'pixelFlags_interpolated',
161 'pixelFlags_saturated',
168 source_selector.isolated.parentName =
'parentSourceId'
169 source_selector.isolated.nChildName =
'deblend_nChild'
171 source_selector.unresolved.maximum = 0.5
172 source_selector.unresolved.name =
'extendedness'
174 source_selector.requireFiniteRaDec.raColName = self.
ra_column
175 source_selector.requireFiniteRaDec.decColName = self.
dec_column
179 """Associate sources into isolated star catalogs.
181 ConfigClass = IsolatedStarAssociationConfig
182 _DefaultName = 'isolatedStarAssociation'
184 def __init__(self, **kwargs):
185 super().__init__(**kwargs)
187 self.makeSubtask(
'source_selector')
189 self.source_selector.log.setLevel(self.source_selector.log.WARN)
192 input_ref_dict = butlerQC.get(inputRefs)
194 tract = butlerQC.quantum.dataId[
'tract']
196 source_table_refs = input_ref_dict[
'source_table_visit']
198 self.log.info(
'Running with %d source_table_visit dataRefs',
199 len(source_table_refs))
201 source_table_ref_dict_temp = {source_table_ref.dataId[
'visit']: source_table_ref
for
202 source_table_ref
in source_table_refs}
204 bands = {source_table_ref.dataId[
'band']
for source_table_ref
in source_table_refs}
206 if band
not in self.config.band_order:
207 self.log.warning(
'Input data has data from band %s but that band is not '
208 'configured for matching', band)
212 source_table_ref_dict = {visit: source_table_ref_dict_temp[visit]
for
213 visit
in sorted(source_table_ref_dict_temp.keys())}
215 struct = self.
run(input_ref_dict[
'skymap'], tract, source_table_ref_dict)
217 butlerQC.put(pd.DataFrame(struct.star_source_cat),
218 outputRefs.isolated_star_sources)
219 butlerQC.put(pd.DataFrame(struct.star_cat),
220 outputRefs.isolated_star_cat)
222 def run(self, skymap, tract, source_table_ref_dict):
223 """Run the isolated star association task.
227 skymap : `lsst.skymap.SkyMap`
231 source_table_ref_dict : `dict`
232 Dictionary of source_table refs. Key is visit, value
is dataref.
236 struct : `lsst.pipe.base.struct`
237 Struct
with outputs
for persistence.
241 primary_bands = self.config.band_order
246 if len(primary_star_cat) == 0:
247 return pipeBase.Struct(star_source_cat=np.zeros(0, star_source_cat.dtype),
248 star_cat=np.zeros(0, primary_star_cat.dtype))
253 if len(primary_star_cat) == 0:
254 return pipeBase.Struct(star_source_cat=np.zeros(0, star_source_cat.dtype),
255 star_cat=np.zeros(0, primary_star_cat.dtype))
258 inner_tract_ids = skymap.findTractIdArray(primary_star_cat[self.config.ra_column],
259 primary_star_cat[self.config.dec_column],
261 use = (inner_tract_ids == tract)
262 self.log.info(
'Total of %d isolated stars in inner tract.', use.sum())
264 primary_star_cat = primary_star_cat[use]
266 if len(primary_star_cat) == 0:
267 return pipeBase.Struct(star_source_cat=np.zeros(0, star_source_cat.dtype),
268 star_cat=np.zeros(0, primary_star_cat.dtype))
273 len(primary_star_cat))
276 star_source_cat, primary_star_cat = self.
_match_sources(primary_bands,
280 return pipeBase.Struct(star_source_cat=star_source_cat,
281 star_cat=primary_star_cat)
284 """Make a catalog of all the star sources.
289 Information about the tract.
290 source_table_ref_dict : `dict`
291 Dictionary of source_table refs. Key is visit, value
is dataref.
295 star_source_cat : `np.ndarray`
296 Catalog of star sources.
302 poly = tract_info.outer_sky_polygon
305 for visit
in source_table_ref_dict:
306 source_table_ref = source_table_ref_dict[visit]
307 df = source_table_ref.get(parameters={
'columns': all_columns})
308 df.reset_index(inplace=
True)
310 goodSrc = self.source_selector.selectSources(df)
312 table = df[persist_columns][goodSrc.selected].to_records()
316 table = np.lib.recfunctions.append_fields(table,
319 [np.where(goodSrc.selected)[0],
320 np.zeros(goodSrc.selected.sum(), dtype=np.int32)],
326 tract_use = poly.contains(np.deg2rad(table[self.config.ra_column]),
327 np.deg2rad(table[self.config.dec_column]))
329 tables.append(table[tract_use])
332 star_source_cat = np.concatenate(tables)
334 return star_source_cat
337 """Get the list of sourceTable_visit columns from the config.
341 all_columns : `list` [`str`]
343 persist_columns : `list` [`str`]
344 Columns to persist (excluding selection columns)
346 columns = [self.config.id_column,
348 self.config.ra_column, self.config.dec_column,
349 self.config.physical_filter_column, self.config.band_column,
350 self.config.inst_flux_field, self.config.inst_flux_field +
'Err']
351 columns.extend(self.config.extra_columns)
353 all_columns = columns.copy()
354 if self.source_selector.config.doFlags:
355 all_columns.extend(self.source_selector.config.flags.bad)
356 if self.source_selector.config.doUnresolved:
357 all_columns.append(self.source_selector.config.unresolved.name)
358 if self.source_selector.config.doIsolated:
359 all_columns.append(self.source_selector.config.isolated.parentName)
360 all_columns.append(self.source_selector.config.isolated.nChildName)
361 if self.source_selector.config.doRequirePrimary:
362 all_columns.append(self.source_selector.config.requirePrimary.primaryColName)
364 return all_columns, columns
367 """Match primary stars.
371 primary_bands : `list` [`str`]
372 Ordered list of primary bands.
373 star_source_cat : `np.ndarray`
374 Catalog of star sources.
378 primary_star_cat : `np.ndarray`
379 Catalog of primary star positions
381 ra_col = self.config.ra_column
382 dec_col = self.config.dec_column
386 primary_star_cat = None
387 for primary_band
in primary_bands:
388 use = (star_source_cat[
'band'] == primary_band)
390 ra = star_source_cat[ra_col][use]
391 dec = star_source_cat[dec_col][use]
393 with Matcher(ra, dec)
as matcher:
396 idx = matcher.query_groups(self.config.match_radius/3600., min_match=1)
397 except AttributeError:
399 idx = matcher.query_self(self.config.match_radius/3600., min_match=1)
404 self.log.info(
'Found 0 primary stars in %s band.', primary_band)
407 band_cat = np.zeros(count, dtype=dtype)
408 band_cat[
'primary_band'] = primary_band
414 if ra.min() < 60.0
and ra.max() > 300.0:
415 ra_temp = (ra + 180.0) % 360. - 180.
421 for i, row
in enumerate(idx):
423 band_cat[ra_col][i] = np.mean(ra_temp[row])
424 band_cat[dec_col][i] = np.mean(dec[row])
428 band_cat[ra_col] %= 360.0
431 if primary_star_cat
is None or len(primary_star_cat) == 0:
432 primary_star_cat = band_cat
434 with Matcher(band_cat[ra_col], band_cat[dec_col])
as matcher:
435 idx = matcher.query_radius(primary_star_cat[ra_col],
436 primary_star_cat[dec_col],
437 self.config.match_radius/3600.)
439 match_indices = np.array([i
for i
in range(len(idx))
if len(idx[i]) > 0])
440 if len(match_indices) > 0:
441 band_cat = np.delete(band_cat, match_indices)
443 primary_star_cat = np.append(primary_star_cat, band_cat)
444 self.log.info(
'Found %d primary stars in %s band.', len(band_cat), primary_band)
447 if primary_star_cat
is None:
448 primary_star_cat = np.zeros(0, dtype=dtype)
450 return primary_star_cat
453 """Remove neighbors from the primary star catalog.
457 primary_star_cat : `np.ndarray`
458 Primary star catalog.
462 primary_star_cat_cut : `np.ndarray`
463 Primary star cat with neighbors removed.
465 ra_col = self.config.ra_column
466 dec_col = self.config.dec_column
468 with Matcher(primary_star_cat[ra_col], primary_star_cat[dec_col])
as matcher:
473 idx = matcher.query_groups(self.config.isolation_radius/3600., min_match=2)
474 except AttributeError:
476 idx = matcher.query_self(self.config.isolation_radius/3600., min_match=2)
479 neighbor_indices = np.concatenate(idx)
481 neighbor_indices = np.zeros(0, dtype=int)
483 if len(neighbor_indices) > 0:
484 neighbored = np.unique(neighbor_indices)
485 self.log.info(
'Cutting %d objects with close neighbors.', len(neighbored))
486 primary_star_cat = np.delete(primary_star_cat, neighbored)
488 return primary_star_cat
491 """Match individual sources to primary stars.
495 bands : `list` [`str`]
497 star_source_cat : `np.ndarray`
498 Array of star sources.
499 primary_star_cat : `np.ndarray`
500 Array of primary stars.
504 star_source_cat_sorted : `np.ndarray`
505 Sorted and cropped array of star sources.
506 primary_star_cat : `np.ndarray`
507 Catalog of isolated stars,
with indexes to star_source_cat_cut.
509 ra_col = self.config.ra_column
510 dec_col = self.config.dec_column
514 n_source_per_band_per_obj = np.zeros((len(bands),
515 len(primary_star_cat)),
519 with Matcher(primary_star_cat[ra_col], primary_star_cat[dec_col])
as matcher:
520 for b, band
in enumerate(bands):
521 band_use, = np.where(star_source_cat[
'band'] == band)
523 idx = matcher.query_radius(star_source_cat[ra_col][band_use],
524 star_source_cat[dec_col][band_use],
525 self.config.match_radius/3600.)
526 n_source_per_band_per_obj[b, :] = np.array([len(row)
for row
in idx])
528 band_uses.append(band_use)
530 n_source_per_obj = np.sum(n_source_per_band_per_obj, axis=0)
532 primary_star_cat[
'nsource'] = n_source_per_obj
533 primary_star_cat[
'source_cat_index'][1:] = np.cumsum(n_source_per_obj)[:-1]
535 n_tot_source = primary_star_cat[
'source_cat_index'][-1] + primary_star_cat[
'nsource'][-1]
538 source_index = np.zeros(n_tot_source, dtype=np.int32)
539 obj_index = np.zeros(n_tot_source, dtype=np.int32)
542 for i
in range(len(primary_star_cat)):
543 obj_index[ctr: ctr + n_source_per_obj[i]] = i
544 for b
in range(len(bands)):
545 source_index[ctr: ctr + n_source_per_band_per_obj[b, i]] = band_uses[b][idxs[b][i]]
546 ctr += n_source_per_band_per_obj[b, i]
548 source_cat_index_band_offset = np.cumsum(n_source_per_band_per_obj, axis=0)
550 for b, band
in enumerate(bands):
551 primary_star_cat[f
'nsource_{band}'] = n_source_per_band_per_obj[b, :]
554 primary_star_cat[f
'source_cat_index_{band}'] = primary_star_cat[
'source_cat_index']
557 primary_star_cat[f
'source_cat_index_{band}'] = (primary_star_cat[
'source_cat_index']
558 + source_cat_index_band_offset[b - 1, :])
560 star_source_cat = star_source_cat[source_index]
561 star_source_cat[
'obj_index'] = obj_index
563 return star_source_cat, primary_star_cat
566 """Compute unique star ids.
568 This is a simple hash of the tract
and star to provide an
569 id that
is unique
for a given processing.
573 skymap : `lsst.skymap.Skymap`
583 Array of unique star ids.
586 mult = 10**(int(np.log10(len(skymap))) + 1)
588 return (np.arange(nstar) + 1)*mult + tract
591 """Get the numpy datatype for the primary star catalog.
595 primary_bands : `list` [`str`]
596 List of primary bands.
600 dtype : `numpy.dtype`
601 Datatype of the primary catalog.
603 max_len = max([len(primary_band) for primary_band
in primary_bands])
605 dtype = [(
'isolated_star_id',
'i8'),
606 (self.config.ra_column,
'f8'),
607 (self.config.dec_column,
'f8'),
608 (
'primary_band', f
'U{max_len}'),
609 (
'source_cat_index',
'i4'),
612 for band
in primary_bands:
613 dtype.append((f
'source_cat_index_{band}',
'i4'))
614 dtype.append((f
'nsource_{band}',
'i4'))
_match_primary_stars(self, primary_bands, star_source_cat)
_remove_neighbors(self, primary_star_cat)
runQuantum(self, butlerQC, inputRefs, outputRefs)
_match_sources(self, bands, star_source_cat, primary_star_cat)
_get_primary_dtype(self, primary_bands)
run(self, skymap, tract, source_table_ref_dict)
_make_all_star_sources(self, tract_info, source_table_ref_dict)
_get_source_table_visit_column_names(self)
_compute_unique_ids(self, skymap, tract, nstar)