ipycytoscape 和 networkx 对象交换
ipycytoscape & networkx interchange of objects
从 ipycytoscape 文档中我可以看到以下内容:
import ipycytoscape as cs
from ipycytoscape import *
import networkx as nx
# === create custom node class (inherits from cytoscape node class)
class CustomNode(cs.Node):
def __init__(self, name, classes=''):
super().__init__()
self.data['id'] = name
self.classes = classes
n1 = CustomNode("node 1", classes='class1')
n2 = CustomNode("node 2", classes='class2')
# === create graph, add custom nodes and an edge
G = nx.Graph()
G.add_node(n1)
G.add_node(n2)
G.add_edge(n1, n2, directed=True)
我的问题是基本问题,而不是寻求代码方面的帮助。
上面的 n1 和 n2 是用 class 继承自 ipycytoscape class CustomNode(cs.Node) 构建的。
但是它们作为节点添加到 networkx 图中 !!!
这怎么可能?
我的意思是一个ipycytoscape对象被添加到一个networkx对象中。
发件人:https://networkx.org/documentation/stable/tutorial.html#what-to-use-as-nodes-and-edges
You might notice that nodes and edges are not specified as NetworkX objects. This leaves you free to use meaningful items as nodes and edges. The most common choices are numbers or strings, but a node can be any hashable object (except None), and an edge can be associated with any object x using G.add_edge(n1, n2, object=x).
所以这不是 ipycytoscape 特有的,您可以使用任何自定义对象作为 networkx 的节点。像这样:
import networkx as nx
class myClass:
def __init__(self, something):
self.something = something
n1 = myClass(53)
n2 = myClass("node 2")
# === create graph, add custom nodes and an edge
G = nx.Graph()
G.add_node(n1)
G.add_node(n2)
G.add_edge(n1, n2, directed=True)
从 ipycytoscape Node 对象继承只是为了让它更容易与 ipycytoscape 一起工作。
从 ipycytoscape 文档中我可以看到以下内容:
import ipycytoscape as cs
from ipycytoscape import *
import networkx as nx
# === create custom node class (inherits from cytoscape node class)
class CustomNode(cs.Node):
def __init__(self, name, classes=''):
super().__init__()
self.data['id'] = name
self.classes = classes
n1 = CustomNode("node 1", classes='class1')
n2 = CustomNode("node 2", classes='class2')
# === create graph, add custom nodes and an edge
G = nx.Graph()
G.add_node(n1)
G.add_node(n2)
G.add_edge(n1, n2, directed=True)
我的问题是基本问题,而不是寻求代码方面的帮助。 上面的 n1 和 n2 是用 class 继承自 ipycytoscape class CustomNode(cs.Node) 构建的。 但是它们作为节点添加到 networkx 图中 !!!
这怎么可能? 我的意思是一个ipycytoscape对象被添加到一个networkx对象中。
发件人:https://networkx.org/documentation/stable/tutorial.html#what-to-use-as-nodes-and-edges
You might notice that nodes and edges are not specified as NetworkX objects. This leaves you free to use meaningful items as nodes and edges. The most common choices are numbers or strings, but a node can be any hashable object (except None), and an edge can be associated with any object x using G.add_edge(n1, n2, object=x).
所以这不是 ipycytoscape 特有的,您可以使用任何自定义对象作为 networkx 的节点。像这样:
import networkx as nx
class myClass:
def __init__(self, something):
self.something = something
n1 = myClass(53)
n2 = myClass("node 2")
# === create graph, add custom nodes and an edge
G = nx.Graph()
G.add_node(n1)
G.add_node(n2)
G.add_edge(n1, n2, directed=True)
从 ipycytoscape Node 对象继承只是为了让它更容易与 ipycytoscape 一起工作。