Skip to content

Commit

Permalink
Switch linting to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
ffl096 committed Jan 31, 2024
1 parent 161df86 commit 0b95211
Show file tree
Hide file tree
Showing 35 changed files with 225 additions and 289 deletions.
34 changes: 6 additions & 28 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
name: "Lint"
name: Linting

on:
push:
branches: [main,github-actions-test]
branches: [ main,github-actions-test ]
pull_request:
branches: [main]
branches: [ main ]

jobs:
black-isort-flake8:

runs-on: ${{matrix.os}}
strategy:
matrix:
os: [ubuntu-latest]
python-version: [3.11.3]
fail-fast: false

ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Linting
uses: actions/setup-python@v4
with:
python-version: ${{matrix.python-version}}
cache: "pip"
cache-dependency-path: "pyproject.toml"

- name: Install main package [pip]
run: |
pip install -e .[dev]
- name : linting [black, isort, flake8]
run: |
black . --check
isort --profile black --check .
flake8 .
- uses: chartboost/ruff-action@v1
33 changes: 12 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
default_language_version:
python: python3.11
python: python3.10

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-byte-order-marker
- id: fix-byte-order-marker
- id: check-case-conflict
- id: check-merge-conflict
- id: check-yaml
Expand All @@ -18,25 +18,16 @@ repos:
- id: trailing-whitespace
- id: requirements-txt-fixer

- repo: https://github.com/psf/black
rev: 23.10.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.14
hooks:
- id: black-jupyter
- id: ruff
types_or: [ python, pyi, jupyter ]
args: [ --fix ]
- id: ruff-format
types_or: [ python, pyi, jupyter ]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/numpy/numpydoc
rev: v1.6.0
hooks:
- id : isort
args : ["--profile=black", "--filter-files"]

- repo: https://github.com/asottile/blacken-docs
rev: 1.13.0
hooks:
- id: blacken-docs
additional_dependencies: [black==23.3.0]

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-docstrings, Flake8-pyproject]
- id: numpydoc-validation
63 changes: 34 additions & 29 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies=[

[project.optional-dependencies]
doc = [
"hypernetx",
"jupyter",
"nbsphinx",
"nbsphinx_link",
Expand All @@ -49,13 +50,8 @@ doc = [
"pydata-sphinx-theme"
]
lint = [
"black < 24.0",
"black[jupyter]",
"flake8",
"flake8-docstrings",
"Flake8-pyproject",
"isort",
"pre-commit"
"pre-commit",
"ruff"
]
test = [
"pytest",
Expand All @@ -74,6 +70,30 @@ all = ["TopoModelX[dev, doc]"]
homepage="https://github.com/pyt-team/TopoModelX"
repository="https://github.com/pyt-team/TopoModelX"

[tool.ruff]
target-version = "py310"
extend-include = ["*.ipynb"]

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.lint]
select = [
"F", # pyflakes errors
"E", # code style
"W", # warnings
"I", # import order
"UP", # pyupgrade rules
"SIM", # code simplifications
]
ignore = ["E501"] # line too long

[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.ruff.per-file-ignores]
"__init__.py" = ["F403"]

[tool.setuptools.dynamic]
version = {attr = "topomodelx.__version__"}

Expand All @@ -98,26 +118,11 @@ ignore_missing_imports = true
[tool.pytest.ini_options]
addopts = "--capture=no"

[tool.black]
line-length = 88

[tool.isort]
line_length = 88
multi_line_output = 3
include_trailing_comma = true
skip = [".gitignore", "__init__.py"]

[tool.flake8]
max-line-length = 88
application_import_names = "topomodelx"
docstring-convention = "numpy"
exclude = [
"topomodelx/__init__.py",
"docs/conf.py"
]

import_order_style = "smarkets"
extend-ignore = ["E501", "E203"]
per-file-ignores = [
"*/__init__.py: D104, F401",
[tool.numpydoc_validation]
checks = [
"all",
"GL01",
"ES01",
"EX01",
"SA01"
]
2 changes: 1 addition & 1 deletion topomodelx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__version__ = "0.0.1"

from .base import *
from .nn import *
from .utils import *
from .base import *
9 changes: 4 additions & 5 deletions topomodelx/nn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from .cell.can_layer import (
CANLayer,
LiftLayer,
MultiHeadLiftLayer,
PoolLayer,
MultiHeadCellAttention,
MultiHeadCellAttention_v2,
CANLayer,
MultiHeadLiftLayer,
PoolLayer,
)
from .cell.ccxn_layer import CCXNLayer
from .cell.cwn_layer import (
CWNLayer,
_CWNDefaultAggregate,
_CWNDefaultFirstConv,
_CWNDefaultSecondConv,
_CWNDefaultAggregate,
_CWNDefaultUpdate,
)


__all__ = [
"LiftLayer",
"MultiHeadLiftLayer",
Expand Down
16 changes: 9 additions & 7 deletions topomodelx/nn/cell/can_layer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Cell Attention Network layer."""

from typing import Callable, Literal
from collections.abc import Callable
from typing import Literal

import torch
from torch import nn, topk
from torch.nn import Linear, Parameter
from torch.nn import Linear, Parameter, init
from torch.nn import functional as F
from torch.nn import init

from topomodelx.base.aggregation import Aggregation
from topomodelx.base.message_passing import MessagePassing
Expand Down Expand Up @@ -112,7 +112,7 @@ def __init__(
signal_lift_activation: Callable,
signal_lift_dropout: float,
) -> None:
super(LiftLayer, self).__init__()
super().__init__()

self.in_channels_0 = in_channels_0
self.att_parameter = nn.Parameter(torch.empty(size=(2 * in_channels_0, heads)))
Expand Down Expand Up @@ -201,7 +201,7 @@ def __init__(
signal_lift_dropout: float = 0.0,
signal_lift_readout: str = "cat",
) -> None:
super(MultiHeadLiftLayer, self).__init__()
super().__init__()

assert heads > 0, ValueError("Number of heads must be > 0")
assert signal_lift_readout in [
Expand Down Expand Up @@ -305,7 +305,7 @@ def __init__(
signal_pool_activation: Callable,
readout: bool = True,
) -> None:
super(PoolLayer, self).__init__()
super().__init__()

self.k_pool = k_pool
self.in_channels_0 = in_channels_0
Expand All @@ -322,7 +322,9 @@ def reset_parameters(self) -> None:
gain = init.calculate_gain("relu")
init.xavier_uniform_(self.att_pool.data, gain=gain)

def forward(self, x_0, lower_neighborhood, upper_neighborhood) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: # type: ignore[override]
def forward( # type: ignore[override]
self, x_0, lower_neighborhood, upper_neighborhood
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
r"""Forward pass.
Parameters
Expand Down
2 changes: 1 addition & 1 deletion topomodelx/nn/combinatorial/hmc_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ def __init__(
update_func_aggregation=None,
initialization="xavier_uniform",
):
super(HMCLayer, self).__init__()
super().__init__()
super().__init__()

assert (
Expand Down
2 changes: 1 addition & 1 deletion topomodelx/nn/hypergraph/allset_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def __init__(
mlp_dropout: float = 0.0,
mlp_norm=None,
) -> None:
super(AllSetBlock, self).__init__()
super().__init__()

self.dropout = dropout
if mlp_num_layers > 0:
Expand Down
2 changes: 1 addition & 1 deletion topomodelx/nn/hypergraph/unigcn_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
) -> None:
super().__init__()

with_linear_transform = False if in_channels == hidden_channels else True
with_linear_transform = in_channels != hidden_channels
self.conv_level1_0_to_1 = Conv(
in_channels=in_channels,
out_channels=hidden_channels,
Expand Down
4 changes: 2 additions & 2 deletions topomodelx/nn/hypergraph/unisage_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ def __init__(
self.vertex2edge = Conv(
in_channels=hidden_channels,
out_channels=hidden_channels,
aggr_norm=False if self.e_aggr == "sum" else True,
aggr_norm=self.e_aggr != "sum",
with_linear_transform=False,
)

self.edge2vertex = Conv(
in_channels=hidden_channels,
out_channels=hidden_channels,
aggr_norm=False if self.v_aggr == "sum" else True,
aggr_norm=self.v_aggr != "sum",
with_linear_transform=False,
)

Expand Down
25 changes: 12 additions & 13 deletions tutorials/cell/can_train.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,12 @@
},
"outputs": [],
"source": [
"import random\n",
"\n",
"import numpy as np\n",
"import torch\n",
"from sklearn.model_selection import train_test_split\n",
"from toponetx.classes.cell_complex import CellComplex\n",
"from torch_geometric.datasets import TUDataset\n",
"from torch_geometric.utils.convert import to_networkx\n",
"import torch.nn.functional as F\n",
"\n",
"\n",
"from topomodelx.nn.cell.can import CAN\n",
"from topomodelx.utils.sparse import from_sparse\n",
Expand Down Expand Up @@ -263,7 +259,7 @@
" try:\n",
" upper_neighborhood_t = cell_complex.up_laplacian_matrix(rank=1)\n",
" upper_neighborhood_t = from_sparse(upper_neighborhood_t)\n",
" except:\n",
" except ValueError:\n",
" upper_neighborhood_t = np.zeros(\n",
" (lower_neighborhood_t.shape[0], lower_neighborhood_t.shape[0])\n",
" )\n",
Expand Down Expand Up @@ -482,9 +478,10 @@
" x_0 = x_0.float().to(device)\n",
" x_1, y = x_1.float().to(device), torch.tensor(y, dtype=torch.long).to(device)\n",
" adjacency = adjacency.float().to(device)\n",
" lower_neighborhood, upper_neighborhood = lower_neighborhood.float().to(\n",
" device\n",
" ), upper_neighborhood.float().to(device)\n",
" lower_neighborhood, upper_neighborhood = (\n",
" lower_neighborhood.float().to(device),\n",
" upper_neighborhood.float().to(device),\n",
" )\n",
" opt.zero_grad()\n",
" y_hat = model(x_0, x_1, adjacency, lower_neighborhood, upper_neighborhood)\n",
" loss = crit(y_hat, y)\n",
Expand All @@ -511,13 +508,15 @@
" y_test,\n",
" ):\n",
" x_0 = x_0.float().to(device)\n",
" x_1, y = x_1.float().to(device), torch.tensor(y, dtype=torch.long).to(\n",
" device\n",
" x_1, y = (\n",
" x_1.float().to(device),\n",
" torch.tensor(y, dtype=torch.long).to(device),\n",
" )\n",
" adjacency = adjacency.float().to(device)\n",
" lower_neighborhood, upper_neighborhood = lower_neighborhood.float().to(\n",
" device\n",
" ), upper_neighborhood.float().to(device)\n",
" lower_neighborhood, upper_neighborhood = (\n",
" lower_neighborhood.float().to(device),\n",
" upper_neighborhood.float().to(device),\n",
" )\n",
" y_hat = model(\n",
" x_0, x_1, adjacency, lower_neighborhood, upper_neighborhood\n",
" )\n",
Expand Down
Loading

0 comments on commit 0b95211

Please sign in to comment.