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

new add method #203

Merged
merged 2 commits into from
Oct 31, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/classes/test_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ def test_contains(edgelist1):
assert [1, 2, 3] not in H


def test_lshift(edgelist1, edgelist2, hyperwithattrs):
H1 = xgi.Hypergraph(edgelist1)
H2 = xgi.Hypergraph(edgelist2)
H3 = hyperwithattrs
H = H1 << H2
assert set(H.nodes) == {1, 2, 3, 4, 5, 6, 7, 8}
assert H.num_edges == 7
assert H.edges.members(0) == {1, 2, 3}

H = H1 << H2 << H3
assert set(H.nodes) == {1, 2, 3, 4, 5, 6, 7, 8}
assert H.num_edges == 10
assert H.nodes[1] == {"color": "red", "name": "horse"}
assert H.edges.members(7) == {1, 2, 3}


def test_string():
H1 = xgi.Hypergraph()
assert str(H1) == "Unnamed Hypergraph with 0 nodes and 0 hyperedges"
Expand Down
49 changes: 49 additions & 0 deletions xgi/classes/hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,55 @@ def func(node=None, *args, **kwargs):

return func

def __lshift__(self, H2):
"""Adds the edges of a hypergraph to the current hypergraph
and updates the attributes.

The node/edge attributes of the new hypergraph take precedence.

Relabels all the edge IDs to preserve all the edges but
keeps the node labels the same.

Parameters
----------
H2 : Hypergraph
The hypergraph to update with.

Returns
-------
Hypergraph
The updated hypergraph

Notes
-----
Addition is not quite commutative; the attributes of nodes and edges
may be overwritten depending on whether they are first or second
to be added. In addition, the edge IDs are assigned based on the order
in which the edges are added, but does not functionally change the
structure of the hypergraph.

Examples
--------

>>> import xgi
>>> H1 = xgi.Hypergraph([[1, 2], [2, 3]])
>>> H2 = xgi.Hypergraph([[1, 3, 4]])
>>> H = H1 << H2
>>> H.edges.members()
[{1, 2}, {2, 3}, {1, 3, 4}]
"""
tempH = Hypergraph()
tempH.add_edges_from(zip(self._edge.values(), self._edge_attr.values()))
tempH.add_nodes_from(zip(self._node.keys(), self._node_attr.values()))

tempH.add_edges_from(zip(H2._edge.values(), H2._edge_attr.values()))
tempH.add_nodes_from(zip(H2._node.keys(), H2._node_attr.values()))

tempH._hypergraph = deepcopy(self._hypergraph)
tempH._hypergraph.update(deepcopy(H2._hypergraph))

return tempH

@property
def nodes(self):
"""A :class:`NodeView` of this network."""
Expand Down
3 changes: 1 addition & 2 deletions xgi/generators/classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import numpy as np

from ..classes import SimplicialComplex
from ..utils import py_random_state

from ..exception import XGIError
from ..utils import py_random_state

__all__ = [
"empty_hypergraph",
Expand Down