Exemple #1
0
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_site_object_methods():
    good_json_2 = {"site_index": [0, 1], "isotropic_j": "-10 Hz"}
    the_coupling = Coupling.parse_dict_with_units(good_json_2)

    # testing method dict()
    result = {
        "site_index": [0, 1],
        "isotropic_j": -10.0,
        "property_units": {
            "isotropic_j": "Hz"
        },
        "name": None,
        "label": None,
        "description": None,
        "dipolar": None,
        "j_symmetric": None,
        "j_antisymmetric": None,
    }
    assert the_coupling.dict() == result, "Failed Coupling.dict()"

    # testing method json()
    result = {"site_index": [0, 1], "isotropic_j": "-10.0 Hz"}
    assert the_coupling.json() == result, "Failed Coupling.json()"

    result = {
        "site_index": [1, 2],
        "isotropic_j": "10.0 Hz",
        "j_symmetric": {
            "zeta": "12.1 Hz",
            "eta": 0.1,
            "alpha": "2.1 rad"
        },
        "j_antisymmetric": {
            "zeta": "-1.1 Hz",
            "alpha": "0.1 rad",
            "beta": "2.5 rad",
        },
        "dipolar": {
            "D": "10000.0 Hz",
            "eta": 0.6
        },
    }
    the_coupling = Coupling(
        site_index=[1, 2],
        isotropic_j=10,
        j_symmetric={
            "zeta": 12.1,
            "eta": 0.1,
            "alpha": 2.1
        },
        j_antisymmetric={
            "zeta": -1.1,
            "alpha": 0.1,
            "beta": 2.5
        },
        dipolar={
            "D": 10e3,
            "eta": 0.6
        },
    )
    assert the_coupling.json() == result, "Failed Coupling.json()"