Skip to content

Commit

Permalink
Simplify pre-commit setup (#29)
Browse files Browse the repository at this point in the history
This PR simplifies the pre-commit setup by:

- removing code checks for imports against external packages, these
checks should be done when running ci/cd integration tests (which will
be added in a separate PR) where external dependencies are installed.
After this PR is merged, pre-commit will therefore only check if code is
clean and self-consistent, without the need to install external
dependencies (this is the same approach taken in for example
https://github.com/pydata/xarray/blob/main/.pre-commit-config.yaml).
Testing imports is _also_ important, but splitting the code-cleaning out
allows for much faster iteration (i.e. these linting tests will fail,
avoiding the need to install all dependencies)

- pinning versions of used linting tools. The current setup is
errorprone because as linting tools evolve the linting rules will
update. Pinning versions ensures that we all use the same versions when
running `pre-commit`

- moves linting tool versions to pre-commit config rather than
`requirements.txt`. This allows pre-commit handle the linting in its own
virtual env, without polluting the dev environment

- using github action to install and run pre-commit rather than our own
run instructions. This ensure that pre-commit is run identically both
during local development and during ci/cd in github actions.

---------

Co-authored-by: sadamov <45732287+sadamov@users.noreply.github.com>
Co-authored-by: khintz <kah@dmi.dk>
  • Loading branch information
3 people authored May 22, 2024
1 parent 4a97a12 commit 5b71be3
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
ignore = E203, F811, I002, W503
36 changes: 14 additions & 22 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
name: Run pre-commit job
name: lint

on:
push:
# trigger on pushes to any branch, but not main
push:
branches-ignore:
- main
# and also on PRs to main
pull_request:
branches:
- main
pull_request:
branches:
- main
- main

jobs:
pre-commit-job:
pre-commit-job:
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0}
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install pre-commit hooks
run: |
pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 \
--index-url https://download.pytorch.org/whl/cpu
pip install -r requirements.txt
pip install pyg-lib==0.2.0 torch-scatter==2.1.1 torch-sparse==0.6.17 \
torch-cluster==1.6.1 torch-geometric==2.3.1 \
-f https://pytorch-geometric.com/whl/torch-2.0.1+cpu.html
- name: Run pre-commit hooks
run: |
pre-commit run --all-files
python-version: ${{ matrix.python-version }}
- uses: pre-commit/action@v2.0.3
66 changes: 26 additions & 40 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,51 +1,37 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-ast
- id: check-case-conflict
- id: check-docstring-first
- id: check-symlinks
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: local
- id: check-ast
- id: check-case-conflict
- id: check-docstring-first
- id: check-symlinks
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
name: codespell
- id: codespell
description: Check for spelling errors
language: system
entry: codespell
- repo: local

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
name: black
- id: black
description: Format Python code
language: system
entry: black
types_or: [python, pyi]
- repo: local

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
name: isort
- id: isort
description: Group and sort Python imports
language: system
entry: isort
types_or: [python, pyi, cython]
- repo: local

- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
name: flake8
- id: flake8
description: Check Python code for correctness, consistency and adherence to best practices
language: system
entry: flake8 --max-line-length=80 --ignore=E203,F811,I002,W503
types: [python]
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint -rn -sn
language: system
types: [python]
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[\#13](https://github.com/joeloskarsson/neural-lam/pull/13)
@joeloskarsson

### Maintenance

- simplify pre-commit setup by 1) reducing linting to only cover static
analysis excluding imports from external dependencies (this will be handled
in build/test cicd action introduced later), 2) pinning versions of linting
tools in pre-commit config (and remove from `requirements.txt`) and 3) using
github action to run pre-commit.
[\#29](https://github.com/mllam/neural-lam/pull/29)
@leifdenby


## [v0.1.0](https://github.com/joeloskarsson/neural-lam/releases/tag/v0.1.0)

First tagged release of `neural-lam`, matching Oskarsson et al 2023 publication
Expand Down
14 changes: 7 additions & 7 deletions neural_lam/models/base_hi_graph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, args):
if level_index < (self.num_levels - 1):
up_edges = self.mesh_up_features[level_index].shape[0]
down_edges = self.mesh_down_features[level_index].shape[0]
print(f" {level_index}<->{level_index+1}")
print(f" {level_index}<->{level_index + 1}")
print(f" - {up_edges} up edges, {down_edges} down edges")
# Embedders
# Assume all levels have same static feature dimensionality
Expand Down Expand Up @@ -179,9 +179,9 @@ def process_step(self, mesh_rep):
)

# Update node and edge vectors in lists
mesh_rep_levels[level_l] = (
new_node_rep # (B, num_mesh_nodes[l], d_h)
)
mesh_rep_levels[
level_l
] = new_node_rep # (B, num_mesh_nodes[l], d_h)
mesh_up_rep[level_l - 1] = new_edge_rep # (B, M_up[l-1], d_h)

# - PROCESSOR -
Expand All @@ -207,9 +207,9 @@ def process_step(self, mesh_rep):
new_node_rep = gnn(send_node_rep, rec_node_rep, edge_rep)

# Update node and edge vectors in lists
mesh_rep_levels[level_l] = (
new_node_rep # (B, num_mesh_nodes[l], d_h)
)
mesh_rep_levels[
level_l
] = new_node_rep # (B, num_mesh_nodes[l], d_h)

# Return only bottom level representation
return mesh_rep_levels[0] # (B, num_mesh_nodes[0], d_h)
Expand Down
6 changes: 1 addition & 5 deletions plot_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ def main():

# Load graph data
hierarchical, graph_ldict = utils.load_graph(args.graph)
(
g2m_edge_index,
m2g_edge_index,
m2m_edge_index,
) = (
(g2m_edge_index, m2g_edge_index, m2m_edge_index,) = (
graph_ldict["g2m_edge_index"],
graph_ldict["m2g_edge_index"],
graph_ldict["m2m_edge_index"],
Expand Down
6 changes: 1 addition & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ Cartopy>=0.22.0
pyproj>=3.4.1
tueplots>=0.0.8
plotly>=5.15.0

# for dev
codespell>=2.0.0
black>=21.9b0
isort>=5.9.3
flake8>=4.0.1
pylint>=3.0.3
pre-commit>=2.15.0

0 comments on commit 5b71be3

Please sign in to comment.