Skip to content

Commit

Permalink
[BE] Single ruff config (#751)
Browse files Browse the repository at this point in the history
* ruff toml

* ruff fix

* fix and format

* update ruff

* remove ruff config in pyproject

* lint workflow use ruff.toml

* unpin ruff

* fix circular imports

* clean up imports

* revert to 3.9 annotations, dataclasses_json does not work with __future__ annotations

* pin ruff
  • Loading branch information
lbluque committed Jul 12, 2024
1 parent 7c3f0d2 commit 2e8578a
Show file tree
Hide file tree
Showing 55 changed files with 1,356 additions and 838 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@ jobs:
- name: ruff
run: |
ruff --version
ruff check --statistics --config packages/fairchem-core/pyproject.toml src/fairchem/core/
ruff check --statistics --config packages/fairchem-data-oc/pyproject.toml src/fairchem/data/oc/
#ruff check --statistics --config packages/fairchem-data-om/pyproject.toml src/fairchem/data/om/
#ruff check --statistics --config packages/fairchem-demo-ocpapi/pyproject.toml src/fairchem/demo/ocpapi/
ruff check src
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.4
rev: v0.5.1
hooks:
- id: ruff
args: [ --fix ]
- id: ruff-format
- repo: https://github.com/adamchainz/blacken-docs
rev: 1.16.0
rev: 1.18.0
hooks:
- id: blacken-docs
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
72 changes: 1 addition & 71 deletions packages/fairchem-core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies = [
]

[project.optional-dependencies] # add optional dependencies to be installed as pip install fairchem.core[dev]
dev = ["pre-commit", "pytest", "pytest-cov", "coverage", "syrupy", "ruff==0.4.10"]
dev = ["pre-commit", "pytest", "pytest-cov", "coverage", "syrupy", "ruff==0.5.1"]
docs = ["jupyter-book", "jupytext", "sphinx","sphinx-autoapi", "umap-learn", "vdict"]
adsorbml = ["dscribe","x3dase","scikit-image"]

Expand Down Expand Up @@ -74,73 +74,3 @@ testpaths = ["tests"]

[tool.coverage.run]
source = ["fairchem.core"]

[tool.ruff]
line-length = 88
lint.select = [
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"E", # pycodestyle error
"EXE", # flake8-executable
"F", # pyflakes
"FA", # flake8-future-annotations
"FBT003", # boolean-positional-value-in-call
"FLY", # flynt
"I", # isort
"ICN", # flake8-import-conventions
"PD", # pandas-vet
"PERF", # perflint
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PYI", # flakes8-pyi
"Q", # flake8-quotes
"RET", # flake8-return
"RSE", # flake8-raise
"RUF", # Ruff-specific rules
"SIM", # flake8-simplify
"SLOT", # flake8-slots
"TCH", # flake8-type-checking
"TID", # tidy imports
"TID", # flake8-tidy-imports
"UP", # pyupgrade
"W", # pycodestyle warning
"YTT", # flake8-2020
]
lint.ignore = [
"PLR", # Design related pylint codes
"E501", # Line too long
"B028", # No explicit stacklevel
"EM101", # Exception must not use a string literal
"EM102", # Exception must not use an f-string literal
"G004", # f-string in Logging statement
"RUF015", # Prefer next(iter())
"RET505", # Unnecessary `elif` after `return`
"PT004", # Fixture does not return anthing
"B017", # pytest.raises
"PT011", # pytest.raises
"PT012", # pytest.raises"
"E741", # ambigous variable naming, i.e. one letter
"FBT003", # boolean positional variable in function call
"PERF203", # `try`-`except` within a loop incurs performance overhead (no overhead in Py 3.11+)
"EXE002", # The file is executable but no shebang is present (not sure why some files come up as this)
]

lint.typing-modules = ["mypackage._compat.typing"]
src = ["src"]
lint.unfixable = [
"T20", # Removes print statements
"F841", # Removes unused variables
]
lint.pydocstyle.convention = "google"
lint.isort.known-first-party = ["fairchem.core"]
lint.isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.per-file-ignores]
# Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`.
"src/fairchem/core/__init__.py" = ["I002"]
"src/fairchem/core/conf.py" = ["I002"]
"src/fairchem/core/common/*" = ["PLW0603"] # Using the global statement to update [] is discouraged
"src/fairchem/core/scripts/*" = ["PLW0603"] # Using the global statement to update [] is discouraged
"src/fairchem/core/models/*" = ["PERF401"] # Use a list comprehension to create a transformed list
"src/fairchem/core/models/gemnet*" = ["B023"] # Function definition does not bind loop variable `first_sph`
5 changes: 0 additions & 5 deletions packages/fairchem-data-oc/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,3 @@ content-type = "text/markdown"
fragments = [
{ path = "src/fairchem/data/oc/README.md" }
]

[tool.ruff.lint.per-file-ignores]
# Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`.
"src/fairchem/data/oc/core/__init__.py" = ["F401"]
"src/fairchem/data/oc/utils/__init__.py" = ["F401"]
76 changes: 76 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
include = ["src/fairchem/core/**/*.py", "src/fairchem/data/oc/**/*.py"]
line-length = 88

[lint]
select = [
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"E", # pycodestyle error
"EXE", # flake8-executable
"F", # pyflakes
"FA", # flake8-future-annotations
"FBT003", # boolean-positional-value-in-call
"FLY", # flynt
"I", # isort
"ICN", # flake8-import-conventions
"PD", # pandas-vet
"PERF", # perflint
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PYI", # flakes8-pyi
"Q", # flake8-quotes
"RET", # flake8-return
"RSE", # flake8-raise
"RUF", # Ruff-specific rules
"SIM", # flake8-simplify
"SLOT", # flake8-slots
"TCH", # flake8-type-checking
"TID", # tidy imports
"TID", # flake8-tidy-imports
"UP", # pyupgrade
"W", # pycodestyle warning
"YTT", # flake8-2020
]
ignore = [
"PLR", # Design related pylint codes
"E501", # Line too long
"B028", # No explicit stacklevel
"EM101", # Exception must not use a string literal
"EM102", # Exception must not use an f-string literal
"G004", # f-string in Logging statement
"RUF015", # Prefer next(iter())
"RET505", # Unnecessary `elif` after `return`
"PT004", # Fixture does not return anthing
"B017", # pytest.raises
"PT011", # pytest.raises
"PT012", # pytest.raises"
"E741", # ambigous variable naming, i.e. one letter
"FBT003", # boolean positional variable in function call
"PERF203", # `try`-`except` within a loop incurs performance overhead (no overhead in Py 3.11+)
"EXE002", # The file is executable but no shebang is present (not sure why some files come up as this)
"PLC2401", # non ASCII characters
]
unfixable = [
"T20", # Removes print statements
"F841", # Removes unused variables
]

[lint.isort]
known-first-party = ["fairchem.core"]
required-imports = ["from __future__ import annotations"]

[lint.pydocstyle]
convention = "google"

[lint.per-file-ignores]
# Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`.
"src/fairchem/core/__init__.py" = ["I002"]
"src/fairchem/core/conf.py" = ["I002"]
"src/fairchem/core/common/*" = ["PLW0603"] # Using the global statement to update [] is discouraged
"src/fairchem/core/scripts/*" = ["PLW0603"] # Using the global statement to update [] is discouraged
"src/fairchem/core/models/*" = ["PERF401"] # Use a list comprehension to create a transformed list
"src/fairchem/core/models/gemnet*" = ["B023"] # Function definition does not bind loop variable `first_sph`
# Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`.
"src/fairchem/data/oc/core/__init__.py" = ["F401"]
"src/fairchem/data/oc/utils/__init__.py" = ["F401"]
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import argparse
import pickle
from collections import defaultdict
Expand All @@ -19,9 +21,7 @@ def is_successful(best_pred_energy, best_dft_energy, SUCCESS_THRESHOLD=0.1):
# Given best ML and DFT energy, compute various success metrics:
# success_parity: base success metric (ML - DFT <= SUCCESS_THRESHOLD)
diff = best_pred_energy - best_dft_energy
success_parity = diff <= SUCCESS_THRESHOLD

return success_parity
return diff <= SUCCESS_THRESHOLD


def compute_valid_ml_success(ml_data, dft_data):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
}
"""

from __future__ import annotations

import argparse
import pickle
from collections import defaultdict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- errors_by_sid.pkl: any errors that occurred
"""

from __future__ import annotations

import argparse
import multiprocessing as mp
import os
Expand Down Expand Up @@ -52,8 +54,7 @@ def parse_args():
)
parser.add_argument("--surface-dir", type=str, help="Path to surface DFT outputs")

args = parser.parse_args()
return args
return parser.parse_args()


def min_diff(atoms_init, atoms_final):
Expand Down Expand Up @@ -159,7 +160,7 @@ def process_mlrs(arg):
continue
anomalies[sid] = anomaly
if not anomaly:
grouped_configs[system].append(tuple([adslab_idx, predE, mlrs]))
grouped_configs[system].append((adslab_idx, predE, mlrs))

# group configs by system and sort
sorted_grouped_configs = {}
Expand Down
4 changes: 3 additions & 1 deletion src/fairchem/applications/AdsorbML/adsorbml/scripts/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import glob
import os

from pymatgen.io.vasp.outputs import *
from pymatgen.io.vasp.outputs import Oszicar

"""
This script provides utility functions that can be useful for trying to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import argparse
import os
import pickle
Expand Down
4 changes: 3 additions & 1 deletion src/fairchem/applications/cattsunami/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .reaction import Reaction
from __future__ import annotations

from .ocpneb import OCPNEB
from .reaction import Reaction

__all__ = ["Reaction", "OCPNEB"]
50 changes: 26 additions & 24 deletions src/fairchem/applications/cattsunami/core/autoframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
and final frames for NEB calculations.
"""

import numpy as np
from __future__ import annotations

import copy
from copy import deepcopy
from itertools import combinations, product
from typing import TYPE_CHECKING

import ase
import networkx as nx
import numpy as np
import torch
from ase.data import atomic_numbers, covalent_radii
from scipy.spatial.distance import euclidean
from itertools import combinations, product
from ase.optimize import BFGS
import copy
import torch
from fairchem.data.oc.utils import DetectTrajAnomaly
import networkx as nx
from copy import deepcopy
from fairchem.applications.cattsunami.core import Reaction
from scipy.spatial.distance import euclidean

if TYPE_CHECKING:
from fairchem.applications.cattsunami.core import Reaction


class AutoFrame:
Expand Down Expand Up @@ -76,13 +82,11 @@ def only_keep_unique_systems(self, systems, energies):
# Iterate over the systems and see where there are matches (systems where every adsorbate atom is overlapping)
for idx in range(len(systems)):
if not any(
[
self.are_all_adsorbate_atoms_overlapping(
adsorbates_stripped_out[unique_system],
adsorbates_stripped_out[idx],
)
for unique_system in unique_systems
]
self.are_all_adsorbate_atoms_overlapping(
adsorbates_stripped_out[unique_system],
adsorbates_stripped_out[idx],
)
for unique_system in unique_systems
):
unique_systems.append(idx)
unique_energies.append(energies[idx])
Expand Down Expand Up @@ -162,9 +166,9 @@ def __init__(
product1_energies: list,
product2_systems: list,
product2_energies: list,
r_product1_max: float = None,
r_product2_max: float = None,
r_product2_min: float = None,
r_product1_max: float | None = None,
r_product2_max: float | None = None,
r_product2_min: float | None = None,
):
"""
Initialize class to handle the automatic generation of NEB frames for dissociation.
Expand Down Expand Up @@ -1071,8 +1075,7 @@ def interpolate_and_correct_frames(
reaction,
map_idx,
)
images = interpolate(initial, final, n_frames)
return images
return interpolate(initial, final, n_frames)


def get_shortest_path(
Expand Down Expand Up @@ -1122,7 +1125,7 @@ def get_shortest_path(
# atoms (2) the bound atom of product 1 (3) the atom of product 2 which formed a new bond
shortest_path_final_positions = []
equivalent_idx_factors = len(initial) * np.array(list(range(9)))
for idx, atom in enumerate(initial):
for idx, _atom in enumerate(initial):
equivalent_indices = equivalent_idx_factors + idx
final_distances = [
euclidean(initial.positions[idx], new_atoms_final.positions[i])
Expand Down Expand Up @@ -1421,8 +1424,7 @@ def get_product2_idx(
edge for edge in reaction.edge_list_initial if edge not in edge_list_final
][0]
flat_nodes = [item for sublist in traversal_rxt1_final for item in sublist]
product2_binding_idx = [idx for idx in broken_edge if idx not in flat_nodes][0]
return product2_binding_idx
return [idx for idx in broken_edge if idx not in flat_nodes][0]


def traverse_adsorbate_general(
Expand Down Expand Up @@ -1556,7 +1558,7 @@ def interpolate(initial_frame: ase.Atoms, final_frame: ase.Atoms, num_frames: in
atoms_frames.append(atoms_now)

# Iteratively update positions to avoid overlap
for i in range(100):
for _i in range(100):
rate = 0.1

frame_dist = []
Expand Down
Loading

0 comments on commit 2e8578a

Please sign in to comment.