Skip to content

Commit

Permalink
Merge pull request #175 from lukasturcani/lukas/optimizer
Browse files Browse the repository at this point in the history
Make ruff stricter and clean up doc strings throughout src and examples directories.

mypy fails due to issue with rdkit version: rdkit/rdkit#7401

We will wait till that is solved to fix.
  • Loading branch information
andrewtarzia authored May 15, 2024
2 parents 3cb09ca + d4d9216 commit 8e71823
Show file tree
Hide file tree
Showing 156 changed files with 3,210 additions and 5,042 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
python-version: "3.11"
cache: "pip"
- run: "pip install -e '.[dev]'"
- run: mypy src
black:
- run: mypy src examples
ruff-format:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
Expand All @@ -35,7 +35,7 @@ jobs:
python-version: "3.11"
cache: "pip"
- run: "pip install -e '.[dev]'"
- run: black --check .
- run: ruff format --check .
pytest:
runs-on: ubuntu-22.04
steps:
Expand All @@ -45,4 +45,4 @@ jobs:
python-version: "3.11"
cache: "pip"
- run: "pip install -e '.[dev]'"
- run: pytest
- run: pytest
28 changes: 17 additions & 11 deletions docs/source/_templates/class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@
:undoc-members:
:show-inheritance:

{% block methods %}
{% if methods %}
.. rubric:: {{ _('Methods') }}
{% block attributes %}
{% if attributes %}
.. rubric:: {{ _('Attributes') }}

.. autosummary::
:nosignatures:
{% for item in methods %}
{%- if not item.startswith('_') %}
{% for item in attributes %}
~{{ name }}.{{ item }}
{%- endif -%}
{%- endfor %}
{% endif %}
{% endblock %}

{% block attributes %}
{% if attributes %}
.. rubric:: {{ _('Attributes') }}
{% block methods %}

{% set public_methods = [] %}
{% for item in methods %}
{% if not item.startswith('_') %}
{% set public_methods = public_methods.append(item) %}
{% endif %}
{% endfor %}

{% if public_methods %}
.. rubric:: {{ _('Methods') }}

.. autosummary::
{% for item in attributes %}
:nosignatures:
{% for item in public_methods %}
~{{ name }}.{{ item }}
{%- endfor %}
{% endif %}
Expand Down
1 change: 0 additions & 1 deletion docs/source/cage_analysis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ like all other methods in :mod:`stko`.
DitopicThreeSiteAnalyser <_autosummary/stko.molecule_analysis.DitopicThreeSiteAnalyser>
ConstructedAnalyser <_autosummary/stko.molecule_analysis.ConstructedAnalyser>
GeometryAnalyser <_autosummary/stko.molecule_analysis.GeometryAnalyser>
PoreAnalyser <_autosummary/stko.molecule_analysis.PoreAnalyser>


There is an example script with some of the analyses
Expand Down
1 change: 0 additions & 1 deletion docs/source/calculators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ Calculators
MatchedTorsionCalculator <_autosummary/stko.MatchedTorsionCalculator>
PlanarityCalculator <_autosummary/stko.PlanarityCalculator>
GeometryAnalyser <_autosummary/stko.molecule_analysis.GeometryAnalyser>
PoreAnalyser <_autosummary/stko.molecule_analysis.PoreAnalyser>
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# -- Project information ----------------------------------------------

project = "stko"
copyright = "2023, Steven Bennett, Andrew Tarzia, Lukas Turcani"
project_copyright = "2023, Steven Bennett, Andrew Tarzia, Lukas Turcani"
author = "Steven Bennett, Andrew Tarzia, Lukas Turcani"

# -- General configuration ---------------------------------------------------
Expand All @@ -29,7 +29,7 @@

autodoc_typehints = "description"
autodoc_member_order = "groupwise"
autoclass_content = "both"
autoclass_content = "class"

intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
Expand Down
8 changes: 8 additions & 0 deletions docs/source/helpers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Helpers
=======

.. toctree::
:maxdepth: 1

MAEExtractor <_autosummary/stko.MAEExtractor>
XTBExtractor <_autosummary/stko.XTBExtractor>
3 changes: 2 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Calculators <calculators>
Optimizers <optimizers>
Cage analysis <cage_analysis>
Helpers <helpers>


.. toctree::
Expand Down Expand Up @@ -80,4 +81,4 @@ Indices and Tables

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
* :ref:`search`
3 changes: 1 addition & 2 deletions docs/source/molecular.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ Molecular
DecomposeMOC <_autosummary/stko.molecule_analysis.DecomposeMOC>
DitopicThreeSiteAnalyser <_autosummary/stko.molecule_analysis.DitopicThreeSiteAnalyser>
ConstructedAnalyser <_autosummary/stko.molecule_analysis.ConstructedAnalyser>
SubgroupAnalyser <_autosummary/stko.molecule_analysis.SubgroupAnalyser>
AlkyneAngle <_autosummary/stko.molecule_analysis.AlkyneAngle>
X5Planarity <_autosummary/stko.molecule_analysis.X5Planarity>
C5N1Planarity <_autosummary/stko.molecule_analysis.C5N1Planarity>
C6Planarity <_autosummary/stko.molecule_analysis.C6Planarity>
Subgroup <_autosummary/stko.molecule_analysis.Subgroup>
SubgroupAnalyser <_autosummary/stko.molecule_analysis.SubgroupAnalyser>
SubgroupAnalyser <_autosummary/stko.molecule_analysis.SubgroupAnalyser>
2 changes: 0 additions & 2 deletions docs/source/optimizers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ Optimizers
XTBCREST <_autosummary/stko.XTBCREST>
XTBFFCREST <_autosummary/stko.XTBFFCREST>


GulpUFFMDOptimizer <_autosummary/stko.GulpUFFMDOptimizer>
MacroModelMD <_autosummary/stko.MacroModelMD>
MAEExtractor <_autosummary/stko.MAEExtractor>
23 changes: 12 additions & 11 deletions examples/aligner_example.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import os
from pathlib import Path

import numpy as np
import stk
import stko


def main():
examples_output = "aligner_directory"
if not os.path.exists(examples_output):
os.mkdir(examples_output)
def main() -> None:
"""Run the example."""
examples_output = Path("aligner_directory")
if not examples_output.exists():
examples_output.mkdir()

bb1 = stk.BuildingBlock("NCCN", [stk.PrimaryAminoFactory()])
bb2 = stk.BuildingBlock(
Expand Down Expand Up @@ -45,20 +46,20 @@ def main():
np.array((0, 0, 1)),
np.array((0, 0, 0)),
)
mol.write(os.path.join(examples_output, f"unaligned_{i}.mol"))
initial.write(os.path.join(examples_output, f"init_{i}.mol"))
mol.write(examples_output / f"unaligned_{i}.mol")
initial.write(examples_output / f"init_{i}.mol")
aligner = stko.Aligner(
initial_molecule=initial,
matching_pairs=pairs,
)
if uff_opt:
mol = aligner.optimize(_opt.optimize(mol))
aligned_mol = aligner.optimize(_opt.optimize(mol))
else:
mol = aligner.optimize(mol)
mol.write(os.path.join(examples_output, f"aligned_{i}.mol"))
aligned_mol = aligner.optimize(mol)
aligned_mol.write(examples_output / f"aligned_{i}.mol")
rmsd_calculator = stko.RmsdMappedCalculator(initial)
rmsd = rmsd_calculator.get_results(mol).get_rmsd()
print(f"molecule {i} RMSD: {rmsd}")
print(f"molecule {i} RMSD: {rmsd}") # noqa: T201


if __name__ == "__main__":
Expand Down
17 changes: 9 additions & 8 deletions examples/basic_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
from pathlib import Path

import stk
import stko


def main():
def main() -> None:
"""Run the example."""
bb1 = stk.BuildingBlock("NCCNCCN", [stk.PrimaryAminoFactory()])
bb2 = stk.BuildingBlock("O=CCCC=O", [stk.AldehydeFactory()])
polymer = stk.ConstructedMolecule(
Expand All @@ -16,20 +17,20 @@ def main():
)
)

examples_output = "output_directory"
if not os.path.exists(examples_output):
os.mkdir(examples_output)
examples_output = Path("output_directory")
if not examples_output.exists():
examples_output.mkdir()

# Run optimisations.
uff = stko.UFF()
polymer = uff.optimize(polymer)
polymer.write(os.path.join(examples_output, "poly_uff.mol"))
polymer.write(examples_output / "poly_uff.mol")
mmff = stko.MMFF()
polymer = mmff.optimize(polymer)
polymer.write(os.path.join(examples_output, "poly_mmff.mol"))
polymer.write(examples_output / "poly_mmff.mol")
etkdg = stko.ETKDG()
polymer = etkdg.optimize(polymer)
polymer.write(os.path.join(examples_output, "poly_etkdg.mol"))
polymer.write(examples_output / "poly_etkdg.mol")


if __name__ == "__main__":
Expand Down
12 changes: 7 additions & 5 deletions examples/cage_analysis_example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: T201
import logging
from collections import defaultdict

Expand All @@ -6,7 +7,8 @@
import stko


def main():
def main() -> None:
"""Run the example."""
logging.warning(
"This code is only present in the latest versions of stko that "
"require Python 3.11!"
Expand All @@ -15,9 +17,9 @@ def main():
pd = stk.BuildingBlock(
smiles="[Pd+2]",
functional_groups=(
stk.SingleAtom(stk.Pd(0, charge=2)) for i in range(4)
stk.SingleAtom(stk.Pd(0, charge=2)) for _ in range(4)
),
position_matrix=[[0.0, 0.0, 0.0]],
position_matrix=np.array([[0.0, 0.0, 0.0]]),
)
ditopic_bb = stk.BuildingBlock(
smiles="C1=NC=CC(C2=CC=CC(C3=CC=NC=C3)=C2)=C1",
Expand Down Expand Up @@ -99,8 +101,8 @@ def main():
# on the constructed molecule or maintain building block chemistry.
# It simply extracts the parts of building blocks still present in
# the molecule.
analyser = stko.molecule_analysis.ConstructedAnalyser()
centroids = analyser.get_building_block_centroids(apdcage)
constructed_analyser = stko.molecule_analysis.ConstructedAnalyser()
centroids = constructed_analyser.get_building_block_centroids(apdcage)
print("building block centroids:")
print(centroids)
print()
Expand Down
33 changes: 21 additions & 12 deletions examples/calculators_example.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import os
import sys
# ruff: noqa: T201
import argparse
from pathlib import Path

import stk
import stko


def main():
if len(sys.argv) > 1:
xtb_path = sys.argv[1]
else:
xtb_path = None

def main() -> None:
"""Run the example."""
args = _parse_args()
bb1 = stk.BuildingBlock("NCCNCCN", [stk.PrimaryAminoFactory()])

# Run calculations.
Expand Down Expand Up @@ -39,11 +37,11 @@ def main():
mmff.get_energy(bb1),
)

if xtb_path is not None:
if args.xtb_path is not None:
print("doing XTB calculation.")
xtb = stko.XTBEnergy(
xtb_path=xtb_path,
output_dir=os.path.join("output_directory", "example_xtb_out"),
xtb_path=args.xtb_path,
output_dir=Path("output_directory") / "example_xtb_out",
unlimited_memory=True,
calculate_ip_and_ea=True,
)
Expand Down Expand Up @@ -76,7 +74,7 @@ def main():
print("Expected fail")

# Try yielded option.
calculations.append(xtb.calculate((bb1)))
calculations.append(xtb.calculate(bb1))

# Run through yield statements using the `.calculate` method
# all in once.
Expand All @@ -98,5 +96,16 @@ def main():
)


def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"--xtb_path",
type=Path,
help="Path to xtb binary.",
default=None,
)
return parser.parse_args()


if __name__ == "__main__":
main()
Loading

0 comments on commit 8e71823

Please sign in to comment.