Skip to content

Commit

Permalink
Pull request #107: updated Hypergraph.dual() to deepcopy the datafram…
Browse files Browse the repository at this point in the history
…es to separate base data from H and H.dual()

Merge in HYP/hypernetx from hotfix/hyp-347 to master

* commit '89917e5cebb48cbe655a3882320eb848968cf625':
  bump: version 2.0.3 → 2.0.4
  Fix formatting
  put deepcopy back
  added test for dual not switching column names
  changed deepcopy to pandas copy deep=True
  HYP-347 Fix formatting
  updated Hypergraph.dual() to deepcopy the dataframes to separate base data from H and H.dual()
  • Loading branch information
brendapraggastis authored and bonicim committed Aug 30, 2023
2 parents 97ccb93 + 89917e5 commit f861138
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .cz.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.commitizen]
name = "cz_conventional_commits"
version = "2.0.3"
version = "2.0.4"
version_files = [
"setup.py",
"docs/source/conf.py",
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import os


__version__ = "2.0.3"
__version__ = "2.0.4"


# If extensions (or modules to document with autodoc) are in another directory,
Expand Down
2 changes: 1 addition & 1 deletion hypernetx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
from hypernetx.utils import *
from hypernetx.utils.toys import *

__version__ = "2.0.3"
__version__ = "2.0.4"
17 changes: 10 additions & 7 deletions hypernetx/classes/hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pickle
import warnings
from copy import deepcopy
from collections import defaultdict
from collections.abc import Sequence, Iterable
from typing import Optional, Any, TypeVar, Union, Mapping, Hashable
Expand Down Expand Up @@ -1242,18 +1243,20 @@ def dual(self, name=None, switch_names=True):
: hypergraph
"""
dfp = self.edges.properties.copy()
if "level" in dfp.columns:
dfp = dfp.reset_index()
dfp.level = dfp.level.apply(lambda x: 1 * (x == 0))
dfp = dfp.set_index(["level", "id"])
dfp = deepcopy(self.edges.properties)
dfp = dfp.reset_index()
dfp.level = dfp.level.apply(lambda x: 1 * (x == 0))
dfp = dfp.set_index(["level", "id"])

edge, node, wt = self._edge_col, self._node_col, self._cell_weight_col
df = self.dataframe.copy()
df = deepcopy(self.dataframe)
cprops = [col for col in df.columns if not col in [edge, node, wt]]

df[[edge, node]] = df[[node, edge]]
if edge != "edges" or node != "nodes":
if switch_names == True and not (
self._edge_col == "edges" and self._node_col == "nodes"
):
# if switch_names == False or (self._edge_col == 'edges' and self._node_col == 'nodes'):
df = df.rename(columns={edge: self._node_col, node: self._edge_col})
node = self._edge_col
edge = self._node_col
Expand Down
2 changes: 1 addition & 1 deletion hypernetx/classes/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def triloop2():

@pytest.fixture
def sbs_hypergraph(sbs):
return Hypergraph(sbs.edgedict, name="sbsh")
return Hypergraph(sbs.edgedict, name="sbsh", edge_col="edges", node_col="nodes")


@pytest.fixture
Expand Down
11 changes: 11 additions & 0 deletions hypernetx/classes/tests/test_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ def test_dual(sbs_hypergraph):
HD = H.dual()
assert set(H.nodes) == set(HD.edges)
assert set(H.edges) == set(HD.nodes)
assert list(H.dataframe.columns) == list(HD.dataframe.columns)


def test_dual_again(sbs_edgedict):
H = Hypergraph(sbs_edgedict, edge_col="Types", node_col="Values")
assert list(H.dataframe.columns[0:2]) == ["Types", "Values"]
assert list(H.dual().dataframe.columns[0:2]) == ["Values", "Types"]
assert list(H.dual(switch_names=False).dataframe.columns[0:2]) == [
"Types",
"Values",
]


@pytest.mark.filterwarnings("ignore:No 3-path between ME and FN")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from setuptools import setup

__version__ = "2.0.3"
__version__ = "2.0.4"

setup(version=__version__)

0 comments on commit f861138

Please sign in to comment.