一、简介
DotMotif是一个Python的第三方库,用于检测大型图(graph)中的子图(subgraph)或模体(motif),可以应用在复杂网络相关项目中。
官网链接:https://github.com/aplbrain/dotmotif
二、官方文档
Argument | Type, Default | Behavior |
---|---|---|
ignore_direction | bool: False | Whether to disregard direction when generating the database query |
limit | int: None | A limit (if any) to impose on the query results |
enforce_inequality | bool: False | Whether to enforce inequality; in other words, whether two nodes should be permitted to be aliases for the same node. For example, in A->B->C; if A!=C, then set to True |
exclude_automorphisms | bool: False | Whether to return only a single example for each detected automorphism. See more in the documentation |
参数 | 类型,默认值 | 行为 |
---|---|---|
ignore_direction | bool:False | 生成数据库查询时是否忽略方向(取决于被检测的图是否为无向图) |
limit | int:None | 对查询结果施加的限制(如果有的话) |
enforce_inequality | bool:False | 是否强制执行不平等;换句话说,是否允许两个节点作为同一节点的别名。例如,在A->B->C中;如果A不等于C,那么设置为True |
exclude_automorphisms | bool:False | 是否将自同构体算作一项。详见文档 |
三、示例
0.安装dotmotif
使用pip安装:
pip install dotmotif
1.引入库
import networkx as nx
from dotmotif import Motif, NetworkXExecutor
2.创建图
G = nx.DiGraph()
G.add_nodes_from(['a', 'b', 'c'])
G.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
3.创建检测器
E = NetworkXExecutor(graph=G)
triangle = Motif(input_motif="""
A -> B
B -> C
C -> A
""", ignore_direction=False, exclude_automorphisms=True)
4.检测模体、输出结果
result = E.find(triangle)
print(result)
print(len(result))
5.模体保存及读取
triangle.save('motif_triangle.pkl')
triangle.load('motif_triangle.pkl')
四、代码汇总
import networkx as nx
from dotmotif import Motif, NetworkXExecutor
G = nx.DiGraph()
G.add_nodes_from(['a', 'b', 'c'])
G.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'a')])
E = NetworkXExecutor(graph=G)
triangle = Motif(input_motif="""
A -> B
B -> C
C -> A
""", ignore_direction=False, exclude_automorphisms=True)
#triangle.save('motif_triangle.pkl')
#triangle=triangle.load('motif_triangle.pkl')
result = E.find(triangle)
print(result)
print(len(result))