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

Fix #224 uid counter initialisation #225

Merged
merged 14 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 25 additions & 2 deletions tests/classes/test_hypergraph.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from itertools import count
maximelucas marked this conversation as resolved.
Show resolved Hide resolved

import pytest

import xgi
Expand Down Expand Up @@ -265,6 +267,10 @@ def test_add_edges_from_format1():
assert list(H.edges) == [e[1] for e in edges]
assert H.edges.members(dtype=dict) == {e[1]: e[0] for e in edges}

# check counter
H.add_edge([1, 9, 2])
assert H.edges.members(101) == {1, 9, 2}


def test_add_edges_from_format2():
edges = [
Expand All @@ -278,6 +284,9 @@ def test_add_edges_from_format2():
assert H.edges.members() == [e[0] for e in edges]
for idx, e in enumerate(H.edges):
assert H.edges[e] == edges[idx][1]
# check counter
H.add_edge([1, 9, 2])
assert H.edges.members(3) == {1, 9, 2}


def test_add_edges_from_format3():
Expand All @@ -292,14 +301,20 @@ def test_add_edges_from_format3():
assert H.edges.members() == [e[0] for e in edges]
for idx, e in enumerate(H.edges):
assert H.edges[e] == edges[idx][2]
# check counter
H.add_edge([1, 9, 2])
assert H.edges.members(0) == {1, 9, 2}


def test_add_edges_from_dict():
edges = {"one": [0, 1], "two": [1, 2], "three": [2, 3, 4]}
edges = {"one": [0, 1], "two": [1, 2], 2: [2, 3, 4]}
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.edges) == ["one", "two", "three"]
assert list(H.edges) == ["one", "two", 2]
assert H.edges.members() == [set(edges[e]) for e in edges]
# check counter
H.add_edge([1, 9, 2])
assert H.edges.members(3) == {1, 9, 2}


def test_add_edges_from_attr_precedence():
Expand Down Expand Up @@ -377,6 +392,14 @@ def test_copy(edgelist1):
assert list(copy.edges.members()) == list(H.edges.members())
assert H._hypergraph == copy._hypergraph

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


def test_copy_issue128():
# see https://github.com/ComplexGroupInteractions/xgi/issues/128
Expand Down
41 changes: 40 additions & 1 deletion xgi/classes/hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,23 @@ def add_edges_from(self, ebunch_to_add, **attr):
self._node_attr[n] = self._node_attr_dict_factory()
self._node[n].add(uid)
self._edge_attr[uid] = self._hyperedge_attr_dict_factory()

# If we don't set the start of self._edge_uid correctly, it will start at 0,
# which will overwrite any existing edges when calling add_edge(). First, we
# use the somewhat convoluted float(e).is_integer() instead of using
# isinstance(e, int) because there exist integer-like numeric types (such as
# np.int32) which fail the isinstance() check.
edges_with_int_id = [
int(e)
for e in self.edges
if (not isinstance(e, str)) and float(e).is_integer()
]

# Then, we set the start at one plus the maximum edge ID that is an integer,
# because count() only yields integer IDs.
start = max(edges_with_int_id) + 1 if edges_with_int_id else 0
self._edge_uid = count(start=start)

return

# in formats 1-4 we only know that ebunch_to_add is an iterable, so we iterate
Expand Down Expand Up @@ -668,6 +685,24 @@ def add_edges_from(self, ebunch_to_add, **attr):
try:
e = next(new_edges)
except StopIteration:

if format2 or format4:
# If we don't set the start of self._edge_uid correctly, it will start at 0,
# which will overwrite any existing edges when calling add_edge(). First, we
# use the somewhat convoluted float(e).is_integer() instead of using
# isinstance(e, int) because there exist integer-like numeric types (such as
# np.int32) which fail the isinstance() check.
edges_with_int_id = [
maximelucas marked this conversation as resolved.
Show resolved Hide resolved
int(e)
for e in self.edges
if (not isinstance(e, str)) and float(e).is_integer()
]

# Then, we set the start at one plus the maximum edge ID that is an integer,
# because count() only yields integer IDs.
start = max(edges_with_int_id) + 1 if edges_with_int_id else 0
self._edge_uid = count(start=start)

break

def add_weighted_edges_from(self, ebunch, weight="weight", **attr):
Expand Down Expand Up @@ -989,7 +1024,11 @@ def copy(self):
# use the somewhat convoluted float(e).is_integer() instead of using
# isinstance(e, int) because there exist integer-like numeric types (such as
# np.int32) which fail the isinstance() check.
edges_with_int_id = [int(e) for e in self.edges if float(e).is_integer()]
edges_with_int_id = [
int(e)
for e in self.edges
if (not isinstance(e, str)) and float(e).is_integer()
]

# Then, we set the start at one plus the maximum edge ID that is an integer,
# because count() only yields integer IDs.
Expand Down
34 changes: 34 additions & 0 deletions xgi/classes/simplicialcomplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,22 @@ def add_simplices_from(self, ebunch_to_add, max_order=None, **attr):
faces = self._subfaces(members)
self.add_simplices_from(faces)

# If we don't set the start of self._edge_uid correctly, it will start at 0,
# which will overwrite any existing edges when calling add_edge(). First, we
# use the somewhat convoluted float(e).is_integer() instead of using
# isinstance(e, int) because there exist integer-like numeric types (such as
# np.int32) which fail the isinstance() check.
edges_with_int_id = [
int(e)
for e in self.edges
if (not isinstance(e, str)) and float(e).is_integer()
]

# Then, we set the start at one plus the maximum edge ID that is an integer,
# because count() only yields integer IDs.
start = max(edges_with_int_id) + 1 if edges_with_int_id else 0
self._edge_uid = count(start=start)

return

# in formats 1-4 we only know that ebunch_to_add is an iterable, so we iterate
Expand Down Expand Up @@ -478,6 +494,24 @@ def add_simplices_from(self, ebunch_to_add, max_order=None, **attr):
try:
e = next(new_edges)
except StopIteration:

if format2 or format4:
maximelucas marked this conversation as resolved.
Show resolved Hide resolved
# If we don't set the start of self._edge_uid correctly, it will start at 0,
# which will overwrite any existing edges when calling add_edge(). First, we
# use the somewhat convoluted float(e).is_integer() instead of using
# isinstance(e, int) because there exist integer-like numeric types (such as
# np.int32) which fail the isinstance() check.
edges_with_int_id = [
int(e)
for e in self.edges
if (not isinstance(e, str)) and float(e).is_integer()
]

# Then, we set the start at one plus the maximum edge ID that is an integer,
# because count() only yields integer IDs.
start = max(edges_with_int_id) + 1 if edges_with_int_id else 0
self._edge_uid = count(start=start)

break

def close(self):
Expand Down
8 changes: 2 additions & 6 deletions xgi/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
from numpy import matrix, ndarray
from scipy.sparse import coo_matrix, csc_matrix, csr_matrix, lil_matrix

from .classes import (
Hypergraph,
SimplicialComplex,
maximal_simplices,
set_edge_attributes,
)
from .classes import (Hypergraph, SimplicialComplex, maximal_simplices,
set_edge_attributes)
from .exception import XGIError
from .generators import empty_hypergraph, empty_simplicial_complex
from .linalg import adjacency_matrix, incidence_matrix
Expand Down
2 changes: 1 addition & 1 deletion xgi/dynamics/synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import numpy as np

import xgi
from ..exception import XGIError

from ..exception import XGIError

__all__ = [
"compute_kuramoto_order_parameter",
Expand Down
8 changes: 5 additions & 3 deletions xgi/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def dual_dict(edge_dict):
return dict(node_dict)


def powerset(iterable, include_empty=False, include_full=False, include_singletons=True):
def powerset(
iterable, include_empty=False, include_full=False, include_singletons=True
):
"""Returns all possible subsets of the elements in iterable, with options
to include the empty set and the set containing all elements.

Expand All @@ -60,7 +62,7 @@ def powerset(iterable, include_empty=False, include_full=False, include_singleto
Notes
-----
include_empty overrides include_singletons if True: singletons will always
be included if the empty set is.
be included if the empty set is.

Examples
--------
Expand All @@ -72,7 +74,7 @@ def powerset(iterable, include_empty=False, include_full=False, include_singleto
"""

start = 1 if include_singletons else 2
start = 0 if include_empty else start # overrides include_singletons if True
start = 0 if include_empty else start # overrides include_singletons if True
end = 1 if include_full else 0

s = list(iterable)
Expand Down