Skip to content

Commit

Permalink
Minor cleanup (#379)
Browse files Browse the repository at this point in the history
* Optimize `ColoredHyperGraph._remove_hyperedge`

* fix typos in docstrings

* whitespace fixes

* optimize `ColoredHyperGraph._remove_hyperedge`
  • Loading branch information
ffl096 committed Jul 5, 2024
1 parent 04181fa commit d19d591
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 55 deletions.
2 changes: 1 addition & 1 deletion test/classes/test_combinatorial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()"

Expand Down
1 change: 1 addition & 0 deletions test/generators/test_classic_cell_complex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the classic cell complex generators."""

from toponetx.generators.classic_cell_complexes import (
pyrmaid_complex,
single_cell_complex,
Expand Down
1 change: 1 addition & 0 deletions test/generators/test_random_cell_complexes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the random cell complex generators."""

from toponetx.generators.random_cell_complexes import np_cell_complex


Expand Down
1 change: 1 addition & 0 deletions test/generators/test_random_simplicial_complexes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the random simplicial complex generators."""

from toponetx.generators.random_simplicial_complexes import (
linial_meshulam_complex,
multiparameter_linial_meshulam_complex,
Expand Down
1 change: 1 addition & 0 deletions test/readwrite/test_atomlist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for the atomlist read/write functions."""

import pytest

from toponetx.classes import CellComplex, PathComplex, SimplicialComplex
Expand Down
1 change: 0 additions & 1 deletion test/transform/test_graph_to_cell_complex.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 3 additions & 18 deletions toponetx/classes/colored_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
35 changes: 2 additions & 33 deletions toponetx/classes/combinatorial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion toponetx/classes/simplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
1 change: 1 addition & 0 deletions toponetx/datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Initializing the datasets module."""

from .graph import *
from .mesh import *
from .utils import *
1 change: 1 addition & 0 deletions toponetx/generators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"""Initialize the generators module."""

from .random_simplicial_complexes import *
1 change: 1 addition & 0 deletions toponetx/generators/random_simplicial_complexes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Generators for random simplicial complexes."""

import random
from collections.abc import Sequence
from itertools import combinations
Expand Down
1 change: 1 addition & 0 deletions toponetx/readwrite/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Module for reading and writing complexes from and to files."""

from .atomlist import *
from .serialization import *
1 change: 1 addition & 0 deletions toponetx/readwrite/serialization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Read and write complexes as pickled objects."""

import pickle

__all__ = ["to_pickle", "load_from_pickle"]
Expand Down
3 changes: 2 additions & 1 deletion toponetx/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Intialize the utils module."""
"""Initialize the utils module."""

from .normalization import *
from .structure import *
1 change: 1 addition & 0 deletions toponetx/utils/normalization.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d19d591

Please sign in to comment.