Exemplo n.º 1
0
def makeLSQspline(xl, yl, xr, yr):
    """docstring for makespline"""

    xmin = xr.min() - 1
    xmax = xr.max() + 1
    ymin = yr.min() - 1
    ymax = yr.max() + 1
    n = len(xl)

    print "xrange: ", xmin, xmax, '\t', "yrange: ", ymin, ymax
    s = 1.1
    yknots, xknots = np.mgrid[
        ymin + s:ymax - s:10j,
        xmin + s:xmax - s:10j]  # Makes an 11x11 regular grid of knot locations
    yknots = np.linspace(ymin + s, ymax - s, 10)
    xknots = np.linspace(xmin + s, xmax - s, 10)

    xspline = interpolate.LSQBivariateSpline(xr, yr, xl, xknots.flat,
                                             yknots.flat)
    yspline = interpolate.LSQBivariateSpline(xr, yr, yl, xknots.flat,
                                             yknots.flat)

    def mapping(xr, yr):
        xl = xspline.ev(xr, yr)
        yl = yspline.ev(xr, yr)
        return xl, yl

    return mapping, xspline, yspline
Exemplo n.º 2
0
def interp_beta():
    """
    We will interpolate based on reasonable values of the mean and std dev.

    For beta, -8 is pretty small on the log scale and 8 is quite large. Similarly,
    0.001 is a very small std dev and 4 is a pretty big spread (on the log scale).

    :return: a stand in fn for interpolating the conjugate map
    """
    # get the grid we will search
    ft_lb, ft_ub, qt_lb, qt_ub = -8, 8, 0.0001, 4
    ft, qt = _get_grid(ft_lb, ft_ub, qt_lb, qt_ub)

    # do a bunch of solves
    z = np.empty((len(ft), 2))
    for i in range(len(ft)):
        z[i, :] = np.log(
            bern_conjugate_params(ft[i],
                                  qt[i]**2,
                                  alpha=1,
                                  beta=1,
                                  interp=False))

    # ft and qt knots
    num_knots = 75
    ftv, qtv = np.sort(np.unique(ft)), np.sort(np.unique(qt))
    # make sure there are a few points outside the knots
    ft_knots = np.linspace(ftv[3], ftv[-3], num_knots)
    qt_knots = np.exp(np.linspace(np.log(qtv[3]), np.log(qtv[-3]), num_knots))
    bbox = [ft_lb - 2, ft_ub + 2, 0., qt_ub + 2]

    _interp_fn_log_alpha = interpolate.LSQBivariateSpline(ft,
                                                          qt,
                                                          z[:, 0],
                                                          ft_knots,
                                                          qt_knots,
                                                          bbox=bbox,
                                                          kx=1,
                                                          ky=1)
    _interp_fn_log_beta = interpolate.LSQBivariateSpline(ft,
                                                         qt,
                                                         z[:, 1],
                                                         ft_knots,
                                                         qt_knots,
                                                         bbox=bbox,
                                                         kx=1,
                                                         ky=1)
    # transform to original scale and variance instead of std dev
    fn = partial(transformer,
                 fn1=_interp_fn_log_alpha,
                 fn2=_interp_fn_log_beta)
    fn.ft_lb, fn.ft_ub, fn.qt_lb, fn.qt_ub = bbox
    return fn
Exemplo n.º 3
0
def test_2d_make_lsq(ndspline):
    N = 500
    sample_x = get_query_points(ndspline, n=N).squeeze()
    sample_y = ndspline(sample_x)
    for kx in range(2, 4):
        knots_x = np.r_[(0.0, ) * (kx + 1), 0.25, 0.5, 0.75,
                        (1.0, ) * (kx + 1)]
        for ky in range(2, 4):
            knots_y = np.r_[(0.0, ) * (ky + 1), 0.25, 0.5, 0.75,
                            (1.0, ) * (ky + 1)]

            knots = [knots_x, knots_y]

            # unweighted
            nspl = ndsplines.make_lsq_spline(sample_x, sample_y, knots,
                                             [kx, ky])
            ispl = interpolate.LSQBivariateSpline(sample_x[:, 0],
                                                  sample_x[:, 1],
                                                  sample_y,
                                                  knots_x[kx + 1:-kx - 1],
                                                  knots_y[ky + 1:-ky - 1],
                                                  bbox=[0., 1., 0., 1.],
                                                  kx=kx,
                                                  ky=ky)
            assert_allclose(ispl.get_knots()[0], nspl.knots[0])
            assert_allclose(ispl.get_knots()[1], nspl.knots[1])
            assert_allclose(nspl.coefficients.reshape(-1),
                            ispl.get_coeffs().reshape(-1))

            # random weights
            w = np.random.random(N)
            nspl = ndsplines.make_lsq_spline(sample_x, sample_y, knots,
                                             [kx, ky], w)
            ispl = interpolate.LSQBivariateSpline(
                sample_x[:, 0],
                sample_x[:, 1],
                sample_y,
                knots_x[kx + 1:-kx - 1],
                knots_y[ky + 1:-ky - 1],
                w=w,
                bbox=[0., 1., 0., 1.],
                kx=kx,
                ky=ky,
            )
            assert_allclose(ispl.get_knots()[0], nspl.knots[0])
            assert_allclose(ispl.get_knots()[1], nspl.knots[1])
            assert_allclose(nspl.coefficients.reshape(-1),
                            ispl.get_coeffs().reshape(-1))
Exemplo n.º 4
0
def _spline_psf_model(paras):
    psfs, xs, ys, xvec, yvec = paras
    normalized_psfs_func_list = []
    for wv_index in range(psfs.shape[-1]):
        # if 1:#np.isnan(psf_func(0,0)[0,0]):
        #     model_psf = psfs[:,:, :, wv_index]
        #     import matplotlib.pyplot as plt
        #     from mpl_toolkits.mplot3d import Axes3D
        #     fig = plt.figure(1)
        #     ax = fig.add_subplot(111,projection="3d")
        #     for k,color in zip(range(model_psf.shape[0]),["pink","blue","green","purple","orange"]):
        #         ax.scatter(xs[k].ravel(),ys[k].ravel(),model_psf[k].ravel(),c=color)
        model_psf = psfs[:, :, :, wv_index].ravel()
        where_nans = np.where(np.isfinite(model_psf))
        psf_func = interpolate.LSQBivariateSpline(xs.ravel()[where_nans],
                                                  ys.ravel()[where_nans],
                                                  model_psf[where_nans], xvec,
                                                  yvec)
        # if 1:
        #     print(psf_func(0,0))
        #     x_psf_vec, y_psf_vec = np.arange(2*nx_psf * 1.)/2.-nx_psf//2, np.arange(2*ny_psf* 1.)/2.-ny_psf//2
        #     x_psf_grid, y_psf_grid = np.meshgrid(x_psf_vec, y_psf_vec)
        #     ax.scatter(x_psf_grid.ravel(),y_psf_grid.ravel(),psf_func(x_psf_vec,y_psf_vec).transpose().ravel(),c="red")
        #     plt.show()
        normalized_psfs_func_list.append(psf_func)
    # print(len(normalized_psfs_func_list))
    return normalized_psfs_func_list
Exemplo n.º 5
0
def fit_spline( x, y, x_ref, knot_x=None, knot_y=None, num_knots=5, smooth=False, smooth_fac=None, weights=None, order=3):
        '''
        performs spline fit of the form dx = f(x,y)
        knot_x/knot_y are 1-d arrays that are the  x and y coordinates of knot locations
        '''
        if knot_x == None:
            knot_x = np.linspace(np.min(x), np.max(x), num=num_knots)
        if knot_y == None:
            knot_y = np.linspace(np.min(y), np.max(y), num=num_knots)
    

        if not smooth:
            if smooth_fac == None:
                spline = interpolate.LSQBivariateSpline(x, y, x_ref, knot_x, knot_y, kx=order, ky=order, w=weights)
                x_new = spline.ev(x, y)
            else:
                spline = interpolate.LSQBivariateSpline(x, y, x_ref, knot_x, knot_y, kx=order, ky=order,w=weights, s=smooth_fac)
                x_new = spline.ev(x, y)
        else:
            spline = interpolate.SmoothBivariateSpline(x, y, x_ref, w=weights)
            x_new = spline.ev(x, y)
        return x_new, spline 
Exemplo n.º 6
0
    nl_psf, ny_psf, nx_psf = psfs.shape

    x_psf_grid, y_psf_grid = np.meshgrid(
        np.arange(nx_psf * 1.) - nx_psf // 2,
        np.arange(ny_psf * 1.) - ny_psf // 2)
    psfs_func_list = []
    psfs[np.where(np.isnan(psfs))] = 0
    import warnings
    from scipy import interpolate
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        for wv_index in range(nl_psf):
            print(wv_index)
            model_psf = psfs[wv_index, :, :]
            psf_func = interpolate.LSQBivariateSpline(
                x_psf_grid.ravel(), y_psf_grid.ravel(), model_psf.ravel(),
                x_psf_grid[0, 0:nx_psf - 1] + 0.5,
                y_psf_grid[0:ny_psf - 1, 0] + 0.5)
            psfs_func_list.append(psf_func)

    ccmap = np.zeros((30, 10))
    nycc, nxcc = ccmap.shape
    for k in range(nxcc):
        for l in range(nycc):
            print(k, l)
            #build model
            psf_model = np.zeros(nphpfinputs.shape)
            for z in range(nlall):
                center = centers[z // nl_psf]
                # x_grid, y_grid = np.meshgrid(np.arange(nxall * 1.)-center[0],np.arange(nyall* 1.)-center[1])
                x_vec_centered = np.arange(
                    nxall * 1.) - (center[0] + k - nxcc // 2)
Exemplo n.º 7
0
 def time_lsq_bivariate_spline(self, n_samples):
     interpolate.LSQBivariateSpline(self.x, self.y, self.z,
                                    self.xknots.flat, self.yknots.flat)
Exemplo n.º 8
0
    def __init__(self,
                 inputs_shape,
                 numbasis,
                 sep,
                 pa,
                 dflux,
                 input_psfs,
                 input_wvs,
                 flux_conversion=None,
                 spectrallib=None,
                 spectrallib_units="flux",
                 star_spt=None,
                 refine_fit=False):
        """
        Defining the planet to characterizae

        Args:
            inputs_shape: shape of the inputs numpy array. Typically (N, y, x)
            numbasis: 1d numpy array consisting of the number of basis vectors to use
            sep: separation of the planet
            pa: position angle of the planet
            dflux: guess for contrast of planet averaged across band w.r.t star
            input_psfs: the psf of the image. A numpy array with shape (wv, y, x)
            input_wvs: the wavelegnths that correspond to the input psfs
                (doesn't need to be tiled to match the dimension of the input data of the instrument class)
            flux_conversion: an array of length N to convert from contrast to DN for each frame. Units of DN/contrast. 
                             If None, assumes dflux is the ratio between the planet flux and tthe input_psfs flux
            spectrallib: if not None, a list of spectra based on input_wvs
            spectrallib_units: can be either "flux"" or "contrast". Flux units requires dividing by the flux of the star to get contrast 
            star_spt: star spectral type, if None default to some random one
            refine_fit: (NOT implemented) refine the separation and pa supplied
        """
        # allocate super class
        super(FMPlanetPSF, self).__init__(inputs_shape, numbasis)

        self.inputs_shape = inputs_shape
        self.numbasis = numbasis
        self.sep = sep
        self.pa = pa
        self.dflux = dflux

        if spectrallib_units.lower() != "flux" and spectrallib_units.lower(
        ) != "contrast":
            raise ValueError(
                "spectrallib_units needs to be either 'flux' or 'contrast', not {0}"
                .format(spectrallib_units))

        # only need spectral info if not broadband
        numwvs = np.size(input_wvs)
        if numwvs > 1:
            if spectrallib is not None:
                self.spectrallib = spectrallib
            else:
                spectra_folder = os.path.dirname(os.path.abspath(
                    spec.__file__)) + os.sep + "spectra" + os.sep
                spectra_files = [spectra_folder + "t650g18nc.flx"]
                self.spectrallib = [
                    spec.get_planet_spectrum(filename, input_wvs)[1]
                    for filename in spectra_files
                ]

            # TODO: calibrate to contrast units
            # calibrate spectra to DN
            if spectrallib_units.lower() == "flux":
                # need to divide by flux of the star to get contrast units
                self.spectrallib = [
                    spectrum /
                    (spec.get_star_spectrum(input_wvs, star_type=star_spt)[1])
                    for spectrum in self.spectrallib
                ]
            self.spectrallib = [
                spectrum / np.mean(spectrum) for spectrum in self.spectrallib
            ]
        else:
            self.spectrallib = [np.array([1])]

        self.input_psfs = input_psfs
        self.input_psfs_wvs = input_wvs

        if flux_conversion is None:
            flux_conversion = np.ones(inputs_shape[0])
        self.flux_conversion = flux_conversion

        self.psf_centx_notscaled = {}
        self.psf_centy_notscaled = {}

        numwv, ny_psf, nx_psf = self.input_psfs.shape
        x_psf_grid, y_psf_grid = np.meshgrid(
            np.arange(nx_psf * 1.) - nx_psf // 2,
            np.arange(ny_psf * 1.) - ny_psf // 2)
        psfs_func_list = []
        for wv_index in range(numwv):
            model_psf = self.input_psfs[
                wv_index, :, :]  #* self.flux_conversion * self.spectrallib[0][wv_index] * self.dflux
            #psfs_interp_model_list.append(interpolate.bisplrep(x_psf_grid,y_psf_grid,model_psf))
            #psfs_interp_model_list.append(interpolate.SmoothBivariateSpline(x_psf_grid.ravel(),y_psf_grid.ravel(),model_psf.ravel()))
            psfs_func_list.append(
                interpolate.LSQBivariateSpline(
                    x_psf_grid.ravel(), y_psf_grid.ravel(), model_psf.ravel(),
                    x_psf_grid[0, 0:nx_psf - 1] + 0.5,
                    y_psf_grid[0:ny_psf - 1, 0] + 0.5))
            #psfs_interp_model_list.append(interpolate.interp2d(x_psf_grid,y_psf_grid,model_psf,kind="cubic",bounds_error=False,fill_value=0.0))
            #psfs_interp_model_list.append(interpolate.Rbf(x_psf_grid,y_psf_grid,model_psf,function="gaussian"))

            if 0:
                import matplotlib.pylab as plt
                #print(x_psf_grid.shape)
                #print(psfs_interp_model_list[wv_index](x_psf_grid.ravel(),y_psf_grid.ravel()).shape)
                a = psfs_func_list[wv_index](x_psf_grid[0, :],
                                             y_psf_grid[:, 0]).transpose()
                plt.figure()
                plt.subplot(1, 3, 1)
                plt.imshow(a, interpolation="nearest")
                plt.colorbar()
                ##plt.imshow(psfs_interp_model_list[wv_index](np.linspace(-10,10,500),np.linspace(-10,10,500)),interpolation="nearest")
                plt.subplot(1, 3, 2)
                plt.imshow(self.input_psfs[wv_index, :, :],
                           interpolation="nearest")
                plt.colorbar()
                plt.subplot(1, 3, 3)
                plt.imshow(abs(self.input_psfs[wv_index, :, :] - a),
                           interpolation="nearest")
                plt.colorbar()
                plt.show()

        self.psfs_func_list = psfs_func_list
def plot_integral(values,
                  start_date,
                  end_date,
                  smooth=True,
                  smooth_type='LSQ-bspline'):
    """
    Plot cumulative net worth values over time
    inputs
    -------
    values : (np.array) of cumulative net worth over time. See calculate_gradients()
    start_date : (datetime.date) Start date of plotting
        This MUST be the same start_date used to calculate values
    end_date : (datetime.date) end date of plotting
        This MUST be the same end_date used to calculate values
    smooth : (bool) Apply smoothing to the 3D graph surface
    smooth_type : (str) one of ['bv-bspline','LSQ-bspline','Smooth-bspline',
        'wiener']. The smoothing method to apply to the 3-d surface
    outputs
    -------
    None
    """

    msg = 'values should be numpy array, got {}'
    assert isinstance(values, np.ndarray), msg.format(type(values))
    msg = 'date must be datetime.date, got {}'
    assert isinstance(start_date, datetime.date), msg.format(type(start_date))
    assert isinstance(end_date, datetime.date), msg.format(type(end_date))

    # Dates for labeling x axis
    delta = end_date - start_date
    dates = [(start_date + datetime.timedelta(days=i))
             for i in range(delta.days)]

    #Apply b-spline fit to surface
    if smooth:

        if smooth_type == 'bv-bspline':
            x = np.arange(values.shape[0])  # Dates
            y = np.arange(values.shape[2])  # Probability
            xy, yx = np.meshgrid(x, y)
            xs = np.ravel(xy)
            ys = np.ravel(yx)
            # # Unpack in row-major order :)
            z = values[:, 0, :]
            zs = np.ravel(values[:, 0, :], order='F')
            """A sequence of length 5 returned by bisplrep containing the knot
            locations, the coefficients, and the degree of the spline:
                [tx, ty, c, kx, ky]."""
            s = sum(spline_smoothing_factor(xs.shape[0])) / 2
            tck = interpolate.bisplrep(xs, ys, zs, s=s * 100, eps=1e-12)
            z3d = interpolate.bisplev(x, y, tck)

        elif smooth_type == 'LSQ-bspline':
            # Least squares bivariate spline approximation
            x = np.arange(values.shape[0])  # Dates
            y = np.arange(values.shape[2])  # Probability
            xy, yx = np.meshgrid(x, y)
            xs = np.ravel(xy)
            ys = np.ravel(yx)
            # # Unpack in row-major order :)
            z = values[:, 0, :]
            zs = np.ravel(values[:, 0, :], order='F')
            """Weighted least-squares bivariate spline approximation
            https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.LSQBivariateSpline.html"""
            # Find knots
            # Knots are the intervals polynomials are broken into. More knots
            # Mean a more precise spline
            # knots = np.linspace(start, end, n_knots)
            tx = np.linspace(0, x.shape[0], int(np.sqrt(2 * x.shape[0])))
            ty = np.linspace(0, y.shape[0], int(np.sqrt(2 * x.shape[0])))
            # Evaluate spline
            LSQBSpline = interpolate.LSQBivariateSpline(xs, ys, zs, tx, ty)
            z3d = LSQBSpline(x, y, grid=True)

        elif smooth_type == 'Smooth-bspline':
            # Smooth bivariate spline approximation
            x = np.arange(values.shape[0])  # Dates
            y = np.arange(values.shape[2])  # Probability
            xy, yx = np.meshgrid(x, y)
            xs = np.ravel(xy)
            ys = np.ravel(yx)
            # # Unpack in row-major order :)
            z = values[:, 0, :]
            zs = np.ravel(values[:, 0, :], order='F')
            """Smooth bivariate spline approximation
            see https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.SmoothBivariateSpline.html"""
            # s = sum(spline_smoothing_factor(xs.shape[0])) / 2
            SmoothBSpline = interpolate.SmoothBivariateSpline(xs, ys, zs)
            z3d = SmoothBSpline(x, y, grid=True)

        elif smooth_type == 'wiener':
            # Smooth bivariate spline approximation
            x = np.arange(values.shape[0])  # Dates
            y = np.arange(values.shape[2])  # Probability
            xy, yx = np.meshgrid(x, y)
            z = values[:, 0, :]
            z3d = signal.wiener(z, mysize=(25, 1))

    else:

        x = np.arange(values.shape[0])
        y = np.arange(values.shape[2])
        xy, yx = np.meshgrid(x, y)
        z3d = values[:, 0, :]

    # X labels - dates
    num_ticks = 4
    tick_index = np.linspace(min(x), max(x), num=num_ticks, dtype=np.int16)
    xticks = x[tick_index]
    xlabel_index = tick_index
    xlabel_date = [dates[idx] for idx in xlabel_index]
    xlabels = [x.isoformat() for x in xlabel_date]
    # Y labels
    yticks = [int(y.shape[0] / 2)]
    ylabels = ['Worst → Best Case']
    """2-dimensional plotting"""
    fig2d = plt.figure(1)
    ax2d = fig2d.add_subplot(111)
    z2d = values.mean(axis=2)
    line = ax2d.plot(x, z2d, label='Bank Value', linewidth=2)

    ax2d.set_title('Mean Value Non-smoothed')
    ax2d.set_xlabel('Date')
    ax2d.set_ylabel('Total Value [$]')
    ax2d.set_xticks(xticks)
    ax2d.set_xticklabels(xlabels)
    """3-dimensional plotting"""
    fig3d = plt.figure(2)
    ax3d = fig3d.add_subplot(111, projection='3d')
    surf = ax3d.plot_surface(xy,
                             yx,
                             z3d.transpose(),
                             cmap='summer',
                             linewidth=0,
                             antialiased=False)
    # Label date axis
    ax3d.set_xticks(xticks)
    ax3d.set_xticklabels(xlabels)
    # Label probability axis
    ax3d.set_yticks(yticks)
    ax3d.set_yticklabels(ylabels)
    # Connect event to rotate probability axsi label
    rotateTickLabel = RotateTickLabel(ax3d.yaxis, ax3d)
    fig3d.canvas.mpl_connect('draw_event', rotateTickLabel)
    # Label value axis
    formatter = FuncFormatter(value_label_formatter)
    ax3d.zaxis.set_major_formatter(formatter)
    # ax3d.tick_params(pad=10)

    # Label axis
    fig3d.colorbar(surf, shrink=0.5, aspect=10)
    ax3d.set_title('Value, Time, Risk')
    ax3d.set_xlabel('Date', labelpad=15)
    ax3d.set_zlabel('Total Value [$]', labelpad=10)

    # Elev is in z plane, azim is in x,y plane
    ax3d.view_init(elev=20, azim=125)

    for angle in range(100, 125):
        ax3d.view_init(elev=20, azim=angle)
        plt.draw()
        plt.pause(0.003)

    plt.show()

    return None
Exemplo n.º 10
0
z_hst_lngth = np.max(XZarr[:, 1]) - np.min(XZarr[:, 1])

# Calculate knots (t) needed for LSQBivariateSpline
t_XZ = np.linspace(
    np.min(XZarr[:, 0]) + 0.1 * x_hst_lngth,
    np.max(XZarr[:, 0]) - 0.1 * x_hst_lngth, 11)
t_YZ = np.linspace(
    np.min(YZarr[:, 0]) + 0.1 * y_hst_lngth,
    np.max(YZarr[:, 0]) - 0.1 * y_hst_lngth, 11)
t_ZZ = np.linspace(
    np.min(XZarr[:, 1]) + 0.1 * z_hst_lngth,
    np.max(XZarr[:, 1]) - 0.1 * z_hst_lngth, 11)

# Interpolate using LSQBivariateSpline, hash if want to use Interp2D
f_Dens_XZ = interpolate.LSQBivariateSpline(XZarr[:, 0].ravel(),
                                           XZarr[:, 1].ravel(),
                                           XZarr[:, 2].ravel(), t_XZ, t_ZZ)
f_Dens_YZ = interpolate.LSQBivariateSpline(YZarr[:, 0].ravel(),
                                           YZarr[:, 1].ravel(),
                                           YZarr[:, 2].ravel(), t_YZ, t_ZZ)

# Interpolate using Interp2D - unhash if want to use instead of LSQ
#f_Dens_XZ=interpolate.interp2d(XZarr[:,0].ravel(), XZarr[:,1].ravel(),XZarr[:,2].ravel())
#f_Dens_YZ=interpolate.interp2d(YZarr[:,0].ravel(), YZarr[:,1].ravel(),YZarr[:,2].ravel())

#*****Plot the density profile XZ,YZ
# Create data for plot
PLT_X = np.linspace(
    min(mA_X) - S_factor * size_x,
    max(mA_X) + S_factor * size_x, 100)
PLT_Y = np.linspace(
Exemplo n.º 11
0
from scipy import interpolate

# Load data from 'HeVexpEHe.dat'
# x = col[0], y = col[1], z = col[2]
x, y, z = loadtxt('HeVexpEHe.dat', usecols = (0,1,2) , unpack=True)   

# Smooth bivariate spline approximation
# kx, ky are the degrees of the bivariate spline in the x and y direction respectively
zsfit5 = interpolate.SmoothBivariateSpline(x,y,z,kx=5,ky=5)
# Spline approximation evaluated at x,y
speval5 = zsfit5.ev(x,y)

# Least-squares bivariate spline approximation with specified 1D sequences of knot coordinates
xknots1 = array([0,1,240,249,250])
yknots1 = array([0,1,40,49,50])
lsqzs1fit5 = interpolate.LSQBivariateSpline(x,y,z,xknots1.flat,yknots1.flat,kx=5,ky=5)
lsqev1 = lsqzs1fit5.ev(x,y)

xknots2 = array([0,1,40,75,183,194,222,231,250])
yknots2 = array([0,1,5,11,43,45,46,47,50])
lsqzs2fit5 = interpolate.LSQBivariateSpline(x,y,z,xknots2.flat,yknots2.flat,kx=5,ky=5)
lsqev2 = lsqzs2fit5.ev(x,y)

xknots3 = array([0,1,14,125,165,184,219,220,223,225,231,232,239,241,250])
yknots3 = array([0,1,11,24,25,30,43,45,46,47,48,49,50])
lsqzs3fit5 = interpolate.LSQBivariateSpline(x,y,z,xknots3.flat,yknots3.flat,kx=5,ky=5)
lsqev3 = lsqzs3fit5.ev(x,y)

#PLOT 3D FIGURE
fig = plt.figure()
rect = fig.add_subplot(2,2,1).get_position()
Exemplo n.º 12
0
    #print smap(xr, yr)
    plt.plot(xl)
    plt.plot(xr)
    #plt.show()
    xsp = interpolate.SmoothBivariateSpline(xr, yr, xl, kx=2, ky=2)
    print xsp.get_knots()

    #example from tests, testfitpack.py

    x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
    y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
    z = [3, 3, 4, 4, 5, 6, 3, 3, 3]
    s = 0.1
    tx = [1 + s, 3 - s]
    ty = [1 + s, 3 - s]
    lut = interpolate.LSQBivariateSpline(x, y, z, tx, ty, kx=1, ky=1)

    import numpy as np
    from scipy import interpolate
    import matplotlib.pyplot as plt

    #2d spline interpolation example from the tutorial
    #-------------------------------------------------

    # Define function over sparse 20x20 grid

    x, y = np.mgrid[-1:1:20j, -1:1:20j]
    z = (x + y) * np.exp(-6.0 * (x * x + y * y))

    plt.figure()
    plt.pcolor(x, y, z)
Exemplo n.º 13
0
    def __init__(self,
                 inputs_shape,
                 numbasis,
                 sep,
                 pa,
                 input_psfs,
                 input_psfs_wvs,
                 datatype="float",
                 stamp_size=None):
        """
        Defining the planet to characterizae

        Args:
            inputs_shape: shape of the inputs numpy array. Typically (N, y, x)
            numbasis: 1d numpy array consisting of the number of basis vectors to use
            sep: separation of the planet
            pa: position angle of the planet
            input_psfs: the psf of the image. A numpy array with shape (wv, y, x)
            input_psfs_wvs: the wavelegnths that correspond to the input psfs
            flux_conversion: an array of length N to convert from contrast to DN for each frame. Units of DN/contrast
            wavelengths: wavelengths of data. Can just be a string like 'H' for H-band
            spectrallib: if not None, a list of spectra
            star_spt: star spectral type, if None default to some random one
            refine_fit: refine the separation and pa supplied
        """
        # allocate super class
        super(ExtractSpec, self).__init__(inputs_shape, np.array(numbasis))

        if stamp_size is None:
            self.stamp_size = 10
        else:
            self.stamp_size = stamp_size

        if datatype == "double":
            self.data_type = ctypes.c_double
        elif datatype == "float":
            self.data_type = ctypes.c_float

        self.N_numbasis = np.size(numbasis)
        self.ny = self.inputs_shape[1]
        self.nx = self.inputs_shape[2]
        self.N_frames = self.inputs_shape[0]

        self.inputs_shape = inputs_shape
        self.numbasis = numbasis
        self.sep = sep
        self.pa = pa

        self.input_psfs = input_psfs
        self.input_psfs_wvs = list(
            np.array(input_psfs_wvs, dtype=self.data_type))
        self.nl = np.size(input_psfs_wvs)
        #self.flux_conversion = flux_conversion
        # Make sure the peak value is unity for all wavelengths
        self.sat_spot_spec = np.nanmax(self.input_psfs, axis=(1, 2))
        self.aper_over_peak_ratio = np.zeros(np.size(self.input_psfs_wvs))
        for l_id in range(self.input_psfs.shape[0]):
            self.aper_over_peak_ratio[l_id] = np.nansum(
                self.input_psfs[l_id, :, :]) / self.sat_spot_spec[l_id]
            self.input_psfs[l_id, :, :] = self.input_psfs[
                l_id, :, :] / np.nansum(self.input_psfs[l_id, :, :])

        self.nl, self.ny_psf, self.nx_psf = self.input_psfs.shape

        self.psf_centx_notscaled = {}
        self.psf_centy_notscaled = {}

        numwv, ny_psf, nx_psf = self.input_psfs.shape
        x_psf_grid, y_psf_grid = np.meshgrid(
            np.arange(nx_psf * 1.) - nx_psf / 2,
            np.arange(ny_psf * 1.) - ny_psf / 2)
        psfs_func_list = []
        for wv_index in range(numwv):
            model_psf = self.input_psfs[
                wv_index, :, :]  #* self.flux_conversion * self.spectrallib[0][wv_index] * self.dflux
            psfs_func_list.append(
                interpolate.LSQBivariateSpline(
                    x_psf_grid.ravel(), y_psf_grid.ravel(), model_psf.ravel(),
                    x_psf_grid[0, 0:nx_psf - 1] + 0.5,
                    y_psf_grid[0:ny_psf - 1, 0] + 0.5))

        self.psfs_func_list = psfs_func_list
Exemplo n.º 14
0
def flux_model(raw_tar_catalogue, filtered_ref_catalogue, options):
    """
        Take the raw target and reference catalogue, do a quick and dirty cross match of the two with only high snr (10) target sources. Construct a 2D polynomial model of the flux correction factor across the field of sources.
        
        :param raw_tar_catalogue: the target catalogue
        :param filtered_ref_catalogue: the reference catalogue filtered for sources within the RA and DEC range of the target catalogue

        :return: flux correction factor model to apply to target catalogue
        """

    #filter for snr>10 sources
    snr_filter = np.where((raw_tar_catalogue['peak_flux'] /
                           raw_tar_catalogue['local_rms']) >= 10)
    tar_catalogue = raw_tar_catalogue[snr_filter]

    #we want quite a tight match
    limiting_res = 118.0 / 3600.0 * u.degree
    ref_cat = SkyCoord(filtered_ref_catalogue['ra'],
                       filtered_ref_catalogue['dec'],
                       unit=u.degree,
                       frame='icrs')
    tar_cat = SkyCoord(tar_catalogue['ra'],
                       tar_catalogue['dec'],
                       unit=u.degree,
                       frame='icrs')
    gross_matched_idx, gross_matched_sep, dum = tar_cat.match_to_catalog_sky(
        ref_cat)
    tar_cat_matched_within_resolution = tar_catalogue[np.where(
        gross_matched_sep <= limiting_res)]
    ref_cat_matched_within_resolution = filtered_ref_catalogue[
        gross_matched_idx][np.where(gross_matched_sep <= limiting_res)]

    #get our fluxes and ratios for the target and reference catalogues
    reference_flux = ref_cat_matched_within_resolution['peak_flux']
    flux_ratio = tar_cat_matched_within_resolution['peak_flux'] / reference_flux

    #use a polynomial 2D model for the field of sources
    resulting_model = interpolate.LSQBivariateSpline(
        tar_cat_matched_within_resolution['ra'],
        tar_cat_matched_within_resolution['dec'],
        flux_ratio, [
            min(tar_cat_matched_within_resolution['ra']),
            max(tar_cat_matched_within_resolution['ra'])
        ], [
            min(tar_cat_matched_within_resolution['dec']),
            max(tar_cat_matched_within_resolution['dec'])
        ],
        kx=options.flux_model_deg,
        ky=options.flux_model_deg)

    if options.plotting == True:
        #plot resulting model for user
        x = tar_cat_matched_within_resolution['ra']
        y = tar_cat_matched_within_resolution['dec']
        xi = np.linspace(min(x), max(x), 100)
        yi = np.linspace(min(y), max(y), 100)
        zi = interpolate.griddata((x, y),
                                  flux_ratio, (xi[None, :], yi[:, None]),
                                  method='cubic')
        x = np.linspace(min(tar_catalogue['ra']), max(tar_catalogue['ra']),
                        100)
        y = np.linspace(min(tar_catalogue['dec']), max(tar_catalogue['dec']),
                        100)
        X, Y = np.meshgrid(x, y)
        solution = resulting_model.ev(X, Y)
        solution_masked = np.ma.masked_array(solution, mask=np.isnan(zi))
        plot = plt.imshow(solution_masked)
        plt.colorbar(plot)
        plt.savefig('Flux_correction_model' + options.save_file_suffix +
                    '.png')
        plt.close()
    return resulting_model
Exemplo n.º 15
0
# remove nan - Fluxes
BFullFs_rm = np.copy(BFullFs)
# print('... remove isolated points (3 sided)')
# skip_inds3a = remove_indexes(BFullFs_rm, ygrid, xgrid, FullYs, FullXs, size, 3)
# BFullFs_rm[skip_inds3a] = np.nan
# print('... remove isolated points (4 sided)')
# skip_inds4a = remove_indexes(BFullFs_rm, ygrid, xgrid, FullYs, FullXs, size, 4)
# BFullFs_rm[skip_inds4a] = np.nan

nan_inds = np.where(np.isfinite(BFullFs_rm))[0]
print(len(nan_inds))

print('... running spline fit')
time1 = datetime.now()
SplineFit = interpolate.LSQBivariateSpline(xfull[nan_inds], yfull[nan_inds], BFullFs_rm[nan_inds], knot_grid, knot_grid, kx=3, ky=3 )
time2 = datetime.now()
print('     spline time: ', time2 - time1)



####      
## fill in gaps
#round 1
print('~~ finding gaps, round 1')
interp_addi = interp_indexes(BFullFs_rm, ygrid, xgrid, FullYs, FullXs, size, gap,frac)
interp_inds = np.sort(np.append(nan_inds, interp_addi))

TempGrid = SplineFit.ev(xfull[interp_inds],yfull[interp_inds])
TempFull = np.empty([size*size])*np.nan
TempFull[interp_inds] = TempGrid
Exemplo n.º 16
0
def interpolate_g(xi, yi, zi, xx, yy, knots=10, error=False, mask=None):
    """Create a grid of zi values interpolating the values from xi,yi,zi
    
    **ARGUMENTS**
    
        ========= ==================================================================
        xi,yi,zi  1D Lists  or arrays containing the values to use as base for the 
                  interpolation
        xx,yy     1D vectors or lists containing the output coordinates
        samples   tuple containing the shape of the output array.
        knots     number of knots to be used in each direction
        error     if set to true, half of the points (x, y, z) are used to create 
                  the interpolation, and half are used to evaluate the interpolation
                  error
        ========= ==================================================================
    
    
    """
    xi = array(xi)
    yi = array(yi)
    zi = array(zi)

    #print xi
    #print yi
    #print zi
    assert xi.ndim == 1, "xi must ba a 1D array or list"
    assert yi.ndim == 1, "yi must ba a 1D array or list"
    assert zi.ndim == 1, "zi must ba a 1D array or list"

    assert xx.ndim == 1, "xx must ba a 1D array or list"
    assert yy.ndim == 1, "yy must ba a 1D array or list"

    assert len(xi) == len(yi) and len(xi) == len(
        zi), "xi, yi, zi must have the same number of items"

    if error == True:
        # Create a list of indexes to be able to select the points that are going
        # to be used as spline generators, and as control points
        idx = where(arange(len(xi)) % 2 == 0, False, True)

# Use only half of the samples to create the Spline,
    if error == True:
        isp = argwhere(idx == True)
        ich = argwhere(idx == False)

        xsp = xi[isp]
        ysp = yi[isp]
        zsp = zi[isp]

        xch = xi[ich]
        ych = yi[ich]
        zch = zi[ich]
    else:
        xsp = xi
        ysp = yi
        zsp = zi

    #Distribute homogeneously the knots
    xk = linspace(xsp.min(), xsp.max(), knots)
    yk = linspace(ysp.min(), ysp.max(), knots)

    # LSQBivariateSpline using some knots gives smaller error than
    # SmoothBivariateSpline
    di = interpolate.LSQBivariateSpline(xsp, ysp, zsp, xk[1:-1], yk[1:-1])
    #print xsp,ysp,zsp
    #di=interpolate.SmoothBivariateSpline(xsp, ysp, zsp)

    # Evaluate error
    if error == True:
        zch1 = di.ev(xch, ych)
        er = (zch.flatten() - zch1).std()

    if mask == None:
        #d=griddata(xi,  yi,  zi,  xx, yy) #
        d = di(xx, yy).transpose()
    else:
        d = ma_array(di(xx, yy).transpose(), mask=mask)

    if error == True: return d, er
    else: return d
Exemplo n.º 17
0
def upsample2DDens(x, y, wghts, bnx0, bny0, S_factor, plotbasename=False):
    # Merge XZ and YZ into common array to allow creation of 2D histogram data for XZ and YZ planes
    xy = np.vstack((x.flat, y.flat)).T

    lenx = np.amax(x) - np.amin(x)
    leny = np.amax(y) - np.amin(y)
    #HxHz,edges_XZ = np.histogramdd(m_Xm_Z, bins = (binnumber_X,binnumber_Z),range=((min(mA_X)-S_factor*size_x,max(mA_X)+S_factor*size_x),(min(mA_Z)-S_factor*size_z,max(mA_Z)+S_factor*size_z)),normed=False,weights=m_WGHT)
    # Crate histogram for XZ and YZ planes and stretch it using S_factor variable
    HxHy, edges_XY = np.histogramdd(xy,
                                    bins=(bnx0, bny0),
                                    range=((min(x) - S_factor * lenx,
                                            max(x) + S_factor * lenx),
                                           (min(y) - S_factor * leny,
                                            max(y) + S_factor * leny)),
                                    normed=False,
                                    weights=wghts)

    # HxHz = ndimage.gaussian_filter(HxHz,1.50)

    if (not plotbasename):
        qplots = False
    else:
        qplots = True

    if (qplots):
        XL, YL = np.meshgrid(edges_XY[0], edges_XY[1])

        zyplot = plt.subplot(111)
        zyplot.pcolormesh(XL, YL, HxHy)
        #plt.show()
        plt.savefig(plotbasename + '-ORIG.png')
        plt.clf()

    lexz = len(edges_XY[0]) - 1
    lexz2 = len(edges_XY[1]) - 1

    XYarr = np.zeros((lexz * lexz2, 3))

    # Convert XZ/YZ density histograms to XZ_Density/YZ_Density arrays (move the histogram to array like: Value,X,Y,Z)
    for zz in range(1, len(edges_XY[1])):
        for xx in range(1, len(edges_XY[0])):
            XYarr[(xx - 1) + (zz - 1) * lexz,
                  0] = (edges_XY[0][xx] + edges_XY[0][xx - 1]) * 0.5
            XYarr[(xx - 1) + (zz - 1) * lexz,
                  1] = (edges_XY[1][zz] + edges_XY[1][zz - 1]) * 0.5
            XYarr[(xx - 1) + (zz - 1) * lexz, 2] = HxHy[xx - 1, zz - 1]

    #*** INTERPOLATE XZ AND YZ PLANES USING 2D FUNCTION
    # Calculate the length of X,Y,Z histogram for fitting
    x_hst_lngth = np.max(XYarr[:, 0]) - np.min(XYarr[:, 0])
    y_hst_lngth = np.max(XYarr[:, 1]) - np.min(XYarr[:, 1])

    # Calculate knots (t) needed for LSQBivariateSpline
    t_XY = np.linspace(
        np.min(XYarr[:, 0]) + 0.1 * x_hst_lngth,
        np.max(XYarr[:, 0]) - 0.1 * x_hst_lngth, 25)
    t_YY = np.linspace(
        np.min(XYarr[:, 1]) + 0.1 * y_hst_lngth,
        np.max(XYarr[:, 1]) - 0.1 * y_hst_lngth, 25)

    # Interpolate using LSQBivariateSpline, hash if want to use Interp2D
    f_Dens_XY = interpolate.LSQBivariateSpline(XYarr[:, 0].ravel(),
                                               XYarr[:, 1].ravel(),
                                               XYarr[:, 2].ravel(), t_XY, t_YY)

    if (qplots):

        PLT_X = np.linspace(
            min(x) - S_factor * lenx,
            max(x) + S_factor * lenx, 2500)
        PLT_Y = np.linspace(
            min(y) - S_factor * leny,
            max(y) + S_factor * leny, 2500)

        zxplot = plt.subplot(111)

        zxplot.pcolormesh(PLT_Y, PLT_X, f_Dens_XY(PLT_X, PLT_Y))
        #plt.show()
        plt.savefig(plotbasename + '-UPSAMPLED.png')
        plt.clf()

    return f_Dens_XY