def generate_kernel(n, data, count0, inc0, count1, inc1, channel, B0, theta, n_su, d_range, k_typ): if data is None: raise PreventUpdate if d_range is None: d_range = [[0, -1], [0, -1]] data = cp.parse_dict(data) anisotropic_dimension = data.dimensions[0] inverse_dimensions = [ cp.LinearDimension(count=count0, increment=f"{inc0} Hz", label="x"), cp.LinearDimension(count=count1, increment=f"{inc1} Hz", label="y"), ] vr = 0 ns = 1 if k_typ == "sideband-correlation": vr = anisotropic_dimension.increment.to("Hz") ns = anisotropic_dimension.count if k_typ == "MAF": vr = "1 GHz" ns = 1 K = ShieldingPALineshape( anisotropic_dimension=anisotropic_dimension, inverse_dimension=inverse_dimensions, channel=channel, magnetic_flux_density=f"{B0} T", rotor_angle=f"{theta} °", rotor_frequency=f"{vr}", number_of_sidebands=ns, ).kernel(supersampling=int(n_su)) ranges = slice(d_range[1][0], d_range[1][1], None) data_truncated = data[:, ranges] new_system = TSVDCompression(K, data_truncated) compressed_K = new_system.compressed_K compressed_s = new_system.compressed_s return { "kernel": compressed_K, "signal": compressed_s.dict(), "inverse_dimensions": [item.dict() for item in inverse_dimensions], }
def test_TopHat(): test_dataset = cp.CSDM( dependent_variables=[cp.as_dependent_variable(np.ones(500))], dimensions=[cp.LinearDimension(500, "1 s")], ) processor = sp.SignalProcessor() processor.operations = [ sp.apodization.TopHat(rising_edge="100 s", falling_edge="400 s") ] rise_and_fall_dataset = processor.apply_operations(test_dataset.copy()) rise_and_fall_should_be = np.zeros(500) rise_and_fall_should_be[100:400] = 1 assert np.allclose(rise_and_fall_dataset.y[0].components, rise_and_fall_should_be) processor.operations = [sp.apodization.TopHat(rising_edge="100 s")] rise_only_dataset = processor.apply_operations(test_dataset.copy()) rise_only_should_be = np.zeros(500) rise_only_should_be[100:] = 1 assert np.allclose(rise_only_dataset.y[0].components, rise_only_should_be) processor.operations = [sp.apodization.TopHat(falling_edge="400 s")] fall_only_dataset = processor.apply_operations(test_dataset.copy()) fall_only_should_be = np.zeros(500) fall_only_should_be[:400] = 1 assert np.allclose(fall_only_dataset.y[0].components, fall_only_should_be)
def solve(l1, l2, data): inverse_dimensions = [ cp.LinearDimension(**item) for item in data["inverse_dimensions"] ] compressed_K = np.asarray(data["kernel"], dtype=np.float64) compressed_s = cp.parse_dict(data["signal"]) print(compressed_K, compressed_s, inverse_dimensions) # s_lasso = SmoothLassoCV( # # alpha=l2, # # lambda1=l1, # inverse_dimension=inverse_dimensions, # # method="lars", # tolerance=1e-3, # ) s_lasso = SmoothLasso( alpha=l2, lambda1=l1, inverse_dimension=inverse_dimensions, method="lars", tolerance=1e-3, ) s_lasso.fit(K=compressed_K, s=compressed_s) res = s_lasso.f / s_lasso.f.max() return [ res.to_dict(), s_lasso.hyperparameters["lambda"], s_lasso.hyperparameters["alpha"], ]
def test_get_spectral_dimensions_even_count_positive_increment(): # even # complex_fft csdm = cp.as_csdm(np.arange(10)) csdm.x[0] = cp.LinearDimension(count=10, increment="1 Hz", complex_fft=True) res = {"count": 10, "spectral_width": 10, "reference_offset": 0} assertion(csdm, res, "even complex_fft") csdm.x[0] = cp.LinearDimension(count=10, increment="3.4 Hz", complex_fft=True) res = {"count": 10, "spectral_width": 34, "reference_offset": 0} assertion(csdm, res, "even complex_fft") # even # complex_fft # coordinates_offset csdm.x[0] = cp.LinearDimension(count=10, increment="1 Hz", coordinates_offset="-0.05 kHz", complex_fft=True) res = {"count": 10, "spectral_width": 10, "reference_offset": -50} assertion(csdm, res, "even complex_fft coordinates_offset") csdm.x[0] = cp.LinearDimension(count=10, increment="3.4 Hz", coordinates_offset="-0.05 kHz", complex_fft=True) res = {"count": 10, "spectral_width": 34, "reference_offset": -50} assertion(csdm, res, "even complex_fft coordinates_offset") # even csdm.x[0] = cp.LinearDimension(count=10, increment="1 Hz") res = {"count": 10, "spectral_width": 10, "reference_offset": 5} assertion(csdm, res, "even") csdm.x[0] = cp.LinearDimension(count=10, increment="3.4 Hz") res = {"count": 10, "spectral_width": 34, "reference_offset": 17} assertion(csdm, res, "even") # even # coordinates_offset csdm.x[0] = cp.LinearDimension(count=10, increment="1 Hz", coordinates_offset="5 Hz", label="1H") res = { "count": 10, "spectral_width": 10, "reference_offset": 10, "label": "1H" } assertion(csdm, res, "even coordinates_offset") csdm.x[0] = cp.LinearDimension(count=10, increment="3.4 Hz", coordinates_offset="-0.05 kHz") res = {"count": 10, "spectral_width": 34, "reference_offset": -33} assertion(csdm, res, "even coordinates_offset")
def test_get_spectral_dimensions_odd_count_negative_increment(): # odd csdm = cp.as_csdm(np.arange(15)) csdm.x[0] = cp.LinearDimension(count=15, increment="-1 Hz", label="1H") res = { "count": 15, "spectral_width": 15, "reference_offset": -7, "label": "1H" } assertion(csdm, res, "odd") csdm.x[0] = cp.LinearDimension(count=15, increment="-3.4 Hz", label="1H") res = { "count": 15, "spectral_width": 51, "reference_offset": -23.8, "label": "1H" } assertion(csdm, res, "odd") # even # coordinates_offset csdm.x[0] = cp.LinearDimension(count=15, increment="-1 Hz", coordinates_offset="-10 Hz", label="1H") res = { "count": 15, "spectral_width": 15, "reference_offset": -17, "label": "1H" } assertion(csdm, res, "odd coordinates_offset") csdm.x[0] = cp.LinearDimension(count=15, increment="-3.4 Hz", coordinates_offset="-10 Hz", label="1H") res = { "count": 15, "spectral_width": 51, "reference_offset": -33.8, "label": "1H" } assertion(csdm, res, "odd coordinates_offset")
def test_get_spectral_dimensions_even_count_negative_increment(): # even csdm = cp.as_csdm(np.arange(10)) csdm.x[0] = cp.LinearDimension(count=10, increment="-1 Hz", label="1H") res = { "count": 10, "spectral_width": 10, "reference_offset": -4, "label": "1H" } assertion(csdm, res, "even") csdm.x[0] = cp.LinearDimension(count=10, increment="-3.4 Hz", label="1H") res = { "count": 10, "spectral_width": 34, "reference_offset": -13.6, "label": "1H" } assertion(csdm, res, "even") # even # coordinates_offset csdm.x[0] = cp.LinearDimension(count=10, increment="-1 Hz", coordinates_offset="-10 Hz", label="1H") res = { "count": 10, "spectral_width": 10, "reference_offset": -14, "label": "1H" } assertion(csdm, res, "even coordinates_offset") csdm.x[0] = cp.LinearDimension(count=10, increment="-3.4 Hz", coordinates_offset="-10 Hz", label="1H") res = { "count": 10, "spectral_width": 34, "reference_offset": -23.6, "label": "1H" } assertion(csdm, res, "even coordinates_offset")
def test_get_spectral_dimensions_odd_count_positive_increment(): # odd # complex_fft csdm = cp.as_csdm(np.arange(15)) csdm.x[0] = cp.LinearDimension(count=15, increment="1 Hz", complex_fft=True) res = {"count": 15, "spectral_width": 15, "reference_offset": 0} assertion(csdm, res, "odd complex_fft") csdm.x[0] = cp.LinearDimension(count=15, increment="3.4 Hz", complex_fft=True) res = {"count": 15, "spectral_width": 51, "reference_offset": 0} assertion(csdm, res, "odd complex_fft") # odd # complex_fft # coordinates_offset csdm.x[0] = cp.LinearDimension(count=15, increment="1 Hz", complex_fft=True, coordinates_offset="12 Hz") res = {"count": 15, "spectral_width": 15, "reference_offset": 12} assertion(csdm, res, "odd complex_fft coordinates_offset") csdm.x[0] = cp.LinearDimension(count=15, increment="3.4 Hz", coordinates_offset="12 Hz", complex_fft=True) res = {"count": 15, "spectral_width": 51, "reference_offset": 12} assertion(csdm, res, "odd complex_fft coordinates_offset") # odd csdm = cp.as_csdm(np.arange(15)) csdm.x[0] = cp.LinearDimension(count=15, increment="1 Hz") res = {"count": 15, "spectral_width": 15, "reference_offset": 7} assertion(csdm, res, "odd complex_fft") csdm.x[0] = cp.LinearDimension(count=15, increment="3.4 Hz") res = {"count": 15, "spectral_width": 51, "reference_offset": 23.8} assertion(csdm, res, "odd complex_fft") # odd # coordinates_offset csdm.x[0] = cp.LinearDimension(count=15, increment="1 Hz", coordinates_offset="12 Hz") res = {"count": 15, "spectral_width": 15, "reference_offset": 19} assertion(csdm, res, "odd coordinates_offset") csdm.x[0] = cp.LinearDimension(count=15, increment="3.4 Hz", coordinates_offset="12 Hz") res = {"count": 15, "spectral_width": 51, "reference_offset": 35.8} assertion(csdm, res, "odd coordinates_offset")
def test_supersampling_linear(): dim = cp.LinearDimension(count=20, coordinates_offset="0 Hz", increment="0.5 kHz") y = np.arange(20) * 0.5 for i in range(10): oversample = i + 1 y_oversampled = _supersampled_coordinates(dim, supersampling=oversample) y_reduced = y_oversampled.reshape(-1, oversample).mean(axis=-1) assert np.allclose(y_reduced.value, y)
def test_get_spectral_dimensions(): # 1 csdm = cp.as_csdm(np.arange(10)) csdm.dimensions[0] = cp.LinearDimension(count=10, increment="1 Hz", complex_fft=True) res = {"count": 10, "spectral_width": 10, "reference_offset": 0} assert get_spectral_dimensions(csdm)[0] == res # 2 csdm.dimensions[0] = cp.LinearDimension(count=10, increment="1 Hz", coordinates_offset="-0.05 kHz", complex_fft=True) res = {"count": 10, "spectral_width": 10, "reference_offset": -50} assert get_spectral_dimensions(csdm)[0] == res # 3 csdm.dimensions[0] = cp.LinearDimension(count=10, increment="1 Hz") res = {"count": 10, "spectral_width": 10, "reference_offset": 5} assert get_spectral_dimensions(csdm)[0] == res # 4 csdm.dimensions[0] = cp.LinearDimension(count=10, increment="1 Hz", label="1H") res = { "count": 10, "spectral_width": 10, "reference_offset": 5, "label": "1H" } assert get_spectral_dimensions(csdm)[0] == res
def test_01(): post_sim = sp.SignalProcessor() operations = [ sp.IFFT(), sp.apodization.Gaussian(FWHM="12 K", dim_index=0, dv_index=0), sp.FFT(), ] post_sim.operations = operations with pytest.raises(ValueError, match="The data must be a CSDM object."): post_sim.apply_operations([]) data = cp.as_csdm(np.arange(20)) data.x[0] = cp.LinearDimension(count=20, increment="10 K") post_sim.apply_operations(data) # to dict with units dict_ = post_sim.json() assert dict_ == { "operations": [ { "dim_index": 0, "function": "IFFT" }, { "function": "apodization", "type": "Gaussian", "FWHM": "12.0 K", "dim_index": 0, "dv_index": 0, }, { "dim_index": 0, "function": "FFT" }, ], } # parse dict with units post_sim_2 = sp.SignalProcessor.parse_dict_with_units(dict_) assert post_sim.operations == post_sim_2.operations
def test_generic(): # 7 csdm = cp.as_csdm(np.arange(10)) csdm.x[0] = cp.LinearDimension( count=10, increment="-1 Hz", coordinates_offset="-10 Hz", origin_offset="100 MHz", label="1H", ) res = { "count": 10, "spectral_width": 10, "reference_offset": -14, "origin_offset": 100e6, "label": "1H", } assertion(csdm, res, "7")
def to_csdm(self): """ Return a csdm object containing data. Returns ------- data : csdm object CSDM object containing parameters and data """ try: import csdmpy as cp # self._oproc = {} # self._odtype = str(self._data.dtype) # create a list of dimension objects dimensions = [ cp.LinearDimension( count=value["size"], increment=f'{1 / value["sw"]} s', reciprocal={ "coordinates_offset": f'{value["car"]} Hz', "origin_offset": f'{value["obs"]} MHz', }, label=value["label"], ) for key, value in list(self._udic.items()) if type(key) == int and value["size"] != 1 ] return cp.CSDM( dimensions=dimensions[::-1], dependent_variables=[ cp.as_dependent_variable(self._data.copy()) ], ) except: raise ImportError( "csdmpy must be installed to use this function. Please install by typing 'pip install csdmpy' in the terminal." )
# ---------------------- # # Dimension setup # ''''''''''''''' # # **Anisotropic-dimension:** # The dimension of the dataset that holds the pure anisotropic frequency # contributions. In ``mrinversion``, this must always be the dimension at index 0 of # the data object. anisotropic_dimension = data_object_truncated.dimensions[0] # %% # **x-y dimensions:** # The two inverse dimensions corresponding to the `x` and `y`-axis of the `x`-`y` grid. inverse_dimensions = [ cp.LinearDimension(count=25, increment="500 Hz", label="x"), # the `x`-dimension. cp.LinearDimension(count=25, increment="500 Hz", label="y"), # the `y`-dimension. ] # %% # Generating the kernel # ''''''''''''''''''''' # # For MAF datasets, the line-shape kernel corresponds to the pure nuclear shielding # anisotropy line-shapes. Use the # :class:`~mrinversion.kernel.nmr.ShieldingPALineshape` class to generate a # shielding line-shape kernel. lineshape = ShieldingPALineshape( anisotropic_dimension=anisotropic_dimension, inverse_dimension=inverse_dimensions,
# %% # Here the applied offset will be the following function # # .. math:: # # f(x) = 0.00001 \cdot x^2 + 0.2 # # Next we create a CSDM object with a test dataset which our signal processor will # operate on. Here, the dataset spans 500 Hz with a delta function centered at # 100 Hz. test_data = np.zeros(500) test_data[350] = 1 csdm_object = cp.CSDM( dependent_variables=[cp.as_dependent_variable(test_data)], dimensions=[ cp.LinearDimension(count=500, increment="1 Hz", complex_fft=True) ], ) # %% # Now to apply the processor to the CSDM object, use the # :py:meth:`~mrsimulator.signal_processor.SignalProcessor.apply_operations` method as # follows processed_dataset = processor.apply_operations(dataset=csdm_object.copy()).real # %% # To see the results of the exponential apodization, we create a simple plot using the # ``matplotlib`` library. import matplotlib.pyplot as plt fig, ax = plt.subplots(1,
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, )
# -*- coding: utf-8 -*- import csdmpy as cp import numpy as np from mrsimulator import signal_processing as sp __author__ = "Deepansh J. Srivastava" __email__ = "*****@*****.**" # Creating a test CSDM object. test_data = np.zeros((40, 40)) test_data[20, :] = 1 csdm_object = cp.CSDM( dependent_variables=[cp.as_dependent_variable(test_data)], dimensions=[ cp.LinearDimension(count=40, increment="1 s", label="0", complex_fft=True), cp.Dimension(type="linear", count=40, increment="1 K", label="1", complex_fft=True), ], ) csdm_object2 = csdm_object.copy() def test_shear_01(): processor = sp.SignalProcessor(operations=[ sp.IFFT(dim_index=1),
operations=[ sp.IFFT(), sp.apodization.TopHat(rising_edge="1 s", falling_edge="9 s"), sp.FFT(), ] ) # %% # Next we create a CSDM object with a test dataset which our signal processor will # operate on. Here, the dataset is a delta function centered at 0 Hz with a some # applied Gaussian line broadening. test_data = np.zeros(500) test_data[250] = 1 csdm_object = cp.CSDM( dependent_variables=[cp.as_dependent_variable(test_data)], dimensions=[cp.LinearDimension(count=500, increment="0.1 Hz", complex_fft=True)], ) # %% # To apply the previously defined signal processor, we use the # :py:meth:`~mrsimulator.signal_processor.SignalProcessor.apply_operations` method as # as follows processed_dataset = processor.apply_operations(dataset=csdm_object).real # %% # To see the results of the top hat apodization, we create a simple plot using the # ``matplotlib`` library. fig, ax = plt.subplots(1, 2, figsize=(8, 3.5), subplot_kw={"projection": "csdm"}) ax[0].plot(csdm_object, color="black", linewidth=1) ax[0].set_title("Before") ax[1].plot(processed_dataset.real, color="black", linewidth=1)