예제 #1
0
def test_inversion():
    domain = "https://www.ssnmr.org/sites/default/files/mrsimulator"
    filename = f"{domain}/MAS_SE_PIETA_5%25Li2O_FT.csdf"
    data_object = cp.load(filename)

    # Inversion only requires the real part of the complex dataset.
    data_object = data_object.real
    sigma = 1110.521  # data standard deviation

    # Convert the MAS dimension from Hz to ppm.
    data_object.dimensions[0].to("ppm", "nmr_frequency_ratio")

    data_object = data_object.T
    data_object_truncated = data_object[:, 1220:-1220]

    data_object_truncated.dimensions[0].to("s")  # set coordinates to 's'
    kernel_dimension = data_object_truncated.dimensions[0]

    relaxT2 = relaxation.T2(
        kernel_dimension=kernel_dimension,
        inverse_dimension=dict(
            count=32,
            minimum="1e-3 s",
            maximum="1e4 s",
            scale="log",
            label="log (T2 / s)",
        ),
    )
    inverse_dimension = relaxT2.inverse_dimension
    K = relaxT2.kernel(supersampling=20)

    new_system = TSVDCompression(K, data_object_truncated)
    compressed_K = new_system.compressed_K
    compressed_s = new_system.compressed_s

    assert new_system.truncation_index == 18

    # setup the pre-defined range of alpha and lambda values
    lambdas = 10 ** (-4 + 5 * (np.arange(32) / 31))

    # setup the smooth lasso cross-validation class
    s_lasso = LassoFistaCV(
        lambdas=lambdas,  # A numpy array of lambda values.
        sigma=sigma,  # data standard deviation
        folds=5,  # The number of folds in n-folds cross-validation.
        inverse_dimension=inverse_dimension,  # previously defined inverse dimensions.
    )

    # run the fit method on the compressed kernel and compressed data.
    s_lasso.fit(K=compressed_K, s=compressed_s)
    np.testing.assert_almost_equal(s_lasso.hyperparameters["lambda"], 0.116, decimal=1)

    s_lasso.cv_plot()

    residuals = s_lasso.residuals(K=K, s=data_object_truncated)
    np.testing.assert_almost_equal(residuals.std().value, 1538.48, decimal=1)
예제 #2
0
def test_fista():
    domain = "https://sandbox.zenodo.org/record/1065394/files"
    filename = f"{domain}/test1_signal.csdf"
    signal = cp.load(filename)
    sigma = 0.0008

    datafile = f"{domain}/test1_t2.csdf"
    true_dist = cp.load(datafile)

    kernel_dimension = signal.dimensions[0]
    relaxT2 = relaxation.T2(
        kernel_dimension=kernel_dimension,
        inverse_dimension=dict(
            count=64,
            minimum="1e-2 s",
            maximum="1e3 s",
            scale="log",
            label="log (T2 / s)",
        ),
    )
    inverse_dimension = relaxT2.inverse_dimension
    K = relaxT2.kernel(supersampling=1)

    new_system = TSVDCompression(K, signal)
    compressed_K = new_system.compressed_K
    compressed_s = new_system.compressed_s

    assert new_system.truncation_index == 29

    lambdas = 10 ** (-5 + 4 * (np.arange(16) / 15))

    f_lasso_cv = LassoFistaCV(
        lambdas=lambdas,  # A numpy array of lambda values.
        folds=5,  # The number of folds in n-folds cross-validation.
        sigma=sigma,  # noise standard deviation
        inverse_dimension=inverse_dimension,  # previously defined inverse dimensions.
    )

    f_lasso_cv.fit(K=compressed_K, s=compressed_s)

    sol = f_lasso_cv.f
    assert np.argmax(sol.y[0].components[0]) == np.argmax(true_dist.y[0].components[0])

    residuals = f_lasso_cv.residuals(K=K, s=signal)
    std = residuals.std()
    np.testing.assert_almost_equal(std.value, sigma, decimal=3)
plt.figure(figsize=(4.5, 3.5))
signal.plot()
plt.tight_layout()
plt.show()

# %%
# Linear Inversion setup
# ----------------------
# Generating the kernel
# '''''''''''''''''''''
kernel_dimension = signal.dimensions[0]

relaxT2 = relaxation.T2(
    kernel_dimension=kernel_dimension,
    inverse_dimension=dict(count=64,
                           minimum="1e-2 s",
                           maximum="1e3 s",
                           scale="log",
                           label="log (T2 / s)"),
)
inverse_dimension = relaxT2.inverse_dimension
K = relaxT2.kernel(supersampling=1)

# %%
# Data Compression
# ''''''''''''''''
new_system = TSVDCompression(K, signal)
compressed_K = new_system.compressed_K
compressed_s = new_system.compressed_s

print(f"truncation_index = {new_system.truncation_index}")