예제 #1
0
 def MtX(self, x):
     r"""Adjoint to degradation operator :func:`MX`."""
     x = utils.reg_format(x * self.SNR_weights)
     upsamp_x = np.array(
         [nf * utils.adjoint_degradation_op(x_i, shift_ker, self.D) for
          nf, x_i, shift_ker
          in zip(self.normfacs, x, utils.reg_format(self.ker_rot))])
     x, upsamp_x = utils.rca_format(x), utils.rca_format(upsamp_x)
     return utils.apply_transform(upsamp_x.dot(self.A.T), self.filters)
예제 #2
0
 def update_H_glob(self, new_H_glob):
     r"""Update current global model."""
     self.H_glob = new_H_glob
     dec_H_glob = np.array(
         [nf * utils.degradation_op(H_i, shift_ker, self.D)
          for nf, shift_ker, H_i in zip(self.normfacs,
                                        utils.reg_format(self.ker),
                                        utils.reg_format(self.H_glob))])
     self.FdH_glob = utils.rca_format(dec_H_glob)
예제 #3
0
 def update_H_loc(self, new_H_loc):
     r"""Update current local models."""
     self.H_loc = new_H_loc
     dec_H_loc = np.array([nf * utils.degradation_op(H_i, shift_ker, self.D)
                           for nf, shift_ker, H_i in
                           zip(self.normfacs,
                               utils.reg_format(self.ker),
                               utils.reg_format(self.H_loc))])
     self.FdH_loc = utils.rca_format(dec_H_loc)
예제 #4
0
 def update_S(self, new_S, update_spectral_radius=True):
     r"""Update current eigenPSFs."""
     self.S = new_S
     self.FdS = np.array([[nf * utils.degradation_op(S_j, shift_ker, self.D)
                           for nf, shift_ker in
                           zip(self.normfacs, utils.reg_format(self.ker))]
                          for S_j in utils.reg_format(self.S)])
     if update_spectral_radius:
         PowerMethod.get_spec_rad(self)
예제 #5
0
    def MtX(self, x):
        r"""Adjoint to degradation operator :func:`MX`.

        Parameters
        ----------
        x : numpy.ndarray
            Set of finer-grid images.
        """
        x = utils.reg_format(x * self.SNR_weights)  # [TL] CHECK
        STx = np.array([np.sum(FdS_i * x, axis=(1, 2)) for FdS_i in self.FdS])
        return STx.dot(self.Pi.T)
예제 #6
0
    def MX(self, transf_S):
        r"""Apply degradation operator and renormalize.

        Parameters
        ----------
        transf_S : numpy.ndarray
            Current eigenPSFs in Starlet space.

        Returns
        -------
        numpy.ndarray result

        """
        S = utils.rca_format(
            np.array([filter_convolve(transf_Sj, self.filters, filter_rot=True)
                      for transf_Sj in transf_S]))
        dec_rec = np.array(
            [nf * utils.degradation_op(S.dot(A_i), shift_ker, self.D)
             for nf, A_i, shift_ker in zip(self.normfacs,
                                           self.A.T,
                                           utils.reg_format(self.ker))])
        self._current_rec = utils.rca_format(dec_rec)
        return self._current_rec
예제 #7
0
    def outlier_rejection(star_list,
                          pos_list,
                          mask_list,
                          ccd_list,
                          SNR_list=None,
                          RA_list=None,
                          DEC_list=None,
                          shape_std_max=5.,
                          print_fun=None):
        r"""Outlier star rejection method.

        Notes
        -----
        It is based on the measurements from Galsim's HSM adaptive moments.
        The method calculates the 2nd order moments from the stars
        in the exposure. If there are stars that have an aberrant value in one
        of the stats, e1, e2 or R2 we discard the star.
        An aberrant value is defined as a value that is more
        than ``shape_std_max`` sigmas away from the mean.

        It inputs all the lists that will be used as a method and returns
        the same lists without the stars that where considered as outliers.

        ``print_fun`` is a function that prints details about the stars
        being removed.

        """
        # Define the printing function. Could be printing on a log file.
        if print_fun is None:

            def print_fun(msg):
                print(msg)

        # Reject outliers
        all_stars = np.concatenate(star_list, axis=2)
        all_stars = utils.reg_format(np.copy(all_stars))
        all_masks = np.concatenate(mask_list, axis=2)
        all_masks = utils.reg_format(np.copy(all_masks))
        # hsm thinks 0 means good
        badpix_masks = np.rint(np.abs(all_masks - 1))

        star_moms = [
            hsm.FindAdaptiveMom(Image(star), badpix=Image(bp), strict=False)
            for star, bp in zip(all_stars, badpix_masks)
        ]
        star_shapes = np.array([[
            moms.observed_shape.g1, moms.observed_shape.g2,
            2. * moms.moments_sigma**2,
            int(bool(moms.error_message))
        ] for moms in star_moms])

        # Outlier rejection based on e1, e2 and R2
        R2_thresh = shape_std_max * np.std(star_shapes[:, 2]) + \
            np.mean(star_shapes[:, 2])
        R2_bad_stars = (abs(star_shapes[:, 2]) > R2_thresh)

        e2_thresh = shape_std_max * np.std(star_shapes[:, 1]) + \
            np.mean(star_shapes[:, 1])
        e2_bad_stars = (abs(star_shapes[:, 1]) > e2_thresh)

        e1_thresh = shape_std_max * np.std(star_shapes[:, 0]) + \
            np.mean(star_shapes[:, 0])
        e1_bad_stars = (abs(star_shapes[:, 0]) > e1_thresh)

        bad_stars = np.logical_or(e1_bad_stars, e2_bad_stars)
        bad_stars = np.logical_or(bad_stars, R2_bad_stars)

        bad_stars_idx = np.nonzero(bad_stars)[0]
        print_fun(bad_stars_idx)
        print_fun(bad_stars_idx.shape)

        # Create masks
        erase_masks = [
            np.zeros(star_list[i].shape[2], dtype=bool)
            for i in range(len(star_list))
        ]

        if bad_stars_idx.size > 0:
            # We have to erase the outliers
            # Create the reference ids (to match the global
            # array to the list of arrays)
            idx_ref = np.zeros((len(all_stars), 3), dtype=int)
            glob_id, star_id, ccd_id = 0, 0, 0
            for stars in star_list:
                star_id = 0
                for _star in utils.reg_format(stars):
                    idx_ref[glob_id, 0] = glob_id
                    idx_ref[glob_id, 1] = star_id
                    idx_ref[glob_id, 2] = ccd_id
                    glob_id += 1
                    star_id += 1
                ccd_id += 1

            # select outlier stars
            for bad_id in bad_stars_idx:
                print_fun('Outlier: Glob_id=%d , star_id=%d , ccd_id=%d' %
                          (idx_ref[bad_id, 0], idx_ref[bad_id,
                                                       1], idx_ref[bad_id, 2]))
                erase_masks[idx_ref[bad_id, 2]][idx_ref[bad_id, 1]] = True

            for it_star in range(len(star_list)):
                mask = ~erase_masks[it_star]
                # erase elements and overwrite
                star_list[it_star] = star_list[it_star][:, :, mask]
                mask_list[it_star] = mask_list[it_star][:, :, mask]
                pos_list[it_star] = pos_list[it_star][mask, :]
                if SNR_list is not None:
                    SNR_list[it_star] = SNR_list[it_star][mask]
                if RA_list is not None:
                    RA_list[it_star] = RA_list[it_star][mask]
                    DEC_list[it_star] = DEC_list[it_star][mask]

        return star_list, pos_list, mask_list, ccd_list, SNR_list, RA_list,\
            DEC_list, erase_masks