示例#1
0
文件: models.py 项目: rrgupta/sncosmo
    def __init__(self, r_v=3.1):

        self._param_names = ['ebv']
        self.param_names_latex = ['E(B-V)']
        self._parameters = np.array([0.])
        self._r_v = r_v

        kknots = f99kknots(self._XKNOTS, r_v)
        self._spline = splmake(self._XKNOTS, kknots, order=3)
def test_func():
    from scipy import interpolate
    import matplotlib.pyplot as plt
    import matplotlib
    matplotlib.interactive(False)

    coef = np.array([[1, 1], [0, 1]])  # linear from 0 to 2
    # coef = np.array([[1,1],[1,1],[0,2]]) # linear from 0 to 2
    breaks = [0, 1, 2]
    pp = PPform(coef, breaks, a=-100, b=100)
    x = linspace(-1, 3, 20)
    y = pp(x)  # @UnusedVariable

    x = linspace(0, 2 * pi + pi / 4, 20)
    y = sin(x) + np.random.randn(x.size)
    tck = interpolate.splrep(x, y, s=len(x))  # @UndefinedVariable
    xnew = linspace(0, 2 * pi, 100)
    ynew = interpolate.splev(xnew, tck, der=0)  # @UndefinedVariable
    tck0 = interpolate.splmake(  # @UndefinedVariable
        xnew,
        ynew,
        order=3,
        kind='smoothest',
        conds=None)
    pp = interpolate.ppform.fromspline(*tck0)  # @UndefinedVariable

    plt.plot(x, y, "x", xnew, ynew, xnew, sin(xnew), x, y, "b", x, pp(x), 'g')
    plt.legend(['Linear', 'Cubic Spline', 'True'])
    plt.title('Cubic-spline interpolation')
    plt.show()

    t = np.arange(0, 1.1, .1)
    x = np.sin(2 * np.pi * t)
    y = np.cos(2 * np.pi * t)
    _tck1, _u = interpolate.splprep([t, y], s=0)  # @UndefinedVariable
    tck2 = interpolate.splrep(t, y, s=len(t), task=0)  # @UndefinedVariable
    # interpolate.spl
    tck = interpolate.splmake(t, y, order=3, kind='smoothest', conds=None)
    self = interpolate.ppform.fromspline(*tck2)  # @UndefinedVariable
    plt.plot(t, self(t))
    plt.show('hold')
    pass
示例#3
0
    def __init__(self, a_v, r_v=3.1):
        if not HAS_SCIPY:
            raise ImportError("To use this function scipy needs to be installed")

        from scipy.interpolate import splmake

        self.a_v = a_v
        self.r_v = r_v

        kknots = cextinction.f99kknots(_f99_xknots, self.r_v)

        self._spline = splmake(_f99_xknots, kknots, order=3)
示例#4
0
    def __init__(self, a_v, r_v=3.1):
        if not HAS_SCIPY:
            raise ImportError('To use this function scipy needs to be installed')

        from scipy.interpolate import splmake

        self.a_v = a_v
        self.r_v = r_v

        kknots = cextinction.f99kknots(_f99_xknots, self.r_v)

        self._spline = splmake(_f99_xknots, kknots, order=3)
    def build_and_write_spline_parameters(self,filename):
        """
        Building the parameters for a spline is quite time consuming. It is best to 
        do this once, write to file, and be done with it.

        This routine assumes self.build_scattering_kernel has already created the 
        appropriate arrays.
        """

        # generate the splines

        # The function splmake has inteface:
        #
        #           (xk, Ck, order)  = splmake(xk, yk, order)   
        #
        # There is no point in storing "xk" and "order" as part of the output tuple. 
        # Spleval will need this information in the form of a tuple, which we can re-build
        #  at evalation time.

        print ' ====   Computing spline of SR ====='
        list_xi, re_SR_spline, order = splmake(self.list_xi, N.real(self.SR), order = self.spline_order)
        list_xi, im_SR_spline, order = splmake(self.list_xi, N.imag(self.SR), order = self.spline_order)

        print ' ====   Computing spline of SI ====='
        list_xi, re_SI_spline, order = splmake(self.list_xi, N.real(self.SI), order = self.spline_order)
        list_xi, im_SI_spline, order = splmake(self.list_xi, N.imag(self.SI), order = self.spline_order)

        # same length for all splines
        spline_length = len(im_SI_spline)


        re_TR_spline = N.zeros([2,len(self.list_hw_ext),spline_length ])
        im_TR_spline = N.zeros([2,len(self.list_hw_ext),spline_length ])
        re_TI_spline = N.zeros([2,len(self.list_hw_ext),spline_length ])
        im_TI_spline = N.zeros([2,len(self.list_hw_ext),spline_length ])

        for i_eta, eta in enumerate(self.list_eta):
            for i_hw_ext, hw_ext in enumerate( self.list_hw_ext):
                print ' ====   Computing spline for TR and TI, eta = %i,  hw_ext = %4.3f meV ====='%(eta,1000*hw_ext)

                list_xi, re_TR_spline[i_eta,i_hw_ext,:], order = splmake(self.list_xi, N.real(self.TR[i_eta,i_hw_ext,:]), order = self.spline_order)
                list_xi, im_TR_spline[i_eta,i_hw_ext,:], order = splmake(self.list_xi, N.imag(self.TR[i_eta,i_hw_ext,:]), order = self.spline_order)

                list_xi, re_TI_spline[i_eta,i_hw_ext,:], order = splmake(self.list_xi, N.real(self.TI[i_eta,i_hw_ext,:]), order = self.spline_order)
                list_xi, im_TI_spline[i_eta,i_hw_ext,:], order = splmake(self.list_xi, N.imag(self.TI[i_eta,i_hw_ext,:]), order = self.spline_order)

        write_splmake(  re_SR_spline, im_SR_spline, re_SI_spline, im_SI_spline,
                        re_TR_spline, im_TR_spline, re_TI_spline, im_TI_spline,
                        filename, self.spline_order, self.list_xi, self.list_hw_ext, 
                        self.mu, self.beta, self.kernel_Gamma_width, self.Green_Gamma_width)
示例#6
0
def test_func():
    from scipy import interpolate
    import matplotlib.pyplot as plt
    import matplotlib
    matplotlib.interactive(False)

    coef = np.array([[1, 1], [0, 1]])  # linear from 0 to 2
    # coef = np.array([[1,1],[1,1],[0,2]]) # linear from 0 to 2
    breaks = [0, 1, 2]
    pp = PPform(coef, breaks, a=-100, b=100)
    x = linspace(-1, 3, 20)
    y = pp(x)  # @UnusedVariable

    x = linspace(0, 2 * pi + pi / 4, 20)
    y = sin(x) + np.random.randn(x.size)
    tck = interpolate.splrep(x, y, s=len(x))  # @UndefinedVariable
    xnew = linspace(0, 2 * pi, 100)
    ynew = interpolate.splev(xnew, tck, der=0)  # @UndefinedVariable
    tck0 = interpolate.splmake(  # @UndefinedVariable
        xnew, ynew, order=3, kind='smoothest', conds=None)
    pp = interpolate.ppform.fromspline(*tck0)  # @UndefinedVariable

    plt.plot(x, y, "x", xnew, ynew, xnew, sin(xnew), x, y, "b", x, pp(x), 'g')
    plt.legend(['Linear', 'Cubic Spline', 'True'])
    plt.title('Cubic-spline interpolation')
    plt.show()

    t = np.arange(0, 1.1, .1)
    x = np.sin(2 * np.pi * t)
    y = np.cos(2 * np.pi * t)
    _tck1, _u = interpolate.splprep([t, y], s=0)  # @UndefinedVariable
    tck2 = interpolate.splrep(t, y, s=len(t), task=0)  # @UndefinedVariable
    # interpolate.spl
    tck = interpolate.splmake(t, y, order=3, kind='smoothest', conds=None)
    self = interpolate.ppform.fromspline(*tck2)  # @UndefinedVariable
    plt.plot(t, self(t))
    plt.show('hold')
    pass
def upsample(data, factor):
    """
    upsample array of data by a given factor using cubic splines
    array.shape is assumed to be (num_events, num_values_per_event)
    """

    # vpe is values per event
    num_m, num_vpe = data.shape
    # new values appear only between old values, hence the "-1"
    up_num_vpe = (num_vpe - 1) * factor + 1
    axis = arange(0, up_num_vpe, factor)
    up_axis = arange(up_num_vpe)
    #   up_data = zeros((num_m, up_num_vpe))
    #   for i in range(num_m):
    #       up_data[i] = spline(axis, data[i], up_axis)
    splines = splmake(axis, data.T)
    up_data = spleval(splines, up_axis)

    return up_data.T
示例#8
0
def upsample(data, factor):
    """
    upsample array of data by a given factor using cubic splines
    array.shape is assumed to be (num_events, num_values_per_event)
    """


    # vpe is values per event
    num_m, num_vpe = data.shape
    # new values appear only between old values, hence the "-1"
    up_num_vpe = (num_vpe - 1) * factor + 1
    axis = arange(0, up_num_vpe, factor) 
    up_axis = arange(up_num_vpe)
#   up_data = zeros((num_m, up_num_vpe))
#   for i in xrange(num_m):
#       up_data[i] = spline(axis, data[i], up_axis)
    splines = splmake(axis, data.T)
    up_data = spleval(splines, up_axis)

    return up_data.T
示例#9
0
    """

    f = ExtinctionF99(a_v, r_v)
    return f(wave)


# fm07 knots for spline
_fm07_r_v = 3.1
_fm07_xknots = np.array(
    [0.0, 0.25, 0.50, 0.75, 1.0, 1.0e4 / 5530.0, 1.0e4 / 4000.0, 1.0e4 / 3300.0, 1.0e4 / 2700.0, 1.0e4 / 2600.0]
)
_fm07_kknots = cextinction.fm07kknots(_fm07_xknots)
try:
    from scipy.interpolate import splmake

    _fm07_spline = splmake(_fm07_xknots, _fm07_kknots, order=3)
except ImportError:
    pass


def extinction_fm07(wave, a_v):
    """Fitzpatrick & Massa (2007) extinction model for R_V = 3.1.

    The Fitzpatrick & Massa (2007) [1]_ model, which has a slightly
    different functional form from that of Fitzpatrick (1999) [3]_
    (`extinction_f99`). Fitzpatrick & Massa (2007) claim it is
    preferable, although it is unclear if signficantly so (Gordon et
    al. 2009 [2]_). Defined from 910 A to 6 microns.

    .. note :: This model is not R_V dependent.
示例#10
0
    .. [1] Fitzpatrick, E. L. 1999, PASP, 111, 63
    .. [2] Fitzpatrick, E. L. & Massa, D. 1990, ApJS, 72, 163
    """

    f = ExtinctionF99(a_v, r_v)
    return f(wave)


# fm07 knots for spline
_fm07_r_v = 3.1
_fm07_xknots = np.array([0., 0.25, 0.50, 0.75, 1., 1.e4/5530., 1.e4/4000.,
                        1.e4/3300., 1.e4/2700., 1.e4/2600.])
_fm07_kknots = cextinction.fm07kknots(_fm07_xknots)
try:
    from scipy.interpolate import splmake
    _fm07_spline = splmake(_fm07_xknots, _fm07_kknots, order=3)
except ImportError:
    pass


def extinction_fm07(wave, a_v):
    """Fitzpatrick & Massa (2007) extinction model for R_V = 3.1.

    The Fitzpatrick & Massa (2007) [1]_ model, which has a slightly
    different functional form from that of Fitzpatrick (1999) [3]_
    (`extinction_f99`). Fitzpatrick & Massa (2007) claim it is
    preferable, although it is unclear if signficantly so (Gordon et
    al. 2009 [2]_). Defined from 910 A to 6 microns.

    .. note :: This model is not R_V dependent.