HyperNetX + Graphistry = 💪💪💪#
您可以通过下面的示例类快速探索使用Graphistry的HyperNetX图。
PNNL的HyperNetX是一个新的Python库,用于操作超图:其中一条边可以连接任意数量的节点。下面的辅助类以两种不同的模式将HyperNetX图转换为2边图(节点/边属性表作为Pandas数据框):
hypernetx_to_graphistry_bipartite(hnx_graph)
:将每个超边和超节点转换为节点。它们形成一个二分图:每当一个超边包含一个超节点时,就从超边的节点创建一条边到超节点的节点。
例如:超边
0: ['a', 'b', 'c']
=> 边(0, 'a')
, 边(0, 'b')
, 边(0, 'c')
hypernetx_to_graphistry_nodes(hnx_graph)
:将每个超节点转换为一个节点,并且每当两个超节点共享同一个超边时,在它们对应的节点之间创建一条边
为了强调边是无向的,库将边的曲率设置为0(直线)
例如:超边
0: ['a', 'b', 'c']
=> 边('a', 'b')
, 边('a', 'c')
, 边('b', 'c')
安装#
Graphistry Core 发行版中已预安装的依赖项
[1]:
# ! pip install hypernetx -q
# ! pip install graphistry -q
库#
[2]:
import pandas as pd
class HyperNetXG:
def __init__(self, graphistry):
self.graphistry = graphistry
def normalize_id(self, id):
t = type(id)
if t == float or t == int:
return '__id__' + str(id)
return str(id)
def hypernetx_to_graphistry_bipartite(self, h):
nodes_df = pd.concat(
[pd.DataFrame({
'node': [self.normalize_id(x) for x in list(H.nodes)],
'type': 'hypernode'}),
pd.DataFrame({
'node': [self.normalize_id(x) for x in H.edges],
'type': 'hyperedge'})],
ignore_index=True,
sort=False)
edges_df = pd.concat(
[ pd.DataFrame({'src': [], 'dst': []}) ] +
[
pd.DataFrame({
'src': self.normalize_id(k),
'dst': [self.normalize_id(x) for x in list(es)]
})
for k, es in H.incidence_dict.items()
], ignore_index=True, sort=False)
return self.graphistry.bind(
source='src',
destination='dst',
node='node').nodes(nodes_df).edges(edges_df)
def __hyperedge_to_graph(self, k, es):
lst = list(es)
edges_df = pd.concat([
pd.DataFrame({'src': [], 'dst': [], 'hyperedge': []})] + [
pd.DataFrame({
'src': self.normalize_id(lst[i]),
'dst': [self.normalize_id(x) for x in lst[i+1:]],
'hyperedge': self.normalize_id(k)})
for i in range(0, len(lst))
], ignore_index=True, sort=False)
return edges_df
def hypernetx_to_graphistry_nodes(self, h):
hg = self.hypernetx_to_graphistry_bipartite(h)
nodes_df = pd.DataFrame({
'node': [self.normalize_id(x) for x in list(h.nodes)],
'type': 'hypernode'})
edges_df = pd.concat(
[pd.DataFrame({'src': [], 'dst': [], 'hyperedge': []})] +
[
self.__hyperedge_to_graph(k, es)
for (k, es) in h.incidence_dict.items()
])
return self.graphistry.bind(
source='src',
destination='dst',
node='node').settings(url_params={'edgeCurvature': 0}).nodes(nodes_df).edges(edges_df)
演示#
初始化#
[3]:
import hypernetx as hnx
import graphistry
# To specify Graphistry account & server, use:
# graphistry.register(api=3, username='...', password='...', protocol='https', server='hub.graphistry.com')
# For more options, see https://github.com/graphistry/pygraphistry#configure
scenes = [
('FN', 'TH'),
('TH', 'JV'),
('BM', 'FN', 'JA'),
('JV', 'JU', 'CH', 'BM'),
('JU', 'CH', 'BR', 'CN', 'CC', 'JV', 'BM'),
('TH', 'GP'),
('GP', 'MP'),
('MA', 'GP')
]
H = hnx.Hypergraph(dict(enumerate(scenes)))
hg = HyperNetXG(graphistry)
hypernetx_to_graphistry_bipartite#
展平为Pandas数据框/Graphistry并检查:
[4]:
g = hg.hypernetx_to_graphistry_bipartite(H)
g._nodes.sample(3)
[4]:
node | type | |
---|---|---|
18 | __id__5 | hyperedge |
17 | __id__4 | hyperedge |
15 | __id__2 | hyperedge |
[5]:
g._edges.sample(3)
[5]:
src | dst | |
---|---|---|
18 | __id__5 | TH |
20 | __id__6 | MP |
10 | __id__3 | CH |
[6]:
g.plot()
[6]:
[7]:
hg.hypernetx_to_graphistry_bipartite(H.dual())._edges.sample(3)
[7]:
src | dst | |
---|---|---|
16 | __id__4 | CN |
17 | __id__4 | BR |
14 | __id__4 | BM |
[8]:
hg.hypernetx_to_graphistry_bipartite(H.dual()).plot()
[8]:
hypernetx_to_graphistry_nodes#
[9]:
g = hg.hypernetx_to_graphistry_nodes(H)
g._edges.sample(3)
[9]:
src | dst | hyperedge | |
---|---|---|---|
16 | BM | CN | __id__4 |
0 | JA | BM | __id__2 |
2 | JU | CH | __id__3 |
[10]:
hg.hypernetx_to_graphistry_nodes(H).plot()
[10]:
[11]:
hg.hypernetx_to_graphistry_nodes(H.dual()).plot()
[11]: