Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jthorton authored and SimonBoothroyd committed Jun 3, 2024
1 parent 4dcb967 commit 14d5e48
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion descent/targets/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,13 @@ def create_from_evaluator(dataset_file: pathlib.Path) -> datasets.Dataset:

properties: list[DataEntry] = []
property_data = json.load(dataset_file.open())

for phys_prop in property_data["properties"]:
try:
prop_type = _evaluator_to_prop[phys_prop["@type"]]
except KeyError:
raise KeyError(f"{phys_prop['@type']} not currently supported.") from None

smiles_and_role = [
(comp["smiles"], comp["smiles"] + "{" + comp["role"]["value"] + "}")
for comp in phys_prop["substance"]["components"]
Expand All @@ -217,7 +223,6 @@ def create_from_evaluator(dataset_file: pathlib.Path) -> datasets.Dataset:
std = phys_prop["uncertainty"]["value"] * getattr(
unit, phys_prop["uncertainty"]["unit"]
)
prop_type = _evaluator_to_prop[phys_prop["@type"]]
default_units = getattr(unit, _prop_units[prop_type])
prop = {
"type": prop_type,
Expand Down
1 change: 1 addition & 0 deletions descent/tests/data/evaluator_mock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"properties": [{"id": "1", "substance": {"components": [{"smiles": "CCO", "role": {"value": "solv", "@type": "openff.evaluator.substances.components.Component.Role"}, "@type": "openff.evaluator.substances.components.Component"}, {"smiles": "O", "role": {"value": "solv", "@type": "openff.evaluator.substances.components.Component.Role"}, "@type": "openff.evaluator.substances.components.Component"}], "amounts": {"CCO{solv}": [{"value": 0.48268, "@type": "openff.evaluator.substances.amounts.MoleFraction"}], "O{solv}": [{"value": 0.51732, "@type": "openff.evaluator.substances.amounts.MoleFraction"}]}, "@type": "openff.evaluator.substances.substances.Substance"}, "phase": 2, "thermodynamic_state": {"temperature": {"value": 298.15, "unit": "kelvin", "@type": "openff.evaluator.unit.Quantity"}, "pressure": {"value": 101.3, "unit": "kilopascal", "@type": "openff.evaluator.unit.Quantity"}, "@type": "openff.evaluator.thermodynamics.ThermodynamicState"}, "value": {"value": 0.99, "unit": "gram / milliliter", "@type": "openff.evaluator.unit.Quantity"}, "uncertainty": {"value": 0.000505, "unit": "gram / milliliter", "@type": "openff.evaluator.unit.Quantity"}, "source": {"doi": "mock", "reference": "", "@type": "openff.evaluator.datasets.provenance.MeasurementSource"}, "gradients": [], "@type": "openff.evaluator.properties.density.Density"}]}
1 change: 1 addition & 0 deletions descent/tests/data/missing_property_evaluator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"properties": [{"id": "1", "substance": {"components": [{"smiles": "CC(C)(C)O", "role": {"value": "solv", "@type": "openff.evaluator.substances.components.Component.Role"}, "@type": "openff.evaluator.substances.components.Component"}, {"smiles": "O", "role": {"value": "solv", "@type": "openff.evaluator.substances.components.Component.Role"}, "@type": "openff.evaluator.substances.components.Component"}], "amounts": {"CC(C)(C)O{solv}": [{"value": 0.48268, "@type": "openff.evaluator.substances.amounts.MoleFraction"}], "O{solv}": [{"value": 0.51732, "@type": "openff.evaluator.substances.amounts.MoleFraction"}]}, "@type": "openff.evaluator.substances.substances.Substance"}, "phase": 2, "thermodynamic_state": {"temperature": {"value": 298.15, "unit": "kelvin", "@type": "openff.evaluator.unit.Quantity"}, "pressure": {"value": 101.3, "unit": "kilopascal", "@type": "openff.evaluator.unit.Quantity"}, "@type": "openff.evaluator.thermodynamics.ThermodynamicState"}, "value": {"value": 0.99, "unit": "gram / milliliter", "@type": "openff.evaluator.unit.Quantity"}, "uncertainty": {"value": 0.000505, "unit": "gram / milliliter", "@type": "openff.evaluator.unit.Quantity"}, "source": {"doi": "mock", "reference": "", "@type": "openff.evaluator.datasets.provenance.MeasurementSource"}, "gradients": [], "@type": "openff.evaluator.properties.density.DielectricConstant"}]}
28 changes: 28 additions & 0 deletions descent/tests/targets/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
_predict,
_simulate,
create_dataset,
create_from_evaluator,
default_closure,
default_config,
extract_smiles,
Expand Down Expand Up @@ -583,3 +584,30 @@ def test_default_closure(tmp_cwd, mock_density_pure, mocker):
assert torch.isclose(loss, expected_loss)
assert grad.shape == mock_x.shape
assert hess.shape == (1, 1)


def test_create_from_evaluator(data_dir):
dataset = create_from_evaluator(dataset_file=data_dir / "evaluator_mock.json")

entries = list(descent.utils.dataset.iter_dataset(dataset))
expected = {
"smiles_a": "[C:1]([C:2]([O:3][H:9])([H:7])[H:8])([H:4])([H:5])[H:6]",
"x_a": 0.48268,
"smiles_b": "[O:1]([H:2])[H:3]",
"x_b": 0.51732,
"temperature": 298.15,
"pressure": 0.999753269183321,
"value": 0.99,
"std": 0.000505,
"units": "g/mL",
"source": "mock",
"type": "density",
}
assert entries[0] == expected


def test_unsupported_property(data_dir):
with pytest.raises(KeyError):
_ = create_from_evaluator(
dataset_file=data_dir / "missing_property_evaluator.json"
)

0 comments on commit 14d5e48

Please sign in to comment.