def load_spin_systems(self, filename: str): """Load a list of spin systems from the given JSON serialized file. See an `example <https://raw.githubusercontent.com/deepanshs/mrsimulator-examples/ master/spin_systems_v0.3.json>`_ of a JSON serialized file. For details, refer to the :ref:`load_spin_systems` section of this documentation. Args: str filename: A local or remote address to a JSON serialized file. Example ------- >>> sim.load_spin_systems(filename) # doctest:+SKIP """ contents = import_json(filename) self.spin_systems = [ SpinSystem.parse_dict_with_units(obj) for obj in contents ]
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, } valuedict_proc = { "SP_0_operation_1_Exponential_FWHM": 100, "SP_0_operation_3_Scale_factor": 10, } assert params.valuesdict() == { **valuesdict_system, **valuedict_proc, }, "Parameter creation failed" params = sf.make_LMFIT_params(sim) assert params.valuesdict( ) == valuesdict_system, "Parameter creation failed" # alias params = sf.make_LMFIT_parameters(sim) assert params.valuesdict( ) == valuesdict_system, "Parameter creation failed"
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)
def parse_dict_with_units(cls, py_dict: dict): """Parse the physical quantity from a dictionary representation of the Simulator object, where the physical quantity is expressed as a string with a number and a unit. Args: dict py_dict: A required python dict object. Returns: A :ref:`simulator_api` object. Example ------- >>> sim_py_dict = { ... 'config': { ... 'decompose_spectrum': 'none', ... 'integration_density': 70, ... 'integration_volume': 'octant', ... 'number_of_sidebands': 64 ... }, ... 'spin_systems': [ ... { ... 'abundance': '100 %', ... 'sites': [{ ... 'isotope': '13C', ... 'isotropic_chemical_shift': '20.0 ppm', ... 'shielding_symmetric': {'eta': 0.5, 'zeta': '10.0 ppm'} ... }] ... }, ... { ... 'abundance': '100 %', ... 'sites': [{ ... 'isotope': '1H', ... 'isotropic_chemical_shift': '-4.0 ppm', ... 'shielding_symmetric': {'eta': 0.1, 'zeta': '2.1 ppm'} ... }] ... }, ... { ... 'abundance': '100 %', ... 'sites': [{ ... 'isotope': '27Al', ... 'isotropic_chemical_shift': '120.0 ppm', ... 'shielding_symmetric': {'eta': 0.1, 'zeta': '2.1 ppm'} ... }] ... } ... ] ... } >>> sim = Simulator.parse_dict_with_units(sim_py_dict) >>> len(sim.spin_systems) 3 """ py_copy_dict = deepcopy(py_dict) if "spin_systems" in py_copy_dict: spin_sys = py_copy_dict["spin_systems"] spin_sys = [SpinSystem.parse_dict_with_units(obj) for obj in spin_sys] py_copy_dict["spin_systems"] = spin_sys if "methods" in py_copy_dict: methods = py_copy_dict["methods"] methods = [Method.parse_dict_with_units(obj) for obj in methods] py_copy_dict["methods"] = methods return Simulator(**py_copy_dict)
{"count": 2048, "spectral_width": "25 kHz", "reference_offset": "0 Hz",} ], } method2 = { "channels": ["1H"], "magnetic_flux_density": "9.4 T", "rotor_frequency": "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=(6, 3)) 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")
def test_direct_init_spin_system(): # test-1 # empty spin system the_spin_system = SpinSystem(sites=[], abundance=10, transition_pathways=None) assert the_spin_system.sites == [] assert the_spin_system.abundance == 10.0 assert the_spin_system.transition_pathways is None assert the_spin_system.json() == {"abundance": "10.0 %"} assert the_spin_system.json(units=False) == { "abundance": 10.0, } # test-2 # site test_site = Site(isotope="29Si", isotropic_chemical_shift=10) assert test_site.isotope.symbol == "29Si" assert test_site.isotropic_chemical_shift == 10.0 assert test_site.property_units["isotropic_chemical_shift"] == "ppm" assert test_site.json() == { "isotope": "29Si", "isotropic_chemical_shift": "10.0 ppm", } assert test_site.json(units=False) == { "isotope": "29Si", "isotropic_chemical_shift": 10.0, } # test-3 # one site spin system the_spin_system = SpinSystem(sites=[test_site], abundance=10) assert isinstance(the_spin_system.sites[0], Site) assert the_spin_system.abundance == 10.0 assert the_spin_system.json() == { "sites": [{ "isotope": "29Si", "isotropic_chemical_shift": "10.0 ppm" }], "abundance": "10.0 %", } assert the_spin_system.json(units=False) == { "sites": [{ "isotope": "29Si", "isotropic_chemical_shift": 10.0 }], "abundance": 10, } # test-4 # two sites spin system the_spin_system = SpinSystem(sites=[test_site, test_site], abundance=10) assert isinstance(the_spin_system.sites[0], Site) assert isinstance(the_spin_system.sites[1], Site) assert id(the_spin_system.sites[0]) != id(the_spin_system.sites[1]) assert the_spin_system.abundance == 10.0 assert the_spin_system.json() == { "sites": [ { "isotope": "29Si", "isotropic_chemical_shift": "10.0 ppm" }, { "isotope": "29Si", "isotropic_chemical_shift": "10.0 ppm" }, ], "abundance": "10.0 %", } assert the_spin_system.json(units=False) == { "sites": [ { "isotope": "29Si", "isotropic_chemical_shift": 10.0 }, { "isotope": "29Si", "isotropic_chemical_shift": 10.0 }, ], "abundance": 10, } # test-5 # coupling test_coupling = Coupling(site_index=[0, 1], isotropic_j=10, dipolar={"D": 100}) assert test_coupling.site_index == [0, 1] assert test_coupling.isotropic_j == 10.0 assert test_coupling.property_units["isotropic_j"] == "Hz" assert test_coupling.dipolar.D == 100.0 assert test_coupling.dipolar.property_units["D"] == "Hz" assert test_coupling.json() == { "site_index": [0, 1], "isotropic_j": "10.0 Hz", "dipolar": { "D": "100.0 Hz" }, } assert test_coupling.json(units=False) == { "site_index": [0, 1], "isotropic_j": 10.0, "dipolar": { "D": 100.0 }, } # test-6 # two sites and one coupling spin system the_spin_system = SpinSystem(sites=[test_site, test_site], couplings=[test_coupling], abundance=10) assert isinstance(the_spin_system.sites[0], Site) assert isinstance(the_spin_system.sites[1], Site) assert isinstance(the_spin_system.couplings[0], Coupling) assert id(the_spin_system.sites[0]) != id(the_spin_system.sites[1]) assert the_spin_system.abundance == 10.0 assert the_spin_system.json() == { "sites": [ { "isotope": "29Si", "isotropic_chemical_shift": "10.0 ppm" }, { "isotope": "29Si", "isotropic_chemical_shift": "10.0 ppm" }, ], "couplings": [{ "site_index": [0, 1], "isotropic_j": "10.0 Hz", "dipolar": { "D": "100.0 Hz" }, }], "abundance": "10.0 %", } assert the_spin_system.json(units=False) == { "sites": [ { "isotope": "29Si", "isotropic_chemical_shift": 10.0 }, { "isotope": "29Si", "isotropic_chemical_shift": 10.0 }, ], "couplings": [{ "site_index": [0, 1], "isotropic_j": 10.0, "dipolar": { "D": 100.0 } }], "abundance": 10, } # test-5 the_spin_system = SpinSystem( name="Just a test", description="The same", sites=[ { "isotope": "1H", "isotropic_chemical_shift": 0 }, { "isotope": "17O", "isotropic_chemical_shift": -10, "quadrupolar": { "Cq": 5.1e6, "eta": 0.5 }, }, ], couplings=[{ "site_index": [0, 1], "isotropic_j": 34 }], abundance=4.23, ) assert the_spin_system.name == "Just a test" assert the_spin_system.description == "The same" assert the_spin_system.sites[0].isotope.symbol == "1H" assert the_spin_system.sites[0].isotropic_chemical_shift == 0 assert the_spin_system.sites[1].isotope.symbol == "17O" assert the_spin_system.sites[1].isotropic_chemical_shift == -10 assert the_spin_system.sites[1].quadrupolar.Cq == 5.1e6 assert the_spin_system.sites[1].quadrupolar.eta == 0.5 assert the_spin_system.couplings[0].site_index == [0, 1] assert the_spin_system.couplings[0].isotropic_j == 34.0 assert the_spin_system.abundance == 4.23 serialize = the_spin_system.json() assert serialize == { "name": "Just a test", "description": "The same", "sites": [ { "isotope": "1H", "isotropic_chemical_shift": "0.0 ppm" }, { "isotope": "17O", "isotropic_chemical_shift": "-10.0 ppm", "quadrupolar": { "Cq": "5100000.0 Hz", "eta": 0.5 }, }, ], "couplings": [{ "site_index": [0, 1], "isotropic_j": "34.0 Hz" }], "abundance": "4.23 %", } assert the_spin_system == SpinSystem.parse_dict_with_units(serialize) json_no_unit = the_spin_system.json(units=False) assert json_no_unit == { "name": "Just a test", "description": "The same", "sites": [ { "isotope": "1H", "isotropic_chemical_shift": 0 }, { "isotope": "17O", "isotropic_chemical_shift": -10.0, "quadrupolar": { "Cq": 5100000, "eta": 0.5 }, }, ], "couplings": [{ "site_index": [0, 1], "isotropic_j": 34.0 }], "abundance": 4.23, } assert the_spin_system == SpinSystem(**json_no_unit)
import csdmpy as cp import numpy as np from mrsimulator import signal_processing as sp from mrsimulator import Simulator from mrsimulator import SpinSystem from mrsimulator.methods import BlochDecaySpectrum from .test_signal_processing import setup_read_write __author__ = "Maxwell C. Venetos" __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 }], ) ]