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)
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)) f_lasso_cv.cv_plot() plt.tight_layout() plt.show() # %% # The optimum solution # '''''''''''''''''''' sol = f_lasso_cv.f plt.figure(figsize=(4, 3)) plt.subplot(projection="csdm") plt.plot(true_t2_dist / true_t2_dist.max(), label="true distribution") plt.plot(sol / sol.max(), label="opt solution") plt.legend() plt.grid() plt.tight_layout() plt.show() # %% # Residuals # ''''''''' residuals = f_lasso_cv.residuals(K=K, s=signal) print(residuals.std()) plt.figure(figsize=(4.5, 3.5)) residuals.plot() plt.tight_layout() plt.show()
# %% # The optimum solution # '''''''''''''''''''' f_sol = s_lasso.f levels = np.arange(15) / 15 + 0.1 plt.figure(figsize=(4, 3)) ax = plt.subplot(projection="csdm") ax.contour(f_sol / f_sol.max(), levels=levels, cmap="jet_r") ax.set_ylim(-70, -130) ax.set_xlim(-3, 2.5) plt.grid(linestyle="--", alpha=0.75) plt.tight_layout() plt.show() # %% # The fit residuals # ''''''''''''''''' residuals = s_lasso.residuals(K=K, s=data_object_truncated) plot2D(residuals) # %% # The standard deviation of the residuals is residuals.std() # %% # Saving the solution # ''''''''''''''''''' f_sol.save("T2_inverse.csdf") # save the solution residuals.save("T2_residue.csdf") # save the residuals