def test_2D_area(): data = np.zeros((256, 128), dtype=float) data[128, 64] = 1.0 csdm_obj = cp.as_csdm(data) # test00 PS = [ sp.IFFT(dim_index=(0, 1)), sp.apodization.Gaussian(FWHM="35", dim_index=0), sp.apodization.Gaussian(FWHM="55", dim_index=1), sp.FFT(dim_index=(0, 1)), ] post_sim = sp.SignalProcessor(operations=PS) dataset_new = post_sim.apply_operations(dataset=csdm_obj.copy()) _, __, y1 = dataset_new.to_list() assert np.allclose(y1.sum(), data.sum()) # test01 PS = [ sp.IFFT(dim_index=(0, 1)), sp.apodization.Gaussian(FWHM="35", dim_index=0), sp.apodization.Exponential(FWHM="55", dim_index=1), sp.FFT(dim_index=(0, 1)), ] post_sim = sp.SignalProcessor(operations=PS) dataset_new = post_sim.apply_operations(dataset=csdm_obj.copy()) _, __, y1 = dataset_new.to_list() assert np.allclose(y1.sum(), data.sum())
def test_baseline_polynomial(): dataset_in = generate_dataset() dataset_in.dimensions[0] *= cp.ScalarQuantity("1 ms") x_c = dataset_in.dimensions[0].coordinates.value # zeroth order PS_0 = [sp.baseline.Polynomial(polynomial_dictionary={"c0": 10})] dataset_out = sp.SignalProcessor(operations=PS_0).apply_operations( dataset=dataset_in.copy() ) _, y0, y1, y2 = dataset_in.to_list() _, y0_, y1_, y2_ = dataset_out.to_list() for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]): assert np.allclose(out_.max() - in_.max(), 10), "Offset failed" # first order PS_0 = [sp.baseline.Polynomial(polynomial_dictionary={"c0": 30, "c1": 1})] dataset_out = sp.SignalProcessor(operations=PS_0).apply_operations( dataset=dataset_in.copy() ) _, y0, y1, y2 = dataset_in.to_list() _, y0_, y1_, y2_ = dataset_out.to_list() for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]): assert np.allclose(out_, in_ + 30 + x_c), "Polynomial 1st order failed" # second order PS_0 = [sp.baseline.Polynomial(polynomial_dictionary={"c0": 1, "c2": 1})] dataset_out = sp.SignalProcessor(operations=PS_0).apply_operations( dataset=dataset_in.copy() ) _, y0, y1, y2 = dataset_in.to_list() _, y0_, y1_, y2_ = dataset_out.to_list() for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]): assert np.allclose(out_, in_ + 1 + x_c**2), "Polynomial 2nd order failed" # third order PS_0 = [ sp.baseline.Polynomial(polynomial_dictionary={"c0": 10, "c3": 2, "c1": 13.1}) ] dataset_out = sp.SignalProcessor(operations=PS_0).apply_operations( dataset=dataset_in.copy() ) _, y0, y1, y2 = dataset_in.to_list() _, y0_, y1_, y2_ = dataset_out.to_list() for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]): assert np.allclose( out_, in_ + 10 + 13.1 * x_c + 2 * x_c**3 ), "Polynomial3rd order failed"
def setup(): site = Site(isotope="1H", shielding_symmetric={"zeta": -100, "eta": 0.3}) spin_sys = SpinSystem(sites=[site, site], abundance=45) method = BlochDecaySpectrum(channels=["1H"]) sim = Simulator(spin_systems=[spin_sys, spin_sys], methods=[method, method]) sim.run(method_index=0) sim.methods[0].simulation._timestamp = None processor = sp.SignalProcessor(operations=[ sp.IFFT(), sp.apodization.Exponential(FWHM="2500 Hz"), sp.FFT(), sp.Scale(factor=20), ]) processors = [processor] * 2 application = { "com.github.DeepanshS.mrsimulator": { "foo": "This is some metadata" }, "com.github.DeepanshS.mrsimulator-app": { "params": "The JSON string of params" }, } return sim, processors, application
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 test_serialization_and_parse(): processor = sp.SignalProcessor(operations=[ sp.IFFT(dim_index=1), sp.affine.Shear(factor="-1 K/s", dim_index=1, parallel=0), sp.FFT(dim_index=1), ]) serialize = processor.json() expected = { "operations": [ { "dim_index": 1, "function": "IFFT" }, { "dim_index": 1, "factor": "-1.0 K / s", "function": "affine", "parallel": 0, "type": "Shear", }, { "dim_index": 1, "function": "FFT" }, ], } assert serialize == expected recovered = sp.SignalProcessor.parse_dict_with_units(serialize) assert recovered == processor
def test_Gaussian(): FWHM = 200 * 2.354820045030949 PS_2 = [ sp.IFFT(dim_index=0), sp.apodization.Gaussian(FWHM=f"{FWHM} Hz", dim_index=0, dv_index=[0, 1]), sp.FFT(dim_index=0), ] PS_3 = [ sp.IFFT(dim_index=0), sp.apodization.Gaussian(FWHM=f"{FWHM} Hz", dim_index=0, dv_index=None), sp.FFT(dim_index=0), ] post_sim = sp.SignalProcessor(operations=PS_2) dataset = post_sim.apply_operations( dataset=sim.methods[0].simulation.copy()) _, y0, y1, _ = dataset.to_list() sigma = 200 test = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-((freqHz / sigma)**2) / 2) assert np.allclose(y0, y1), "Gaussian apodization on two dv are not equal." assert np.allclose(test / test.sum(), y0 / y0.sum(), atol=1e-04), "Gaussian apodization amplitude failed" # test None for dv_index post_sim = sp.SignalProcessor(operations=PS_3) dataset = post_sim.apply_operations( dataset=sim.methods[0].simulation.copy()) _, y0, y1, y2 = dataset.to_list() assert np.allclose( y0, y1), "Gaussian apodization on dv at 0 and 1 are unequal." assert np.allclose( y0, y2), "Gaussian apodization on dv at 0 and 2 are unequal." assert np.allclose(test / test.sum(), y0 / y0.sum(), atol=1e-04), "Gaussian apodization amplitude failed"
def test_scale(): PS_0 = [sp.Scale(factor=10)] post_sim = sp.SignalProcessor(operations=PS_0) dataset = post_sim.apply_operations( dataset=sim.methods[0].simulation.copy()) _, y0, y1, y2 = sim.methods[0].simulation.to_list() _, y0_, y1_, y2_ = dataset.to_list() # cast complex dataset assert np.allclose(y0_, y0 * 10), "Scaling failed" assert np.allclose(y1_, y1 * 10), "Scaling failed" assert np.allclose(y2_, y2 * 10), "Scaling failed"
def test_scale(): c_inc = csdm_object.x[1].increment.value c_off = csdm_object.x[1].coordinates_offset.value processor = sp.SignalProcessor( operations=[sp.affine.Scale(factor=2, dim_index=1)]) scaled_dataset = processor.apply_operations(dataset=csdm_object) s_inc = scaled_dataset.x[1].increment.value s_off = scaled_dataset.x[1].coordinates_offset.value assert s_inc == 2 * c_inc assert s_off == 2 * c_off
def setup_signal_processor(): op_list1 = [ sp.IFFT(dim_index=0), sp.apodization.Exponential(FWHM=100), sp.apodization.Gaussian(FWHM=200), sp.FFT(dim_index=0), sp.Scale(factor=10), sp.baseline.ConstantOffset(offset=43.1), sp.Linear(amplitude=32.9, offset=13.4), ] op_list2 = [ sp.Scale(factor=20), sp.baseline.ConstantOffset(offset=-43.1), sp.Linear(amplitude=1.2, offset=0.4), ] return [sp.SignalProcessor(operations=op) for op in [op_list1, op_list2]]
def test_baseline_constant_offset(): dataset_in = generate_dataset() PS_0 = [sp.baseline.ConstantOffset(offset=10)] operator = sp.SignalProcessor(operations=PS_0) dataset_out = operator.apply_operations(dataset=dataset_in.copy()) _, y0, y1, y2 = dataset_in.to_list() _, y0_, y1_, y2_ = dataset_out.to_list() for in_, out_ in zip([y0, y1, y2], [y0_, y1_, y2_]): assert np.allclose(out_.max() - in_.max(), 10), "Offset failed" py_dict = { "function": "baseline", "type": "ConstantOffset", "offset": 10.0, } setup_read_write(PS_0[0], py_dict, sp.baseline.ConstantOffset)
def test_shear_01(): processor = sp.SignalProcessor(operations=[ sp.IFFT(dim_index=1), sp.affine.Shear(factor="-1 K/s", dim_index=1, parallel=0), sp.FFT(dim_index=1), ]) shear_dataset = processor.apply_operations(dataset=csdm_object) index = np.where(shear_dataset.y[0].components[0] > 0.99999999) a = np.arange(40) assert np.allclose(index, [a, a]) # complex_fft dim=0 to false csdm_object.x[0].complex_fft = False csdm_object.x[1].complex_fft = True shear_dataset = processor.apply_operations(dataset=csdm_object) index = np.where(shear_dataset.y[0].components[0] > 0.99999999) a1 = np.arange(20) b1 = a1 + 20 b = np.append(b1, a1) assert np.allclose(index, [a, b]) # complex_fft dim=1 to false csdm_object.x[0].complex_fft = True csdm_object.x[1].complex_fft = False shear_dataset = processor.apply_operations(dataset=csdm_object) index = np.where(shear_dataset.y[0].components[0] > 0.99999999) b = np.arange(40) b[1:] = a[::-1][:-1] assert np.allclose(index, [a, b]) # both complex_fft set to false csdm_object.x[0].complex_fft = False csdm_object.x[1].complex_fft = False shear_dataset = processor.apply_operations(dataset=csdm_object) index = np.where(shear_dataset.y[0].components[0] > 0.99999999) a1 = np.arange(21)[::-1] b1 = a1[1:-1] + 20 b = np.append(a1, b1) assert np.allclose(index, [a, b])
def test_SkewedGaussian(): # TODO: update this test for multiple skews and using np.convolve skew = 2 FWHM = 200 * 2.354820045030949 PS_2 = [ sp.IFFT(dim_index=0), sp.apodization.SkewedGaussian(skew=skew, FWHM=f"{FWHM} Hz", dim_index=0, dv_index=[0, 1]), sp.FFT(dim_index=0), ] post_sim = sp.SignalProcessor(operations=PS_2) dataset = post_sim.apply_operations( dataset=sim.methods[0].simulation.copy()) _, y0, y1, _ = dataset.to_list() assert np.allclose(y0, y1), "Gaussian apodization on two dv are not equal."
def test_Lorentzian(): PS_1 = [ sp.IFFT(dim_index=0), sp.apodization.Exponential(FWHM="200 Hz", dim_index=0, dv_index=0), sp.FFT(dim_index=0), ] post_sim = sp.SignalProcessor(operations=PS_1) dataset = post_sim.apply_operations( dataset=sim.methods[0].simulation.copy()) _, y0, y1, y2 = dataset.to_list() FWHM = 200 test = (FWHM / 2) / (np.pi * (freqHz**2 + (FWHM / 2)**2)) assert np.allclose(y1, y2) assert np.all(y0 != y1) assert np.allclose(test / test.sum(), y0 / y0.sum(), atol=1e-04), "Lorentzian apodization amplitude failed" assert np.allclose( y1.sum(), y0.sum()), "Area not conserved after Lorentzian apodization"
def test_Mask(): one_mask = np.zeros(shape=len(freqHz)) PS_5 = [ sp.IFFT(dim_index=0), sp.apodization.Mask(mask=one_mask, dim_index=0, dv_index=[0, 1]), sp.FFT(dim_index=0), ] post_sim = sp.SignalProcessor(operations=PS_5) dataset = post_sim.apply_operations( dataset=sim.methods[0].simulation.copy()) _, y0, y1, _ = dataset.to_list() _, test_y0, test_y1, _ = sim.methods[0].simulation.to_list() nonzero_y0 = np.count_nonzero(y0) nonzero_y1 = np.count_nonzero(y1) assert np.allclose(y0, y1), "Mask on two dv are not equal." assert np.allclose(nonzero_y0, nonzero_y1, atol=1e-04), "Mask apodization amplitude failed"
def test_7(): site = Site(isotope="23Na") sys = SpinSystem(sites=[site], abundance=50) sim = Simulator() sim.spin_systems = [sys, sys] sim.methods = [BlochDecayCTSpectrum(channels=["23Na"])] sim.methods[0].experiment = cp.as_csdm(np.zeros(1024)) processor = sp.SignalProcessor(operations=[ sp.IFFT(dim_index=0), sp.apodization.Gaussian(FWHM="0.2 kHz", dim_index=0), sp.FFT(dim_index=0), ]) def test_array(): sim.run() dataset = processor.apply_operations(sim.methods[0].simulation) data_sum = 0 for dv in dataset.y: data_sum += dv.components[0] params = sf.make_LMFIT_params(sim, processor) a = sf.LMFIT_min_function(params, sim, processor) np.testing.assert_almost_equal(-a, data_sum, decimal=8) dat = sf.add_csdm_dvs(dataset.real) fits = sf.bestfit(sim, processor) assert sf.add_csdm_dvs(fits[0]) == dat res = sf.residuals(sim, processor) assert res[0] == -dat test_array() sim.config.decompose_spectrum = "spin_system" test_array()
def test_5_multi_spin_systems(): sim = Simulator() spin_system1 = {"sites": [H], "abundance": "100%"} system_object1 = SpinSystem.parse_dict_with_units(spin_system1) spin_system2 = {"sites": [C], "abundance": "60%"} system_object2 = SpinSystem.parse_dict_with_units(spin_system2) sim.spin_systems += [system_object1, system_object2] post_sim = sp.SignalProcessor(operations=op_list) params = sf.make_LMFIT_params(sim, post_sim) valuesdict_system = { "sys_0_site_0_isotropic_chemical_shift": 10, "sys_0_site_0_shielding_symmetric_zeta": 5, "sys_0_site_0_shielding_symmetric_eta": 0.1, "sys_0_site_0_shielding_symmetric_alpha": 3.12, "sys_0_site_0_shielding_symmetric_gamma": 0.341, "sys_0_abundance": 62.5, "sys_1_site_0_isotropic_chemical_shift": -10, "sys_1_site_0_shielding_symmetric_zeta": 15, "sys_1_site_0_shielding_symmetric_eta": 0.2, "sys_1_site_0_shielding_symmetric_beta": 4.12, "sys_1_abundance": 37.5, } compare_result(params, valuesdict_system, sim)
def test_6_coupled(): sim = Simulator() spin_system = {"sites": [H, C], "couplings": [CH], "abundance": "100%"} system_object = SpinSystem.parse_dict_with_units(spin_system) sim.spin_systems += [system_object] post_sim = sp.SignalProcessor(operations=op_list) params = sf.make_LMFIT_params(sim, post_sim) valuesdict_system = { "sys_0_site_0_isotropic_chemical_shift": 10, "sys_0_site_0_shielding_symmetric_zeta": 5, "sys_0_site_0_shielding_symmetric_eta": 0.1, "sys_0_site_0_shielding_symmetric_alpha": 3.12, "sys_0_site_0_shielding_symmetric_gamma": 0.341, "sys_0_site_1_isotropic_chemical_shift": -10, "sys_0_site_1_shielding_symmetric_zeta": 15, "sys_0_site_1_shielding_symmetric_eta": 0.2, "sys_0_site_1_shielding_symmetric_beta": 4.12, "sys_0_coupling_0_isotropic_j": 10, "sys_0_coupling_0_j_symmetric_zeta": 60, "sys_0_coupling_0_j_symmetric_eta": 0.4, "sys_0_abundance": 100, } compare_result(params, valuesdict_system, sim)
maf.plot() plt.show() # %% # Create the Simulator object, add the method and spin system objects, and run the # simulation. sim = Simulator(spin_systems=spin_systems, methods=[maf]) sim.run() # %% # Add post-simulation signal processing. csdm_dataset = sim.methods[0].simulation processor = sp.SignalProcessor( operations=[ sp.IFFT(dim_index=(0, 1)), sp.apodization.Gaussian(FWHM="50 Hz", dim_index=0), sp.apodization.Gaussian(FWHM="50 Hz", dim_index=1), sp.FFT(dim_index=(0, 1)), ] ) processed_dataset = processor.apply_operations(dataset=csdm_dataset).real processed_dataset /= processed_dataset.max() # %% # The plot of the simulation after signal processing. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") cb = ax.imshow(processed_dataset.T, aspect="auto", cmap="gist_ncar_r") plt.colorbar(cb) ax.invert_xaxis() ax.invert_yaxis() plt.tight_layout()
def test_MQMAS(): site = Site( isotope="87Rb", isotropic_chemical_shift=-9, shielding_symmetric={ "zeta": 100, "eta": 0 }, quadrupolar={ "Cq": 3.5e6, "eta": 0.36, "beta": 70 / 180 * np.pi }, ) spin_system = SpinSystem(sites=[site]) B0 = 9.394 method = Method( channels=["87Rb"], magnetic_flux_density=B0, rotor_frequency=1e12, spectral_dimensions=[ { "count": 128, "spectral_width": 20000, "events": [{ "transition_queries": [{ "ch1": { "P": [-3], "D": [0] } }] }], }, { "count": 128, "spectral_width": 20000, "events": [{ "transition_queries": [{ "ch1": { "P": [-1], "D": [0] } }] }], }, ], ) sim = Simulator() sim.spin_systems = [spin_system] sim.methods = [method] sim.config.integration_volume = "hemisphere" sim.run() # process k = 21 / 27 # shear factor processor = sp.SignalProcessor(operations=[ sp.IFFT(dim_index=1), aft.Shear(factor=-k, dim_index=1, parallel=0), aft.Scale(factor=1 + k, dim_index=1), sp.FFT(dim_index=1), ]) processed_dataset = processor.apply_operations( dataset=sim.methods[0].simulation) processed_dataset = processed_dataset.real # Since there is a single site, after the shear and scaling transformations, there # should be a single perak along the isotropic dimension at index 70. # The isotropic coordinate of this peak is given by # w_iso = (17.8)*iso_shift + 1e6/8 * (vq/v0)^2 * (eta^2 / 3 + 1) # ref: D. Massiot et al. / Solid State Nuclear Magnetic Resonance 6 (1996) 73-83 iso_slice = processed_dataset[40, :] assert np.argmax(iso_slice.y[0].components[0]) == 70 # calculate the isotropic coordinate spin = method.channels[0].spin w0 = method.channels[0].gyromagnetic_ratio * B0 * 1e6 wq = 3 * 3.5e6 / (2 * spin * (2 * spin - 1)) w_iso = -9 * 17 / 8 + 1e6 / 8 * (wq / w0)**2 * ((0.36**2) / 3 + 1) # the coordinate from spectrum w_iso_spectrum = processed_dataset.x[1].coordinates[70].value np.testing.assert_almost_equal(w_iso, w_iso_spectrum, decimal=2) # The projection onto the MAS dimension should be the 1D block decay central # transition spectrum mas_slice = processed_dataset.sum(axis=1).y[0].components[0] # MAS spectrum method = BlochDecayCTSpectrum( channels=["87Rb"], magnetic_flux_density=9.4, rotor_frequency=np.inf, spectral_dimensions=[{ "count": 128, "spectral_width": 20000 }], ) sim = Simulator() sim.spin_systems = [spin_system] sim.methods = [method] sim.config.integration_volume = "hemisphere" sim.run() data = sim.methods[0].simulation.y[0].components[0] np.testing.assert_almost_equal(data / data.max(), mas_slice / mas_slice.max(), decimal=2, err_msg="not equal")
ax = plt.subplot(projection="csdm") cb = ax.imshow(dataset.real / dataset.real.max(), aspect="auto", cmap="gist_ncar_r") plt.colorbar(cb) ax.invert_xaxis() ax.invert_yaxis() plt.tight_layout() plt.show() # %% # Add post-simulation signal processing. processor = sp.SignalProcessor(operations=[ # Gaussian convolution along both dimensions. sp.IFFT(dim_index=(0, 1)), sp.apodization.Gaussian(FWHM="0.08 kHz", dim_index=0), sp.apodization.Gaussian(FWHM="0.22 kHz", dim_index=1), sp.FFT(dim_index=(0, 1)), ]) processed_dataset = processor.apply_operations( dataset=sim.methods[0].simulation) processed_dataset /= processed_dataset.max() # %% # The plot of the simulation after signal processing. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") cb = ax.imshow(processed_dataset.real, cmap="gist_ncar_r", aspect="auto") plt.colorbar(cb) ax.set_ylim(-40, -70) ax.set_xlim(-20, -60)
# evaluate the transition pathways once and store it as follows for sys in spin_systems: sys.transition_pathways = MAS_CT.get_transition_pathways(sys) # %% # **Step 3:** Create the Simulator object and add the method and spin system objects. sim = Simulator(spin_systems=spin_systems, methods=[MAS_CT]) sim.config.decompose_spectrum = "spin_system" sim.run() # %% # **Step 4:** Create a SignalProcessor class object and apply the post-simulation # signal processor operations. processor = sp.SignalProcessor(operations=[ sp.IFFT(), sp.apodization.Gaussian(FWHM="100 Hz"), sp.FFT(), sp.Scale(factor=200.0), ]) processed_dataset = processor.apply_operations( dataset=sim.methods[0].simulation).real # %% # **Step 5:** The plot of the dataset and the guess spectrum. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(experiment, color="black", linewidth=0.5, label="Experiment") ax.plot(processed_dataset, linewidth=2, alpha=0.6) ax.set_xlim(100, -50) plt.legend() plt.grid() plt.tight_layout()
# %% # For 2D spinning sideband simulation, set the number of spinning sidebands in the # Simulator.config object to `spectral_width/rotor_frequency` along the sideband # dimension. sim.config.number_of_sidebands = 20 # run the simulation. sim.run() # %% # Apply post-simulation processing. Here, we apply a Lorentzian line broadening to the # isotropic dimension. dataset = sim.methods[0].simulation processor = sp.SignalProcessor(operations=[ sp.IFFT(dim_index=0), sp.apodization.Exponential(FWHM="100 Hz", dim_index=0), sp.FFT(dim_index=0), ]) processed_dataset = processor.apply_operations(dataset=dataset).real processed_dataset /= processed_dataset.max() # %% # The plot of the simulation. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") cb = ax.imshow(processed_dataset, aspect="auto", cmap="gist_ncar_r", vmax=0.5) plt.colorbar(cb) ax.invert_xaxis() ax.invert_yaxis() plt.tight_layout() plt.show()
sim = Simulator(spin_systems=spin_systems, methods=[method]) sim.run() # The plot of the simulation before signal processing. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(sim.methods[0].simulation.real, color="black", linewidth=1) ax.invert_xaxis() plt.tight_layout() plt.show() # %% # Add post-simulation signal processing. processor = sp.SignalProcessor(operations=[ sp.IFFT(), sp.apodization.Exponential(FWHM="30 Hz"), sp.apodization.Gaussian(FWHM="145 Hz"), sp.FFT(), ]) processed_dataset = processor.apply_operations( dataset=sim.methods[0].simulation) # The plot of the simulation after signal processing. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(processed_dataset.real, color="black", linewidth=1) ax.invert_xaxis() plt.tight_layout() plt.show() # %% # .. [#f1] Grandinetti, P. J., Baltisberger, J. H., Farnan, I., Stebbins, J. F.,
# :py:class:`~mrsimulator.signal_processor.baseline.ConstantOffset` class to # offset the baseline of a dataset by a constant. # # Below we import the necessary modules import csdmpy as cp import numpy as np from mrsimulator import signal_processor as sp # sphinx_gallery_thumbnail_number = 1 # %% # First we create ``processor``, an instance of the # :py:class:`~mrsimulator.signal_processor.SignalProcessor` class. The required # attribute of the SignalProcessor class, *operations*, is a list of operations to which # we add a :py:class:`~mrsimulator.signal_processor.baseline.ConstantOffset` object. processor = sp.SignalProcessor( operations=[sp.baseline.ConstantOffset(offset=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 # 0 hz 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="1 Hz", complex_fft=True) ], ) # %%
# evaluate the transition pathways once and store it as follows for sys in spin_systems: sys.transition_pathways = MAS.get_transition_pathways(sys) # %% # **Step 3:** Create the Simulator object and add the method and spin system objects. sim = Simulator(spin_systems=spin_systems, methods=[MAS]) sim.run() # %% # **Step 4:** Create a SignalProcessor class object and apply the post-simulation # signal processing operations. processor = sp.SignalProcessor(operations=[ sp.IFFT(), sp.apodization.Exponential(FWHM="0.3 kHz"), sp.FFT(), sp.Scale(factor=300), sp.baseline.ConstantOffset(offset=-2), ]) processed_dataset = processor.apply_operations( dataset=sim.methods[0].simulation).real # %% # **Step 5:** The plot of the dataset and the guess spectrum. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(experiment, color="black", linewidth=0.5, label="Experiment") ax.plot(processed_dataset, linewidth=2, alpha=0.6, label="Guess Spectrum") ax.set_xlim(150, -150) plt.legend() plt.grid()
import numpy as np from mrsimulator import signal_processor as sp # sphinx_gallery_thumbnail_number = 1 # %% # First we create ``processor``, and instance of the # :py:class:`~mrsimulator.signal_processor.SignalProcessor` class. The required # attribute of the SignalProcessor class, *operations*, is a list of operations to which # we add a :py:class:`~mrsimulator.signal_processor.apodization.TopHat` object # sandwiched between two Fourier transformations. Here the window is between # 1 and 9 seconds. processor = sp.SignalProcessor( 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)], )
from mrsimulator import signal_processor as sp # sphinx_gallery_thumbnail_number = 1 # %% # First we create ``processor``, an instance of the # :py:class:`~mrsimulator.signal_processor.SignalProcessor` class. The required # attribute of the SignalProcessor class, *operations*, is a list of operations to which # we add a :py:class:`~mrsimulator.signal_processor.baseline.Polynomial` object. # # The required argument for the polynomial offset is *polynomial_dictionary* which is a # Python dict defining the polynomial coefficients. An arbitrary number of coefficients # may be passed. processor = sp.SignalProcessor(operations=[ sp.baseline.Polynomial(polynomial_dictionary={ "c0": 0.2, "c2": 0.00001 }) ]) # %% # 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
# %% # **Guess Spectrum** # Simulation # ---------- sim = Simulator(spin_systems=spin_systems, methods=[PASS]) sim.run() # Post Simulation Processing # -------------------------- processor = sp.SignalProcessor( operations=[ # Lorentzian convolution along the isotropic dimensions. sp.FFT(dim_index=0), sp.apodization.Exponential(FWHM="50 Hz"), sp.IFFT(dim_index=0), sp.Scale(factor=212260), ] ) processed_dataset = processor.apply_operations(dataset=sim.methods[0].simulation).real # Plot of the guess Spectrum # -------------------------- plt.figure(figsize=(8, 3.5)) ax = plt.subplot(projection="csdm") ax.contour(mat_dataset, colors="k", **options) ax.contour(processed_dataset, colors="r", linestyles="--", **options) ax.set_xlim(180, 15) plt.grid() plt.tight_layout()
for sys in spin_systems: sys.transition_pathways = MAS_CT.get_transition_pathways(sys) # %% # **Guess Model Spectrum** # Simulation # ---------- sim = Simulator(spin_systems=spin_systems, methods=[MAS_CT]) sim.run() # Post Simulation Processing # -------------------------- processor = sp.SignalProcessor(operations=[ sp.IFFT(), sp.apodization.Exponential(FWHM="100 Hz"), sp.FFT(), sp.Scale(factor=200), ]) processed_dataset = processor.apply_operations( dataset=sim.methods[0].simulation).real # Plot of the guess Spectrum # -------------------------- plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(experiment, "k", linewidth=1, label="Experiment") ax.plot(processed_dataset, "r", alpha=0.75, linewidth=1, label="guess spectrum")
import csdmpy as cp import numpy as np from mrsimulator import signal_processor as sp # sphinx_gallery_thumbnail_number = 1 # %% # First we create ``processor``, an instance of the # :py:class:`~mrsimulator.signal_processor.SignalProcessor` class. The required # attribute of the SignalProcessor class, *operations*, is a list of operations to which # we add a :py:class:`~mrsimulator.signal_processor.apodization.Exponential` object # sandwiched between two Fourier transformations. processor = sp.SignalProcessor( operations=[ sp.IFFT(), sp.apodization.Exponential(FWHM="75 Hz"), sp.FFT(), ] ) # %% # 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 # 0 Hz. 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="1 Hz", complex_fft=True)], )