Example #1
0
    def test_with_fitters_and_sigma_clip(self):
        import scipy.stats as stats

        np.random.seed(0)
        c = stats.bernoulli.rvs(0.25, size=self.z.shape)
        self.z += (np.random.normal(0., 0.2, self.z.shape) +
                   c*np.random.normal(self.z, 2.0, self.z.shape))

        guess = self.initial_guess(self.z, np.array([self.y, self.x]))
        g2_init = models.Gaussian2D(amplitude=guess[0], x_mean=guess[1],
                                    y_mean=guess[2], x_stddev=0.75,
                                    y_stddev=1.25)

        # test with Levenberg-Marquardt Least Squares fitter
        fit = FittingWithOutlierRemoval(LevMarLSQFitter(), sigma_clip,
                                        niter=3, sigma=3.)
        fitted_model, _ = fit(g2_init, self.x, self.y, self.z)
        assert_allclose(fitted_model.parameters[0:5], self.model_params,
                        atol=1e-1)
        # test with Sequential Least Squares Programming fitter
        fit = FittingWithOutlierRemoval(SLSQPLSQFitter(), sigma_clip, niter=3,
                                        sigma=3.)
        fitted_model, _ = fit(g2_init, self.x, self.y, self.z)
        assert_allclose(fitted_model.parameters[0:5], self.model_params,
                        atol=1e-1)
        # test with Simplex LSQ fitter
        fit = FittingWithOutlierRemoval(SimplexLSQFitter(), sigma_clip,
                                        niter=3, sigma=3.)
        fitted_model, _ = fit(g2_init, self.x, self.y, self.z)
        assert_allclose(fitted_model.parameters[0:5], self.model_params,
                        atol=1e-1)
Example #2
0
    def test_with_fitters_and_sigma_clip(self):
        import scipy.stats as stats

        np.random.seed(0)
        c = stats.bernoulli.rvs(0.25, size=self.x.shape)
        self.y += (np.random.normal(0., 0.2, self.x.shape) +
                   c * np.random.normal(3.0, 5.0, self.x.shape))

        g_init = models.Gaussian1D(amplitude=1., mean=0, stddev=1.)
        # test with Levenberg-Marquardt Least Squares fitter
        fit = FittingWithOutlierRemoval(LevMarLSQFitter(),
                                        sigma_clip,
                                        niter=3,
                                        sigma=3.0)
        fitted_model, _ = fit(g_init, self.x, self.y)
        assert_allclose(fitted_model.parameters, self.model_params, rtol=1e-1)
        # test with Sequential Least Squares Programming fitter
        fit = FittingWithOutlierRemoval(SLSQPLSQFitter(),
                                        sigma_clip,
                                        niter=3,
                                        sigma=3.0)
        fitted_model, _ = fit(g_init, self.x, self.y)
        assert_allclose(fitted_model.parameters, self.model_params, rtol=1e-1)
        # test with Simplex LSQ fitter
        fit = FittingWithOutlierRemoval(SimplexLSQFitter(),
                                        sigma_clip,
                                        niter=3,
                                        sigma=3.0)
        fitted_model, _ = fit(g_init, self.x, self.y)
        assert_allclose(fitted_model.parameters, self.model_params, atol=1e-1)
Example #3
0
def test_psf_photometry_uncertainties():
    """
    Test an Astropy fitter that does not return a parameter
    covariance matrix (param_cov). The output table should not
    contain flux_unc, x_0_unc, and y_0_unc columns.
    """
    psf = IntegratedGaussianPRF(sigma=GAUSSIAN_WIDTH)

    basic_phot = BasicPSFPhotometry(group_maker=DAOGroup(2),
                                    bkg_estimator=None, psf_model=psf,
                                    fitter=SimplexLSQFitter(),
                                    fitshape=7)
    phot_tbl = basic_phot(image=image, init_guesses=INTAB)
    columns = ('flux_unc', 'x_0_unc', 'y_0_unc')
    for column in columns:
        assert column not in phot_tbl.colnames
Example #4
0
def test_fitters_interface():
    """
    Test that ``**kwargs`` work with all optimizers.
    This is a basic smoke test.
    """
    levmar = LevMarLSQFitter()
    slsqp = SLSQPLSQFitter()
    simplex = SimplexLSQFitter()

    kwargs = {'maxiter': 77, 'verblevel': 1, 'epsilon': 1e-2, 'acc': 1e-6}
    simplex_kwargs = {'maxiter': 77, 'verblevel': 1, 'acc': 1e-6}
    model = models.Gaussian1D(10, 4, .3)
    x = np.arange(21)
    y = model(x)

    _ = slsqp(model, x, y, **kwargs)
    _ = simplex(model, x, y, **simplex_kwargs)
    kwargs.pop('verblevel')
    _ = levmar(model, x, y, **kwargs)
Example #5
0
    def test_simplex_lsq_fitter(self):
        """A basic test for the `SimplexLSQ` fitter."""
        class Rosenbrock(Fittable2DModel):
            a = Parameter()
            b = Parameter()

            @staticmethod
            def evaluate(x, y, a, b):
                return (a - x)**2 + b * (y - x**2)**2

        x = y = np.linspace(-3.0, 3.0, 100)
        with NumpyRNGContext(_RANDOM_SEED):
            z = Rosenbrock.evaluate(x, y, 1.0, 100.0)
            z += np.random.normal(0., 0.1, size=z.shape)

        fitter = SimplexLSQFitter()
        r_i = Rosenbrock(1, 100)
        r_f = fitter(r_i, x, y, z)

        assert_allclose(r_f.parameters, [1.0, 100.0], rtol=1e-2)
Example #6
0
        Keyword arguments for the fitter (name and astropy default values)::

          * `LinearLSQFitter`: `calc_uncertainties=False`
          * `LevMarLSQFitter`: `calc_uncertainties=False`
          * `SimplexLSQFitter`, `SLSQPLSQFitter`: N/A
          * `JointFitter`: `models`, jointparameters`, `initvals` (must be given)
    """
    if fitter_name.lower().startswith("lev") or fitter_name.lower().startswith(
            "lm"):
        return LevMarLSQFitter(**kwargs)
    elif fitter_name.lower().startswith("lin"):
        return LinearLSQFitter(**kwargs)
    elif fitter_name.lower().startswith("sl"):
        return SLSQPLSQFitter(**kwargs)
    elif fitter_name.lower().startswith("sim"):
        return SimplexLSQFitter(**kwargs)
    elif fitter_name.lower().startswith("jo"):
        return JointFitter(**kwargs)
    else:
        return fitter_name(**kwargs)
        # ^ assume the `fitter_name` is already an astropy fitter


def get_model(model_name, *args, **kwargs):
    """ Finds and returns the model with the given name.
    For instance, it's customary to put degrees as args/kwargs for polynomial models:
    `get_model("chebyshev2d", 2, 2)` or `get_model("chebyshev2d", x_degree=2,
    y_degree=2)` return `Chebyshev2D(x_degree=2, y_degree=2)`.
    For other cases, it depends: `get_model("gaussian1d")` returns
    `Gaussian1D`, but sometimes you may want to initialize it with initial
    values: `get_model("gaussian1d", amplitude=1, mean=0, stddev=1)`.