コード例 #1
0
def test_confinter_linear():
    #=======================================================================
    "Check that the confidence intervals of the linear parameters are correct"

    # Prepare test data
    r = np.linspace(1, 8, 150)
    t = np.linspace(0, 4, 200)
    lam = 0.25
    K = dipolarkernel(t, r, mod=lam)
    parin = [3.5, 0.4, 0.6, 4.5, 0.5, 0.4]
    P = dd_gauss2(r, *parin)
    V = K @ P + whitegaussnoise(t, 0.05, seed=1)

    # Non-linear parameters
    # nlpar = [lam]
    nlpar0 = 0.2
    lb = 0
    ub = 1
    # Linear parameters: non-negativity
    lbl = np.zeros(len(r))
    ubl = np.full(len(r), np.inf)
    # Separable LSQ fit
    fit = snlls(V, lambda lam: dipolarkernel(t, r, mod=lam), nlpar0, lb, ub,
                lbl)
    Pfit = np.round(fit.lin, 6)
    uq = fit.linUncert
    Pci50 = np.round(uq.ci(50), 6)
    Pci95 = np.round(uq.ci(95), 6)

    assert_confidence_intervals(Pci50, Pci95, Pfit, lbl, ubl)
コード例 #2
0
def test_multiple_datasets():
    # ======================================================================
    "Check bootstrapping when using multiple input datasets"

    t1 = np.linspace(0, 5, 200)
    t2 = np.linspace(-0.5, 3, 300)
    r = np.linspace(2, 6, 300)
    P = dd_gauss(r, 4, 0.8)
    K1 = dipolarkernel(t1, r)
    K2 = dipolarkernel(t2, r)

    Vexp1 = K1 @ P + whitegaussnoise(t1, 0.01, seed=1)
    Vexp2 = K2 @ P + whitegaussnoise(t2, 0.02, seed=2)

    def Vmodel(par):
        V1 = K1 @ dd_gauss(r, *par)
        V2 = K2 @ dd_gauss(r, *par)
        return [V1, V2]

    par0 = [3, 0.5]
    fit = snlls([Vexp1, Vexp2], Vmodel, par0)
    Vfit1, Vfit2 = fit.model

    def bootfcn(V):
        fit = snlls(V, Vmodel, par0)
        return fit.nonlin

    paruq = bootstrap_analysis(bootfcn, [Vexp1, Vexp2], [Vfit1, Vfit2], 5)

    assert all(abs(paruq.mean - fit.nonlin) < 1.5e-2)
コード例 #3
0
def test_globalfit():
    # ======================================================================
    "Check that global fitting yields correct results"

    r = np.linspace(2, 5, 200)
    par = np.array([3, 0.2])
    P = dd_gauss(r, *par)

    t1 = np.linspace(0, 3, 300)
    K1 = dipolarkernel(t1, r)
    V1 = K1 @ P

    t2 = np.linspace(-0.5, 4, 200)
    K2 = dipolarkernel(t2, r)
    V2 = K2 @ P

    def Vmodel(par):
        Pfit = dd_gauss(r, *par)
        V1 = K1 @ Pfit
        V2 = K2 @ Pfit
        return [V1, V2]

    par0 = [5, 0.5]
    lb = [1, 0.1]
    ub = [20, 1]
    fit = snlls([V1, V2], Vmodel, par0, lb, ub)

    assert all(abs(par - fit.nonlin) < 1e-2)
コード例 #4
0
def assert_solver(solver):
    # Prepare test data
    r = np.linspace(1, 8, 80)
    t = np.linspace(0, 4, 200)
    lam = 0.25
    K = dipolarkernel(t, r, mod=lam)
    parin = [3.5, 0.4, 0.6, 4.5, 0.5, 0.4]
    P = dd_gauss2(r, *parin)
    V = K @ P

    # Non-linear parameters
    # nlpar = [lam]
    nlpar0 = 0.2
    lb = 0
    ub = 1
    # Linear parameters: non-negativity
    lbl = np.zeros(len(r))
    ubl = []
    # Separable LSQ fit
    fit = snlls(V,
                lambda lam: dipolarkernel(t, r, mod=lam),
                nlpar0,
                lb,
                ub,
                lbl,
                ubl,
                nnlsSolver=solver,
                uq=False)
    Pfit = fit.lin

    assert ovl(P, Pfit) > 0.95
コード例 #5
0
def test_globalfit_scales():
    #============================================================
    "Check that the global fit with arbitrary amplitudes works."
    t1 = np.linspace(0, 5, 300)
    t2 = np.linspace(0, 2, 300)
    r = np.linspace(3, 5, 100)
    P = dd_gauss(r, 4, 0.25)
    K1 = dipolarkernel(t1, r)
    K2 = dipolarkernel(t2, r)
    scales = [1e3, 1e9]
    V1 = scales[0] * K1 @ P
    V2 = scales[1] * K2 @ P

    def Vmodel(par):
        Pfit = dd_gauss(r, *par)
        V1 = K1 @ Pfit
        V2 = K2 @ Pfit
        return block_diag(V1, V2).T

    par0 = [5, 0.5]
    lb = [1, 0.1]
    ub = [20, 1]
    fit = snlls(np.concatenate([V1, V2]), Vmodel, par0, lb, ub)

    assert max(abs(np.asarray(scales) / fit.lin - 1)) < 1e-2
コード例 #6
0
def test_goodness_of_fit():
    #============================================================
    "Check the goodness-of-fit statistics are correct"

    # Prepare test data
    r = np.linspace(2, 5, 150)
    t = np.linspace(-0.2, 4, 100)
    lam = 0.25
    K = dipolarkernel(t, r, mod=lam)
    parin = [3.5, 0.15, 0.6, 4.5, 0.2, 0.4]
    P = dd_gauss2(r, *parin)
    sigma = 0.03
    V = K @ P + whitegaussnoise(t, sigma, seed=2, rescale=True)

    # Non-linear parameters
    # nlpar = [lam]
    nlpar0 = 0.2
    lb = 0
    ub = 1
    # Linear parameters: non-negativity
    lbl = np.zeros(len(r))
    ubl = []
    # Separable LSQ fit
    fit = snlls(V,
                lambda lam: dipolarkernel(t, r, mod=lam),
                nlpar0,
                lb,
                ub,
                lbl,
                ubl,
                noiselvl=sigma,
                uq=False)
    stats = fit.stats

    assert abs(stats['chi2red'] - 1) < 0.05
コード例 #7
0
def test_plot():
    # ======================================================================
    "Check that the plot method works"

    # Prepare test data
    r = np.linspace(1, 8, 80)
    t = np.linspace(0, 4, 200)
    lam = 0.25
    K = dipolarkernel(t, r, mod=lam)
    parin = [3.5, 0.4, 0.6, 4.5, 0.5, 0.4]
    P = dd_gauss2(r, *parin)
    V = K @ P
    # Linear parameters: non-negativity
    lbl = np.zeros(len(r))
    # Separable LSQ fit
    fit = snlls(V,
                lambda lam: dipolarkernel(t, r, mod=lam),
                par0=0.2,
                lb=0,
                ub=1,
                lbl=lbl,
                uq=False)
    fig = fit.plot(show=False)

    assert str(fig.__class__) == "<class 'matplotlib.figure.Figure'>"
コード例 #8
0
def test_reg_tikhonov():
    #============================================================
    "Check that Tikhonov regularization of linear problem works"

    # Prepare test data
    r = np.linspace(1, 8, 80)
    t = np.linspace(0, 4, 100)
    lam = 0.25
    K = dipolarkernel(t, r, mod=lam)
    parin = [3.5, 0.4, 0.6, 4.5, 0.5, 0.4]
    P = dd_gauss2(r, *parin)
    V = K @ P

    # Non-linear parameters
    # nlpar = [lam]
    nlpar0 = 0.2
    lb = 0
    ub = 1
    # Linear parameters: non-negativity
    lbl = np.zeros(len(r))
    # Separable LSQ fit
    fit = snlls(V,
                lambda lam: dipolarkernel(t, r, mod=lam),
                nlpar0,
                lb,
                ub,
                lbl,
                uq=False)
    Pfit = fit.lin

    assert ovl(P, Pfit) > 0.95
コード例 #9
0
def test_cost_value():
    #============================================================
    "Check that the cost value is properly returned"

    # Prepare test data
    r = np.linspace(1, 8, 80)
    t = np.linspace(0, 4, 200)
    lam = 0.25
    K = dipolarkernel(t, r, mod=lam)
    parin = [3.5, 0.4, 0.6, 4.5, 0.5, 0.4]
    P = dd_gauss2(r, *parin)
    V = K @ P
    # Non-linear parameters
    nlpar0 = 0.2
    lb = 0
    ub = 1
    # Linear parameters: non-negativity
    lbl = np.zeros(len(r))
    ubl = np.full(len(r), np.inf)
    # Separable LSQ fit
    fit = snlls(V, lambda lam: dipolarkernel(t, r, mod=lam), nlpar0, lb, ub,
                lbl, ubl)

    assert isinstance(fit.cost, float) and np.round(
        fit.cost / np.sum(fit.residuals**2), 5) == 1
コード例 #10
0
def test_multiple_penalties():
    # ======================================================================
    "Check that multiple additional penaltyies can be passed correctly"

    t = np.linspace(0, 5, 300)
    r = np.linspace(2, 6, 90)
    P = dd_gauss(r, 4.5, 0.25)
    param = 0.2
    K = dipolarkernel(t, r, mod=param)
    V = K @ P + whitegaussnoise(t, 0.001, seed=1)
    dr = np.mean(np.diff(r))
    beta = 0.05
    R = 0.5
    compactness_penalty = lambda pnonlin, plin: beta * np.sqrt(plin * (
        r - np.trapz(plin * r, r))**2 * dr)
    radial_penalty = lambda pnonlin, plin: 1 / R**2 * (np.linalg.norm(
        (pnonlin - param) / param - R))**2

    Kmodel = lambda lam: dipolarkernel(t, r, mod=lam)
    fit0 = snlls(V,
                 Kmodel,
                 par0=0.2,
                 lb=0,
                 ub=1,
                 lbl=np.zeros_like(r),
                 extrapenalty=[compactness_penalty])
    fitmoved = snlls(V,
                     Kmodel,
                     par0=0.2,
                     lb=0,
                     ub=1,
                     lbl=np.zeros_like(r),
                     extrapenalty=[compactness_penalty, radial_penalty])

    assert ovl(P, fit0.lin) > ovl(P, fitmoved.lin)
コード例 #11
0
def test_confinter_model():
    #=======================================================================
    "Check that the confidence intervals of the fitted model are correct"

    # Prepare test data
    r = np.linspace(1, 8, 150)
    t = np.linspace(0, 4, 200)
    lam = 0.25
    K = dipolarkernel(t, r, mod=lam)
    parin = [3.5, 0.4, 0.6, 4.5, 0.5, 0.4]
    P = dd_gauss2(r, *parin)
    V = K @ P + whitegaussnoise(t, 0.05, seed=1)

    nlpar0 = 0.2
    lb = 0
    ub = 1
    lbl = np.full(len(r), 0)
    # Separable LSQ fit
    fit = snlls(V, lambda lam: dipolarkernel(t, r, mod=lam), nlpar0, lb, ub,
                lbl)
    Vfit = fit.model
    Vuq = fit.modelUncert
    Vci50 = Vuq.ci(50)
    Vci95 = Vuq.ci(95)

    Vlb = np.full(len(t), -np.inf)
    Vub = np.full(len(t), np.inf)

    assert_confidence_intervals(Vci50, Vci95, Vfit, Vlb, Vub)
コード例 #12
0
def test_confinter_nonlinear():
    #=======================================================================
    "Check that the confidence intervals of the non-linear parameters are correct"

    # Prepare test data
    r = np.linspace(1, 8, 80)
    t = np.linspace(0, 4, 200)
    lam = 0.25
    K = dipolarkernel(t, r, mod=lam)
    parin = [3.5, 0.4, 0.6, 4.5, 0.5, 0.4]
    P = dd_gauss2(r, *parin)
    V = K @ P

    # Non-linear parameters
    # nlpar = [lam]
    nlpar0 = 0.2
    lb = 0
    ub = 1
    # Linear parameters: non-negativity
    lbl = np.zeros(len(r))
    ubl = np.full(len(r), np.inf)

    # Separable LSQ fit
    fit = snlls(V, lambda lam: dipolarkernel(t, r, mod=lam), nlpar0, lb, ub,
                lbl, ubl)
    parfit = fit.nonlin
    uq = fit.nonlinUncert
    parci50 = np.atleast_2d(uq.ci(50))
    parci95 = np.atleast_2d(uq.ci(95))

    assert_confidence_intervals(parci50, parci95, parfit, lb, ub)
コード例 #13
0
def test_confinter_scaling():
    #============================================================
    "Check that the confidence intervals are agnostic w.r.t. scaling"

    # Prepare test data
    r = np.linspace(1, 8, 80)
    t = np.linspace(0, 4, 50)
    lam = 0.25
    K = dipolarkernel(t, r, mod=lam)
    parin = [3.5, 0.4, 0.6, 4.5, 0.5, 0.4]
    P = dd_gauss2(r, *parin)
    V = K @ P + whitegaussnoise(t, 0.01, seed=1)
    # Non-linear parameters
    nlpar0 = 0.2
    lb = 0
    ub = 1
    # Linear parameters: non-negativity
    lbl = np.zeros(len(r))
    V0_1 = 1
    V0_2 = 1e8

    # Separable LSQ fit
    fit1 = snlls(V * V0_1,
                 lambda lam: dipolarkernel(t, r, mod=lam),
                 nlpar0,
                 lb,
                 ub,
                 lbl,
                 nonlin_tol=1e-3)
    fit2 = snlls(V * V0_2,
                 lambda lam: dipolarkernel(t, r, mod=lam),
                 nlpar0,
                 lb,
                 ub,
                 lbl,
                 nonlin_tol=1e-3)

    # Assess linear parameter uncertainties
    ci1 = fit1.linUncert.ci(95)
    ci2 = fit2.linUncert.ci(95)
    ci1[ci1 == 0] = 1e-16
    ci2[ci2 == 0] = 1e-16

    assert np.max(abs(ci2 / V0_2 - ci1)) < 1e-6

    # Assess nonlinear parameter uncertainties
    ci1 = fit1.nonlinUncert.ci(95)
    ci2 = fit2.nonlinUncert.ci(95)
    ci1[ci1 == 0] = 1e-16
    ci2[ci2 == 0] = 1e-16

    assert np.max(abs(ci2 - ci1)) < 1e-6
コード例 #14
0
def generate_global_dataset():

    t1 = np.linspace(-0.5, 3, 200)
    t2 = np.linspace(-0.5, 4, 200)
    r = np.linspace(2.5, 5, 80)
    P = dd_gauss2(r, 3.7, 4.3, 0.2, 0.1, 0.5, 0.5)
    P /= np.trapz(P, r)
    K1 = dipolarkernel(t1, r)
    K2 = dipolarkernel(t2, r)
    np.random.seed(1)
    V1 = K1 @ P + whitegaussnoise(t1, 0.01, seed=1, rescale=True)
    np.random.seed(2)
    V2 = K2 @ P + whitegaussnoise(t2, 0.01, seed=1, rescale=True)

    return r, P, V1, V2, K1, K2
コード例 #15
0
def assert_full_output(method):

    t = np.linspace(0, 5, 80)
    r = np.linspace(2, 5, 80)
    P = dd_gauss(r, 3, 0.4)
    K = dipolarkernel(t, r)
    L = regoperator(r, 2)
    V = K @ P

    alpha, alphas_evaled, functional, residuals, penalties = selregparam(
        V,
        K,
        cvxnnls,
        method='aic',
        algorithm=method,
        full_output=True,
        regop=L)
    errors = []
    if np.size(alpha) != 1:
        errors.append("alphaopt is not a scalar")
    if len(functional) != len(alphas_evaled):
        errors.append(
            "The number of elements of functional values and evaluated alphas are different."
        )
    if len(residuals) != len(penalties):
        errors.append(
            "The number of elements of evluated residuals and penalties are different"
        )
    if not alpha in alphas_evaled:
        errors.append("The optimal alpha is not part of the evaluated alphas")
    assert not errors, f"Errors occured:\n{chr(10).join(errors)}"
コード例 #16
0
def test_extrapenalty():
    # ======================================================================
    "Check that custom penalties can be passed and act on the solution"

    t = np.linspace(0, 3, 300)
    r = np.linspace(2, 5, 200)
    P = dd_gauss2(r, 3.5, 0.5, 0.5, 4, 0.1, 0.5)
    K = dipolarkernel(t, r)
    V = K @ P + whitegaussnoise(t, 0.15, seed=1)

    par0 = [2.5, 0.01, 0.1, 4.5, 0.01, 0.6]
    lb = [1, 0.01, 0, 1, 0.01, 0]
    ub = [20, 1, 1, 20, 1, 1]
    # Fit case it fails, stuck at "spicky" Gaussians
    model = lambda p: K @ dd_gauss2(r, *p)
    fit = snlls(V, model, par0, lb, ub)

    # Fit with Tikhonov penalty on the Gaussians model
    L = regoperator(r, 2)
    alpha = 1e-4
    tikhonov = lambda p, _: alpha * L @ dd_gauss2(r, *p)
    fit_tikh = snlls(V, model, par0, lb, ub, extrapenalty=tikhonov)

    Pfit = dd_gauss2(r, *fit.nonlin)
    Pfit_tikh = dd_gauss2(r, *fit_tikh.nonlin)

    assert ovl(P, Pfit) < ovl(P, Pfit_tikh)
コード例 #17
0
def test_algorithms():
    #=======================================================================
    "Check that the value returned by the the grid and Brent algorithms coincide"

    t = np.linspace(0, 5, 80)
    r = np.linspace(2, 5, 80)
    P = dd_gauss(r, 3, 0.2)
    K = dipolarkernel(t, r)
    L = regoperator(r, 2)
    V = K @ P + whitegaussnoise(t, 0.02, seed=1)

    alpha_grid = selregparam(V,
                             K,
                             cvxnnls,
                             method='aic',
                             algorithm='grid',
                             regop=L)
    alpha_brent = selregparam(V,
                              K,
                              cvxnnls,
                              method='aic',
                              algorithm='brent',
                              regop=L)

    assert abs(1 - alpha_grid / alpha_brent) < 0.15
コード例 #18
0
def assert_multigauss_SNLLS_problem(nonlinearconstr=True, linearconstr=True):
    # Prepare test data
    r = np.linspace(1, 8, 80)
    t = np.linspace(0, 4, 200)
    K = dipolarkernel(t, r)
    parin = [3.5, 0.4, 0.6, 4.5, 0.5, 0.4]
    P = dd_gauss2(r, *parin)
    V = K @ P

    def Kmodel(p, t, r):
        # Unpack parameters
        r1, w1, r2, w2 = p
        # Generate basic kernel
        K0 = dipolarkernel(t, r)
        # Get Gauss basis functions
        P1 = dd_gauss(r, r1, w1)
        P2 = dd_gauss(r, r2, w2)
        # Combine all non-linear functions into one
        K = np.zeros((len(t), 2))
        K[:, 0] = K0 @ P1
        K[:, 1] = K0 @ P2
        return K

    # Non-linear parameters
    # nlpar = [r1 w1 r2 w2]
    nlpar0 = [3.2, 0.2, 4.2, 0.3]
    if nonlinearconstr:
        lb = [1, 0.1, 1, 0.1]
        ub = [20, 5, 20, 5]
    else:
        lb = []
        ub = []
    # Linear parameters
    if linearconstr:
        lbl = [0, 0]
        ubl = [1, 1]
    else:
        lbl = []
        ubl = []

    # Separable LSQ fit
    fit = snlls(V,
                lambda p: Kmodel(p, t, r),
                nlpar0,
                lb,
                ub,
                lbl,
                ubl,
                reg=False,
                uq=False)
    nonlinfit = fit.nonlin
    linfit = fit.lin
    parout = [
        nonlinfit[0], nonlinfit[1], linfit[0], nonlinfit[2], nonlinfit[3],
        linfit[1]
    ]
    parout = np.asarray(parout)
    parin = np.asarray(parin)
    assert np.max(abs(parout - parin) < 1e-1)
コード例 #19
0
def test_global_weights_default():
    # ======================================================================
    "Check the correct fit of two signals when one is of very low quality"

    t = np.linspace(0, 5, 300)
    r = np.linspace(2, 6, 90)
    param = [4.5, 0.25]
    P = dd_gauss(r, *param)

    K = dipolarkernel(t, r, mod=0.2)
    scales = [1e3, 1e9]
    V1 = scales[0] * (K @ P + whitegaussnoise(t, 0.001, seed=1))
    V2 = scales[1] * (K @ P + whitegaussnoise(t, 0.1, seed=1))

    Kmodel = lambda lam: [dipolarkernel(t, r, mod=lam)] * 2
    fit = snlls([V1, V2], Kmodel, par0=[0.2], lb=0, ub=1, lbl=np.zeros_like(r))

    assert ovl(P, fit.lin) > 0.93
コード例 #20
0
def get_alpha_from_method(method):

    t = np.linspace(0, 5, 500)
    r = np.linspace(2, 5, 80)
    P = dd_gauss(r, 3.0, 0.16986436005760383)
    K = dipolarkernel(t, r)
    L = regoperator(r, 2, includeedges=True)
    V = K @ P

    alpha = selregparam(V, K, cvxnnls, method=method, noiselvl=0, regop=L)
    return np.log10(alpha)
コード例 #21
0
def test_compensate_condition():
    #=======================================================================
    "Check that alpha compensates for larger condition numbers"

    r = np.linspace(2, 6, 100)
    P = dd_gauss(r, 3, 0.2)

    # Lower condition number
    t1 = np.linspace(0, 3, 200)
    K1 = dipolarkernel(t1, r)
    V1 = K1 @ P
    alpha1 = selregparam(V1, K1, cvxnnls, method='aic')

    # Larger condition number
    t2 = np.linspace(0, 3, 400)
    K2 = dipolarkernel(t2, r)
    V2 = K2 @ P
    alpha2 = selregparam(V2, K2, cvxnnls, method='aic')

    assert alpha2 > alpha1
コード例 #22
0
def assert_solver(solver):
    #============================================================

    np.random.seed(1)
    t = np.linspace(-2, 4, 300)
    r = np.linspace(2, 6, 100)
    P = dd_gauss(r, 3, 0.2)
    K = dipolarkernel(t, r)
    V = K @ P + whitegaussnoise(t, 0.01)
    fit = snlls(V, K, lbl=np.zeros_like(r), nnlsSolver=solver, uq=False)

    assert ovl(P, fit.param) > 0.95  # more than 95% overlap
コード例 #23
0
def test_confinter_tikh():
    #============================================================
    "Check that the confidence intervals are correctly computed using Tikhonov regularization"

    t = np.linspace(-2, 4, 300)
    r = np.linspace(2, 6, 100)
    P = dd_gauss(r, 3, 0.2)
    K = dipolarkernel(t, r)
    V = K @ P

    fit = snlls(V, K, lbl=np.zeros_like(r))

    assert_confidence_intervals(fit.paramUncert, fit.param)
コード例 #24
0
def test_confinter_Vfit():
    #============================================================
    "Check that the confidence intervals are correctly for the fitted signal"

    t = np.linspace(-2, 4, 300)
    r = np.linspace(2, 6, 100)
    P = dd_gauss(r, 3, 0.2)
    K = dipolarkernel(t, r, mod=0.2)
    V = K @ P + whitegaussnoise(t, 0.05)

    fit = snlls(V, K, lbl=np.zeros_like(r))

    assert_confidence_intervals(fit.modelUncert, fit.model)
コード例 #25
0
def test_nonuniform_r():
    #============================================================
    "Check that fit works correctly with non-uniform distance vectors"

    t = np.linspace(0, 4, 300)
    r = np.sqrt(np.linspace(1, 7**2, 200))
    P = dd_gauss(r, 3, 0.2)
    K = dipolarkernel(t, r)
    V = K @ P

    fit = snlls(V, K, lbl=np.zeros_like(r))
    Vfit = K @ fit.param
    assert abs(Vfit[0] - 1) < 1e-6
コード例 #26
0
def test_plot():
    #============================================================
    "Check the plotting method"

    t = np.linspace(0, 3, 200)
    r = np.linspace(1, 5, 100)
    P = dd_gauss(r, 3, 0.08)
    K = dipolarkernel(t, r)
    V = K @ P

    fit = snlls(V, K, lbl=np.zeros_like(r))
    fig = fit.plot(show=False)
    assert str(fig.__class__) == "<class 'matplotlib.figure.Figure'>"
コード例 #27
0
def test_convergence_criteria():
    #============================================================
    "Check that convergence criteria can be specified without crashing"

    t = np.linspace(0, 3, 200)
    r = np.linspace(1, 5, 100)
    P = dd_gauss(r, 3, 0.08)
    K = dipolarkernel(t, r)
    V = K @ P

    fit = snlls(V, K, lin_tol=1e-9, lin_maxiter=2e3, lbl=np.zeros_like(r))

    assert ovl(P, fit.param) > 0.90  # more than 80% overlap
コード例 #28
0
 def Kmodel(p, t, r):
     # Unpack parameters
     r1, w1, r2, w2 = p
     # Generate basic kernel
     K0 = dipolarkernel(t, r)
     # Get Gauss basis functions
     P1 = dd_gauss(r, r1, w1)
     P2 = dd_gauss(r, r2, w2)
     # Combine all non-linear functions into one
     K = np.zeros((len(t), 2))
     K[:, 0] = K0 @ P1
     K[:, 1] = K0 @ P2
     return K
コード例 #29
0
def test_global_weights():
    # ======================================================================
    "Check that the global weights properly work when specified"

    t = np.linspace(-0.3, 5, 300)
    r = np.linspace(2, 6, 150)

    P1 = dd_gauss(r, 3, 0.2)
    P2 = dd_gauss(r, 5, 0.2)

    K = dipolarkernel(t, r, mod=0.2)

    scales = [1e3, 1e9]
    sigma1 = 0.001
    V1 = K @ P1 + whitegaussnoise(t, sigma1, seed=1)
    sigma2 = 0.001
    V2 = K @ P2 + whitegaussnoise(t, sigma2, seed=1)

    V1 = scales[0] * V1
    V2 = scales[1] * V2

    Kmodel = lambda lam: [dipolarkernel(t, r, mod=lam)] * 2
    fit1 = snlls([V1, V2],
                 Kmodel,
                 par0=[0.2],
                 lb=0,
                 ub=1,
                 lbl=np.zeros_like(r),
                 weights=[1, 1e-10])
    fit2 = snlls([V1, V2],
                 Kmodel,
                 par0=[0.2],
                 lb=0,
                 ub=1,
                 lbl=np.zeros_like(r),
                 weights=[1e-10, 1])

    assert ovl(P1, fit1.lin) > 0.93 and ovl(P2, fit2.lin) > 0.93
コード例 #30
0
def test_regularized_global():
    #=======================================================================
    "Check global SNLLS of a nonlinear-constrained + linear-regularized problem"

    t1 = np.linspace(0, 3, 150)
    t2 = np.linspace(0, 4, 200)
    r = np.linspace(2.5, 5, 80)
    P = dd_gauss2(r, 3.7, 0.5, 0.5, 4.3, 0.3, 0.5)
    kappa = 0.50
    lam1 = 0.25
    lam2 = 0.35
    K1 = dipolarkernel(t1, r, mod=lam1, bg=bg_exp(t1, kappa))
    K2 = dipolarkernel(t2, r, mod=lam2, bg=bg_exp(t2, kappa))
    V1 = K1 @ P
    V2 = K2 @ P

    # Global non-linear model
    def globalKmodel(par):
        # Unpack parameters
        kappa, lam1, lam2 = par
        K1 = dipolarkernel(t1, r, mod=lam1, bg=bg_exp(t1, kappa))
        K2 = dipolarkernel(t2, r, mod=lam2, bg=bg_exp(t2, kappa))
        return K1, K2

    # Non-linear parameters
    # [kappa lambda1 lambda2]
    par0 = [0.5, 0.5, 0.5]
    lb = [0, 0, 0]
    ub = [1, 1, 1]
    # Linear parameters: non-negativity
    lbl = np.zeros(len(r))
    ubl = []

    # Separable LSQ fit
    fit = snlls([V1, V2], globalKmodel, par0, lb, ub, lbl, ubl, uq=False)
    Pfit = fit.lin

    assert ovl(P, Pfit) > 0.9