Skip to content

Commit

Permalink
Ruff + configuration -> pyproject.toml; newer Ruff version in dev env…
Browse files Browse the repository at this point in the history
…; final Ruff checks and fixes in scripts; added Ruff CI job
  • Loading branch information
JHKru committed Sep 24, 2024
1 parent d5ab251 commit 87269f4
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 56 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ name: Testing Springcraft
on: [push, pull_request]

jobs:
lint:
name: Check code style
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install ruff
run: pip install ruff==0.5.2
- name: Check code formatting
run: ruff format --diff
- name: Lint code base
run: ruff check
test:
name: Testing

Expand All @@ -19,7 +33,7 @@ jobs:
auto-update-conda: true
python-version: '3.10'
- name: Installing dependencies
run: conda install -c conda-forge poetry prody pytest r-bio3d rpy2
run: conda install -c conda-forge poetry pytest
- name: Building distribution
run: poetry build -f wheel
- name: Installing distribution
Expand Down
10 changes: 5 additions & 5 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

__author__ = "Patrick Kunzmann"

from os.path import realpath, dirname, join
import types
import sys
import types
from os.path import dirname, join, realpath

import scraper
import springcraft

# Include 'src/' in PYTHONPATH
# in order to import the 'Ammolite' package
doc_path = dirname(realpath(__file__))
package_path = join(dirname(doc_path), "src")
sys.path.insert(0, package_path)
import springcraft

# Include springcraft/doc in PYTHONPATH
# in order to import modules for example generation etc.
sys.path.insert(0, doc_path)
import scraper


#### General ####

Expand Down
7 changes: 3 additions & 4 deletions doc/examples/scripts/basic_nma.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
# Code source: Jan Krumbach
# License: BSD 3 clause

import numpy as np
import matplotlib.pyplot as plt
import biotite
import biotite.database.rcsb as rcsb
import biotite.structure as struc
import biotite.structure.io.mmtf as mmtf
import biotite.database.rcsb as rcsb
import matplotlib.pyplot as plt
import numpy as np
import springcraft


# Fetch G:T/U Mismatch-specific DNA glycosylase from E. coli
PDB_ID = "1MUG"
mmtf_file = mmtf.MMTFFile.read(rcsb.fetch(PDB_ID, "mmtf"))
Expand Down
7 changes: 3 additions & 4 deletions doc/examples/scripts/normal_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
# Code source: Patrick Kunzmann
# License: BSD 3 clause

import numpy as np
import ammolite
import biotite.database.rcsb as rcsb
import biotite.structure as struc
import biotite.structure.io.mmtf as mmtf
import biotite.database.rcsb as rcsb
import ammolite
import numpy as np
import springcraft


PNG_SIZE = (800, 800)


Expand Down
5 changes: 3 additions & 2 deletions doc/scraper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from glob import glob
import datetime
import shutil
import tempfile
import datetime
import time
from os.path import getsize

from sphinx_gallery.scrapers import figure_rst

NO_ASSIGN = "___"
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ dependencies:
- sphinx-gallery =0.9.0
- numpydoc >=0.8
- ammolite >=0.8
- ruff =0.5.2
- ruff >=0.6.7
21 changes: 20 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,29 @@ sphinx-gallery = "0.9.0"
numpydoc = ">= 0.8"
ammolite = ">=0.8"


[build-system]
requires = ["poetry_core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.ruff.lint]
# pyflakes, pycodestyle isort and variable naming
select = ["F", "E", "W", "I", "TID", "N"]
ignore = [
# In docstrings long lines are often intentional
# Most other ocassions are caught by the ruff formatter
"E501",
# camel cases for forcefields
"N802",
# eANM/sdENM as static methods of TabulatedForceField
# should not accept "self" as argument by design
"N805",
# Due to constants used in functions
"N806",
]

[tool.ruff.lint.per-file-ignores]
# Due to `from .module import *` imports in `__init__.py` modules
"__init__.py" = ["F403", "TID252"]

[tool.pytest.ini_options]
addopts = "--ignore=tests/data"
5 changes: 3 additions & 2 deletions src/springcraft/anm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
__author__ = "Patrick Kunzmann"
__all__ = ["ANM"]

import numpy as np
import biotite.structure as struc
from .interaction import compute_hessian
import numpy as np

from . import nma
from .interaction import compute_hessian

K_B = 1.380649e-23
N_A = 6.02214076e23
Expand Down
18 changes: 9 additions & 9 deletions src/springcraft/forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"TabulatedForceField",
]

import numbers
import abc
from os.path import join, dirname, realpath
import numpy as np
import biotite.structure as struc
import biotite.sequence as seq
import numbers
from os.path import dirname, join, realpath

import biotite.sequence as seq
import biotite.structure as struc
import numpy as np

DATA_DIR = join(dirname(realpath(__file__)), "data")

Expand Down Expand Up @@ -439,7 +439,7 @@ def __init__(self, atoms, bonded, intra_chain, inter_chain, cutoff_distance):
raise TypeError(f"Expected 'AtomArray', not {type(atoms).__name__}")
if not np.all((atoms.atom_name == "CA") & (atoms.element == "C")):
raise struc.BadStructureError(
f"AtomArray does not contain exclusively CA atoms"
"AtomArray does not contain exclusively CA atoms"
)

self._natoms = atoms.array_length()
Expand Down Expand Up @@ -756,8 +756,8 @@ def e_anm(atoms, nonbonded_mean=False):
Protein Science, 7 2578-2586 (1998)
"""

intra = _load_matrix(f"miyazawa.csv")
inter = _load_matrix(f"keskin.csv")
intra = _load_matrix("miyazawa.csv")
inter = _load_matrix("keskin.csv")

if nonbonded_mean:
intra = np.average(intra) * np.ones(shape=(20, 20))
Expand Down Expand Up @@ -882,7 +882,7 @@ def _convert_to_matrix(value, n_bins):
3D matrix.
"""
if np.isnan(value).any():
raise IndexError(f"Array contains NaN elements")
raise IndexError("Array contains NaN elements")

if isinstance(value, numbers.Number):
# One value for all distances and types
Expand Down
5 changes: 3 additions & 2 deletions src/springcraft/gnm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
__author__ = "Patrick Kunzmann, Faisal Islam"
__all__ = ["GNM"]

import numpy as np
import biotite.structure as struc
from .interaction import compute_kirchhoff
import numpy as np

from . import nma
from .interaction import compute_kirchhoff

K_B = 1.380649e-23
N_A = 6.02214076e23
Expand Down
2 changes: 1 addition & 1 deletion src/springcraft/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
__author__ = "Patrick Kunzmann, Jan Krumbach"
__all__ = ["compute_kirchhoff", "compute_hessian"]

import numpy as np
import biotite.structure as struc
import numpy as np


def compute_kirchhoff(coord, force_field, use_cell_list=True):
Expand Down
11 changes: 6 additions & 5 deletions src/springcraft/nma.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
]

import numpy as np

# -> Import ANM/GNM in functions to prevent circular import error

K_B = 1.380649e-23
Expand Down Expand Up @@ -43,8 +44,8 @@ def eigen(enm):
Eigenvectors of the *Kirchhoff*/*Hessian* matrix.
``eig_values[i]`` corresponds to ``eig_vectors[i]``.
"""
from .gnm import GNM
from .anm import ANM
from .gnm import GNM

# Assign Kirchhoff/Hessian
if isinstance(enm, GNM):
Expand Down Expand Up @@ -81,8 +82,8 @@ def frequencies(enm):
The frequency in ascending order of the associated modes'
Eigenvalues.
"""
from .gnm import GNM
from .anm import ANM
from .gnm import GNM

if isinstance(enm, GNM):
ntriv_modes = 1
Expand Down Expand Up @@ -133,8 +134,8 @@ def mean_square_fluctuation(enm, mode_subset=None, tem=None, tem_factors=K_B):
msqf : ndarray, shape=(n,), dtype=float
The mean square fluctuations for each atom in the model.
"""
from .gnm import GNM
from .anm import ANM
from .gnm import GNM

if not isinstance(enm, (GNM, ANM)):
raise ValueError("Instance of GNM/ANM class expected.")
Expand Down Expand Up @@ -215,8 +216,8 @@ def bfactor(enm, mode_subset=None, tem=None, tem_factors=K_B):
bfac_values : ndarray, shape=(n,), dtype=float
B-factors of C-alpha atoms.
"""
from .gnm import GNM
from .anm import ANM
from .gnm import GNM

if not isinstance(enm, (GNM, ANM)):
raise ValueError("Instance of GNM/ANM class expected.")
Expand Down Expand Up @@ -287,8 +288,8 @@ def dcc(enm, mode_subset=None, norm=True, tem=None, tem_factors=K_B):
for 'mode_subset' and 'memory_efficient' are passed to the function.
"""

from .gnm import GNM
from .anm import ANM
from .gnm import GNM

eig_values, eig_vectors = enm.eigen()
n_nodes = len(enm._coord)
Expand Down
10 changes: 4 additions & 6 deletions tests/data/test_data_prep_prody_bio3d.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import abc
import os
from os import path

import biotite.database.rcsb as rcsb
import biotite.structure as struc
import biotite.structure.io as bstio
import biotite.structure.io.pdb as pdb
import biotite.database.rcsb as rcsb
import numpy as np
import prody
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import DataFrame as DataFrame_r
from rpy2.robjects import numpy2ri
from rpy2.robjects.conversion import localconverter
from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import DataFrame as DataFrame_r

FETCH_PDB_IDS = ["1l2y", "7cal"]

Expand Down Expand Up @@ -184,7 +182,7 @@ def prody_enm_nma(enm_type, structure_path, cutoff_list, output_markers="all"):
def bio3d_anm_nma(structure_path, bio3d_ff, output_markers="all"):
# Check bio3d_ff and outputs
accepted_bio3d_ff = ["calpha", "sdenm", "pfanm"]
if not bio3d_ff in accepted_bio3d_ff:
if bio3d_ff not in accepted_bio3d_ff:
raise ValueError(
"Onle the following bio3d FFs are accepted: \n", f"{accepted_bio3d_ff}"
)
Expand Down
9 changes: 5 additions & 4 deletions tests/test_anm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import glob
import itertools
from multiprocessing.spawn import prepare
from os.path import basename, join
import glob
import numpy as np
import pytest

import biotite.structure as struc
import biotite.structure.io.pdb as pdb
import numpy as np
import pytest
import springcraft

from .util import data_dir


Expand Down
10 changes: 6 additions & 4 deletions tests/test_forcefield.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import itertools
from os.path import join
import numpy as np
import pytest

import biotite.sequence as seq
import biotite.structure as struc
import biotite.structure.io.pdb as pdb
import biotite.sequence as seq
import numpy as np
import pytest
import springcraft
from springcraft.forcefield import InvariantForceField

from .util import data_dir


Expand Down Expand Up @@ -330,6 +331,7 @@ def test_tabulated_forcefield_predefined(atoms, name):
meth = getattr(springcraft.TabulatedForceField, name)
ff = meth(atoms)

assert ff is not None

def test_parameterfree_forcefield():
"""
Expand Down
6 changes: 4 additions & 2 deletions tests/test_gnm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import itertools
from os.path import join, basename
from os.path import basename, join

import biotite.structure.io.pdb as pdb
import numpy as np
import pytest
import biotite.structure.io.pdb as pdb
import springcraft

from .util import data_dir


Expand Down
3 changes: 2 additions & 1 deletion tests/test_interaction.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import itertools
from os.path import join

import numpy as np
import pytest
import biotite.structure.io.pdb as pdb
import springcraft

from .util import data_dir


Expand Down
3 changes: 2 additions & 1 deletion tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from os.path import join, dirname, realpath
from os.path import dirname, join, realpath

from springcraft import __version__


Expand Down
Loading

0 comments on commit 87269f4

Please sign in to comment.