MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / Hypergraph

Class Hypergraph

easygraph/classes/hypergraph.py:23–1792  ·  view source on GitHub ↗

r"""The ``Hypergraph`` class is developed for hypergraph structures. Args: ``num_v`` (``int``): The number of vertices in the hypergraph. ``e_list`` (``Union[List[int], List[List[int]]]``, optional): A list of hyperedges describes how the vertices point to the hyperedges. Defaul

Source from the content-addressed store, hash-verified

21
22
23class Hypergraph(BaseHypergraph):
24 r"""The ``Hypergraph`` class is developed for hypergraph structures.
25
26 Args:
27 ``num_v`` (``int``): The number of vertices in the hypergraph.
28 ``e_list`` (``Union[List[int], List[List[int]]]``, optional): A list of hyperedges describes how the vertices point to the hyperedges. Defaults to ``None``.
29 ``e_weight`` (``Union[float, List[float]]``, optional): A list of weights for hyperedges. If set to ``None``, the value ``1`` is used for all hyperedges. Defaults to ``None``.
30 ``merge_op`` (``str``): The operation to merge those conflicting hyperedges in the same hyperedge group, which can be ``'mean'``, ``'sum'`` or ``'max'``. Defaults to ``'mean'``.
31 ``device`` (``torch.device``, optional): The device to store the hypergraph. Defaults to ``torch.device('cpu')``.
32 """
33
34 def __init__(
35 self,
36 num_v: int,
37 e_list: Optional[Union[List[int], List[List[int]]]] = None,
38 e_weight: Optional[Union[float, List[float]]] = None,
39 merge_op: str = "mean",
40 device: torch.device = torch.device("cpu"),
41 ):
42 super().__init__(num_v, device=device)
43 if e_list is not None:
44 self.add_hyperedges(e_list, e_weight, merge_op=merge_op)
45
46 def __repr__(self) -> str:
47 r"""Print the hypergraph information."""
48 return f"Hypergraph(num_vertex={self.num_v}, num_hyperedge={self.num_e})"
49
50 @property
51 def state_dict(self) -> Dict[str, Any]:
52 r"""Get the state dict of the hypergraph."""
53 return {"num_v": self.num_v, "raw_groups": self._raw_groups}
54
55 def save(self, file_path: Union[str, Path]):
56 r"""Save the DHG's hypergraph structure a file.
57
58 Args:
59 ``file_path`` (``Union[str, Path]``): The file path to store the DHG's hypergraph structure.
60 """
61 file_path = Path(file_path)
62 assert file_path.parent.exists(), "The directory does not exist."
63 data = {
64 "class": "Hypergraph",
65 "state_dict": self.state_dict,
66 }
67 with open(file_path, "wb") as fp:
68 pickle.dump(data, fp)
69
70 @staticmethod
71 def load(file_path: Union[str, Path]):
72 r"""Load the DHG's hypergraph structure from a file.
73
74 Args:
75 ``file_path`` (``Union[str, Path]``): The file path to load the DHG's hypergraph structure.
76 """
77 file_path = Path(file_path)
78 assert file_path.exists(), "The file does not exist."
79 with open(file_path, "rb") as fp:
80 data = pickle.load(fp)

Callers 5

cloneMethod · 0.85
from_state_dictMethod · 0.85
from_feature_kNNMethod · 0.85
from_graphMethod · 0.85
from_graph_kHopMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected