Skip to content

Commit

Permalink
Add Ruff to pre-commit hooks (#1207)
Browse files Browse the repository at this point in the history
* update CI and precommit files

* add ruff changes

* remove gmso lines

* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci

* change error type in test

* raise the error that is created

* remove duplicate windows 3.12 test

* fix precommit errors

* fix import error

* fix CI error

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and CalCraven committed Nov 22, 2024
1 parent 62f7af2 commit 77eb88e
Show file tree
Hide file tree
Showing 67 changed files with 448 additions and 1,031 deletions.
48 changes: 42 additions & 6 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ on:
jobs:
test:
if: github.event.pull_request.draft == false
name: mBuild Tests
name: mBuild Tests (python)
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macOS-latest, macOS-13, ubuntu-latest]
os: [ubuntu-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
include:
- os: windows-latest
python-version: "3.12"

defaults:
run:
Expand Down Expand Up @@ -60,6 +57,45 @@ jobs:
name: mBuild-Coverage
verbose: true
files: ./coverage.xml
arch-test:
if: github.event.pull_request.draft == false
name: mBuild Tests (arch)
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macOS-latest, macOS-13, ubuntu-latest, windows-latest]
python-version: ["3.12"]

defaults:
run:
shell: bash -l {0}

steps:
- uses: actions/checkout@v4
name: Checkout Branch / Pull Request

- name: Install Mamba (Linux)
uses: mamba-org/setup-micromamba@v1
with:
environment-file: environment-dev.yml
create-args: >-
python=${{ matrix.python-version }}
if: runner.os != 'Windows'

- name: Install Mamba (Windows)
uses: mamba-org/setup-micromamba@v1
with:
environment-file: environment-dev-win.yml
create-args: >-
python=${{ matrix.python-version }}
if: runner.os == 'Windows'

- name: Install Package
run: python -m pip install -e .

- name: Test (OS -> ${{ matrix.os }} / Python -> ${{ matrix.python-version }})
run: python -m pytest -v --cov=mbuild --cov-report=xml --cov-append --cov-config=setup.cfg --color yes --pyargs mbuild

bleeding-edge-test:
if: github.event.pull_request.draft == false
Expand All @@ -77,7 +113,7 @@ jobs:
uses: mamba-org/setup-micromamba@v1
with:
environment-file: environment-dev.yml
create-args: python=3.11
create-args: python=3.12

- name: Clone Foyer and GMSO
run: |
Expand Down
23 changes: 10 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
ci:
autofix_commit_msg: |
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
autofix_prs: true
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
autoupdate_schedule: weekly
skip: []
submodules: false
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.7.0
hooks:
# Run the linter.
- id: ruff
args: [--line-length=80, --fix]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
exclude: setup.cfg
- repo: https://github.com/psf/black
rev: 24.10.0
hooks:
- id: black
args: [--line-length=80]

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
args: [--profile=black, --line-length=80]
- repo: https://github.com/pycqa/pydocstyle
rev: '6.3.0'
hooks:
- id: pydocstyle
exclude: ^(mbuild/tests/|docs/|devtools/|setup.py|setup.cfg|mbuild/formats/charmm_writer.py|mbuild/formats/gomc_conf_writer.py|mbuild/utils/specific_ff_to_residue.py)
args: [--convention=numpy]
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sys

import mock
import sphinx_rtd_theme

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down Expand Up @@ -205,7 +206,6 @@
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
# html_theme = 'default'
import sphinx_rtd_theme

html_theme = "sphinx_rtd_theme"
hhtml_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
Expand Down
4 changes: 1 addition & 3 deletions docs/sphinxext/notebook_sphinxext.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ def setup(app):
setup.config = app.config
setup.confdir = app.confdir

app.add_node(
notebook_node, html=(visit_notebook_node, depart_notebook_node)
)
app.add_node(notebook_node, html=(visit_notebook_node, depart_notebook_node))

app.add_directive("notebook", NotebookDirective)
2 changes: 2 additions & 0 deletions mbuild/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ruff: noqa: F401
# ruff: noqa: F403
"""mBuild: a hierarchical, component based molecule builder."""

from mbuild.box import Box
Expand Down
2 changes: 0 additions & 2 deletions mbuild/bond_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

from collections import defaultdict

import networkx as nx


Expand Down
30 changes: 7 additions & 23 deletions mbuild/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ def from_uvec_lengths(cls, uvec, lengths, precision=None):
scaled_vec = (uvec.T * lengths).T
(alpha, beta, gamma) = _calc_angles(scaled_vec)

return cls(
lengths=lengths, angles=(alpha, beta, gamma), precision=precision
)
return cls(lengths=lengths, angles=(alpha, beta, gamma), precision=precision)

@classmethod
def from_mins_maxs_angles(cls, mins, maxs, angles, precision=None):
Expand All @@ -123,28 +121,20 @@ def from_vectors(cls, vectors, precision=None):
Ly = np.linalg.norm(v2)
Lz = np.linalg.norm(v3)
lengths = (Lx, Ly, Lz)
return cls(
lengths=lengths, angles=(alpha, beta, gamma), precision=precision
)
return cls(lengths=lengths, angles=(alpha, beta, gamma), precision=precision)

@classmethod
def from_lengths_tilt_factors(
cls, lengths, tilt_factors=None, precision=None
):
def from_lengths_tilt_factors(cls, lengths, tilt_factors=None, precision=None):
"""Generate a box from box lengths and tilt factors."""
(Lx, Ly, Lz) = lengths
if tilt_factors is None:
(xy, xz, yz) = (0.0, 0.0, 0.0)
else:
(xy, xz, yz) = tilt_factors

vecs = np.asarray(
[[Lx, 0.0, 0.0], [Ly * xy, Ly, 0.0], [Lz * xz, Lz * yz, Lz]]
)
vecs = np.asarray([[Lx, 0.0, 0.0], [Ly * xy, Ly, 0.0], [Lz * xz, Lz * yz, Lz]])
(alpha, beta, gamma) = _calc_angles(vecs)
return cls(
lengths=lengths, angles=[alpha, beta, gamma], precision=precision
)
return cls(lengths=lengths, angles=[alpha, beta, gamma], precision=precision)

@classmethod
def from_lo_hi_tilt_factors(cls, lo, hi, tilt_factors, precision=None):
Expand All @@ -159,9 +149,7 @@ def from_lo_hi_tilt_factors(cls, lo, hi, tilt_factors, precision=None):
yhi_bound = yhi + max([0.0, yz])

lengths = [xhi_bound - xlo_bound, yhi_bound - ylo_bound, zhi - zlo]
return cls.from_lengths_tilt_factors(
lengths=lengths, tilt_factors=tilt_factors
)
return cls.from_lengths_tilt_factors(lengths=lengths, tilt_factors=tilt_factors)

@property
def vectors(self):
Expand Down Expand Up @@ -327,8 +315,6 @@ def _lengths_angles_to_vectors(lengths, angles, precision):
cos_b = np.clip(np.cos(beta), -1.0, 1.0)
cos_g = np.clip(np.cos(gamma), -1.0, 1.0)

sin_a = np.clip(np.sin(alpha), -1.0, 1.0)
sin_b = np.clip(np.sin(beta), -1.0, 1.0)
sin_g = np.clip(np.sin(gamma), -1.0, 1.0)
a_vec = np.asarray([a, 0.0, 0.0])

Expand Down Expand Up @@ -376,9 +362,7 @@ def _normalize_box(vectors):
sign = np.linalg.det(Q)
R = R * sign

signs = np.diag(
np.diag(np.where(R < 0, -np.ones(R.shape), np.ones(R.shape)))
)
signs = np.diag(np.diag(np.where(R < 0, -np.ones(R.shape), np.ones(R.shape))))
transformed_vecs = R.dot(signs)
return _reduced_form_vectors(transformed_vecs.T)

Expand Down
10 changes: 3 additions & 7 deletions mbuild/coarse_graining.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ def _clone(self, clone_of=None, root_container=None):
if self.labels:
for label, compound in self.labels.items():
if not isinstance(compound, list):
newone.labels[label] = compound._clone(
clone_of, root_container
)
newone.labels[label] = compound._clone(clone_of, root_container)
compound.referrers.add(clone_of[compound])
else:
# compound is a list of compounds, so we create an empty
Expand Down Expand Up @@ -150,13 +148,11 @@ def _create_proxy_compounds(real_thing, memo, particle_classes):
proxy = Proxy(real_thing)
memo[real_thing] = proxy

if not type(real_thing) in particle_classes:
if type(real_thing) not in particle_classes:
if not is_leaf(real_thing): # Recurse only if it has parts.
# Recursively create proxies for parts.
for part in real_thing.children:
part_proxy = _create_proxy_compounds(
part, memo, particle_classes
)
part_proxy = _create_proxy_compounds(part, memo, particle_classes)
proxy.add(part_proxy)

return proxy
Expand Down
Loading

0 comments on commit 77eb88e

Please sign in to comment.