def export_spin_systems(self, filename: str): """ Export a list of spin systems to a 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. Args: str filename: A filename of the serialized file. Example ------- >>> sim.export_spin_systems(filename) # doctest:+SKIP """ spin_systems = [SpinSystem.json(obj) for obj in self.spin_systems] with open(filename, "w", encoding="utf8") as outfile: json.dump( spin_systems, outfile, ensure_ascii=False, sort_keys=False, allow_nan=False, )
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)
def test_direct_init_spin_system(): # test-1 the_spin_system = SpinSystem(sites=[], abundance=10) assert the_spin_system.sites == [] assert the_spin_system.abundance == 10.0 assert the_spin_system.json() == {"sites": [], "abundance": "10.0 %"} assert the_spin_system.reduced_dict() == { "sites": [], "abundance": 10.0, } # test-2 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.reduced_dict() == { "isotope": "29Si", "isotropic_chemical_shift": 10.0, } # test-3 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.reduced_dict() == { "sites": [{ "isotope": "29Si", "isotropic_chemical_shift": 10.0 }], "abundance": 10, } # test-4 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.reduced_dict() == { "sites": [ { "isotope": "29Si", "isotropic_chemical_shift": 10.0 }, { "isotope": "29Si", "isotropic_chemical_shift": 10.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 }, }, ], 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.abundance == 4.23 assert the_spin_system.json() == { "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 }, }, ], "abundance": "4.23 %", } assert the_spin_system.reduced_dict() == { "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 }, }, ], "abundance": 4.23, }