Skip to content

Commit

Permalink
Fix karateclub warning about self-loops
Browse files Browse the repository at this point in the history
  • Loading branch information
ffl096 committed Jul 10, 2024
1 parent 3da6693 commit 9e2040f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion test/classes/test_cell2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestCell2Vec:
def test_fit_and_get_embedding(self):
"""Test get_embedding."""
# Create a small complex
cx = tnx.classes.CellComplex([[1, 2, 3, 4], [3, 4, 5, 6, 7, 8]], ranks=2)
cx = tnx.CellComplex([[1, 2, 3, 4], [3, 4, 5, 6, 7, 8]], ranks=2)

# Create a Cell2Vec object
dc = Cell2Vec(dimensions=5)
Expand Down
4 changes: 3 additions & 1 deletion topoembedx/classes/cell2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import toponetx as tnx
from karateclub import Node2Vec
from scipy.sparse import csr_matrix

from topoembedx.neighborhood import neighborhood_from_complex

Expand Down Expand Up @@ -50,7 +51,7 @@ class Cell2Vec(Node2Vec):
Random seed value.
"""

A: np.ndarray
A: csr_matrix
ind: list

def fit(
Expand Down Expand Up @@ -94,6 +95,7 @@ def fit(
)

g = nx.from_numpy_array(self.A)
g.add_edges_from((index, index) for index in range(g.number_of_nodes()))

super().fit(g)

Expand Down
6 changes: 5 additions & 1 deletion topoembedx/classes/cell_diff2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import toponetx as tnx
from karateclub import Diff2Vec
from scipy.sparse import csr_matrix

from topoembedx.neighborhood import neighborhood_from_complex

Expand Down Expand Up @@ -35,7 +36,7 @@ class CellDiff2Vec(Diff2Vec):
Random seed value.
"""

A: np.ndarray
A: csr_matrix
ind: list

def fit(
Expand Down Expand Up @@ -78,11 +79,14 @@ def fit(
complex, neighborhood_type, neighborhood_dim
)

self.A.setdiag(1)
g = nx.from_numpy_array(self.A)

if self.diffusion_cover > g.number_of_nodes():
raise ValueError(
"The diffusion_cover is too large for the size of the graph."
)

super().fit(g)

def get_embedding(self, get_dict: bool = False) -> dict | np.ndarray:
Expand Down
4 changes: 3 additions & 1 deletion topoembedx/classes/deepcell.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import toponetx as tnx
from karateclub import DeepWalk
from scipy.sparse import csr_matrix

from topoembedx.neighborhood import neighborhood_from_complex

Expand Down Expand Up @@ -35,7 +36,7 @@ class DeepCell(DeepWalk):
Random seed to use for reproducibility.
"""

A: np.ndarray
A: csr_matrix
ind: list

def fit(
Expand Down Expand Up @@ -78,6 +79,7 @@ def fit(
complex, neighborhood_type, neighborhood_dim
)

self.A.setdiag(1)
g = nx.from_numpy_array(self.A)

super().fit(g)
Expand Down
4 changes: 3 additions & 1 deletion topoembedx/classes/higher_order_laplacian_eigenmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import toponetx as tnx
from karateclub import LaplacianEigenmaps
from scipy.sparse import csr_matrix

from topoembedx.neighborhood import neighborhood_from_complex

Expand All @@ -23,7 +24,7 @@ class HigherOrderLaplacianEigenmaps(LaplacianEigenmaps):
Random seed value.
"""

A: np.ndarray
A: csr_matrix
ind: list

def __init__(
Expand Down Expand Up @@ -75,6 +76,7 @@ def fit(
complex, neighborhood_type, neighborhood_dim
)

self.A.setdiag(1)
g = nx.from_numpy_array(self.A)

super().fit(g)
Expand Down
4 changes: 3 additions & 1 deletion topoembedx/classes/hoglee.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import toponetx as tnx
from karateclub import GLEE
from scipy.sparse import csr_matrix

from topoembedx.neighborhood import neighborhood_from_complex

Expand All @@ -21,7 +22,7 @@ class HOGLEE(GLEE):
Random seed value. Defaults to 42.
"""

A: np.ndarray
A: csr_matrix
ind: list

def fit(
Expand Down Expand Up @@ -64,6 +65,7 @@ def fit(
complex, neighborhood_type, neighborhood_dim
)

self.A.setdiag(1)
g = nx.from_numpy_array(self.A)

super().fit(g)
Expand Down
6 changes: 3 additions & 3 deletions topoembedx/neighborhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

from typing import Literal

import numpy as np
import toponetx as tnx
from scipy.sparse import csr_matrix


def neighborhood_from_complex(
complex: tnx.Complex,
neighborhood_type: Literal["adj", "coadj"] = "adj",
neighborhood_dim=None,
) -> tuple[list, np.ndarray]:
) -> tuple[list, csr_matrix]:
"""Compute the neighborhood of a complex.
This function returns the indices and matrix for the neighborhood specified
Expand Down Expand Up @@ -44,7 +44,7 @@ def neighborhood_from_complex(
-------
ind : list
A list of the indices for the nodes in the neighborhood.
A : ndarray
A : scipy.sparse.csr_matrix
The matrix representing the neighborhood.
Raises
Expand Down

0 comments on commit 9e2040f

Please sign in to comment.