Skip to content

Commit

Permalink
Activate additional ruff rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ffl096 committed Oct 21, 2024
1 parent 74deed8 commit ae72a6a
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 44 deletions.
12 changes: 6 additions & 6 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Configure our testing suite."""

import networkx
import numpy
import networkx as nx
import numpy as np
import pytest

import toponetx
import toponetx as tnx


@pytest.fixture(autouse=True)
Expand All @@ -26,6 +26,6 @@ def doctest_default_imports(doctest_namespace):
doctest_namespace : dict
The namespace of the doctest.
"""
doctest_namespace["np"] = numpy
doctest_namespace["nx"] = networkx
doctest_namespace["tnx"] = toponetx
doctest_namespace["np"] = np
doctest_namespace["nx"] = nx
doctest_namespace["tnx"] = tnx
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# -- Project information -----------------------------------------------------

project = "TopoNetX"
copyright = "2022-2023, PyT-Team, Inc."
copyright = "2022-2023, PyT-Team, Inc." # noqa: A001
author = "PyT-Team Authors"
language = "en"

Expand Down Expand Up @@ -66,7 +66,7 @@
"url": "https://github.com/pyt-team/TopoNetX",
"icon": "fa-brands fa-github",
"type": "fontawesome",
}
},
],
"use_edit_page_button": True,
}
Expand Down
30 changes: 27 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ all = [
documentation="https://pyt-team.github.io/toponetx/"
source="https://github.com/pyt-team/TopoNetX/"

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

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

Expand All @@ -92,23 +89,50 @@ select = [
"E", # code style
"W", # warnings
"I", # import order
"D", # pydocstyle rules
"UP", # pyupgrade rules
"YTT", # flake8-2020 rules
"S", # bandit rules
"BLE", # blind except
"B", # bugbear rules
"A", # builtin shadowing
"COM", # comma rules
"C4", # comprehensions
"DTZ", # datetime rules
"T10", # debugger calls
"FA", # future annotations
"ISC", # implicit str concatenation
"ICN", # import conventions
"LOG", # logging rules
"G", # logging format rules
"PIE", # pie rules
"Q", # quote rules
"RSE", # raise rules
"RET", # return rules
"SLOT", # slot rules
"SIM", # code simplifications
"TID", # tidy imports
"TCH", # type checking rules
"PTH", # use pathlib
"PD", # pandas rules
"PLC", # pylint conventions
"PLE", # pylint errors
"FLY", # flynt
"NPY", # numpy rules
"PERF", # performance rules
"FURB", # refurb
"RUF", # miscellaneous rules
]
ignore = [
"E501", # line too long
"COM812", # trailing commas; conflict with `ruff format`
"ISC001", # implicitly single-line str concat; conflict with `ruff format`
"PERF203", # allow try-except within loops
]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F403"]
"test/**.py" = ["S101"]

[tool.ruff.lint.pydocstyle]
convention = "numpy"
Expand Down
4 changes: 2 additions & 2 deletions test/classes/test_colored_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ def test_chg_getitem(self):

# invalid inputs should raise `KeyError`s as well
with pytest.raises(KeyError):
_ = CHG[tuple()]
_ = CHG[()]
with pytest.raises(KeyError):
_ = CHG[(tuple(), 0)]
_ = CHG[((), 0)]

def test_add_cell(self):
"""Test adding a cell to a CHG."""
Expand Down
6 changes: 3 additions & 3 deletions test/test_tutorials.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Unit tests for the tutorials."""

import glob
import subprocess
import tempfile
from pathlib import Path

import pytest

Expand All @@ -28,11 +28,11 @@ def _exec_tutorial(path):
tmp_file.name,
path,
]
subprocess.check_call(args)
subprocess.check_call(args) # noqa: S603


TUTORIALS_DIR = "tutorials"
paths = sorted(glob.glob(f"{TUTORIALS_DIR}/*.ipynb"))
paths = sorted(Path(TUTORIALS_DIR).glob("*.ipynb"))


@pytest.mark.parametrize("path", paths)
Expand Down
4 changes: 2 additions & 2 deletions toponetx/algorithms/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def distance(
try:
return nx.shortest_path_length(G, rowdict[source], rowdict[target])
except nx.NetworkXNoPath as exc:
raise TopoNetXNoPath() from exc
raise TopoNetXNoPath from exc


def cell_distance(
Expand Down Expand Up @@ -172,4 +172,4 @@ def cell_distance(
try:
return nx.shortest_path_length(G, cell_dict[source], cell_dict[target])
except nx.NetworkXNoPath as exc:
raise TopoNetXNoPath() from exc
raise TopoNetXNoPath from exc
4 changes: 2 additions & 2 deletions toponetx/classes/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def __init__(self, elements: Collection, regular: bool = True, **kwargs) -> None
for e in self._boundary:
if e[0] in _adjdict:
raise ValueError(
f" Node {e[0]} is repeated multiple times in the input cell."
+ " Input cell violates the cell complex regularity condition."
f"Node {e[0]} is repeated multiple times in the input cell. "
"Input cell violates the cell complex regularity condition."
)
_adjdict[e[0]] = e[1]
else:
Expand Down
6 changes: 3 additions & 3 deletions toponetx/classes/cell_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ def __iter__(self) -> Iterator:
Iterator over nodes.
"""
return chain(
map(lambda x: (x,), self.nodes),
((x,) for x in self.nodes),
self.edges,
map(lambda cell: tuple(cell), self.cells),
(tuple(cell) for cell in self.cells),
)

def __contains__(self, item) -> bool:
Expand Down Expand Up @@ -624,7 +624,7 @@ def cell_neighbors(self, cell, s: int = 1):
list
List of cell neighbors.
"""
raise NotImplementedError()
raise NotImplementedError

def remove_node(self, node: Hashable) -> None:
"""Remove the given node from the cell complex.
Expand Down
2 changes: 1 addition & 1 deletion toponetx/classes/colored_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ def from_trimesh(cls, mesh: trimesh.Trimesh):
>>> CHG = tnx.ColoredHyperGraph.from_trimesh(mesh)
>>> CHG.nodes
"""
raise NotImplementedError()
raise NotImplementedError

def restrict_to_cells(self, cell_set):
"""Construct a Colored Hypergraph using a subset of the cells.
Expand Down
4 changes: 2 additions & 2 deletions toponetx/classes/combinatorial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,13 @@ def _CCC_condition(self, hyperedge_: set, rank: int) -> None:
if rank > e_rank and existing_hyperedge.issuperset(hyperedge_):
raise ValueError(
"a violation of the combinatorial complex condition:"
+ f"the hyperedge {existing_hyperedge} in the complex has rank {e_rank} is larger than {rank}, the rank of the input hyperedge {hyperedge_} "
f"the hyperedge {existing_hyperedge} in the complex has rank {e_rank} is larger than {rank}, the rank of the input hyperedge {hyperedge_} "
)

if rank < e_rank and hyperedge_.issuperset(existing_hyperedge):
raise ValueError(
"violation of the combinatorial complex condition : "
+ f"the hyperedge {existing_hyperedge} in the complex has rank {e_rank} is smaller than {rank}, the rank of the input hyperedge {hyperedge_} "
f"the hyperedge {existing_hyperedge} in the complex has rank {e_rank} is smaller than {rank}, the rank of the input hyperedge {hyperedge_} "
)

def _add_hyperedge(self, hyperedge, rank, **attr):
Expand Down
3 changes: 1 addition & 2 deletions toponetx/classes/simplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ def construct_simplex_tree(elements: Collection) -> frozenset["Simplex"]:
frozenset[Simplex]
The set of faces of the simplex.
"""

faceset = set()
for r in range(len(elements), 0, -1):
for face in combinations(elements, r):
Expand Down Expand Up @@ -165,7 +164,7 @@ def sign(self, face) -> int:
face : Simplex
A face of the simplex.
"""
raise NotImplementedError()
raise NotImplementedError

@property
@deprecated(
Expand Down
8 changes: 5 additions & 3 deletions toponetx/classes/simplicial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,10 @@ def incidence_matrix(
values.append((-1) ** i)
face = frozenset(simplex).difference({left_out})
idx_faces.append(simplex_dict_d_minus_1[tuple(sorted(face))])
assert len(values) == (rank + 1) * len(simplex_dict_d)

if len(values) != (rank + 1) * len(simplex_dict_d):
raise RuntimeError("Error in computing the incidence matrix.")

Check warning on line 882 in toponetx/classes/simplicial_complex.py

View check run for this annotation

Codecov / codecov/patch

toponetx/classes/simplicial_complex.py#L882

Added line #L882 was not covered by tests

boundary = csr_matrix(
(values, (idx_faces, idx_simplices)),
dtype=np.float32,
Expand Down Expand Up @@ -1313,8 +1316,7 @@ def add_elements_from_nx_graph(self, G: nx.Graph) -> None:
G : networkx.Graph
A networkx graph instance.
"""
_simplices = list(G.edges) + list(map(lambda n: [n], G.nodes))

_simplices = [[n] for n in G.nodes] + list(G.edges)
self.add_simplices_from(_simplices)

def restrict_to_simplices(self, cell_set) -> Self:
Expand Down
3 changes: 2 additions & 1 deletion toponetx/datasets/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ def coauthorship() -> SimplicialComplex:

if not dataset_file.exists():
r = requests.get(
"https://github.com/pyt-team/topological-datasets/raw/main/resources/coauthorship.npy"
"https://github.com/pyt-team/topological-datasets/raw/main/resources/coauthorship.npy",
timeout=10,
)
with dataset_file.open("wb") as f:
f.write(r.content)
Expand Down
7 changes: 4 additions & 3 deletions toponetx/datasets/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def stanford_bunny(

if not dataset_file.exists():
r = requests.get(
"https://github.com/pyt-team/topological-datasets/raw/main/resources/bunny.obj"
"https://github.com/pyt-team/topological-datasets/raw/main/resources/bunny.obj",
timeout=10,
)
with dataset_file.open("wb") as f:
f.write(r.content)
Expand Down Expand Up @@ -145,7 +146,7 @@ def shrec_16(size: Literal["full", "small"] = "full"):

if not training.exists() or not testing.exists():
print(f"downloading shrec 16 {size} dataset...\n")
r = requests.get(url)
r = requests.get(url, timeout=10)
with zipfile.ZipFile(BytesIO(r.content)) as zip_ref:
zip_ref.extractall(DIR)
print("done!")
Expand Down Expand Up @@ -211,7 +212,7 @@ def coseg(data: Literal["alien", "vase", "chair"] = "alien"):

if not unziped_file.exists():
print(f"downloading {data} dataset...\n")
r = requests.get(url)
r = requests.get(url, timeout=10)
with zipfile.ZipFile(BytesIO(r.content)) as zip_ref:
zip_ref.extractall(DIR)
print("done!")
Expand Down
6 changes: 3 additions & 3 deletions toponetx/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def load_ppi():
]
proteins = "%0d".join(protein_list)
url = f"https://string-db.org/api/tsv/network?identifiers={proteins}&species=9606"
r = requests.get(url)
r = requests.get(url, timeout=10)

lines = r.text.split("\n")
data = [line.split("\t") for line in lines]
df = pd.DataFrame(data[1:-1], columns=data[0])
protein_df = pd.DataFrame(data[1:-1], columns=data[0])

interactions = df[["preferredName_A", "preferredName_B", "score"]]
interactions = protein_df[["preferredName_A", "preferredName_B", "score"]]

G = nx.Graph(name="Protein Interaction Graph")
interactions = np.array(interactions)
Expand Down
11 changes: 6 additions & 5 deletions toponetx/readwrite/serialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Read and write complexes as pickled objects."""

import pickle
from pathlib import Path

__all__ = ["to_pickle", "load_from_pickle"]

Expand All @@ -12,10 +13,10 @@ def to_pickle(obj, filename: str) -> None:
----------
obj : object
Object to write.
filename : str
filename : Path or str
Filename.
"""
with open(filename, "wb") as f:
with Path(filename).open("wb") as f:

Check warning on line 19 in toponetx/readwrite/serialization.py

View check run for this annotation

Codecov / codecov/patch

toponetx/readwrite/serialization.py#L19

Added line #L19 was not covered by tests
pickle.dump(obj, f)


Expand All @@ -24,13 +25,13 @@ def load_from_pickle(filepath):
Parameters
----------
filepath : str
filepath : Path or str
Filepath.
Returns
-------
object
Object.
"""
with open(filepath, "rb") as f:
return pickle.load(f)
with Path(filepath).open("rb") as f:
return pickle.load(f) # noqa: S301

Check warning on line 37 in toponetx/readwrite/serialization.py

View check run for this annotation

Codecov / codecov/patch

toponetx/readwrite/serialization.py#L36-L37

Added lines #L36 - L37 were not covered by tests
4 changes: 3 additions & 1 deletion toponetx/utils/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def compute_x_laplacian_normalized_matrix(L: csr_matrix, Lx: csr_matrix) -> csr_
This function normalizes the up or down Laplacian matrices by dividing them
by the largest eigenvalue of the Laplacian matrix.
"""
assert L.shape[0] == L.shape[1]
if not L.shape[0] == L.shape[1]:
raise ValueError("Laplacian matrix must be square.")

Check warning on line 69 in toponetx/utils/normalization.py

View check run for this annotation

Codecov / codecov/patch

toponetx/utils/normalization.py#L69

Added line #L69 was not covered by tests

topeig = spl.eigsh(L.asfptype(), k=1, which="LM", return_eigenvectors=False)[0]
return Lx * (1.0 / topeig)

Expand Down

0 comments on commit ae72a6a

Please sign in to comment.