590 catalog_ref: pd.DataFrame,
591 catalog_target: pd.DataFrame,
592 catalog_match_ref: pd.DataFrame,
593 catalog_match_target: pd.DataFrame,
594 wcs: afwGeom.SkyWcs =
None,
595 ) -> pipeBase.Struct:
596 """Load matched reference and target (measured) catalogs, measure summary statistics, and output
597 a combined matched catalog with columns from both inputs.
601 catalog_ref : `pandas.DataFrame`
602 A reference catalog to diff objects/sources from.
603 catalog_target : `pandas.DataFrame`
604 A target catalog to diff reference objects/sources to.
605 catalog_match_ref : `pandas.DataFrame`
606 A catalog with match indices of target sources and selection flags
607 for each reference source.
608 catalog_match_target : `pandas.DataFrame`
609 A catalog with selection flags for each target source.
610 wcs : `lsst.afw.image.SkyWcs`
611 A coordinate system to convert catalog positions to sky coordinates,
616 retStruct : `lsst.pipe.base.Struct`
617 A struct with output_ref and output_target attribute containing the
618 output matched catalogs.
622 select_ref = catalog_match_ref[
'match_candidate'].values
625 select_target = (catalog_match_target[
'match_candidate'].values
626 if 'match_candidate' in catalog_match_target.columns
627 else np.ones(len(catalog_match_target), dtype=bool))
628 for column
in config.columns_target_select_true:
629 select_target &= catalog_target[column].values
630 for column
in config.columns_target_select_false:
631 select_target &= ~catalog_target[column].values
633 ref, target = config.coord_format.format_catalogs(
634 catalog_ref=catalog_ref, catalog_target=catalog_target,
635 select_ref=
None, select_target=select_target, wcs=wcs, radec_to_xy_func=radec_to_xy,
636 return_converted_columns=config.coord_format.coords_ref_to_convert
is not None,
638 cat_ref = ref.catalog
639 cat_target = target.catalog
640 n_target = len(cat_target)
642 match_row = catalog_match_ref[
'match_row'].values
643 matched_ref = match_row >= 0
644 matched_row = match_row[matched_ref]
645 matched_target = np.zeros(n_target, dtype=bool)
646 matched_target[matched_row] =
True
649 cat_left = cat_target.iloc[matched_row]
650 has_index_left = cat_left.index.name
is not None
651 cat_right = cat_ref[matched_ref].reset_index()
652 cat_matched = pd.concat(objs=(cat_left.reset_index(drop=
True), cat_right), axis=1)
654 cat_matched.index = cat_left.index
655 cat_matched.columns.values[len(cat_target.columns):] = [f
'refcat_{col}' for col
in cat_right.columns]
658 coord1_target_err, coord2_target_err = config.columns_target_coord_err
659 column_dist, column_dist_err =
'distance',
'distanceErr'
660 dist = np.full(n_target, np.Inf)
662 dist[matched_row] = np.hypot(
663 target.coord1[matched_row] - ref.coord1[matched_ref],
664 target.coord2[matched_row] - ref.coord2[matched_ref],
666 dist_err = np.full(n_target, np.Inf)
667 dist_err[matched_row] = np.hypot(cat_target.iloc[matched_row][coord1_target_err].values,
668 cat_target.iloc[matched_row][coord2_target_err].values)
669 cat_target[column_dist], cat_target[column_dist_err] = dist, dist_err
672 column_dummy =
'dummy'
673 cat_ref[column_dummy] = np.zeros_like(ref.coord1)
676 extended_ref = cat_ref[config.column_ref_extended]
677 if config.column_ref_extended_inverted:
678 extended_ref = 1 - extended_ref
680 extended_target = cat_target[config.column_target_extended].values >= config.extendedness_cut
683 suffixes = {MeasurementType.DIFF:
'diff', MeasurementType.CHI:
'chi'}
685 suffixes_flux = {MeasurementType.CHI: suffixes[MeasurementType.CHI]}
687 suffixes_mag = {MeasurementType.DIFF: suffixes[MeasurementType.DIFF]}
688 stats = {stat.name_short(): stat()
for stat
in (Median, SigmaIQR, SigmaMAD)}
690 for percentile
in self.config.percentiles:
691 stat =
Percentile(percentile=float(Decimal(percentile)))
692 stats[stat.name_short()] = stat
696 bands_columns=config.columns_flux,
698 suffixes_flux=suffixes_flux,
699 suffixes_mag=suffixes_mag,
702 column_dist=column_dist,
706 n_bins = config.mag_num_bins
707 data = np.zeros((n_bins,), dtype=[(key, value)
for key, value
in columns.items()])
708 data[
'bin'] = np.arange(n_bins)
711 bins_mag = np.linspace(start=config.mag_brightest_ref, stop=config.mag_faintest_ref,
713 data[
'mag_min'] = bins_mag[:-1]
714 data[
'mag_max'] = bins_mag[1:]
715 bins_mag = tuple((bins_mag[idx], bins_mag[idx + 1])
for idx
in range(n_bins))
718 column_mag_temp =
'mag_temp'
719 column_color_temp =
'color_temp'
720 column_color_err_temp =
'colorErr_temp'
721 flux_err_frac_prev = [
None]*n_models
722 mag_prev = [
None]*n_models
725 target.column_coord1: (
726 ref.column_coord1, target.column_coord1, coord1_target_err,
False,
728 target.column_coord2: (
729 ref.column_coord2, target.column_coord2, coord2_target_err,
False,
731 column_dist: (column_dummy, column_dist, column_dist_err,
False),
736 band_fluxes = [(band, config_flux)
for (band, config_flux)
in config.columns_flux.items()]
737 n_bands = len(band_fluxes)
738 band_fluxes.append(band_fluxes[0])
739 flux_err_frac_first =
None
744 for idx_band, (band, config_flux)
in enumerate(band_fluxes):
745 if idx_band == n_bands:
747 mag_ref = mag_ref_first
748 flux_err_frac = flux_err_frac_first
749 mag_model = mag_first
751 mag_ref = -2.5*np.log10(cat_ref[config_flux.column_ref_flux]) + config.mag_zeropoint_ref
752 flux_err_frac = [
None]*n_models
753 mag_model = [
None]*n_models
756 cat_ref[column_color_temp] = cat_ref[column_mag_temp] - mag_ref
758 cat_ref[column_mag_temp] = mag_ref
760 select_ref_bins = [select_ref & (mag_ref > mag_lo) & (mag_ref < mag_hi)
761 for idx_bin, (mag_lo, mag_hi)
in enumerate(bins_mag)]
764 for idx_model
in range(n_models):
765 column_target_flux = config_flux.columns_target_flux[idx_model]
766 column_target_flux_err = config_flux.columns_target_flux_err[idx_model]
768 flux_target = cat_target[column_target_flux]
769 mag_target = -2.5*np.log10(flux_target) + config.mag_zeropoint_target
770 if config.mag_ceiling_target
is not None:
771 mag_target[mag_target > config.mag_ceiling_target] = config.mag_ceiling_target
772 mag_model[idx_model] = mag_target
775 flux_err_frac[idx_model] = cat_target[column_target_flux_err]/flux_target
780 column_mag_temp_model = f
'{column_mag_temp}{idx_model}'
781 cat_target[column_mag_temp_model] = mag_target
783 columns_target[f
'flux_{column_target_flux}'] = (
784 config_flux.column_ref_flux,
786 column_target_flux_err,
790 columns_target[f
'mag_{column_target_flux}'] = (
791 column_mag_temp, column_mag_temp_model,
None,
False,
796 skip_color = (idx_band == n_bands)
and (n_bands <= 2)
798 column_color_temp_model = f
'{column_color_temp}{idx_model}'
799 column_color_err_temp_model = f
'{column_color_err_temp}{idx_model}'
802 cat_target[column_color_temp_model] = mag_prev[idx_model] - mag_model[idx_model]
805 cat_target[column_color_err_temp_model] = 2.5/np.log(10)*np.hypot(
806 flux_err_frac[idx_model], flux_err_frac_prev[idx_model])
807 columns_target[f
'color_{band_prev}_m_{band}_{column_target_flux}'] = (
809 column_color_temp_model,
810 column_color_err_temp_model,
814 for idx_bin, (mag_lo, mag_hi)
in enumerate(bins_mag):
818 select_ref_bin = select_ref_bins[idx_bin]
819 select_target_bin = select_target & (mag_target > mag_lo) & (mag_target < mag_hi)
821 for sourcetype
in SourceType:
822 sourcetype_info = sourcetype.value
823 is_extended = sourcetype_info.is_extended
825 select_ref_sub = select_ref_bin.copy()
826 select_target_sub = select_target_bin.copy()
827 if is_extended
is not None:
828 is_extended_ref = (extended_ref == is_extended)
829 select_ref_sub &= is_extended_ref
831 n_ref_sub = np.count_nonzero(select_ref_sub)
833 MatchType.ALL.value)] = n_ref_sub
834 select_target_sub &= (extended_target == is_extended)
835 n_target_sub = np.count_nonzero(select_target_sub)
837 MatchType.ALL.value)] = n_target_sub
840 match_row_bin = match_row.copy()
841 match_row_bin[~select_ref_sub] = -1
842 match_good = match_row_bin >= 0
844 n_match = np.count_nonzero(match_good)
849 rows_matched = match_row_bin[match_good]
850 subset_target = cat_target.iloc[rows_matched]
851 if (is_extended
is not None)
and (idx_model == 0):
852 right_type = extended_target[rows_matched] == is_extended
853 n_total = len(right_type)
854 n_right = np.count_nonzero(right_type)
856 MatchType.MATCH_RIGHT.value)] = n_right
858 band, sourcetype_info.label,
'n_ref', MatchType.MATCH_WRONG.value,
859 )] = n_total - n_right
862 for column, (column_ref, column_target, column_err_target, skip_diff) \
863 in columns_target.items():
864 values_ref = cat_ref[column_ref][match_good].values
866 subset_target[column_err_target].values
867 if column_err_target
is not None
872 subset_target[column_target].values,
877 prefix=f
'{band}_{sourcetype_info.label}_{column}',
885 select_target_sub &= matched_target
887 if is_extended
is not None and (np.count_nonzero(select_target_sub) > 0):
888 n_total = np.count_nonzero(select_target_sub)
889 right_type = np.zeros(n_target, dtype=bool)
890 right_type[match_row[matched_ref & is_extended_ref]] =
True
891 right_type &= select_target_sub
892 n_right = np.count_nonzero(right_type)
894 MatchType.MATCH_RIGHT.value)] = n_right
896 MatchType.MATCH_WRONG.value)] = n_total - n_right
899 for prefix
in (
'flux',
'mag'):
900 del columns_target[f
'{prefix}_{column_target_flux}']
902 del columns_target[f
'color_{band_prev}_m_{band}_{column_target_flux}']
905 flux_err_frac_prev = flux_err_frac
909 flux_err_frac_first = flux_err_frac
910 mag_first = mag_model
911 mag_ref_first = mag_ref
913 retStruct = pipeBase.Struct(cat_matched=cat_matched, diff_matched=pd.DataFrame(data))