Skip to content

Commit 1004602

Browse files
Update canary script, automation (#114)
* Update canary script * Re-format with `ruff` * Also run CI on Python 3.12
1 parent ac307b1 commit 1004602

File tree

17 files changed

+57
-129
lines changed

17 files changed

+57
-129
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
python-version:
2525
- "3.10"
2626
- "3.11"
27+
- "3.12"
2728
os:
2829
- ubuntu-latest
2930
- macos-latest

.github/workflows/canary.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
python-version:
1919
- "3.10"
2020
- "3.11"
21+
- "3.12"
2122
cfg:
2223
- os: ubuntu-latest
2324
- os: macOS-latest

.pre-commit-config.yaml

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,8 @@ repos:
77
- id: check-yaml
88
- id: trailing-whitespace
99
- id: debug-statements
10-
- repo: https://github.com/psf/black
11-
rev: 24.4.2
10+
- repo: https://github.com/astral-sh/ruff-pre-commit
11+
rev: v0.6.4
1212
hooks:
13-
- id: black
14-
- repo: https://github.com/PyCQA/isort
15-
rev: 5.13.2
16-
hooks:
17-
- id: isort
18-
- repo: https://github.com/PyCQA/flake8
19-
rev: 7.1.0
20-
hooks:
21-
- id: flake8
22-
- repo: https://github.com/asottile/pyupgrade
23-
rev: v3.16.0
24-
hooks:
25-
- id: pyupgrade
26-
args: ["--py310-plus"]
13+
- id: ruff
14+
- id: ruff-format

canary/scripts/test_hmr.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import sys
33

44
import numpy as np
5+
import openmm.app
6+
import openmm.unit
57
from openff.toolkit.topology import Molecule
68
from openmmforcefields.generators import SystemGenerator
7-
from simtk import openmm, unit
8-
from simtk.openmm import app
99

1010
DATA_PATH = importlib.resources.files("canary") / "data"
1111
coverage_mols = DATA_PATH / "coverage.smi"
@@ -36,15 +36,13 @@ def hmr_driver(mol, ff_name):
3636
"""Given an OpenFF Molecule, run a short 4 fs HMR simulation. This function is adapted from
3737
https://github.com/openforcefield/openforcefields/issues/19#issuecomment-689816995
3838
"""
39-
print(
40-
f"Running HMR with force field {ff_name} and molecule with SMILES {mol.to_smiles()}"
41-
)
39+
print(f"Running HMR with force field {ff_name} and molecule with SMILES {mol.to_smiles()}")
4240

4341
forcefield_kwargs = {
44-
"constraints": app.HBonds,
42+
"constraints": openmm.app.HBonds,
4543
"rigidWater": True,
4644
"removeCMMotion": False,
47-
"hydrogenMass": 4 * unit.amu,
45+
"hydrogenMass": 4 * openmm.unit.amu,
4846
}
4947

5048
system_generator = SystemGenerator(
@@ -54,9 +52,9 @@ def hmr_driver(mol, ff_name):
5452
)
5553
system = system_generator.create_system(mol.to_topology().to_openmm())
5654

57-
temperature = 300 * unit.kelvin
58-
collision_rate = 1.0 / unit.picoseconds
59-
timestep = 4.0 * unit.femtoseconds
55+
temperature = 300 * openmm.unit.kelvin
56+
collision_rate = 1.0 / openmm.unit.picoseconds
57+
timestep = 4.0 * openmm.unit.femtoseconds
6058

6159
integrator = openmm.LangevinMiddleIntegrator(temperature, collision_rate, timestep)
6260
context = openmm.Context(system, integrator)

devtools/conda-envs/canary_env.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ dependencies:
55
- python
66
- pip
77
- pytest
8-
- openff-toolkit >=0.14.3
8+
- openff-toolkit >=0.16.4
99
- openmmforcefields

openforcefields/tests/compare.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ def _compare(
2020
The second system to compare.
2121
2222
"""
23-
assert (
24-
system1.getNumForces() == system2.getNumForces()
25-
), f"{system1.getForces()} != {system2.getForces()}"
23+
assert system1.getNumForces() == system2.getNumForces(), f"{system1.getForces()} != {system2.getForces()}"
2624

2725
for force in system1.getForces():
2826
assert type(force) in [openmm.NonbondedForce]
@@ -45,17 +43,11 @@ def _compare_nonbonded_forces(
4543
charge1, sigma1, epsilon1 = force1.getParticleParameters(particle_index)
4644
charge2, sigma2, epsilon2 = force2.getParticleParameters(particle_index)
4745

48-
assert (
49-
abs(charge1 - charge2) < tolerances["charge"]
50-
), f"{charge1} != {charge2}, {charge1 - charge2}"
46+
assert abs(charge1 - charge2) < tolerances["charge"], f"{charge1} != {charge2}, {charge1 - charge2}"
5147

5248
# Water models commonly have zero epsilon and meaningless/inconsistent values
5349
# of sigma. In this case, do not compare sigma values.
5450
if epsilon1._value * epsilon2._value != 0.0:
55-
assert (
56-
abs(sigma1 - sigma2) < tolerances["sigma"]
57-
), f"{sigma1} != {sigma2}, {sigma1 - sigma2}"
51+
assert abs(sigma1 - sigma2) < tolerances["sigma"], f"{sigma1} != {sigma2}, {sigma1 - sigma2}"
5852

59-
assert (
60-
abs(epsilon1 - epsilon2) < tolerances["epsilon"]
61-
), f"{epsilon1} != {epsilon2}, {epsilon1 - epsilon2}"
53+
assert abs(epsilon1 - epsilon2) < tolerances["epsilon"], f"{epsilon1} != {epsilon2}, {epsilon1 - epsilon2}"

openforcefields/tests/test_water_models.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,7 @@ def get_virtual_site_coordinates(
9090
system: openmm.System,
9191
conformer: Quantity,
9292
) -> Quantity:
93-
n_virtual_sites = len(
94-
[
95-
i
96-
for i in range(system.getNumParticles())
97-
if system.getParticleMass(i)._value == 0.0
98-
]
99-
)
93+
n_virtual_sites = len([i for i in range(system.getNumParticles()) if system.getParticleMass(i)._value == 0.0])
10094

10195
coordinates = openmm.unit.Quantity(
10296
numpy.vstack(
@@ -133,9 +127,7 @@ def get_oxygen_virtual_site_distance(
133127
) -> float:
134128
virtual_site_coordinates = get_virtual_site_coordinates(system, conformer)
135129

136-
return numpy.linalg.norm(
137-
(virtual_site_coordinates[index, :] - conformer[0, :]).m_as(unit.nanometer)
138-
)
130+
return numpy.linalg.norm((virtual_site_coordinates[index, :] - conformer[0, :]).m_as(unit.nanometer))
139131

140132

141133
def get_out_of_plane_angle(
@@ -145,9 +137,7 @@ def get_out_of_plane_angle(
145137
) -> float:
146138
parent, orientation1, orientation2 = conformer.m_as(unit.nanometer)
147139

148-
virtual_site_coordinates = get_virtual_site_coordinates(system, conformer)[
149-
index
150-
].m_as(unit.nanometer)
140+
virtual_site_coordinates = get_virtual_site_coordinates(system, conformer)[index].m_as(unit.nanometer)
151141

152142
normal = numpy.cross(orientation1 - parent, orientation2 - parent)
153143

@@ -461,9 +451,7 @@ def test_most_recent_version_match(water_model, pattern):
461451

462452
maximum_version = version.Version("0.0.0")
463453

464-
matched_files = list(
465-
filter(lambda x: re.match(pattern, x) is not None, get_available_force_fields())
466-
)
454+
matched_files = list(filter(lambda x: re.match(pattern, x) is not None, get_available_force_fields()))
467455

468456
assert len(matched_files) > 0, f"Failed to match any files for pattern {pattern}!"
469457

@@ -474,9 +462,7 @@ def test_most_recent_version_match(water_model, pattern):
474462
if found_verison > maximum_version:
475463
maximum_version = found_verison
476464

477-
assert maximum_version > version.Version(
478-
"0.0.0"
479-
), f"failed to update version of {water_model}"
465+
assert maximum_version > version.Version("0.0.0"), f"failed to update version of {water_model}"
480466

481467
maximum_version_file = f"{water_model}-{maximum_version}.offxml"
482468
shorthand_file = f"{water_model}.offxml"
@@ -514,9 +500,7 @@ def test_ion_parameter_assignment(water_molecule):
514500
)
515501
system = ff.create_openmm_system(off_top)
516502

517-
nbf = [
518-
force for force in system.getForces() if type(force) is openmm.NonbondedForce
519-
][0]
503+
nbf = next(force for force in system.getForces() if type(force) is openmm.NonbondedForce)
520504

521505
sigma_tol = 1e-10 * openmm.unit.nanometer
522506
eps_tol = 1e-10 * openmm.unit.kilojoule_per_mole
@@ -548,14 +532,10 @@ def test_ion_parameter_assignment(water_molecule):
548532

549533
# Ensure that this test covered all the ion parameters
550534
for key, parameter_was_used in ion_vdw_params_used.items():
551-
assert (
552-
parameter_was_used
553-
), f"The ion vdW parameter with smirks {key} was not assigned"
535+
assert parameter_was_used, f"The ion vdW parameter with smirks {key} was not assigned"
554536

555537
for key, parameter_was_used in ion_librarycharge_params_used.items():
556-
assert (
557-
parameter_was_used
558-
), f"The ion LibraryCharge parameter with smirks {key} was not assigned"
538+
assert parameter_was_used, f"The ion LibraryCharge parameter with smirks {key} was not assigned"
559539

560540

561541
@pytest.mark.parametrize(

pyproject.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[tool.ruff]
2+
line-length = 119
3+
exclude = ["versioneer.py", "openforcefields/_version.py"]
4+
5+
[tool.ruff.lint]
6+
select = ["F", "E", "W", "NPY", "UP", "RUF", "I"]
7+
8+
[tool.ruff.lint.per-file-ignores]
9+
"openforcefields/**/__init__.py" = ["F401"]
10+
"scripts/add_xe_vdw.py" = ["RUF003"]
11+
12+
[tool.ruff.lint.isort]
13+
known-third-party = ["openff.toolkit", "openff.utilities", "openff.units"]
14+

scripts/add_xe_vdw.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434

3535
# Make sure new FF loads with most recent toolkit version
3636
test_sage_211 = ForceField("../openforcefields/offxml/openff-2.1.1.offxml")
37-
test_sage_211_uc = ForceField(
38-
"../openforcefields/offxml/openff_unconstrained-2.1.1.offxml"
39-
)
37+
test_sage_211_uc = ForceField("../openforcefields/offxml/openff_unconstrained-2.1.1.offxml")
4038

4139

4240
topology = Topology.from_molecules(

scripts/get_ion_nb_params_from_ambertools_frcmod.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def main(amber_frcmod_path):
3333
if line.startswith("NONBON"):
3434
nonbonded = True
3535

36-
output_path = Path(
37-
"openforcefields", "data", f"{Path(amber_frcmod_path).suffix[1:]}.csv"
38-
)
36+
output_path = Path("openforcefields", "data", f"{Path(amber_frcmod_path).suffix[1:]}.csv")
3937
pandas.DataFrame(ion_nb_params).set_index("element").to_csv(output_path)
4038

4139

0 commit comments

Comments
 (0)