Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if key:
M = key(M)
## Remove empty column indices from M columns and edgenames
colidx = np.array([jdx for jdx in range(M.shape[1]) if any(M[:,jdx])])
colidxsum = np.sum(colidx)
if not colidxsum:
return Hypergraph()
else:
M = M[:,colidx]
edgenames = edgenames[colidx]
edict = dict()
## Create an EntitySet of edges from M
for jdx,e in enumerate(edgenames):
edict[e] = nodenames[[idx for idx in range(M.shape[0]) if M[idx,jdx]]]
return Hypergraph(edict,name=name)
def remove_singletons(self, name=None):
"""
Constructs clone of hypergraph with singleton edges removed.
Parameters
----------
name: str, optional, default: None
Returns
-------
new hypergraph : Hypergraph
"""
singles = self.singletons()
edgeset = [e for e in self._edges if e not in singles]
return Hypergraph({e:self.edges[e] for e in edgeset},name)
if edge_names is not None:
edgenames = np.array(edge_names)
if len(edgenames) != M.shape[1]:
raise HyperNetXError('Number of edge_names does not match number of columns.')
else:
edgenames = np.array([f'e{jdx}' for jdx in range(M.shape[1])])
## apply boolean key if available
if key:
M = key(M)
## Remove empty column indices from M columns and edgenames
colidx = np.array([jdx for jdx in range(M.shape[1]) if any(M[:,jdx])])
colidxsum = np.sum(colidx)
if not colidxsum:
return Hypergraph()
else:
M = M[:,colidx]
edgenames = edgenames[colidx]
edict = dict()
## Create an EntitySet of edges from M
for jdx,e in enumerate(edgenames):
edict[e] = nodenames[[idx for idx in range(M.shape[0]) if M[idx,jdx]]]
return Hypergraph(edict,name=name)
If use_reps=True the frozen sets will be replaced with a representative
from the equivalence classes.
Example
-------
>>> h = Hypergraph(EntitySet('example',elements=[Entity('E1', ['a','b']),Entity('E2',['a','b'])]))
>>> h.incidence_dict
{'E1': {'a', 'b'}, 'E2': {'a', 'b'}}
>>> h.collapse_edges().incidence_dict
{frozenset({'E1', 'E2'}): {'a', 'b'}}
>>> h.collapse_edges(use_reps=True).incidence_dict
{('E1', 2): {'a', 'b'}}
"""
return Hypergraph(self.edges.collapse_identical_elements('_',use_reps=use_reps, return_counts=return_counts), name)
Returns
-------
new hypergraph : Hypergraph
"""
memberships = set()
for node in nodeset:
if node in self.nodes:
memberships.update(set(self.nodes[node].memberships))
newedgeset = dict()
for e in memberships:
if e in self.edges:
temp = self.edges[e].uidset.intersection(nodeset)
if temp:
newedgeset[e] = Entity(e,temp,**self.edges[e].properties)
return Hypergraph(newedgeset,name)
"""
Constructs a hypergraph using a subset of the edges in hypergraph
Parameters
----------
edgeset: iterable of hashables or Entities
A subset of elements of the hypergraph edges
name: str, optional, default: None
Returns
-------
new hypergraph : Hypergraph
"""
name = name or self.name
return Hypergraph({e:self.edges[e] for e in edgeset},name)
thdict = dict()
for e in temp.edges:
thdict[e] = temp.edges[e].uidset
tops = dict()
for e in temp.edges:
flag = True
old_tops = dict(tops)
for top in old_tops:
if thdict[e].issubset(thdict[top]):
flag = False
break
elif set(thdict[top]).issubset(thdict[e]):
del tops[top]
if flag:
tops.update({e : thdict[e]})
return Hypergraph(tops,name)
Returns
-------
edge_adjacency_matrix : scipy.sparse.csr.csr_matrix
column dictionary : dict
Notes
-----
This is also the adjacency matrix for the line graph.
Two edges are s-adjacent if they share at least s nodes.
If index=True, returns a dictionary column_index:edge_uid
"""
M = self.incidence_matrix(index=index)
if index:
return Hypergraph.__incidence_to_adjacency(M[0].transpose(),s=s,weighted=weighted), M[2]
else:
return Hypergraph.__incidence_to_adjacency(M.transpose(),s=s,weighted=weighted)
Constructs a new hypergraph with roles of edges and nodes of hypergraph reversed.
Parameters
----------
name : hashable
Returns
-------
dual : hypergraph
"""
from collections import defaultdict
E = defaultdict(list)
for k,v in self.edges.incidence_dict.items():
for n in v:
E[n].append(k)
return Hypergraph(E,name=name)