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 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_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 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_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()
# the method. 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,
plt.figure(figsize=(4.25, 3.0)) 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)
# %% # **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() plt.show()
# Add the methods to the Simulator object and run the simulation # Add the methods. sim.methods = [method_13C, method_15N] # Run the simulation. sim.run() # Get the simulation dataset from the respective methods. dataset_13C = sim.methods[0].simulation # method at index 0 is 13C Bloch decay method. dataset_15N = sim.methods[1].simulation # method at index 1 is 15N Bloch decay method. # %% # Add post-simulation signal processing. processor = sp.SignalProcessor( operations=[sp.IFFT(), sp.apodization.Exponential(FWHM="10 Hz"), sp.FFT()] ) # apply post-simulation processing to dataset_13C processed_dataset_13C = processor.apply_operations(dataset=dataset_13C).real # apply post-simulation processing to dataset_15N processed_dataset_15N = processor.apply_operations(dataset=dataset_15N).real # %% # The plot of the simulation after signal processing. fig, ax = plt.subplots( 1, 2, subplot_kw={"projection": "csdm"}, sharey=True, figsize=(9, 4) ) ax[0].plot(processed_dataset_13C, color="black", linewidth=0.5) ax[0].invert_xaxis()
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")
# %% # Simulate the spectrum. 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="70 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] Hansen, M. R., Jakobsen, H. J., Skibsted, J., :math:`^{29}\text{Si}`