def test_convert_value_from_dg():
    eps = 0.001
    ##############################################
    # ORIGINAL = 'dg'
    ##############################################
    dg = utils.unit_registry.Quantity(1, utils.unit_registry("kJ / mole"))
    assert (pytest.approx(1.0, eps) == utils.convert_value(
        dg, "dg", "dg", out_unit=utils.unit_registry("kJ / mole")).magnitude)
    assert (pytest.approx(1.0, eps) == utils.convert_value(
        dg,
        "dg",
        "dg",
        temperature=273,
        out_unit=utils.unit_registry("kJ / mole")).to(
            utils.unit_registry("kJ / mole")).magnitude)
    assert (pytest.approx(0.239, eps) == utils.convert_value(
        dg,
        "dg",
        "dg",
        temperature=273,
        out_unit=utils.unit_registry("kJ / mole")).to(
            utils.unit_registry("kcal / mole")).magnitude)
    assert (pytest.approx(0.6697, eps) == utils.convert_value(
        dg, "dg", "ki").to(utils.unit_registry.molar).magnitude)
    assert (pytest.approx(0.6697, eps) == utils.convert_value(
        dg, "dg", "ic50").to(utils.unit_registry.molar).magnitude)
    assert (pytest.approx(0.1741,
                          eps) == utils.convert_value(dg, "dg",
                                                      "pic50").magnitude)
    with pytest.raises(NotImplementedError):
        assert "0.24 kcal/mol" == utils.convert_value(dg, "dg", "fakeObs")
def test_derive_observables():
    for target in targets.target_dict.keys():
        ligand_set = ligands.LigandSet(target)
        for name, lig in ligand_set.items():
            for i, t in enumerate(["dg", "ki", "ic50", "pic50"]):
                lig.derive_observables(derived_type=t,
                                       destination=f"DerivedMeasurement{i}")
                for original_type in lig._observables:
                    if ("measurement", original_type) in list(lig._data.index):
                        assert lig._data[(f"DerivedMeasurement{i}",
                                          t)] == utils.convert_value(
                                              lig._data[("measurement",
                                                         original_type)],
                                              original_type=original_type,
                                              final_type=t,
                                          )

            # Test expected exception when trying to convert to unknown observable
            with pytest.raises(
                    NotImplementedError,
                    match=f"Conversion to observable xxx not possible. "
                    f"Observable must be any of: dg, ki, ic50 or pic50.",
            ):
                lig.derive_observables(derived_type="xxx",
                                       destination=f"DerivedMeasurement")

            # Test expected exception when trying to convert from unknown observable
            for original_type in lig._observables:
                if ("measurement", original_type) in list(lig._data.index):
                    lig._data.rename({original_type: "xxx"},
                                     inplace=True,
                                     level=1)
                    with pytest.raises(
                            ValueError,
                            match=f"No known measured observable found. "
                            f"Measured observable should be any of: dg, ki, ic50 or pic50.",
                    ):
                        lig.derive_observables(
                            derived_type="pic50",
                            destination=f"DerivedMeasurement{i}")
def test_convert_value_from_ki():
    eps = 0.001
    ##############################################
    # ORIGINAL = 'ki'
    ##############################################
    ki = utils.unit_registry.Quantity(1, utils.unit_registry.molar)
    assert (pytest.approx(0.0, eps) == utils.convert_value(
        ki,
        "ki",
        "dg",
        temperature=300,
        out_unit=utils.unit_registry("kJ / mole")).to(
            utils.unit_registry("kJ / mole")).magnitude)
    assert (pytest.approx(0.0, eps) == utils.convert_value(
        ki,
        "ki",
        "dg",
        temperature=273,
        out_unit=utils.unit_registry("kJ / mole")).to(
            utils.unit_registry("kJ / mole")).magnitude)
    assert (pytest.approx(0.0, eps) == utils.convert_value(
        ki, "ki", "dg", out_unit=utils.unit_registry("kcal / mole")).to(
            utils.unit_registry("kJ / mole")).magnitude)
    assert (pytest.approx(1.0, eps) == utils.convert_value(
        ki, "ki", "ki", out_unit=utils.unit_registry.molar).to(
            utils.unit_registry.molar).magnitude)
    assert (pytest.approx(1.0, eps) == utils.convert_value(
        ki, "ki", "ic50").to(utils.unit_registry.molar).magnitude)

    assert pytest.approx(0.0, eps) == utils.convert_value(ki, "ki",
                                                          "pic50").magnitude

    with pytest.raises(NotImplementedError):
        assert "xxx" == utils.convert_value(ki, "ki", "fakeObs")

    ki = utils.unit_registry.Quantity(1, utils.unit_registry("nanomolar"))
    assert (pytest.approx(-51.69, eps) == utils.convert_value(
        ki,
        "ki",
        "dg",
        temperature=300,
        out_unit=utils.unit_registry("kJ / mole")).to(
            utils.unit_registry("kJ / mole")).magnitude)

    assert (pytest.approx(-12.35,
                          eps) == utils.convert_value(ki, "ki", "dg").to(
                              utils.unit_registry("kcal / mole")).magnitude)
    assert (pytest.approx(-12.35, eps) == utils.convert_value(
        ki, "ki", "dg",
        temperature=300).to(utils.unit_registry("kcal / mole")).magnitude)
    assert (pytest.approx(-12.35, eps) == utils.convert_value(
        ki,
        "ki",
        "dg",
        temperature=300,
        out_unit=utils.unit_registry("kcal / mole")).to(
            utils.unit_registry("kcal / mole")).magnitude)

    assert (pytest.approx(-47.04, eps) == utils.convert_value(
        ki,
        "ki",
        "dg",
        temperature=273,
        out_unit=utils.unit_registry("kJ / mole")).to(
            utils.unit_registry("kJ / mole")).magnitude)

    assert (pytest.approx(1.00, eps) == utils.convert_value(ki, "ki", "ki").to(
        utils.unit_registry("nanomolar")).magnitude)
    assert (pytest.approx(1.00, eps) == utils.convert_value(
        ki, "ki", "ki", out_unit=utils.unit_registry("nanomolar")).to(
            utils.unit_registry("nanomolar")).magnitude)

    assert (pytest.approx(1.0,
                          eps) == utils.convert_value(ki, "ki", "ic50").to(
                              utils.unit_registry("nanomolar")).magnitude)

    assert pytest.approx(9, eps) == utils.convert_value(ki, "ki",
                                                        "pic50").magnitude

    with pytest.raises(NotImplementedError):
        assert "xxx" == utils.convert_value(ki, "ki", "fakeObs")
def test_convert_value_from_ic50():
    eps = 0.001
    ##############################################
    # ORIGINAL = 'pic50'
    ##############################################
    pic50 = utils.unit_registry.Quantity(0, "")
    assert (pytest.approx(0.0, eps) == utils.convert_value(
        pic50,
        "pic50",
        "dg",
        temperature=300.0,
        out_unit=utils.unit_registry("kJ / mole"),
    ).to(utils.unit_registry("kJ / mole")).magnitude)
    assert (pytest.approx(0.0, eps) == utils.convert_value(
        pic50,
        "pic50",
        "dg",
        temperature=273.0,
        out_unit=utils.unit_registry("kJ / mole"),
    ).to(utils.unit_registry("kJ / mole")).magnitude)
    assert (pytest.approx(0.0, eps) == utils.convert_value(
        pic50, "pic50", "dg", out_unit=utils.unit_registry("kcal / mole")).to(
            utils.unit_registry("kJ / mole")).magnitude)

    assert (pytest.approx(1.0, eps) == utils.convert_value(
        pic50, "pic50", "ki", out_unit=utils.unit_registry.molar).to(
            utils.unit_registry.molar).magnitude)

    assert (pytest.approx(1.0, eps) == utils.convert_value(
        pic50, "pic50", "ic50", out_unit=utils.unit_registry.molar).to(
            utils.unit_registry.molar).magnitude)

    assert (pytest.approx(0.0,
                          eps) == utils.convert_value(pic50, "pic50",
                                                      "pic50").magnitude)

    with pytest.raises(NotImplementedError):
        utils.convert_value(pic50, "pic50", "fakeObs")

    pic50 = utils.unit_registry.Quantity(9, "")
    assert (pytest.approx(-51.69, eps) == utils.convert_value(
        pic50,
        "pic50",
        "dg",
        temperature=300,
        out_unit=utils.unit_registry("kJ / mole"),
    ).to(utils.unit_registry("kJ / mole")).magnitude)

    assert (pytest.approx(-12.35, eps) == utils.convert_value(
        pic50, "pic50", "dg").to(utils.unit_registry("kcal / mole")).magnitude)
    assert (pytest.approx(-12.35, eps) == utils.convert_value(
        pic50, "pic50", "dg",
        temperature=300).to(utils.unit_registry("kcal / mole")).magnitude)
    assert (pytest.approx(-12.35, eps) == utils.convert_value(
        pic50,
        "pic50",
        "dg",
        temperature=300,
        out_unit=utils.unit_registry("kcal / mole"),
    ).to(utils.unit_registry("kcal / mole")).magnitude)

    assert (pytest.approx(-47.04, eps) == utils.convert_value(
        pic50,
        "pic50",
        "dg",
        temperature=273,
        out_unit=utils.unit_registry("kJ / mole"),
    ).to(utils.unit_registry("kJ / mole")).magnitude)

    assert (pytest.approx(1.00, eps) == utils.convert_value(
        pic50, "pic50", "ki").to(utils.unit_registry("nanomolar")).magnitude)

    assert (pytest.approx(1.00, eps) == utils.convert_value(
        pic50, "pic50", "ic50").to(utils.unit_registry("nanomolar")).magnitude)
    assert (pytest.approx(1.00, eps) == utils.convert_value(
        pic50, "pic50", "ic50", out_unit=utils.unit_registry("nanomolar")).to(
            utils.unit_registry("nanomolar")).magnitude)

    assert (pytest.approx(9,
                          eps) == utils.convert_value(pic50, "pic50",
                                                      "pic50").magnitude)
    with pytest.raises(NotImplementedError):
        utils.convert_value(pic50, "pic50", "fakeObs")