From 0b95211d5a9bb0427fa5914b5c4e96696e8a4480 Mon Sep 17 00:00:00 2001 From: Florian Frantzen Date: Wed, 31 Jan 2024 09:31:01 +0100 Subject: [PATCH] Switch linting to ruff --- .github/workflows/lint.yml | 34 ++-------- .pre-commit-config.yaml | 33 ++++------ pyproject.toml | 63 ++++++++++--------- topomodelx/__init__.py | 2 +- topomodelx/nn/__init__.py | 9 ++- topomodelx/nn/cell/can_layer.py | 16 ++--- topomodelx/nn/combinatorial/hmc_layer.py | 2 +- topomodelx/nn/hypergraph/allset_layer.py | 2 +- topomodelx/nn/hypergraph/unigcn_layer.py | 2 +- topomodelx/nn/hypergraph/unisage_layer.py | 4 +- tutorials/cell/can_train.ipynb | 25 ++++---- tutorials/cell/ccxn_train.ipynb | 35 ++++++----- tutorials/cell/cwn_train.ipynb | 6 +- tutorials/combinatorial/hmc_train.ipynb | 6 +- tutorials/hypergraph/allset_train.ipynb | 21 +++---- .../hypergraph/allset_transformer_train.ipynb | 19 +++--- tutorials/hypergraph/dhgcn_train.ipynb | 11 ++-- tutorials/hypergraph/hmpnn_train.ipynb | 9 +-- tutorials/hypergraph/hnhn_train.ipynb | 21 +++---- tutorials/hypergraph/hypergat_train.ipynb | 9 +-- tutorials/hypergraph/hypersage_train.ipynb | 21 +++---- tutorials/hypergraph/unigcn_train.ipynb | 15 ++--- tutorials/hypergraph/unigcnii_train.ipynb | 24 +++---- tutorials/hypergraph/unigin_train.ipynb | 12 ++-- tutorials/hypergraph/unisage_train.ipynb | 22 +++---- tutorials/simplicial/dist2cycle_train.ipynb | 7 +-- tutorials/simplicial/hsn_train.ipynb | 5 +- tutorials/simplicial/san_train.ipynb | 6 +- tutorials/simplicial/sca_cmps_train.ipynb | 6 +- tutorials/simplicial/sccn_train.ipynb | 5 +- tutorials/simplicial/sccnn_train.ipynb | 12 ++-- tutorials/simplicial/scconv_train.ipynb | 12 +--- tutorials/simplicial/scn2_train.ipynb | 6 +- tutorials/simplicial/scnn_train.ipynb | 17 +++-- tutorials/simplicial/scone_train.ipynb | 15 +++-- 35 files changed, 225 insertions(+), 289 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index bcc2747ff..88d863b0a 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1a72e1968..ca6c2d41c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 057bb87cf..886d0c157 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,7 @@ dependencies=[ [project.optional-dependencies] doc = [ + "hypernetx", "jupyter", "nbsphinx", "nbsphinx_link", @@ -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", @@ -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__"} @@ -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" ] diff --git a/topomodelx/__init__.py b/topomodelx/__init__.py index 6c352aef8..f8b991c80 100644 --- a/topomodelx/__init__.py +++ b/topomodelx/__init__.py @@ -1,5 +1,5 @@ __version__ = "0.0.1" +from .base import * from .nn import * from .utils import * -from .base import * diff --git a/topomodelx/nn/__init__.py b/topomodelx/nn/__init__.py index cfe464523..b7cc10fec 100644 --- a/topomodelx/nn/__init__.py +++ b/topomodelx/nn/__init__.py @@ -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", diff --git a/topomodelx/nn/cell/can_layer.py b/topomodelx/nn/cell/can_layer.py index 41d869e2e..edbc73b3e 100644 --- a/topomodelx/nn/cell/can_layer.py +++ b/topomodelx/nn/cell/can_layer.py @@ -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 @@ -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))) @@ -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 [ @@ -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 @@ -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 diff --git a/topomodelx/nn/combinatorial/hmc_layer.py b/topomodelx/nn/combinatorial/hmc_layer.py index ea277deaf..38daaae11 100644 --- a/topomodelx/nn/combinatorial/hmc_layer.py +++ b/topomodelx/nn/combinatorial/hmc_layer.py @@ -809,7 +809,7 @@ def __init__( update_func_aggregation=None, initialization="xavier_uniform", ): - super(HMCLayer, self).__init__() + super().__init__() super().__init__() assert ( diff --git a/topomodelx/nn/hypergraph/allset_layer.py b/topomodelx/nn/hypergraph/allset_layer.py index 0c9ed84ca..655a7d550 100644 --- a/topomodelx/nn/hypergraph/allset_layer.py +++ b/topomodelx/nn/hypergraph/allset_layer.py @@ -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: diff --git a/topomodelx/nn/hypergraph/unigcn_layer.py b/topomodelx/nn/hypergraph/unigcn_layer.py index 2fe963fee..84a402ee5 100644 --- a/topomodelx/nn/hypergraph/unigcn_layer.py +++ b/topomodelx/nn/hypergraph/unigcn_layer.py @@ -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, diff --git a/topomodelx/nn/hypergraph/unisage_layer.py b/topomodelx/nn/hypergraph/unisage_layer.py index b1264860c..989e783f1 100644 --- a/topomodelx/nn/hypergraph/unisage_layer.py +++ b/topomodelx/nn/hypergraph/unisage_layer.py @@ -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, ) diff --git a/tutorials/cell/can_train.ipynb b/tutorials/cell/can_train.ipynb index b7427f561..900fbd40d 100644 --- a/tutorials/cell/can_train.ipynb +++ b/tutorials/cell/can_train.ipynb @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/tutorials/cell/ccxn_train.ipynb b/tutorials/cell/ccxn_train.ipynb index 01844dccc..8ac5d2add 100644 --- a/tutorials/cell/ccxn_train.ipynb +++ b/tutorials/cell/ccxn_train.ipynb @@ -57,13 +57,10 @@ }, "outputs": [], "source": [ - "import torch\n", - "\n", "import numpy as np\n", - "from sklearn.model_selection import train_test_split\n", - "\n", "import toponetx.datasets as datasets\n", - "\n", + "import torch\n", + "from sklearn.model_selection import train_test_split\n", "\n", "from topomodelx.nn.cell.ccxn import CCXN\n", "from topomodelx.utils.sparse import from_sparse\n", @@ -377,9 +374,10 @@ " torch.tensor(x_1).float().to(device),\n", " torch.tensor(y).float().to(device),\n", " )\n", - " incidence_2_t, adjacency_0 = incidence_2_t.float().to(\n", - " device\n", - " ), adjacency_0.float().to(device)\n", + " incidence_2_t, adjacency_0 = (\n", + " incidence_2_t.float().to(device),\n", + " adjacency_0.float().to(device),\n", + " )\n", " opt.zero_grad()\n", " y_hat = model(x_0, x_1, adjacency_0, incidence_2_t)\n", " loss = loss_fn(y_hat, y)\n", @@ -398,9 +396,10 @@ " torch.tensor(x_1).float().to(device),\n", " torch.tensor(y).float().to(device),\n", " )\n", - " incidence_2_t, adjacency_0 = incidence_2_t.float().to(\n", - " device\n", - " ), adjacency_0.float().to(device)\n", + " incidence_2_t, adjacency_0 = (\n", + " incidence_2_t.float().to(device),\n", + " adjacency_0.float().to(device),\n", + " )\n", " y_hat = model(x_0, x_1, adjacency_0, incidence_2_t)\n", " test_loss = loss_fn(y_hat, y)\n", " print(\n", @@ -501,9 +500,10 @@ " torch.tensor(x_1).float().to(device),\n", " torch.tensor(y).float().to(device),\n", " )\n", - " incidence_2_t, adjacency_0 = incidence_2_t.float().to(\n", - " device\n", - " ), adjacency_0.float().to(device)\n", + " incidence_2_t, adjacency_0 = (\n", + " incidence_2_t.float().to(device),\n", + " adjacency_0.float().to(device),\n", + " )\n", " opt.zero_grad()\n", " y_hat = model(x_0, x_1, adjacency_0, incidence_2_t)\n", " loss = loss_fn(y_hat, y)\n", @@ -522,9 +522,10 @@ " torch.tensor(x_1).float().to(device),\n", " torch.tensor(y).float().to(device),\n", " )\n", - " incidence_2_t, adjacency_0 = incidence_2_t.float().to(\n", - " device\n", - " ), adjacency_0.float().to(device)\n", + " incidence_2_t, adjacency_0 = (\n", + " incidence_2_t.float().to(device),\n", + " adjacency_0.float().to(device),\n", + " )\n", " y_hat = model(x_0, x_1, adjacency_0, incidence_2_t)\n", " test_loss = loss_fn(y_hat, y)\n", " print(\n", diff --git a/tutorials/cell/cwn_train.ipynb b/tutorials/cell/cwn_train.ipynb index 5a71c8bf9..0e60cf6e2 100644 --- a/tutorials/cell/cwn_train.ipynb +++ b/tutorials/cell/cwn_train.ipynb @@ -54,12 +54,10 @@ }, "outputs": [], "source": [ - "import torch\n", - "import torch.nn.functional as F\n", "import numpy as np\n", - "from sklearn.model_selection import train_test_split\n", - "\n", "import toponetx.datasets as datasets\n", + "import torch\n", + "from sklearn.model_selection import train_test_split\n", "\n", "from topomodelx.nn.cell.cwn import CWN\n", "from topomodelx.utils.sparse import from_sparse\n", diff --git a/tutorials/combinatorial/hmc_train.ipynb b/tutorials/combinatorial/hmc_train.ipynb index 6b27c6853..27726b20b 100644 --- a/tutorials/combinatorial/hmc_train.ipynb +++ b/tutorials/combinatorial/hmc_train.ipynb @@ -132,11 +132,11 @@ }, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", + "import torch\n", "from toponetx.datasets.mesh import shrec_16\n", - "from torch.utils.data import Dataset, DataLoader\n", - "from topomodelx.nn.combinatorial.hmc_layer import HMCLayer\n", + "from torch.utils.data import DataLoader, Dataset\n", + "\n", "from topomodelx.nn.combinatorial.hmc import HMC" ] }, diff --git a/tutorials/hypergraph/allset_train.ipynb b/tutorials/hypergraph/allset_train.ipynb index 715c4ca67..ca9fadd6a 100644 --- a/tutorials/hypergraph/allset_train.ipynb +++ b/tutorials/hypergraph/allset_train.ipynb @@ -55,10 +55,10 @@ "\n", "\"\"\"\n", "\n", - "import torch\n", "import numpy as np\n", - "from torch_geometric.utils import to_undirected\n", + "import torch\n", "import torch_geometric.datasets as geom_datasets\n", + "from torch_geometric.utils import to_undirected\n", "\n", "from topomodelx.nn.hypergraph.allset import AllSet\n", "\n", @@ -235,7 +235,7 @@ "num_single_node_hyperedges = sum(np.array(hyperedge_sizes) == 1)\n", "\n", "# Print the hyperedge statistics.\n", - "print(f\"Hyperedge statistics: \")\n", + "print(\"Hyperedge statistics: \")\n", "print(\"Number of hyperedges without duplicated hyperedges\", len(hyperedges))\n", "print(f\"min = {min_size}, \")\n", "print(f\"max = {max_size}, \")\n", @@ -264,8 +264,8 @@ " for row in neighibourhood:\n", " incidence_1[row, col] = 1\n", "\n", - "assert all(incidence_1.sum(0) > 0) == True, \"Some hyperedges are empty\"\n", - "assert all(incidence_1.sum(1) > 0) == True, \"Some nodes are not in any hyperedges\"\n", + "assert all(incidence_1.sum(0) > 0) is True, \"Some hyperedges are empty\"\n", + "assert all(incidence_1.sum(1) > 0) is True, \"Some nodes are not in any hyperedges\"\n", "incidence_1 = torch.Tensor(incidence_1).to_sparse_coo()" ] }, @@ -326,17 +326,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0, incidence_1):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0, incidence_1)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] @@ -397,8 +394,10 @@ "# Categorial cross-entropy loss\n", "loss_fn = torch.nn.CrossEntropyLoss()\n", "\n", + "\n", "# Accuracy\n", - "acc_fn = lambda y, y_hat: (y == y_hat).float().mean()" + "def acc_fn(y, y_hat):\n", + " return (y == y_hat).float().mean()" ] }, { diff --git a/tutorials/hypergraph/allset_transformer_train.ipynb b/tutorials/hypergraph/allset_transformer_train.ipynb index a1a2cc2eb..95e10e5b4 100644 --- a/tutorials/hypergraph/allset_transformer_train.ipynb +++ b/tutorials/hypergraph/allset_transformer_train.ipynb @@ -89,10 +89,10 @@ "\n", "\"\"\"\n", "\n", - "import torch\n", "import numpy as np\n", - "from torch_geometric.utils import to_undirected\n", + "import torch\n", "import torch_geometric.datasets as geom_datasets\n", + "from torch_geometric.utils import to_undirected\n", "\n", "from topomodelx.nn.hypergraph.allset_transformer import AllSetTransformer\n", "\n", @@ -264,8 +264,8 @@ " for row in neighibourhood:\n", " incidence_1[row, col] = 1\n", "\n", - "assert all(incidence_1.sum(0) > 0) == True, \"Some hyperedges are empty\"\n", - "assert all(incidence_1.sum(1) > 0) == True, \"Some nodes are not in any hyperedges\"\n", + "assert all(incidence_1.sum(0) > 0) is True, \"Some hyperedges are empty\"\n", + "assert all(incidence_1.sum(1) > 0) is True, \"Some nodes are not in any hyperedges\"\n", "incidence_1 = torch.Tensor(incidence_1).to_sparse_coo()" ] }, @@ -326,17 +326,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0, incidence_1):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0, incidence_1)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] @@ -399,8 +396,10 @@ "# Categorial cross-entropy loss\n", "loss_fn = torch.nn.CrossEntropyLoss()\n", "\n", + "\n", "# Accuracy\n", - "acc_fn = lambda y, y_hat: (y == y_hat).float().mean()" + "def acc_fn(y, y_hat):\n", + " return (y == y_hat).float().mean()" ] }, { diff --git a/tutorials/hypergraph/dhgcn_train.ipynb b/tutorials/hypergraph/dhgcn_train.ipynb index 040b08bd7..7728103ee 100644 --- a/tutorials/hypergraph/dhgcn_train.ipynb +++ b/tutorials/hypergraph/dhgcn_train.ipynb @@ -21,11 +21,11 @@ }, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", + "import toponetx.datasets as datasets\n", + "import torch\n", "from sklearn.model_selection import train_test_split\n", "\n", - "import toponetx.datasets as datasets\n", "from topomodelx.nn.hypergraph.dhgcn import DHGCN\n", "from topomodelx.utils.sparse import from_sparse" ] @@ -283,17 +283,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] diff --git a/tutorials/hypergraph/hmpnn_train.ipynb b/tutorials/hypergraph/hmpnn_train.ipynb index e7d54785a..6c4d13314 100644 --- a/tutorials/hypergraph/hmpnn_train.ipynb +++ b/tutorials/hypergraph/hmpnn_train.ipynb @@ -24,10 +24,10 @@ }, "outputs": [], "source": [ + "import numpy as np\n", "import torch\n", "import torch_geometric.datasets as geom_datasets\n", "from sklearn.metrics import accuracy_score\n", - "import numpy as np\n", "\n", "from topomodelx.nn.hypergraph.hmpnn import HMPNN\n", "\n", @@ -166,17 +166,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0, x_1, incidence_1):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0, x_1, incidence_1)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] diff --git a/tutorials/hypergraph/hnhn_train.ipynb b/tutorials/hypergraph/hnhn_train.ipynb index 525ad1db1..9902c1140 100644 --- a/tutorials/hypergraph/hnhn_train.ipynb +++ b/tutorials/hypergraph/hnhn_train.ipynb @@ -54,10 +54,10 @@ "\n", "\"\"\"\n", "\n", - "import torch\n", "import numpy as np\n", - "from torch_geometric.utils import to_undirected\n", + "import torch\n", "import torch_geometric.datasets as geom_datasets\n", + "from torch_geometric.utils import to_undirected\n", "\n", "from topomodelx.nn.hypergraph.hnhn import HNHN\n", "\n", @@ -224,7 +224,7 @@ "num_single_node_hyperedges = sum(np.array(hyperedge_sizes) == 1)\n", "\n", "# Print the hyperedge statistics.\n", - "print(f\"Hyperedge statistics: \")\n", + "print(\"Hyperedge statistics: \")\n", "print(\"Number of hyperedges without duplicated hyperedges\", len(hyperedges))\n", "print(f\"min = {min_size}, \")\n", "print(f\"max = {max_size}, \")\n", @@ -253,8 +253,8 @@ " for row in neighibourhood:\n", " incidence_1[row, col] = 1\n", "\n", - "assert all(incidence_1.sum(0) > 0) == True, \"Some hyperedges are empty\"\n", - "assert all(incidence_1.sum(1) > 0) == True, \"Some nodes are not in any hyperedges\"\n", + "assert all(incidence_1.sum(0) > 0) is True, \"Some hyperedges are empty\"\n", + "assert all(incidence_1.sum(1) > 0) is True, \"Some nodes are not in any hyperedges\"\n", "incidence_1 = torch.Tensor(incidence_1).to_sparse_coo()" ] }, @@ -315,17 +315,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] @@ -386,8 +383,10 @@ "# Categorial cross-entropy loss\n", "loss_fn = torch.nn.CrossEntropyLoss()\n", "\n", + "\n", "# Accuracy\n", - "acc_fn = lambda y, y_hat: (y == y_hat).float().mean()" + "def acc_fn(y, y_hat):\n", + " return (y == y_hat).float().mean()" ] }, { diff --git a/tutorials/hypergraph/hypergat_train.ipynb b/tutorials/hypergraph/hypergat_train.ipynb index 09d0a240e..3c06826ad 100644 --- a/tutorials/hypergraph/hypergat_train.ipynb +++ b/tutorials/hypergraph/hypergat_train.ipynb @@ -54,9 +54,9 @@ "metadata": {}, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", "import toponetx.datasets as datasets\n", + "import torch\n", "from sklearn.model_selection import train_test_split\n", "\n", "from topomodelx.nn.hypergraph.hypergat import HyperGAT\n", @@ -253,17 +253,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0, incidence_1):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0, incidence_1)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] diff --git a/tutorials/hypergraph/hypersage_train.ipynb b/tutorials/hypergraph/hypersage_train.ipynb index 0206b6c30..d87d91490 100644 --- a/tutorials/hypergraph/hypersage_train.ipynb +++ b/tutorials/hypergraph/hypersage_train.ipynb @@ -57,10 +57,10 @@ "\n", "\"\"\"\n", "\n", - "import torch\n", "import numpy as np\n", - "from torch_geometric.utils import to_undirected\n", + "import torch\n", "import torch_geometric.datasets as geom_datasets\n", + "from torch_geometric.utils import to_undirected\n", "\n", "from topomodelx.nn.hypergraph.hypersage import HyperSAGE\n", "\n", @@ -227,7 +227,7 @@ "num_single_node_hyperedges = sum(np.array(hyperedge_sizes) == 1)\n", "\n", "# Print the hyperedge statistics.\n", - "print(f\"Hyperedge statistics: \")\n", + "print(\"Hyperedge statistics: \")\n", "print(\"Number of hyperedges without duplicated hyperedges\", len(hyperedges))\n", "print(f\"min = {min_size}, \")\n", "print(f\"max = {max_size}, \")\n", @@ -256,8 +256,8 @@ " for row in neighibourhood:\n", " incidence_1[row, col] = 1\n", "\n", - "assert all(incidence_1.sum(0) > 0) == True, \"Some hyperedges are empty\"\n", - "assert all(incidence_1.sum(1) > 0) == True, \"Some nodes are not in any hyperedges\"\n", + "assert all(incidence_1.sum(0) > 0) is True, \"Some hyperedges are empty\"\n", + "assert all(incidence_1.sum(1) > 0) is True, \"Some nodes are not in any hyperedges\"\n", "incidence_1 = torch.Tensor(incidence_1).to_sparse_coo()" ] }, @@ -318,17 +318,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0, incidence_1):\n", " # Base model\n", " x_0 = self.base_model(x_0, incidence_1)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] @@ -389,8 +386,10 @@ "# Categorial cross-entropy loss\n", "loss_fn = torch.nn.CrossEntropyLoss()\n", "\n", + "\n", "# Accuracy\n", - "acc_fn = lambda y, y_hat: (y == y_hat).float().mean()" + "def acc_fn(y, y_hat):\n", + " return (y == y_hat).float().mean()" ] }, { diff --git a/tutorials/hypergraph/unigcn_train.ipynb b/tutorials/hypergraph/unigcn_train.ipynb index d67c2b05e..ecb4a4d32 100644 --- a/tutorials/hypergraph/unigcn_train.ipynb +++ b/tutorials/hypergraph/unigcn_train.ipynb @@ -7,15 +7,15 @@ "metadata": {}, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", + "import torch\n", "from sklearn.model_selection import train_test_split\n", + "from toponetx.classes.simplicial_complex import SimplicialComplex\n", "from torch_geometric.datasets import TUDataset\n", "from torch_geometric.utils.convert import to_networkx\n", - "from toponetx.classes.simplicial_complex import SimplicialComplex\n", - "from topomodelx.utils.sparse import from_sparse\n", - "from topomodelx.nn.hypergraph.unigcn import UniGCN\n", "\n", + "from topomodelx.nn.hypergraph.unigcn import UniGCN\n", + "from topomodelx.utils.sparse import from_sparse\n", "\n", "torch.manual_seed(0)\n", "np.random.seed(0)" @@ -150,17 +150,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0, incidence_1):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0, incidence_1)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] diff --git a/tutorials/hypergraph/unigcnii_train.ipynb b/tutorials/hypergraph/unigcnii_train.ipynb index 9156a42b0..0261b0508 100644 --- a/tutorials/hypergraph/unigcnii_train.ipynb +++ b/tutorials/hypergraph/unigcnii_train.ipynb @@ -20,13 +20,10 @@ "metadata": {}, "outputs": [], "source": [ - "import torch\n", - "\n", "import numpy as np\n", - "from torch_geometric.utils import to_undirected\n", + "import torch\n", "import torch_geometric.datasets as geom_datasets\n", - "from sklearn.metrics import accuracy_score\n", - "\n", + "from torch_geometric.utils import to_undirected\n", "\n", "from topomodelx.nn.hypergraph.unigcnii import UniGCNII\n", "\n", @@ -182,7 +179,7 @@ "num_single_node_hyperedges = sum(np.array(hyperedge_sizes) == 1)\n", "\n", "# Print the hyperedge statistics.\n", - "print(f\"Hyperedge statistics: \")\n", + "print(\"Hyperedge statistics: \")\n", "print(\"Number of hyperedges without duplicated hyperedges\", len(hyperedges))\n", "print(f\"min = {min_size}, \")\n", "print(f\"max = {max_size}, \")\n", @@ -204,8 +201,8 @@ " for row in neighibourhood:\n", " incidence_1[row, col] = 1\n", "\n", - "assert all(incidence_1.sum(0) > 0) == True, \"Some hyperedges are empty\"\n", - "assert all(incidence_1.sum(1) > 0) == True, \"Some nodes are not in any hyperedges\"\n", + "assert all(incidence_1.sum(0) > 0) is True, \"Some hyperedges are empty\"\n", + "assert all(incidence_1.sum(1) > 0) is True, \"Some nodes are not in any hyperedges\"\n", "incidence_1 = torch.Tensor(incidence_1).to_sparse_coo()" ] }, @@ -265,17 +262,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0, incidence_1):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0, incidence_1)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] @@ -360,8 +354,10 @@ "# Categorial cross-entropy loss\n", "loss_fn = torch.nn.CrossEntropyLoss()\n", "\n", + "\n", "# Accuracy\n", - "acc_fn = lambda y, y_hat: (y == y_hat).float().mean()" + "def acc_fn(y, y_hat):\n", + " return (y == y_hat).float().mean()" ] }, { diff --git a/tutorials/hypergraph/unigin_train.ipynb b/tutorials/hypergraph/unigin_train.ipynb index c5f60bf38..41e011f5c 100644 --- a/tutorials/hypergraph/unigin_train.ipynb +++ b/tutorials/hypergraph/unigin_train.ipynb @@ -6,12 +6,13 @@ "metadata": {}, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", + "import torch\n", "from sklearn.model_selection import train_test_split\n", + "from toponetx.classes.simplicial_complex import SimplicialComplex\n", "from torch_geometric.datasets import TUDataset\n", "from torch_geometric.utils.convert import to_networkx\n", - "from toponetx.classes.simplicial_complex import SimplicialComplex\n", + "\n", "from topomodelx.nn.hypergraph.unigin import UniGIN\n", "from topomodelx.utils.sparse import from_sparse\n", "\n", @@ -143,17 +144,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0, incidence_1):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0, incidence_1)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] diff --git a/tutorials/hypergraph/unisage_train.ipynb b/tutorials/hypergraph/unisage_train.ipynb index 8b69ce6d0..e6ed19d6b 100644 --- a/tutorials/hypergraph/unisage_train.ipynb +++ b/tutorials/hypergraph/unisage_train.ipynb @@ -7,10 +7,10 @@ "metadata": {}, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", - "from torch_geometric.utils import to_undirected\n", + "import torch\n", "import torch_geometric.datasets as geom_datasets\n", + "from torch_geometric.utils import to_undirected\n", "\n", "from topomodelx.nn.hypergraph.unisage import UniSAGE\n", "\n", @@ -167,7 +167,7 @@ "num_single_node_hyperedges = sum(np.array(hyperedge_sizes) == 1)\n", "\n", "# Print the hyperedge statistics.\n", - "print(f\"Hyperedge statistics: \")\n", + "print(\"Hyperedge statistics: \")\n", "print(\"Number of hyperedges without duplicated hyperedges\", len(hyperedges))\n", "print(f\"min = {min_size}, \")\n", "print(f\"max = {max_size}, \")\n", @@ -198,8 +198,8 @@ " for row in neighibourhood:\n", " incidence_1[row, col] = 1\n", "\n", - "assert all(incidence_1.sum(0) > 0) == True, \"Some hyperedges are empty\"\n", - "assert all(incidence_1.sum(1) > 0) == True, \"Some nodes are not in any hyperedges\"\n", + "assert all(incidence_1.sum(0) > 0) is True, \"Some hyperedges are empty\"\n", + "assert all(incidence_1.sum(1) > 0) is True, \"Some nodes are not in any hyperedges\"\n", "incidence_1 = torch.Tensor(incidence_1).to_sparse_coo()" ] }, @@ -256,17 +256,14 @@ "\n", " # Readout\n", " self.linear = torch.nn.Linear(hidden_channels, out_channels)\n", - " self.out_pool = True if task_level == \"graph\" else False\n", + " self.out_pool = task_level == \"graph\"\n", "\n", " def forward(self, x_0, incidence_1):\n", " # Base model\n", " x_0, x_1 = self.base_model(x_0, incidence_1)\n", "\n", " # Pool over all nodes in the hypergraph\n", - " if self.out_pool is True:\n", - " x = torch.max(x_0, dim=0)[0]\n", - " else:\n", - " x = x_0\n", + " x = torch.max(x_0, dim=0)[0] if self.out_pool is True else x_0\n", "\n", " return self.linear(x)" ] @@ -329,8 +326,10 @@ "# Categorial cross-entropy loss\n", "loss_fn = torch.nn.CrossEntropyLoss()\n", "\n", + "\n", "# Accuracy\n", - "acc_fn = lambda y, y_hat: (y == y_hat).float().mean()" + "def acc_fn(y, y_hat):\n", + " return (y == y_hat).float().mean()" ] }, { @@ -369,6 +368,7 @@ }, { "cell_type": "markdown", + "id": "7fb27b941602401d91542211134fc71a", "metadata": {}, "source": [ "**Note: The number of epochs below have been kept low to facilitate debugging and testing. Real use cases should likely require more epochs.**" diff --git a/tutorials/simplicial/dist2cycle_train.ipynb b/tutorials/simplicial/dist2cycle_train.ipynb index 7c95389c6..80e29e563 100644 --- a/tutorials/simplicial/dist2cycle_train.ipynb +++ b/tutorials/simplicial/dist2cycle_train.ipynb @@ -32,14 +32,13 @@ "metadata": {}, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", - "\n", + "import numpy.linalg as npla\n", "import toponetx.datasets.graph as graph\n", + "import torch\n", "\n", "from topomodelx.nn.simplicial.dist2cycle import Dist2Cycle\n", "from topomodelx.utils.sparse import from_sparse\n", - "import numpy.linalg as npla\n", "\n", "%load_ext autoreload\n", "%autoreload 2" @@ -549,7 +548,7 @@ "source": [ "class Network(torch.nn.Module):\n", " def __init__(self, channels, out_channels, n_layers=2):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = Dist2Cycle(\n", " channels=channels,\n", " n_layers=n_layers,\n", diff --git a/tutorials/simplicial/hsn_train.ipynb b/tutorials/simplicial/hsn_train.ipynb index 3e1e3312e..65f0117e3 100644 --- a/tutorials/simplicial/hsn_train.ipynb +++ b/tutorials/simplicial/hsn_train.ipynb @@ -37,10 +37,9 @@ "metadata": {}, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", - "\n", "import toponetx.datasets.graph as graph\n", + "import torch\n", "\n", "from topomodelx.nn.simplicial.hsn import HSN\n", "from topomodelx.utils.sparse import from_sparse\n", @@ -306,7 +305,7 @@ "source": [ "class Network(torch.nn.Module):\n", " def __init__(self, channels, out_channels, n_layers=2):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = HSN(\n", " channels=channels,\n", " n_layers=n_layers,\n", diff --git a/tutorials/simplicial/san_train.ipynb b/tutorials/simplicial/san_train.ipynb index 3388a61e7..efd2cb5a6 100644 --- a/tutorials/simplicial/san_train.ipynb +++ b/tutorials/simplicial/san_train.ipynb @@ -87,10 +87,8 @@ ], "source": [ "import numpy as np\n", - "import torch\n", - "from torch.nn.parameter import Parameter\n", "import toponetx.datasets.graph as graph\n", - "from torch_geometric.utils.convert import to_networkx\n", + "import torch\n", "\n", "from topomodelx.nn.simplicial.san import SAN\n", "from topomodelx.utils.sparse import from_sparse\n", @@ -426,7 +424,7 @@ "source": [ "class Network(torch.nn.Module):\n", " def __init__(self, in_channels, hidden_channels, out_channels, n_layers=1):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SAN(\n", " in_channels=in_channels,\n", " hidden_channels=hidden_channels,\n", diff --git a/tutorials/simplicial/sca_cmps_train.ipynb b/tutorials/simplicial/sca_cmps_train.ipynb index b94ca6148..c04e60558 100644 --- a/tutorials/simplicial/sca_cmps_train.ipynb +++ b/tutorials/simplicial/sca_cmps_train.ipynb @@ -38,10 +38,8 @@ ], "source": [ "import numpy as np\n", - "import torch\n", - "from torch.nn.parameter import Parameter\n", "import toponetx.datasets.graph as graph\n", - "from torch_geometric.utils.convert import to_networkx\n", + "import torch\n", "\n", "from topomodelx.nn.simplicial.sca_cmps import SCACMPS\n", "from topomodelx.utils.sparse import from_sparse\n", @@ -299,7 +297,7 @@ "source": [ "class Network(torch.nn.Module):\n", " def __init__(self, in_channels_all, out_channels, complex_dim, n_layers=1):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SCACMPS(\n", " in_channels_all=in_channels_all,\n", " complex_dim=complex_dim,\n", diff --git a/tutorials/simplicial/sccn_train.ipynb b/tutorials/simplicial/sccn_train.ipynb index 28ba9bcff..4d52a7ee0 100644 --- a/tutorials/simplicial/sccn_train.ipynb +++ b/tutorials/simplicial/sccn_train.ipynb @@ -39,10 +39,9 @@ "metadata": {}, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", - "\n", "import toponetx.datasets.graph as graph\n", + "import torch\n", "\n", "from topomodelx.nn.simplicial.sccn import SCCN\n", "from topomodelx.utils.sparse import from_sparse\n", @@ -420,7 +419,7 @@ " def __init__(\n", " self, channels, out_channels, max_rank, n_layers=2, update_func=\"sigmoid\"\n", " ):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SCCN(\n", " channels=channels,\n", " max_rank=max_rank,\n", diff --git a/tutorials/simplicial/sccnn_train.ipynb b/tutorials/simplicial/sccnn_train.ipynb index ce8fc3a21..2d582a46d 100644 --- a/tutorials/simplicial/sccnn_train.ipynb +++ b/tutorials/simplicial/sccnn_train.ipynb @@ -72,11 +72,11 @@ } ], "source": [ - "import torch\n", "import numpy as np\n", - "from sklearn.model_selection import train_test_split\n", "import toponetx.datasets as datasets\n", - "from topomodelx.nn.simplicial.sccnn import SCCNN\n", + "import torch\n", + "from sklearn.model_selection import train_test_split\n", + "\n", "from topomodelx.utils.sparse import from_sparse\n", "\n", "%load_ext autoreload\n", @@ -217,7 +217,7 @@ " max_rank,\n", " n_layers=2,\n", " ):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SCCNN(\n", " in_channels_all=in_channels_all,\n", " hidden_channels_all=hidden_channels_all,\n", @@ -627,7 +627,7 @@ " which_feat = \"face_feat\"\n", " else:\n", " raise ValueError(\n", - " f\"input dimension must be 0, 1 or 2, because features are supported on nodes, edges and faces\"\n", + " \"input dimension must be 0, 1 or 2, because features are supported on nodes, edges and faces\"\n", " )\n", "\n", " x = []\n", @@ -752,7 +752,7 @@ " update_func=None,\n", " n_layers=2,\n", " ):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SCCNN(\n", " in_channels_all=in_channels_all,\n", " hidden_channels_all=hidden_channels_all,\n", diff --git a/tutorials/simplicial/scconv_train.ipynb b/tutorials/simplicial/scconv_train.ipynb index 97a34b179..e319a7b5b 100644 --- a/tutorials/simplicial/scconv_train.ipynb +++ b/tutorials/simplicial/scconv_train.ipynb @@ -74,15 +74,9 @@ ], "source": [ "import numpy as np\n", - "import torch\n", - "from torch.nn.parameter import Parameter\n", "import toponetx.datasets.graph as graph\n", - "from torch_geometric.utils.convert import to_networkx\n", - "\n", - "from scipy.sparse import coo_matrix\n", - "from scipy.sparse import diags\n", - "\n", - "from topomodelx.base.aggregation import Aggregation\n", + "import torch\n", + "from scipy.sparse import coo_matrix, diags\n", "\n", "from topomodelx.nn.simplicial.scconv import SCConv\n", "from topomodelx.utils.sparse import from_sparse\n", @@ -387,7 +381,7 @@ "source": [ "class Network(torch.nn.Module):\n", " def __init__(self, in_channels, out_channels, n_layers=1):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SCConv(\n", " node_channels=in_channels,\n", " n_layers=n_layers,\n", diff --git a/tutorials/simplicial/scn2_train.ipynb b/tutorials/simplicial/scn2_train.ipynb index b2d297c68..9d90ecd95 100644 --- a/tutorials/simplicial/scn2_train.ipynb +++ b/tutorials/simplicial/scn2_train.ipynb @@ -34,11 +34,11 @@ } ], "source": [ - "import torch\n", "import numpy as np\n", "import toponetx.datasets as datasets\n", - "\n", + "import torch\n", "from sklearn.model_selection import train_test_split\n", + "\n", "from topomodelx.nn.simplicial.scn2 import SCN2\n", "from topomodelx.utils.sparse import from_sparse\n", "\n", @@ -194,7 +194,7 @@ " def __init__(\n", " self, in_channels_0, in_channels_1, in_channels_2, out_channels, n_layers=2\n", " ):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SCN2(\n", " in_channels_0=in_channels_0,\n", " in_channels_1=in_channels_1,\n", diff --git a/tutorials/simplicial/scnn_train.ipynb b/tutorials/simplicial/scnn_train.ipynb index 0518acdb5..1ce52eaf2 100644 --- a/tutorials/simplicial/scnn_train.ipynb +++ b/tutorials/simplicial/scnn_train.ipynb @@ -62,11 +62,10 @@ "metadata": {}, "outputs": [], "source": [ - "import torch\n", "import numpy as np\n", - "from sklearn.model_selection import train_test_split\n", - "\n", "import toponetx.datasets as datasets\n", + "import torch\n", + "from sklearn.model_selection import train_test_split\n", "\n", "from topomodelx.nn.simplicial.scnn import SCNN\n", "from topomodelx.utils.sparse import from_sparse\n", @@ -221,7 +220,7 @@ " x = x_2s\n", " in_channels = in_channels_2\n", "else:\n", - " raise ValueError(f\"Rank must be not larger than 2 on this dataset\")" + " raise ValueError(\"Rank must be not larger than 2 on this dataset\")" ] }, { @@ -240,7 +239,7 @@ " conv_order_up,\n", " n_layers=2,\n", " ):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SCNN(\n", " in_channels=in_channels,\n", " hidden_channels=hidden_channels,\n", @@ -552,7 +551,7 @@ " which_feat = \"face_feat\"\n", " else:\n", " raise ValueError(\n", - " f\"input dimension must be 0, 1 or 2, because features are supported on nodes, edges and faces\"\n", + " \"input dimension must be 0, 1 or 2, because features are supported on nodes, edges and faces\"\n", " )\n", "\n", " x = []\n", @@ -648,7 +647,7 @@ " conv_order_up,\n", " n_layers=2,\n", " ):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SCNN(\n", " in_channels=in_channels,\n", " hidden_channels=hidden_channels,\n", @@ -686,7 +685,7 @@ ], "source": [ "\"\"\"\n", - "Select the simplex order, i.e., on which level of simplices the learning will be performed \n", + "Select the simplex order, i.e., on which level of simplices the learning will be performed\n", "\"\"\"\n", "rank = 1 # simplex level\n", "conv_order_down = 2\n", @@ -704,7 +703,7 @@ " laplacian_down = laplacian_down_2\n", " laplacian_up = laplacian_up_2\n", "else:\n", - " raise ValueError(f\"Rank must be not larger than 2 on this dataset\")\n", + " raise ValueError(\"Rank must be not larger than 2 on this dataset\")\n", "\n", "hidden_channels = 16\n", "out_channels = 2 # num classes\n", diff --git a/tutorials/simplicial/scone_train.ipynb b/tutorials/simplicial/scone_train.ipynb index 3005eac9a..bbdabdfef 100644 --- a/tutorials/simplicial/scone_train.ipynb +++ b/tutorials/simplicial/scone_train.ipynb @@ -51,18 +51,17 @@ "source": [ "import random\n", "from itertools import product\n", - "import numpy as np\n", + "\n", "import matplotlib.pyplot as plt\n", - "import toponetx as tnx\n", - "from toponetx.classes.simplicial_complex import SimplicialComplex\n", "import networkx as nx\n", + "import numpy as np\n", "import torch\n", "import torch.nn as nn\n", - "from torch.utils.data.dataset import Dataset\n", - "from torch.utils.data import random_split, DataLoader\n", - "from torch.optim import Adam\n", - "from tqdm.notebook import tqdm\n", "from scipy.spatial import Delaunay, distance\n", + "from toponetx.classes.simplicial_complex import SimplicialComplex\n", + "from torch.optim import Adam\n", + "from torch.utils.data import DataLoader, random_split\n", + "from torch.utils.data.dataset import Dataset\n", "\n", "from topomodelx.nn.simplicial.scone import SCoNe\n", "\n", @@ -494,7 +493,7 @@ "source": [ "class Network(torch.nn.Module):\n", " def __init__(self, in_channels, hidden_channels, out_channels, n_layers):\n", - " super(Network, self).__init__()\n", + " super().__init__()\n", " self.base_model = SCoNe(\n", " in_channels=in_channels,\n", " hidden_channels=hidden_channels,\n",