NetworkX多向图可能吗?NetworkX

2023-09-11 04:52:36 作者:毁于╮一瞬间↘

我有哪些,我试图找出最佳的图形重新presentation网络。我不是图理论家,而是一位生物学家,所以请原谅我缺乏技术性这里。

I have a network for which I'm trying to figure out the best possible graph representation. I'm no graph theorist, but a biologist, so please pardon my lack of technicality here.

目前,网络可以被认为是如下:n个层的网络,每一层保持一组不同的节点之间的边缘。每个边缘被导向,并具有与之相关联的概率,但概率属性之前不使用后。每一层都被存储为一个单独的图形,为CSV文件,在邻接表重新presentation。

Currently, the network can be thought of as follows: "n" layers of networks, each layer holding a different set of edges between the nodes. Each edge is directed, and has a probability associated with it, but that probability property isn't used until later. Each layer is stored as a separate graph, as a CSV file, in an adjacency list representation.

使用邻接表重新presentation,我有一个摘要层,我在其中COM preSS所有的N层,每一层贡献的+1的值之间的权每个节点。这是目前存储为一个单独的图形,为CSV文件,在邻接表重新presentation。

Using an adjacency list representation, I have a "summary" layer, in which I compress all "n" layers, with each layer contributing a value of "+1" to the weights between each node. This is currently stored as a separate graph, as a CSV file, in an adjacency list representation.

如果有n个一对节点之间的边,然后在总结层,有边缘将具有n个的重量;只能有任何一对节点之间的n个或更少的边缘。

If there were "n" edges between a pair of nodes, then in the summary layer, there the edge would have a weight of "n"; there can only be "n" or fewer edges between any pair of nodes.

我也有一个全才层,其中仅包括有重量的n的边缘。同样,当前存储为CSV文件,在邻接表重新presentation。

I also have a "full-only" layer, which is only comprised of the edges that have weight "n". Similarly, currently stored as a CSV file, in an adjacency list representation.

最后,我有一个最可能完全只是层。在这一层中,概率踢对于每个全仅层的边缘,我相乘所有每个n个边缘(召回相关联的概率:满层是n的边缘上的总和,用概率各边)。

Finally, I have a "most probable full-only" layer. In this layer, the probabilities kick in. For each of the "full-only" layer edges, I multiply all of the probabilities associated with each of the n edges (recall: the "full" layer is the sum of "n" edges, each edge with a probability).

在我的这个网络的分析,有时它的方便,以便能够任意n个层和摘要层之间进行切换。然而,最方便的最小的存储格式(即没有$ P $对 - 计算任何内容)是存储各个边沿作为表(下面示出):

In my analysis of this network, sometimes it's convenient to be able to switch between any of the "n" layers and the "summary" layers. However, the most convenient minimal storage format (i.e. without pre-computing anything) is to store the individual edges as a table (illustrated below):

|Node 1 | Node 2 | Layer 1 Weight | Layer 2 Weight | ... | Layer n Weight |
|-------|--------|----------------|----------------|-----|----------------|
|  x    |   y    |   0.99         |       1.00     | ... |       1.00     |
|  x    |   z    |   0.98         |       1.00     | ... |       0.97     |
|  y    |   z    |   0 (no edge)  |       1.00     | ... |       1.00     |

我说,这种格式是方便的,因为我是能够产生这样的表很容易。

I say that this format is convenient, because I am able to generate such a table very easily.

因此​​,这里是我的问题:是否有可能在NetworkX存储这样的图(多层,导演对每一层)?如果有可能的话,我会想象能够编写函数来计算,在即时的摘要图中,全才图,最有可能完全只是图,因为,它们是彼此的子集。我还可以想像写作函数计算其他图形,例如,又结合互补套多个边成不具有充分的边缘进入每个节点的节点的图形。

So here's my question: is it possible in NetworkX to store such a graph (multi-layered, directed on each layer)? If it were possible, then I'd imagine being able to write functions to compute, on-the-fly, the "summary" graph, the "full-only" graph, and the "most probable full-only" graph, since they are subsets of one another. I can also imagine writing functions that compute other graphs, such as the graph that also incorporates complementary sets of multiple edges into the nodes that don't have full edges going into each node.

不过,检查NetworkX文档,我找不到像我要找的东西。尽我所能找到的是一个多图,它允许节点之间的多条边,但每条边都必须无向。我失去了一些东西呢?

However, checking the NetworkX documentation, I can't find anything like what I'm looking for. The best I could find is a "multigraph", which allows multiple edges between nodes, but each edge has to be undirected. Am I missing something here?

此外,有没有更好的再presentation什么我想达到什么目的?同样,我缺乏与图论的经验在这里,所以我可能失去了一些东西。非常感谢(提前),大家谁需要时间来作出回应!

Also, is there a better representation for what I'm trying to achieve? Again, I'm lacking experience with graph theory here, so I might be missing something. Many thanks (in advance) to everyone who takes time to respond!

推荐答案

有一个在NetworkX一个MultiDiGraph()对象可能工作在您的案件。 您可以存储多个有向边,每个任意属性。 节点也可具有任意特性。

There is a MultiDiGraph() object in NetworkX that might work in your case. You can store multiple directed edges, each with arbitrary attributes. The nodes can also have arbitrary attributes.

In [1]: import networkx as nx

In [2]: G = nx.MultiDiGraph()

In [3]: G.add_edge(1,2,color='green')

In [4]: G.add_edge(1,2,color='red')

In [5]: G.edges(data=True)
Out[5]: [(1, 2, {'color': 'green'}), (1, 2, {'color': 'red'})]

In [6]: G.node[1]['layer1']=17

In [7]: G.node[1]['layer2']=42

In [8]: G.nodes(data=True)
Out[8]: [(1, {'layer1': 17, 'layer2': 42}), (2, {})]

有一些辅助函数来获取和设置节点的属性,可能是​​有用的,例如:

There are some helper functions to get and set node attributes that might be useful, e.g.

In [9]: nx.get_node_attributes(G,'layer1')
Out[9]: {1: 17}
 
精彩推荐
图片推荐