Skip to content

Commit

Permalink
adding arithmetic for hypergraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
brendapraggastis committed Mar 27, 2024
1 parent f93504f commit 29cfed8
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions hypernetx/classes/hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2040,3 +2040,89 @@ def from_incidence_dataframe(
return dfnew
else:
return Hypergraph(dfnew, cell_weight_col="weight", name=name, **kwargs)

def __add__(self,other):
"""
Concatenate incidences from two hypergraphs, removing duplicates and
dropping duplicate property data in the order of addition.
Parameters
----------
other : Hypergraph
Returns
-------
Hypergraph
"""
return self.sum(other)

def __sub__(self,other):
"""
Concatenate incidences from two hypergraphs, removing duplicates and
dropping duplicate property data in the order of addition.
Parameters
----------
other : Hypergraph
Returns
-------
Hypergraph
"""
return self.difference(other)

def sum(self,other):
"""
Concatenate incidences from two hypergraphs, removing duplicates and
dropping duplicate property data in the order of addition.
Parameters
----------
other : Hypergraph
Returns
-------
Hypergraph
"""
df = self.dataframe
odf = other.dataframe
ndf = pd.concat([df,odf]).groupby(['edges','nodes']).agg('first')
edf = self.edges.dataframe
oedf = other.edges.dataframe
nedf = pd.concat([edf,oedf]).groupby('uid').agg('first')
nddf = self.nodes.dataframe
onddf = other.nodes.dataframe
nnddf = pd.concat([nddf,onddf]).groupby('uid').agg('first')
return self._construct_hyp_from_stores(ndf,edge_ps=PropertyStore(nedf),node_ps=PropertyStore(nnddf))

def difference(self,other):
"""
Concatenate incidences from two hypergraphs, removing duplicates and
dropping duplicate property data in the order of addition.
Parameters
----------
other : Hypergraph
Returns
-------
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))




0 comments on commit 29cfed8

Please sign in to comment.