Skip to content

Commit

Permalink
Pull request pnnl#154: difference_function fix
Browse files Browse the repository at this point in the history
Merge in HYP/hypernetx from diff_fix to develop

* commit '307e0629c91fd2f1a8069d2adfb7da231d036a25':
  Add tests for difference method
  fixed difference of hypergraphs as it removed properties and shouldn't have
  Remove setup.py
  Run formatter
  Update tox.ini
  Update ci.yml
  Fix github workflows
  Cleanup required dependencies
  Update cz file
  Update GH workflows
  Cleanup Makefile
  testing new docs-deps
  testing
  Modify tox to integrate with poetry
  Add poetry
  • Loading branch information
brendapraggastis authored and bonicim committed Apr 10, 2024
2 parents 370e671 + 307e062 commit e6220d2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
13 changes: 3 additions & 10 deletions hypernetx/classes/hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2117,27 +2117,20 @@ def sum(self, other):

def difference(self, other):
"""
Concatenate incidences from two hypergraphs, removing duplicates and
dropping duplicate property data in the order of addition.
Remove incidence pairs from self that belong to other.
Parameters
----------
other : Hypergraph
Returns
-------
Hypergraph
: Hypergraph
"""
df = self.incidences.properties
odf = other.incidences.properties
ndf = df.loc[~df.index.isin(odf.index.tolist())]
edf = self.edges.properties
oedf = other.edges.properties
nedf = edf.loc[~edf.index.isin(oedf.index.tolist())]
nddf = self.nodes.properties
onddf = other.nodes.properties
nnddf = nddf.loc[~nddf.index.isin(onddf.index.tolist())]
return self._construct_hyp_from_stores(
ndf, edge_ps=PropertyStore(nedf), node_ps=PropertyStore(nnddf)
ndf, edge_ps=self.edges._property_store, node_ps=self.nodes._property_store
)
48 changes: 48 additions & 0 deletions hypernetx/classes/tests/test_hypergraph.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import OrderedDict

import pytest
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -371,3 +373,49 @@ def test_construct_hypergraph_empty_dict():
def test_static_hypergraph_s_connected_components(lesmis):
H = Hypergraph(lesmis.edgedict)
assert {7, 8} in list(H.s_connected_components(edges=True, s=4))


def test_difference_on_same_hypergraph(lesmis):
hg = Hypergraph(lesmis.edgedict)
hg_copy = Hypergraph(lesmis.edgedict)

hg_diff = hg - hg_copy

assert len(hg_diff) == 0
assert len(hg_diff.nodes) == 0
assert len(hg_diff.edges) == 0
assert hg_diff.shape == (0, 0)
assert hg_diff.incidence_dict == {}


def test_difference_on_empty_hypergraph(sbs_hypergraph):
hg_empty = Hypergraph()

hg_diff = sbs_hypergraph - hg_empty

assert len(hg_diff) == 7
assert len(hg_diff.nodes) == 7
assert len(hg_diff.edges) == 6
assert hg_diff.shape == (7, 6)

edges = ["I", "L", "O", "P", "R", "S"]
assert all(uid in edges for uid in hg_diff.incidence_dict.keys())


def test_difference_on_similar_hypergraph(sbs_hypergraph):
a, c, e, k, t1, t2, v = ("A", "C", "E", "K", "T1", "T2", "V")
l, o, p, r, s = ("L", "O", "P", "R", "S")
data = OrderedDict(
[(p, {a, c, k}), (r, {a, e}), (s, {a, k, t2, v}), (l, {c, e}), (o, {t1, t2})]
)
hg_similar = Hypergraph(data, edge_col="edges", node_col="nodes")

hg_diff = sbs_hypergraph - hg_similar

assert len(hg_diff) == 2
assert len(hg_diff.nodes) == 2
assert len(hg_diff.edges) == 1
assert hg_diff.shape == (2, 1)
print(hg_diff.incidence_dict.keys())
edges = ["I"]
assert all(uid in edges for uid in hg_diff.incidence_dict.keys())

0 comments on commit e6220d2

Please sign in to comment.