我是Python的新手,我想使用包图工具通过随机块模型(嵌套和非嵌套)方法来估计网络中的最佳社区数量。
我阅读了与核心功能“图形”(创建图形)有关的文档,然后阅读了“ minimize_blockmodel_dl”和“ minimize_nested_blockmodel_dl”,最终得到了我所需要的东西,但是我找不到针对双网的特定内容。
函数Graph似乎不允许创建二分图,但这很奇怪...
因此,我刚刚看到了如何使用networkx包创建一个,然后使用我在网上找到的功能将其转换为Graph对象:
#def get_prop_type(value, key=None):
# Deal with the value
if isinstance(value, bool):
tname = 'bool'
elif isinstance(value, int):
tname = 'float'
value = float(value)
elif isinstance(value, float):
tname = 'float'
elif isinstance(value, str):
tname = 'string'
elif isinstance(value, dict):
tname = 'object'
else:
tname = 'string'
value = str(value)
return tname, value, key
#
def nx2gt(nxG):
# Phase 0: Create a directed or undirected graph-tool Graph
gtG = Graph(directed=False)
# Add the Graph properties as "internal properties"
for key, value in nxG.graph.items():
# Convert the value and key into a type for graph-tool
tname, value, key = get_prop_type(value, key)
prop = gtG.new_graph_property(tname) # Create the PropertyMap
gtG.graph_properties[key] = prop # Set the PropertyMap
gtG.graph_properties[key] = value # Set the actual value
# Phase 1: Add the vertex and edge property maps
# Go through all nodes and edges and add seen properties
# Add the node properties first
nprops = set() # cache keys to only add properties once
for node, data in nxG.nodes(data=True):
# Go through all the properties if not seen and add them.
for key, val in data.items():
if key in nprops: continue # Skip properties already added
# Convert the value and key into a type for graph-tool
tname, _, key = get_prop_type(val, key)
prop = gtG.new_vertex_property(tname) # Create the PropertyMap
gtG.vertex_properties[key] = prop # Set the PropertyMap
# Add the key to the already seen properties
nprops.add(key)
# Also add the node id: in NetworkX a node can be any hashable type, but
# in graph-tool node are defined as indices. So we capture any strings
# in a special PropertyMap called 'id' -- modify as needed!
gtG.vertex_properties['id'] = gtG.new_vertex_property('string')
# Add the edge properties second
eprops = set() # cache keys to only add properties once
for src, dst, data in nxG.edges(data=True):
# Go through all the edge properties if not seen and add them.
for key, val in data.items():
if key in eprops: continue # Skip properties already added
# Convert the value and key into a type for graph-tool
tname, _, key = get_prop_type(val, key)
prop = gtG.new_edge_property(tname) # Create the PropertyMap
gtG.edge_properties[key] = prop # Set the PropertyMap
# Add the key to the already seen properties
eprops.add(key)
# Phase 2: Actually add all the nodes and vertices with their properties
# Add the nodes
vertices = {} # vertex mapping for tracking edges later
for node, data in nxG.nodes(data=True):
# Create the vertex and annotate for our edges later
v = gtG.add_vertex()
vertices[node] = v
# Set the vertex properties, not forgetting the id property
data['id'] = str(node)
for key, value in data.items():
gtG.vp[key][v] = value # vp is short for vertex_properties
# Add the edges
for src, dst, data in nxG.edges(data=True):
# Look up the vertex structs from our vertices mapping and add edge.
e = gtG.add_edge(vertices[src], vertices[dst])
# Add the edge properties
for key, value in data.items():
gtG.ep[key][e] = value # ep is short for edge_properties
return gtG
#
因此,使用其方法list_properties(),我看到以下内容:
有向(图)(类型:bool,val:0) 双向(顶点)(类型:字符串) id(顶点)(类型:字符串) 重量(边缘)(类型:两倍)
确定,无向,二分法,其顶点具有整数序列,作为边的标签和权重。
到目前为止,看来一切都很好。
最后,尝试将新的Graph对象赋予函数 minimal_blockmodel_dl并使用get_blocks()方法获取网络中每个顶点的最终标签,我意识到实际上发生了这样的情况:属于二分网络不同集合的顶点与其他网络集合的顶点一起组成了群集。这意味着成为二分法的初始属性不再存在,并且该模型也不应用此约束。
为什么?
我希望一些使用这些功能的人可以帮助我解决问题。 谢谢!