Exemple #1
0
 def grid(self, ngrid=30):
     x1 = np.linspace(self.lim['x1'][0], self.lim['x1'][1], ngrid)
     x2 = np.linspace(self.lim['x2'][0], self.lim['x2'][1], ngrid)
     (x1, x2) = np.meshgrid(x1, x2)
     x1r = x1.reshape((-1, 1))
     x2r = x2.reshape((-1, 1))
     x = np.concatenate((x1r, x2r), 1)
     if self.type == 'fun':
         y = self.fun.eval(x)
     else:
         y = self.model.sample(x)
     y = y.reshape((ngrid, ngrid))
     return (x1, x2, y)
Exemple #2
0
 def grid(self, ngrid=30):
     x1 = np.linspace(self.lim['x1'][0], self.lim['x1'][1], ngrid)
     x2 = np.linspace(self.lim['x2'][0], self.lim['x2'][1], ngrid)
     (x1, x2) = np.meshgrid(x1, x2)
     x1r = x1.reshape((-1, 1));
     x2r = x2.reshape((-1, 1));
     x = np.concatenate((x1r, x2r), 1)
     if self.type == 'fun':
         y = self.fun.eval(x)
     else:
         y = self.model.sample(x)
     y = y.reshape((ngrid, ngrid))
     return (x1, x2, y)
def lhood_test():
    clist = [np.array([-3.5, 0]), np.array([3.5, 0])]
    wlist = [0.1, 0.1]
    rlist = [2.0, 2.0]
    lhood = GShellLhood2(clist[0], clist[1], rlist[0], rlist[1], wlist[0],
                         wlist[1])
    xgrid = np.arange(0.0, 1.0, 1 / 400)
    ygrid = np.arange(0.0, 1.0, 1 / 400)
    X, Y = matlib.meshgrid(xgrid, ygrid)
    logL = np.zeros(X.shape)
    for i in range(X.shape[0]):
        for j in range(X.shape[1]):
            theta = np.array([X[i, j], Y[i, j]])
            logL[i, j] = lhood.evaluate(theta)
    plt.pcolormesh(12 * X - 6, 12 * Y - 6, np.exp(logL))
    plt.colorbar()
    plt.xlabel('x1')
    plt.ylabel('x2')
    return X, Y, logL
Exemple #4
0
 def from_testcase(cls, tc, fclass=None):
     yres = _GRAPH_YRES_MLP*_GRAPH_YRES_BASIC + 1
     x = np.linspace(tc.lim['x1'][0], tc.lim['x1'][1], _GRAPH_XRES)
     y = np.linspace(tc.lim['x2'][0], tc.lim['x2'][1], yres)
     (x, y) = np.meshgrid(x, y)
     return Graph(x.T, y.T, fclass)
Exemple #5
0
 def from_testcase(cls, tc, fclass=None):
     yres = _GRAPH_YRES_MLP * _GRAPH_YRES_BASIC + 1
     x = np.linspace(tc.lim['x1'][0], tc.lim['x1'][1], _GRAPH_XRES)
     y = np.linspace(tc.lim['x2'][0], tc.lim['x2'][1], yres)
     (x, y) = np.meshgrid(x, y)
     return Graph(x.T, y.T, fclass)
Exemple #6
0
def covmat1d_from_cfun(xp1,Std1,cfun1,Cl1=None,cco1=0,mapfun1=None):        
    
    xp = copy.deepcopy(xp1)
    Std = copy.deepcopy(Std1)
    cfun = copy.deepcopy(cfun1)
    Cl = copy.deepcopy(Cl1)
    cco = copy.deepcopy(cco1)
    mapfun = copy.deepcopy(mapfun1)
    
    def handle_expand(x,xi):
        
        v1 = np.min( x );
        i1 = ( xi<v1 );
        
        v2 = np.max( x );
        i2 = ( xi>v2 );
        
        if np.sum(v1) > 0:
            xi[i1,] = v1;
          
        if np.sum(v2) > 0:
            xi[i2,] = v2;
        
        return xi
    
    # Determine standard deviations
    #
    n  = len( xp );
    #
    if np.isscalar(Std):
        si = npm.repmat( Std, n, 1 );
    else:
        f = spi.interp1d(Std[:,0], Std[:,1])
        si = f(handle_expand(Std[:,0],xp));
        si = np.array([si]).T
    
    # Handle diagonal matrices separately (note return)
    #
    if cfun.lower() == 'drc':
        S = np.zeros([n,n])
        for i in range(0,n):
            S[i,i] = si[i]**2 ;
        return S
    
    # Conversion of length unit
    #
    if mapfun != None:
        if not callable(mapfun):
            Exception('Input *mapfun* must be empty or a function handle.')
        xp = mapfun(xp)
        if not np.isscalar(Cl):
            Cl[:,0] = mapfun( Cl[:,0] );
    
    # Distance matrix
    #
    [X1, X2] = npm.meshgrid(xp, xp);
    D = np.abs(X1-X2)
    
    # Correlation length matrix
    #
    if np.isscalar(Cl):
        L = Cl;
    else:
        f = spi.interp1d(Cl[:,0], Cl[:,1])
        cl = f(handle_expand(handle_expand(Cl[:,0],xp)));
        [X1,X2] = npm.meshgrid( cl, cl );
        L       = ( X1 + X2 ) / 2;
    
    # Create correlation matrix
    #
    if cfun.lower() == 'lin':
        S = 1 - (1-np.exp(-1)) * ( D/L );
        # Negativa values removed by cco
    elif cfun.lower() == 'exp':
        S = np.exp(-D/L)
    elif cfun.lower() == 'gau':
        S = np.exp( -(D/L)**2 );
    else:
        Exception('Unknown correlation function: ' + cfun)
    
    # Remove values below correlation cut-off limit, convert to sparse and
    # include standard deviations
    #
    S[S < cco] = 0;
    S       = (si @ si.T) * S;

    return S
Exemple #7
0
    def generate_correlated_noise_fft(nx, ny, std_long, sp):
        """ A function to create synthetic turbulent troposphere delay using an FFT approach. 
        The power of the turbulence is tuned by the weather model at the longer wavelengths.
        
        Inputs:
            nx (int) -- width of troposphere 
            Ny (int) -- length of troposphere 
            std_long (float) -- standard deviation of the weather model at the longer wavelengths. Default = ?
            sp | int | pixel spacing in km
            
        Outputs:
            APS (float): 2D array, Ny * nx, units are m.
            
        History:
            2020_??_?? | LS | Adapted from code by Andy Hooper.  
            2021_03_01 | MEG | Small change to docs and inputs to work with SyInterferoPy
        """

        import numpy as np
        import numpy.matlib as npm
        import math

        np.seterr(divide='ignore')

        cut_off_freq = 1 / 50  # drop wavelengths above 50 km

        x = np.arange(0, int(nx / 2))  # positive frequencies only
        y = np.arange(0, int(ny / 2))  # positive frequencies only
        freq_x = np.divide(x, nx * sp)
        freq_y = np.divide(y, ny * sp)
        Y, X = npm.meshgrid(freq_x, freq_y)
        freq = np.sqrt((X * X + Y * Y) / 2)  # 2D positive frequencies

        log_power = np.log10(freq) * -11 / 3  # -11/3 in 2D gives -8/3 in 1D
        ix = np.where(freq < 2 / 3)
        log_power[ix] = np.log10(freq[ix]) * -8 / 3 - math.log10(
            2 / 3)  # change slope at 1.5 km (2/3 cycles per km)

        bin_power = np.power(10, log_power)
        ix = np.where(freq < cut_off_freq)
        bin_power[ix] = 0

        APS_power = np.zeros(
            (ny, nx))  # mirror positive frequencies into other quadrants
        APS_power[0:int(ny / 2), 0:int(nx / 2)] = bin_power
        # APS_power[0:int(ny/2), int(nx/2):nx]=npm.fliplr(bin_power)
        # APS_power[int(ny/2):ny, 0:int(nx/2)]=npm.flipud(bin_power)
        # APS_power[int(ny/2):ny, int(nx/2):nx]=npm.fliplr(npm.flipud(bin_power))
        APS_power[0:int(ny / 2), int(np.ceil(nx / 2)):] = npm.fliplr(bin_power)
        APS_power[int(np.ceil(ny / 2)):, 0:int(nx / 2)] = npm.flipud(bin_power)
        APS_power[int(np.ceil(ny / 2)):,
                  int(np.ceil(nx / 2)):] = npm.fliplr(npm.flipud(bin_power))
        APS_filt = np.sqrt(APS_power)

        x = np.random.randn(ny, nx)  # white noise
        y_tmp = np.fft.fft2(x)
        y_tmp2 = np.multiply(y_tmp, APS_filt)  # convolve with filter
        y = np.fft.ifft2(y_tmp2)
        APS = np.real(y)

        APS = APS / np.std(
            APS
        ) * std_long  #  adjust the turbulence by the weather model at the longer wavelengths.
        APS = APS * 0.01  # convert from cm to m
        return APS