Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weight parameter throughout source code #242

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions toponetx/algorithms/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,15 @@ def laplacian_spectrum(matrix, weight: str = "weight"):


def cell_complex_hodge_laplacian_spectrum(
CX: CellComplex, rank: int, weight: str = "weight"
CX: CellComplex, rank: int, weight: str | None = None
):
"""Return eigenvalues of the Laplacian of G.

Parameters
----------
matrix : scipy sparse matrix
weight : str or None, optional (default='weight')
CX : CellComplex
rank : int
weight : str, optional
If None, then each cell has weight 1.

Returns
Expand All @@ -218,7 +219,7 @@ def cell_complex_hodge_laplacian_spectrum(

Examples
--------
>>> from toponetx import CellComplex
>>> from toponetx.classes import CellComplex
>>> CX = CellComplex()
>>> CX.add_cell([1,2,3,4],rank=2)
>>> CX.add_cell([2,3,4,5],rank=2)
Expand All @@ -229,13 +230,14 @@ def cell_complex_hodge_laplacian_spectrum(


def simplicial_complex_hodge_laplacian_spectrum(
SC: SimplicialComplex, rank, weight: str = "weight"
SC: SimplicialComplex, rank: int, weight: str = "weight"
):
"""Return eigenvalues of the Laplacian of G.

Parameters
----------
matrix : scipy sparse matrix
SC : SimplicialComplex
rank : int
weight : str or None, optional (default='weight')
If None, then each cell has weight 1.

Expand All @@ -246,7 +248,7 @@ def simplicial_complex_hodge_laplacian_spectrum(

Examples
--------
>>> from toponetx import SimplicialComplex
>>> from toponetx.classes import SimplicialComplex
>>> SC=SimplicialComplex([[1,2,3],[2,3,5],[0,1]])
>>> spectrum=simplicial_complex_hodge_laplacian_spectrum(SC,1)
"""
Expand Down Expand Up @@ -284,14 +286,15 @@ def cell_complex_adjacency_spectrum(CX: CellComplex, rank):


def simplicial_complex_adjacency_spectrum(
SC: SimplicialComplex, dim: int, weight: str = "weight"
SC: SimplicialComplex, dim: int, weight: str | None = None
):
"""Return eigenvalues of the Laplacian of G.

Parameters
----------
matrix : scipy sparse matrix
weight : str or None, optional (default='weight')
SC : SimplicialComplex
dim : int
weight : str, optional
If None, then each cell has weight 1.

Returns
Expand Down
121 changes: 68 additions & 53 deletions toponetx/classes/cell_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,19 +1325,18 @@
return True

def node_to_all_cell_incidence_matrix(
self, weight: bool = False, index: bool = False
self, weight: str | None = None, index: bool = False
) -> scipy.sparse.csc_matrix | tuple[dict, dict, scipy.sparse.csc_matrix]:
"""Nodes/cells incidence matrix for the indexed by nodes X cells.

Parameters
----------
weight : bool, default=False
If False all nonzero entries are 1.
If True and self.static all nonzero entries are filled by
self.cells.cell_weight dictionary values.
index : boolean, optional, default False
weight : str, optional
If not given, all nonzero entries are 1.
index : boolean, default=False
If True return will include a dictionary of node uid : row number
and cell uid : column number

Returns
-------
scipy.sparse.csr.csc_matrix | tuple[dict, dict, scipy.sparse.csc_matrix]
Expand All @@ -1362,7 +1361,7 @@
return A.asformat("csc")

def node_to_all_cell_adjacnecy_matrix(
self, s: int | None = None, weight: bool = False, index: bool = False
self, s: int | None = None, weight: str | None = None, index: bool = False
) -> scipy.sparse.csc_matrix | tuple[dict, dict, scipy.sparse.csc_matrix]:
"""Nodes s-adjacency matrix where adjacency is computed with respect to 2-cells.

Expand All @@ -1371,13 +1370,12 @@

Parameters
----------
weight : bool, default=False
If False all nonzero entries are 1.
If True and self.static all nonzero entries are filled by
self.cells.cell_weight dictionary values.
index : boolean, optional, default False
weight : str, optional
If not given, all nonzero entries are 1.
index : boolean, default=False
If True return will include a dictionary of node uid : row number
and cell uid : column number

Returns
-------
scipy.sparse.csr.csc_matrix | tuple[dict, dict, scipy.sparse.csc_matrix]
Expand Down Expand Up @@ -1414,7 +1412,7 @@
)

def all_cell_to_node_codjacnecy_matrix(
self, s: int | None = None, weight: bool = False, index: bool = False
self, s: int | None = None, weight: str | None = None, index: bool = False
) -> scipy.sparse.csc_matrix | tuple[dict, dict, scipy.sparse.csc_matrix]:
"""All cells s-coadjacency matrix where coadjacency is computed with respect to 0-cells.

Expand All @@ -1423,11 +1421,11 @@
Parameters
----------
weight : bool, default=False
If False all nonzero entries are 1.
If True and self.static all nonzero entries are filled by
self.cells.cell_weight dictionary values.
If not given, all nonzero entries are 1.

index : boolean, optional, default False
If True return will include a dictionary of cell uid

Returns
-------
scipy.sparse.csr.csc_matrix | tuple[dict, dict, scipy.sparse.csc_matrix]
Expand All @@ -1446,7 +1444,11 @@
)

def incidence_matrix(
self, rank: int, signed: bool = True, weight: bool = False, index: bool = False
self,
rank: int,
signed: bool = True,
weight: str | None = None,
index: bool = False,
) -> scipy.sparse.csc_matrix | tuple[dict, dict, scipy.sparse.csc_matrix]:
"""Incidence matrix for the cx indexed by nodes x cells.

Expand All @@ -1456,10 +1458,8 @@
The rank for which an incidence matrix should be computed.
signed : bool, default=True
Whether the returned incidence matrix should be signed (i.e., respect orientations) or unsigned.
weight : bool, default=False
If False all nonzero entries are 1.
If True and self.static all nonzero entries are filled by
self.cells.cell_weight dictionary values.
weight : str, optional
If not given, all nonzero entries are 1.
index : boolean, optional, default False
If True return will include a dictionary of node uid : row number
and cell uid : column number
Expand Down Expand Up @@ -1600,7 +1600,11 @@
raise ValueError(f"Only dimensions 0, 1 and 2 are supported, got {rank}.")

def hodge_laplacian_matrix(
self, rank: int, signed: bool = True, weight: bool = False, index: bool = False
self,
rank: int,
signed: bool = True,
weight: str | None = None,
index: bool = False,
) -> scipy.sparse.csc_matrix | tuple[dict, dict, scipy.sparse.csc_matrix]:
"""Compute the hodge-laplacian matrix for the CX.

Expand All @@ -1614,10 +1618,8 @@
adjacency matrices from the hodge-laplacian
higher-order adjacency matrices' entries are
typically positive.
weight : bool, default=False
If False all nonzero entries are 1.
If True and self.static all nonzero entries are filled by
self.cells.cell_weight dictionary values.
weight : str, optional
If not given, all nonzero entries are 1.
index : boolean, default False
indicates whether to return the indices that define the Laplacian matrix

Expand Down Expand Up @@ -1693,7 +1695,11 @@
)

def up_laplacian_matrix(
self, rank: int, signed: bool = True, weight: bool = False, index: bool = False
self,
rank: int,
signed: bool = True,
weight: str | None = None,
index: bool = False,
):
"""Compute up laplacian.

Expand All @@ -1706,10 +1712,8 @@
adjacency matrices from the hodge-laplacian
typically higher-order adjacency matrices' entries are
typically positive.
weight : bool, default=False
If False all nonzero entries are 1.
If True and self.static all nonzero entries are filled by
self.cells.cell_weight dictionary values.
weight : str, optional
If not given, all nonzero entries are 1.
index : boolean, optional, default False
list identifying rows with nodes,edges or cells used to index the hodge Laplacian matrix
depending on the input dimension
Expand Down Expand Up @@ -1737,7 +1741,8 @@
>>> print(index)
>>> print(L1_up)
"""
weight = None # this feature is not supported in this version
if weight is not None:
raise ValueError("Weighted Laplacian is not supported in this version.")

Check warning on line 1745 in toponetx/classes/cell_complex.py

View check run for this annotation

Codecov / codecov/patch

toponetx/classes/cell_complex.py#L1745

Added line #L1745 was not covered by tests

if rank == 0:
row, col, B_next = self.incidence_matrix(
Expand All @@ -1763,7 +1768,11 @@
return L_up

def down_laplacian_matrix(
self, rank: int, signed: bool = True, weight: bool = False, index: bool = False
self,
rank: int,
signed: bool = True,
weight: str | None = None,
index: bool = False,
):
"""Compute down laplacian.

Expand All @@ -1776,10 +1785,8 @@
adjacency matrices from the hodge-laplacian
typically higher-order adjacency matrices' entries are
typically positive.
weight : bool, default=False
If False all nonzero entries are 1.
If True and all nonzero entries are filled by
self.cells.cell_weight dictionary values.
weight : str, optional
If not given, all nonzero entries are 1.
index : boolean, optional, default False
list identifying rows with nodes,edges or cells used to index the hodge Laplacian matrix
depending on the input dimension
Expand All @@ -1798,16 +1805,17 @@
>>> import networkx as nx
>>> G = nx.path_graph(3)
>>> CX = CellComplex(G)
>>> CX.add_cell([1,2,3,4], rank=2)
>>> CX.add_cell([1,2,3,4], rank=2)
>>> CX.add_cell([2,3,4,1], rank=2)
>>> CX.add_cell([1,2,4], rank=2,)
>>> CX.add_cell([3,4,8], rank=2)
>>> CX.add_cell([1, 2, 3, 4], rank=2)
>>> CX.add_cell([1, 2, 3, 4], rank=2)
>>> CX.add_cell([2, 3, 4, 1], rank=2)
>>> CX.add_cell([1, 2, 4], rank=2,)
>>> CX.add_cell([3, 4, 8], rank=2)
>>> CX.down_laplacian_matrix(2)
"""
weight = None # this feature is not supported in this version
if weight:
raise ValueError("Weighted Laplacian is not supported in this version.")

Check warning on line 1816 in toponetx/classes/cell_complex.py

View check run for this annotation

Codecov / codecov/patch

toponetx/classes/cell_complex.py#L1816

Added line #L1816 was not covered by tests

if rank <= self.dim and rank > 0:
if 0 < rank <= self.dim:
row, column, B = self.incidence_matrix(rank, weight=weight, index=True)
L_down = B.transpose() @ B
else:
Expand All @@ -1821,7 +1829,13 @@
else:
return L_down

def adjacency_matrix(self, rank: int, signed: bool = False, index: bool = False):
def adjacency_matrix(
self,
rank: int,
signed: bool = False,
weight: str | None = None,
index: bool = False,
):
"""Compute adjacency matrix for a given rank."""
if index:
ind, _, incidence = self.incidence_matrix(
Expand All @@ -1837,16 +1851,19 @@
else:
return incidence_to_adjacency(incidence)

def coadjacency_matrix(self, rank: int, signed: bool = False, index: bool = False):
def coadjacency_matrix(
self,
rank: int,
signed: bool = False,
weight: str | None = None,
index: bool = False,
):
"""Compute coadjacency matrix for a given rank."""
if index:
_, ind, incidence = self.incidence_matrix(rank, signed=signed, index=True)
else:
incidence = self.incidence_matrix(rank, signed=signed)

if index:
return ind, incidence_to_adjacency(incidence)
else:
incidence = self.incidence_matrix(rank, signed=signed)
return incidence_to_adjacency(incidence)

def restrict_to_cells(
Expand Down Expand Up @@ -2100,7 +2117,7 @@
"""Euler characteristics of the cell complex."""
return len(self.nodes) - len(self.edges) + len(self.cells)

def remove_singletons(self) -> "CellComplex":
def remove_singletons(self) -> None:
"""Remove singleton nodes (see `CellComplex.singletons()`)."""
for node in self.singletons():
self._G.remove_node(node)
Expand Down Expand Up @@ -2546,14 +2563,12 @@
first_ind = np.min(mesh.faces)

if first_ind == 0:

CX.set_cell_attributes(
dict(zip(range(len(mesh.vertices)), mesh.vertices)),
name="position",
rank=0,
)
else: # first index starts at 1.

CX.set_cell_attributes(
dict(
zip(range(first_ind, len(mesh.vertices) + first_ind), mesh.vertices)
Expand Down
16 changes: 6 additions & 10 deletions toponetx/classes/colored_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def incidence_matrix(
self,
rank,
to_rank,
weight=None,
weight: str | None = None,
sparse: bool = True,
index: bool = False,
):
Expand All @@ -929,10 +929,8 @@ def incidence_matrix(
----------
rank : int
to_rank: int
weight : bool, default=False
If False all nonzero entries are 1.
If True and self.static all nonzero entries are filled by
self.cells.cell_weight dictionary values.
weight : str, optional
If not given, all nonzero entries are 1.
index : bool, default False
If True return will include a dictionary of node uid : row number
and cell uid : column number
Expand All @@ -950,18 +948,16 @@ def incidence_matrix(
def all_ranks_incidence_matrix(
self,
rank,
weight=None,
weight: str | None = None,
sparse: bool = True,
index: bool = False,
):
"""Compute incidence matrix for the CHG indexed by nodes x cells.

Parameters
----------
weight : bool, default=False
If False all nonzero entries are 1.
If True and self.static all nonzero entries are filled by
self.cells.cell_weight dictionary values.
weight : str, optional
If not given, all nonzero entries are 1.
index : boolean, optional, default False
If True return will include a dictionary of node uid : row number
and cell uid : column number
Expand Down