def add_cp_dimension(doctest_namespace): doctest_namespace["cp"] = cp doctest_namespace["np"] = np doctest_namespace["Dimension"] = Dimension doctest_namespace["DependentVariable"] = DependentVariable doctest_namespace["x"] = Dimension( type="linear", description="This is a test", count=10, increment="5 G", coordinates_offset="10 mT", origin_offset="10 T", label="field strength", ) doctest_namespace["dimension_dictionary"] = { "type": "linear", "description": "This is a test", "count": 10, "increment": "5 G", "coordinates_offset": "10 mT", "origin_offset": "10 T", "label": "field strength", } numpy_array = np.arange(30).reshape(3, 10).astype(np.float32) doctest_namespace["y"] = DependentVariable( type="internal", description="A test image", name="star", unit="W s", quantity_name="energy", quantity_type="pixel_3", components=numpy_array, ) doctest_namespace["dependent_variable_dictionary"] = { "type": "internal", "description": "A test image", "name": "star", "unit": "W s", "quantity_name": "energy", "quantity_type": "pixel_3", "components": numpy_array, } doctest_namespace["data"] = cp.load(cp.tests.test01) doctest_namespace["my_data"] = cp.load(cp.tests.test02)
def test02(): dataset = cp.load(cp.tests.test02) assert dataset.dependent_variables[0].type == "internal" # encoding is always set to 'base64' after import assert dataset.dependent_variables[0].encoding == "base64" assert dataset.dependent_variables[0].numeric_type == "float64" assert dataset.dependent_variables[0].components.dtype == np.float64 assert dataset.description == "Base64 encoding test" assert len(dataset.dependent_variables) == 4 assert len(dataset.dimensions) == 1 assert dataset.dimensions[0].type == "monotonic" assert str(dataset.dimensions[0].origin_offset) == "0.0 cm" assert dataset.dimensions[0].count == 10 assert dataset.dimensions[0].quantity_name == "length"
def test01(): dataset = cp.load(cp.tests.test01) assert dataset.dependent_variables[0].type == "internal" # encoding is always set to 'base64' after import assert dataset.dependent_variables[0].encoding == "base64" assert dataset.dependent_variables[0].numeric_type == "float32" assert dataset.dependent_variables[0].components.dtype == np.float32 assert dataset.description == "A simulated sine curve." assert len(dataset.dependent_variables) == 1 assert len(dataset.dimensions) == 1 assert dataset.dimensions[0].type == "linear" assert str(dataset.dimensions[0].increment) == "0.1 s" assert str(dataset.dimensions[0].origin_offset) == "0.0 s" assert dataset.dimensions[0].count == 10 assert dataset.dimensions[0].quantity_name == "time" assert np.all(dataset.dimensions[0].coordinates.value == np.arange(10) * 0.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)
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)
plt.tight_layout() plt.show() # %% # Dataset setup # ------------- # # Import the dataset # '''''''''''''''''' # # Load the dataset. Here, we import the dataset as the CSDM data-object. # The 2D MAF dataset in csdm format filename = "https://sandbox.zenodo.org/record/1065347/files/2Na2O_3SiO2_MAF.csdf" data_object = cp.load(filename) # For inversion, we only interest ourselves with the real part of the complex dataset. data_object = data_object.real # We will also convert the coordinates of both dimensions from Hz to ppm. _ = [item.to("ppm", "nmr_frequency_ratio") for item in data_object.dimensions] # %% # Here, the variable ``data_object`` is a # `CSDM <https://csdmpy.readthedocs.io/en/latest/api/CSDM.html>`_ # object that holds the real part of the 2D MAF dataset. The plot of the 2D MAF dataset # is plot2D(data_object) # %%
from mrsimulator import Simulator, SpinSystem, Site from mrsimulator.methods import BlochDecaySpectrum from mrsimulator import signal_processing as sp from mrsimulator.utils import spectral_fitting as sf from mrsimulator.spin_system.tensors import SymmetricTensor # sphinx_gallery_thumbnail_number = 3 # %% # Import the dataset # ------------------ # Use the `csdmpy <https://csdmpy.readthedocs.io/en/stable/index.html>`_ # module to load the synthetic dataset as a CSDM object. file_ = "https://sandbox.zenodo.org/record/835664/files/synthetic_cuspidine_test.csdf?" synthetic_experiment = cp.load(file_).real # standard deviation of noise from the dataset sigma = 0.03383338 # convert the dimension coordinates from Hz to ppm synthetic_experiment.x[0].to("ppm", "nmr_frequency_ratio") # Plot of the synthetic dataset. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(synthetic_experiment, "k", alpha=0.5) ax.set_xlim(50, -200) plt.grid() plt.tight_layout() plt.show()
import matplotlib.pyplot as plt import numpy as np from mrinversion.kernel import relaxation from mrinversion.linear_model import LassoFistaCV, TSVDCompression # sphinx_gallery_thumbnail_number = 3 # %% # Dataset setup # ------------- # Load the dataset # '''''''''''''''' domain = "https://sandbox.zenodo.org/record/1065394/files" filename = f"{domain}/test1_signal.csdf" signal = cp.load(filename) sigma = 0.0008 # %% # The variable ``signal`` holds the 1D dataset. For comparison, let's # also import the true t2 distribution from which the synthetic 1D signal # decay is simulated. datafile = f"{domain}/test1_t2.csdf" true_t2_dist = cp.load(datafile) plt.figure(figsize=(4.5, 3.5)) signal.plot() plt.tight_layout() plt.show() # %%
def test04(): data1 = cp.load(cp.tests.test04) data2 = data1.copy() assert data1.data_structure == data2.data_structure
import matplotlib.pyplot as plt from lmfit import Minimizer from mrsimulator import Simulator, SpinSystem, Site from mrsimulator.methods import BlochDecaySpectrum from mrsimulator import signal_processing as sp from mrsimulator.utils import spectral_fitting as sf from mrsimulator.utils import get_spectral_dimensions # sphinx_gallery_thumbnail_number = 3 # %% # Import the dataset # ------------------ name = "https://sandbox.zenodo.org/record/814455/files/LHistidine_cross_section.csdf" pass_cross_section = cp.load(name) # standard deviation of noise from the dataset sigma = 4.640351 # For the spectral fitting, we only focus on the real part of the complex dataset. pass_cross_section = pass_cross_section.real # Convert the coordinates along each dimension from Hz to ppm. _ = [ item.to("ppm", "nmr_frequency_ratio") for item in pass_cross_section.dimensions ] # The plot of the dataset. plt.figure(figsize=(4.25, 3.0))
from lmfit import Minimizer from mrsimulator import Simulator from mrsimulator.methods import SSB2D from mrsimulator import signal_processing as sp from mrsimulator.utils import spectral_fitting as sf from mrsimulator.utils import get_spectral_dimensions from mrsimulator.utils.collection import single_site_system_generator # sphinx_gallery_thumbnail_number = 3 # %% # Import the dataset # ------------------ filename = "https://sandbox.zenodo.org/record/814455/files/1H13C_CPPASS_LHistidine.csdf" mat_data = cp.load(filename) # standard deviation of noise from the dataset sigma = 0.4192854 # For the spectral fitting, we only focus on the real part of the complex dataset. mat_data = mat_data.real # Convert the coordinates along each dimension from Hz to ppm. _ = [item.to("ppm", "nmr_frequency_ratio") for item in mat_data.dimensions] # %% # When using the SSB2D method, ensure the horizontal dimension of the dataset is the # isotropic dimension. Here, we apply an appropriate transpose operation to the dataset. mat_data = mat_data.T # transpose
from mrsimulator import Simulator, SpinSystem, Site from mrsimulator.methods import BlochDecaySpectrum from mrsimulator.utils import get_spectral_dimensions from mrsimulator.utils.spectral_fitting import LMFIT_min_function, make_LMFIT_params from lmfit import Minimizer, report_fit # global plot configuration mpl.rcParams["figure.figsize"] = [4.5, 3.0] mpl.rcParams["grid.linestyle"] = "--" # sphinx_gallery_thumbnail_number = 4 # %% # Import the dataset # ------------------ filename = "https://sandbox.zenodo.org/record/687656/files/1H13C_CPPASS_LHistidine.csdf" pass_data = cp.load(filename) # For the spectral fitting, we only focus on the real part of the complex dataset. # The script assumes that the dimension at index 0 is the isotropic dimension. # Transpose the dataset as required. pass_data = pass_data.real.T # Convert the coordinates along each dimension from Hz to ppm. _ = [item.to("ppm", "nmr_frequency_ratio") for item in pass_data.dimensions] # Normalize the spectrum. pass_data /= pass_data.max() # The plot of the dataset. levels = (np.arange(10) + 0.3) / 15 # contours are drawn at these levels. ax = plt.subplot(projection="csdm")
from lmfit import Minimizer from mrsimulator import Simulator, SpinSystem, Site from mrsimulator.method.lib import SSB2D from mrsimulator import signal_processor as sp from mrsimulator.utils import spectral_fitting as sf from mrsimulator.utils import get_spectral_dimensions from mrsimulator.spin_system.tensors import SymmetricTensor # sphinx_gallery_thumbnail_number = 3 # %% # Import the dataset # ------------------ filename = "https://ssnmr.org/sites/default/files/mrsimulator/Rb2SO4_QMAT.csdf" qmat_dataset = cp.load(filename) # standard deviation of noise from the dataset sigma = 6.530634 # For the spectral fitting, we only focus on the real part of the complex dataset. qmat_dataset = qmat_dataset.real # Convert the coordinates along each dimension from Hz to ppm. _ = [item.to("ppm", "nmr_frequency_ratio") for item in qmat_dataset.dimensions] # plot of the dataset. max_amp = qmat_dataset.max() levels = (np.arange(31) + 0.15) * max_amp / 32 # contours are drawn at these levels. options = dict(levels=levels, alpha=1, linewidths=0.5) # plot options
def test00(): error = "Missing a required data file address." with pytest.raises(Exception, match=".*{0}.*".format(error)): cp.load()
from mrsimulator.methods import BlochDecayCTSpectrum from mrsimulator import signal_processing as sp from mrsimulator.utils import spectral_fitting as sf from mrsimulator.utils import get_spectral_dimensions # sphinx_gallery_thumbnail_number = 3 # %% # Import the dataset # ------------------ # # Import the experimental data. We use dataset file serialized with the CSDM # file-format, using the # `csdmpy <https://csdmpy.readthedocs.io/en/stable/index.html>`_ module. filename = "https://sandbox.zenodo.org/record/814455/files/Na2SiO3_O17.csdf" experiment = cp.load(filename) # standard deviation of noise from the dataset sigma = 1.931335 # For spectral fitting, we only focus on the real part of the complex dataset experiment = experiment.real # Convert the dimension coordinates from Hz to ppm. experiment.x[0].to("ppm", "nmr_frequency_ratio") # plot of the dataset. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(experiment, color="black", linewidth=0.5, label="Experiment") ax.set_xlim(100, -50)
ax.set_aspect("equal") # %% # Dataset setup # ------------- # # Import the dataset # '''''''''''''''''' # # Load the dataset. Here, we import the dataset as a CSDM data-object. # the 1D MAF cross-section data in csdm format domain = "https://sandbox.zenodo.org/record/1065347/files" filename = f"{domain}/puxfgdh25rru1q3li124anylkgup8rdp.csdf" data_object = cp.load(filename) # convert the data dimension from `Hz` to `ppm`. data_object.dimensions[0].to("ppm", "nmr_frequency_ratio") # %% # The variable ``data_object`` holds the 1D MAF cross-section. For comparison, let's # also import the true tensor parameter distribution from which the synthetic 1D pure # anisotropic MAF cross-section line-shape is simulated. datafile = f"{domain}/s5wpm26w4cv3w64qjhouqu458ch4z0nd.csdf" true_data_object = cp.load(datafile) # %% # The plot of the 1D MAF cross-section along with the 2D true tensor parameter # distribution of the synthetic dataset is shown below.
# -*- coding: utf-8 -*- """CSDM main.""" import sys import csdmpy as cp from csdmpy.helper_functions import preview __author__ = "Deepansh J. Srivastava" __email__ = "*****@*****.**" if __name__ == "__main__": for i in range(len(sys.argv) - 1): data = cp.load(sys.argv[i + 1]) preview(data)
from mrsimulator import Simulator from mrsimulator.method.lib import SSB2D from mrsimulator import signal_processor as sp from mrsimulator.utils import spectral_fitting as sf from mrsimulator.utils import get_spectral_dimensions from mrsimulator.utils.collection import single_site_system_generator # sphinx_gallery_thumbnail_number = 3 # %% # Import the dataset # ------------------ host = "https://ssnmr.org/sites/default/files/mrsimulator/" filename = "1H13C_CPPASS_LHistidine.csdf" mat_dataset = cp.load(host + filename) # standard deviation of noise from the dataset sigma = 0.4192854 # For the spectral fitting, we only focus on the real part of the complex dataset. mat_dataset = mat_dataset.real # Convert the coordinates along each dimension from Hz to ppm. _ = [item.to("ppm", "nmr_frequency_ratio") for item in mat_dataset.dimensions] # %% # When using the SSB2D method, ensure the horizontal dimension of the dataset is the # isotropic dimension. Here, we apply an appropriate transpose operation to the dataset. mat_dataset = mat_dataset.T # transpose
from mrsimulator.spin_system.tensors import SymmetricTensor # sphinx_gallery_thumbnail_number = 3 # %% # Import the datasets # ------------------- # Import the datasets and assign the standard deviation of noise for each dataset. Here, # ``sigma1``, ``sigma2``, and ``sigma3`` are the noise standard deviation for the # dataset acquired at 5 kHz, 1.94 kHz, and 960 Hz spinning speeds, respectively. host = "https://nmr.cemhti.cnrs-orleans.fr/Dmfit/Help/csdm/" filename1 = "13C MAS 5000Hz - Glycine.csdf" filename2 = "13C MAS 1940Hz - Glycine.csdf" filename3 = "13C MAS 960Hz - Glycine.csdf" experiment1 = cp.load(host + filename1).real experiment2 = cp.load(host + filename2).real experiment3 = cp.load(host + filename3).real experiments = [experiment1, experiment2, experiment3] # standard deviation of noise from the dataset sigma1 = 1.97637 sigma2 = 3.822249 sigma3 = 3.982936 sigmas = [sigma1, sigma2, sigma3] # Convert the coordinates along each dimension from Hz to ppm for each dataset fig, ax = plt.subplots(1, 3, figsize=(12, 3), subplot_kw={"projection": "csdm"})
from mrsimulator import Simulator, Site, SpinSystem from mrsimulator.method.lib import BlochDecayCTSpectrum from mrsimulator import signal_processor as sp from mrsimulator.utils import spectral_fitting as sf from mrsimulator.utils import get_spectral_dimensions from mrsimulator.spin_system.tensors import SymmetricTensor # sphinx_gallery_thumbnail_number = 3 # %% # Import the dataset # ------------------ host = "https://ssnmr.org/sites/default/files/mrsimulator/" filename = "11B_lithum_orthoborate.csdf" experiment = cp.load(host + filename) # standard deviation of noise from the dataset sigma = 0.08078374 # For spectral fitting, we only focus on the real part of the complex dataset experiment = experiment.real # Convert the coordinates along each dimension from Hz to ppm. _ = [item.to("ppm", "nmr_frequency_ratio") for item in experiment.dimensions] # plot of the dataset. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(experiment, "k", alpha=0.5) ax.set_xlim(100, -100)
def test_01(): domain = "https://sandbox.zenodo.org/record/1065347/files" filename = f"{domain}/8lnwmg0dr7y6egk40c2orpkmmugh9j7c.csdf" data_object = cp.load(filename) data_object = data_object.real _ = [item.to("ppm", "nmr_frequency_ratio") for item in data_object.dimensions] data_object = data_object.T data_object_truncated = data_object[:, 155:180] anisotropic_dimension = data_object_truncated.dimensions[0] inverse_dimensions = [ cp.LinearDimension(count=25, increment="400 Hz", label="x"), cp.LinearDimension(count=25, increment="400 Hz", label="y"), ] lineshape = ShieldingPALineshape( anisotropic_dimension=anisotropic_dimension, inverse_dimension=inverse_dimensions, channel="29Si", magnetic_flux_density="9.4 T", rotor_angle="87.14°", rotor_frequency="14 kHz", number_of_sidebands=4, ) K = lineshape.kernel(supersampling=2) new_system = TSVDCompression(K, data_object_truncated) compressed_K = new_system.compressed_K compressed_s = new_system.compressed_s assert new_system.truncation_index == 87 s_lasso = SmoothLasso( alpha=2.07e-7, lambda1=7.85e-6, inverse_dimension=inverse_dimensions ) s_lasso.fit(K=compressed_K, s=compressed_s) f_sol = s_lasso.f residuals = s_lasso.residuals(K=K, s=data_object_truncated) # assert np.allclose(residuals.mean().value, 0.00048751) np.testing.assert_almost_equal(residuals.std().value, 0.00336372, decimal=2) f_sol /= f_sol.max() [item.to("ppm", "nmr_frequency_ratio") for item in f_sol.dimensions] Q4_region = f_sol[0:8, 0:8, 3:18] Q4_region.description = "Q4 region" Q3_region = f_sol[0:8, 11:22, 8:20] Q3_region.description = "Q3 region" # Analysis int_Q4 = stats.integral(Q4_region) # volume of the Q4 distribution mean_Q4 = stats.mean(Q4_region) # mean of the Q4 distribution std_Q4 = stats.std(Q4_region) # standard deviation of the Q4 distribution int_Q3 = stats.integral(Q3_region) # volume of the Q3 distribution mean_Q3 = stats.mean(Q3_region) # mean of the Q3 distribution std_Q3 = stats.std(Q3_region) # standard deviation of the Q3 distribution np.testing.assert_almost_equal( (100 * int_Q4 / (int_Q4 + int_Q3)).value, 60.45388973909665, decimal=1 ) np.testing.assert_almost_equal( np.asarray([mean_Q4[0].value, mean_Q4[1].value, mean_Q4[2].value]), np.asarray([8.604842824865958, 9.05845796147297, -103.6976331077773]), decimal=0, ) np.testing.assert_almost_equal( np.asarray([mean_Q3[0].value, mean_Q3[1].value, mean_Q3[2].value]), np.asarray([10.35036818411856, 79.02481579085152, -90.58326773441284]), decimal=0, ) np.testing.assert_almost_equal( np.asarray([std_Q4[0].value, std_Q4[1].value, std_Q4[2].value]), np.asarray([4.525457744683861, 4.686253809896416, 5.369228151035292]), decimal=0, ) np.testing.assert_almost_equal( np.asarray([std_Q3[0].value, std_Q3[1].value, std_Q3[2].value]), np.asarray([6.138761032132587, 7.837190479891721, 4.210912435356488]), decimal=0, )