Skip to content

Commit ad3d720

Browse files
committed
Merge branch 'main' of github.com:iMoonLab/DeepHypergraph
2 parents 7e559b9 + e124154 commit ad3d720

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1112
-185
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
![Release version](https://img.shields.io/github/v/release/iMoonLab/DeepHypergraph)
66
[![PyPI version](https://img.shields.io/pypi/v/dhg?color=purple)](https://pypi.org/project/dhg/)
77
[![Website Build Status](https://github.com/yifanfeng97/dhg-page-source/actions/workflows/website.yml/badge.svg)](https://deephypergraph.com/)
8-
[![Documentation Status](https://readthedocs.org/projects/deephypergraph/badge/?version=latest)](https://deephypergraph.readthedocs.io/en/latest/?badge=latest)
8+
[![Documentation Status](https://readthedocs.org/projects/deephypergraph/badge/?version=latest)](https://deephypergraph.readthedocs.io/)
99
[![Downloads](https://pepy.tech/badge/dhg)](https://pepy.tech/project/dhg)
1010
[![Visits Badge](https://visitor-badge.glitch.me/badge?page_id=iMoonLab.DeepHypergraph)](https://visitor-badge.glitch.me/)
1111
[![license](https://img.shields.io/github/license/imoonlab/DeepHypergraph)](LICENSE)

dhg/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from ._global import AUTHOR_EMAIL, CACHE_ROOT, DATASETS_ROOT, REMOTE_DATASETS_ROOT
1616

17-
__version__ = "0.9.2"
17+
__version__ = "0.9.3"
1818

1919
__all__ = {
2020
"AUTHOR_EMAIL",

dhg/metrics/recommender.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ def recall(
112112
assert y_true.max() == 1, "The input y_true must be binary."
113113
pred_seq = y_true.gather(1, torch.argsort(y_pred, dim=-1, descending=True))[:, :k]
114114
num_true = y_true.sum(dim=1)
115-
res_list = (pred_seq.sum(dim=1) / num_true).detach().cpu()
115+
res_list = (pred_seq.sum(dim=1) / num_true).cpu()
116+
res_list[torch.isinf(res_list)] = 0
117+
res_list[torch.isnan(res_list)] = 0
116118
if ret_batch:
117119
return [res.item() for res in res_list]
118120
else:
@@ -169,6 +171,7 @@ def ndcg(
169171

170172
res_list = (pred_dcg / ideal_dcg).detach().cpu()
171173
res_list[torch.isinf(res_list)] = 0
174+
res_list[torch.isnan(res_list)] = 0
172175
if ret_batch:
173176
return [res.item() for res in res_list]
174177
else:

dhg/metrics/retrieval.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ def recall(
104104
assert y_true.max() == 1, "The input y_true must be binary."
105105
pred_seq = y_true.gather(1, torch.argsort(y_pred, dim=-1, descending=True))[:, :k]
106106
num_true = y_true.sum(dim=1)
107-
res_list = (pred_seq.sum(dim=1) / num_true).detach().cpu()
107+
res_list = (pred_seq.sum(dim=1) / num_true).cpu()
108+
res_list[torch.isinf(res_list)] = 0
109+
res_list[torch.isnan(res_list)] = 0
108110
if ret_batch:
109111
return res_list
110112
else:
@@ -252,6 +254,7 @@ def ndcg(
252254

253255
res_list = pred_dcg / ideal_dcg
254256
res_list[torch.isinf(res_list)] = 0
257+
res_list[torch.isnan(res_list)] = 0
255258
if ret_batch:
256259
return res_list
257260
else:

dhg/structure/hypergraphs/hypergraph.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def draw(
9696
pull_center_strength: float = 1.0,
9797
):
9898
r"""Draw the hypergraph structure.
99-
99+
100100
Args:
101101
``e_style`` (``str``): The style of hyperedges. The available styles are only ``'circle'``. Defaults to ``'circle'``.
102102
``v_label`` (``list``): The labels of vertices. Defaults to ``None``.
@@ -201,7 +201,7 @@ def from_feature_kNN(features: torch.Tensor, k: int, device: torch.device = torc
201201

202202
@staticmethod
203203
def from_graph(graph: "Graph", device: torch.device = torch.device("cpu")) -> "Hypergraph":
204-
r"""Construct the hypergraph from the graph. Each edge in the graph is treated as a hyperedge in the constructed hypergraph.
204+
r"""Construct the hypergraph from the graph. Each edge in the graph is treated as a hyperedge in the constructed hypergraph.
205205
206206
.. note::
207207
The construsted hypergraph is a 2-uniform hypergraph, and has the same number of vertices and edges/hyperedges as the graph.
@@ -263,13 +263,13 @@ def from_graph_kHop(
263263

264264
@staticmethod
265265
def _e_list_from_bigraph(bigraph: "BiGraph", U_as_vertex: bool = True) -> List[tuple]:
266-
r"""Construct hyperedges from the bipartite graph.
266+
r"""Construct hyperedges from the bipartite graph.
267267
268268
Args:
269269
``bigraph`` (``BiGraph``): The bipartite graph to construct the hypergraph.
270-
``U_as_vertex`` (``bool``, optional): If set to ``True``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
271-
will be treated as vertices and hyperedges in the constructed hypergraph, respectively.
272-
If set to ``False``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
270+
``U_as_vertex`` (``bool``, optional): If set to ``True``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
271+
will be treated as vertices and hyperedges in the constructed hypergraph, respectively.
272+
If set to ``False``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
273273
will be treated as hyperedges and vertices in the constructed hypergraph, respectively. Defaults to ``True``.
274274
"""
275275
e_list = []
@@ -289,13 +289,13 @@ def _e_list_from_bigraph(bigraph: "BiGraph", U_as_vertex: bool = True) -> List[t
289289
def from_bigraph(
290290
bigraph: "BiGraph", U_as_vertex: bool = True, device: torch.device = torch.device("cpu")
291291
) -> "Hypergraph":
292-
r"""Construct the hypergraph from the bipartite graph.
292+
r"""Construct the hypergraph from the bipartite graph.
293293
294294
Args:
295295
``bigraph`` (``BiGraph``): The bipartite graph to construct the hypergraph.
296-
``U_as_vertex`` (``bool``, optional): If set to ``True``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
297-
will be treated as vertices and hyperedges in the constructed hypergraph, respectively.
298-
If set to ``False``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
296+
``U_as_vertex`` (``bool``, optional): If set to ``True``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
297+
will be treated as vertices and hyperedges in the constructed hypergraph, respectively.
298+
If set to ``False``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
299299
will be treated as hyperedges and vertices in the constructed hypergraph, respectively. Defaults to ``True``.
300300
``device`` (``torch.device``, optional): The device to store the hypergraph. Defaults to ``torch.device('cpu')``.
301301
"""
@@ -356,7 +356,7 @@ def add_hyperedges_from_feature_kNN(self, feature: torch.Tensor, k: int, group_n
356356
self.add_hyperedges(e_list, group_name=group_name)
357357

358358
def add_hyperedges_from_graph(self, graph: "Graph", group_name: str = "main"):
359-
r"""Add hyperedges from edges in the graph. Each edge in the graph is treated as a hyperedge.
359+
r"""Add hyperedges from edges in the graph. Each edge in the graph is treated as a hyperedge.
360360
361361
Args:
362362
``graph`` (``Graph``): The graph to join the hypergraph.
@@ -385,13 +385,13 @@ def add_hyperedges_from_graph_kHop(
385385
self.add_hyperedges(e_list, group_name=group_name)
386386

387387
def add_hyperedges_from_bigraph(self, bigraph: "BiGraph", U_as_vertex: bool = False, group_name: str = "main"):
388-
r"""Add hyperedges from the bipartite graph.
388+
r"""Add hyperedges from the bipartite graph.
389389
390390
Args:
391391
``bigraph`` (``BiGraph``): The bigraph to join the hypergraph.
392-
``U_as_vertex`` (``bool``): If set to ``True``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
393-
will be treated as vertices and hyperedges in the constructed hypergraph, respectively.
394-
If set to ``False``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
392+
``U_as_vertex`` (``bool``): If set to ``True``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
393+
will be treated as vertices and hyperedges in the constructed hypergraph, respectively.
394+
If set to ``False``, vertices in set :math:`\mathcal{U}` and set :math:`\mathcal{V}`
395395
will be treated as hyperedges and vertices in the constructed hypergraph, respectively. Defaults to ``True``.
396396
``group_name`` (``str``, optional): The target hyperedge group to add these hyperedges. Defaults to the ``main`` hyperedge group.
397397
"""
@@ -624,17 +624,17 @@ def group_names(self) -> List[str]:
624624
# properties for deep learning
625625
@property
626626
def vars_for_DL(self) -> List[str]:
627-
r"""Return a name list of available variables for deep learning in the hypergraph including
627+
r"""Return a name list of available variables for deep learning in the hypergraph including
628628
629629
Sparse Matrices:
630-
630+
631631
.. math::
632632
\mathbf{H}, \mathbf{H}^\top, \mathcal{L}_{sym}, \mathcal{L}_{rw} \mathcal{L}_{HGNN},
633-
633+
634634
Sparse Diagnal Matrices:
635635
636636
.. math::
637-
\mathbf{W}_e, \mathbf{D}_v, \mathbf{D}_v^{-1}, \mathbf{D}_v^{-\frac{1}{2}}, \mathbf{D}_e, \mathbf{D}_e^{-1},
637+
\mathbf{W}_e, \mathbf{D}_v, \mathbf{D}_v^{-1}, \mathbf{D}_v^{-\frac{1}{2}}, \mathbf{D}_e, \mathbf{D}_e^{-1},
638638
639639
Vectors:
640640
@@ -670,7 +670,7 @@ def v2e_src(self) -> torch.Tensor:
670670

671671
def v2e_src_of_group(self, group_name: str) -> torch.Tensor:
672672
r"""Return the source vertex index vector :math:`\overrightarrow{v2e}_{src}` of the connections (vertices point to hyperedges) in the specified hyperedge group.
673-
673+
674674
Args:
675675
``group_name`` (``str``): The name of the specified hyperedge group.
676676
"""
@@ -685,7 +685,7 @@ def v2e_dst(self) -> torch.Tensor:
685685

686686
def v2e_dst_of_group(self, group_name: str) -> torch.Tensor:
687687
r"""Return the destination hyperedge index vector :math:`\overrightarrow{v2e}_{dst}` of the connections (vertices point to hyperedges) in the specified hyperedge group.
688-
688+
689689
Args:
690690
``group_name`` (``str``): The name of the specified hyperedge group.
691691
"""
@@ -700,7 +700,7 @@ def v2e_weight(self) -> torch.Tensor:
700700

701701
def v2e_weight_of_group(self, group_name: str) -> torch.Tensor:
702702
r"""Return the weight vector :math:`\overrightarrow{v2e}_{weight}` of the connections (vertices point to hyperedges) in the specified hyperedge group.
703-
703+
704704
Args:
705705
``group_name`` (``str``): The name of the specified hyperedge group.
706706
"""
@@ -715,7 +715,7 @@ def e2v_src(self) -> torch.Tensor:
715715

716716
def e2v_src_of_group(self, group_name: str) -> torch.Tensor:
717717
r"""Return the source hyperedge index vector :math:`\overrightarrow{e2v}_{src}` of the connections (hyperedges point to vertices) in the specified hyperedge group.
718-
718+
719719
Args:
720720
``group_name`` (``str``): The name of the specified hyperedge group.
721721
"""
@@ -730,7 +730,7 @@ def e2v_dst(self) -> torch.Tensor:
730730

731731
def e2v_dst_of_group(self, group_name: str) -> torch.Tensor:
732732
r"""Return the destination vertex index vector :math:`\overrightarrow{e2v}_{dst}` of the connections (hyperedges point to vertices) in the specified hyperedge group.
733-
733+
734734
Args:
735735
``group_name`` (``str``): The name of the specified hyperedge group.
736736
"""
@@ -745,7 +745,7 @@ def e2v_weight(self) -> torch.Tensor:
745745

746746
def e2v_weight_of_group(self, group_name: str) -> torch.Tensor:
747747
r"""Return the weight vector :math:`\overrightarrow{e2v}_{weight}` of the connections (hyperedges point to vertices) in the specified hyperedge group.
748-
748+
749749
Args:
750750
``group_name`` (``str``): The name of the specified hyperedge group.
751751
"""
@@ -1010,7 +1010,7 @@ def N_e_of_group(self, v_idx: int, group_name: str) -> torch.Tensor:
10101010
return e_indices.clone()
10111011

10121012
def N_v(self, e_idx: int) -> torch.Tensor:
1013-
r"""Return the neighbor vertices of the specified hyperedge with ``torch.Tensor`` format.
1013+
r"""Return the neighbor vertices of the specified hyperedge with ``torch.Tensor`` format.
10141014
10151015
.. note::
10161016
The ``e_idx`` must be in the range of [0, :attr:`num_e`).
@@ -1048,7 +1048,7 @@ def smoothing(self, X: torch.Tensor, L: torch.Tensor, lamb: float) -> torch.Tens
10481048
@property
10491049
def L_sym(self) -> torch.Tensor:
10501050
r"""Return the symmetric Laplacian matrix :math:`\mathcal{L}_{sym}` of the hypergraph with ``torch.sparse_coo_tensor`` format.
1051-
1051+
10521052
.. math::
10531053
\mathcal{L}_{sym} = \mathbf{I} - \mathbf{D}_v^{-\frac{1}{2}} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} \mathbf{H}^\top \mathbf{D}_v^{-\frac{1}{2}}
10541054
"""
@@ -1064,10 +1064,10 @@ def L_sym(self) -> torch.Tensor:
10641064

10651065
def L_sym_of_group(self, group_name: str) -> torch.Tensor:
10661066
r"""Return the symmetric Laplacian matrix :math:`\mathcal{L}_{sym}` of the specified hyperedge group with ``torch.sparse_coo_tensor`` format.
1067-
1067+
10681068
.. math::
10691069
\mathcal{L}_{sym} = \mathbf{I} - \mathbf{D}_v^{-\frac{1}{2}} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} \mathbf{H}^\top \mathbf{D}_v^{-\frac{1}{2}}
1070-
1070+
10711071
Args:
10721072
``group_name`` (``str``): The name of the specified hyperedge group.
10731073
"""
@@ -1085,7 +1085,7 @@ def L_sym_of_group(self, group_name: str) -> torch.Tensor:
10851085
@property
10861086
def L_rw(self) -> torch.Tensor:
10871087
r"""Return the random walk Laplacian matrix :math:`\mathcal{L}_{rw}` of the hypergraph with ``torch.sparse_coo_tensor`` format.
1088-
1088+
10891089
.. math::
10901090
\mathcal{L}_{rw} = \mathbf{I} - \mathbf{D}_v^{-1} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} \mathbf{H}^\top
10911091
"""
@@ -1105,10 +1105,10 @@ def L_rw(self) -> torch.Tensor:
11051105

11061106
def L_rw_of_group(self, group_name: str) -> torch.Tensor:
11071107
r"""Return the random walk Laplacian matrix :math:`\mathcal{L}_{rw}` of the specified hyperedge group with ``torch.sparse_coo_tensor`` format.
1108-
1108+
11091109
.. math::
11101110
\mathcal{L}_{rw} = \mathbf{I} - \mathbf{D}_v^{-1} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} \mathbf{H}^\top
1111-
1111+
11121112
Args:
11131113
``group_name`` (``str``): The name of the specified hyperedge group.
11141114
"""
@@ -1151,7 +1151,7 @@ def L_HGNN_of_group(self, group_name: str) -> torch.Tensor:
11511151
11521152
.. math::
11531153
\mathcal{L}_{HGNN} = \mathbf{D}_v^{-\frac{1}{2}} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} \mathbf{H}^\top \mathbf{D}_v^{-\frac{1}{2}}
1154-
1154+
11551155
Args:
11561156
``group_name`` (``str``): The name of the specified hyperedge group.
11571157
"""
@@ -1172,8 +1172,8 @@ def smoothing_with_HGNN(self, X: torch.Tensor, drop_rate: float = 0.0) -> torch.
11721172
r"""Return the smoothed feature matrix with the HGNN Laplacian matrix :math:`\mathcal{L}_{HGNN}`.
11731173
11741174
.. math::
1175-
\mathbf{X} = \mathbf{D}_v^{-\frac{1}{2}} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} \mathbf{H}^\top \mathbf{D}_v^{-\frac{1}{2}} \mathbf{X}
1176-
1175+
\mathbf{X} = \mathbf{D}_v^{-\frac{1}{2}} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} \mathbf{H}^\top \mathbf{D}_v^{-\frac{1}{2}} \mathbf{X}
1176+
11771177
Args:
11781178
``X`` (``torch.Tensor``): The feature matrix. Size :math:`(|\mathcal{V}|, C)`.
11791179
``drop_rate`` (``float``): Dropout rate. Randomly dropout the connections in incidence matrix with probability ``drop_rate``. Default: ``0.0``.
@@ -1190,8 +1190,8 @@ def smoothing_with_HGNN_of_group(self, group_name: str, X: torch.Tensor, drop_ra
11901190
r"""Return the smoothed feature matrix with the HGNN Laplacian matrix :math:`\mathcal{L}_{HGNN}`.
11911191
11921192
.. math::
1193-
\mathbf{X} = \mathbf{D}_v^{-\frac{1}{2}} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} \mathbf{H}^\top \mathbf{D}_v^{-\frac{1}{2}} \mathbf{X}
1194-
1193+
\mathbf{X} = \mathbf{D}_v^{-\frac{1}{2}} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} \mathbf{H}^\top \mathbf{D}_v^{-\frac{1}{2}} \mathbf{X}
1194+
11951195
Args:
11961196
``group_name`` (``str``): The name of the specified hyperedge group.
11971197
``X`` (``torch.Tensor``): The feature matrix. Size :math:`(|\mathcal{V}|, C)`.

dhg/utils/structure.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def remap_edge_list(
6565
``bipartite_graph`` (``bool``): Whether the structure is bipartite graph. Defaults to ``False``.
6666
``ret_map`` (``bool``): Whether to return the map dictionary of raw marker to new index. Defaults to ``False``.
6767
"""
68+
e_list = [[str(v) for v in e] for e in e_list]
6869
if bipartite_graph:
6970
u_set, v_set = set(), set()
7071
for u, v in e_list:
@@ -87,7 +88,7 @@ def remap_edge_list(
8788
v_set.add(v)
8889
v_list = sorted(v_set)
8990
v_map = {raw_v: new_v for new_v, raw_v in enumerate(v_list)}
90-
e_list = [set([v_map[v] for v in e]) for e in e_list]
91+
e_list = [tuple([v_map[v] for v in e]) for e in e_list]
9192
if ret_map:
9293
return e_list, v_map
9394
else:
@@ -107,6 +108,7 @@ def remap_edge_lists(
107108
``bipartite_graph`` (``bool``): Whether the structure is bipartite graph. Defaults to ``False``.
108109
``ret_map`` (``bool``): Whether to return the map dictionary of raw marker to new index. Defaults to ``False``.
109110
"""
111+
e_lists = [[[str(v) for v in e] for e in e_list] for e_list in e_lists]
110112
if bipartite_graph:
111113
u_set, v_set = set(), set()
112114
for e_list in e_lists:
@@ -131,7 +133,7 @@ def remap_edge_lists(
131133
v_set.add(v)
132134
v_list = sorted(v_set)
133135
v_map = {raw_v: new_v for new_v, raw_v in enumerate(v_list)}
134-
e_list = [[set([v_map[v] for v in e]) for e in e_list] for e_list in e_lists]
136+
e_list = [[tuple([v_map[v] for v in e]) for e in e_list] for e_list in e_lists]
135137
if ret_map:
136138
return e_list, v_map
137139
else:
@@ -151,6 +153,7 @@ def remap_adj_list(
151153
``bipartite_graph`` (``bool``): Whether the structure is bipartite graph. Defaults to ``False``.
152154
``ret_map`` (``bool``): Whether to return the map dictionary of raw marker to new index. Defaults to ``False``.
153155
"""
156+
adj_list = [[str(v) for v in line] for line in adj_list]
154157
if bipartite_graph:
155158
u_set, v_set = set(), set()
156159
for line in adj_list:
@@ -202,6 +205,7 @@ def remap_adj_lists(
202205
``bipartite_graph`` (``bool``): Whether the structure is bipartite graph. Defaults to ``False``.
203206
``ret_map`` (``bool``): Whether to return the map dictionary of raw marker to new index. Defaults to ``False``.
204207
"""
208+
adj_lists = [[[str(v) for v in line] for line in adj_list] for adj_list in adj_lists]
205209
if bipartite_graph:
206210
u_set, v_set = set(), set()
207211
for adj_list in adj_lists:

dhg/visualization/structure/defaults.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def default_size(
6767
v_size: Union[float, list] = 1.0,
6868
v_line_width: Union[float, list] = 1.0,
6969
e_line_width: Union[float, list] = 1.0,
70-
font_size: int = 1.0,
70+
font_size: float = 1.0,
7171
):
7272
_v_size = 1 / np.sqrt(num_v + 10) * 0.1
7373
_v_line_width = 1 * np.exp(-num_v / 50)
@@ -91,8 +91,8 @@ def default_bipartite_size(
9191
v_size: Union[float, list] = 1.0,
9292
v_line_width: Union[float, list] = 1.0,
9393
e_line_width: Union[float, list] = 1.0,
94-
u_font_size: int = 1.0,
95-
v_font_size: int = 1.0,
94+
u_font_size: float = 1.0,
95+
v_font_size: float = 1.0,
9696
):
9797
_u_size = 1 / np.sqrt(num_u + 12) * 0.08
9898
_u_line_width = 1 * np.exp(-num_u / 50)

0 commit comments

Comments
 (0)