本文整理汇总了Python中networkx.DiGraph.nodes方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.nodes方法的具体用法?Python DiGraph.nodes怎么用?Python DiGraph.nodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.DiGraph
的用法示例。
在下文中一共展示了DiGraph.nodes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: merge
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def merge(self,minN):
import numpy as np
merged=[]
merged_cliq=[]
while len(DiGraph.nodes(self)):
#print(len(self.nodes()))
contcmp,ct_cliq=self.splitG(minN)
if not DiGraph.nodes(self):
break
merged=merged+contcmp
merged_cliq=merged_cliq+ct_cliq
try:
#print("point1")
cut_nodes=minimum_node_cut(self)
#print("point2")
except:
nodes=DiGraph.nodes(self)
index=np.random.randint(len(nodes))
cut_nodes=[nodes[index]]
for node in cut_nodes:
DiGraph.remove_node(self,node)
self.topics=merged
self.topic_cliq=merged_cliq
示例2: filter_nodes
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def filter_nodes(self,thresh):
nodes=DiGraph.nodes(self)
for node in nodes:
if DiGraph.in_degree(self,node)<thresh:
DiGraph.remove_node(self,node)
nodes=DiGraph.nodes(self)
for node in nodes:
if DiGraph.in_degree(self,node)==0 and DiGraph.out_degree(self,node)==0:
DiGraph.remove_node(self,node)
示例3: add_nodes_from
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def add_nodes_from(self, nodes, **attr):
H=DiGraph()
H.add_nodes_from(self.names)
h_names=sorted(H.nodes())
H.add_edges_from([(h_names[e[0]], h_names[e[1]], self.edge[e[0]][e[1]]) for e in self.edges()])
causes={h_names[v]: {h_names[item]: self.node[v]['causes'][item] for item in self.node[v]['causes']} for v in self.dep_vars}
self.clear()
self.indep_vars=[]
self.dep_vars=[]
if not H.nodes():
DiGraph.add_nodes_from(self, nodes, **attr)
self.names=names=sorted(nodes)
for i, n in enumerate(self.names):
self.node[i]={'name': n, 'pmf': Pmf()}
self.node[i]['pmf'].Set(1,self.p)
self.node[i]['pmf'].Set(0, 1-self.p)
self.remove_node(n)
self.edge[i]={}
self.indep_vars+=[i]
self.SetProbs()
return
#DiGraph.add_nodes_from(self, nodes, **attr)
#ind_vars=[var for var in H.indep_vars]
#DiGraph.add_nodes_from(self, ind_vars)
self.names=names=sorted(set(H.nodes() + nodes))
for i, n in enumerate(names):
if n in H.nodes():
self.node[i], self.edge[i]=H.node[n], {names.index(item): H.edge[n][item] for item in H.edge[n]}
self.node[i]['causes']={names.index(item): causes[n][item] for item in causes[n]} if n in causes else {}
self.node[i]['name']=n
self.node[i]['pmf']=Pmf()
if not self.node[i]['causes']:
self.node[i]['pmf'].Set(1,self.p)
self.node[i]['pmf'].Set(0, 1-self.p)
self.indep_vars+=[i]
else: self.dep_vars+=[i]
else:
self.node[i]={'name': n, 'pmf': Pmf()}
self.node[i]['pmf'].Set(1,self.p)
self.node[i]['pmf'].Set(0, 1-self.p)
#self.remove_node(n)
self.edge[i]={}
self.indep_vars+=[i]
self.SetProbs()
示例4: splitG
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def splitG(self,minN):
contcmp=[]
ct_cliq=[]
cncp=list(strongly_connected_components(self))
for item in cncp:
#print(len(item))
if len(item)<=minN and len(item)>1:
#print("topic")
tmp=set()
tmp_cliq=[]
for each in item:
tmp=tmp.union(set(self.node[each]["keywords"]))
tmp_cliq.append(each)
DiGraph.remove_node(self,each)
contcmp.append(tmp)
ct_cliq.append(tmp_cliq)
if len(item)==1:
DiGraph.remove_node(self,list(item)[0])
nodes=DiGraph.nodes(self)
for node in nodes:
if DiGraph.in_degree(self,node)==0 and DiGraph.out_degree(self,node)==0:
DiGraph.remove_node(self,node)
return contcmp,ct_cliq
示例5: test
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def test():
bayesian_network = DiGraph()
edges = [('A', 'C'), ('B', 'C'), ('C', 'D'), ('C', 'E'), ('D', 'F'), ('D', 'G')]
bayesian_network.add_edges_from(edges)
for node in bayesian_network.nodes():
node_object = bayesian_network.node[node]
# All the variables are binary
node_object['values'] = ['0', '1']
conditional_probabilities = {
'A1': 0.7,
'A0':0.3,
'B1': 0.4,
'B0':0.6,
'C1|A0,B0': 0.1, 'C1|A1,B0': 0.3,
'C1|A0,B1': 0.5, 'C1|A1,B1': 0.9,
'C0|A0,B0': 0.9, 'C0|A1,B0': 0.7,
'C0|A0,B1': 0.5, 'C0|A1,B1': 0.1,
'D1|C0': 0.8, 'D1|C1': 0.3,
'D0|C0': 0.2, 'D0|C1': 0.7,
'E1|C0': 0.2, 'E1|C1': 0.6,
'E0|C0': 0.8, 'E0|C1': 0.4,
'F1|D0': 0.1, 'F1|D1': 0.7,
'F0|D0': 0.9, 'F0|D1': 0.3,
'G1|D0': 0.9, 'G1|D1': 0.4,
'G0|D0': 0.1, 'G0|D1': 0.6
}
inference = PearlsInference(bayesian_network, conditional_probabilities)
print '-------------------------------'
inference.add_evidence(['C', '1'])
print '----------------------------------'
inference.add_evidence(['A', '1'])
pprint(conditional_probabilities)
示例6: incidence_matrix
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def incidence_matrix(graph: nx.DiGraph) -> scs.csc_matrix:
edges = graph.edges()
nodes = sorted(graph.nodes())
result = scs.lil_matrix((nodes.__len__(), edges.__len__()))
for i in range(nodes.__len__()):
for j in range(edges.__len__()):
u, v = edges[j]
if nodes[i] == u:
result[i, j] = 1
elif nodes[i] == v:
result[i, j] = -1
return result.tocsc()
示例7: generate_code
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def generate_code(model_urls, pkg_metadata, basedir, overwrite=True, write_deps=False):
"""
>>> import volib
>>> class MetaData(object):
... name = 'reference'
... version = '1.0'
>>> m = MetaData()
>>> basedir = os.path.dirname(volib.resources.__file__)
>>> os.chdir(basedir)
>>> generate_code(['file:ReferenceDM-1.0.vodml.xml',], m, 'output', write_deps=True)
:param model_urls:
:param pkg_metadata:
:param basedir:
:param overwrite:
:return:
"""
pkg_dir = os.path.join(basedir, pythonify(pkg_metadata.name))
models_dir = os.path.join(pkg_dir, pythonify(pkg_metadata.name))
if overwrite and os.path.exists(basedir):
shutil.rmtree(basedir)
if not os.path.exists(pkg_dir):
os.makedirs(pkg_dir)
models_graph = DiGraph()
models_to_write = []
for url in model_urls:
model = vodml.CreateFromDocument(urllib2.urlopen(url).read())
if model.name != 'ivoa':
models_to_write.append(get_tuple(model))
add_model(models_graph, model)
models_list = topological_sort(models_graph)
models_list.reverse()
node_dict = dict(models_graph.nodes(data=True))
for model in models_list:
if write_deps or model in models_to_write and model.name != 'ivoa':
write_model(node_dict[model]['model'], pkg_metadata, models_dir)
with open(os.path.join(pkg_dir, 'setup.py'), 'w') as f:
template = env.get_template('setup.jj')
models = models_to_write if not write_deps else models_list
output = template.render(meta=pkg_metadata, models=models)
f.write(output)
with open(os.path.join(models_dir, '__init__.py'), 'w'):
pass
示例8: update_from_db
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def update_from_db(self, session):
# Only allow one thread at a time to construct a fresh graph.
with self.update_lock:
checkpoint, checkpoint_time = self._get_checkpoint(session)
if checkpoint == self.checkpoint:
self.logger.debug("Checkpoint hasn't changed. Not Updating.")
return
self.logger.debug("Checkpoint changed; updating!")
new_graph = DiGraph()
new_graph.add_nodes_from(self._get_nodes_from_db(session))
new_graph.add_edges_from(self._get_edges_from_db(session))
rgraph = new_graph.reverse()
users = set()
groups = set()
for (node_type, node_name) in new_graph.nodes():
if node_type == "User":
users.add(node_name)
elif node_type == "Group":
groups.add(node_name)
user_metadata = self._get_user_metadata(session)
permission_metadata = self._get_permission_metadata(session)
service_account_permissions = all_service_account_permissions(session)
group_metadata = self._get_group_metadata(session, permission_metadata)
group_service_accounts = self._get_group_service_accounts(session)
permission_tuples = self._get_permission_tuples(session)
group_tuples = self._get_group_tuples(session)
disabled_group_tuples = self._get_group_tuples(session, enabled=False)
with self.lock:
self._graph = new_graph
self._rgraph = rgraph
self.checkpoint = checkpoint
self.checkpoint_time = checkpoint_time
self.users = users
self.groups = groups
self.permissions = {perm.permission
for perm_list in permission_metadata.values()
for perm in perm_list}
self.user_metadata = user_metadata
self.group_metadata = group_metadata
self.group_service_accounts = group_service_accounts
self.permission_metadata = permission_metadata
self.service_account_permissions = service_account_permissions
self.permission_tuples = permission_tuples
self.group_tuples = group_tuples
self.disabled_group_tuples = disabled_group_tuples
示例9: _dependency_graph
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def _dependency_graph(asts):
# Compute the dependency relation: nodes depend on their inputs, and
# outputs depend on their nodes
g = DiGraph()
for ast in asts:
v = walk(ast, NameFinder())
for i in v.free:
g.add_edge(ast, i)
for o in v.locals | v.conditional_locals:
g.add_edge(o, ast)
# FIXME Manually detect self-loops, since we can't use XDiGraph
# yet. (See https://networkx.lanl.gov/ticket/100)
if v.free & (v.locals | v.conditional_locals):
raise CyclicGraph()
# Take the transitive closure of the relation and filter out non-Nodes
g = closure(g)
return g.subgraph( n for n in g.nodes() if isinstance(n, Node) )
示例10: main
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def main(access_token, package_name, max_depth):
graph = DiGraph()
graphcommons = GraphCommons(access_token)
import_package_dependencies(graph, package_name, max_depth=max_depth)
signals = []
for (node, data) in graph.nodes(data=True):
if data['type'] == 'PACKAGE':
reference = "https://www.npmjs.com/package/%s" % node
else:
reference = 'https://www.npmjs.com/~%s' % node
signals.append(Signal(
action="node_create",
name=node,
type=data['type'],
reference=reference
))
for source, target, data in graph.edges(data=True):
signals.append(Signal(
action="edge_create",
from_name=source,
from_type=graph.node[source]['type'],
to_name=target,
to_type=graph.node[target]['type'],
name=data['type'],
weight=1
))
created_graph = graphcommons.new_graph(
name="Dependency Network of %s" % package_name,
description="Dependency Network of %s Package" % package_name,
signals=signals
)
print 'Created Graph URL:'
print 'https://graphcommons.com/graphs/%s' % created_graph.id
示例11: update_from_db
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
def update_from_db(self, session):
checkpoint, checkpoint_time = self._get_checkpoint(session)
if checkpoint == self.checkpoint:
logging.debug("Checkpoint hasn't changed. Not Updating.")
return
logging.debug("Checkpoint changed; updating!")
new_graph = DiGraph()
new_graph.add_nodes_from(self._get_nodes_from_db(session))
new_graph.add_edges_from(self._get_edges_from_db(session))
rgraph = new_graph.reverse()
users = set()
groups = set()
for (node_type, node_name) in new_graph.nodes():
if node_type == "User":
users.add(node_name)
elif node_type == "Group":
groups.add(node_name)
user_metadata = self._get_user_metadata(session)
permission_metadata = self._get_permission_metadata(session)
group_metadata = self._get_group_metadata(session, permission_metadata)
with self.lock:
self._graph = new_graph
self._rgraph = rgraph
self.checkpoint = checkpoint
self.checkpoint_time = checkpoint_time
self.users = users
self.groups = groups
self.permissions = {perm.permission
for perm_list in permission_metadata.values()
for perm in perm_list}
self.user_metadata = user_metadata
self.group_metadata = group_metadata
self.permission_metadata = permission_metadata
示例12: VerseGenerator
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
class VerseGenerator(object):
def __init__(self, text=None, pos_tags=True, rhyme_table=True):
self.rhyme_table = rhyme_table
self.pos_tags = pos_tags
self.chain = DiGraph()
self.rhymes = {}
if text:
self.load_text(text)
def __getitem__(self, item):
return self.chain[item]
def load_text(self, text):
'''Add the given text to the Markov chain
'''
for sentence in tokenise(text):
if self.pos_tags:
tagged = pos_tag(sentence, tagset=TAGSET)
else:
tagged = ((w, None) for w in sentence)
previous = None
for w, tag in tagged:
current = Word(w, tag)
# Add connection between word and previous to the graph,
# or increment the edge counter if it already exists
if previous:
try:
self.chain[previous][current]['count'] += 1
except KeyError:
self.chain.add_edge(previous, current, count=1)
previous = current
if self.rhyme_table:
rhymes = {w: self.rhymes_for_word(w) for w in self.chain.nodes()}
self.rhymes = rhymes
def load_corpus(self, folder):
'''Load text files in given folder and any subfolders into the
markov chain.
'''
for root, _, fpaths in os.walk(folder):
for path in fpaths:
fullpath = os.path.join(root, path)
with codecs.open(fullpath, 'r', 'utf-8-sig') as f:
try:
text = f.read()
self.load_text(text)
except UnicodeDecodeError as e:
msg = '{} is not utf-8'.format(fullpath)
raise IOError(msg) from e
def dump_chain(self, path):
'''
Serialize the markov chain digraph to a file
'''
with open(path, 'wb') as f:
pickle.dump(self.chain, f)
def load_chain(self, path):
'''
Load a pickled markov chain. Wipes any existing data
'''
with open(path, 'rb') as f:
self.chain = pickle.load(f)
def roulette_select(self, node, pred=False):
'''
A generator which returns successors or predecessors to the
given node in roulette-wheel selected random order.
'''
r = random.random()
current_sum = 0
if pred:
adjacent = self.chain.pred[node]
else:
adjacent = self.chain.succ[node]
for node, props in adjacent.items():
count = props['count']
prob = count / len(adjacent)
current_sum += prob
if r <= current_sum:
return node
return None
def shuffled_successors(self, word, order='roulette'):
'''
Return successor words for given word in random order
proportional to their frequency.
'''
if order is 'roulette':
succ = self.chain.succ[word].items()
key = lambda kv: random.expovariate(1/0.5) * kv[1]['count']
return [pair[0] for pair in sorted(succ, key=key)]
elif order is 'random':
#.........这里部分代码省略.........
示例13: __init__
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
class GVMAnalysis :
def __init__(self, vmx, apk) :
self.vmx = vmx
self.vm = self.vmx.get_vm()
self.nodes = {}
self.nodes_id = {}
self.entry_nodes = []
self.G = DiGraph()
for j in self.vmx.get_tainted_packages().get_internal_packages() :
n1 = self._get_node( j.get_method().get_class_name(), j.get_method().get_name(), j.get_method().get_descriptor() )
n2 = self._get_node( j.get_class_name(), j.get_name(), j.get_descriptor() )
self.G.add_edge( n1.id, n2.id )
n1.add_edge( n2, j )
# print "\t %s %s %s %x ---> %s %s %s" % (j.get_method().get_class_name(), j.get_method().get_name(), j.get_method().get_descriptor(), \
# j.get_bb().start + j.get_idx(), \
# j.get_class_name(), j.get_name(), j.get_descriptor())
if apk != None :
for i in apk.get_activities() :
j = bytecode.FormatClassToJava(i)
n1 = self._get_exist_node( j, "onCreate", "(Landroid/os/Bundle;)V" )
if n1 != None :
n1.set_attributes( { "type" : "activity" } )
n1.set_attributes( { "color" : ACTIVITY_COLOR } )
n2 = self._get_new_node_from( n1, "ACTIVITY" )
n2.set_attributes( { "color" : ACTIVITY_COLOR } )
self.G.add_edge( n2.id, n1.id )
self.entry_nodes.append( n1.id )
for i in apk.get_services() :
j = bytecode.FormatClassToJava(i)
n1 = self._get_exist_node( j, "onCreate", "()V" )
if n1 != None :
n1.set_attributes( { "type" : "service" } )
n1.set_attributes( { "color" : SERVICE_COLOR } )
n2 = self._get_new_node_from( n1, "SERVICE" )
n2.set_attributes( { "color" : SERVICE_COLOR } )
self.G.add_edge( n2.id, n1.id )
self.entry_nodes.append( n1.id )
for i in apk.get_receivers() :
j = bytecode.FormatClassToJava(i)
n1 = self._get_exist_node( j, "onReceive", "(Landroid/content/Context; Landroid/content/Intent;)V" )
if n1 != None :
n1.set_attributes( { "type" : "receiver" } )
n1.set_attributes( { "color" : RECEIVER_COLOR } )
n2 = self._get_new_node_from( n1, "RECEIVER" )
n2.set_attributes( { "color" : RECEIVER_COLOR } )
self.G.add_edge( n2.id, n1.id )
self.entry_nodes.append( n1.id )
# Specific Java/Android library
for c in self.vm.get_classes() :
#if c.get_superclassname() == "Landroid/app/Service;" :
# n1 = self._get_node( c.get_name(), "<init>", "()V" )
# n2 = self._get_node( c.get_name(), "onCreate", "()V" )
# self.G.add_edge( n1.id, n2.id )
if c.get_superclassname() == "Ljava/lang/Thread;" or c.get_superclassname() == "Ljava/util/TimerTask;" :
for i in self.vm.get_method("run") :
if i.get_class_name() == c.get_name() :
n1 = self._get_node( i.get_class_name(), i.get_name(), i.get_descriptor() )
n2 = self._get_node( i.get_class_name(), "start", i.get_descriptor() )
# link from start to run
self.G.add_edge( n2.id, n1.id )
n2.add_edge( n1, {} )
# link from init to start
for init in self.vm.get_method("<init>") :
if init.get_class_name() == c.get_name() :
n3 = self._get_node( init.get_class_name(), "<init>", init.get_descriptor() )
#n3 = self._get_node( i.get_class_name(), "<init>", i.get_descriptor() )
self.G.add_edge( n3.id, n2.id )
n3.add_edge( n2, {} )
#elif c.get_superclassname() == "Landroid/os/AsyncTask;" :
# for i in self.vm.get_method("doInBackground") :
# if i.get_class_name() == c.get_name() :
# n1 = self._get_node( i.get_class_name(), i.get_name(), i.get_descriptor() )
# n2 = self._get_exist_node( i.get_class_name(), "execute", i.get_descriptor() )
# print n1, n2, i.get_descriptor()
#for j in self.vm.get_method("doInBackground") :
# n2 = self._get_exist_node( i.get_class_name(), j.get_name(), j.get_descriptor() )
# print n1, n2
# n2 = self._get_node( i.get_class_name(), "
# raise("ooo")
#for j in self.vmx.tainted_packages.get_internal_new_packages() :
# print "\t %s %s %s %x ---> %s %s %s" % (j.get_method().get_class_name(), j.get_method().get_name(), j.get_method().get_descriptor(), \
# j.get_bb().start + j.get_idx(), \
# j.get_class_name(), j.get_name(), j.get_descriptor())
list_permissions = self.vmx.get_permissions( [] )
for x in list_permissions :
for j in list_permissions[ x ] :
#print "\t %s %s %s %x ---> %s %s %s" % (j.get_method().get_class_name(), j.get_method().get_name(), j.get_method().get_descriptor(), \
# j.get_bb().start + j.get_idx(), \
#.........这里部分代码省略.........
示例14: ApkViewer
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
class ApkViewer(object):
def __init__(self, a):
self.a = a
self.G = DiGraph()
self.all_files = {}
self.ids = {}
root = Directory( "APK" )
root.set_color( "00FF00" )
self.ids[ root ] = len(self.ids)
self.G.add_node( root )
for x, y, z in self.a.get_files_information():
print(x, y, z, os.path.basename(x))
l = []
splitall( x, l )
l.reverse()
l.pop(0)
last = root
for i in l:
if i not in self.all_files:
tmp = Directory( i )
self.ids[ tmp ] = len(self.ids)
self.all_files[ i ] = tmp
else:
tmp = self.all_files[ i ]
self.G.add_edge(last, tmp)
last = tmp
n1 = last
n2 = File( x, y, z )
self.G.add_edge(n1, n2)
self.ids[ n2 ] = len(self.ids)
def export_to_gml(self):
buff = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
buff += "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:y=\"http://www.yworks.com/xml/graphml\" xmlns:yed=\"http://www.yworks.com/xml/yed/3\" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd\">\n"
buff += "<key attr.name=\"description\" attr.type=\"string\" for=\"node\" id=\"d5\"/>\n"
buff += "<key for=\"node\" id=\"d6\" yfiles.type=\"nodegraphics\"/>\n"
buff += "<graph edgedefault=\"directed\" id=\"G\">\n"
for node in self.G.nodes():
print(node)
buff += "<node id=\"%d\">\n" % self.ids[node]
buff += "<data key=\"d6\">\n"
buff += "<y:ShapeNode>\n"
buff += "<y:Geometry height=\"%f\" width=\"%f\"/>\n" % (60.0, 7 * node.width)
buff += "<y:Fill color=\"#%s\" transparent=\"false\"/>\n" % node.color
buff += "<y:NodeLabel>\n"
buff += "%s\n" % node.basename
if isinstance(node, File):
buff += "%s\n" % node.file_type
buff += "%s\n" % hex(node.file_crc)
buff += "</y:NodeLabel>\n"
buff += "</y:ShapeNode>\n"
buff += "</data>\n"
buff += "</node>\n"
nb = 0
for edge in self.G.edges():
buff += "<edge id=\"%d\" source=\"%d\" target=\"%d\">\n" % (nb, self.ids[edge[0]], self.ids[edge[1]])
buff += "</edge>\n"
nb += 1
buff += "</graph>\n"
buff += "</graphml>\n"
return buff
示例15: GraphManager
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import nodes [as 别名]
class GraphManager(object):
""" Generates and processes the graph based on packets
"""
def __init__(self, packets, layer=3, geo_ip=os.path.expanduser('~/GeoIP.dat')):
self.graph = DiGraph()
self.layer = layer
self.geo_ip = None
self.data = {}
try:
self.geo_ip = GeoIP(geo_ip)
except:
logging.warning("could not load GeoIP data")
if self.layer == 2:
edges = map(self._layer_2_edge, packets)
elif self.layer == 3:
edges = map(self._layer_3_edge, packets)
elif self.layer == 4:
edges = map(self._layer_4_edge, packets)
else:
raise ValueError("Other layers than 2,3 and 4 are not supported yet!")
for src, dst, packet in filter(lambda x: not (x is None), edges):
if src in self.graph and dst in self.graph[src]:
self.graph[src][dst]['packets'].append(packet)
else:
self.graph.add_edge(src, dst, {'packets': [packet]})
for node in self.graph.nodes():
self._retrieve_node_info(node)
for src, dst in self.graph.edges():
self._retrieve_edge_info(src, dst)
def get_in_degree(self, print_stdout=True):
unsorted_degrees = self.graph.in_degree()
return self._sorted_results(unsorted_degrees, print_stdout)
def get_out_degree(self, print_stdout=True):
unsorted_degrees = self.graph.out_degree()
return self._sorted_results(unsorted_degrees, print_stdout)
@staticmethod
def _sorted_results(unsorted_degrees, print_stdout):
sorted_degrees = OrderedDict(sorted(unsorted_degrees.items(), key=lambda t: t[1], reverse=True))
for i in sorted_degrees:
if print_stdout:
print(sorted_degrees[i], i)
return sorted_degrees
def _retrieve_node_info(self, node):
self.data[node] = {}
if self.layer >= 3 and self.geo_ip:
if self.layer == 3:
self.data[node]['ip'] = node
elif self.layer == 4:
self.data[node]['ip'] = node.split(':')[0]
node_ip = self.data[node]['ip']
country = self.geo_ip.country_name_by_addr(node_ip)
self.data[node]['country'] = country if country else 'private'
#TODO layer 2 info?
def _retrieve_edge_info(self, src, dst):
edge = self.graph[src][dst]
if edge:
packets = edge['packets']
edge['layers'] = set(list(itertools.chain(*[set(GraphManager.get_layers(p)) for p in packets])))
edge['transmitted'] = sum(len(p) for p in packets)
edge['connections'] = len(packets)
@staticmethod
def get_layers(packet):
return list(GraphManager.expand(packet))
@staticmethod
def expand(x):
yield x.name
while x.payload:
x = x.payload
yield x.name
@staticmethod
def _layer_2_edge(packet):
return packet[0].src, packet[0].dst, packet
@staticmethod
def _layer_3_edge(packet):
if packet.haslayer(IP):
return packet[1].src, packet[1].dst, packet
@staticmethod
def _layer_4_edge(packet):
if any(map(lambda p: packet.haslayer(p), [TCP, UDP])):
src = packet[1].src
dst = packet[1].dst
_ = packet[2]
return "%s:%i" % (src, _.sport), "%s:%i" % (dst, _.dport), packet
#.........这里部分代码省略.........