Esempio n. 1
0
    def information_from_grid(self,
                              theta_grid,
                              fisherinformation_grid,
                              option="smooth",
                              inverse="exact"):
        """
        Loads a grid of coordinates and corresponding Fisher Information, which is then interpolated.
            
        Parameters
        ----------
        theta_grid : ndarray
            List if parameter points `theta` at which the Fisher information matrices `I_ij(theta)`
            is evaluated. Shape (n_gridpoints, n_dimension).
            
        fisherinformation_grid : ndarray
            List if Fisher information matrices `I_ij(theta)`. Shape (n_gridpoints, n_dimension, n_dimension).
            
        option : {"smooth", "linear"}
            Defines if the Fisher Information is interpolated smoothly using the function
            CloughTocher2DInterpolator() or piecewise linear using LinearNDInterpolator().
            Default = 'smooth'.
            
        inverse : {"exact", "interpolate"}
            Defines if the inverse Fisher Information is obtained by either first interpolating
            the Fisher Information and then inverting it ("exact") or by first inverting the grid
            of Fisher Informations and then interpolating the inverse ("interpolate"). Default = 'exact'.
        """

        self.infotype = "grid"
        self.inverse = inverse

        # load from file
        theta_grid = load_and_check(theta_grid)
        fisherinformation_grid = load_and_check(fisherinformation_grid)
        self.dimension = len(fisherinformation_grid[0])

        # Interpolate Information
        if option == "linear":
            self.infofunction = LinearNDInterpolator(
                points=theta_grid, values=np.array(fisherinformation_grid))
        elif option == "smooth":
            self.infofunction = CloughTocher2DInterpolator(
                points=theta_grid, values=np.array(fisherinformation_grid))
        else:
            RuntimeError("Option %s unknown", option)

        # Interpolate inverse information
        if self.inverse == "interpolate":
            inv_fisherinformation_grid = np.array(
                [np.linalg.inv(info) for info in fisherinformation_grid])
            if option == "linear":
                self.infofunction_inv = LinearNDInterpolator(
                    points=theta_grid, values=inv_fisherinformation_grid)
            elif option == "smooth":
                self.infofunction_inv = CloughTocher2DInterpolator(
                    points=theta_grid, values=inv_fisherinformation_grid)
Esempio n. 2
0
def GridInterpolation(pos, data, Xi, Yi):
    from scipy.spatial.qhull import Delaunay
    from scipy.interpolate import CloughTocher2DInterpolator
    import itertools

    extremes = np.array([pos.min(axis=0), pos.max(axis=0)])
    diffs = extremes[1] - extremes[0]
    extremes[0] -= diffs
    extremes[1] += diffs

    eidx = np.array(
        list(
            itertools.product(*([[0] * (pos.shape[1] - 1) + [1]] *
                                pos.shape[1]))))
    pidx = np.tile(np.arange(pos.shape[1])[np.newaxis], (len(eidx), 1))
    n_extra = pidx.shape[0]
    outer_pts = extremes[eidx, pidx]
    pos = np.concatenate((pos, outer_pts))
    tri = Delaunay(pos)

    data = np.concatenate((data, np.zeros(n_extra)))
    Interpolator = CloughTocher2DInterpolator(tri, data)
    args = [Xi, Yi]
    Zi = Interpolator(*args)
    return Zi
Esempio n. 3
0
def interpolate_spectrum(value: np.array,
                         coordinates: np.array,
                         resolution: int = 64) -> np.array:
    """
    Generates an image from a topography expressed in a number
    of colour dimensions along with a 2D map of channels.
    """
    try:
        _, color_channels = value.shape
    except ValueError:
        color_channels = 1

    # Images should be square
    width = height = np.round(np.max(coordinates))
    step_size = (width + height) / resolution

    x, y = np.meshgrid(np.arange(-width, width, step_size),
                       np.arange(-height, height, step_size))

    interpolation = CloughTocher2DInterpolator(coordinates, value)
    z = interpolation(np.c_[x.ravel(), y.ravel()])

    img_width = img_height = int(np.sqrt(len(z)))

    if color_channels < 2:
        img_data = np.reshape(z, (img_width, img_height))
    else:
        img_data = np.reshape(z, (img_width, img_height, color_channels))

    img = Image.fromarray((img_data * 255).astype(np.uint8))

    return img
Esempio n. 4
0
    def _map_reservoir_surface_to_velocity(self, res_surface, vcube):
        """
        Interpolates reservoir top surface to velocity grid
        """
        # downsample segy if segy resultion of CDP and
        # sample rate is higher than of the Eclgrid
        x, y, traces, nt, self._dt = self._read_velocity(
            vcube, res_surface.cell_corners)
        # this is some clever integration of traces using
        # averaging between two samples?
        vel_t_int = np.zeros_like(traces)
        vel_t_int[:, :, 1:] = (traces[:, :, 0:-1] + traces[:, :, 1:]) / 2
        # cumulative sum of each trace and reshape to 2D
        # array of size (num_trace, num_samples)
        self._z3d = np.cumsum(vel_t_int * self._dt / 2,
                              2).reshape(-1, vel_t_int.shape[-1])
        # this creates interploation functions over existing surface / grid
        ip = CloughTocher2DInterpolator((res_surface.x, res_surface.y),
                                        res_surface.z)
        # interpolate over our vel field given by pos x and y
        z = ip(x, y)
        # z gives a new depth for segy CDP pos given the grid inerface surface

        # So far we have downsampled the segy file to correspond
        # with the resolution of the grid.
        # The x,y values corresponds to the remaining segy positions.
        # furthermore the z values are the interpolated values that
        # match the top layer of the active cells in the grid file.
        self._nx, self._ny = x.shape
        self._x = x.reshape(-1)
        self._y = y.reshape(-1)
        self._z = z.reshape(-1)
Esempio n. 5
0
    def set_values(self, v):
        """Set the values at interpolation points."""
        # Rbf with thin-plate is what we used to use, but it's slower and
        # looks about the same:
        #
        #     zi = Rbf(x, y, v, function='multiquadric', smooth=0)(xi, yi)
        #
        # Eventually we could also do set_values with this class if we want,
        # see scipy/interpolate/rbf.py, especially the self.nodes one-liner.
        from scipy.interpolate import CloughTocher2DInterpolator

        if isinstance(self.border, Integral):
            v_extra = np.ones(self.n_extra) * self.border
        elif isinstance(self.border, str) and self.border == 'mean':
            n_points = v.shape[0]
            v_extra = np.zeros(self.n_extra)
            indices, indptr = self.tri.vertex_neighbor_vertices
            rng = range(n_points, n_points + self.n_extra)
            for idx, extra_idx in enumerate(rng):
                ngb = indptr[indices[extra_idx]:indices[extra_idx + 1]]
                ngb = ngb[ngb < n_points]
                v_extra[idx] = v[ngb].mean()

        v = np.concatenate((v, v_extra))
        self.interpolator = CloughTocher2DInterpolator(self.tri, v)
        return self
Esempio n. 6
0
def apply_interp(index, x1, y1, x2, y2, data):
    model = CloughTocher2DInterpolator(
        np.transpose([x1, y1]), np.ravel(data), fill_value=-1
    )
    # evaluate the model over this range
    newdata = model(x2, y2)
    return index, newdata
Esempio n. 7
0
def correct_images(fnames, dxmodel, dymodel, suffix):
    """
    Read a list of fits image, and apply pixel-by-pixel corrections based on the
    given x/y models. Interpolate back to a regular grid, and then write an
    output file
    :param fname: input fits file
    :param dxodel: x model
    :param dymodel: x model
    :param fout: output fits file
    :return: None
    """
    # Get co-ordinate system from first image
    im = fits.open(fnames[0])
    data = np.squeeze(im[0].data)
    # create a map of (x,y) pixel pairs as a list of tuples
    xy = np.indices(data.shape, dtype=np.float32)
    xy.shape = (2, xy.shape[1] * xy.shape[2])

    x = np.array(xy[1, :])
    y = np.array(xy[0, :])

    # calculate the corrections in blocks of 100k since the rbf fails on large blocks
    print 'applying corrections to pixel co-ordinates',
    if len(x) > 100000:
        print 'in cycles'
        n = 0
        borders = range(0, len(x) + 1, 100000)
        if borders[-1] != len(x):
            borders.append(len(x))
        for s1 in [slice(a, b) for a, b in zip(borders[:-1], borders[1:])]:
            x[s1] += dxmodel(x[s1], y[s1])
            # the x coords were just changed so we need to refer back to the original coords
            y[s1] += dymodel(xy[1, :][s1], y[s1])
            n += 1
            sys.stdout.write("{0:3.0f}%...".format(100 * n / len(borders)))
            sys.stdout.flush()
    else:
        print 'all at once'
        x += dxmodel(x, y)
        y += dymodel(xy[1, :], y)

    for fname in fnames:
        fout = fname.replace(".fits", "_" + suffix + ".fits")
        im = fits.open(fname)
        data = np.squeeze(im[0].data)
        print 'interpolating', fname
        ifunc = CloughTocher2DInterpolator(np.transpose([x, y]),
                                           np.ravel(data))
        interpolated_map = ifunc(xy[1, :], xy[0, :])
        interpolated_map.shape = data.shape
        # Float32 instead of Float64 since the precision is meaningless
        im[0].data = np.array(interpolated_map, dtype=np.float32)
        # NaN the edges by 10 pixels to avoid weird edge effects
        im[0].data[0:10, :] = np.nan
        im[0].data[:, 0:10] = np.nan
        im[0].data[:, -10:im[0].data.shape[0]] = np.nan
        im[0].data[-10:im[0].data.shape[1], :] = np.nan
        im.writeto(fout, clobber=True)
        print "wrote", fout
    return
Esempio n. 8
0
 def get_Grid(self, data):
     from scipy.interpolate import CloughTocher2DInterpolator
     idata = np.concatenate((data, np.zeros(self.n_extra)))
     Interpolator = CloughTocher2DInterpolator(self.tri, idata)
     args = [self.Xi, self.Yi]
     Zi = Interpolator(*args)
     return Zi
Esempio n. 9
0
    def __init__(self, filename, band):
        self.fi = filename
        self.refdata = self.Stack()
        self.telescope = Telescope(airmass=1.1)
        self.blue_cutoff = 300.
        self.red_cutoff = 800.
        self.param_Fisher = ['X0', 'X1', 'Color']
        self.method = 'cubic'

        self.band = band
        idx = self.refdata['band'] == band
        lc_ref = self.refdata[idx]

        # load reference values (only once)
        phase_ref = lc_ref['phase']
        z_ref = lc_ref['z']
        flux_ref = lc_ref['flux']
        fluxerr_ref = lc_ref['fluxerr']
        self.gamma_ref = lc_ref['gamma'][0]
        self.m5_ref = np.unique(lc_ref['m5'])[0]
        self.dflux_interp = {}
        self.flux_interp = CloughTocher2DInterpolator((phase_ref, z_ref),
                                                      flux_ref,
                                                      fill_value=1.e-5)
        self.fluxerr_interp = CloughTocher2DInterpolator((phase_ref, z_ref),
                                                         fluxerr_ref,
                                                         fill_value=1.e-8)
        for val in self.param_Fisher:
            dflux = lc_ref['d' + val]
            self.dflux_interp[val] = CloughTocher2DInterpolator(
                (phase_ref, z_ref), dflux, fill_value=1.e-8)
            """
            dFlux[val] = griddata((phase, z), dflux, (phase_obs, yi_arr),
                                  method=self.method, fill_value=0.)
            """
        # this is to convert mag to flux in e per sec
        self.mag_to_flux_e_sec = {}
        mag_range = np.arange(14., 32., 0.1)
        for band in 'grizy':
            fluxes_e_sec = self.telescope.mag_to_flux_e_sec(
                mag_range, [band] * len(mag_range), [30] * len(mag_range))
            self.mag_to_flux_e_sec[band] = interpolate.interp1d(
                mag_range,
                fluxes_e_sec[:, 1],
                fill_value=0.,
                bounds_error=False)
Esempio n. 10
0
def interpolate(thetas,
                z_thetas,
                xx,
                yy,
                method='linear',
                z_uncertainties_thetas=None,
                matern_exponent=0.5,
                length_scale_min=0.001,
                length_scale_default=1.,
                length_scale_max=1000.,
                noise_level=0.001,
                subtract_min=False):
    if method == 'cubic':

        interpolator = CloughTocher2DInterpolator(thetas[:], z_thetas)

        zz = interpolator(np.dstack((xx.flatten(), yy.flatten())))
        zi = zz.reshape(xx.shape)

    elif method == 'gp':

        if z_uncertainties_thetas is not None:
            gp = GaussianProcessRegressor(
                normalize_y=True,
                kernel=ConstantKernel(1.0, (1.e-9, 1.e9)) * Matern(
                    length_scale=[length_scale_default],
                    length_scale_bounds=[(length_scale_min, length_scale_max)],
                    nu=matern_exponent) + WhiteKernel(noise_level),
                n_restarts_optimizer=10,
                alpha=z_uncertainties_thetas)
        else:
            gp = GaussianProcessRegressor(
                normalize_y=True,
                kernel=ConstantKernel(1.0, (1.e-9, 1.e9)) * Matern(
                    length_scale=length_scale_default,
                    length_scale_bounds=(length_scale_min, length_scale_max),
                    nu=matern_exponent) + WhiteKernel(noise_level),
                n_restarts_optimizer=10)

        gp.fit(thetas[:], z_thetas[:])

        zz, _ = gp.predict(np.c_[xx.ravel(), yy.ravel()], return_std=True)
        zi = zz.reshape(xx.shape)

    elif method == 'linear':
        interpolator = LinearNDInterpolator(thetas[:], z_thetas)
        zz = interpolator(np.dstack((xx.flatten(), yy.flatten())))
        zi = zz.reshape(xx.shape)

    else:
        raise ValueError

    mle = np.unravel_index(zi.argmin(), zi.shape)

    if subtract_min:
        zi -= zi[mle]

    return zi, mle
Esempio n. 11
0
    def fit(self, X, y):

        if X.shape[1] != 2:
            raise ValueError('Only 2D interpolation is supported '
                             'via {}'.format(__class__.__name__))

        self._interpolator = CloughTocher2DInterpolator(
            X, y, fill_value=self.fill_value, maxiter=self.maxiter,
            rescale=self.rescale, tol=self.tol
        )
Esempio n. 12
0
    def loadMesh(self):
        """load R-Z mesh and psi values, then create map between each psi 
           value and the series of points on that surface.
        """
        # get mesh R,Z and psin
        RZ = self.readCmd(self.mesh_file, 'coordinates/values')
        R = RZ[:, 0]
        Z = RZ[:, 1]
        psi = self.readCmd(self.mesh_file, 'psi')
        psin = psi / self.unit_dic['psi_x']
        tri = self.readCmd(self.mesh_file,
                           'cell_set[0]/node_connect_list')  #already 0-based

        # set limits if not user specified
        if self.Rmin is None: self.Rmin = np.min(R)
        if self.Rmax is None: self.Rmax = np.max(R)
        if self.Zmin is None: self.Zmin = np.min(Z)
        if self.Zmax is None: self.Zmax = np.max(Z)
        if self.psinMin is None: self.psinMin = np.min(psin)
        if self.psinMax is None: self.psinMax = np.max(psin)

        #limit to the user-input ranges
        self.rzInds = ((R >= self.Rmin) & (R <= self.Rmax) & (Z >= self.Zmin) &
                       (Z <= self.Zmax) & (psin >= self.psinMin) &
                       (psin <= self.psinMax))

        self.RZ = RZ[self.rzInds, :]
        self.psin = psin[self.rzInds]

        # psi interpolant
        fill_ = np.nan
        if self.kind == 'linear':
            self.psi_interp = LinearNDInterpolator(self.RZ,
                                                   self.psin,
                                                   fill_value=fill_)
        elif self.kind == 'cubic':
            self.psi_interp = CloughTocher2DInterpolator(self.RZ,
                                                         self.psin,
                                                         fill_value=fill_)
        else:
            raise NameError("The method '{}' is not defined".format(self.kind))

        #get the triangles which are all contained within the vertices defined by
        #the indexes igrid
        #find which triangles are in the defined spatial region
        tmp = self.rzInds[tri]  #rzInds T/F array, same size as R
        goodTri = np.all(
            tmp, axis=1)  #only use triangles who have all vertices in rzInds
        self.tri = tri[goodTri, :]
        #remap indices in triangulation
        indices = np.where(self.rzInds)[0]
        for i in range(len(indices)):
            self.tri[self.tri == indices[i]] = i
 def update_paramgrid(paramgrid_old, idx1, idx2, x_all, y_all, vals, grid_x, grid_y):
     """Compute parameter grid from two curves, merge this grid with old grid."""
     # Make vectors of x, y, and parameter values for these two curves.
     x = np.concatenate([x_all[idx1], x_all[idx2]])
     y = np.concatenate([y_all[idx1], y_all[idx2]])
     vals = np.concatenate([vals[idx1], vals[idx2]])
     # Perform interpolation for each parameter of exponential function.
     # Syntax note: CloughTocher function returns a CloughTocher function, this
     # function is called on the meshgrids to produce the interpolated grid.
     paramgrid_new = CloughTocher2DInterpolator((x, y), vals)(grid_x, grid_y)
     # Merge parameter grids by keeping all non-nan values in the new grid, and 
     # replacing all nan positions with the values from the old grid. 
     paramgrids_merged = np.where(~np.isnan(paramgrid_new), paramgrid_new, paramgrid_old)
     return paramgrids_merged
Esempio n. 14
0
def gen_func(x, y, z):
    #ziped_xyz=zip(zip(x,y),z)
    #ziped_xyz=[x for x in ziped_xyz if x[1]!=0]
    #unziped_xyz=zip(*ziped_xyz)
    #points=array(unziped_xyz[0],dtype=float64)
    #z=array(unziped_xyz[1],dtype=float64)
    points = array(zip(x, y), dtype=float16)
    z = array(z, dtype=float16)

    # print points
    # print z

    f = CloughTocher2DInterpolator(points, z)  #Clough-Tocher method
    with open('estimated_func.pkl', 'wb') as output:
        pickle.dump(f, output, pickle.HIGHEST_PROTOCOL)
Esempio n. 15
0
 def __call__(self, c):
     temp_interp = []
     for i in range(self.nSamples):
         if self.method == 'CT2D':
             ip = CloughTocher2DInterpolator(self.locs,
                                             self.feat_array_temp[c][i, :],
                                             fill_value=np.nan)
             temp_interp.append(ip((self.grid_x, self.grid_y)))
         elif self.method == 'RBF':
             ip = Rbf(self.locs[:, 0],
                      self.locs[:, 1],
                      self.feat_array_temp[c][i, :],
                      function='cubic',
                      smooth=0)
             temp_interp.append(ip(self.grid_x, self.grid_y))
     return temp_interp
Esempio n. 16
0
    def load_mesh_psi_3D(self):
        """load R-Z mesh and psi values, then create map between each psi 
           value and the series of points on that surface, calculate the arc length table.
        """
        # open the file
        mesh = h5.File(self.mesh_file, 'r')
        RZ = mesh['coordinates']['values']
        Rpts = RZ[:, 0]

        Zpts = RZ[:, 1]

        psi = mesh['psi']
        # psi interpolant
        fill_ = np.nan
        if not self.lim:
            fill_ = max(psi)
        if self.kind == 'linear':
            self.psi_interp = LinearNDInterpolator(np.array([Rpts, Zpts]).T,
                                                   psi,
                                                   fill_value=fill_)
        elif self.kind == 'cubic':
            self.psi_interp = CloughTocher2DInterpolator(np.array([Rpts,
                                                                   Zpts]).T,
                                                         psi,
                                                         fill_value=fill_)
        else:
            raise NameError("The method '{}' is not defined".format(self.kind))

        self.points = np.array([Rpts, Zpts]).transpose()

        if self.lim:
            # 0.99 and 1.01 are for increasing the window in order to take the last point
            # in it
            self.Rmin = np.max([self.Rmin, 0.999 * np.min(Rpts)])
            self.Rmax = np.min([self.Rmax, 1.001 * np.max(Rpts)])
            self.Zmin = np.max([self.Zmin, 0.999 * np.min(Zpts)])
            self.Zmax = np.min([self.Zmax, 1.001 * np.max(Zpts)])

        mesh.close()

        # get the number of toroidal planes from fluctuation data file
        fluc_file0 = self.xgc_path + 'xgc.3d.' + str(
            self.time_steps[0]).zfill(5) + '.h5'
        fmesh = h5.File(fluc_file0, 'r')
        self.n_plane = fmesh['dpot'].shape[1]

        fmesh.close()
Esempio n. 17
0
    def _create_surface(self, z=None):
        """
        Generate irap surface

        :param z: replace z values of surface
        """
        nx = self._surface.nx
        ny = self._surface.ny
        x = self._surface.x
        y = self._surface.y
        if z is None:
            z = self._surface.z

        xstart = np.min(x)
        ystart = np.min(y)

        if nx < 2 or ny < 2:
            raise RuntimeError("Cannot create IRAP surface if nx or ny is <2")

        xinc = (np.max(x) - xstart) / (nx - 1)
        yinc = (np.max(y) - ystart) / (ny - 1)

        surf = Surface(nx=nx,
                       ny=ny,
                       xinc=xinc,
                       yinc=yinc,
                       xstart=xstart,
                       ystart=ystart,
                       angle=0)

        irap_x = np.empty(nx * ny)
        irap_y = np.array(irap_x)

        for i in range(len(surf)):
            irap_x[i], irap_y[i] = surf.getXY(i)

        # Interpolate vel grid to irap grid, should be the same apart from ordering
        z = np.nan_to_num(z)
        ip = CloughTocher2DInterpolator((x, y), z, fill_value=0)
        irap_z = ip(irap_x, irap_y)

        for i in range(len(surf)):
            surf[i] = irap_z[i]

        return surf
Esempio n. 18
0
def _grid_interp(*, image, bad_msk):
    """
    interpolate the bad pixels in an image

    Parameters
    ----------
    image: array
        the pixel data
    bad_msk: array
        boolean array, True means it is a bad pixel
    """

    nrows, ncols = image.shape
    npix = bad_msk.size

    nbad = bad_msk.sum()
    bm_frac = nbad / npix

    if bm_frac < 0.90:

        bad_pix, good_pix, good_im, good_ind = \
            _get_nearby_good_pixels(image, bad_msk, nbad)

        # extract unique ones
        gi, ind = np.unique(good_ind, return_index=True)

        good_pix = good_pix[ind, :]
        good_im = good_im[ind]

        img_interp = CloughTocher2DInterpolator(
            good_pix,
            good_im,
            fill_value=0.0,
        )
        interp_image = image.copy()
        interp_image[bad_msk] = img_interp(bad_pix)

        return interp_image

    else:
        return None
Esempio n. 19
0
    def fit(self, x, y):
        """Fit function ``y = f(x)`` to data.

        This fits a scalar function defined on 2-D data to the provided x-y
        pairs. The 2-D *x* coordinates do not have to lie on a regular grid,
        and can be in any order.

        Parameters
        ----------
        x : array-like, shape (2, N)
            Known input values as a 2-D numpy array, or sequence
        y : array-like, shape (N,)
            Known output values as a 1-D numpy array, or sequence

        Returns
        -------
        self : :class:`Delaunay2DScatterFit` object
            Reference to self, to allow chaining of method calls

        """
        # Check dimensions of known data
        x = np.atleast_2d(np.asarray(x))
        y = np.atleast_1d(np.asarray(y))
        if (len(x.shape) != 2) or (x.shape[0] != 2) or \
           (len(y.shape) != 1) or (y.shape[0] != x.shape[1]):
            raise ValueError("Delaunay interpolator requires input data with "
                             "shape (2, N) and output data with shape (N,), "
                             "got %s and %s instead" % (x.shape, y.shape))
        if self.jitter:
            x = x + 0.00001 * x.std(axis=1)[:, np.newaxis] * \
                np.random.standard_normal(x.shape)
        if self.interp_type == 'cubic':
            self._interp = CloughTocher2DInterpolator(
                x.T, y, fill_value=self.default_val)
        elif self.interp_type == 'linear':
            self._interp = LinearNDInterpolator(x.T,
                                                y,
                                                fill_value=self.default_val)
        else:
            self._interp = NearestNDInterpolator(x.T, y)
        return self
Esempio n. 20
0
    def __init__(self,xy=None,z=None,method='CloughTocher',filename=None):

        self.suit = OrderedDict()
        self.suit['Ni']     = np.array( 0,     dtype=np.int32)
        self.suit['L']      = np.array( 0,     dtype=np.double)
        self.suit['N_SET']  = np.array( 1,     dtype=np.int32)
        self.suit['N_MODE'] = np.array( 1,     dtype=np.int32)
        self.suit['s2b']    = np.array( [0]*7, dtype=np.int32)

        if filename is not None:
            self.load(filename)
            u = np.linspace(-1,1,self.suit['Ni'])*self.suit['L']*0.5
            x,y = np.meshgrid(u,u)
            xy = np.vstack([x.flatten(),y.flatten()]).T
            z = self.suit['M'].reshape(-1,self.suit['Ni']**2).T

        if xy is not None:

            assert xy.shape[0]==z.shape[0], "The first dimension of the first and second arguments must be the same!"

            m = np.isnan(z[:,0])
            if any(m):
                m = ~m
                xy = xy[m]
                z = z[m]

            #print("Setting up interpolant: ")
            self.datatri   = Delaunay(xy)
            self.__ct_itpr__ = []
            self.__near_itpr__ = []
            self.nz = z.shape[1]
            self.suit['N_MODE'] = np.array( self.nz,     dtype=np.int32)
            self.__near_itpr__ = NearestNDInterpolator(self.datatri,z)
            if method=='CloughTocher':
                self.__ct_itpr__   = CloughTocher2DInterpolator(self.datatri,z)
            elif method=='Linear':
                self.__ct_itpr__   = LinearNDInterpolator(self.datatri,z)
            else:
                self.__ct_itpr__ = self.__near_itpr__
Esempio n. 21
0
def interp_image_nocheck(image, bad_msk):
    """
    interpolate the bad pixels in an image with no checking on the fraction of
    masked pixels

    Parameters
    ----------
    image: array
        the pixel data
    bad_msk: array
        boolean array, True means it is a bad pixel

    Returns
    -------
    interp_image: ndarray
        The interpolated image
    """

    nbad = bad_msk.sum()

    bad_pix, good_pix, good_im, good_ind = \
        _get_nearby_good_pixels(image, bad_msk, nbad)

    # extract unique ones
    gi, ind = np.unique(good_ind, return_index=True)

    good_pix = good_pix[ind, :]
    good_im = good_im[ind]

    img_interp = CloughTocher2DInterpolator(
        good_pix,
        good_im,
        fill_value=0.0,
    )
    interp_image = image.copy()
    interp_image[bad_msk] = img_interp(bad_pix)

    return interp_image
Esempio n. 22
0
 def compute_interpolant(self):
     """ Compute the interpolant for the ion and electron
         density
     """
     # list of interpolant
     self.interpfluc = []
     for j in range(len(self.planes)):
         # computation of interpolant
         if self.kind == 'linear':
             self.interpfluc.append(
                 LinearNDInterpolator(
                     self.points,
                     np.array([self.phi[j, :], self.nane[j, :]]).T,
                     fill_value=np.nan))
         elif self.kind == 'cubic':
             self.interpfluc.append(
                 CloughTocher2DInterpolator(
                     self.points,
                     np.array([self.phi[j, :], self.nane[j, :]]).T,
                     fill_value=np.nan))
         else:
             raise NameError("The method '{}' is not defined".format(
                 self.kind))
Esempio n. 23
0
def map2ddata(xy, vr, xyi, radius=5000.0, maptype='idw'):
    # stats=sts.describe(vr)
    # statsstd=sts.tstd(vr)
    if maptype == 'idw':
        vri = idw(xy, vr, xyi)
    elif maptype == 'nearest':
        vri = griddata(xy, vr, (xyi[:, 0], xyi[:, 1]), method='nearest')
    elif maptype == 'linear':
        #                vri=griddata(xy,vr,(xyifhull[:,0],xyifhull[:,1]),method='linear')
        vri = griddata(xy, vr, (xyi[:, 0], xyi[:, 1]), method='linear')
    elif maptype == 'cubic':
        vri = griddata(xy, vr, (xyi[:, 0], xyi[:, 1]), method='cubic')
    elif maptype == 'rbf':
        rbf = Rbf(xy[:, 0], xy[:, 1], vr)
        vri = rbf(xyi[:, 0], xyi[:, 1])
    # elif maptype =='avgmap':
    #     vri=dataavgmap(xy,vr,xyi,radius)
    elif maptype == 'triang':
        linearnd = LinearNDInterpolator(xy, vr, stats[2])
        vri = linearnd(xyi)
    elif maptype == 'ct':
        ct = CloughTocher2DInterpolator(xy, vr, stats[2])
        vri = ct(xyi)
    return vri
Esempio n. 24
0
def correct_images(fnames, dxmodel, dymodel, suffix):
    """
    Read a list of fits image, and apply pixel-by-pixel corrections based on the
    given x/y models. Interpolate back to a regular grid, and then write an
    output file
    :param fname: input fits file
    :param dxodel: x model
    :param dymodel: x model
    :param fout: output fits file
    :return: None
    """
    # Get co-ordinate system from first image
    im = fits.open(fnames[0])
    data = np.squeeze(im[0].data)
    # create a map of (x,y) pixel pairs as a list of tuples
    xy = np.indices(data.shape, dtype=np.float32)
    xy.shape = (2, xy.shape[1] * xy.shape[2])

    x = np.array(xy[1, :])
    y = np.array(xy[0, :])

    mem = int(psutil.virtual_memory().available * 0.75)
    print("Detected memory ~{0}GB".format(mem / 2**30))
    # 32-bit floats, bit to byte conversion, MB conversion
    print("Image is {0}MB".format(data.shape[0] * data.shape[1] * 32 /
                                  (8 * 2**20)))
    pixmem = 40000
    print("Allowing {0}kB per pixel".format(pixmem / 2**10))
    stride = mem / pixmem
    # Make sure it is row-divisible
    stride = (stride // data.shape[0]) * data.shape[0]

    # calculate the corrections in blocks of 100k since the rbf fails on large blocks
    print('Applying corrections to pixel co-ordinates', end='')
    # remember the largest offset
    maxx = maxy = 0
    if len(x) > stride:
        print(" {0} rows at a time".format(stride // data.shape[0]))
        n = 0
        borders = range(0, len(x) + 1, stride)
        if borders[-1] != len(x):
            borders.append(len(x))
        for s1 in [slice(a, b) for a, b in zip(borders[:-1], borders[1:])]:
            off = dxmodel(x[s1], y[s1])
            maxx = max(np.max(np.abs(off)), maxx)
            x[s1] += off
            # the x coords were just changed so we need to refer back to the original coords
            off = dymodel(xy[1, :][s1], y[s1])
            maxy = max(np.max(np.abs(off)), maxy)
            y[s1] += off
            n += 1
            sys.stdout.write("{0:3.0f}%...".format(100 * n / len(borders)))
            sys.stdout.flush()
        print("")
    else:
        print('all at once')
        x += dxmodel(x, y)
        y += dymodel(xy[1, :], y)


# Note that a potential speed-up would be to nest the file loop inside the
# model calculation loop, so you don't have to calculate the model so many times
# However this would require either:
# 1) holding all the files in memory; but the model calculation is done in a loop
#    in order to reduce memory use, so this would be counter-productive; or:
# 2) writing out partly-complete FITS files and then reading them back in again,
#    which is a bit messy and so has not yet been implemented.
# Fancy splines need about this amount of buffer in order to interpolate the data
# if = 5, differences ~ 10^-6 Jy/beam ; if = 15, differences ~ 10^-9; if = 25, 0
    extra_lines = 25
    #    extra_lines = int(max(maxx, maxy)) + 1
    # Testing shows that this stage is much less memory intensive, and we can do more
    # rows per cycle. However there is very little speed-up from processing with fewer
    # rows, so if needed this number can be decreased by factors of 1-10 with no ill
    # effect on processing time.
    stride *= 40
    for fname in fnames:
        fout = fname.replace(".fits", "_" + suffix + ".fits")
        im = fits.open(fname)
        im.writeto(fout, overwrite=True, output_verify='fix+warn')
        oldshape = im[0].data.shape
        data = np.squeeze(im[0].data)
        # Replace NaNs with zeroes because otherwise it breaks the interpolation
        nandices = np.isnan(data)
        data[nandices] = 0.0
        squeezedshape = data.shape
        print('interpolating {0}'.format(fname))
        # Note that we need a fresh copy of the data because otherwise we will be trying to
        # interpolate over the results of our interpolation
        newdata = np.copy(data)
        print("Remapping data", end='')
        if len(x) > stride:
            print("{0} rows at a time".format(stride // data.shape[0]))
            n = 0
            borders = range(0, len(x) + 1, stride)
            if borders[-1] != len(x):
                borders.append(len(x))
            for a, b in zip(borders, borders[1:]):
                # indexes into the image based on the index into the raveled data
                idx = np.unravel_index(range(a, b), data.shape)
                # model using an extra few lines to avoid edge effects
                bp = min(b + data.shape[0] * extra_lines, len(x))
                # also go backwards by a few lines
                ap = max(0, a - data.shape[0] * extra_lines)
                idxp = np.unravel_index(range(ap, bp), data.shape)
                model = CloughTocher2DInterpolator(np.transpose(
                    [x[ap:bp], y[ap:bp]]),
                                                   np.ravel(data[idxp]),
                                                   fill_value=-1)
                # evaluate the model over this range
                newdata[idx] = model(xy[1, a:b], xy[0, a:b])
                n += 1
                sys.stdout.write("{0:3.0f}%...".format(100 * n / len(borders)))
                sys.stdout.flush()
            print("")
        else:
            print('all at once')
            model = CloughTocher2DInterpolator(np.transpose([x, y]),
                                               np.ravel(data))
            data = model(xy[1, :], xy[0, :])
            # print(data.shape)
        # Float32 instead of Float64 since the precision is meaningless
        print("int64 -> int32")
        data = np.float32(newdata.reshape(squeezedshape))
        # NaN the edges by 10 pixels to avoid weird edge effects
        print("blanking edges")
        data[0:10, :] = np.nan
        data[:, 0:10] = np.nan
        data[:, -10:data.shape[0]] = np.nan
        data[-10:data.shape[1], :] = np.nan
        # Re-apply any previous NaN mask to the data
        data[nandices] = np.nan
        im[0].data = data.reshape(oldshape)
        print("saving...")
        im.writeto(fout, overwrite=True, output_verify='fix+warn')
        print("wrote {0}".format(fout))
        # Explicitly delete potential memory hogs
        del im, data
    return
Esempio n. 25
0
def fesom2regular(
    data,
    mesh,
    lons,
    lats,
    distances_path=None,
    inds_path=None,
    qhull_path=None,
    how="nn",
    k=5,
    radius_of_influence=100000,
    n_jobs=2,
    dumpfile=True,
    basepath=None,
):
    """
    Interpolates data from FESOM mesh to target (usually regular) mesh.

    Parameters
    ----------
    data : array
        1d array that represents FESOM data at one
    mesh : fesom_mesh object
        pyfesom mesh representation
    lons/lats : array
        2d arrays with target grid values.
    distances_path : string
        Path to the file with distances. If not provided and dumpfile=True, it will be created.
    inds_path : string
        Path to the file with inds. If not provided and dumpfile=True, it will be created.
    qhull_path : str
         Path to the file with qhull (needed for linear and cubic interpolations). If not provided and dumpfile=True, it will be created.
    how : str
       Interpolation method. Options are 'nn' (nearest neighbor), 'idist' (inverce distance), "linear" and "cubic".
    k : int
        k-th nearest neighbors to use. Only used when how==idist
    radius_of_influence : int
        Cut off distance in meters, only used in nn and idist.
    n_jobs : int, optional
        Number of jobs to schedule for parallel processing. If -1 is given
        all processors are used. Default: 1. Only used for nn and idist.
    dumpfile: bool
        wether to dump resulted distances and inds to the file.
    basepath: str
        path where to store additional interpolation files. If None (default),
        the path of the mesh will be used.

    Returns
    -------
    data_interpolated : 2d array
        array with data interpolated to the target grid.

    """

    left, right, down, up = np.min(lons), np.max(lons), np.min(lats), np.max(
        lats)
    lonNumber, latNumber = lons.shape[1], lats.shape[0]

    if how == "nn":
        kk = 1
    else:
        kk = k

    if not basepath:
        basepath = mesh.path

    if (distances_path is None) and (inds_path is None):
        distances_file = "distances_{}_{}_{}_{}_{}_{}_{}_{}".format(
            mesh.n2d, left, right, down, up, lonNumber, latNumber, kk)
        inds_file = "inds_{}_{}_{}_{}_{}_{}_{}_{}".format(
            mesh.n2d, left, right, down, up, lonNumber, latNumber, kk)

        distances_path = os.path.join(basepath, distances_file)
        inds_path = os.path.join(basepath, inds_file)

    if qhull_path is None:
        qhull_file = "qhull_{}".format(mesh.n2d)
        qhull_path = os.path.join(basepath, qhull_file)
    # print distances

    if how == "nn":
        if os.path.isfile(distances_path) and os.path.isfile(distances_path):
            logging.info("Note: using precalculated file from {}".format(
                distances_path))
            logging.info(
                "Note: using precalculated file from {}".format(inds_path))
            distances = joblib.load(distances_path)
            inds = joblib.load(inds_path)
        else:
            distances, inds = create_indexes_and_distances(mesh,
                                                           lons,
                                                           lats,
                                                           k=kk,
                                                           n_jobs=n_jobs)
            if dumpfile:
                joblib.dump(distances, distances_path)
                joblib.dump(inds, inds_path)

        data_interpolated = data[inds]
        data_interpolated[distances >= radius_of_influence] = np.nan
        data_interpolated = data_interpolated.reshape(lons.shape)
        data_interpolated = np.ma.masked_invalid(data_interpolated)
        return data_interpolated

    elif how == "idist":
        if os.path.isfile(distances_path) and os.path.isfile(distances_path):
            logging.info("Note: using precalculated file from {}".format(
                distances_path))
            logging.info(
                "Note: using precalculated file from {}".format(inds_path))
            distances = joblib.load(distances_path)
            inds = joblib.load(inds_path)
        else:
            distances, inds = create_indexes_and_distances(mesh,
                                                           lons,
                                                           lats,
                                                           k=kk,
                                                           n_jobs=n_jobs)
            if dumpfile:
                joblib.dump(distances, distances_path)
                joblib.dump(inds, inds_path)

        distances_ma = np.ma.masked_greater(distances, radius_of_influence)

        w = 1.0 / distances_ma**2
        data_interpolated = np.ma.sum(w * data[inds], axis=1) / np.ma.sum(
            w, axis=1)
        data_interpolated.shape = lons.shape
        data_interpolated = np.ma.masked_invalid(data_interpolated)
        return data_interpolated

    elif how == "linear":
        if os.path.isfile(qhull_path):
            logging.info(
                "Note: using precalculated file from {}".format(qhull_path))
            qh = joblib.load(qhull_path)
        else:
            points = np.vstack((mesh.x2, mesh.y2)).T
            qh = qhull.Delaunay(points)
            if dumpfile:
                joblib.dump(qh, qhull_path)
        data_interpolated = LinearNDInterpolator(qh, data)((lons, lats))
        data_interpolated = np.ma.masked_invalid(data_interpolated)
        return data_interpolated

    elif how == "cubic":
        if os.path.isfile(qhull_path):
            logging.info(
                "Note: using precalculated file from {}".format(qhull_path))
            qh = joblib.load(qhull_path)
        else:
            points = np.vstack((mesh.x2, mesh.y2)).T
            qh = qhull.Delaunay(points)
            if dumpfile:
                joblib.dump(qh, qhull_path)
        data_interpolated = CloughTocher2DInterpolator(qh, data)((lons, lats))
        data_interpolated = np.ma.masked_invalid(data_interpolated)
        return data_interpolated
    else:
        raise ValueError("Interpolation method is not supported")
Esempio n. 26
0
    def __getMean3D_CloughTocher(self, dx, dy, mask=None, clip=False, force=False):
        
        # Get the discretization
        if (dx is None):
            tmp = self.points.bounds[1]-self.points.bounds[0]
            dx = 0.01 * tmp
        assert dx > 0.0, "dx must be positive!"
        
        # Get the discretization
        if (dy is None):
            tmp = self.points.bounds[3]-self.points.bounds[2]
            dy = 0.01 * tmp
        assert dy > 0.0, "dy must be positive!"
        
        tmp = np.column_stack((self.points.x, self.points.y))

        # Get the points to interpolate to
        x,y,intPoints = interpolation.getGridLocations2D(self.points.bounds, dx, dy)

        # Create a distance mask
        if mask:
            self.points.setKdTree(nDims=2) # Set the KdTree on the data points
            g = np.meshgrid(x,y)
            xi = _ndim_coords_from_arrays(tuple(g), ndim=tmp.shape[1])
            dists, indexes = self.points.kdtree.query(xi)
            iMask = np.where(dists > mask)

        # Get the value bounds
        minV = np.nanmin(self.mean)
        maxV = np.nanmax(self.mean)

        # Initialize 3D volume
        mean3D = StatArray(np.zeros([self.zGrid.size, y.size, x.size], order = 'F'),name = 'Conductivity', units = '$Sm^{-1}$')

        # Triangulate the data locations
        dTri = Delaunay(tmp)

        # Interpolate for each depth
        print('Interpolating using clough tocher')
        Bar=progressbar.ProgressBar()
        for i in Bar(range(self.zGrid.size)):
            # Get the model values for the current depth
            vals1D = self.mean[i,:]
            # Create the interpolant
            f=CloughTocher2DInterpolator(dTri,vals1D)
            # Interpolate to the grid
            vals = f(intPoints)
            # Reshape to a 2D array
            vals = vals.reshape(y.size,x.size)

            # clip values to the observed values
            if (clip):
                vals.clip(minV, maxV)

            # Mask based on distance
            if (mask):
                vals[iMask] = np.nan

            # Add values to the 3D array
            mean3D[i,:,:] = vals
        self.mean3D = mean3D #.reshape(self.zGrid.size*y.size*x.size)
Esempio n. 27
0
def showfile(ifile, variable, depth, meshpath, box, res, influence, timestep,
             levels, quiet, ofile, mapproj, abg, clim, cmap, interp, ptype, k):
    '''
    meshpath - Path to the folder with FESOM1.4 mesh files.

    ifile    - Path to FESOM1.4 netCDF file.

    variable - The netCDF variable to be plotted.
    '''
    if not quiet:
        click.secho('Mesh: {}'.format(meshpath))
        click.secho('File: {}'.format(ifile))
        click.secho('Variable: {}'.format(variable), fg='red')
        click.secho('Depth: {}'.format(depth), fg='red')
        click.secho('BOX: {}'.format(box))
        click.secho('Resolution: {}'.format(res))
        click.secho('Influence raduis: {} meters'.format(influence), fg='red')
        click.secho('Timestep: {}'.format(timestep))
        if levels:
            click.secho('Levels: {}'.format(levels), fg='red')
        else:
            click.secho('Levels: auto', fg='red')

    if cmap:
        if cmap in cmo.cmapnames:
            colormap = cmo.cmap_d[cmap]
        elif cmap in plt.cm.datad:
            colormap = plt.get_cmap(cmap)
        else:
            raise ValueError(
                'Get unrecognised name for the colormap `{}`. Colormaps should be from standard matplotlib set of from cmocean package.'
                .format(cmap))
    else:
        if clim:
            colormap = cmo.cmap_d['balance']
        else:
            colormap = plt.get_cmap('Spectral_r')

    sstep = timestep
    radius_of_influence = influence

    left, right, down, up = box
    lonNumber, latNumber = res

    mesh = pf.load_mesh(meshpath, abg=abg, usepickle=False, usejoblib=True)
    flf = Dataset(ifile)
    lonreg = np.linspace(left, right, lonNumber)
    latreg = np.linspace(down, up, latNumber)
    lonreg2, latreg2 = np.meshgrid(lonreg, latreg)

    dind = (abs(mesh.zlevs - depth)).argmin()
    realdepth = mesh.zlevs[dind]

    level_data, nnn = pf.get_data(flf.variables[variable][sstep], mesh,
                                  realdepth)
    if interp == 'nn':
        ofesom = pf.fesom2regular(level_data,
                                  mesh,
                                  lonreg2,
                                  latreg2,
                                  radius_of_influence=radius_of_influence)
    elif interp == 'idist':
        ofesom = pf.fesom2regular(level_data,
                                  mesh,
                                  lonreg2,
                                  latreg2,
                                  radius_of_influence=radius_of_influence,
                                  how='idist',
                                  k=k)
    elif interp == 'linear':
        points = np.vstack((mesh.x2, mesh.y2)).T
        qh = qhull.Delaunay(points)
        ofesom = LinearNDInterpolator(qh, level_data)((lonreg2, latreg2))

    elif interp == 'cubic':
        points = np.vstack((mesh.x2, mesh.y2)).T
        qh = qhull.Delaunay(points)
        ofesom = CloughTocher2DInterpolator(qh, level_data)((lonreg2, latreg2))

    if clim:
        if variable == 'temp':
            climvar = 'T'
        elif variable == 'salt':
            climvar = 'S'
        else:
            raise ValueError(
                'You have selected --clim/-c option, but variable `{}` is not in climatology. Acceptable values are `temp` and `salt` only.'
                .format(variable))
        #os.path.join(os.path.dirname(__file__), "../")
        pathToClim = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                  "../data/")
        print(pathToClim)
        w = pf.climatology(pathToClim, clim)
        xx, yy, oclim = pf.clim2regular(
            w,
            climvar,
            lonreg2,
            latreg2,
            levels=[realdepth],
            radius_of_influence=radius_of_influence)
        oclim = oclim[0, :, :]
        data = ofesom - oclim
    else:
        data = ofesom

    if mapproj == 'merc':
        ax = plt.subplot(111, projection=ccrs.Mercator())
    elif mapproj == 'pc':
        ax = plt.subplot(111, projection=ccrs.PlateCarree())
    elif mapproj == 'np':
        ax = plt.subplot(111, projection=ccrs.NorthPolarStereo())
    elif mapproj == 'sp':
        ax = plt.subplot(111, projection=ccrs.SouthPolarStereo())
    elif mapproj == 'rob':
        ax = plt.subplot(111, projection=ccrs.Robinson())

    ax.set_extent([left, right, down, up], crs=ccrs.PlateCarree())

    if levels:
        mmin, mmax, nnum = levels
        nnum = int(nnum)
    else:
        mmin = np.nanmin(data)
        mmax = np.nanmax(data)
        nnum = 40

    data_levels = np.linspace(mmin, mmax, nnum)
    if ptype == 'cf':
        mm = ax.contourf(lonreg,\
                     latreg,\
                     data,
                     levels = data_levels,
                     transform=ccrs.PlateCarree(),
                     cmap=colormap,
                    extend='both')
    elif ptype == 'pcm':
        data_cyc, lon_cyc = add_cyclic_point(data, coord=lonreg)
        mm = ax.pcolormesh(lon_cyc,\
                         latreg,\
                         data_cyc,
                         vmin = mmin,
                         vmax = mmax,
                         transform=ccrs.PlateCarree(),
                         cmap=colormap,
                        )
    else:
        raise ValueError('Inknown plot type {}'.format(ptype))

    ax.coastlines(resolution='50m', lw=0.5)
    ax.add_feature(
        cfeature.GSHHSFeature(levels=[1], scale='low', facecolor='lightgray'))
    cb = plt.colorbar(mm, orientation='horizontal', pad=0.03)
    cb.set_label(flf.variables[variable].units)
    plt.title('{} at {}m.'.format(variable, realdepth))
    plt.tight_layout()
    if ofile:
        plt.savefig(ofile, dpi=100)
    else:
        plt.show()
Esempio n. 28
0
def gen_images(locs,
               features,
               n_gridpoints,
               normalize=True,
               augment=False,
               pca=False,
               std_mult=0.1,
               n_components=2,
               edgeless=False,
               multip=False,
               method='RBF'):
    """
    Generates EEG images given electrode locations in 2D space and multiple feature values for each electrode

    :param locs: An array with shape [n_electrodes, 2] containing X, Y
                        coordinates for each electrode.
    :param features: Feature matrix as [n_samples, n_features]
                                Features are as columns.
                                Features corresponding to each frequency band are concatenated.
                                (alpha1, alpha2, ..., beta1, beta2,...)
    :param n_gridpoints: Number of pixels in the output images
    :param normalize:   Flag for whether to normalize each band over all samples
    :param augment:     Flag for generating augmented images
    :param pca:         Flag for PCA based data augmentation
    :param std_mult     Multiplier for std of added noise
    :param n_components: Number of components in PCA to retain for augmentation
    :param edgeless:    If True generates edgeless images by adding artificial channels
                        at four corners of the image with value = 0 (default=False).
    :return:            Tensor of size [samples, colors, W, H] containing generated
                        images.
    """
    feat_array_temp = []
    nElectrodes = locs.shape[0]  # Number of electrodes
    # Test whether the feature vector length is divisible by number of electrodes
    assert features.shape[1] == nElectrodes
    n_colors = features.shape[2]
    feat_array_temp = np.moveaxis(np.asarray(features), -1, 0)
    if augment:
        if pca:
            for c in range(n_colors):
                feat_array_temp[c] = augment_EEG(feat_array_temp[c],
                                                 std_mult,
                                                 pca=True,
                                                 n_components=n_components)
        else:
            for c in range(n_colors):
                feat_array_temp[c] = augment_EEG(feat_array_temp[c],
                                                 std_mult,
                                                 pca=False,
                                                 n_components=n_components)
    nSamples = features.shape[0]
    # Interpolate the values
    grid_x, grid_y = np.mgrid[min(locs[:, 0]):max(locs[:,
                                                       0]):n_gridpoints * 1j,
                              min(locs[:, 1]):max(locs[:,
                                                       1]):n_gridpoints * 1j]
    temp_interp = []
    for c in range(n_colors):
        temp_interp.append(np.zeros([nSamples, n_gridpoints, n_gridpoints]))
    # Generate edgeless images
    if edgeless:
        min_x, min_y = np.min(locs, axis=0)
        max_x, max_y = np.max(locs, axis=0)
        locs = np.append(locs,
                         np.array([[min_x, min_y], [min_x, max_y],
                                   [max_x, min_y], [max_x, max_y]]),
                         axis=0)
        feat_array_stack = []
        for c in range(n_colors):
            feat_array_stack.append(
                np.hstack((feat_array_temp[c], np.zeros((nSamples, 4)))))
        feat_array_temp = np.asarray(feat_array_stack)

    # Interpolating
    if multip:
        try:
            pool = Pool(8)  # on 8 processors
            engine = gen_images_engine(nSamples, n_colors, locs,
                                       feat_array_temp, grid_x, grid_y, method)
            temp_interp = pool.map(engine, range(n_colors))
        finally:  # To make sure processes are closed in the end, even if errors happen
            pool.close()
            pool.join()
    else:
        for i in range(nSamples):
            for c in range(n_colors):
                # print('Interpolating {0}/{1}\r'.format(i+1, nSamples))
                if method == 'CT2D':
                    ip = CloughTocher2DInterpolator(locs,
                                                    feat_array_temp[c][i, :],
                                                    fill_value=np.nan)
                    temp_interp[c][i, :, :] = ip((grid_x, grid_y))
                elif method == 'RBF':
                    ip = Rbf(locs[:, 0],
                             locs[:, 1],
                             feat_array_temp[c][i, :],
                             function='cubic',
                             smooth=0)
                    temp_interp[c][i, :, :] = ip(grid_x, grid_y)
    print('Interpolating...\r')
    # Normalizing
    for c in range(n_colors):
        if normalize:
            temp_norm = np.reshape(temp_interp[c], (-1))
            temp_norm[~np.isnan(temp_norm)] = \
                scale(temp_norm[~np.isnan(temp_norm)], with_mean=False)
            temp_interp[c] = np.reshape(temp_norm,
                                        np.asarray(temp_interp[c]).shape)
        temp_interp[c] = np.nan_to_num(temp_interp[c])
    return np.swapaxes(np.asarray(temp_interp), 0,
                       1)  # swap axes to have [samples, colors, W, H]
Esempio n. 29
0
def elc_stagger(loggs, teffs, fehs):
    """ Find the equivalent limb-darkening coefficients for a given teff and 
    logg and their uncertainties. Code by Tim White.
    
    Parameters
    ----------
    loggs: float
        Surface gravities, log(g), in cgs units.
    teff: float
        Effective temperatures in K.    
    fehs: float
        Metallicities [Fe/H] relative to Solar.
    
    Returns
    ----------
    elcs: float array
        Equivalent linear coefficients, one per wavelength.
        
    scls: float array
        LDD scaling parameters to be used with the equivalent linear
        coefficents, one per wavelength channel.
        
    ftcs: float array
        Four term limb darkening coefficients, of shape 4 x N wavelengths.
    """
    grid1 = read_magic('data/stagger_pionier_m00.tab')
    grid2 = read_magic('data/stagger_pionier_m10.tab')
    ks = [0.5, 1.0, 1.5, 2.0]

    # Triangulate the grids
    loggs1 = []
    teffs1 = []
    for d in grid1:
        logg = d['logg']
        teff = d['teff']
        loggs1.append(logg)
        teffs1.append(teff)

    loggs2 = []
    teffs2 = []
    for d in grid2:
        logg = d['logg']
        teff = d['teff']  #
        loggs2.append(logg)
        teffs2.append(teff)

    points2D = np.vstack([np.log10(teffs1), np.log10(loggs1)]).T
    tri1 = Delaunay(points2D)

    points2D = np.vstack([np.log10(teffs2), np.log10(loggs2)]).T
    tri2 = Delaunay(points2D)

    #Values to be interpolated to
    t1 = np.log10(teffs)
    l1 = np.log10(loggs)
    f1 = fehs

    # Define wavelength channels
    channels = list(d)  #.keys()
    channels.remove('teff')
    channels.remove('sigteff')
    channels.remove('logg')
    channels.remove('feh')
    channels.sort()

    wls = []
    elcs = []
    scls = []
    ftcs = []

    # Loop over wavelength channels and perform interpolation
    for c in channels:
        a1s1 = []
        a2s1 = []
        a3s1 = []
        a4s1 = []
        for d in grid1:
            ch = d[c]
            a1s1.append(ch['a1'])
            a2s1.append(ch['a2'])
            a3s1.append(ch['a3'])
            a4s1.append(ch['a4'])

        resCTa11 = CloughTocher2DInterpolator(tri1, a1s1)
        resCTa21 = CloughTocher2DInterpolator(tri1, a2s1)
        resCTa31 = CloughTocher2DInterpolator(tri1, a3s1)
        resCTa41 = CloughTocher2DInterpolator(tri1, a4s1)

        a1s2 = []
        a2s2 = []
        a3s2 = []
        a4s2 = []
        for d in grid2:
            ch = d[c]
            a1s2.append(ch['a1'])
            a2s2.append(ch['a2'])
            a3s2.append(ch['a3'])
            a4s2.append(ch['a4'])

        resCTa12 = CloughTocher2DInterpolator(tri2, a1s2)
        resCTa22 = CloughTocher2DInterpolator(tri2, a2s2)
        resCTa32 = CloughTocher2DInterpolator(tri2, a3s2)
        resCTa42 = CloughTocher2DInterpolator(tri2, a4s2)

        csCT1 = [
            resCTa11(t1, l1),
            resCTa21(t1, l1),
            resCTa31(t1, l1),
            resCTa41(t1, l1)
        ]
        csCT2 = [
            resCTa12(t1, l1),
            resCTa22(t1, l1),
            resCTa32(t1, l1),
            resCTa42(t1, l1)
        ]
        csCT = np.asarray(csCT1) - f1 * (np.asarray(csCT2) - np.asarray(csCT1))

        clean = [x for x in np.asarray(csCT).T if (np.isnan(x[0]) == False)]
        wls.append(ch['wl'])
        csCT = np.asarray(clean).T
        elc, scl = get_elc(ks, csCT)

        ftcs.append(np.asarray(csCT))
        elcs.append(elc)
        scls.append(scl)

    #melcs = np.nanmean(elcs,axis=1)
    #sigelcs = np.nanstd(elcs,axis=1)
    #mscls = np.nanmean(scls,axis=1)
    #sigscls = np.nanstd(scls,axis=1)
    #mftcs = np.nanmean(ftcs,axis=2)
    #sigftcs = np.nanstd(ftcs,axis=2)
    #wl = np.asarray(wls)

    return np.asarray(elcs), np.array(scls), np.array(ftcs)
Esempio n. 30
0
    # interp2d - Runs dfitpack.regrid_smth (a fortran code in scipy), if on a rectangular grid.
    cubic_2d = interp2d(x_in, y_in, f_in, kind='cubic')
    f_cubic = cubic_2d(xsamples, ysamples)

    # RectBivariateSpline - Runs from dfitpack.regrid_smth (a fortran code in scipy).
    cubic_2da = RectBivariateSpline(x_in, y_in, f_in, kx=3, ky=3)
    f_cubica = cubic_2da(xsamples, ysamples)

    # CloughTocher2DInterpolator - an iterative piecewise interpolator method.
    x_flat = np.reshape(x_in_full, -1)
    y_flat = np.reshape(y_in_full, -1)
    f_inflat = np.reshape(f_in, -1)
    xy_flat = np.concatenate((x_flat[:, np.newaxis], y_flat[:, np.newaxis]),
                             axis=1)
    interp_clough_tocher = CloughTocher2DInterpolator(xy_flat, f_inflat)

    # griddata - a wrapper for CloughTocher2DInterpolator
    grid_z = griddata(xy_flat,
                      f_inflat, (xsamples_in_full, ysamples_in_full),
                      method='cubic')

    # It is not possible to get exactly the same as other methods for cubic interpolation.
    # The method of getting the coefficients is slightly different. Using a version to check for changes instead.
    interpolator2D_cubic_nearest = Interpolator2DArray(
        x_in,
        y_in,
        f_in,
        'cubic',
        'nearest',
        extrapolation_range_x=2.0,