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

Rename _hypergraph to _net_attr #560

Merged
merged 2 commits into from
Jul 12, 2024
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
2 changes: 1 addition & 1 deletion tests/convert/test_hypergraph_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_from_hypergraph_dict(edgelist1):
for n in H.nodes:
assert H.nodes[n] == Hd.nodes[n]

assert H._hypergraph == Hd._hypergraph
assert H._net_attr == Hd._net_attr

# test bad dicts
hd = {
Expand Down
10 changes: 5 additions & 5 deletions tests/core/test_dihypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_constructor(diedgelist1, diedgedict1):

def test_hypergraph_attrs():
H = xgi.DiHypergraph()
assert H._hypergraph == {}
assert H._net_attr == {}
with pytest.raises(XGIError):
H["name"]
H = xgi.DiHypergraph(name="test")
Expand Down Expand Up @@ -347,7 +347,7 @@ def test_copy(diedgelist1):
assert list(copy.nodes) == list(H.nodes)
assert list(copy.edges) == list(H.edges)
assert list(copy.edges.members()) == list(H.edges.members())
assert H._hypergraph == copy._hypergraph
assert H._net_attr == copy._net_attr

H.add_node(10)
assert list(copy.nodes) != list(H.nodes)
Expand All @@ -357,23 +357,23 @@ def test_copy(diedgelist1):
assert list(copy.edges) != list(H.edges)

H["key2"] = "value2"
assert H._hypergraph != copy._hypergraph
assert H._net_attr != copy._net_attr

copy.add_node(10)
copy.add_edge(([1, 3, 5], [6]))
copy["key2"] = "value2"
assert list(copy.nodes) == list(H.nodes)
assert list(copy.edges) == list(H.edges)
assert list(copy.edges.members()) == list(H.edges.members())
assert H._hypergraph == copy._hypergraph
assert H._net_attr == copy._net_attr

H1 = xgi.DiHypergraph()
H1.add_edge(([1, 2], [3]), id="x")
copy2 = H1.copy() # does not throw error because of str id
assert list(copy2.nodes) == list(H1.nodes)
assert list(copy2.edges) == list(H1.edges)
assert list(copy2.edges.members()) == list(H1.edges.members())
assert H1._hypergraph == copy2._hypergraph
assert H1._net_attr == copy2._net_attr


def test_copy_issue128():
Expand Down
10 changes: 5 additions & 5 deletions tests/core/test_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_constructor(edgelist5, dict5, incidence5, dataframe5):

def test_hypergraph_attrs():
H = xgi.Hypergraph()
assert H._hypergraph == {}
assert H._net_attr == {}
with pytest.raises(XGIError):
H["name"]
H = xgi.Hypergraph(name="test")
Expand Down Expand Up @@ -414,7 +414,7 @@ def test_copy(edgelist1):
assert list(copy.nodes) == list(H.nodes)
assert list(copy.edges) == list(H.edges)
assert list(copy.edges.members()) == list(H.edges.members())
assert H._hypergraph == copy._hypergraph
assert H._net_attr == copy._net_attr

H.add_node(10)
assert list(copy.nodes) != list(H.nodes)
Expand All @@ -424,23 +424,23 @@ def test_copy(edgelist1):
assert list(copy.edges) != list(H.edges)

H["key2"] = "value2"
assert H._hypergraph != copy._hypergraph
assert H._net_attr != copy._net_attr

copy.add_node(10)
copy.add_edge([1, 3, 5])
copy["key2"] = "value2"
assert list(copy.nodes) == list(H.nodes)
assert list(copy.edges) == list(H.edges)
assert list(copy.edges.members()) == list(H.edges.members())
assert H._hypergraph == copy._hypergraph
assert H._net_attr == copy._net_attr

H1 = xgi.Hypergraph()
H1.add_edge((1, 2), id="x")
copy2 = H1.copy() # does not throw error because of str id
assert list(copy2.nodes) == list(H1.nodes)
assert list(copy2.edges) == list(H1.edges)
assert list(copy2.edges.members()) == list(H1.edges.members())
assert H1._hypergraph == copy2._hypergraph
assert H1._net_attr == copy2._net_attr


def test_copy_issue128():
Expand Down
8 changes: 4 additions & 4 deletions tests/core/test_simplicialcomplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def test_copy(edgelist1):
assert list(copy.nodes) == list(H.nodes)
assert list(copy.edges) == list(H.edges)
assert list(copy.edges.members()) == list(H.edges.members())
assert H._hypergraph == copy._hypergraph
assert H._net_attr == copy._net_attr

H.add_node(10)
assert list(copy.nodes) != list(H.nodes)
Expand All @@ -399,23 +399,23 @@ def test_copy(edgelist1):
assert list(copy.edges) != list(H.edges)

H["key2"] = "value2"
assert H._hypergraph != copy._hypergraph
assert H._net_attr != copy._net_attr

copy.add_node(10)
copy.add_simplex([1, 3, 5])
copy["key2"] = "value2"
assert list(copy.nodes) == list(H.nodes)
assert list(copy.edges) == list(H.edges)
assert list(copy.edges.members()) == list(H.edges.members())
assert H._hypergraph == copy._hypergraph
assert H._net_attr == copy._net_attr

H1 = xgi.SimplicialComplex()
H1.add_simplex((1, 2), id="x")
copy2 = H1.copy() # does not throw error because of str id
assert list(copy2.nodes) == list(H1.nodes)
assert list(copy2.edges) == list(H1.edges)
assert list(copy2.edges.members()) == list(H1.edges.members())
assert H1._hypergraph == copy2._hypergraph
assert H1._net_attr == copy2._net_attr


def test_duplicate_edges(edgelist1):
Expand Down
2 changes: 1 addition & 1 deletion tests/readwrite/test_json.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tempfile
from os.path import join

import pytest
from os.path import join

import xgi
from xgi.exception import XGIError
Expand Down
10 changes: 5 additions & 5 deletions xgi/convert/higher_order_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ def to_hypergraph(data, create_using=None):
H.add_nodes_from((n, attr) for n, attr in data.nodes.items())
ee = data.edges
H.add_edges_from((ee.members(e), e, deepcopy(attr)) for e, attr in ee.items())
H._hypergraph = deepcopy(data._hypergraph)
H._net_attr = deepcopy(data._net_attr)
return H

elif isinstance(data, DiHypergraph):
H = empty_hypergraph(create_using)
H.add_nodes_from((n, attr) for n, attr in data.nodes.items())
ee = data.edges
H.add_edges_from((ee.members(e), e, deepcopy(attr)) for e, attr in ee.items())
H._hypergraph = deepcopy(data._hypergraph)
H._net_attr = deepcopy(data._net_attr)
if not isinstance(create_using, DiHypergraph):
return H

Expand All @@ -86,7 +86,7 @@ def to_hypergraph(data, create_using=None):
H.add_nodes_from((n, attr) for n, attr in data.nodes.items())
ee = data.edges
H.add_edges_from((ee.members(e), e, deepcopy(attr)) for e, attr in ee.items())
H._hypergraph = deepcopy(data._hypergraph)
H._net_attr = deepcopy(data._net_attr)
return H

elif isinstance(data, list):
Expand Down Expand Up @@ -161,7 +161,7 @@ def to_dihypergraph(data, create_using=None):
H.add_nodes_from((n, attr) for n, attr in data.nodes.items())
ee = data.edges
H.add_edges_from((ee.dimembers(e), e, deepcopy(attr)) for e, attr in ee.items())
H._hypergraph = deepcopy(data._hypergraph)
H._net_attr = deepcopy(data._net_attr)
if not isinstance(create_using, DiHypergraph):
return H

Expand Down Expand Up @@ -222,7 +222,7 @@ def to_simplicial_complex(data, create_using=None):
H.add_simplices_from(
(ee.members(e), e, deepcopy(attr)) for e, attr in ee.items()
)
H._hypergraph = deepcopy(data._hypergraph)
H._net_attr = deepcopy(data._net_attr)
return H

elif isinstance(data, Hypergraph):
Expand Down
4 changes: 2 additions & 2 deletions xgi/convert/hypergraph_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def to_hypergraph_dict(H):
data["type"] = get_network_type(H)
# name always gets written (default is an empty string)
data["hypergraph-data"] = {}
data["hypergraph-data"].update(H._hypergraph)
data["hypergraph-data"].update(H._net_attr)

# get node data
data["node-data"] = {str(idx): H.nodes[idx] for idx in H.nodes}
Expand Down Expand Up @@ -99,7 +99,7 @@ def from_hypergraph_dict(data, nodetype=None, edgetype=None, max_order=None):
"""
H = empty_hypergraph()
try:
H._hypergraph.update(data["hypergraph-data"])
H._net_attr.update(data["hypergraph-data"])
except KeyError:
raise XGIError("Failed to get hypergraph data attributes.")

Expand Down
36 changes: 19 additions & 17 deletions xgi/core/dihypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
This is currently an experimental feature.

"""

from collections.abc import Hashable, Iterable
from copy import copy, deepcopy
from itertools import count
Expand Down Expand Up @@ -81,11 +82,12 @@ class DiHypergraph:
>>> [sorted(e) for e in DH.edges.members()]
[[1, 2, 3, 4], [5, 6, 7, 8]]
"""

_node_dict_factory = IDDict
_node_attr_dict_factory = IDDict
_hyperedge_dict_factory = IDDict
_hyperedge_attr_dict_factory = IDDict
_hypergraph_attr_dict_factory = dict
_edge_dict_factory = IDDict
_edge_attr_dict_factory = IDDict
_net_attr_dict_factory = dict

def __getstate__(self):
"""Function that allows pickling.
Expand All @@ -103,7 +105,7 @@ def __getstate__(self):
"""
return {
"_edge_uid": self._edge_uid,
"_hypergraph": self._hypergraph,
"_net_attr": self._net_attr,
"_node": self._node,
"_node_attr": self._node_attr,
"_edge": self._edge,
Expand All @@ -124,7 +126,7 @@ def __setstate__(self, state):
This allows the python multiprocessing module to be used.
"""
self._edge_uid = state["_edge_uid"]
self._hypergraph = state["_hypergraph"]
self._net_attr = state["_net_attr"]
self._node = state["_node"]
self._node_attr = state["_node_attr"]
self._edge = state["_edge"]
Expand All @@ -134,13 +136,13 @@ def __setstate__(self, state):

def __init__(self, incoming_data=None, **attr):
self._edge_uid = count()
self._hypergraph = self._hypergraph_attr_dict_factory()
self._net_attr = self._net_attr_dict_factory()

self._node = self._node_dict_factory()
self._node_attr = self._node_attr_dict_factory()

self._edge = self._hyperedge_dict_factory()
self._edge_attr = self._hyperedge_attr_dict_factory()
self._edge = self._edge_dict_factory()
self._edge_attr = self._edge_attr_dict_factory()

self._nodeview = DiNodeView(self)
"""A :class:`~xgi.core.direportviews.DiNodeView` of the directed hypergraph."""
Expand All @@ -154,7 +156,7 @@ def __init__(self, incoming_data=None, **attr):
from ..convert import to_dihypergraph

to_dihypergraph(incoming_data, create_using=self)
self._hypergraph.update(attr) # must be after convert
self._net_attr.update(attr) # must be after convert

def __str__(self):
"""Returns a short summary of the directed hypergraph.
Expand Down Expand Up @@ -217,13 +219,13 @@ def __len__(self):
def __getitem__(self, attr):
"""Read dihypergraph attribute."""
try:
return self._hypergraph[attr]
return self._net_attr[attr]
except KeyError:
raise XGIError("This attribute has not been set.")

def __setitem__(self, attr, val):
"""Write dihypergraph attribute."""
self._hypergraph[attr] = val
self._net_attr[attr] = val

def __getattr__(self, attr):
stat = getattr(self.nodes, attr, None)
Expand Down Expand Up @@ -557,7 +559,7 @@ def add_edge(self, members, id=None, **attr):
self._node[node]["in"].add(uid)
self._edge[uid]["out"].add(node)

self._edge_attr[uid] = self._hyperedge_attr_dict_factory()
self._edge_attr[uid] = self._edge_attr_dict_factory()
self._edge_attr[uid].update(attr)

if id: # set self._edge_uid correctly
Expand Down Expand Up @@ -686,7 +688,7 @@ def add_edges_from(self, ebunch_to_add, **attr):
self._node[n] = {"in": set(), "out": set()}
self._node_attr[n] = self._node_attr_dict_factory()
self._node[n]["out"].add(id)
self._edge_attr[id] = self._hyperedge_attr_dict_factory()
self._edge_attr[id] = self._edge_attr_dict_factory()

for n in head:
if n not in self._node:
Expand Down Expand Up @@ -759,7 +761,7 @@ def add_edges_from(self, ebunch_to_add, **attr):
self._node[node]["in"].add(id)
self._edge[id]["out"].add(node)

self._edge_attr[id] = self._hyperedge_attr_dict_factory()
self._edge_attr[id] = self._edge_attr_dict_factory()
self._edge_attr[id].update(attr)
self._edge_attr[id].update(eattr)

Expand Down Expand Up @@ -899,7 +901,7 @@ def clear(self, hypergraph_attr=True):
self._edge.clear()
self._edge_attr.clear()
if hypergraph_attr:
self._hypergraph.clear()
self._net_attr.clear()

def copy(self):
"""A deep copy of the dihypergraph.
Expand All @@ -920,7 +922,7 @@ def copy(self):
(e, id, deepcopy(self.edges[id]))
for id, e in ee.dimembers(dtype=dict).items()
)
cp._hypergraph = deepcopy(self._hypergraph)
cp._net_attr = deepcopy(self._net_attr)

cp._edge_uid = copy(self._edge_uid)

Expand All @@ -944,7 +946,7 @@ def cleanup(self, isolates=False, relabel=True, in_place=True):
(e, id, deepcopy(temp.edges[id]))
for id, e in ee.dimembers(dtype=dict).items()
)
self._hypergraph = deepcopy(temp._hypergraph)
self._net_attr = deepcopy(temp._net_attr)
else:
DH = self.copy()
if not isolates:
Expand Down
2 changes: 1 addition & 1 deletion xgi/core/globalviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def subhypergraph(H, nodes=None, edges=None, keep_isolates=True):
"""
new = H.__class__()

new._hypergraph = H._hypergraph.copy()
new._net_attr = H._net_attr.copy()
nodes = set(H.nodes) if nodes is None else (set(nodes) & set(H.nodes))
edges = set(H.edges) if edges is None else (set(edges) & set(H.edges))

Expand Down
Loading
Loading