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_LSQ_SLSQP_with_constraints(self):
        """
        Runs `LevMarLSQFitter` and `SLSQPLSQFitter` on a model with
        constraints.
        """

        g1 = models.Gaussian1D(100, 5, stddev=1)
        g1.mean.fixed = True
        fitter = LevMarLSQFitter()
        fslsqp = SLSQPLSQFitter()
        slsqp_model = fslsqp(g1, self.xdata, self.ydata)
        model = fitter(g1, self.xdata, self.ydata)
        assert_allclose(model.parameters, slsqp_model.parameters,
                        rtol=10 ** (-4))
Example #4
0
def test_psf_photometry_uncertainties_warning_check():
    psf = IntegratedGaussianPRF(sigma=GAUSSIAN_WIDTH)
    with pytest.warns(AstropyUserWarning):
        f = psf_photometry(image,
                           INTAB,
                           psf,
                           fitter=SLSQPLSQFitter(),
                           param_uncert=True)
        assert_equal(
            'flux_fit_uncertainty' in f.colnames
            or 'y_0_fit_uncertainty' in f.colnames
            or 'x_0_fit_uncertainty' in f.colnames, False)
        # test that AstropyUserWarning is raised
        warnings.warn(
            'uncertainties on fitted parameters cannot be '
            'computed because fitter does not contain '
            '`param_cov` key in its `fit_info` dictionary.',
            AstropyUserWarning)
Example #5
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 #6
0
print(GrayBody)
print(GrayBody._supports_unit_fitting)
#exit()
'''


# In[52]:


# Fit graybody
from astropy.modeling.fitting import LevMarLSQFitter, SLSQPLSQFitter
from astropy.utils.console import ProgressBar
from scipy.optimize import curve_fit

#fitter = LevMarLSQFitter()
fitter = SLSQPLSQFitter()

waves = [53, 89, 155, 216]*u.micron

# dataiter holds 4-tuples of each pixel in all 4 wavelengths
dataiter = list(zip(a_orig.data.flat,c_repr.data.flat,d_repr.data.flat,e_repr.data.flat))

# This is the function that we apply to every pixel in the maps.
#   Note that we first convert the data values from Jy to erg/cm2/s to match the output from GrayBody
def fit_gb(y):
    y = (y*u.Jy).to(FNU, equivalencies=u.spectral_density(waves))
    print(y)
    print('wut')
    return fitter(GrayBody, waves, y, maxiter=50, acc=1e-5)

#print(GrayBody)
Example #7
0
    **kwargs :
        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