示例#1
0
def test_FunctionOperator_NoAdjoint(par):
    """Forward and adjoint for FunctionOperator operator where the adjoint
    is not implemented.
    """
    np.random.seed(10)
    G = (np.random.normal(0, 1, (par['nr'], par['nc'])) +
         np.random.normal(0, 1, (par['nr'], par['nc'])) * par['imag']).astype(
             par['dtype'])

    def forward_f(x):
        return G @ x

    if par['nr'] == par['nc']:
        Fop = FunctionOperator(forward_f, par['nr'], dtype=par['dtype'])
    else:
        Fop = FunctionOperator(forward_f,
                               par['nr'],
                               par['nc'],
                               dtype=par['dtype'])

    x = (np.ones(par['nc']) + np.ones(par['nc']) * par['imag']).astype(
        par['dtype'])
    y = (np.ones(par['nr']) + np.ones(par['nr']) * par['imag']).astype(
        par['dtype'])

    F_x = Fop @ x
    G_x = np.asarray(G @ x)
    assert_array_equal(F_x, G_x)

    # check error is raised when applying the adjoint
    with pytest.raises(NotImplementedError):
        _ = Fop.H @ y
def test_FunctionOperator_NoAdjoint(par):
    """Forward and adjoint for FunctionOperator operator where the adjoint
    is not implemented.
    """
    np.random.seed(10)
    G = np.matrix(np.random.normal(0, 1, (par['nr'], par['nc'])) +
                  np.random.normal(0, 1, (par['nr'], par['nc'])) * par['imag'],
                  dtype=par['dtype'])

    def forward_f(x):
        return G @ x

    if par['nr'] == par['nc']:
        Fop = FunctionOperator(forward_f, par['nr'], dtype=par['dtype'])
    else:
        Fop = FunctionOperator(forward_f,
                               par['nr'],
                               par['nc'],
                               dtype=par['dtype'])

    x = (np.ones(par['nc']) + np.ones(par['nc']) * par['imag']).astype(
        par['dtype'])
    y = (np.ones(par['nr']) + np.ones(par['nr']) * par['imag']).astype(
        par['dtype'])

    F_x = Fop @ x
    G_x = np.squeeze(np.asarray(G @ x))
    assert_array_equal(F_x, G_x)

    try:
        FH_x = Fop.H @ y
        GH_x = G.H @ y
        assert_array_equal(FH_x, GH_x)
    except NotImplementedError:
        pass
示例#3
0
def test_FunctionOperator(par):
    """Dot-test and inversion for FunctionOperator operator."""
    print(par)
    np.random.seed(10)
    G = (np.random.normal(0, 1, (par["nr"], par["nc"])) +
         np.random.normal(0, 1, (par["nr"], par["nc"])) * par["imag"]).astype(
             par["dtype"])

    def forward_f(x):
        return G @ x

    def adjoint_f(y):
        return np.conj(G.T) @ y

    if par["nr"] == par["nc"]:
        Fop = FunctionOperator(forward_f,
                               adjoint_f,
                               par["nr"],
                               dtype=par["dtype"])
    else:
        Fop = FunctionOperator(forward_f,
                               adjoint_f,
                               par["nr"],
                               par["nc"],
                               dtype=par["dtype"])

    assert dottest(
        Fop,
        par["nr"],
        par["nc"],
        complexflag=0 if par["imag"] == 0 else 3,
        tol=par["tol"],
    )

    x = (np.ones(par["nc"]) + np.ones(par["nc"]) * par["imag"]).astype(
        par["dtype"])
    y = (np.ones(par["nr"]) + np.ones(par["nr"]) * par["imag"]).astype(
        par["dtype"])

    F_x = Fop @ x
    FH_y = Fop.H @ y

    G_x = np.asarray(G @ x)
    GH_y = np.asarray(np.conj(G.T) @ y)

    assert_array_equal(F_x, G_x)
    assert_array_equal(FH_y, GH_y)

    # Only test inversion for square or overdetermined systems
    if par["nc"] <= par["nr"]:
        xlsqr = lsqr(Fop, F_x, damp=0, iter_lim=100, show=0)[0]
        assert_array_almost_equal(x, xlsqr, decimal=4)
def test_FunctionOperator(par):
    """Dot-test and inversion for FunctionOperator operator.
    """
    np.random.seed(10)
    G = np.matrix(np.random.normal(0, 1, (par['nr'], par['nc'])) +
                  np.random.normal(0, 1, (par['nr'], par['nc'])) * par['imag'],
                  dtype=par['dtype'])

    def forward_f(x):
        return G @ x

    def adjoint_f(y):
        return G.H @ y

    if par['nr'] == par['nc']:
        Fop = FunctionOperator(forward_f,
                               adjoint_f,
                               par['nr'],
                               dtype=par['dtype'])
    else:
        Fop = FunctionOperator(forward_f,
                               adjoint_f,
                               par['nr'],
                               par['nc'],
                               dtype=par['dtype'])

    assert dottest(Fop,
                   par['nr'],
                   par['nc'],
                   complexflag=0 if par['imag'] == 0 else 3)

    x = (np.ones(par['nc']) + np.ones(par['nc']) * par['imag']).astype(
        par['dtype'])
    y = (np.ones(par['nr']) + np.ones(par['nr']) * par['imag']).astype(
        par['dtype'])

    F_x = Fop @ x
    FH_y = Fop.H @ y

    G_x = np.squeeze(np.asarray(G @ x))
    GH_y = np.squeeze(np.asarray(G.H @ y))

    assert_array_equal(F_x, G_x)
    assert_array_equal(FH_y, GH_y)

    # Only test inversion for square or overdetermined systems
    if par['nc'] <= par['nr']:
        xlsqr = lsqr(Fop, F_x, damp=0, iter_lim=100, show=0)[0]
        assert_array_almost_equal(x, xlsqr, decimal=4)
def test_predict_trace(par):
    """Dot-test for _predict_trace operator
    """
    t = np.arange(par['nt']) * par['dt']
    for slope in [-0.2, 0., 0.3]:
        Fop = FunctionOperator(
            lambda x: _predict_trace(x, t, par['dt'], par['dx'], slope), lambda
            x: _predict_trace(x, t, par['dt'], par['dx'], slope, adj=True),
            par['nt'], par['nt'])
        dottest(Fop, par['nt'], par['nt'])
示例#6
0
def test_predict_trace(par):
    """Dot-test for _predict_trace operator"""
    t = np.arange(par["nt"]) * par["dt"]
    for slope in [-0.2, 0.0, 0.3]:
        Fop = FunctionOperator(
            lambda x: _predict_trace(x, t, par["dt"], par["dx"], slope),
            lambda x: _predict_trace(x, t, par["dt"], par["dx"], slope, adj=True),
            par["nt"],
            par["nt"],
        )
        dottest(Fop, par["nt"], par["nt"])
def test_predict(par):
    """Dot-test for _predict operator
    """
    def _predict_reshape(predictor,
                         traces,
                         nt,
                         nx,
                         dt,
                         dx,
                         slopes,
                         repeat=0,
                         backward=False,
                         adj=False):
        return predictor(traces.reshape(nx, nt),
                         dt,
                         dx,
                         slopes,
                         repeat=repeat,
                         backward=backward,
                         adj=adj)

    for predictor in (_predict_haar, _predict_lin):
        for repeat in (0, 1, 2):
            slope = \
                np.random.normal(0, .1, (2 ** (repeat + 1) * par['nx'],
                                         par['nt']))
            for backward in (False, True):
                Fop = FunctionOperator(
                    lambda x: _predict_reshape(predictor,
                                               x,
                                               par['nt'],
                                               par['nx'],
                                               par['dt'],
                                               par['dx'],
                                               slope,
                                               backward=backward),
                    lambda x: _predict_reshape(predictor,
                                               x,
                                               par['nt'],
                                               par['nx'],
                                               par['dt'],
                                               par['dx'],
                                               slope,
                                               backward=backward,
                                               adj=True),
                    par['nt'] * par['nx'], par['nt'] * par['nx'])
                dottest(Fop, par['nt'] * par['nx'], par['nt'] * par['nx'])