def test_BlockDiag_multiproc(par): """Single and multiprocess consistentcy for BlockDiag 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(4 * par['ny']) + par['imag'] * np.ones(4 * par['ny']) BDop = BlockDiag([MatrixMult(G, dtype=par['dtype'])] * 4, dtype=par['dtype']) BDmultiop = BlockDiag([MatrixMult(G, dtype=par['dtype'])] * 4, nproc=nproc, dtype=par['dtype']) assert dottest(BDmultiop, 4 * par['ny'], 4 * par['nx'], complexflag=0 if par['imag'] == 0 else 3) # forward assert_array_almost_equal(BDop * x, BDmultiop * x, decimal=4) # adjoint assert_array_almost_equal(BDop.H * y, BDmultiop.H * y, decimal=4) # close pool BDmultiop.pool.close()
def test_BlockDiag(par): """Dot-test and inversion for BlockDiag operator""" np.random.seed(0) G1 = np.random.normal(0, 10, (par["ny"], par["nx"])).astype(par["dtype"]) G2 = np.random.normal(0, 10, (par["ny"], par["nx"])).astype(par["dtype"]) x = np.ones(2 * par["nx"]) + par["imag"] * np.ones(2 * par["nx"]) BDop = BlockDiag( [MatrixMult(G1, dtype=par["dtype"]), MatrixMult(G2, dtype=par["dtype"])], dtype=par["dtype"], ) assert dottest( BDop, 2 * par["ny"], 2 * par["nx"], complexflag=0 if par["imag"] == 0 else 3 ) xlsqr = lsqr( BDop, BDop * x, damp=1e-20, iter_lim=500, atol=1e-8, btol=1e-8, show=0 )[0] assert_array_almost_equal(x, xlsqr, decimal=3) # use numpy matrix directly in the definition of the operator BD1op = BlockDiag([MatrixMult(G1, dtype=par["dtype"]), G2], dtype=par["dtype"]) assert dottest( BD1op, 2 * par["ny"], 2 * par["nx"], complexflag=0 if par["imag"] == 0 else 3 ) # use scipy matrix directly in the definition of the operator G2 = sp_random(par["ny"], par["nx"], density=0.4).astype("float32") BD2op = BlockDiag([MatrixMult(G1, dtype=par["dtype"]), G2], dtype=par["dtype"]) assert dottest( BD2op, 2 * par["ny"], 2 * par["nx"], complexflag=0 if par["imag"] == 0 else 3 )
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
def test_BlockDiag_rlinear(par): """BlockDiag 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['nx'], ), dtype=par['dtype']) BDop = BlockDiag([Rop, MatrixMult(G, dtype=par['dtype'])], dtype=par['dtype']) assert BDop.clinear == False assert dottest(BDop, par['nx'] + par['ny'], 2 * par['nx'], complexflag=0 if par['imag'] == 0 else 3) # forward x = np.random.randn( 2 * par['nx']) + par['imag'] * np.random.randn(2 * par['nx']) expected = np.concatenate([np.real(x[:par['nx']]), G @ x[par['nx']:]]) assert_array_almost_equal(expected, BDop * x, decimal=4)
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_BlockDiag(par): """Dot-test and inversion for BlockDiag operator """ np.random.seed(10) G1 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype(par['dtype']) G2 = np.random.normal(0, 10, (par['ny'], par['nx'])).astype(par['dtype']) x = np.ones(2*par['nx']) + par['imag']*np.ones(2*par['nx']) BDop = BlockDiag([MatrixMult(G1, dtype=par['dtype']), MatrixMult(G2, dtype=par['dtype'])], dtype=par['dtype']) assert dottest(BDop, 2*par['ny'], 2*par['nx'], complexflag=0 if par['imag'] == 0 else 3) xlsqr = lsqr(BDop, BDop * x, damp=1e-20, iter_lim=500, show=0)[0] assert_array_almost_equal(x, xlsqr, decimal=3)
def test_BlockDiag(par): """Dot-test and comparison with pylops for BlockDiag operator """ np.random.seed(10) G1 = da.random.normal(0, 10, (par['ny'], par['nx'])).astype(par['dtype']) G2 = da.random.normal(0, 10, (par['ny'], par['nx'])).astype(par['dtype']) x = da.ones(2*par['nx']) + par['imag']*np.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'])] dBDop = dBlockDiag(dops, compute=(True, True), dtype=par['dtype']) BDop = BlockDiag(ops, dtype=par['dtype']) assert dottest(dBDop, 2 * par['ny'], 2 * par['nx'], chunks=(2 * par['ny'], 2 * par['nx']), complexflag=0 if par['imag'] == 0 else 3) dy = dBDop * x.ravel() y = BDop * x.ravel().compute() assert_array_almost_equal(dy, y, decimal=4)
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
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 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
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
def Marchenko_depthloop_IndepRadon(zinvs, zendvs): toff = 0.045 # direct arrival time shift nfmax = 550 # max frequency for MDC (#samples) nfft = 2**11 jr = 3 # subsampling in r # subsurface array xinvs = 600 # receiver array initial point in x xendvs = 2400 # receiver array last point in x dvsx = 20 # receiver array sampling in x dvsz = 5 # receiver array sampling in z # line of subsurface points for virtual sources vsx = np.arange(xinvs, xendvs + dvsx, dvsx) vsz = np.arange(zinvs, zendvs + dvsz, dvsz) # geometry nz = 401 oz = 0 dz = 4 z = np.arange(oz, oz + nz * dz, dz) nx = 751 ox = 0 dx = 4 x = np.arange(ox, ox + nx * dx, dx) # time axis ot = 0 nt = 1081 dt = 0.0025 t = np.arange(ot, ot + nt * dt, dt) # Receivers r = np.loadtxt(path0 + 'r.dat', delimiter=',') nr = r.shape[1] # Sources s = np.loadtxt(path0 + 's.dat', delimiter=',') ns = s.shape[1] ds = s[0, 1] - s[0, 0] # restriction operators iava = np.loadtxt(path0 + 'select_rfrac70.dat', delimiter=',', dtype=int) - 1 Restrop = Restriction(ns * (2 * nt - 1), iava, dims=(ns, 2 * nt - 1), dir=0, dtype='float64') # data print('Loading reflection data...') R = np.zeros((nt, ns, nr), 'f') for isrc in range(ns - 1): is_ = isrc * jr R[:, :, isrc] = np.loadtxt(path0 + 'R/dat1_' + str(is_) + '.dat', delimiter=',') R = 2 * np.swapaxes(R, 0, 2) # wavelet wav = np.loadtxt(path0 + 'wav.dat', delimiter=',') wav_c = wav[np.argmax(wav) - 60:np.argmax(wav) + 60] W = np.abs(np.fft.rfft(wav, nfft)) * dt # Convolution operators print('Creating MDC operators...') Rtwosided = np.concatenate((np.zeros((nr, ns, nt - 1)), R), axis=-1) R1twosided = np.concatenate((np.flip(R, axis=-1), np.zeros( (nr, ns, nt - 1))), axis=-1) Rtwosided_fft = np.fft.rfft(Rtwosided, 2 * nt - 1, axis=-1) / np.sqrt(2 * nt - 1) Rtwosided_fft = Rtwosided_fft[..., :nfmax] R1twosided_fft = np.fft.rfft(R1twosided, 2 * nt - 1, axis=-1) / np.sqrt(2 * nt - 1) R1twosided_fft = R1twosided_fft[..., :nfmax] Rop = MDC(Rtwosided_fft[iava], nt=2 * nt - 1, nv=1, dt=dt, dr=ds, twosided=True, dtype='complex64') R1op = MDC(R1twosided_fft[iava], nt=2 * nt - 1, nv=1, dt=dt, dr=ds, twosided=True, dtype='complex64') del wav, R, Rtwosided, R1twosided, Rtwosided_fft, R1twosided_fft # configuring radon transform nwin = 23 nwins = 14 nover = 10 npx = 101 pxmax = 0.006 px = np.linspace(-pxmax, pxmax, npx) t2 = np.concatenate([-t[::-1], t[1:]]) nt2 = t2.shape[0] dimsd = (nr, nt2) dimss = (nwins * npx, dimsd[1]) # sliding window radon with overlap RadOp = Radon2D(t2, np.linspace(-ds * nwin // 2, ds * nwin // 2, nwin), px, centeredh=True, kind='linear', engine='numba') Slidop = Sliding2D(RadOp, dimss, dimsd, nwin, nover, tapertype='cosine', design=True) Sparseop = BlockDiag([Slidop, Slidop]) del nwin, nwins, nover, npx, pxmax, px, t2, nt2, dimsd, dimss, RadOp, Slidop # the loop print('Entering loop...') z_steps = np.arange(zinvs, zendvs + 1, dvsz) for z_current in z_steps: redatuming_wrapper(toff, W, wav_c, iava, Rop, R1op, Restrop, Sparseop, vsx, vsz, x, z, z_current, nt, dt, nfft, nr, ds, dvsx)
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=',')