Skip to content

Commit

Permalink
Migrate formatting to use ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwthompson committed Sep 10, 2024
1 parent c84b321 commit 2398551
Show file tree
Hide file tree
Showing 45 changed files with 311 additions and 447 deletions.
31 changes: 3 additions & 28 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,10 @@ ci:
autoupdate_schedule: "quarterly"
files: ^openff|(^examples/((?!deprecated).)*$)|^docs|(^utilities/((?!deprecated).)*$)
repos:
- repo: https://github.com/psf/black
rev: 24.4.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.4
hooks:
- id: black
- id: black-jupyter
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/PyCQA/flake8
rev: 7.1.0
hooks:
- id: flake8
files: ^openff|(^utilities/((?!deprecated).)*$)
additional_dependencies: [
'flake8-absolute-import',
'flake8-no-pep420',
'flake8-bugbear',
]
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
hooks:
- id: nbqa-pyupgrade
args:
- --py39-plus
- id: nbqa-isort
- id: nbqa-flake8
args:
- '--select=F'
- id: ruff
- repo: https://github.com/adamchainz/blacken-docs
rev: 1.18.0
hooks:
Expand Down
14 changes: 7 additions & 7 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# openff-toolkit documentation build configuration file, created by
# sphinx-quickstart on Sun Dec 3 23:12:54 2017.
Expand All @@ -19,12 +18,12 @@
#
import os
import sys
from importlib.util import find_spec as find_import_spec

import openff.toolkit

sys.path.insert(0, os.path.abspath("."))

import sphinx

import openff.toolkit

# -- General configuration ------------------------------------------------

Expand Down Expand Up @@ -128,8 +127,6 @@
# sphinx-notfound-page
# https://github.com/readthedocs/sphinx-notfound-page
# Renders a 404 page with absolute links
from importlib.util import find_spec as find_import_spec

if find_import_spec("notfound"):
extensions.append("notfound.extension")

Expand Down Expand Up @@ -292,7 +289,10 @@
"OpenFF Toolkit Documentation",
author,
"openff-toolkit",
"A modern, extensible library for molecular mechanics force field science from the Open Force Field Consortium.",
(
"A modern, extensible library for molecular mechanics force field science from the Open Force "
"Field Consortium.",
),
"Miscellaneous",
),
]
48 changes: 16 additions & 32 deletions docs/users/molecule_cookbook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,24 @@
"source": [
"# Workaround for https://github.com/conda-forge/qcfractal-feedstock/issues/43\n",
"try:\n",
" import qcportal # noqa\n",
" import qcportal\n",
"except ImportError:\n",
" pass\n",
"\n",
"import warnings\n",
"import sys\n",
"import logging\n",
"\n",
"from openeye import oechem\n",
"from rdkit import Chem\n",
"import mdtraj\n",
"import openmm.app\n",
"from openff.toolkit import Molecule, Topology\n",
"from openff.toolkit.utils import get_data_file_path\n",
"from openff.toolkit.utils.exceptions import UndefinedStereochemistryError\n",
"\n",
"ipython = get_ipython() # noqa\n",
"\n",
"ipython = get_ipython()\n",
"\n",
"\n",
"def hide_traceback(\n",
Expand All @@ -72,16 +84,12 @@
"ipython.showtraceback = hide_traceback\n",
"\n",
"# Hide NumPy warnings\n",
"import warnings\n",
"\n",
"warnings.filterwarnings(\n",
" \"ignore\",\n",
" r\"The value of the smallest subnormal for \",\n",
")\n",
"\n",
"# Hide QCPortal version warnings\n",
"import logging\n",
"\n",
"qcportal_logger = logging.getLogger(\"PortalClientBase\")\n",
"\n",
"\n",
Expand All @@ -93,16 +101,6 @@
"qcportal_logger.addFilter(NoParsingFilter())"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "213f1264",
"metadata": {},
"outputs": [],
"source": [
"from openff.toolkit import Molecule, Topology"
]
},
{
"cell_type": "markdown",
"id": "37d692a1",
Expand Down Expand Up @@ -378,8 +376,6 @@
}
],
"source": [
"from openff.toolkit.utils.exceptions import UndefinedStereochemistryError\n",
"\n",
"try:\n",
" smiles_non_isomeric = Molecule.from_smiles(\"CC([NH3+])C(=O)[O-]\")\n",
"except UndefinedStereochemistryError as error:\n",
Expand Down Expand Up @@ -920,8 +916,6 @@
}
],
"source": [
"from openff.toolkit.utils import get_data_file_path\n",
"\n",
"path = get_data_file_path(\"proteins/T4-protein.pdb\")\n",
"topology = Topology.from_pdb(path)\n",
"protein = topology.molecule(0)\n",
Expand Down Expand Up @@ -1027,8 +1021,6 @@
}
],
"source": [
"from openff.toolkit.utils import get_data_file_path\n",
"\n",
"ligand_path = get_data_file_path(\"molecules/PT2385.sdf\")\n",
"ligand = Molecule.from_file(ligand_path)\n",
"\n",
Expand Down Expand Up @@ -1413,9 +1405,7 @@
}
],
"source": [
"from openmm.app.pdbfile import PDBFile\n",
"\n",
"openmm_topology = PDBFile(\"zw_l_alanine.pdb\").getTopology()\n",
"openmm_topology = openmm.app.PDBFile(\"zw_l_alanine.pdb\").getTopology()\n",
"openff_topology = Topology.from_openmm(openmm_topology, unique_molecules=[zw_l_alanine])\n",
"\n",
"from_openmm_topology = openff_topology.molecule(0)\n",
Expand Down Expand Up @@ -1492,9 +1482,7 @@
}
],
"source": [
"from mdtraj import load_pdb\n",
"\n",
"mdtraj_topology = load_pdb(\"zw_l_alanine.pdb\").topology\n",
"mdtraj_topology = mdtraj.load_pdb(\"zw_l_alanine.pdb\").topology\n",
"openff_topology = Topology.from_mdtraj(mdtraj_topology, unique_molecules=[zw_l_alanine])\n",
"\n",
"from_mdtraj_topology = openff_topology.molecule(0)\n",
Expand Down Expand Up @@ -1600,8 +1588,6 @@
}
],
"source": [
"from rdkit import Chem\n",
"\n",
"rdmol = Chem.MolFromSmiles(\"C[C@H]([NH3+])C([O-])=O\")\n",
"\n",
"print(\"rdmol is of type\", type(rdmol))\n",
Expand Down Expand Up @@ -1691,8 +1677,6 @@
}
],
"source": [
"from openeye import oechem\n",
"\n",
"oemol = oechem.OEGraphMol()\n",
"oechem.OESmilesToMol(oemol, \"C[C@H]([NH3+])C([O-])=O\")\n",
"\n",
Expand Down
5 changes: 2 additions & 3 deletions docs/users/pdb_cookbook/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@
"outputs": [],
"source": [
"import sys\n",
"import warnings\n",
"\n",
"from openff.toolkit import Molecule, Topology\n",
"\n",
"ipython = get_ipython() # noqa\n",
"ipython = get_ipython()\n",
"\n",
"\n",
"def hide_traceback(\n",
Expand All @@ -93,8 +94,6 @@
"ipython.showtraceback = hide_traceback\n",
"\n",
"# Hide NumPy warnings\n",
"import warnings\n",
"\n",
"warnings.filterwarnings(\n",
" \"ignore\",\n",
" r\"The value of the smallest subnormal for \",\n",
Expand Down
6 changes: 5 additions & 1 deletion examples/conformer_energies/conformer_energies.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ def compute_conformer_energies_from_file(filename):

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Perform energy minimization on a molecule, potentially with many conformers. For each conformer, this script will provide the initial energy, minimized energy, and RMSD between the initial and minimized structure (both as STDOUT and a csv file). The minimized conformers will be written out to SDF."
description=(
"Perform energy minimization on a molecule, potentially with many conformers. For each conformer, this "
"script will provide the initial energy, minimized energy, and RMSD between the initial and minimized "
"structure (both as STDOUT and a csv file). The minimized conformers will be written out to SDF."
),
)
parser.add_argument(
"-f", "--filename", help="Name of an input file containing conformers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@
}
],
"source": [
"# Create a parameterized OpenMM System from the PDB topology without bond constraints so we can convert to other packages\n",
"# Create a parameterized OpenMM System from the PDB topology without bond constraints\n",
"# so we can convert to other packages\n",
"system = forcefield.createSystem(\n",
" pdbfile.topology,\n",
" nonbondedMethod=app.PME,\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"# Retrieve protein and ligand files for BRD4 and a docked inhibitor from the benchmark systems GitHub repository\n",
"# https://github.com/MobleyLab/benchmarksets\n",
"import requests\n",
"import parmed\n",
"\n",
"from openmm import XmlSerializer, app, unit\n",
"from openmm.app import HBonds, NoCutoff, PDBFile\n",
"\n",
"repo_url = (\n",
" \"https://raw.githubusercontent.com/MobleyLab/benchmarksets/master/input_files/\"\n",
Expand Down Expand Up @@ -92,16 +96,6 @@
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from openmm import XmlSerializer, app, unit\n",
"from openmm.app import HBonds, NoCutoff, PDBFile"
]
},
{
"cell_type": "code",
"execution_count": 4,
Expand All @@ -120,8 +114,6 @@
"ligand_pdbfile = PDBFile(\"ligand.pdb\")\n",
"\n",
"# Convert OpenMM System object containing ligand parameters into a ParmEd Structure.\n",
"import parmed\n",
"\n",
"ligand_structure = parmed.openmm.load_topology(\n",
" ligand_pdbfile.topology, ligand_system, xyz=ligand_pdbfile.positions\n",
")"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1574,8 +1574,8 @@
}
],
"source": [
"fluorine = [atom for atom in ligand.atoms if atom.symbol == \"F\"][0]\n",
"carbon = [neighbor_atom for neighbor_atom in fluorine.bonded_atoms][0]\n",
"fluorine = next(atom for atom in ligand.atoms if atom.symbol == \"F\")\n",
"carbon = next(iter(fluorine.bonded_atoms))\n",
"fluorine_carbon_bond_indices = (\n",
" fluorine.molecule_atom_index,\n",
" carbon.molecule_atom_index,\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/virtual_sites/vsite_showcase.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@
"_energy_difference = from_openmm(energy_difference)\n",
"\n",
"print(\n",
" f\"Results for: OpenFF – OpenMM comparison (per molecule) \\n\"\n",
" f\"Results for: OpenFF – OpenMM comparison (per molecule) \\n\" # noqa: RUF001\n",
" f\"Energy difference ({_energy_difference.units}):\\n\\t\"\n",
" f\"{_energy_difference.m:0.3e}\\n\"\n",
" f\"Coordinates difference ({coordinate_difference.units}, norm):\\n\\t\"\n",
Expand Down
6 changes: 3 additions & 3 deletions examples/visualization/visualization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
},
"outputs": [],
"source": [
"from openff.toolkit import Molecule, Topology"
"from openff.toolkit import Molecule, Topology\n",
"from openff.toolkit.utils import get_data_file_path"
]
},
{
Expand Down Expand Up @@ -225,7 +226,7 @@
}
],
"source": [
"display(m) # noqa"
"display(m)"
]
},
{
Expand Down Expand Up @@ -733,7 +734,6 @@
},
"outputs": [],
"source": [
"from openff.toolkit.utils.utils import get_data_file_path\n",
"\n",
"topology = Topology.from_pdb(\n",
" get_data_file_path(\"systems/test_systems/T4_lysozyme_water_ions.pdb\")\n",
Expand Down
4 changes: 1 addition & 3 deletions openff/toolkit/_tests/_stale_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ def test_electrostatics_options(self):
# Change electrostatics method
forcefield.forces["Electrostatics"].method = method
f = partial(check_system_creation_from_molecule, forcefield, molecule)
f.description = "Testing {} parameter assignment using molecule {}".format(
offxml_file_path, molecule.name
)
f.description = f"Testing {offxml_file_path} parameter assignment using molecule {molecule.name}"
# yield f
# TODO: Implement a similar test, where we compare OpenMM energy evals from an
# AMBER-parameterized system to OFF-parameterized systems
Expand Down
4 changes: 2 additions & 2 deletions openff/toolkit/_tests/create_molecules.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ def propane_from_smiles_w_vsites():
def tip5_water():
# Make a TIP5 water
molecule = Molecule.from_smiles("[H][O][H]")
O1 = [atom for atom in molecule.atoms if atom.atomic_number == 8][0]
H1, H2 = [atom for atom in O1.bonded_atoms if atom.atomic_number == 1]
O1 = next(atom for atom in molecule.atoms if atom.atomic_number == 8)
H1, H2 = (atom for atom in O1.bonded_atoms if atom.atomic_number == 1)
molecule.add_divalent_lone_pair_virtual_site(
(H1, O1, H2),
0.7 * unit.angstrom,
Expand Down
2 changes: 1 addition & 1 deletion openff/toolkit/_tests/test_energies.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_reference(constrained, mol):
by version 0.8.0 of the toolkit"""
# TODO: Also test periodic vs. vacuum
with open(
get_data_file_path("reference_energies/reference_after_1007.json"), "r"
get_data_file_path("reference_energies/reference_after_1007.json")
) as fi:
reference = json.loads(fi.read())

Expand Down
2 changes: 1 addition & 1 deletion openff/toolkit/_tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def find_readme_examples() -> list[str]:
if readme_file_path is None:
return list()

with open(readme_file_path, "r") as f:
with open(readme_file_path) as f:
readme_content = f.read()

return re.findall("```python(.*?)```", readme_content, flags=re.DOTALL)
Expand Down
Loading

0 comments on commit 2398551

Please sign in to comment.