Ejemplo n.º 1
0
def test_HStack(par):
    """Dot-test and inversion for HStack operator with numpy array as input
    """
    np.random.seed(0)
    G1 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype('float32')
    G2 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype('float32')
    x = np.ones(2 * par['nx']) + par['imag'] * np.ones(2 * par['nx'])

    Hop = HStack([G1, MatrixMult(G2, dtype=par['dtype'])], dtype=par['dtype'])
    assert dottest(Hop,
                   par['ny'],
                   2 * par['nx'],
                   complexflag=0 if par['imag'] == 0 else 3)

    xlsqr = lsqr(Hop, Hop * x, damp=1e-20, iter_lim=300, show=0)[0]
    assert_array_almost_equal(x, xlsqr, decimal=4)

    # use numpy matrix directly in the definition of the operator
    H1op = HStack([G1, MatrixMult(G2, dtype=par['dtype'])], dtype=par['dtype'])
    assert dottest(H1op,
                   par['ny'],
                   2 * par['nx'],
                   complexflag=0 if par['imag'] == 0 else 3)

    # use scipy matrix directly in the definition of the operator
    G1 = sp_random(par['ny'], par['nx'], density=0.4).astype('float32')
    H2op = HStack([G1, MatrixMult(G2, dtype=par['dtype'])], dtype=par['dtype'])
    assert dottest(H2op,
                   par['ny'],
                   2 * par['nx'],
                   complexflag=0 if par['imag'] == 0 else 3)
Ejemplo n.º 2
0
def focusing_wrapper(direct,toff,g0VS,iava,Rop,R1op,Restrop,t):
    nr=direct.shape[0]
    nsava=iava.shape[0]
    
    nt=t.shape[0]
    dt=t[1]-t[0]
    
    # window
    directVS_off = direct - toff
    idirectVS_off = np.round(directVS_off/dt).astype(np.int)
    w = np.zeros((nr, nt))
    wi = np.ones((nr, nt))
    for ir in range(nr-1):
        w[ir, :idirectVS_off[ir]]=1   
    wi = wi - w
         
    w = np.hstack((np.fliplr(w), w[:, 1:]))
    wi = np.hstack((np.fliplr(wi), wi[:, 1:]))
    
    # smoothing
    nsmooth=10
    if nsmooth>0:
        smooth=np.ones(nsmooth)/nsmooth
        w  = filtfilt(smooth, 1, w)
        wi  = filtfilt(smooth, 1, wi)
        
    # Input focusing function
    fd_plus =  np.concatenate((np.fliplr(g0VS.T), np.zeros((nr, nt-1))), axis=-1)
    
    # operators
    Wop = Diagonal(w.flatten())
    WSop = Diagonal(w[iava].flatten())
    WiSop = Diagonal(wi[iava].flatten())
    
    Mop = VStack([HStack([Restrop, -1*WSop*Rop]),
                   HStack([-1*WSop*R1op, Restrop])])*BlockDiag([Wop, Wop])
    
    Gop = VStack([HStack([Restrop, -1*Rop]),
                   HStack([-1*R1op, Restrop])])
    
    p0_minus = Rop*fd_plus.flatten()
    d = WSop*p0_minus
    
    p0_minus = p0_minus.reshape(nsava, 2*nt-1)
    d = np.concatenate((d.reshape(nsava, 2*nt-1), np.zeros((nsava, 2*nt-1))))
    
    # solve
    f1 = lsqr(Mop, d.flatten(), iter_lim=10, show=False)[0]
    f1 = f1.reshape(2*nr, (2*nt-1))
    f1_tot = f1 + np.concatenate((np.zeros((nr, 2*nt-1)), fd_plus))
    
    g = BlockDiag([WiSop,WiSop])*Gop*f1_tot.flatten()
    g = g.reshape(2*nsava, (2*nt-1))
    
    f1_minus, f1_plus =  f1_tot[:nr], f1_tot[nr:]
    g_minus, g_plus =  -g[:nsava], np.fliplr(g[nsava:])

    return f1_minus, f1_plus, g_minus, g_plus, p0_minus
Ejemplo n.º 3
0
def test_dense_skinny(par):
    """Dense matrix representation of skinny matrix"""
    diag = np.arange(par["nx"]) + par["imag"] * np.arange(par["nx"])
    D = np.diag(diag)
    Dop = Diagonal(diag, dtype=par["dtype"])
    Zop = Zero(par["nx"], 3, dtype=par["dtype"])
    Op = HStack([Dop, Zop])
    O = np.hstack((D, np.zeros((par["nx"], 3))))
    assert_array_equal(Op.todense(), O)
Ejemplo n.º 4
0
def test_describe():
    """Testing the describe method. As it is is difficult to verify that the
    output is correct, at this point we merely test that no error arises when
    applying this method to a variety of operators
    """
    A = MatrixMult(np.ones((10, 5)))
    A.name = "A"
    B = Diagonal(np.ones(5))
    B.name = "A"
    C = MatrixMult(np.ones((10, 5)))
    C.name = "C"

    AT = A.T
    AH = A.H
    A3 = 3 * A
    D = A + C
    E = D * B
    F = (A + C) * B + A
    G = HStack((A * B, C * B))
    H = BlockDiag((F, G))

    describe(A)
    describe(AT)
    describe(AH)
    describe(A3)
    describe(D)
    describe(E)
    describe(F)
    describe(G)
    describe(H)
def test_skinnyregularization(par):
    """Solve inversion with a skinny regularization (rows are smaller than
    the number of elements in the model vector)
    """
    np.random.seed(10)
    d = np.arange(par['nx'] - 1).astype(par['dtype']) + 1.
    Dop = Diagonal(d, dtype=par['dtype'])
    Regop = HStack([Identity(par['nx'] // 2), Identity(par['nx'] // 2)])

    x = np.arange(par['nx'] - 1)
    y = Dop * x

    xinv = NormalEquationsInversion(Dop, [
        Regop,
    ], y, epsRs=[
        1e-4,
    ])
    assert_array_almost_equal(x, xinv, decimal=2)

    xinv = RegularizedInversion(Dop, [
        Regop,
    ], y, epsRs=[
        1e-4,
    ])
    assert_array_almost_equal(x, xinv, decimal=2)
Ejemplo n.º 6
0
def test_HStack_incosistent_columns(par):
    """Check error is raised if operators with different number of rows
    are passed to VStack
    """
    G1 = np.random.normal(0, 10, (par["ny"], par["nx"])).astype(par["dtype"])
    G2 = np.random.normal(0, 10, (par["ny"] + 1, par["nx"])).astype(par["dtype"])
    with pytest.raises(ValueError):
        HStack(
            [MatrixMult(G1, dtype=par["dtype"]), MatrixMult(G2, dtype=par["dtype"])],
            dtype=par["dtype"],
        )
Ejemplo n.º 7
0
def test_HStack_multiproc(par):
    """Single and multiprocess consistentcy for HStack operator"""
    np.random.seed(0)
    nproc = 2
    G = np.random.normal(0, 10, (par["ny"], par["nx"])).astype(par["dtype"])
    x = np.ones(4 * par["nx"]) + par["imag"] * np.ones(4 * par["nx"])
    y = np.ones(par["ny"]) + par["imag"] * np.ones(par["ny"])

    Hop = HStack([MatrixMult(G, dtype=par["dtype"])] * 4, dtype=par["dtype"])
    Hmultiop = HStack(
        [MatrixMult(G, dtype=par["dtype"])] * 4, nproc=nproc, dtype=par["dtype"]
    )
    assert dottest(
        Hmultiop, par["ny"], 4 * par["nx"], complexflag=0 if par["imag"] == 0 else 3
    )
    # forward
    assert_array_almost_equal(Hop * x, Hmultiop * x, decimal=4)
    # adjoint
    assert_array_almost_equal(Hop.H * y, Hmultiop.H * y, decimal=4)

    # close pool
    Hmultiop.pool.close()
Ejemplo n.º 8
0
def test_HStack(par):
    """Dot-test and inversion for HStack operator
    """
    np.random.seed(10)
    G1 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype('float32')
    G2 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype('float32')
    x = np.ones(2*par['nx']) + par['imag']*np.ones(2*par['nx'])

    Hop = HStack([MatrixMult(G1, dtype=par['dtype']),
                  MatrixMult(G2, dtype=par['dtype'])],
                 dtype=par['dtype'])
    assert dottest(Hop, par['ny'], 2*par['nx'], complexflag=0 if par['imag'] == 0 else 3)

    xlsqr = lsqr(Hop, Hop * x, damp=1e-20, iter_lim=300, show=0)[0]
    assert_array_almost_equal(x, xlsqr, decimal=4)
Ejemplo n.º 9
0
def test_HStack(par):
    """Dot-test and inversion for HStack operator
    """
    np.random.seed(10)
    G1 = da.random.normal(0, 10, (par['ny'], par['nx'])).astype('float32')
    G2 = da.random.normal(0, 10, (par['ny'], par['nx'])).astype('float32')
    x = da.ones(2 * par['nx']) + par['imag'] * da.ones(2 * par['nx'])
    dops = [dMatrixMult(G1, dtype=par['dtype']),
            dMatrixMult(G2, dtype=par['dtype'])]
    ops = [MatrixMult(G1.compute(), dtype=par['dtype']),
           MatrixMult(G2.compute(), dtype=par['dtype'])]
    dHop = dHStack(dops, compute=(True, True), dtype=par['dtype'])
    Hop = HStack(ops, dtype=par['dtype'])
    assert dottest(dHop, par['ny'], 2*par['nx'],
                   chunks=(par['ny'], 2*par['nx']),
                   complexflag=0 if par['imag'] == 0 else 3)

    dy = dHop * x.ravel()
    y = Hop * x.ravel().compute()
    assert_array_almost_equal(dy, y, decimal=4)
Ejemplo n.º 10
0
def test_HStack_rlinear(par):
    """HStack operator applied to mix of R-linear and C-linear operators"""
    np.random.seed(0)
    if np.dtype(par["dtype"]).kind == "c":
        G = (
            np.random.normal(0, 10, (par["ny"], par["nx"]))
            + 1j * np.random.normal(0, 10, (par["ny"], par["nx"]))
        ).astype(par["dtype"])
    else:
        G = np.random.normal(0, 10, (par["ny"], par["nx"])).astype(par["dtype"])
    Rop = Real(dims=(par["ny"],), dtype=par["dtype"])

    HSop = HStack([Rop, MatrixMult(G, dtype=par["dtype"])], dtype=par["dtype"])
    assert HSop.clinear == False
    assert dottest(
        HSop, par["ny"], par["nx"] + par["ny"], complexflag=0 if par["imag"] == 0 else 3
    )
    # forward
    x = np.random.randn(par["nx"] + par["ny"]) + par["imag"] * np.random.randn(
        par["nx"] + par["ny"]
    )
    expected = np.sum([np.real(x[: par["ny"]]), G @ x[par["ny"] :]], axis=0)
    assert_array_almost_equal(expected, HSop * x, decimal=4)
Ejemplo n.º 11
0
def Sliding3D(Op, dims, dimsd, nwin, nover, nop,
              tapertype='hanning', design=False, nproc=1):
    """3D Sliding transform operator.

    Apply a transform operator ``Op`` repeatedly to patches of the model
    vector in forward mode and patches of the data vector in adjoint mode.
    More specifically, in forward mode the model vector is divided into patches
    each patch is transformed, and patches are then recombined in a sliding
    window fashion. Both model and data should be 3-dimensional
    arrays in nature as they are internally reshaped and interpreted as
    3-dimensional arrays. Each patch contains in fact a portion of the
    array in the first and second dimensions (and the entire third dimension).

    This operator can be used to perform local, overlapping transforms (e.g.,
    :obj:`pylops.signalprocessing.FFTND`
    or :obj:`pylops.signalprocessing.Radon3D`) of 3-dimensional arrays.

    .. note:: The shape of the model has to be consistent with
       the number of windows for this operator not to return an error. As the
       number of windows depends directly on the choice of ``nwin`` and
       ``nover``, it is recommended to use ``design=True`` if unsure about the
       choice ``dims`` and use the number of windows printed on screen to
       define such input parameter.

    .. warning:: Depending on the choice of `nwin` and `nover` as well as the
       size of the data, sliding windows may not cover the entire first and/or
       second dimensions. The start and end indeces of each window can be
       displayed using ``design=True`` while defining the best sliding window
       approach.

    Parameters
    ----------
    Op : :obj:`pylops.LinearOperator`
        Transform operator
    dims : :obj:`tuple`
        Shape of 3-dimensional model. Note that ``dims[0]`` and ``dims[1]``
        should be multiple of the model sizes of the transform in the
        first and second dimensions
    dimsd : :obj:`tuple`
        Shape of 3-dimensional data
    nwin : :obj:`tuple`
        Number of samples of window
    nover : :obj:`tuple`
        Number of samples of overlapping part of window
    nop : :obj:`tuple`
        Number of samples in axes of transformed domain associated
        to spatial axes in the data
    tapertype : :obj:`str`, optional
        Type of taper (``hanning``, ``cosine``, ``cosinesquare`` or ``None``)
    design : :obj:`bool`, optional
        Print number sliding window (``True``) or not (``False``)

    Returns
    -------
    Sop : :obj:`pylops.LinearOperator`
        Sliding operator

    Raises
    ------
    ValueError
        Identified number of windows is not consistent with provided model
        shape (``dims``).

    """
    # model windows
    mwin0_ins, mwin0_ends = _slidingsteps(dims[0],
                                          Op.shape[1]//(nop[1]*dims[2]), 0)
    mwin1_ins, mwin1_ends = _slidingsteps(dims[1],
                                          Op.shape[1]//(nop[0]*dims[2]), 0)

    # data windows
    dwin0_ins, dwin0_ends = _slidingsteps(dimsd[0], nwin[0], nover[0])
    dwin1_ins, dwin1_ends = _slidingsteps(dimsd[1], nwin[1], nover[1])
    nwins0 = len(dwin0_ins)
    nwins1 = len(dwin1_ins)
    nwins = nwins0*nwins1

    # create tapers
    if tapertype is not None:
        tap = taper3d(dimsd[2], nwin, nover, tapertype=tapertype)

    # check that identified number of windows agrees with mode size
    if design:
        logging.warning('(%d,%d) windows required...', nwins0, nwins1)
        logging.warning('model wins - start0:%s, end0:%s, start1:%s, end1:%s',
                        str(mwin0_ins), str(mwin0_ends),
                        str(mwin1_ins), str(mwin1_ends))
        logging.warning('data wins - start0:%s, end0:%s, start1:%s, end1:%s',
                        str(dwin0_ins), str(dwin0_ends),
                        str(dwin1_ins), str(dwin1_ends))

    if nwins*Op.shape[1]//dims[2] != dims[0]*dims[1]:
        raise ValueError('Model shape (dims=%s) is not consistent with chosen '
                         'number of windows. Choose dims[0]=%d and '
                         'dims[1]=%d for the operator to work with '
                         'estimated number of windows, or create '
                         'the operator with design=True to find out the'
                         'optimal number of windows for the current '
                         'model size...'
                         % (str(dims), nwins0*Op.shape[1]//(nop[1]*dims[2]),
                            nwins1 * Op.shape[1]//(nop[0]*dims[2])))
    # transform to apply
    if tapertype is None:
        OOp = BlockDiag([Op for _ in range(nwins)], nproc=nproc)
    else:
        OOp = BlockDiag([Diagonal(tap.flatten()) * Op
                         for _ in range(nwins)], nproc=nproc)

    hstack = HStack([Restriction(dimsd[1] * dimsd[2] * nwin[0],
                                 range(win_in, win_end),
                                 dims=(nwin[0], dimsd[1], dimsd[2]),
                                 dir=1).H
                     for win_in, win_end in zip(dwin1_ins,
                                                dwin1_ends)])

    combining1 = BlockDiag([hstack]*nwins0)
    combining0 = HStack([Restriction(np.prod(dimsd),
                                     range(win_in, win_end),
                                     dims=dimsd, dir=0).H
                         for win_in, win_end in zip(dwin0_ins, dwin0_ends)])
    Sop = combining0 * combining1 * OOp
    return Sop
Ejemplo n.º 12
0
def Block(ops, dtype=None):
    r"""Block operator.

    Create a block operator from N lists of M linear operators each.

    Parameters
    ----------
    ops : :obj:`list`
        List of lists of operators to be combined in block fashion
    dtype : :obj:`str`, optional
        Type of elements in input array.

    Attributes
    ----------
    shape : :obj:`tuple`
        Operator shape
    explicit : :obj:`bool`
        Operator contains a matrix that can be solved explicitly (``True``) or
        not (``False``)

    Notes
    -----
    In mathematics, a block or a partitioned matrix is a matrix that is
    interpreted as being broken into sections called blocks or submatrices.
    Similarly a block operator is composed of N sets of M linear operators
    each such that its application in forward mode leads to

    .. math::
        \begin{bmatrix}
            \mathbf{L_{1,1}}  & \mathbf{L_{1,2}} &  ... & \mathbf{L_{1,M}}  \\
            \mathbf{L_{2,1}}  & \mathbf{L_{2,2}} &  ... & \mathbf{L_{2,M}}  \\
            ...               & ...              &  ... & ...               \\
            \mathbf{L_{N,1}}  & \mathbf{L_{N,2}} &  ... & \mathbf{L_{N,M}}  \\
        \end{bmatrix}
        \begin{bmatrix}
            \mathbf{x}_{1}  \\
            \mathbf{x}_{2}  \\
            ...     \\
            \mathbf{x}_{M}
        \end{bmatrix} =
        \begin{bmatrix}
            \mathbf{L_{1,1}} \mathbf{x}_{1} + \mathbf{L_{1,2}} \mathbf{x}_{2} +
            \mathbf{L_{1,M}} \mathbf{x}_{M} \\
            \mathbf{L_{2,1}} \mathbf{x}_{1} + \mathbf{L_{2,2}} \mathbf{x}_{2} +
            \mathbf{L_{2,M}} \mathbf{x}_{M} \\
            ...     \\
            \mathbf{L_{N,1}} \mathbf{x}_{1} + \mathbf{L_{N,2}} \mathbf{x}_{2} +
            \mathbf{L_{N,M}} \mathbf{x}_{M} \\
        \end{bmatrix}

    while its application in adjoint mode leads to

    .. math::
        \begin{bmatrix}
            \mathbf{L_{1,1}}^H  & \mathbf{L_{2,1}}^H &  ... &
            \mathbf{L_{N,1}}^H  \\
            \mathbf{L_{1,2}}^H  & \mathbf{L_{2,2}}^H &  ... &
            \mathbf{L_{N,2}}^H  \\
            ...                 & ...                &  ... & ... \\
            \mathbf{L_{1,M}}^H  & \mathbf{L_{2,M}}^H &  ... &
            \mathbf{L_{N,M}}^H  \\
        \end{bmatrix}
        \begin{bmatrix}
            \mathbf{y}_{1}  \\
            \mathbf{y}_{2}  \\
            ...     \\
            \mathbf{y}_{N}
        \end{bmatrix} =
        \begin{bmatrix}
            \mathbf{L_{1,1}}^H \mathbf{y}_{1} +
            \mathbf{L_{2,1}}^H \mathbf{y}_{2} +
            \mathbf{L_{N,1}}^H \mathbf{y}_{N} \\
            \mathbf{L_{1,2}}^H \mathbf{y}_{1} +
            \mathbf{L_{2,2}}^H \mathbf{y}_{2} +
            \mathbf{L_{N,2}}^H \mathbf{y}_{N} \\
            ...     \\
            \mathbf{L_{1,M}}^H \mathbf{y}_{1} +
            \mathbf{L_{2,M}}^H \mathbf{y}_{2} +
            \mathbf{L_{N,M}}^H \mathbf{y}_{N} \\
        \end{bmatrix}

    """
    hblocks = [HStack(hblock, dtype=dtype) for hblock in ops]
    return VStack(hblocks, dtype=dtype)
Ejemplo n.º 13
0
def Sliding1D(Op, dim, dimd, nwin, nover, tapertype="hanning", design=False):
    r"""1D Sliding transform operator.

    Apply a transform operator ``Op`` repeatedly to slices of the model
    vector in forward mode and slices of the data vector in adjoint mode.
    More specifically, in forward mode the model vector is divided into
    slices, each slice is transformed, and slices are then recombined in a
    sliding window fashion.

    This operator can be used to perform local, overlapping transforms (e.g.,
    :obj:`pylops.signalprocessing.FFT`) on 1-dimensional arrays.

    .. note:: The shape of the model has to be consistent with
       the number of windows for this operator not to return an error. As the
       number of windows depends directly on the choice of ``nwin`` and
       ``nover``, it is recommended to use ``design=True`` if unsure about the
       choice ``dims`` and use the number of windows printed on screen to
       define such input parameter.

    .. warning:: Depending on the choice of `nwin` and `nover` as well as the
       size of the data, sliding windows may not cover the entire data.
       The start and end indices of each window can be displayed using
       ``design=True`` while defining the best sliding window approach.

    Parameters
    ----------
    Op : :obj:`pylops.LinearOperator`
        Transform operator
    dim : :obj:`tuple`
        Shape of 1-dimensional model.
    dimd : :obj:`tuple`
        Shape of 1-dimensional data
    nwin : :obj:`int`
        Number of samples of window
    nover : :obj:`int`
        Number of samples of overlapping part of window
    tapertype : :obj:`str`, optional
        Type of taper (``hanning``, ``cosine``, ``cosinesquare`` or ``None``)
    design : :obj:`bool`, optional
        Print number of sliding window (``True``) or not (``False``)

    Returns
    -------
    Sop : :obj:`pylops.LinearOperator`
        Sliding operator

    Raises
    ------
    ValueError
        Identified number of windows is not consistent with provided model
        shape (``dims``).

    """
    # model windows
    mwin_ins, mwin_ends = _slidingsteps(dim, Op.shape[1], 0)
    # data windows
    dwin_ins, dwin_ends = _slidingsteps(dimd, nwin, nover)
    nwins = len(dwin_ins)

    # create tapers
    if tapertype is not None:
        tap = taper(nwin, nover, tapertype=tapertype)
        tapin = tap.copy()
        tapin[:nover] = 1
        tapend = tap.copy()
        tapend[-nover:] = 1
        taps = {}
        taps[0] = tapin
        for i in range(1, nwins - 1):
            taps[i] = tap
        taps[nwins - 1] = tapend

    # check that identified number of windows agrees with mode size
    if design:
        logging.warning("%d windows required...", nwins)
        logging.warning("model wins - start:%s, end:%s", str(mwin_ins), str(mwin_ends))
        logging.warning("data wins - start:%s, end:%s", str(dwin_ins), str(dwin_ends))
    if nwins * Op.shape[1] != dim:
        raise ValueError(
            "Model shape (dim=%d) is not consistent with chosen "
            "number of windows. Choose dim=%d for the "
            "operator to work with estimated number of windows, "
            "or create the operator with design=True to find "
            "out the optimal number of windows for the current "
            "model size..." % (dim, nwins * Op.shape[1])
        )
    # transform to apply
    if tapertype is None:
        OOp = BlockDiag([Op for _ in range(nwins)])
    else:
        OOp = BlockDiag([Diagonal(taps[itap].ravel()) * Op for itap in range(nwins)])

    combining = HStack(
        [
            Restriction(dimd, np.arange(win_in, win_end), dtype=Op.dtype).H
            for win_in, win_end in zip(dwin_ins, dwin_ends)
        ]
    )
    Sop = combining * OOp
    return Sop
def focusing_wrapper(direct, toff, g0VS, iava1, Rop1, R1op1, Restrop1, iava2,
                     Rop2, R1op2, Restrop2, t):
    nsmooth = 10
    nr = direct.shape[0]
    nsava1 = iava1.shape[0]
    nsava2 = iava2.shape[0]

    nt = t.shape[0]
    dt = t[1] - t[0]

    # window
    directVS_off = direct - toff
    idirectVS_off = np.round(directVS_off / dt).astype(np.int)
    w = np.zeros((nr, nt))
    wi = np.ones((nr, nt))
    for ir in range(nr - 1):
        w[ir, :idirectVS_off[ir]] = 1
    wi = wi - w

    w = np.hstack((np.fliplr(w), w[:, 1:]))
    wi = np.hstack((np.fliplr(wi), wi[:, 1:]))

    if nsmooth > 0:
        smooth = np.ones(nsmooth) / nsmooth
        w = filtfilt(smooth, 1, w)
        wi = filtfilt(smooth, 1, wi)

    # Input focusing function
    fd_plus = np.concatenate((np.fliplr(g0VS.T), np.zeros((nr, nt - 1))),
                             axis=-1)

    # operators
    Wop = Diagonal(w.flatten())
    WSop1 = Diagonal(w[iava1].flatten())
    WSop2 = Diagonal(w[iava2].flatten())
    WiSop1 = Diagonal(wi[iava1].flatten())
    WiSop2 = Diagonal(wi[iava2].flatten())

    Mop1 = VStack([
        HStack([Restrop1, -1 * WSop1 * Rop1]),
        HStack([-1 * WSop1 * R1op1, Restrop1])
    ]) * BlockDiag([Wop, Wop])
    Mop2 = VStack([
        HStack([Restrop2, -1 * WSop2 * Rop2]),
        HStack([-1 * WSop2 * R1op2, Restrop2])
    ]) * BlockDiag([Wop, Wop])
    Mop = VStack([
        HStack([Mop1, Mop1, Zero(Mop1.shape[0], Mop1.shape[1])]),
        HStack([Mop2, Zero(Mop2.shape[0], Mop2.shape[1]), Mop2])
    ])

    Gop1 = VStack(
        [HStack([Restrop1, -1 * Rop1]),
         HStack([-1 * R1op1, Restrop1])])
    Gop2 = VStack(
        [HStack([Restrop2, -1 * Rop2]),
         HStack([-1 * R1op2, Restrop2])])

    d1 = WSop1 * Rop1 * fd_plus.flatten()
    d1 = np.concatenate(
        (d1.reshape(nsava1, 2 * nt - 1), np.zeros((nsava1, 2 * nt - 1))))
    d2 = WSop2 * Rop2 * fd_plus.flatten()
    d2 = np.concatenate(
        (d2.reshape(nsava2, 2 * nt - 1), np.zeros((nsava2, 2 * nt - 1))))

    d = np.concatenate((d1, d2))

    # solve
    comb_f = lsqr(Mop, d.flatten(), iter_lim=10, show=False)[0]
    comb_f = comb_f.reshape(6 * nr, (2 * nt - 1))
    comb_f_tot = comb_f + np.concatenate((np.zeros(
        (nr, 2 * nt - 1)), fd_plus, np.zeros((4 * nr, 2 * nt - 1))))

    f1_1 = comb_f_tot[:2 * nr] + comb_f_tot[2 * nr:4 * nr]
    f1_2 = comb_f_tot[:2 * nr] + comb_f_tot[4 * nr:]

    g_1 = BlockDiag([WiSop1, WiSop1]) * Gop1 * f1_1.flatten()
    g_1 = g_1.reshape(2 * nsava1, (2 * nt - 1))
    g_2 = BlockDiag([WiSop2, WiSop2]) * Gop2 * f1_2.flatten()
    g_2 = g_2.reshape(2 * nsava2, (2 * nt - 1))

    f1_1_minus, f1_1_plus = f1_1[:nr], f1_1[nr:]
    f1_2_minus, f1_2_plus = f1_2[:nr], f1_2[nr:]
    g_1_minus, g_1_plus = -g_1[:nsava1], np.fliplr(g_1[nsava1:])
    g_2_minus, g_2_plus = -g_2[:nsava2], np.fliplr(g_2[nsava2:])

    return f1_1_minus, f1_1_plus, f1_2_minus, f1_2_plus, g_1_minus, g_1_plus, g_2_minus, g_2_plus
def redatuming_wrapper(toff, W, wav, iava, Rop, R1op, Restrop, Sparseop, vsx,
                       vsz, x, z, z_current, nt, dt, nfft, nr, ds, dvsx):

    from scipy.signal import filtfilt

    nava = iava.shape[0]
    nvsx = vsx.shape[0]

    PUP = np.zeros(shape=(nava, nvsx, nt))
    PDOWN = np.zeros(shape=(nava, nvsx, nt))

    for ix in range(nvsx):
        s = '####### Point ' + str(ix + 1) + ' of ' + str(
            nvsx) + ' of current line (z = ' + str(z_current) + ', x = ' + str(
                vsx[ix]) + ')'
        print(s)

        # direct wave
        direct = np.loadtxt(path0 + 'Traveltimes/trav_x' + str(vsx[ix]) +
                            '_z' + str(z_current) + '.dat',
                            delimiter=',')
        f = 2 * np.pi * np.arange(nfft) / (dt * nfft)
        g0VS = np.zeros((nfft, nr), dtype=np.complex128)
        for it in range(len(W)):
            g0VS[it] = W[it] * f[it] * hankel2(0, f[it] * direct + 1e-10) / 4
        g0VS = np.fft.irfft(g0VS, nfft, axis=0) / dt
        g0VS = np.real(g0VS[:nt])

        nr = direct.shape[0]
        nsava = iava.shape[0]

        # window
        directVS_off = direct - toff
        idirectVS_off = np.round(directVS_off / dt).astype(np.int)
        w = np.zeros((nr, nt))
        wi = np.ones((nr, nt))
        for ir in range(nr - 1):
            w[ir, :idirectVS_off[ir]] = 1
        wi = wi - w

        w = np.hstack((np.fliplr(w), w[:, 1:]))
        wi = np.hstack((np.fliplr(wi), wi[:, 1:]))

        # smoothing
        nsmooth = 10
        if nsmooth > 0:
            smooth = np.ones(nsmooth) / nsmooth
            w = filtfilt(smooth, 1, w)
            wi = filtfilt(smooth, 1, wi)

        # Input focusing function
        fd_plus = np.concatenate((np.fliplr(g0VS.T), np.zeros((nr, nt - 1))),
                                 axis=-1)

        # create operators
        Wop = Diagonal(w.flatten())
        WSop = Diagonal(w[iava].flatten())
        WiSop = Diagonal(wi[iava].flatten())

        Mop = VStack([
            HStack([Restrop, -1 * WSop * Rop]),
            HStack([-1 * WSop * R1op, Restrop])
        ]) * BlockDiag([Wop, Wop])

        Mop_radon = Mop * Sparseop

        Gop = VStack(
            [HStack([Restrop, -1 * Rop]),
             HStack([-1 * R1op, Restrop])])

        d = WSop * Rop * fd_plus.flatten()
        d = np.concatenate(
            (d.reshape(nsava, 2 * nt - 1), np.zeros((nsava, 2 * nt - 1))))

        # solve with SPGL1
        f = SPGL1(Mop_radon,
                  d.flatten(),
                  sigma=1e-5,
                  iter_lim=35,
                  opt_tol=0.05,
                  dec_tol=0.05,
                  verbosity=1)[0]

        # alternatively solve with FISTA
        #f = FISTA(Mop_radon, d.flatten(), eps=1e-1, niter=200,
        #           alpha=2.129944e-04, eigsiter=4, eigstol=1e-3,
        #           tol=1e-2, returninfo=False, show=True)[0]

        # alternatively solve with LSQR
        #f = lsqr(Mop_radon, d.flatten(), iter_lim=100, show=True)[0]

        f = Sparseop * f
        f = f.reshape(2 * nr, (2 * nt - 1))
        f_tot = f + np.concatenate((np.zeros((nr, 2 * nt - 1)), fd_plus))

        g_1 = BlockDiag([WiSop, WiSop]) * Gop * f_tot.flatten()
        g_1 = g_1.reshape(2 * nsava, (2 * nt - 1))

        #f1_minus, f1_plus =  f_tot[:nr], f_tot[nr:]
        g_minus, g_plus = -g_1[:nsava], np.fliplr(g_1[nsava:])

        #
        PUP[:, ix, :] = g_minus[:, nt - 1:]
        PDOWN[:, ix, :] = g_plus[:, nt - 1:]

    # save redatumed wavefield (line-by-line)
    jt = 2
    redatumed = MDD(PDOWN[:, :, ::jt],
                    PUP[:, :, ::jt],
                    dt=jt * dt,
                    dr=dvsx,
                    wav=wav[::jt],
                    twosided=True,
                    adjoint=False,
                    psf=False,
                    dtype='complex64',
                    dottest=False,
                    **dict(iter_lim=20, show=0))

    np.savetxt(path_save + 'Line1_' + str(z_current) + '.dat',
               np.diag(redatumed[:, :, (nt + 1) // jt - 1]),
               delimiter=',')

    # calculate and save angle gathers (line-by-line)
    vel_sm = np.loadtxt(path0 + 'vel_sm.dat', delimiter=',')
    cp = vel_sm[find_closest(z_current, z), 751 // 2]

    irA = np.asarray([7, 15, 24, 35])
    nalpha = 201
    A = np.zeros((nalpha, len(irA)))

    for i in np.arange(0, len(irA)):
        ir = irA[i]
        anglegath, alpha = AngleGather(np.swapaxes(redatumed, 0, 2), nvsx,
                                       nalpha, dt * jt, ds, ir, cp)
        A[:, i] = anglegath

    np.savetxt(path_save + 'AngleGather1_' + str(z_current) + '.dat',
               A,
               delimiter=',')
Ejemplo n.º 16
0
def Patch2D(Op,
            dims,
            dimsd,
            nwin,
            nover,
            nop,
            tapertype="hanning",
            design=False):
    """2D Patch transform operator.

    Apply a transform operator ``Op`` repeatedly to patches of the model
    vector in forward mode and patches of the data vector in adjoint mode.
    More specifically, in forward mode the model vector is divided into
    patches, each patch is transformed, and patches are then recombined
    together. Both model and data are internally reshaped and
    interpreted as 2-dimensional arrays: each patch contains a portion
    of the array in both the first and second dimension.

    This operator can be used to perform local, overlapping transforms (e.g.,
    :obj:`pylops.signalprocessing.FFT2D`
    or :obj:`pylops.signalprocessing.Radon2D`) on 2-dimensional arrays.

    .. note:: The shape of the model has to be consistent with
       the number of windows for this operator not to return an error. As the
       number of windows depends directly on the choice of ``nwin`` and
       ``nover``, it is recommended to use ``design=True`` if unsure about the
       choice ``dims`` and use the number of windows printed on screen to
       define such input parameter.

    .. warning:: Depending on the choice of `nwin` and `nover` as well as the
       size of the data, patches may not cover the entire size of the data.
       The start and end indices of each window can be displayed using
       ``design=True`` while defining the best patching approach.

    Parameters
    ----------
    Op : :obj:`pylops.LinearOperator`
        Transform operator
    dims : :obj:`tuple`
        Shape of 2-dimensional model. Note that ``dims[0]`` and ``dims[1]``
        should be multiple of the model size of the transform in their
        respective dimensions
    dimsd : :obj:`tuple`
        Shape of 2-dimensional data
    nwin : :obj:`tuple`
        Number of samples of window
    nover : :obj:`tuple`
        Number of samples of overlapping part of window
    nop : :obj:`tuple`
        Size of model in the transformed domain
    tapertype : :obj:`str`, optional
        Type of taper (``hanning``, ``cosine``, ``cosinesquare`` or ``None``)
    design : :obj:`bool`, optional
        Print number of sliding window (``True``) or not (``False``)

    Returns
    -------
    Sop : :obj:`pylops.LinearOperator`
        Sliding operator

    Raises
    ------
    ValueError
        Identified number of windows is not consistent with provided model
        shape (``dims``).

    See Also
    --------
    Sliding2d: 2D Sliding transform operator.

    """
    # model windows
    mwin0_ins, mwin0_ends = _slidingsteps(dims[0], nop[0], 0)
    mwin1_ins, mwin1_ends = _slidingsteps(dims[1], nop[1], 0)

    # data windows
    dwin0_ins, dwin0_ends = _slidingsteps(dimsd[0], nwin[0], nover[0])
    dwin1_ins, dwin1_ends = _slidingsteps(dimsd[1], nwin[1], nover[1])
    nwins0 = len(dwin0_ins)
    nwins1 = len(dwin1_ins)
    nwins = nwins0 * nwins1

    # create tapers
    if tapertype is not None:
        tap = taper2d(nwin[1], nwin[0], nover,
                      tapertype=tapertype).astype(Op.dtype)
        taps = {itap: tap for itap in range(nwins)}
        # topmost tapers
        taptop = tap.copy()
        taptop[:nover[0]] = tap[nwin[0] // 2]
        for itap in range(0, nwins1):
            taps[itap] = taptop
        # bottommost tapers
        tapbottom = tap.copy()
        tapbottom[-nover[0]:] = tap[nwin[0] // 2]
        for itap in range(nwins - nwins1, nwins):
            taps[itap] = tapbottom
        # leftmost tapers
        tapleft = tap.copy()
        tapleft[:, :nover[1]] = tap[:, nwin[1] // 2][:, np.newaxis]
        for itap in range(0, nwins, nwins1):
            taps[itap] = tapleft
        # rightmost tapers
        tapright = tap.copy()
        tapright[:, -nover[1]:] = tap[:, nwin[1] // 2][:, np.newaxis]
        for itap in range(nwins1 - 1, nwins, nwins1):
            taps[itap] = tapright
        # lefttopcorner taper
        taplefttop = tap.copy()
        taplefttop[:, :nover[1]] = tap[:, nwin[1] // 2][:, np.newaxis]
        taplefttop[:nover[0]] = taplefttop[nwin[0] // 2]
        taps[0] = taplefttop
        # righttopcorner taper
        taprighttop = tap.copy()
        taprighttop[:, -nover[1]:] = tap[:, nwin[1] // 2][:, np.newaxis]
        taprighttop[:nover[0]] = taprighttop[nwin[0] // 2]
        taps[nwins1 - 1] = taprighttop
        # leftbottomcorner taper
        tapleftbottom = tap.copy()
        tapleftbottom[:, :nover[1]] = tap[:, nwin[1] // 2][:, np.newaxis]
        tapleftbottom[-nover[0]:] = tapleftbottom[nwin[0] // 2]
        taps[nwins - nwins1] = tapleftbottom
        # rightbottomcorner taper
        taprightbottom = tap.copy()
        taprightbottom[:, -nover[1]:] = tap[:, nwin[1] // 2][:, np.newaxis]
        taprightbottom[-nover[0]:] = taprightbottom[nwin[0] // 2]
        taps[nwins - 1] = taprightbottom

    # check that identified number of windows agrees with mode size
    if design:
        logging.warning("%d-%d windows required...", nwins0, nwins1)
        logging.warning(
            "model wins - start:%s, end:%s / start:%s, end:%s",
            str(mwin0_ins),
            str(mwin0_ends),
            str(mwin1_ins),
            str(mwin1_ends),
        )
        logging.warning(
            "data wins - start:%s, end:%s / start:%s, end:%s",
            str(dwin0_ins),
            str(dwin0_ends),
            str(dwin1_ins),
            str(dwin1_ends),
        )
    if nwins0 * nop[0] != dims[0] or nwins1 * nop[1] != dims[1]:
        raise ValueError("Model shape (dims=%s) is not consistent with chosen "
                         "number of windows. Choose dims[0]=%d and "
                         "dims[1]=%d for the operator to work with "
                         "estimated number of windows, or create "
                         "the operator with design=True to find out the"
                         "optimal number of windows for the current "
                         "model size..." %
                         (str(dims), nwins0 * nop[0], nwins1 * nop[1]))
    # transform to apply
    if tapertype is None:
        OOp = BlockDiag([Op for _ in range(nwins)])
    else:
        OOp = BlockDiag([
            Diagonal(taps[itap].ravel(), dtype=Op.dtype) * Op
            for itap in range(nwins)
        ])

    hstack = HStack([
        Restriction(
            dimsd[1] * nwin[0],
            range(win_in, win_end),
            dims=(nwin[0], dimsd[1]),
            dir=1,
            dtype=Op.dtype,
        ).H for win_in, win_end in zip(dwin1_ins, dwin1_ends)
    ])

    combining1 = BlockDiag([hstack] * nwins0)
    combining0 = HStack([
        Restriction(
            np.prod(dimsd),
            range(win_in, win_end),
            dims=dimsd,
            dir=0,
            dtype=Op.dtype,
        ).H for win_in, win_end in zip(dwin0_ins, dwin0_ends)
    ])
    Pop = combining0 * combining1 * OOp
    return Pop
Ejemplo n.º 17
0
def Sliding2D(Op, dims, dimsd, nwin, nover, tapertype='hanning', design=False):
    """2D Sliding transform operator.

    Apply a transform operator ``Op`` repeatedly to patches of the model
    vector in forward mode and patches of the data vector in adjoint mode.
    More specifically, in forward mode the model vector is divided into patches
    each patch is transformed, and patches are then recombined in a sliding
    window fashion. Both model and data should be 2-dimensional
    arrays in nature as they are internally reshaped and interpreted as
    2-dimensional arrays. Each patch contains in fact a portion of the
    array in the first dimension (and the entire second dimension).

    This operator can be used to perform local, overlapping transforms (e.g.,
    :obj:`pylops.signalprocessing.FFT2`
    or :obj:`pylops.signalprocessing.Radon2D`) of 2-dimensional arrays.

    .. note:: The shape of the model has to be consistent with
       the number of windows for this operator not to return an error. As the
       number of windows depends directly on the choice of ``nwin`` and
       ``nover``, it is recommended to use ``design=True`` if unsure about the
       choice ``dims`` and use the number of windows printed on screen to
       define such input parameter.

    .. warning:: Depending on the choice of `nwin` and `nover` as well as the
       size of the data, sliding windows may not cover the entire first dimension.
       The start and end indeces of each window can be displayed using
       ``design=True`` while defining the best sliding window approach.

    Parameters
    ----------
    Op : :obj:`pylops.LinearOperator`
        Transform operator
    dims : :obj:`tuple`
        Shape of 2-dimensional model. Note that ``dims[0]`` should be multiple
        of the model size of the transform in the first dimension
    dimsd : :obj:`tuple`
        Shape of 2-dimensional data
    nwin : :obj:`int`
        Number of samples of window
    nover : :obj:`int`
        Number of samples of overlapping part of window
    tapertype : :obj:`str`, optional
        Type of taper (``hanning``, ``cosine``, ``cosinesquare`` or ``None``)
    design : :obj:`bool`, optional
        Print number of sliding window (``True``) or not (``False``)

    Returns
    -------
    Sop : :obj:`pylops.LinearOperator`
        Sliding operator

    Raises
    ------
    ValueError
        Identified number of windows is not consistent with provided model
        shape (``dims``).

    """
    # model windows
    mwin_ins, mwin_ends = _slidingsteps(dims[0], Op.shape[1] // dims[1], 0)
    # data windows
    dwin_ins, dwin_ends = _slidingsteps(dimsd[0], nwin, nover)
    nwins = len(dwin_ins)

    # create tapers
    if tapertype is not None:
        tap = taper2d(dimsd[1], nwin, nover, tapertype=tapertype)
        tapin = tap.copy()
        tapin[:nover] = 1
        tapend = tap.copy()
        tapend[-nover:] = 1
        taps = {}
        taps[0] = tapin
        for i in range(1, nwins - 1):
            taps[i] = tap
        taps[nwins - 1] = tapend

    # check that identified number of windows agrees with mode size
    if design:
        logging.warning('%d windows required...', nwins)
        logging.warning('model wins - start:%s, end:%s', str(mwin_ins),
                        str(mwin_ends))
        logging.warning('data wins - start:%s, end:%s', str(dwin_ins),
                        str(dwin_ends))
    if nwins * Op.shape[1] // dims[1] != dims[0]:
        raise ValueError('Model shape (dims=%s) is not consistent with chosen '
                         'number of windows. Choose dims[0]=%d for the '
                         'operator to work with estimated number of windows, '
                         'or create the operator with design=True to find '
                         'out the optimal number of windows for the current '
                         'model size...' %
                         (str(dims), nwins * Op.shape[1] // dims[1]))
    # transform to apply
    if tapertype is None:
        OOp = BlockDiag([Op for _ in range(nwins)])
    else:
        OOp = BlockDiag(
            [Diagonal(taps[itap].flatten()) * Op for itap in range(nwins)])

    combining = HStack([
        Restriction(np.prod(dimsd), range(win_in, win_end), dims=dimsd).H
        for win_in, win_end in zip(dwin_ins, dwin_ends)
    ])
    Sop = combining * OOp
    return Sop