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 CSA_VAS_method(): return BlochDecaySpectrum( channels=["29Si"], rotor_frequency=5000, rotor_angle=1.57079, spectral_dimensions=[dict(spectral_width=25000)], )
def setup_test(spin_system, volume="octant", sw=25000, n_gamma=500): mth_kwargs = { "channels": [spin_system.sites[0].isotope.symbol], "spectral_dimensions": [{ "count": 1024, "spectral_width": sw }], } data = [] for angle in [0, 54.735, 90]: method = BlochDecaySpectrum(rotor_angle=angle * np.pi / 180, rotor_frequency=0, **mth_kwargs) sim = Simulator(spin_systems=[spin_system], methods=[method]) sim.config.integration_volume = volume sim.config.number_of_gamma_angles = 1 if angle == 0 else n_gamma sim.run(auto_switch=False) res = sim.methods[0].simulation.y[0].components[0] res /= res.max() data.append(res) # plt.plot(data[0]) # plt.plot(data[1], "--") # plt.plot(data[2], "-.") # plt.show() np.testing.assert_almost_equal(data[0], data[1], decimal=2) np.testing.assert_almost_equal(data[0], data[2], decimal=1.6)
def test_empty_spin_sys_simulator(): sim = Simulator() sim.methods = [ BlochDecaySpectrum(channels=["1H"], spectral_dimensions=[{"count": 10}]) ] sim.config.decompose_spectrum = "spin_system" sim.run() assert np.allclose(sim.methods[0].simulation.y[0].components[0], 0)
def test_two_site_no_coupling_test(): site1 = Site( isotope="29Si", isotropic_chemical_shift=10, shielding_symmetric={ "zeta": 5, "eta": 0 }, ) site2 = Site( isotope="29Si", isotropic_chemical_shift=-10, shielding_symmetric={ "zeta": -5, "eta": 0 }, ) iso_two_site = [SpinSystem(sites=[site1, site2])] iso_single_sites = [SpinSystem(sites=[site1]), SpinSystem(sites=[site2])] sim1 = Simulator() sim1.spin_systems += iso_two_site sim1.methods += [ BlochDecaySpectrum( channels=["29Si"], spectral_dimensions=[dict(count=2048, spectral_width=25000)], ) ] sim1.run() sim2 = Simulator() sim2.spin_systems += iso_single_sites sim2.methods += [ BlochDecaySpectrum( channels=["29Si"], spectral_dimensions=[dict(count=2048, spectral_width=25000)], ) ] sim2.run() data1 = (sim1.methods[0].simulation / 2).y[0].components data2 = sim2.methods[0].simulation.y[0].components assert np.allclose(data1, data2)
def pre_setup(): site_1 = Site(isotope="13C", shielding_symmetric={"zeta": 50, "eta": 0.5}) spin_system = SpinSystem(sites=[site_1]) method = BlochDecaySpectrum(channels=["13C"], spectral_dimensions=[{ "count": 1024, "spectral_width": 25000 }]) sim = Simulator(spin_systems=[spin_system], methods=[method]) return sim
def test_simulator_2(): sim = Simulator() sim.spin_systems = [ SpinSystem( label="Test", sites=[Site(isotope="1H"), Site(isotope="23Na")], couplings=[Coupling(site_index=[0, 1], isotropic_j=15)], ) ] sim.methods = [ BlochDecaySpectrum( channels=["1H"], spectral_dimensions=[{"count": 10}], experiment=cp.as_csdm(np.arange(10)), ) ] sim.name = "test" sim.label = "test0" sim.description = "testing-testing 1.2.3" sim.config.decompose_spectrum = "spin_system" sim.run() # save sim.save("test_sim_save.temp") sim_load = Simulator.load("test_sim_save.temp") sim_load_dataset = sim_load.methods[0].simulation sim_dataset = sim.methods[0].simulation sim_load_dataset._timestamp = "" assert sim_load_dataset.dict() == sim_dataset.dict() sim_load.methods[0].simulation = None sim.methods[0].simulation = None assert sim_load.spin_systems == sim.spin_systems assert sim_load.methods == sim.methods assert sim_load.name == sim.name assert sim_load.description == sim.description os.remove("test_sim_save.temp")
def setup_simulator(): site = Site( isotope="23Na", isotropic_chemical_shift=32, shielding_symmetric={ "zeta": -120, "eta": 0.1 }, quadrupolar={ "Cq": 1e5, "eta": 0.31, "beta": 5.12 }, ) sys = SpinSystem(sites=[site], abundance=0.123) sim = Simulator() sim.spin_systems.append(sys) sim.methods.append( BlochDecayCTSpectrum(channels=["2H"], rotor_frequency=1e3)) sim.methods.append( BlochDecaySpectrum(channels=["2H"], rotor_frequency=12.5e3)) sim.methods.append(ThreeQ_VAS(channels=["27Al"])) sim.methods.append(SSB2D(channels=["17O"], rotor_frequency=35500)) return sim
def CSA_static_method(): return BlochDecaySpectrum(channels=["29Si"], spectral_dimensions=[dict(spectral_width=25000)])
# corresponding amplitudes. spin_systems = single_site_system_generator( isotope="29Si", isotropic_chemical_shift=iso, shielding_symmetric={"zeta": zeta, "eta": eta}, abundance=pdf, ) # %% # **Method:** # # Let's also create a Bloch decay spectrum method. method = BlochDecaySpectrum( channels=["29Si"], rotor_frequency=0, # in Hz rotor_angle=0, # in rads spectral_dimensions=[ SpectralDimension(spectral_width=25000, reference_offset=-7000) # values in Hz ], ) # %% # The above method simulates a static :math:`^{29}\text{Si}` spectrum at 9.4 T field # (default value). # # **Simulator:** # # Now that we have the spin systems and the method, create the simulator object and # add the respective objects. sim = Simulator(spin_systems=spin_systems, methods=[method]) # %%
# spectral/spectroscopic dimension. # # In the following example, we set up a Bloch decay spectrum method where the # spectral/spectroscopic dimension information, i.e., count, spectral_width, and the # reference_offset, is extracted from the CSDM dimension metadata using the # :func:`~mrsimulator.utils.get_spectral_dimensions` utility function. The remaining # attribute values are set to the experimental conditions. # # get the count, spectral_width, and reference_offset information from the experiment. spectral_dims = get_spectral_dimensions(experiment) MAS = BlochDecaySpectrum( channels=["31P"], magnetic_flux_density=9.395, # in T rotor_frequency=6000, # in Hz spectral_dimensions=spectral_dims, experiment=experiment, # experimental dataset ) # A method object queries every spin system for a list of transition pathways that are # relevant to the given method. Since the method and the number of spin systems remains # unchanged during the least-squares analysis, a one-time query is sufficient. To avoid # querying for the transition pathways at every iteration in a least-squares fitting, # 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])
def SSB2D_setup(ist, vr, method_type): sites = [ Site( isotope=ist, isotropic_chemical_shift=29, shielding_symmetric={ "zeta": -70, "eta": 0.000 }, ), Site( isotope=ist, isotropic_chemical_shift=44, shielding_symmetric={ "zeta": -96, "eta": 0.166 }, ), Site( isotope=ist, isotropic_chemical_shift=57, shielding_symmetric={ "zeta": -120, "eta": 0.168 }, ), ] spin_systems = [SpinSystem(sites=[s]) for s in sites] B0 = 11.7 sq_tq = {"transition_queries": [{"ch1": {"P": [-1]}}]} if method_type == "PASS": method = SSB2D( channels=[ist], magnetic_flux_density=B0, # in T rotor_frequency=vr, spectral_dimensions=[ { "count": 32, "spectral_width": 32 * vr, # in Hz "label": "Anisotropic dimension", }, # The last spectral dimension block is the direct-dimension { "count": 2048, "spectral_width": 2e4, # in Hz "reference_offset": 5e3, # in Hz "label": "Fast MAS dimension", }, ], ) else: method = Method( channels=[ist], magnetic_flux_density=B0, # in T rotor_frequency=1e12, spectral_dimensions=[ { "count": 64, "spectral_width": 8e4, # in Hz "label": "Anisotropic dimension", "events": [{ "rotor_angle": 90 * 3.14159 / 180, **sq_tq }], }, # The last spectral dimension block is the direct-dimension { "count": 2048, "spectral_width": 2e4, # in Hz "reference_offset": 5e3, # in Hz "label": "Fast MAS dimension", "events": [sq_tq], }, ], affine_matrix=[[1, -1], [0, 1]], ) sim = Simulator() sim.spin_systems = spin_systems # add spin systems sim.methods = [method] # add the method. sim.run() data_ssb = sim.methods[0].simulation dim_ssb = data_ssb.x[0].coordinates.value if method_type == "PASS": bloch = BlochDecaySpectrum( channels=[ist], magnetic_flux_density=B0, # in T rotor_frequency=vr, # in Hz spectral_dimensions=[ { "count": 32, "spectral_width": 32 * vr, # in Hz "reference_offset": 0, # in Hz "label": "MAS dimension", }, ], ) else: bloch = BlochDecaySpectrum( channels=[ist], magnetic_flux_density=B0, # in T rotor_frequency=vr, # in Hz rotor_angle=90 * 3.14159 / 180, spectral_dimensions=[ { "count": 64, "spectral_width": 8e4, # in Hz "reference_offset": 0, # in Hz "label": "MAS dimension", }, ], ) for i in range(3): iso = spin_systems[i].sites[0].isotropic_chemical_shift sys = spin_systems[i].copy() sys.sites[0].isotropic_chemical_shift = 0 sim2 = Simulator() sim2.spin_systems = [sys] # add spin systems sim2.methods = [bloch] # add the method. sim2.run() index = np.where(dim_ssb < iso)[0][-1] one_d_section = data_ssb.y[0].components[0][:, index] one_d_section /= one_d_section.max() one_d_sim = sim2.methods[0].simulation.y[0].components[0] one_d_sim /= one_d_sim.max() np.testing.assert_almost_equal(one_d_section, one_d_sim, decimal=4)
__email__ = "*****@*****.**" sim = Simulator() the_site = {"isotope": "1H", "isotropic_chemical_shift": "0 ppm"} the_spin_system = {"name": "site A", "sites": [the_site], "abundance": "80%"} spin_system_object = SpinSystem.parse_dict_with_units(the_spin_system) sim.spin_systems += [ spin_system_object, spin_system_object, spin_system_object ] sim.config.decompose_spectrum = "spin_system" sim.methods += [ BlochDecaySpectrum( channels=["1H"], magnetic_flux_density=9.4, spectral_dimensions=[{ "count": 65536, "spectral_width": 25000 }], ) ] sim.run() freqHz = sim.methods[0].spectral_dimensions[0].coordinates_Hz() 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()
isotropic_chemical_shift=5.0, # in ppm, shielding_symmetric=SymmetricTensor(zeta=-80, eta=0.5), # zeta in Hz ) spin_systems = [SpinSystem(sites=[P_31])] # %% # **Method** # Get the spectral dimension parameters from the experiment. spectral_dims = get_spectral_dimensions(experiment) static1D = BlochDecaySpectrum( channels=["31P"], magnetic_flux_density=9.395, # in T rotor_frequency=0, # in Hz rotor_angle=0, # in rads spectral_dimensions=spectral_dims, experiment=experiment, # experimental dataset ) # Optimize the script by pre-setting the transition pathways for each spin system from # the method. for sys in spin_systems: sys.transition_pathways = static1D.get_transition_pathways(sys) # %% # **Guess Model Spectrum** # Simulation # ---------- sim = Simulator(spin_systems=spin_systems, methods=[static1D])
) ], ) for beta in beta_orientation ] # %% # Next, we create methods to simulate the sideband manifolds for the above spin # systems at four spinning rates: 3 kHz, 5 kHz, 8 kHz, 12 kHz. spin_rates = [3e3, 5e3, 8e3, 12e3] # in Hz # The variable `methods` is a list of four BlochDecaySpectrum methods. methods = [ BlochDecaySpectrum( channels=["13C"], magnetic_flux_density=9.4, # in T rotor_frequency=vr, # in Hz spectral_dimensions=[SpectralDimension(count=2048, spectral_width=8.0e4)], ) for vr in spin_rates ] # %% # Create the Simulator object and add the method and the spin system objects. sim = Simulator(spin_systems=spin_systems, methods=methods) sim.config.integration_volume = "hemisphere" # set averaging to hemisphere # config to decompose spectrum to individual spin systems. sim.config.decompose_spectrum = "spin_system" # %% # The run command will simulate twelve spectra corresponding to the three spin systems # evaluated at four different methods (spinning speeds).
) spin_systems = [ SpinSystem(sites=[Al_1], name="AlO4"), SpinSystem(sites=[Al_2], name="AlO6"), ] # %% # **Method** # Get the spectral dimension parameters from the experiment. spectral_dims = get_spectral_dimensions(experiment) MAS = BlochDecaySpectrum( channels=["27Al"], magnetic_flux_density=9.395, # in T rotor_frequency=15250, # in Hz spectral_dimensions=spectral_dims, experiment=experiment, # add the measurement to the method. ) # Optimize the script by pre-setting the transition pathways for each spin system from # the method. for sys in spin_systems: sys.transition_pathways = MAS.get_transition_pathways(sys) # %% # **Guess Spectrum** # Simulation # ---------- sim = Simulator(spin_systems=spin_systems, methods=[MAS])
def test_more_spectral_dimensions(): error = "Method requires exactly 1 spectral dimensions, given 2." with pytest.raises(ValueError, match=f".*{error}.*"): BlochDecaySpectrum(spectral_dimensions=[{}, {}])
"1 kHz", "rotor_angle": "54.735 deg", "spectral_dimensions": [{ "count": 2048, "spectral_width": "25 kHz", "reference_offset": "0 Hz" }], } sim = Simulator() sim.spin_systems = [ SpinSystem.parse_dict_with_units(item) for item in spin_systems ] sim.methods = [ BlochDecaySpectrum.parse_dict_with_units(method1), BlochDecaySpectrum.parse_dict_with_units(method2), ] sim.run() freq1, amp1 = sim.methods[0].simulation.to_list() freq2, amp2 = sim.methods[1].simulation.to_list() fig, ax = plt.subplots(1, 2, figsize=(8, 3.5)) ax[0].plot(freq1, amp1, linewidth=1.0, color="k") ax[0].set_xlabel(f"frequency ratio / {freq2.unit}") ax[0].grid(color="gray", linestyle="--", linewidth=0.5, alpha=0.5) ax[0].set_title("Static") ax[1].plot(freq2, amp2, linewidth=1.0, color="k") ax[1].set_xlabel(f"frequency ratio / {freq2.unit}")
# Simulate the spectrum # ''''''''''''''''''''' # # Create the spin systems from the above :math:`\zeta` and :math:`\eta` parameters. systems = single_site_system_generator(isotope="13C", shielding_symmetric={ "zeta": z_dist, "eta": e_dist }, abundance=amp) print(len(systems)) # %% method = BlochDecaySpectrum( channels=["13C"], rotor_frequency=0, # in Hz rotor_angle=0, # in rads ) # %% # Create a simulator object and add the above system. sim = Simulator(spin_systems=systems, methods=[method]) sim.run() # %% # The following is the static spectrum arising from a Czjzek distribution of the # second-rank traceless shielding tensors. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(sim.methods[0].simulation.real, color="black", linewidth=1) plt.tight_layout()
def test_simulator_1(): sim = Simulator() sim.spin_systems = [SpinSystem(sites=[Site(isotope="1H"), Site(isotope="23Na")])] sim.methods = [BlochDecaySpectrum(channels=["1H"])] sim.name = "test" sim.label = "test0" sim.description = "testing-testing 1.2.3" red_dict = sim.json(units=False) _ = [item.pop("description") for item in red_dict["methods"]] assert red_dict == { "name": "test", "label": "test0", "description": "testing-testing 1.2.3", "spin_systems": [ { "sites": [ {"isotope": "1H", "isotropic_chemical_shift": 0.0}, {"isotope": "23Na", "isotropic_chemical_shift": 0.0}, ], } ], "methods": [ { "channels": ["1H"], "name": "BlochDecaySpectrum", "magnetic_flux_density": 9.4, "rotor_angle": 0.9553166181245, "rotor_frequency": 0.0, "spectral_dimensions": [ { "count": 1024, "events": [{"transition_queries": [{"ch1": {"P": [-1]}}]}], "spectral_width": 25000.0, } ], } ], "config": { "decompose_spectrum": "none", "integration_density": 70, "integration_volume": "octant", "number_of_sidebands": 64, }, } # save sim.save("test_sim_save.temp") sim_load = Simulator.load("test_sim_save.temp") assert sim_load.spin_systems == sim.spin_systems assert sim_load.methods == sim.methods assert sim_load.name == sim.name assert sim_load.description == sim.description assert sim_load == sim # without units sim.save("test_sim_save_no_unit.temp", with_units=False) sim_load = Simulator.load("test_sim_save_no_unit.temp", parse_units=False) assert sim_load == sim os.remove("test_sim_save.temp") os.remove("test_sim_save_no_unit.temp")
shielding_symmetric=SymmetricTensor(zeta=-70, eta=0.8), ) spin_systems = [SpinSystem(sites=[site])] # %% # **Method** # # For the sideband-only cross-section, use the BlochDecaySpectrum method. # Get the dimension information from the experiment. spectral_dims = get_spectral_dimensions(pass_cross_section) PASS = BlochDecaySpectrum( channels=["13C"], magnetic_flux_density=9.395, # in T rotor_frequency=1500, # in Hz spectral_dimensions=spectral_dims, experiment=pass_cross_section, # also add the measurement to the method. ) # Optimize the script by pre-setting the transition pathways for each spin system from # the method. for sys in spin_systems: sys.transition_pathways = PASS.get_transition_pathways(sys) # %% # **Guess Spectrum** # Simulation # ---------- sim = Simulator(spin_systems=spin_systems, methods=[PASS])
spin_systems = [ SpinSystem(sites=[Si29_1]), SpinSystem(sites=[Si29_2]), SpinSystem(sites=[Si29_3]), ] # %% # Create a Bloch decay spectrum method. method = BlochDecaySpectrum( channels=["29Si"], magnetic_flux_density=14.1, # in T rotor_frequency=1500, # in Hz spectral_dimensions=[ SpectralDimension( count=2048, spectral_width=25000, # in Hz reference_offset=-10000, # in Hz label=r"$^{29}$Si resonances", ) ], ) # A graphical representation of the method object. plt.figure(figsize=(4, 2)) method.plot() plt.show() # %% # Create the Simulator object and add method and spin system objects, and run. sim = Simulator(spin_systems=spin_systems, methods=[method])
SpinSystem(sites=[C1], name="C1"), SpinSystem(sites=[C2], name="C2") ] # %% # **Method**: Create the three MAS method objects with respective MAS spinning speeds. # Get the spectral dimension parameters from the respective experiment and setup the # corresponding method. # Method for dataset 1 spectral_dims1 = get_spectral_dimensions(experiment1) MAS1 = BlochDecaySpectrum( channels=["13C"], magnetic_flux_density=7.05, # in T rotor_frequency=5000, # in Hz spectral_dimensions=spectral_dims1, experiment=experiment1, # add experimental dataset 1 ) # Method for dataset 2 spectral_dims2 = get_spectral_dimensions(experiment2) MAS2 = BlochDecaySpectrum( channels=["13C"], magnetic_flux_density=7.05, # in T rotor_frequency=1940, # in Hz spectral_dimensions=spectral_dims2, experiment=experiment2, # add experimental dataset 2 ) # Method for dataset 3
sim.load_spin_systems(host + filename) # load the spin systems. print(f"number of spin systems = {len(sim.spin_systems)}") # %% all_sites = sim.sites().to_pd() all_sites.head() # %% # Create a :math:`^{13}\text{C}` Bloch decay spectrum method. method_13C = BlochDecaySpectrum( channels=["13C"], magnetic_flux_density=11.74, # in T rotor_frequency=3000, # in Hz spectral_dimensions=[ SpectralDimension( count=8192, spectral_width=5e4, # in Hz reference_offset=2e4, # in Hz label=r"$^{13}$C resonances", ) ], ) # %% # Since the spin systems contain both :math:`^{13}\text{C}` and :math:`^{15}\text{N}` # sites, let's also create a :math:`^{15}\text{N}` Bloch decay spectrum method. method_15N = BlochDecaySpectrum( channels=["15N"], magnetic_flux_density=11.74, # in T rotor_frequency=3000, # in Hz spectral_dimensions=[
def test_BlochDecaySpectrum(): # test-1 m1 = BlochDecaySpectrum(channels=["1H"]) dimension_dictionary_ = { "count": 1024, "spectral_width": "25000.0 Hz", "events": [{ "transition_queries": [{ "ch1": { "P": [-1] } }] }], } should_be = { "name": "BlochDecaySpectrum", "channels": ["1H"], "magnetic_flux_density": "9.4 T", "rotor_frequency": "0.0 Hz", "rotor_angle": "0.9553166181245 rad", "spectral_dimensions": [dimension_dictionary_], } dict_ = m1.json() assert Method.parse_dict_with_units(dict_) == m1 dict_.pop("description") assert dict_ == should_be # test-2 m2_dict = { "channels": ["29Si"], "magnetic_flux_density": "11.7 T", "rotor_angle": "90 deg", "spectral_dimensions": [{}], } m2 = BlochDecaySpectrum.parse_dict_with_units(m2_dict) angle = 90 * np.pi / 180 dimension_dictionary_ = { "count": 1024, "spectral_width": "25000.0 Hz", "events": [{ "transition_queries": [{ "ch1": { "P": [-1] } }] }], } should_be = { "name": "BlochDecaySpectrum", "channels": ["29Si"], "magnetic_flux_density": "11.7 T", "rotor_frequency": "0.0 Hz", "rotor_angle": f"{angle} rad", "spectral_dimensions": [dimension_dictionary_], } dict_ = m2.json() assert Method.parse_dict_with_units(dict_) == m2 dict_.pop("description") assert dict_ == should_be