示例#1
0
文件: GUI.py 项目: nblago/kpy
    def draw_spectra(self):
        ''' Draw nearest spectra '''
        if self.picked is None: return

        print "Drawing spectra"
        #xl = self.ax_spec.get_xlim()
        #yl = self.ax_spec.get_ylim()
        self.ax_spec.cla()
        #self.ax_spec.set_xlim(xl)
        #self.ax_spec.set_ylim(yl)

        
        x,y = self.X2[self.picked], self.Y1[self.picked]
        objs = self.KT.query_ball_point((x,y), 70)

        print "Query around %s found: %s" % ((x,y), objs)

        spec = self.cube[self.picked]
        ix = np.arange(*spec.xrange)
        fiducial_ll = chebval(ix, spec.lamcoeff)
        #self.ax_spec.plot(ix-spec.xrange[0], fiducial_ll, linewidth=3)
        self.ax_spec.step(fiducial_ll, spec.spec, linewidth=3)

        for spec in self.good_cube[objs]:
            try: 
                ix = np.arange(*spec.xrange)
                ll = chebval(ix, spec.lamcoeff)
                #self.ax_spec.plot(ix-spec.xrange[0], ll-fiducial_ll)
                self.ax_spec.step(ll, spec.spec)
            except: pass


        #self.ax_spec.set_ylim(-30,30)
        self.ax_spec.set_xlim(370,700)
        self.fig.show()
示例#2
0
文件: GUI.py 项目: nblago/kpy
    def __init__(self, cube=None, figure=None, pointsize=65, bgd_sub=False, radius_as=3):
        ''' Create spectum picking gui.

        Args:
            cube: Data cube list
            figure: Figure to draw to [default is None or open a new figure
                window '''

        self.actions = {"m": self.mode_switch}

        self.cube = cube
        self.pointsize = pointsize

        for ix, s in enumerate(self.cube):
            if s.xrange is None: continue
            xs = np.arange(s.xrange[0], s.xrange[1], .1)

            if s.lamcoeff is not None:
                lls = chebval(xs, s.lamcoeff)
                ha1 = np.argmin(np.abs(lls - 656.3))/10.0 + s.xrange[0]

            if s.mdn_coeff is not None:
                lls = chebval(xs, s.mdn_coeff)
                ha2 = np.argmin(np.abs(lls - 656.3))/10.0 + s.xrange[0]

            self.X1.append(ha1)
            self.X2.append(ha2)
            self.Y1.append(s.yrange[0])


        self.X1, self.X2, self.Y1 = map(np.array, [self.X1, self.X2,
            self.Y1])

        OK = (np.abs(self.X1 - self.X2) < 2) & np.isfinite(self.X2) & \
            np.isfinite(self.Y1)

        self.good_cube = self.cube[OK]
        locs = np.array([self.X2[OK], self.Y1[OK]]).T
        self.KT = scipy.spatial.KDTree(locs)

        assert(len(locs) == len(self.good_cube))

        # Setup drawing
        gs = gridspec.GridSpec(1,2, width_ratios=[1,2.5])
        self.fig = pl.figure(1, figsize=(22,5.5))
        self.ax_cube = pl.subplot(gs[0])
        self.ax_spec = pl.subplot(gs[1])

        self.ax_cube.set_xlim(-100, 2200)
        self.ax_cube.set_ylim(-100, 2200)

        pl.ion()
        self.draw_cube()

        print "Registering events"
        self.fig.canvas.mpl_connect("button_press_event", self)
        self.fig.canvas.mpl_connect("key_press_event", self)

        pl.show()
示例#3
0
 def referenceFunc_i(point):
     """Reference inverse implementation; point must be in range [-1, 1]
     """
     c1 = np.array([0, 0, 0, 1.6], dtype=float)
     c2 = np.array([0, -3.6], dtype=float)
     x1 = point
     return (
         chebval(x1, c1),
         chebval(x1, c2),
     )
示例#4
0
    def get_lambda_nm(self):
        """Returns lambda spectrum in nm"""
        xs = np.arange(*self.xrange)

        if self.lamcoeff is not None:
            lam = chebval(xs, self.lamcoeff)
        else:
            lam = chebval(xs, self.mdn_coeff)

        return lam
示例#5
0
    def test_chebval(self) :
        def f(x) :
            return x*(x**2 - 1)

        #check empty input
        assert_equal(ch.chebval([], 1).size, 0)

        #check normal input)
        for i in range(5) :
            tgt = 1
            res = ch.chebval(1, [0]*i + [1])
            assert_almost_equal(res, tgt)
            tgt = (-1)**i
            res = ch.chebval(-1, [0]*i + [1])
            assert_almost_equal(res, tgt)
            zeros = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
            tgt = 0
            res = ch.chebval(zeros,  [0]*i + [1])
            assert_almost_equal(res, tgt)
        x = np.linspace(-1,1)
        tgt = f(x)
        res = ch.chebval(x, [0, -.25, 0, .25])
        assert_almost_equal(res, tgt)

        #check that shape is preserved
        for i in range(3) :
            dims = [2]*i
            x = np.zeros(dims)
            assert_equal(ch.chebval(x, [1]).shape, dims)
            assert_equal(ch.chebval(x, [1,0]).shape, dims)
            assert_equal(ch.chebval(x, [1,0,0]).shape, dims)
示例#6
0
 def test_chebvander(self) :
     # check for 1d x
     x = np.arange(3)
     v = ch.chebvander(x, 3)
     assert_(v.shape == (3,4))
     for i in range(4) :
         coef = [0]*i + [1]
         assert_almost_equal(v[...,i], ch.chebval(x, coef))
     # check for 2d x
     x = np.array([[1,2],[3,4],[5,6]])
     v = ch.chebvander(x, 3)
     assert_(v.shape == (3,2,4))
     for i in range(4) :
         coef = [0]*i + [1]
         assert_almost_equal(v[...,i], ch.chebval(x, coef))
示例#7
0
def single_pawley(detector, q, I, back, p_fw=None):
    """ Full pawley fitting for a specific q, I combination.

    Option to use different initial parameters for FWHM fit. This allows a
    lower order polynomial to be specified.

    Args:
        detector: pyxpb detector object
        q (ndarray): Reciprocal lattice
        I (ndarray): Intensity values
        az_idx (int): Azimuthal slice index
        p_fw (list, tuple): New initial estimate
    """
    nmat, nf = len(detector.materials), len(detector._fwhm)
    background = chebval(q, back)
    q_lim = [np.min(q), np.max(q)]
    p0 = extract_parameters(detector, q_lim, np.nanmax(I))

    if p_fw is not None:
        p_fw0_idx = range(nmat, nmat + nf)
        p0_new = [i for idx, i in enumerate(p0) if idx not in p_fw0_idx]

        for idx, i in enumerate(p_fw):
            p0_new.insert(nmat + idx, i)

        nf = len(p_fw)
    else:
        p0_new = p0
    pawley = pawley_hkl(detector, background, nf - 1)
    return curve_fit(pawley, q, I, p0=p0_new)
示例#8
0
    def spec_calibration(self, theta=None, obs=None, **kwargs):
        """Implements a Chebyshev polynomial calibration model. If
        ``"pivot_wave"`` is not present in ``obs`` then 1.0 is returned.

        :returns cal:
           If ``params["cal_type"]`` is ``"poly"``, a polynomial given by
           'spec_norm' * (1 + \Sum_{m=1}^M 'poly_coeffs'[m-1] T_n(x)).
           Otherwise, the exponential of a Chebyshev polynomial.
        """
        if theta is not None:
            self.set_parameters(theta)

        if ('pivot_wave' in obs) and ('poly_coeffs' in self.params):
            # map wavelengths to the interval -1, 1
            x = obs['wavelength'] - obs['wavelength'].min()
            x = 2.0 * (x / x.max()) - 1.0
            # get coefficients.  Here we are setting the first term to 0 so we
            # can deal with it separately for the exponential and regular
            # multiplicative cases
            c = np.insert(self.params['poly_coeffs'], 0, 0)
            poly = chebval(x, c)
            #switch to have spec_norm be multiplicative or additive
            #depending on whether the calibration model is
            #multiplicative in exp^poly or just poly
            if self.params.get('cal_type', 'exp_poly') is 'poly':
                return (1.0 + poly) * self.params['spec_norm']
            else:
                return np.exp(self.params['spec_norm'] + poly)
        else:
            return 1.0
示例#9
0
def chebinterp(x,f,y):
    """ Interpolate a function f on a set of points x onto a set of points y
        x and y should must be in [-1,1]"""
    n = len(x)
    Tx = chebvander(x,n-1)
    a = solve(Tx,f)
    p = chebval(y,a)
    return p
示例#10
0
def Poly(pars, middle, low, high, x):
    """
    Generates a polynomial with the given parameters
    for all of the x-values.
    x is assumed to be a np.ndarray!
     Not meant to be called directly by the user!
    """
    xgrid = (x - middle) / (high - low)  # re-scale
    return chebyshev.chebval(xgrid, pars)
示例#11
0
def plot_result():
    n = [2**x for x in range(1,8)]
    for j in n:
        x = np.linspace(-1,1,num=100,endpoint=True)
        y = np.array([f(i) for i in x])
        chebpoly = chebval(x,cheb_interp(f,j))
        plt.plot(x,y)
        plt.plot(x,chebpoly)
    plt.show()
示例#12
0
 def initModel(self,):
     HtIndx=min(max(int(self.zjet_max-50)/20-1,0),20)
     scoef=spd_coefs[HtIndx]
     dcoef=dir_coefs[HtIndx]
     prms=np.array([0,self.Ri,self.UStar,1])
     if self.zjet_max==self.RefHt:
         prms[0]=self.URef
     else:
         utmp1=chebval(self.RefHt,np.dot(scoef[:,1:],prms[1:][:,None]))
         utmp2=chebval(self.RefHt,scoef[:,0])
         prms[0]=(self.URef-utmp1)/utmp2
     scoef=np.dot(scoef,prms[:,None]) # These are now vectors
     dcoef=np.dot(dcoef,prms[:,None]) # These are now vectors
     ang=chebval(self.grid.z,dcoef)
     ang-=ang[self.grid.ihub[0]] # The hub-height angle should be zero.
     ang[ang<-45.],ang[ang>45.]=-45.,45. # No angle should be more than 45 degrees.
     tmpdat=chebval(self.grid.z,scoef)*np.exp(1j*np.pi/180.*ang)[:,None]
     self._u[:1]=tmpdat.real,tmpdat.imag
示例#13
0
    def test_approximation(self):

        def powx(x, p):
            return x**p

        x = np.linspace(-1, 1, 10)
        for deg in range(0, 10):
            for p in range(0, deg + 1):
                c = cheb.chebinterpolate(powx, deg, (p,))
                assert_almost_equal(cheb.chebval(x, c), powx(x, p), decimal=12)
示例#14
0
def pawley_plot(q, I, detector, az_idx, ax):
    """ Plots q against measured intensity overlaid with Pawley fit.

    Includes highlighting of anticipated Bragg peak locations and
    difference between measured intensity and Pawley fit.

    Args:
        q (ndarray): Reciprocal lattice
        I (ndarray): Intensity
        detector: pyxpb detector instance
        az_idx (int): Azimuthal slice index
        ax: Axis to apply plot to
    """
    background = chebval(q, detector._back[az_idx])
    q_lim = [np.min(q), np.max(q)]
    p0 = extract_parameters(detector, q_lim, np.nanmax(I))
    pawley = pawley_hkl(detector, background)
    coeff, var_mat = curve_fit(pawley, q, I, p0=p0)
    I_pawley = pawley(q, *coeff)

    # Plot raw data and Pawley fit to data
    ax.plot(q, I, 'o', markeredgecolor='0.3', markersize=4,
            markerfacecolor='none', label=r'$\mathregular{I_{obs}}$')
    ax.plot(q, I_pawley, 'r-', linewidth=0.75,
            label=r'$\mathregular{I_{calc}}$')

    # Plot Bragg positions - locate relative to max intensity
    ymin = -ax.get_ylim()[1] / 10
    materials = detector.materials
    for idx, mat in enumerate(materials):
        offset = (1 + idx) * ymin / 2
        for q0 in detector.q0[mat]:
            bragg_line = [offset + ymin / 8, offset - ymin / 8]
            ax.plot([q0, q0], bragg_line, 'g-', linewidth=2)
        # Use error bars to fudge vertical lines in legend
        ax.errorbar(0, 0, yerr=1, fmt='none', capsize=0, ecolor='g',
                    elinewidth=1.5, label=r'Bragg ({})'.format(mat))

    # Plot difference between raw and Pawley fit (shifted below Bragg)
    I_diff = I - I_pawley
    max_diff = np.max(I_diff)
    shifted_error = I - I_pawley + (idx + 2) * ymin / 2 - max_diff
    ax.plot(q, shifted_error, 'b-', linewidth=0.75,
            label=r'$\mathregular{I_{diff}}$')

    # Remove ticks below 0 intensity
    ylocs = ax.yaxis.get_majorticklocs()
    yticks = ax.yaxis.get_major_ticks()
    for idx, yloc in enumerate(ylocs):
        if yloc < 0:
            yticks[idx].set_visible(False)

    legend = ax.legend(numpoints=1)
    frame = legend.get_frame()
    frame.set_facecolor('w')
示例#15
0
    def define_background(self, seg=50, k=12, pnt=None, fwhm=None, plot=True,
                          az_idx=0, auto=True, x=None, y=None):
        """ Background profile fitting - segment data and auto find points.

        Fits a Chebyshev polynomial (of order k) to automatically acquired
        q, I points. These points are selected wrt. the materials present
        (defined via add_materials), with the Bragg peaks being avoided.

        If auto is False, q v I points can be manually specified.

        Args:
            seg (int): Number of points to split data into/extract
            k (int): Order for Chebyshev polynomial
            pnt (tuple): Point co_ords or None for averaged data
            fwhm (float): Peak fwhm (to exclude more data around peaks)
            plot (bool): True/False
            az_idx (int): Azimuthal slice to plot
            auto (bool): Automatically extract q v I data
            x (ndarray): If not auto maunally defined q values
            y (ndarray): If not auto maunally defined I values
        """
        # Automatically averages over all points unless point is specified
        if pnt is None:
            I = np.mean(self.I, tuple(range(self.I.ndim - 2)))
        else:
            I = self.I[pnt]

        if auto:
            az = self.q.shape[0]
            x, y = np.zeros((az, seg)), np.zeros((az, seg))

            for a in range(az):
                split_q = np.array_split(self.q[a], seg)
                split_I = np.array_split(I[a], seg)

                for idx, q in enumerate(split_q):
                    q_min, q_max = np.min(q), np.max(q)
                    for mat in self.detector.materials:
                        q0 = self.detector.q0[mat]
                        fw = self.detector.fwhm[mat] if fwhm is None else fwhm
                        clash = np.any(np.logical_and(q0 > q_min - 2 * fw,
                                                      q0 < q_max + 2 * fw))
                        x[a, idx] = np.nan if clash else np.mean(split_q[idx])
                        y[a, idx] = np.nan if clash else np.mean(split_I[idx])

        self.detector.define_background(x, y, k)
        f = self.detector._back

        if plot:
            plt.plot(self.q[az_idx], I[az_idx], 'k')
            plt.plot(self.q[az_idx], chebval(self.q[az_idx], f[az_idx]), 'r-')
            plt.plot(x[az_idx], y[az_idx], 'r+')
            plt.ylim([0, np.nanmax(y) * 1.5])
            plt.xlabel(r'$\mathregular{q (A^{-1})}$')
            plt.ylabel(r'Intensity')
示例#16
0
    def test_chebfit(self) :
        def f(x) :
            return x*(x - 1)*(x - 2)

        # Test exceptions
        assert_raises(ValueError, cheb.chebfit, [1],    [1],     -1)
        assert_raises(TypeError,  cheb.chebfit, [[1]],  [1],      0)
        assert_raises(TypeError,  cheb.chebfit, [],     [1],      0)
        assert_raises(TypeError,  cheb.chebfit, [1],    [[[1]]],  0)
        assert_raises(TypeError,  cheb.chebfit, [1, 2], [1],      0)
        assert_raises(TypeError,  cheb.chebfit, [1],    [1, 2],   0)
        assert_raises(TypeError,  cheb.chebfit, [1],    [1],   0, w=[[1]])
        assert_raises(TypeError,  cheb.chebfit, [1],    [1],   0, w=[1,1])

        # Test fit
        x = np.linspace(0,2)
        y = f(x)
        #
        coef3 = cheb.chebfit(x, y, 3)
        assert_equal(len(coef3), 4)
        assert_almost_equal(cheb.chebval(x, coef3), y)
        #
        coef4 = cheb.chebfit(x, y, 4)
        assert_equal(len(coef4), 5)
        assert_almost_equal(cheb.chebval(x, coef4), y)
        #
        coef2d = cheb.chebfit(x, np.array([y,y]).T, 3)
        assert_almost_equal(coef2d, np.array([coef3,coef3]).T)
        # test weighting
        w = np.zeros_like(x)
        yw = y.copy()
        w[1::2] = 1
        y[0::2] = 0
        wcoef3 = cheb.chebfit(x, yw, 3, w=w)
        assert_almost_equal(wcoef3, coef3)
        #
        wcoef2d = cheb.chebfit(x, np.array([yw,yw]).T, 3, w=w)
        assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)
        # test scaling with complex values x points whose square
        # is zero when summed.
        x = [1, 1j, -1, -1j]
        assert_almost_equal(cheb.chebfit(x, x, 1), [0, 1])
示例#17
0
    def test_chebval(self) :
        #check empty input
        assert_equal(cheb.chebval([], [1]).size, 0)

        #check normal input)
        x = np.linspace(-1,1)
        y = [polyval(x, c) for c in Tlist]
        for i in range(10) :
            msg = "At i=%d" % i
            tgt = y[i]
            res = cheb.chebval(x, [0]*i + [1])
            assert_almost_equal(res, tgt, err_msg=msg)

        #check that shape is preserved
        for i in range(3) :
            dims = [2]*i
            x = np.zeros(dims)
            assert_equal(cheb.chebval(x, [1]).shape, dims)
            assert_equal(cheb.chebval(x, [1,0]).shape, dims)
            assert_equal(cheb.chebval(x, [1,0,0]).shape, dims)
示例#18
0
def spline_interpolation(pointx, pointy, wave, wave_temp, flux, flux_temp, axis,
                         leg_instance, con_instance, linewidth= 2.0, endpoints = 'y',
                         endpoint_order = 4):
    """Sort spline points and interpolate between marked continuum points"""
    from numpy.polynomial import chebyshev
    from scipy.interpolate import splrep,splev    
    
    #Insert endpoints
    if endpoints == 'y':
        sort_array = np.argsort(pointx)
        x = np.array(pointx)[sort_array]
        y = np.array(pointy)[sort_array]
        chebfit = chebyshev.chebfit(x, y, deg = endpoint_order)
        chebfitval = chebyshev.chebval(wave, chebfit)
        i =wave[150], wave[-150]
        window1, window2 = ((i[0]-70)<=wave) & (wave<=(i[0]+70)), ((i[1]-70)<=wave) & (wave<=(i[1]+70))
        y_val = np.median(chebfitval[window1]).astype(np.float64),np.median(chebfitval[window2]).astype(np.float64)
        pointx = np.concatenate([pointx,i])
        pointy = np.concatenate([pointy,y_val])
        ind_uni = f8(pointx)
        pointx = np.array(pointx)[ind_uni]
        pointy = np.array(pointy)[ind_uni]
        try:
            leg_instance.remove()
        except AttributeError:
            pass
        finally:
               leg_instance, = axis.plot(wave, chebfitval, color='black', lw = linewidth,
                                         label = 'legendre fit', zorder = 10)


    # print(pointx,pointy)
    #Sort numerically
    sort_array = np.argsort(pointx)
    x = np.array(pointx)[sort_array]
    y = np.array(pointy)[sort_array]


    from gen_methods import smooth
    #Interpolate
    spline = splrep(x,y, k=3)
    continuum = splev(wave,spline)
    continuum = smooth(continuum, window_len=1, window='hanning')
    
    #Plot
    try:
        con_instance.remove()
    except AttributeError:
        pass
    finally:
           con_instance, = axis.plot(wave,continuum, color='red', lw = linewidth, 
                                     label = 'continuum', zorder = 10)  
                                     
    return continuum, leg_instance, con_instance
示例#19
0
    def get_dlambda(self):
        """Returns dlambda spectrum """

        # Note the +1 below to handle the eating of an element
        # by diff
        xs = np.arange(self.xrange[0], self.xrange[1] + 1)

        lam = chebval(xs, self.lamcoeff)
        dlam = np.abs(np.diff(lam))

        return dlam
示例#20
0
文件: Extracter.py 项目: nblago/kpy
    def Strength(x):
        if x.xrange is None: return None
        if x.lamcoeff is None: return None
        if x.specw is None: return None
        ix = np.arange(*x.xrange)
        ll = chebval(ix, x.lamcoeff)
        OK = (ll > 450) & (ll < 700)

        if OK.any():
            return np.sum(x.specw[OK])
        else:
            return None
示例#21
0
 def test_chebfit(self) :
     def f(x) :
         return x*(x - 1)*(x - 2)
     # Test exceptions
     assert_raises(ValueError, ch.chebfit, [1],    [1],     -1)
     assert_raises(TypeError,  ch.chebfit, [[1]],  [1],      0)
     assert_raises(TypeError,  ch.chebfit, [],     [1],      0)
     assert_raises(TypeError,  ch.chebfit, [1],    [[[1]]],  0)
     assert_raises(TypeError,  ch.chebfit, [1, 2], [1],      0)
     assert_raises(TypeError,  ch.chebfit, [1],    [1, 2],   0)
     # Test fit
     x = np.linspace(0,2)
     y = f(x)
     coef = ch.chebfit(x, y, 3)
     assert_equal(len(coef), 4)
     assert_almost_equal(ch.chebval(x, coef), y)
     coef = ch.chebfit(x, y, 4)
     assert_equal(len(coef), 5)
     assert_almost_equal(ch.chebval(x, coef), y)
     coef2d = ch.chebfit(x, np.array([y,y]).T, 4)
     assert_almost_equal(coef2d, np.array([coef,coef]).T)
示例#22
0
    def interpolate(self, z, f):
        from numpy.polynomial.chebyshev import chebval
        import numpy as np

        msg = "Can't interpolate outside grid domain"
        assert np.array([z]).min() >= self.zmin, msg
        assert np.array([z]).max() <= self.zmax, msg

        c = self.to_coefficients(f)

        # Convert z-values to standard xg = [-1, 1]
        x = (z - self.zmin) / self.L * 2. - 1.
        return chebval(x, c)
示例#23
0
 def fit_func(dummy_variable, *labels):
     nn_spec = self.network.get_spectrum_scaled(
         scaled_labels=labels[:nnl])
     nn_spec = doppler_shift(self.network.wave, nn_spec, labels[nnl])
     if hasattr(self, 'lsf'):
         nn_resampl = self.lsf.apply(nn_spec)
     else:
         nn_resampl = np.interp(wavelength, self.network.wave, nn_spec)
     Cheb_coefs = labels[nnl + 1:nnl + 1 + self.Cheb_order]
     Cheb_x = np.linspace(-1, 1, len(nn_resampl))
     Cheb_poly = chebval(Cheb_x, Cheb_coefs)
     spec_with_resp = nn_resampl * Cheb_poly
     return spec_with_resp
示例#24
0
def eval(nr, a, b, r):

    # evaluates the projection matrix matrix for the chebyshev basis
    xx = (np.array(r)-b)/a

    coeffs = np.eye(nr)*2

    #coeffs = np.eye(nr)
    coeffs[0,0]=1.

    # return the coo type matrix, because it is needed for integration (evaluation of antiderivative) purposes
    #return spsp.lil_matrix(cheb.chebval(xx, coeffs).transpose())
    return np.mat(cheb.chebval(xx, coeffs).transpose())
示例#25
0
    def __call__(self, point):
        '''
        Evaluate the polynomial at 'point'.

        Parameters
        ----------
        point : array-like
                point at which to evaluate the polynomial

        Returns
        -------
        c : complex
            value of the polynomial at the given point
        '''
        super(MultiCheb, self).__call__(point)

        c = self.coeff
        n = len(c.shape)
        c = cheb.chebval(point[0], c)
        for i in range(1, n):
            c = cheb.chebval(point[i], c, tensor=False)
        return c
示例#26
0
def bit_to_float(matrica_bit):
    matrica_bit, fitness = np.split(matrica_bit, [-1], axis=1)
    days_bit, brod_bit, koeficijenti_bit, snaga = np.split(matrica_bit,
                                                           64*np.array((1, 3, 4+podaci.chebdeg)),
                                                           axis=1)
    days = np.packbits(days_bit.astype(bool), axis=1).view(np.float64)
    # date = np.empty(broj_jedinki)
    # for i in range(broj_jedinki):
    #    date[i] = datetime.timedelta(days=days[i]) + podaci.beg_of_time
    brod = np.packbits(brod_bit.astype(bool), axis=1).view(np.float64)
    koeficijenti = np.packbits(koeficijenti_bit.astype(bool), axis=1).view(np.float64)
    uglovi = chebval(x_osa(broj_segmenata), koeficijenti)
    return days, brod, uglovi, snaga, fitness
示例#27
0
def test_cheb_poly_single():
    deg = 21
    npts = 100
    pts = np.linspace(0, 1, npts, dtype=np.float32)
    out = np.zeros((deg + 1) * npts, dtype=np.float32)
    cheb_utils.cheb_poly_float(deg, pts, npts, out)

    out = out.reshape([deg + 1, npts])
    cheb_coefs = np.zeros(deg + 1)
    for m in range(deg + 1):
        cheb_coefs.fill(0)
        cheb_coefs[m] = 1
        cheb_vals_np = chebval(pts, cheb_coefs)
        assert np.allclose(out[m], cheb_vals_np, rtol=1e-6, atol=1e-6)
示例#28
0
def find_func(x, yreal):
    import numpy as np
    from numpy.polynomial.chebyshev import chebfit, chebval

    f = np.polyfit(x, yreal, len(yreal))
    c = chebfit(x, yreal, len(yreal))

    ytest_poly = [round(np.polyval(f, xi), 3) for xi in x]
    ytest_cheb = [round(chebval(xi, c), 3) for xi in x]

    ytest = ytest_poly
    function_coeff = f

    return ytest, function_coeff
示例#29
0
def bit_to_float(matrica_bit):
    warnings.filterwarnings("error", category=RuntimeWarning)
    days_bit, fuel_bit, koeficijenti_bit, snaga = np.split(matrica_bit,
                                                           np.array((16, 24, (1+podaci.chebdeg)*64 + 24)),
                                                           axis=1)
    days = np.packbits(days_bit.astype(bool), axis=1).view(np.uint16)
    brod = np.packbits(fuel_bit.astype(bool), axis=1).view(np.uint8)
    koeficijenti = np.packbits(koeficijenti_bit.astype(bool), axis=1).view(np.float64)
    try:
        uglovi = chebval(x_osa(broj_segmenata), koeficijenti.T)
        uglovi = np.mod(uglovi, 2*pi)
    except RuntimeWarning:
        uglovi = np.random.random_sample(size=(broj_jedinki, broj_segmenata)) * 2*pi
    print(days.shape, brod.shape, uglovi.shape, snaga.shape)
    return days[:, 0], brod[:, 0], uglovi, snaga
示例#30
0
def cheb_fitcurve( x,y,order ):
    x = cheb.chebpts2(len(x))
    order = 64
    coef = legend.legfit(x, y, order);    assert_equal(len(coef), order+1)
    y1 = legend.legval(x, coef)
    err_1 = np.linalg.norm(y1-y) / np.linalg.norm(y)

    coef = cheb.chebfit(x, y, order);    assert_equal(len(coef), order + 1)
    thrsh = abs(coef[0]/1000)
    for i in range(len(coef)):
        if abs(coef[i])<thrsh:
            coef = coef[0:i+1]
            break

    y2 = cheb.chebval(x, coef)
    err_2 = np.linalg.norm(y2 - y) / np.linalg.norm(y)

    plt.plot(x, y2, '.')
    plt.plot(x, y, '-')
    plt.title("nPt={} order={} err_cheby={:.6g} err_legend={:.6g}".format(len(x),order,err_2,err_1))
    plt.show()
    assert_almost_equal(cheb.chebval(x, coef), y)
    #
    return coef
示例#31
0
文件: Extracter.py 项目: nblago/kpy
def measure_flexure(sky):
    ''' Measure expected (589.3 nm) - measured emission line in nm'''
    ll, ss = sky['nm'], sky['ph_10m_nm']

    pl.figure()
    pl.step(ll, ss)

    pix = SG.argrelmax(ss, order=20)[0]
    skynms = chebval(pix, sky['coefficients'])
    for s in skynms: pl.axvline(s)

    ixmin = np.argmin(np.abs(skynms - 589.3))
    dnm = 589.3 - skynms[ixmin]

    return dnm
示例#32
0
    def _get_temperature(self, R):
        """Return temperature from input resistance or voltage based on
        calibration"""
        # Find the right resistance range
        for n in range(len(self.fitdata)):
            Z = log10(R)
            ZL = self.fitdata[n]["Zlower for fit range"]
            ZU = self.fitdata[n]["Zupper for fit range"]
            if Z > ZL and Z < ZU:
                # Apply the formula
                k = ((Z - ZL) - (ZU - Z)) / (ZU - ZL)
                T = chebval(k, self.fitdata[n]["coef"])
                return T

        print("Temperature out of calibration range.")
示例#33
0
def continuum_fit(wl, flux, fluxerr, edge_mask_len=50):
    """
    Small function to estimate continuum. Takes spectrum and uses only the edges, as specified by edge_mask_len, fits
    a polynomial and returns the fit.
    :param wl: Wavelenght array in which to estimate continuum
    :param flux: Corresponding flux array
    :param fluxerr: Corresponding array containing flux errors
    :param edge_mask_len: Size of edges to use to interpolate continuum
    :return: Interpolated continuum
    """
    from numpy.polynomial import chebyshev
    wl_chebfit = np.hstack((wl[:edge_mask_len],wl[-edge_mask_len:]))
    mask_cont = np.array([i for i,n in enumerate(wl) if n in wl_chebfit])
    chebfit = chebyshev.chebfit(wl_chebfit, flux[mask_cont], deg = 1, w=1/fluxerr[mask_cont]**2)
    chebfitval = chebyshev.chebval(wl, chebfit)
    return chebfitval, chebfit
示例#34
0
def proj_radial(nr, a, b, x = None):

    # evaluate the radial basis functions of degree <nr
    # to the projection of the poloidal field in r direction (Q-part of a qst decomposition
    if x is None:
        xx,ww = cheb.chebgauss(nr)
    else:
        xx = np.array(x)

    # use chebyshev polynomials normalized for FCT
    coeffs = np.eye(nr)*2
    #coeffs = np.eye(nr)
    coeffs[0,0]=1.

    #return spsp.lil_matrix((cheb.chebval(xx,coeffs)).transpose())
    return np.mat((cheb.chebval(xx,coeffs)).transpose())
示例#35
0
def measure_flexure(sky):
    ''' Measure expected (589.3 nm) - measured emission line in nm'''
    ll, ss = sky['nm'], sky['ph_10m_nm']

    pl.figure()
    pl.step(ll, ss)

    pix = SG.argrelmax(ss, order=20)[0]
    skynms = chebval(pix, sky['coefficients'])
    for s in skynms:
        pl.axvline(s)

    ixmin = np.argmin(np.abs(skynms - 589.3))
    dnm = 589.3 - skynms[ixmin]

    return dnm
def chebfit2(x, y, deg, alpha=1e-4):
    """
    This is a reimplementaion of a subset of chebyshev.chebfit
    https://numpy.org/doc/stable/reference/generated/numpy.polynomial.chebyshev.chebfit.html
    using Tichnov regularization (Ridge regression)
    """
    cheb_values = []
    for pos in range(deg + 1):
        coefs = np.zeros(deg + 1)
        coefs[pos] = 1
        vals = cheb.chebval(x, coefs)
        cheb_values.append(vals)
    cheb_values = np.array(cheb_values)
    regr = Ridge(alpha=alpha, fit_intercept=False)
    _ = regr.fit(cheb_values.T, y)
    return regr.coef_
def ifct(f):
    # =============================================================================
    #     INPUT: Chebyshev coefficients
    #     OUTPUT: Inverse discrete cosine tranform of f (f is in spectral space). This function takes f from spectral space to physical space
    #==============================================================================
    # ##Calculate chebyshev coefficients with DCT
    N = 1001
    n = np.arange(0, N, 1)
    theta = np.pi * (2 * n + 1) / ((2 * (N)))
    x = np.cos(theta)
    #Correct to coincide with Moore 2017

    #f[0] = f[0]*2
    #F = idct(f)*len(f)
    F = chebval(x, f)
    return F
def chebstate_to_forwards(state, do_normalize=False):
    """
    we return fwd values that correspond to scaled state (= coefs starting from the 2nd one, first two assume to be  zero)
    Let n=len(state). We set coefs = [0,0,state/norm(state)] and fv = chebval(range(n+2), coefs)

    If we want len(fv) = nf then we need len(state) = nf-2

    details
    https://numpy.org/doc/stable/reference/generated/numpy.polynomial.chebyshev.chebfit.html
    """
    nf = len(state) + 2
    xs = cheb_fwd_xs(nf)
    state_norm = np.linalg.norm(state)
    if do_normalize:
        state = state / state_norm
    return cheb.chebval(xs, c=[0, 0, *state]), state_norm
def th_plot(data, tt):
    fields = 'concurence', 'iops_mediana', 'lat_mediana'
    conc_4k = filter_data('concurrence_test_' + tt, fields, blocksize='4k')
    filtered_data = sorted(list(conc_4k(data)))

    x, iops, lat = zip(*filtered_data)

    _, ax1 = plt.subplots()

    xnew = np.linspace(min(x), max(x), 50)
    # plt.plot(xnew, power_smooth, 'b-', label='iops')
    ax1.plot(x, iops, 'b*')

    for degree in (3, ):
        c = chebfit(x, iops, degree)
        vals = chebval(xnew, c)
        ax1.plot(xnew, vals, 'g--')
示例#40
0
    def interpolate(self, z, f):
        """See equations 17.37 and 17.38 in Boyd"""
        from numpy.polynomial.chebyshev import chebval
        import numpy as np

        msg = "Can't interpolate outside grid domain"
        assert np.array([z]).min() >= self.zmin, msg
        assert np.array([z]).max() <= self.zmax, msg

        # Get coefficients for standard Chebyshev polynomials
        c = self.to_coefficients(f)

        # Convert infinite grid to xg = [-1, 1]
        x = z / np.sqrt(self.C ** 2 + z ** 2)

        # Evaluate the Chebyshev polynomial
        return chebval(x, c)
def th_plot(data, tt):
    fields = 'concurence', 'iops_mediana', 'lat_mediana'
    conc_4k = filter_data('concurrence_test_' + tt, fields, blocksize='4k')
    filtered_data = sorted(list(conc_4k(data)))

    x, iops, lat = zip(*filtered_data)

    _, ax1 = plt.subplots()

    xnew = np.linspace(min(x), max(x), 50)
    # plt.plot(xnew, power_smooth, 'b-', label='iops')
    ax1.plot(x, iops, 'b*')

    for degree in (3,):
        c = chebfit(x, iops, degree)
        vals = chebval(xnew, c)
        ax1.plot(xnew, vals, 'g--')
示例#42
0
文件: transient.py 项目: qiqi/dynakit
def stationarityTest(x):
    windowSize = 10
    xStats = movingWindowStats(x, windowSize, arange(1, 5))
    nSamples = min(128, x.shape[0] // windowSize)
    t = linspace(-1, 1, nSamples)
    i = array(linspace(0, xStats.shape[0] - 1, nSamples), int)
    ct = chebval(t, eye(2)).T
    minPvalues = 1
    for j in range(xStats.shape[1]):
        for k in range(xStats.shape[2]):
            est = sm.OLS(xStats[i, j, k], ct)
            est2 = est.fit()
            print(j, k, est2.pvalues[1:])
            minPvalues = min(minPvalues, est2.pvalues[1:].min())
            # cla()
            # plot(t, xStats[i,j,k], '.-')
            # stop
    return minPvalues
示例#43
0
def proj_radial_r2(nr, a, b, x = None):
    # evaluate the radial basis functions of degree <nr over r
    # this projection operator includes the 1/r**2 part and corresponds
    # to the a projection from spectral to physical for the toroidal field (T-part of a qst decomposition)
    if x is None:
        xx,ww = cheb.chebgauss(nr)
    else:
        xx = np.array(x)

    # use chebyshev polynomials normalized for FCT
    coeffs = np.eye(nr)*2

    #coeffs = np.eye(nr)
    coeffs[0,0]=1.

    temp = (cheb.chebval(xx,coeffs)/(a*xx+b)**2).transpose()
    #return spsp.coo_matrix(temp)
    return np.mat(temp)
def object_func(*params):
    params = params[0]
    J = 0
    fit = chebval(Q, params[:4])
    E = params[4]
    fit = fit + E / Q
    for i in range(len(intensity)):
        if Q[i] < 1:
            if intensity[i] < fit[i]:
                J = J + (intensity[i] - fit[i])**4
            elif intensity[i] >= fit[i]:
                J = J + (intensity[i] - fit[i])**4
        else:
            if intensity[i] < fit[i]:
                J = J + (intensity[i] - fit[i])**4
            elif intensity[i] >= fit[i]:
                J = J + (intensity[i] - fit[i])**2
    return J
示例#45
0
    def evaluate_at(self, point):
        super(MultiCheb, self).evaluate_at(point)

        poly_value = complex(0)
        for mon in self.monomialList():
            mon_value = 1
            for i in range(len(point)):
                cheb_deg = mon[i]
                cheb_coeff = [0. for i in range(cheb_deg)]
                cheb_coeff.append(1.)
                cheb_val = cheb.chebval([point[i]], cheb_coeff)[0]
                mon_value *= cheb_val
            mon_value *= self.coeff[mon]
            poly_value += mon_value

        if abs(poly_value) < 1.e-10:
            return 0
        else:
            return poly_value
示例#46
0
def GaussHermiteIntegrate(degree, A, c, k):
    """This returns the integral of the expected Value function when
 s is a log-normal distribution."""
    # Note that f(x) = V( (s**rho)*exp(sigma*x*sqrt(2))*k**alpha - c, (s**rho) *
    # exp( sigma*x*sqrt(2))) * 1/sqrt(pi)
    integral = 0
    for i in range(degree):
        # Traditonally we used: V( k^alpha - c)
        # Now it has taken the form of: V( s*k^alpha - c, s)
        sNew = np.exp(mu + np.sqrt(2) * sigma * x[i])
        #xPos = np.clip(sNew * k**alpha - c, a, b)
        xPos = sNew * k**alpha - c
        if (xPos < a):
            print(xPos)
        if (xPos > b):
            print(xPos)
        chebValue = cheb.chebval(xPos, A)
        integral += y[i] * chebValue
    return integral * (1. / np.sqrt(math.pi))
示例#47
0
def cheb_eval(nr, a, b, r):
    # INPUT:
    # nr: int, number of radial functions
    # a: double, stretch factor for the mapping to shells
    # b: double, shift factor for the mapping to shells
    # r: np.array or list, list of collocation points

    # evaluates the projection matrix for the chebyshev basis
    xx = (np.array(r) - b) / a

    coeffs = np.eye(nr) * 2
    coeffs[0, 0] = 1.  # set 1 on the first diag entry because of DCT
    # proptierties

    # return the dense matrix, because it is needed for integration
    # (evaluation of antiderivative) purposes

    mat = np.mat(cheb.chebval(xx, coeffs).transpose())
    return mat
示例#48
0
文件: Spectra.py 项目: scizen9/kpy
def find_ha(cc):
    """Find out where in X position Halpha falls

    Args:
        cc (float vector): coefficients of fit giving
            wavelength versus spatial position

    Returns:
        float: spatial position corresponding to Halpha

    Note:
        assumes spatial position falls between 30 and 100
        arcsec and has an accuracy limit of 0.1 arcsec

    """
    ix = np.arange(30, 100, .1)

    haix = np.argmin(np.abs(chebval(ix, cc) - 656.3))

    return ix[haix]
示例#49
0
    def spec_calibration(self, theta=None, obs=None, **kwargs):
        """Implements a Chebyshev polynomial calibration model.  This only
        occurs if ``"poly_coeffs"`` is present in the :py:attr:`params`
        dictionary, otherwise the value of ``params["spec_norm"]`` is returned.

        :param theta: (optional)
            If given, set :py:attr:`params` using this vector before
            calculating the calibration polynomial. ndarray of shape
            ``(ndim,)``

        :param obs:
            A dictionary of observational data, must contain the key
            ``"wavelength"``

        :returns cal:
           If ``params["cal_type"]`` is ``"poly"``, a polynomial given by
           ``'spec_norm'`` :math:`\times (1 + \Sum_{m=1}^M```'poly_coeffs'[m-1]``:math:` \times T_n(x))`.
           Otherwise, the exponential of a Chebyshev polynomial.
        """
        if theta is not None:
            self.set_parameters(theta)

        if ('poly_coeffs' in self.params):
            mask = obs.get('mask', slice(None))
            # map unmasked wavelengths to the interval -1, 1
            # masked wavelengths may have x>1, x<-1
            x = self.wave_to_x(obs["wavelength"], mask)
            # get coefficients.  Here we are setting the first term to 0 so we
            # can deal with it separately for the exponential and regular
            # multiplicative cases
            c = np.insert(self.params['poly_coeffs'], 0, 0)
            poly = chebval(x, c)
            # switch to have spec_norm be multiplicative or additive depending
            # on whether the calibration model is multiplicative in exp^poly or
            # just poly
            if self.params.get('cal_type', 'exp_poly') is 'poly':
                return (1.0 + poly) * self.params.get('spec_norm', 1.0)
            else:
                return np.exp(self.params.get('spec_norm', 0) + poly)
        else:
            return 1.0 * self.params.get('spec_norm', 1.0)
示例#50
0
文件: IO.py 项目: nickkonidaris/kpy
def convert_spectra_to_img(spectra, CRVAL1, CRPIX1):
    
    lfid = exp_fid_wave(CRVAL1=CRVAL1, CRPIX1=CRPIX1)
    img = np.zeros((len(spectra), len(lfid)))
    img2 = np.zeros((len(spectra), len(lfid)))
    img[:] = np.nan
    img2[:] = np.nan

    for ix, spectrum in enumerate(spectra):
        spec = spectrum.specw
        if spec is None: continue

        img[ix, 0:len(spec)] = spec

        try: lam = chebval(np.arange(*spectrum.xrange), spectrum.lamcoeff)
        except: continue
        IF = interp1d(lam, spec, bounds_error=False, fill_value=np.nan)

        img2[ix, :] = IF(lfid)

    return img, img2
示例#51
0
def power_monitor_calibration(wavelength, voltage):
    ps = [ -1.954872514465467656097352744382078527e-02,
        -9.858164527032511337267806084128096700e-01,
        -9.030551056544730559316747076081810519e-02,
        -1.176690008380113228181329532162635587e-01,
        -7.280035069541325454256508464823127724e-02,
        -3.945345662306017076037534252463956363e-02,
        -3.615273166450208575106728403625311330e-02,
         9.788152479007751483042198969997116365e-03,
        -1.456367977705685066991403431302387617e-02,
        -1.786831712872795699387218348874739604e-02,
         2.597734938056468420586320178244932322e-02,
        -2.107763828923307075635662499735190067e-03,
         7.008138732863849161558444933461942128e-03,
         2.152771594326421261689219477375445422e-02,
        -1.331119139869020931432608279010310071e-02,
        -1.377397203478183430880310567090418772e-02,
         5.892610556615005337754986669551726663e-03,
         4.013333930879096900223856891898321919e-03,
        -1.388605295508058999273681699548887991e-03,
        -6.461661481385170711574938984256277763e-04,
         1.965802838584800071083241723712831117e-04,
         5.949925144541367866713593715033425724e-05,
        -1.676361665718153744295687568310881943e-05,
        -2.944515435510320108369441277629263709e-06,
         7.941387605415402886735497592352039931e-07,
         6.088879431605282524011537540614691366e-08,
        -1.605397480549405216611181363393912047e-08]
    #The values below are determined from the dataset associated with the coefficients
    #The fitting is scaled for numerical stability
    w_std = 20.579900281127852
    w_mean = 763.93403214285706
    r_std = 2.9169654028890814e-05
    r_mean = 0.00048691497166276345
    scale_wavelength = (float(wavelength) - w_mean)/w_std
    conversion = chebval(scale_wavelength, ps)
    unscale_r = conversion*r_std + r_mean
    return str(unscale_r*float(voltage))
示例#52
0
    def test_chebint(self) :
        # check exceptions
        assert_raises(ValueError, cheb.chebint, [0], .5)
        assert_raises(ValueError, cheb.chebint, [0], -1)
        assert_raises(ValueError, cheb.chebint, [0], 1, [0,0])

        # test integration of zero polynomial
        for i in range(2, 5):
            k = [0]*(i - 2) + [1]
            res = cheb.chebint([0], m=i, k=k)
            assert_almost_equal(res, [0, 1])

        # check single integration with integration constant
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            tgt = [i] + [0]*i + [1/scl]
            chebpol = cheb.poly2cheb(pol)
            chebint = cheb.chebint(chebpol, m=1, k=[i])
            res = cheb.cheb2poly(chebint)
            assert_almost_equal(trim(res), trim(tgt))

        # check single integration with integration constant and lbnd
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            chebpol = cheb.poly2cheb(pol)
            chebint = cheb.chebint(chebpol, m=1, k=[i], lbnd=-1)
            assert_almost_equal(cheb.chebval(-1, chebint), i)

        # check single integration with integration constant and scaling
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            tgt = [i] + [0]*i + [2/scl]
            chebpol = cheb.poly2cheb(pol)
            chebint = cheb.chebint(chebpol, m=1, k=[i], scl=2)
            res = cheb.cheb2poly(chebint)
            assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with default k
        for i in range(5) :
            for j in range(2,5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = cheb.chebint(tgt, m=1)
                res = cheb.chebint(pol, m=j)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with defined k
        for i in range(5) :
            for j in range(2,5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = cheb.chebint(tgt, m=1, k=[k])
                res = cheb.chebint(pol, m=j, k=list(range(j)))
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with lbnd
        for i in range(5) :
            for j in range(2,5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1)
                res = cheb.chebint(pol, m=j, k=list(range(j)), lbnd=-1)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with scaling
        for i in range(5) :
            for j in range(2,5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = cheb.chebint(tgt, m=1, k=[k], scl=2)
                res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2)
                assert_almost_equal(trim(res), trim(tgt))
示例#53
0
# to the outside and then back to centre and the other side. Hence the need for
# sorting the same.

sortind = np.argsort( rotcurve["Row"] )

fig1 = plt.figure(1)
xs = np.linspace( np.min(rotcurve["Row"]), np.max(rotcurve["Row"]), 1000)
residuals = []
change=0.0
for i in range(15):
	interp_coeff = chebfit( rotcurve["Row"], rotcurve["lambda"], w=1/rotcurve["lambda_err"], deg=i)
	interp_coeff_z = chebfit( rotcurve["Row"], rotcurve["z"], w=1/rotcurve["z_err"], deg=i)


	# Below, we are just generating a sample curve for the user to visualize the fit.
	ys = chebval(xs, interp_coeff)
	
	# Residual calculation in redshift space.
	predicted = chebval( rotcurve["Row"], interp_coeff_z )
	rms = np.sqrt( np.sum( (predicted - rotcurve["z"])**2 ) / len(predicted) )
	print rms
	residuals.append(rms)
	# Now, present the plot to the user.
	plt.errorbar( rotcurve["Row"], rotcurve["lambda"], rotcurve["lambda_err"], fmt="o" )
	plt.plot(xs, ys)
	plt.xlabel("Row Number", fontsize=18)
	plt.ylabel("Wavelength Center", fontsize=18)
	if i!=0:
		change = (residuals[i] - residuals[i-1])/residuals[i-1]
		metric = residuals[i]/residuals[i-1] - 1
		print('Metric of Change = ',metric)