Skip to content

Commit

Permalink
Merge pull request #27 from mattwthompson/cleanup
Browse files Browse the repository at this point in the history
Remove some unused functions, attributes, logic
  • Loading branch information
mattwthompson authored Mar 12, 2024
2 parents 1584974 + 7ec8f99 commit 8fd7168
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Run tests
run: |
pytest -v -n logical --durations=10 \
--cov=ibstore/ --cov-report=xml \
--cov=ibstore/ --cov-report=xml --cov-config=setup.cfg \
ibstore/
- name: CodeCov
Expand Down
27 changes: 0 additions & 27 deletions ibstore/_forcefields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,6 @@
from openff.toolkit import Molecule


def _get_force_field_type(
force_field_path: str,
) -> str:
if force_field_path.endswith(".offxml"):
return "SMIRNOFF"
elif force_field_path.endswith(".xml"):
return "OPENMM"
else:
raise NotImplementedError(f"Force field type {force_field_path} not supported.")


def _get_openmm_system(
molecule: Molecule,
force_field_path: str,
) -> openmm.System:
force_field_type = _get_force_field_type(force_field_path)

if force_field_type == "SMIRNOFF":
return _smirnoff(molecule, force_field_path)

elif force_field_type == "OPENMM":
return _gaff(molecule, force_field_path)

else:
raise NotImplementedError(f"force field type {force_field_type} not supported.")


def _smirnoff(molecule: Molecule, force_field_path: str) -> openmm.System:
from openff.toolkit import ForceField

Expand Down
40 changes: 10 additions & 30 deletions ibstore/_minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,21 @@ def _lazy_load_force_field(force_field_name: str) -> ForceField:
def _minimize_blob(
input: dict[str, dict[str, Union[str, numpy.ndarray]]],
force_field: str,
prune_isomorphs: bool,
n_processes: int = 2,
chunksize=32,
) -> dict[str, list["MinimizationResult"]]:
inputs = list()

for inchi_key in input:
for row in input[inchi_key]:
if prune_isomorphs:
# This behavior is always useless and probably bad as there is
# no reason to use InCHI when mapped SMILES is known. See #7
are_isomorphic, _ = Molecule.are_isomorphic(
Molecule.from_inchi(inchi_key, allow_undefined_stereo=True),
Molecule.from_mapped_smiles(
row["mapped_smiles"],
allow_undefined_stereo=True,
),
)

if not are_isomorphic:
continue

inputs.append(
MinimizationInput(
inchi_key=inchi_key,
qcarchive_id=row["qcarchive_id"],
force_field=force_field,
mapped_smiles=row["mapped_smiles"],
coordinates=row["coordinates"],
),
)
inputs = [
MinimizationInput(
qcarchive_id=row["qcarchive_id"],
force_field=force_field,
mapped_smiles=row["mapped_smiles"],
coordinates=row["coordinates"],
)
for inchi_key in input
for row in input[inchi_key]
]

with Pool(processes=n_processes) as pool:
yield from tqdm(
Expand All @@ -113,7 +97,6 @@ def _minimize_blob(


class MinimizationInput(ImmutableModel):
inchi_key: str = Field(..., description="The InChI key of the molecule")
qcarchive_id: str = Field(
...,
description="The ID of the molecule in the QCArchive",
Expand All @@ -133,7 +116,6 @@ class MinimizationInput(ImmutableModel):


class MinimizationResult(ImmutableModel):
inchi_key: str = Field(..., description="The InChI key of the molecule")
qcarchive_id: str
force_field: str
mapped_smiles: str
Expand All @@ -144,7 +126,6 @@ class MinimizationResult(ImmutableModel):
def _run_openmm(
input: MinimizationInput,
) -> MinimizationResult:
inchi_key: str = input.inchi_key
qcarchive_id: str = input.qcarchive_id
positions: numpy.ndarray = input.coordinates

Expand Down Expand Up @@ -204,7 +185,6 @@ def _run_openmm(
openmm.LocalEnergyMinimizer.minimize(context, 5.0e-9, 1500)

return MinimizationResult(
inchi_key=inchi_key,
qcarchive_id=qcarchive_id,
force_field=input.force_field,
mapped_smiles=input.mapped_smiles,
Expand Down
11 changes: 4 additions & 7 deletions ibstore/_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,15 +477,14 @@ def from_cached_result_collection(
def optimize_mm(
self,
force_field: str,
prune_isomorphs: bool = False,
n_processes: int = 2,
chunksize=32,
):
from ibstore._minimize import _minimize_blob

inchi_keys = self.get_inchi_keys()

_data = defaultdict(list)
inchi_key_qm_conformer_mapping = defaultdict(list)

for inchi_key in inchi_keys:
molecule_id = self.get_molecule_id_by_inchi_key(inchi_key)
Expand All @@ -509,17 +508,16 @@ def optimize_mm(
qcarchive_id=qm_conformer["qcarchive_id"],
force_field=force_field,
):
_data[inchi_key].append(qm_conformer)
inchi_key_qm_conformer_mapping[inchi_key].append(qm_conformer)
else:
pass

if len(_data) == 0:
if len(inchi_key_qm_conformer_mapping) == 0:
return

_minimized_blob = _minimize_blob(
input=_data,
input=inchi_key_qm_conformer_mapping,
force_field=force_field,
prune_isomorphs=prune_isomorphs,
n_processes=n_processes,
chunksize=chunksize,
)
Expand All @@ -546,7 +544,6 @@ def optimize_mm(
for result in _minimized_blob:
if result.qcarchive_id in seen:
continue
inchi_key = result.inchi_key
molecule_id = inchi_to_id[inchi_key]
record = MMConformerRecord(
molecule_id=molecule_id,
Expand Down
6 changes: 0 additions & 6 deletions ibstore/_tests/unit_tests/test_minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def basic_input(force_field="openff-1.0.0") -> MinimizationInput:
ethane.generate_conformers(n_conformers=1)

return MinimizationInput(
inchi_key=ethane.to_inchikey(),
qcarchive_id="test",
force_field=force_field,
mapped_smiles=ethane.to_smiles(mapped=True),
Expand All @@ -41,7 +40,6 @@ def basic_input(force_field="openff-1.0.0") -> MinimizationInput:
@pytest.fixture()
def perturbed_input(perturbed_ethane) -> MinimizationInput:
return MinimizationInput(
inchi_key=perturbed_ethane.to_inchikey(),
qcarchive_id="test",
force_field="openff-1.0.0",
mapped_smiles=perturbed_ethane.to_smiles(mapped=True),
Expand All @@ -57,7 +55,6 @@ def test_minimization_basic(perturbed_input):
result = _run_openmm(perturbed_input)

for attr in (
"inchi_key",
"qcarchive_id",
"mapped_smiles",
):
Expand Down Expand Up @@ -85,7 +82,6 @@ def test_different_force_fields_different_results():
def test_plugin_loadable(ethane):
_run_openmm(
MinimizationInput(
inchi_key=ethane.to_inchikey(),
qcarchive_id="test",
force_field="de-force-1.0.1",
mapped_smiles=ethane.to_smiles(mapped=True),
Expand All @@ -108,7 +104,6 @@ def test_finds_local_force_field(ethane, tmp_path):

_run_openmm(
MinimizationInput(
inchi_key=ethane.to_inchikey(),
qcarchive_id="test",
force_field=(tmp_path / "fOOOO.offxml").as_posix(),
mapped_smiles=ethane.to_smiles(mapped=True),
Expand All @@ -131,7 +126,6 @@ def return_no_paths():

_run_openmm(
MinimizationInput(
inchi_key=ethane.to_inchikey(),
qcarchive_id="test",
force_field="openff-1.0.0",
mapped_smiles=ethane.to_smiles(mapped=True),
Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ exclude =

[coverage:run]
omit =
*/_version.py
*/_tests/*
ibstore/_tests/*
versioneer.py
setup.py
ibstore/_version.py

[coverage:report]
Expand Down

0 comments on commit 8fd7168

Please sign in to comment.