예제 #1
0
def test_Shift_model_set_linear_fit():
    """Test linear fitting of Shift model (issue #6103)."""

    init_model = models.Shift(offset=[0, 0], n_models=2)

    x = np.arange(10)
    yy = np.array([x+0.1, x-0.2])

    fitter = fitting.LinearLSQFitter()
    fitted_model = fitter(init_model, x, yy)

    assert_allclose(fitted_model.parameters, [0.1, -0.2], atol=1e-15)
예제 #2
0
def test_Scale_model_set_linear_fit(Model):
    """Test linear fitting of Scale model (#6103)."""

    init_model = Model(factor=[0, 0], n_models=2)

    x = np.arange(-3, 7)
    yy = np.array([1.15*x, 0.96*x])

    fitter = fitting.LinearLSQFitter()
    fitted_model = fitter(init_model, x, yy)

    assert_allclose(fitted_model.parameters, [1.15, 0.96], atol=1e-15)
def fitModel(calcX, calcY, dsimX, dsimY):
    """
    Fit the 4th deg 2D polynomial function to map from original to calculated.
    Returns the fitted X/Y.
    """
    pdeg = 4
    model0 = models.Polynomial2D(degree=pdeg)
    fitter = fitting.LinearLSQFitter()

    xfitted = fitter(model0, calcX, calcY, dsimX)
    yfitted = fitter(model0, calcX, calcY, dsimY)
    return xfitted, yfitted
예제 #4
0
    def test_wrong_numpset(self):
        """
        A ValueError is raised if a 1 data set (1d x, 1d y) is fit
        with a model with multiple parameter sets.
        """

        with pytest.raises(ValueError):
            p1 = models.Polynomial1D(5)
            y1 = p1(self.x1)
            p1 = models.Polynomial1D(5, n_models=2)
            pfit = fitting.LinearLSQFitter()
            model = pfit(p1, self.x1, y1)
예제 #5
0
def test_create_slit_illumination_with_multi_extension_data(
        ad, change_working_dir, request):
    """
    Test that can run `makeSlitIllum` in multi-extension data.
    """
    plot = request.config.getoption("--do-plots")

    with change_working_dir():

        cwd = os.getcwd()
        print("Running tests inside folder:\n  {}".format(cwd))
        p = primitives_gmos_longslit.GMOSLongslit([ad])
        p.makeSlitIllum(bins=25, border=10, debug_plot=plot)
        slit_illum_ad = p.writeOutputs()[0]

        for ext, slit_ext in zip(ad, slit_illum_ad):
            assert ext.shape == slit_ext.shape

            # Create output data
            data_o = (np.ma.masked_array(ext.data, mask=ext.mask) /
                      np.ma.masked_array(slit_ext.data, mask=slit_ext.mask))

            # Bin columns
            fitter = fitting.LinearLSQFitter()
            model = models.Polynomial1D(degree=2)
            nbins = 10
            rows = np.arange(data_o.shape[0])

            for i in range(nbins):

                col_start = i * data_o.shape[1] // nbins
                col_end = (i + 1) * data_o.shape[1] // nbins

                cols = np.ma.mean(data_o[:, col_start:col_end], axis=1)

                fitted_model = fitter(model, rows, cols)

                # Check column is linear
                np.testing.assert_allclose(fitted_model.c2.value, 0, atol=0.01)

                # Check if slope is (almost) horizontal (< 2.0 deg)
                assert np.abs(
                    np.rad2deg(
                        np.arctan(fitted_model.c1.value /
                                  (rows.size // 2)))) < 1.5

    if plot:
        os.makedirs(PLOT_PATH, exist_ok=True)
        print("Renaming plots to ",
              os.path.join(PLOT_PATH, ad.filename.replace(".fits", ".png")))
        os.rename(
            os.path.join(cwd, slit_illum_ad.filename.replace(".fits", ".png")),
            os.path.join(PLOT_PATH, ad.filename.replace(".fits", ".png")))
예제 #6
0
def make_channel_models(channel):
    f = fits.open('MIRI_FM_LW_A_D2C_01.00.00.fits')
    if channel == 3:
        slice_mask = f[3].data[:, :500]
        b1 = f[0].header['B_DEL3']
        b0 = f[0].header['B_MIN3']

    elif channel == 4:
        slice_mask = f[3].data[:, 500:]
        b1 = f[0].header['B_DEL4']
        b0 = f[0].header['B_MIN4']

    slices = np.unique(slice_mask)
    slices = np.asarray(slices, dtype=np.int16).tolist()
    slices.remove(0)

    #read alpha and lambda planes from pixel maps file
    lam = f[1].data
    alpha = f[2].data
    # create a model to fit to each slice in alpha plane
    amodel = models.Chebyshev2D(x_degree=2, y_degree=1)
    # a model to be fitted to each slice in lambda plane
    lmodel = models.Chebyshev2D(x_degree=1, y_degree=1)
    lmodel.c0_1.fixed = True
    lmodel.c1_1.fixed = True
    fitter = fitting.LinearLSQFitter()
    reg_models = {}
    for sl in slices:
        #print('sl', sl)
        ind = (
            slice_mask == sl).nonzero()  #(slice_mask[:, :500] ==sl).nonzero()
        x0 = ind[0].min()
        x1 = ind[0].max()
        y0 = ind[1].min()
        y1 = ind[1].max()
        if channel == 4:
            y0 += 500
            y1 += 500
        x, y = np.mgrid[x0:x1, y0:y1]
        sllam = lam[x0:x1, y0:y1]
        slalpha = alpha[x0:x1, y0:y1]
        lfitted = fitter(lmodel, x, y, sllam)
        afitted = fitter(amodel, x, y, slalpha)

        if channel == 4:
            beta_model = models.Const1D(b0 + b1 * (sl + 12))
            reg_models[sl + 12] = lfitted, afitted, beta_model, (x0, x1, y0,
                                                                 y1)
        else:
            beta_model = models.Const1D(b0 + b1 * sl)
            reg_models[sl] = lfitted, afitted, beta_model, (x0, x1, y0, y1)

    return reg_models
예제 #7
0
    def _setup_kl_a_fields(self, inf_loss=None, updating=False):
        """Calculate the coefficients of the expansion in basis of KLoeve.

        """
        inf_loss_update = (inf_loss is not None) and \
                          (self.inf_loss != inf_loss)

        if not hasattr(self, '_a_fields') or inf_loss_update or updating:
            if inf_loss is not None:
                self._setup_kl_basis(inf_loss)
                self.inf_loss = inf_loss
            # get the psf basis
            psf_basis = self.kl_basis
            n_fields = len(psf_basis)

            # if only one psf then the afields is  [None]
            if n_fields == 1:
                self._a_fields = [None]
                return self._a_fields

            # get the sources for observations
            best_srcs = self.best_sources  # noqa
            positions = self.stamps_pos
            x = positions[:, 0]
            y = positions[:, 1]

            # Each element in patches brings information about the real PSF
            # evaluated -or measured-, giving an interpolation point for a
            a_fields = []
            # measures = np.zeros((n_fields, self.n_sources))
            # for k in range(self.n_sources):
            #    # Pval = self.db.load(k)[0].flatten()
            #    # Pval = Pval/np.sum(Pval)
            #    # for i in range(n_fields):
            #        # p_i = psf_basis[i].flatten()  # starting from bottom
            #        # p_i_sq = np.sqrt(np.sum(np.dot(p_i, p_i)))

            #        # Pval_sq = np.sqrt(np.sum(np.dot(Pval, Pval)))
            #        # m = np.dot(Pval, p_i)
            #        # m = m/(Pval_sq*p_i_sq)
            #        # measures[i, k] = m
            #    # else:
            #        # measures[i, k] = None
            measures = np.flip(self.eigenv[1][:, -n_fields:].T, 0)
            for i in range(n_fields):
                z = measures[i, :]
                a_field_model = models.Polynomial2D(degree=3)
                fitter = fitting.LinearLSQFitter()
                fit = fitter(a_field_model, x, y, z)

                a_fields.append(fit)
            self._a_fields = a_fields
예제 #8
0
파일: a0v_spec.py 프로젝트: zyajay/plp
def get_flattend(a0v_spec,
                 a0v_wvl, a0v_tel_trans_masked, wvl_solutions, s_list,
                 i1i2_list=None):

    a0v_flattened = []

    if i1i2_list is None:
        i1i2_list = [[0, -1]] * len(wvl_solutions)

    a0v_interp1d = a0v_spec.get_flux_interp1d(a0v_wvl[0], a0v_wvl[-1],
                                              flatten=True,
                                              smooth_pixel=32)

    for wvl, s, (i1, i2) in zip(wvl_solutions, s_list, i1i2_list):

        wvl1, wvl2 = wvl[i1], wvl[i2]
        z_m = (wvl1 < a0v_wvl) & (a0v_wvl < wvl2)

        ss = interp1d(wvl, s)

        x_s = a0v_wvl[z_m]
        if len(x_s):

            s_interped = ss(x_s)

            xxx, yyy = a0v_wvl[z_m], s_interped/a0v_tel_trans_masked[z_m]

            p_init = models.Chebyshev1D(domain=[wvl1, wvl2],
                                        degree=6)
            fit_p = fitting.LinearLSQFitter()
            x_m = np.isfinite(yyy)
            p = fit_p(p_init, xxx[x_m], yyy[x_m])

            res_ = p(wvl)

            s_f = s/res_

            # now divide by A0V
            a0v = a0v_interp1d(wvl)


            s_f = s_f/a0v

            z_m = (wvl1 < wvl) & (wvl < wvl2)
            s_f[~z_m] = np.nan

        else:
            s_f = s / np.nanmax(s)

        a0v_flattened.append(s_f)

    return a0v_flattened
예제 #9
0
파일: test_input.py 프로젝트: architg1/taar
    def test_linear_fitter_1dcheb(self):
        """1 pset, 1 set 1D x, 1 set 1D y, Chebyshev 1D polynomial"""

        expected = np.array([[
            2817.2499999999995, 4226.6249999999991, 1680.7500000000009,
            273.37499999999926
        ]]).T
        ch1 = models.Chebyshev1D(3)
        ch1.parameters = [0, 1, 2, 3]
        y1 = ch1(self.x1)
        pfit = fitting.LinearLSQFitter()
        model = pfit(ch1, self.x1, y1)
        assert_allclose(model.param_sets, expected, atol=10**(-2))
예제 #10
0
파일: test_input.py 프로젝트: architg1/taar
    def test_wrong_pset(self):
        """A case of 1 set of x and multiple sets of y and parameters."""

        expected = np.array([[1., 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5]])
        p1 = models.Polynomial1D(5, n_models=2)
        params = {}
        for i in range(6):
            params[p1.param_names[i]] = [1, i]
        p1 = models.Polynomial1D(5, model_set_axis=0, **params)
        y1 = p1(self.x1, model_set_axis=False)
        pfit = fitting.LinearLSQFitter()
        model = pfit(p1, self.x1, y1)
        assert_allclose(model.param_sets, expected, atol=10**(-7))
예제 #11
0
    def model_constructor(self):
        """Generates callable mathematical model

        It can do chebyshev and linear model only but is easy to implement
        others. Chebyshev 3rd degree is by default since provided the best
        results for Goodman data.
        """
        if self.model_name == 'chebyshev':
            self.model = models.Chebyshev1D(degree=self.degree)
            self.model_fit = fitting.LevMarLSQFitter()
        elif self.model_name == 'linear':
            self.model = models.Linear1D()
            self.model_fit = fitting.LinearLSQFitter()
예제 #12
0
def iterate_spatial_profile(P, DmS, V, f,\
                            smoothing=5, order=3, minpixels=50,\
                            sigma=4, nclip=2, verbose=True):
    poly0 = models.Polynomial1D(degree=order)
    fit_poly = fitting.LinearLSQFitter()    

    Pnew = np.zeros(P.shape)
    for i,row in enumerate(P):
        weights = f**2/V[i]
        weights.mask = weights.mask | np.isnan(weights)
        srow = np.ma.MaskedArray(data=signal.medfilt(row, smoothing),\
                     mask=(np.isnan(signal.medfilt(row, smoothing)) | weights.mask))
        xcoord = np.ma.MaskedArray(data=np.arange(0,len(row),1),\
                                   mask=srow.mask)

        for iter in range(nclip+1):
            nrej_before = np.sum(srow.mask)
            fitted_poly = fit_poly(poly0,\
                                   xcoord[~xcoord.mask], srow[~srow.mask],\
                                   weights=weights[~weights.mask])
            fit = np.array([fitted_poly(x) for x in xcoord])
            resid = (DmS[i]-f*srow)**2 / V[i]
            newmask = (resid > sigma)
            
            weights.mask = weights.mask | newmask
            srow.mask = srow.mask | newmask
            xcoord.mask = xcoord.mask | newmask

            nrej_after = np.sum(srow.mask)
            if nrej_after > nrej_before:
                if verbose:\
                    info('Row {:3d}: Rejected {:d} pixels on clipping '+\
                          'iteration {:d}'.format(\
                           i, nrej_after-nrej_before, iter))
        
        ## Reject row if too few pixels are availabel for the fit
        if (srow.shape[0] - nrej_after) < minpixels:
            if verbose:
                warning('Row {:3d}: WARNING! Only {:d} pixels remain after '+\
                        'clipping and masking'.format(\
                      i, srow.shape[0] - nrej_after))
            fit = np.zeros(fit.shape)
        ## Set negative values to zero
        if np.sum((fit<0)) > 0 and verbose:
            info('Row {:3d}: Reset {:d} negative pixels in fit to 0'.format(\
                  i, np.sum((fit<0))))
        fit[(fit < 0)] = 0
        Pnew[i] = fit
    
    return Pnew
예제 #13
0
파일: test_input.py 프로젝트: architg1/taar
    def test_linear_fitter_Nset(self):
        """1 set 1D x, 2 sets 1D y, 2 param_sets"""

        expected = np.array([[0, 0], [1, 1], [2, 2], [3, 3]])
        p1 = models.Polynomial1D(3, n_models=2)
        p1.parameters = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0]
        params = {}
        for i in range(4):
            params[p1.param_names[i]] = [i, i]
        p1 = models.Polynomial1D(3, model_set_axis=0, **params)
        y1 = p1(self.x1, model_set_axis=False)
        pfit = fitting.LinearLSQFitter()
        model = pfit(p1, self.x1, y1)
        assert_allclose(model.param_sets, expected, atol=10**(-7))
예제 #14
0
파일: prova.py 프로젝트: cloud182/star_fit
def function8():
    directory = '/media/congiu/Data/Dati/PHANGS/star_fit/plots/'

    fitter = fitting.LinearLSQFitter()
    model = models.Linear1D(1, 1)
    for file in os.listdir(directory):
        filename = os.fsdecode(file)
        if filename.endswith('circ.txt'):
            table = ascii.read(directory + filename)
            fit = fitter(model,
                         table['wavelength'],
                         table['fwhm_y'],
                         weights=1 / table['err_fwhm_y'])
            print(filename, fit(6562))
예제 #15
0
def polynomial_fit(x, y, order=3):
    """ Fit a polynomial of the specified order to the given
        x and y data, return an astropy.modeling.Model fit to
        the data.
    """

    if len(x) != len(y):
        raise ValueError("x and y must have the sample shape!")

    p = models.Polynomial1DModel(order)
    fit = fitting.LinearLSQFitter(p)
    fit(x, y)

    return p
예제 #16
0
def calc_pseudo_ew(wave,
                   flux,
                   continuum_l,
                   continuum_r,
                   absorption=True,
                   visualize=False):
    '''
    wave: array
        array of wavelength (can be whole spectrum)
    flux: array
        array of fluxes (can be whole spectrum)
    
    * Create a fit to the continuum and define the continuum for each wavelength in wave
    * Use continuum wavelengths to define index location of feature
    * Calc pseudo equivalent width using flux, continuum, and delta wave as calculated from the
    wave array
    '''
    fitter = fitting.LinearLSQFitter()
    lin_mod = models.Linear1D()
    continuum_fit = fitter(lin_mod, [continuum_l.wave, continuum_r.wave],
                           [continuum_l.flux, continuum_r.flux])
    line_indx = np.int_(
        np.arange(len(wave))[(wave >= continuum_l.wave)
                             & (wave <= continuum_r.wave)])
    continuum = continuum_fit(wave[line_indx])
    delta_lambda = wave[line_indx] - wave[line_indx - 1]
    if absorption is True:
        pew = np.sum(delta_lambda * (continuum - flux[line_indx]) / continuum)
    else:
        pew = np.sum(delta_lambda * (flux[line_indx] - continuum) /
                     continuum)  #Check that this is true
    if visualize is True:
        fig = plt.figure()
        ax1 = fig.add_subplot(1, 2, 1)
        ax2 = fig.add_subplot(1, 2, 2)
        ax2.axhline(1, color='k')
        ax1.plot(wave, flux)
        ax1.plot(wave[line_indx], flux[line_indx], label='data')
        ax1.plot(wave[line_indx], continuum, label='continuum')
        ax1.set_xlim(continuum_l.wave - 10, continuum_r.wave + 10)
        if absorption is True:
            ax2.plot(wave[line_indx],
                     (continuum - flux[line_indx]) / continuum,
                     label='sum for pEW')
        else:
            ax2.plot(wave[line_indx],
                     (flux[line_indx] - continuum) / continuum,
                     label='sum for pEW')
    return pew
예제 #17
0
파일: prova.py 프로젝트: cloud182/star_fit
def function7():
    """ 
    Fa il plot del seeing a 6562 A con il i parametri location ottenuti 
    dal fit della distribuzione della FWHM delle regioni H II
    """

    directory = '/media/congiu/Data/Dati/PHANGS/star_fit/plots/'

    ha_table = ascii.read(
        '/media/congiu/Data/Dati/PHANGS/star_fit/reliable_pointing_ha.txt')
    seeing_ha = []
    location = []
    location_err = []
    fitter = fitting.LinearLSQFitter()
    model = models.Linear1D(1, 1)
    fig, ax = plt.subplots(1, 1, figsize=(8, 6))
    for file in os.listdir(directory):
        filename = os.fsdecode(file)
        if filename.startswith('hii'):
            table = ascii.read(directory + filename)
            for i, value in enumerate(ha_table['pointing']):
                if value in filename:
                    seeing_ha.append(ha_table['fwhm'][i])

                    location.append(table['e_location'][0])
                    location_err.append(table['e_location_err'][0])
                    ax.errorbar(ha_table['fwhm'][i],
                                table['e_location'],
                                yerr=table['e_location_err'],
                                marker='o',
                                label=value)
    print(location)
    location_err = np.array(location_err)
    fit = fitter(model, seeing_ha, location, weights=1 / location_err)
    plt.plot(seeing_ha, fit(seeing_ha), label='fit')
    chi2 = np.sum(
        (location - fit(seeing_ha))**2 / fit(seeing_ha)) / (len(location) - 2)
    ax.set_xlabel('FWHM at 6562A')
    ax.set_ylabel('loc')
    ax.set_xlim([0.4, 1.1])
    ax.set_ylim([0.4, 1.1])
    ax.plot([0.4, 1.1], [0.4, 1.1], label='bisector')
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
    plt.plot([], [], ls='', label='chi2 = {:0.2f}'.format(chi2))
    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    ax.set_title('location (exponnorm)')
    plt.savefig('location_exponnorm.png')
    plt.close()
예제 #18
0
파일: test_input.py 프로젝트: architg1/taar
    def test_linear_fitter_1dlegend(self):
        """
        1 pset, 1 set 1D x, 1 set 1D y, Legendre 1D polynomial
        """

        expected = np.array([[
            1925.5000000000011, 3444.7500000000005, 1883.2500000000014,
            364.4999999999996
        ]]).T
        leg1 = models.Legendre1D(3)
        leg1.parameters = [1, 2, 3, 4]
        y1 = leg1(self.x1)
        pfit = fitting.LinearLSQFitter()
        model = pfit(leg1, self.x1, y1)
        assert_allclose(model.param_sets, expected, atol=10**(-12))
예제 #19
0
 def run_model(self, label):
     model = []
     k = 1
     if label == "Const1D":
         model = self.make_astropy_model(models.Const1D,
                                         fitting.LinearLSQFitter())
         k = 1
     if label == "Linear1D":
         model = self.make_astropy_model(models.Linear1D,
                                         fitting.LinearLSQFitter())
         k = 2
     if label == "Parabola1D":
         model = self.make_astropy_model(models.Polynomial1D,
                                         fitting.LinearLSQFitter(), 2)
         k = 3
     else:
         labels = label.split(" ")
         if labels[0] == "Sine1D":
             freq = float(labels[1])
             amp = float(labels[2])
             model = self.make_scipy_model(self._sine_model, self._simple_err_func, \
                                           [amp, freq, 0], (None, np.inf))
             k = 3
     return self._get_ssr(model), self._get_bic(mode1l, k)
예제 #20
0
파일: model.py 프로젝트: gbrammer/wfc3
    def align_spectrum(self, xspec=None, yspec=None):
        """
        Try to compute alignment of the reference image using cross correlation
        """
        from astropy.modeling import models, fitting

        clean_cutout = self.cutout_sci * 1.
        clean_cutout[self.cutout_dq > 0] = 0
        #max = np.percentile(clean_cutout[clean_cutout != 0], clip_percentile)
        #clean_cutout[(clean_cutout > max) | (clean_cutout < -3*self.cutout_err)] = 0
        clean_cutout[(clean_cutout < -3 * self.cutout_err)
                     | ~np.isfinite(self.cutout_err)] = 0.

        self.compute_model(self.thumb, xspec=xspec, yspec=yspec)

        ### Cross correlation
        cc = nd.correlate(self.model / self.model.sum(),
                          clean_cutout / clean_cutout.sum())

        sh = cc.shape
        shx = sh[1] / 2.
        shy = sh[0] / 2.

        yp, xp = np.indices(cc.shape)
        shx = sh[1] / 2
        shy = sh[0] / 2
        xp = (xp - shx)
        yp = (yp - shy)

        cc[:, :shx - shy] = 0
        cc[:, shx + shy:] = 0
        ccy = cc.sum(axis=1)
        ccx = cc.sum(axis=0)

        #fit = fitting.LevMarLSQFitter()
        #mod = models.Polynomial1D(degree=6) #(1, 0, 1)
        fit = fitting.LinearLSQFitter()

        ix = np.argmax(ccx)
        p2 = models.Polynomial1D(degree=2)
        px = fit(p2, xp[0, ix - 1:ix + 2], ccx[ix - 1:ix + 2] / ccx.max())
        dx = -px.parameters[1] / (2 * px.parameters[2])

        iy = np.argmax(ccy)
        py = fit(p2, yp[iy - 1:iy + 2, 0], ccy[iy - 1:iy + 2] / ccy.max())
        dy = -py.parameters[1] / (2 * py.parameters[2])

        return dx, dy, ccx, ccy
예제 #21
0
def fit_meanphot_vs_varphot(meanphot, varphot, nfr=5, itersigma=4.0, niter=5):

    fit = fitting.LinearLSQFitter()
    # initialize the outlier removal fitter
    or_fit = fitting.FittingWithOutlierRemoval(fit,
                                               sigma_clip,
                                               niter=niter,
                                               sigma=itersigma)
    # initialize a linear model
    line_init = models.Linear1D()
    # fit the data with the fitter
    sigmask, fitted_line = or_fit(line_init, meanphot, varphot)
    slope = fitted_line.slope.value
    g1_iter = get_g1_from_slope_T_N(slope, N=nfr)

    return fitted_line, sigmask, g1_iter
예제 #22
0
    def _model_constructor(self):
        """Generates callable mathematical model

        It can do Chebyshev and Linear model only but is easy to implement
        others. Chebyshev 3rd degree is by default since provided the best
        results for Goodman data.
        """
        if self.model_name == 'chebyshev':
            self.model = models.Chebyshev1D(degree=self.degree)
            self.model_fitter = fitting.LevMarLSQFitter()
        elif self.model_name == 'linear':
            self.model = models.Linear1D()
            self.model_fitter = fitting.LinearLSQFitter()
        else:
            raise NotImplementedError("The model {:s} is "
                                      "not implemented".format(self.model_name))
예제 #23
0
파일: prova.py 프로젝트: cloud182/star_fit
def function10():
    """
    faccio il plot del seeing misurato con le nebulose e di quello dalle stelle
    """


    pn_table = ascii.read('/media/congiu/Data/Dati/PHANGS/PN_selection/'\
                          +'psf_planetary.txt')
    o3_table = ascii.read(
        '/media/congiu/Data/Dati/PHANGS/star_fit/reliable_pointing_o3.txt')

    new_table = join(pn_table, o3_table, keys='pointing')

    fitter = fitting.LinearLSQFitter()
    model = models.Linear1D(1, 1)

    fig, ax = plt.subplots(1, 1, figsize=(8, 6))

    for o3, pn, err, region in zip(new_table['fwhm_2'], new_table['fwhm_1'],
                                   new_table['fwhm_err_1'],
                                   new_table['pointing']):

        ax.errorbar(o3, pn, yerr=err, label=region, ls='', marker='o')

    fit = fitter(model,
                 new_table['fwhm_2'],
                 new_table['fwhm_1'],
                 weights=1 / new_table['fwhm_err_1'])
    print(len(new_table['fwhm_2'].pformat()))

    chi2 = np.sum((new_table['fwhm_1']-fit(new_table['fwhm_2']))**2\
                  / fit(new_table['fwhm_2']))/(len(new_table['fwhm_2'].pformat()))

    plt.plot(new_table['fwhm_2'], fit(new_table['fwhm_2']), label='fit')
    ax.set_xlabel('FWHM at 5007A')
    ax.set_ylabel('PN FWHM')
    ax.set_xlim([0.5, 1.2])
    ax.set_ylim([0.5, 1.2])
    ax.plot([0.4, 1.2], [0.4, 1.2], label='bisector')
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
    plt.plot([], [], ls='', label='chi2 = {:0.2f}'.format(chi2))
    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    ax.set_title('PN FWHM (exponnorm)')
    #    plt.savefig('location_exponnorm.png')
    #    plt.close()
    plt.show()
예제 #24
0
def fit_poly_n(data, deg=1, sigma_factor=0):
    """Fit a Polynomial 1D model to the data.

    Parameters
    ----------

    data: float array
        should be a 1d or 2d array
    deg: int
        The degree of polynomial to fit
    sigma_factor: float (optional)
        If sigma_factor > 0 then clipping will be performed
        on the data during the model fit

    Returns
    -------
    The the polynomial fit model for the function
    """
    if len(data) < deg + 1:
        raise ValueError("fit_poly_n: Need more data for fit")

    # define the model
    poly = models.Polynomial1D(deg)

    # set the axis range for fitting
    ax = np.arange(len(data))

    # define the fitter
    fitter = fitting.LinearLSQFitter()

    if sigma_factor > 0:
        fit = fitting.FittingWithOutlierRemoval(fitter,
                                                sigma_clip,
                                                sigma=sigma_factor,
                                                niter=3)
    else:
        fit = fitter

    try:
        result = fit(poly, ax, data)
    except ValueError:
        result = None

    if sigma_factor > 0:
        result = result[1]

    return result
예제 #25
0
def fit_poly_n(data, x=None, y=None, deg=1):
    """Fit a Polynomial 1D model to the data."""

    # define the model
    poly = models.Polynomial1D(deg)

    # set the axis range for fitting
    ax = np.arange(len(data))

    # define the fitter
    fit = fitting.LinearLSQFitter()
    try:
        result = fit(poly, ax, data)
    except:
        ValueError
        result = None
    return result
예제 #26
0
파일: model.py 프로젝트: gbrammer/wfc3
    def fit_background(self,
                       degree=3,
                       sn_limit=0.1,
                       pfit=None,
                       apply=True,
                       verbose=True,
                       ds9=None):
        from astropy.modeling import models, fitting

        yp, xp = np.indices(self.sh_pad)
        xp = (xp - self.sh_pad[1] / 2.) / (self.sh_flt[1] / 2)
        yp = (yp - self.sh_pad[0] / 2.) / (self.sh_flt[0] / 2)

        if pfit is None:
            mask = (self.im_data['DQ']
                    == 0) & (self.model / self.im_data['ERR'] < sn_limit) & (
                        self.im_data['SCI'] != 0) & (
                            self.im_data['SCI'] > -4 * self.im_data['ERR']) & (
                                self.im_data['SCI'] < 6 * self.im_data['ERR'])
            poly = models.Polynomial2D(degree=degree)
            fit = fitting.LinearLSQFitter()
            pfit = fit(poly, xp[mask], yp[mask], self.im_data['SCI'][mask])
            pout = pfit(xp, yp)

            if ds9:
                ds9.view((self.im_data['SCI'] - pout) * mask)
        else:
            pout = pfit(xp, yp)

        if apply:
            if self.pad > 0:
                slx = slice(self.pad, -self.pad)
                sly = slice(self.pad, -self.pad)

            else:
                slx = slice(0, self.sh_flt[1])
                sly = slice(0, self.sh_flt[0])

            self.im_data['SCI'][sly, slx] -= pout[sly, slx]
            self.im_data_sci_background = True

        if verbose:
            print 'fit_background, %s: p0_0=%7.4f' % (self.flt_file,
                                                      pfit.parameters[0])

        self.fit_background_result = pfit  #(pfit, xp, yp)
예제 #27
0
def gwa_to_ymsa(msa2gwa_model):
    """
    Determine the linear relation d_y(beta_in) for the aperture on the detector.

    Parameters
    ----------
    msa2gwa_model : `astropy.modeling.core.Model`
        The transform from the MSA to the GWA.
    """
    dy = np.linspace(-.55, .55, 1000)
    dx = np.zeros(dy.shape)
    cosin_grating_k = msa2gwa_model(dx, dy)
    fitter = fitting.LinearLSQFitter()
    model = models.Polynomial1D(1)
    poly1d_model = fitter(model, cosin_grating_k[1], dy)
    poly_model = poly1d_model.rename('interpolation')
    return poly_model
예제 #28
0
def fit_line_sigma_clip(x,
                        y,
                        yunc=None,
                        slope=1,
                        intercept=0,
                        niter=5,
                        sigma=3):
    '''
    INPUT
    * x - array of x values
    * y - array of y values

    OPTIONAL INPUT
    * yunc - uncertainty in y values
    * slope - intial guess for slope
    * intercept - intial guess for intercept
    * niter - number of sigma clipping iterations; default is 3
    * sigma - sigma to use in clipping; default is 3

    RETURNS
    * Linear1D model, with slope and intercept
      * you can get fitted y values with fitted_line(x)
    * mask - array indicating the points that were cut in sigma clipping process

    REFERENCES
    https://docs.astropy.org/en/stable/modeling/example-fitting-line.html
    '''
    if yunc is None:
        yunc = np.ones(len(y))
    # initialize a linear fitter
    fit = fitting.LinearLSQFitter()

    # initialize the outlier removal fitter
    or_fit = fitting.FittingWithOutlierRemoval(fit,
                                               sigma_clip,
                                               niter=niter,
                                               sigma=sigma)

    # initialize a linear model
    line_init = models.Linear1D(slope=slope, intercept=intercept)

    # fit the data with the fitter
    fitted_line, mask = or_fit(line_init, x, y, weights=1.0 / yunc)

    return fitted_line, mask
예제 #29
0
파일: prova.py 프로젝트: cloud182/star_fit
def function1():
    """
    confronta la fwhm a 5000A con una delle misure di seeing salvate 
    nell'header
    """

    info = ascii.read('./info_header')

    seeing = ascii.read(
        '/media/congiu/Data/Dati/PHANGS/star_fit/reliable_pointing_ha.txt')

    new_table = join(info, seeing, keys='pointing')

    fig, ax = plt.subplots(1, 1, figsize=(10, 10))

    for i, value in enumerate(new_table['pointing']):
        ax.errorbar(new_table['seeing3'][i],
                    new_table['fwhm'][i],
                    new_table['fwhm_err'][i],
                    ls='',
                    marker='o',
                    label=value)

    fitter = fitting.LinearLSQFitter()
    model = models.Linear1D(1, 0)
    fit = fitter(model, new_table['seeing2'],
                 new_table['fwhm'])  #, weights = 1/new_table['fwhm_err'])
    chi2 = np.sum((new_table['fwhm'] - fit(new_table['seeing3']))**2/(fit(new_table['seeing3'])))\
                    /(len(new_table['seeing2'])-2)

    ax.set_xlim(0.6, 1.3)
    ax.set_ylim(0.6, 1.3)
    ax.plot(new_table['seeing3'], fit(new_table['seeing3']), label='fit')
    ax.plot([], [], ls='', label='m = {:1.2f}'.format(fit.slope[0]))
    ax.plot([], [], ls='', label='q = {:1.2f}'.format(fit.intercept[0]))
    ax.plot([], [], ls='', label='chi2 = {:1.3f}'.format(chi2))
    plt.plot([0, 2], [0, 2], c='k', ls='--')
    ax.set_xlabel('Seeing from header (arcsec)')
    ax.set_ylabel('FWHM at 6500A')
    ax.set_title('FWHMLISOBS')
    plt.legend(loc='best')
    #    plt.savefig('fwhm_seeing.png', dpi = 150)
    #    plt.close()
    plt.show()
예제 #30
0
def plot(filenames, Flat = True):
    time_table, counts_unique_table = tab(filenames)
    counts_unique = np.squeeze(np.array([counts_unique_table[k] for k in counts_unique_table.keys()]))
    time = np.squeeze(np.array([time_table[k] for k in time_table.keys()]))
    if Flat == True:      
        plt.figure()
        fit = fitting.LinearLSQFitter()
        or_fit = fitting.FittingWithOutlierRemoval(fit, sigma_clip, niter=2, sigma=2.5)
        line_init = models.Linear1D()

        fitted_line, mask = or_fit(line_init, time, counts_unique)
        filtered_data = np.ma.masked_array(counts_unique, mask=mask)
        plt.plot(time, counts_unique, 'ko', fillstyle='none')
        plt.plot(time, filtered_data, 'ko')
        plt.plot(time, fitted_line(time), 'k-')
        plt.xlabel('Time (s)')
        plt.ylabel('Counts')
        plt.title('Atik T=-1 Flat')
        plt.savefig('Atik_T=-1_Flat.pdf')

    else:    
        time = sorted(np.squeeze(np.array([time_table[k] for k in time_table.keys()])))

        fig1,ax1 = plt.subplots()
        counts_norm = counts_unique/max(counts_unique)
        ax1.errorbar(time, counts_norm,
                     yerr = np.std(counts_norm, dtype=np.float64),
                    fmt = ' ',
                    marker = 'o',
                    markersize = 5,
                    elinewidth=1)
                   
        ax1.set_xlabel('Time (s)')
        ax1.set_ylabel('Counts')
        ax1.legend(['Block1', 'Block2', 'Block3', 'Block4', 'Block5', 'Block6',
                    'Block7', 'Block8', 'Block9', 'Block10', 'Block11',
                    'Block12', 'Block13', 'Block14', 'Block14', 'Block16'])

        slope, intercept, r_value, p_value, std_err = linregress(time, counts_norm)

##        plt.title('Atik T=3 Dark - Normalized counts')
##        plt.savefig('Atik_T=3_Dark.pdf')
           
    return(intercept, plt.show())