From d19d59175e75b93187784cc8b571af0e5d1188e7 Mon Sep 17 00:00:00 2001 From: Florian Frantzen <2105496+ffl096@users.noreply.github.com> Date: Fri, 5 Jul 2024 13:59:38 +0200 Subject: [PATCH] Minor cleanup (#379) * Optimize `ColoredHyperGraph._remove_hyperedge` * fix typos in docstrings * whitespace fixes * optimize `ColoredHyperGraph._remove_hyperedge` --- test/classes/test_combinatorial_complex.py | 2 +- test/generators/test_classic_cell_complex.py | 1 + test/generators/test_random_cell_complexes.py | 1 + .../test_random_simplicial_complexes.py | 1 + test/readwrite/test_atomlist.py | 1 + test/transform/test_graph_to_cell_complex.py | 1 - toponetx/classes/colored_hypergraph.py | 21 ++--------- toponetx/classes/combinatorial_complex.py | 35 ++----------------- toponetx/classes/simplex.py | 4 ++- toponetx/datasets/__init__.py | 1 + toponetx/generators/__init__.py | 1 + .../generators/random_simplicial_complexes.py | 1 + toponetx/readwrite/__init__.py | 1 + toponetx/readwrite/serialization.py | 1 + toponetx/utils/__init__.py | 3 +- toponetx/utils/normalization.py | 1 + 16 files changed, 21 insertions(+), 55 deletions(-) diff --git a/test/classes/test_combinatorial_complex.py b/test/classes/test_combinatorial_complex.py index 8557776c..197d3d7d 100644 --- a/test/classes/test_combinatorial_complex.py +++ b/test/classes/test_combinatorial_complex.py @@ -347,7 +347,7 @@ def test_dim(self): assert CCC.dim == 3 def test_repr(self): - """Test the represntation function of the CombinatorialComplex object by mentioning a name.""" + """Test the representation function of the CombinatorialComplex object by mentioning a name.""" CCC = CombinatorialComplex() assert repr(CCC) == "CombinatorialComplex()" diff --git a/test/generators/test_classic_cell_complex.py b/test/generators/test_classic_cell_complex.py index 1eae6426..98e47a5e 100644 --- a/test/generators/test_classic_cell_complex.py +++ b/test/generators/test_classic_cell_complex.py @@ -1,4 +1,5 @@ """Test the classic cell complex generators.""" + from toponetx.generators.classic_cell_complexes import ( pyrmaid_complex, single_cell_complex, diff --git a/test/generators/test_random_cell_complexes.py b/test/generators/test_random_cell_complexes.py index dfb3801e..6a99a262 100644 --- a/test/generators/test_random_cell_complexes.py +++ b/test/generators/test_random_cell_complexes.py @@ -1,4 +1,5 @@ """Test the random cell complex generators.""" + from toponetx.generators.random_cell_complexes import np_cell_complex diff --git a/test/generators/test_random_simplicial_complexes.py b/test/generators/test_random_simplicial_complexes.py index 637e49d8..36159712 100644 --- a/test/generators/test_random_simplicial_complexes.py +++ b/test/generators/test_random_simplicial_complexes.py @@ -1,4 +1,5 @@ """Test the random simplicial complex generators.""" + from toponetx.generators.random_simplicial_complexes import ( linial_meshulam_complex, multiparameter_linial_meshulam_complex, diff --git a/test/readwrite/test_atomlist.py b/test/readwrite/test_atomlist.py index c8940df0..65608c2d 100644 --- a/test/readwrite/test_atomlist.py +++ b/test/readwrite/test_atomlist.py @@ -1,4 +1,5 @@ """Tests for the atomlist read/write functions.""" + import pytest from toponetx.classes import CellComplex, PathComplex, SimplicialComplex diff --git a/test/transform/test_graph_to_cell_complex.py b/test/transform/test_graph_to_cell_complex.py index 176893fc..1504e495 100644 --- a/test/transform/test_graph_to_cell_complex.py +++ b/test/transform/test_graph_to_cell_complex.py @@ -1,6 +1,5 @@ """Test graph to cell complex transformation.""" - import networkx as nx from toponetx.transform.graph_to_cell_complex import homology_cycle_cell_complex diff --git a/toponetx/classes/colored_hypergraph.py b/toponetx/classes/colored_hypergraph.py index c695cb46..18119e9f 100644 --- a/toponetx/classes/colored_hypergraph.py +++ b/toponetx/classes/colored_hypergraph.py @@ -611,7 +611,7 @@ def _add_hyperedge(self, hyperedge, rank, key=None, **attr): if rank == 0 and hyperedge_set in self._complex_set.hyperedge_dict[0]: self._complex_set.hyperedge_dict[0][hyperedge_set][0].update(**attr) - def _remove_hyperedge(self, hyperedge): + def _remove_hyperedge(self, hyperedge) -> None: """Remove a hyperedge from the CHG. Parameters @@ -623,24 +623,9 @@ def _remove_hyperedge(self, hyperedge): ------ KeyError If the hyperedge is not present in the complex. - - Returns - ------- - None - None. """ - if hyperedge not in self.cells: - raise KeyError(f"The cell {hyperedge} is not in the complex") - - if isinstance(hyperedge, Hashable) and not isinstance(hyperedge, Iterable): - del self._complex_set.hyperedge_dict[0][hyperedge] - - if isinstance(hyperedge, HyperEdge): - hyperedge_ = hyperedge.elements - else: - hyperedge_ = frozenset(hyperedge) - rank = self._complex_set.get_rank(hyperedge_) - del self._complex_set.hyperedge_dict[rank][hyperedge_] + rank = self._complex_set.get_rank(hyperedge) + del self._complex_set.hyperedge_dict[rank][frozenset(hyperedge)] def _add_node(self, node, **attr) -> None: """Add one node as a hyperedge. diff --git a/toponetx/classes/combinatorial_complex.py b/toponetx/classes/combinatorial_complex.py index 55ef1b2a..12136c1e 100644 --- a/toponetx/classes/combinatorial_complex.py +++ b/toponetx/classes/combinatorial_complex.py @@ -215,7 +215,7 @@ def number_of_nodes(self, node_set=None) -> int: Parameters ---------- - node_set : an interable of Entities, optional + node_set : iterable of Entities, optional If None, then return the number of nodes in the CCC. Returns @@ -254,7 +254,7 @@ def number_of_cells(self, cell_set=None) -> int: Parameters ---------- - cell_set : an interable of HyperEdge, optional + cell_set : iterable of HyperEdge, optional If None, then return the number of cells. Returns @@ -996,37 +996,6 @@ def add_cells_from(self, cells, ranks: Iterable[int] | int | None = None) -> Non for cell in cells: self.add_cell(cell, ranks) - def _remove_hyperedge(self, hyperedge) -> None: - """Remove a hyperedge from the combinatorial complex. - - Parameters - ---------- - hyperedge : Hashable or HyperEdge, Iterable - The hyperedge to be removed. - - Returns - ------- - None - This method does not return any value. It removes the specified hyperedge from the complex in-place. - - Raises - ------ - KeyError - If the hyperedge is not present in the complex. - """ - if hyperedge not in self.cells: - raise KeyError(f"The cell {hyperedge} is not in the complex") - - if isinstance(hyperedge, Hashable) and not isinstance(hyperedge, Iterable): - del self._complex_set.hyperedge_dict[0][hyperedge] - - if isinstance(hyperedge, HyperEdge): - hyperedge_ = hyperedge.elements - else: - hyperedge_ = frozenset(hyperedge) - rank = self._complex_set.get_rank(hyperedge_) - del self._complex_set.hyperedge_dict[rank][hyperedge_] - def add_cell(self, cell, rank=None, **attr) -> None: """Add a single cells to combinatorial complex. diff --git a/toponetx/classes/simplex.py b/toponetx/classes/simplex.py index 9d5713e6..0bb5236d 100644 --- a/toponetx/classes/simplex.py +++ b/toponetx/classes/simplex.py @@ -107,7 +107,9 @@ def validate_attributes(attributes: dict) -> None: If the attributes contain the reserved keys `is_maximal` or `membership`. """ if "is_maximal" in attributes or "membership" in attributes: - raise ValueError("Special attributes `is_maximal` and `membership` are reserved.") + raise ValueError( + "Special attributes `is_maximal` and `membership` are reserved." + ) @staticmethod @deprecated("`Simplex.construct_simplex_tree` is deprecated.") diff --git a/toponetx/datasets/__init__.py b/toponetx/datasets/__init__.py index bf083038..c9558ea8 100644 --- a/toponetx/datasets/__init__.py +++ b/toponetx/datasets/__init__.py @@ -1,4 +1,5 @@ """Initializing the datasets module.""" + from .graph import * from .mesh import * from .utils import * diff --git a/toponetx/generators/__init__.py b/toponetx/generators/__init__.py index 17447300..8e4af6bd 100644 --- a/toponetx/generators/__init__.py +++ b/toponetx/generators/__init__.py @@ -1,2 +1,3 @@ """Initialize the generators module.""" + from .random_simplicial_complexes import * diff --git a/toponetx/generators/random_simplicial_complexes.py b/toponetx/generators/random_simplicial_complexes.py index ea596c96..5ecdb261 100644 --- a/toponetx/generators/random_simplicial_complexes.py +++ b/toponetx/generators/random_simplicial_complexes.py @@ -1,4 +1,5 @@ """Generators for random simplicial complexes.""" + import random from collections.abc import Sequence from itertools import combinations diff --git a/toponetx/readwrite/__init__.py b/toponetx/readwrite/__init__.py index c3b1e3d1..309bf78a 100644 --- a/toponetx/readwrite/__init__.py +++ b/toponetx/readwrite/__init__.py @@ -1,3 +1,4 @@ """Module for reading and writing complexes from and to files.""" + from .atomlist import * from .serialization import * diff --git a/toponetx/readwrite/serialization.py b/toponetx/readwrite/serialization.py index 9ce289cd..bb6c9043 100644 --- a/toponetx/readwrite/serialization.py +++ b/toponetx/readwrite/serialization.py @@ -1,4 +1,5 @@ """Read and write complexes as pickled objects.""" + import pickle __all__ = ["to_pickle", "load_from_pickle"] diff --git a/toponetx/utils/__init__.py b/toponetx/utils/__init__.py index a28af9ab..97904830 100644 --- a/toponetx/utils/__init__.py +++ b/toponetx/utils/__init__.py @@ -1,3 +1,4 @@ -"""Intialize the utils module.""" +"""Initialize the utils module.""" + from .normalization import * from .structure import * diff --git a/toponetx/utils/normalization.py b/toponetx/utils/normalization.py index fe37defe..6bd5abeb 100644 --- a/toponetx/utils/normalization.py +++ b/toponetx/utils/normalization.py @@ -1,4 +1,5 @@ """Normalize of Laplacians, (co)adjacency, boundary matrices of complexes.""" + import numpy as np import scipy.sparse.linalg as spl from numpy.linalg import pinv