本文整理汇总了Python中networkx.read_graphml函数的典型用法代码示例。如果您正苦于以下问题:Python read_graphml函数的具体用法?Python read_graphml怎么用?Python read_graphml使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_graphml函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_graphml
def load_graphml(input_data):
# TODO: allow default properties to be passed in as dicts
try:
graph = nx.read_graphml(input_data)
except IOError, e:
acceptable_errors = set([2, 36, 63]) # no such file or directory
# input string too long for filename
# input string too long for filename
if e.errno in acceptable_errors:
from xml.etree.cElementTree import ParseError
# try as data string rather than filename string
try:
input_pseduo_fh = StringIO(input_data) # load into filehandle to networkx
graph = nx.read_graphml(input_pseduo_fh)
except IOError:
raise autonetkit.exception.AnkIncorrectFileFormat
except IndexError:
raise autonetkit.exception.AnkIncorrectFileFormat
except ParseError:
raise autonetkit.exception.AnkIncorrectFileFormat
except ParseError:
raise autonetkit.exception.AnkIncorrectFileFormat
else:
raise e
示例2: test_real_graph
def test_real_graph(nparts):
logging.info('Reading author collab graph')
author_graph = nx.read_graphml('/home/amir/az/io/spam/mgraph2.gexf')
author_graph.name = 'author graph'
logging.info('Reading the full author product graph')
full_graph = nx.read_graphml('/home/amir/az/io/spam/spam_graph.graphml')
full_graph.name = 'full graph'
proper_author_graph = author_graph.subgraph([a for a in author_graph if 'revLen' in author_graph.node[a]
and 'hlpful_fav_unfav' in author_graph.node[a]
and 'vrf_prchs_fav_unfav' in author_graph.node[a]])
# features = {'revLen': 0.0, 'hlpful_fav_unfav': False, 'vrf_prchs_fav_unfav': False}
# for a in author_graph:
# for feat, def_val in features.items():
# if feat not in author_graph.node[a]:
# author_graph.node[a][feat] = def_val
# sub sample proper_author_graph
# proper_author_graph.remove_edges_from(random.sample(proper_author_graph.edges(), 2*proper_author_graph.size()/3))
# degree = proper_author_graph.degree()
# proper_author_graph.remove_nodes_from([n for n in proper_author_graph if degree[n] == 0])
# author to the product reviewed by him mapping
logging.debug('forming the product mapping')
author_product_mapping = {}
for a in proper_author_graph:
author_product_mapping[a] = [p for p in full_graph[a] if 'starRating' in full_graph[a][p] and
full_graph[a][p]['starRating'] >= 4]
logging.debug('Running EM')
ll, partition = HardEM.run_EM(proper_author_graph, author_product_mapping, nparts=nparts, parallel=True)
print 'best loglikelihood: %s' % ll
for n in partition:
author_graph.node[n]['cLabel'] = int(partition[n])
nx.write_gexf(author_graph, '/home/amir/az/io/spam/spam_graph_mgraph_sage_labeled.gexf')
示例3: load_graphml
def load_graphml(input_data):
#TODO: allow default properties to be passed in as dicts
try:
graph = nx.read_graphml(input_data)
except IOError, e:
acceptable_errors = set([
2, # no such file or directory
36, # input string too long for filename
63, # input string too long for filename
])
if e.errno in acceptable_errors:
# try as data string rather than filename string
try:
input_pseduo_fh = StringIO(input_data) # load into filehandle to networkx
graph = nx.read_graphml(input_pseduo_fh)
except IOError:
raise autonetkit.exception.AnkIncorrectFileFormat
except IndexError:
raise autonetkit.exception.AnkIncorrectFileFormat
else:
try:
import autonetkit.console_script as cs
input_data=cs.parse_options().file
graph = nx.read_graphml(input_data)
except IOError:
raise e
示例4: create_joined_multigraph
def create_joined_multigraph():
G=nx.DiGraph()
upp=nx.read_graphml('upperlevel_hashtags.graphml')
for ed in upp.edges(data=True):
G.add_edge(ed[0],ed[1],attr_dict=ed[2])
G.add_edge(ed[1],ed[0],attr_dict=ed[2])
mid=nx.read_graphml('friendship_graph.graphml')
for ed in mid.edges(data=True):
G.add_edge(ed[0],ed[1],attr_dict=ed[2])
inter=nx.read_graphml('interlevel_hashtags.graphml')
for ed in inter.edges(data=True):
G.add_edge(ed[0],ed[1],attr_dict=ed[2])
G.add_edge(ed[1],ed[0],attr_dict=ed[2])
down=nx.read_graphml('retweet.graphml')
mapping_f={}
for i,v in enumerate(down.nodes()):
mapping_f[v]='%iretweet_net' %i
for ed in down.edges(data=True):
G.add_edge(mapping_f[ed[0]],mapping_f[ed[1]],attr_dict=ed[2])
for nd in mid.nodes():
if nd in mapping_f:
G.add_edge(nd,mapping_f[nd])
G.add_edge(mapping_f[nd],nd)
nx.write_graphml(G,'joined_3layerdigraph.graphm')
return G,upp.nodes(),mid.nodes(),mapping_f.values()
示例5: test_generate_graphml
def test_generate_graphml(self):
self.ts = pyTripleSimple.SimpleTripleStore()
f = open("acme.nt")
self.ts.load_ntriples(f)
f.close()
egfrsts_obj = pyTripleSimple.ExtractGraphFromSimpleTripleStore(self.ts)
egfrsts_obj.register_label()
egfrsts_obj.register_class()
egfrsts_obj.add_pattern_for_links([['a','b','c']],[('b','in',['<http://acme.com/rdf#isLabeller>'])],("a","c"), "labeller")
egfrsts_obj.register_node_predicate("<http://acme.com/rdf#ndc/date_issued>", "date", lambda x : x.upper())
result_xml = egfrsts_obj.translate_into_graphml_file()
from xml.etree.ElementTree import XML
elements = XML(result_xml)
xml_tags = []
for element in elements:
xml_tags.append(element.tag)
self.assertTrue("{http://graphml.graphdrawing.org/xmlns}key" in xml_tags)
self.assertTrue("{http://graphml.graphdrawing.org/xmlns}graph" in xml_tags)
try:
import networkx
fo = open("acme.graphml","w")
fo.write(result_xml)
fo.close()
networkx.read_graphml("acme.graphml")
f.close()
except ImportError:
pass
示例6: graph_from_file
def graph_from_file(cls, path, file_format=GraphFileFormat.GraphMl):
if file_format == GraphFileFormat.GraphMl:
graph = net.read_graphml(path)
elif file_format == GraphFileFormat.AdjList:
graph = net.read_adjlist(path)
elif file_format == GraphFileFormat.Gml:
graph = net.read_gml(path)
elif file_format == GraphFileFormat.Yaml:
graph = net.read_yaml(path)
else:
graph = net.read_graphml(path)
return cls(graph=graph)
示例7: __init__
def __init__(self, configurationFilePath, paretoFilePath):
"""
@param configurationFilePath: path to the file that has information
about how the experiment was runned: 1) demand center,
2) distribution center, 3) objective functions
@param paretoFilePath: path to the file containing the Pareto set.
This file also has information about the objective function types
The pareto was created by running the program facility-location using
the configurationFilePath
"""
self.configurationFilePath = configurationFilePath
self.paretoFilePath = paretoFilePath
configurationFile = open(self.configurationFilePath, 'r')
paretoFile = open(self.paretoFilePath, 'r')
print 'configuration file', self.configurationFilePath
print 'pareto file', self.paretoFilePath
tree = ET.parse(self.configurationFilePath)
#obtain the file path of the distribution centers
for elem in tree.iter(tag='distributionCenters'):
self.distributionCentersFilePath = elem.text
#obtain the file path of the demand centers
for elem in tree.iter(tag='demandCenters'):
self.demandCentersFilePath = elem.text
os.path.normpath(self.distributionCentersFilePath)
os.path.normpath(self.demandCentersFilePath)
#load the demand and distribution centers as s
distributionCenters = nx.read_graphml(self.distributionCentersFilePath, node_type=int)
demandCenters = nx.read_graphml(self.demandCentersFilePath, node_type=int)
#load the ids of the distribution centers of the pareto set
os.path.normpath(self.paretoFilePath)
pareto = myutils.load_pareto(self.paretoFilePath)
"""
probabilityFailureProblem = probabilityfailureproblem.ProbabilityFailureProblem(demandCenters=demandCenters,
distributionCenters=distributionCenters,
pareto=pareto
)
"""
probabilityFailureProblem = probabilityfailureproblem.ProbabilityFailureProblem(demandCentersFilePath=self.demandCentersFilePath,
distributionCentersFilePath=self.distributionCentersFilePath,
pareto=pareto,
configurationFilePath=configurationFilePath
)
示例8: graph_product
def graph_product(G_file):
#TODO: take in a graph (eg when called from graphml) rather than re-reading the graph again
LOG.info("Applying graph product to %s" % G_file)
H_graphs = {}
try:
G = nx.read_graphml(G_file).to_undirected()
except IOError:
G = nx.read_gml(G_file).to_undirected()
return
G = remove_yed_edge_id(G)
G = remove_gml_node_id(G)
#Note: copy=True causes problems if relabelling with same node name -> loses node data
G = nx.relabel_nodes(G, dict((n, data.get('label', n)) for n, data in G.nodes(data=True)))
G_path = os.path.split(G_file)[0]
H_labels = defaultdict(list)
for n, data in G.nodes(data=True):
H_labels[data.get("H")].append(n)
for label in H_labels.keys():
try:
H_file = os.path.join(G_path, "%s.graphml" % label)
H = nx.read_graphml(H_file).to_undirected()
except IOError:
try:
H_file = os.path.join(G_path, "%s.gml" % label)
H = nx.read_gml(H_file).to_undirected()
except IOError:
LOG.warn("Unable to read H_graph %s, used on nodes %s" % (H_file, ", ".join(H_labels[label])))
return
root_nodes = [n for n in H if H.node[n].get("root")]
if len(root_nodes):
# some nodes have root set
non_root_nodes = set(H.nodes()) - set(root_nodes)
H.add_nodes_from( (n, dict(root=False)) for n in non_root_nodes)
H = remove_yed_edge_id(H)
H = remove_gml_node_id(H)
nx.relabel_nodes(H, dict((n, data.get('label', n)) for n, data in H.nodes(data=True)), copy=False)
H_graphs[label] = H
G_out = nx.Graph()
G_out.add_nodes_from(node_list(G, H_graphs))
G_out.add_nodes_from(propagate_node_attributes(G, H_graphs, G_out.nodes()))
G_out.add_edges_from(intra_pop_links(G, H_graphs))
G_out.add_edges_from(inter_pop_links(G, H_graphs))
G_out.add_edges_from(propagate_edge_attributes(G, H_graphs, G_out.edges()))
#TODO: need to set default ASN, etc?
return G_out
示例9: igraph_draw_traj
def igraph_draw_traj(filname,pold,polar=True,layout=None):
import igraph as ig
g = ig.read(filname,format="graphml")
pols=[]
for i in g.vs:
pols.append(pold[i['id']])
# print pols
if polar:
rgbs = [(1-(i+1.)/2,(i+1.)/2,0) for i in pols]
else:
rgbs = [(1-i,i,0) for i in pols]
# print filname
GGG=nx.read_graphml(filname)
g.vs["label"] = GGG.nodes()
visual_style = {}
visual_style["vertex_size"] = 15
visual_style['vertex_color']=rgbs#'pink'
visual_style['vertex_label_size']='10'
visual_style["vertex_label"] = g.vs["label"]
if layout==None:
layout=g.layout("kk")
# else:
visual_style["layout"] = layout
visual_style["bbox"] = (700, 700)
visual_style["margin"] = 100
return g,visual_style,layout
示例10: draw
def draw(args):
"""
Draw a GraphML with the tribe draw method.
"""
G = nx.read_graphml(args.graphml[0])
draw_social_network(G, args.write)
return ""
示例11: main
def main(args):
"""
Entry point.
"""
if len(args) == 0:
print "Usage: python disease.py <params file>"
sys.exit(1)
# Load the simulation parameters.
params = json.load((open(args[0], "r")))
network_params = params["network_params"]
# Setup the network.
if network_params["name"] == "read_graphml":
G = networkx.read_graphml(network_params["args"]["path"])
G = networkx.convert_node_labels_to_integers(G)
else:
G = getattr(networkx, network_params["name"])(**network_params["args"])
# Carry out the requested number of trials of the disease dynamics and
# average the results.
Sm, Im, Rm, Rv = 0.0, 0.0, 0.0, 0.0
for t in range(1, params["trials"] + 1):
S, I, R = single_trial(G, params)
Rm_prev = Rm
Sm += (S - Sm) / t
Im += (I - Im) / t
Rm += (R - Rm) / t
Rv += (R - Rm) * (R - Rm_prev)
# Print the average
print("%.3f\t%.3f\t%.3f\t%.3f" \
%(Sm, Im, Rm, (Rv / params["trials"]) ** 0.5))
示例12: main
def main():
project = sys.argv[1]
if project[-1] != "/":
project += "/"
usegraphs = []
#Get usefull graphs
if "--usegraph" in sys.argv:
usegraphs.append("{0}{1}".format(project, sys.argv[sys.argv.index("--usegraph") + 1]))
else:
for f in os.listdir(project):
if "usegraph" in f:
usegraphs.append("{0}{1}".format(project, f))
for ug in usegraphs:
print("READ {0}".format(ug))
g = nx.read_graphml(ug)
nb_nodes = g.number_of_nodes()
nb_edges = g.number_of_edges()
nb_paths = compute_paths(project, g)
print("Stats for {0}:".format(ug))
print("\tnumber of nodes : {0}".format(nb_nodes))
print("\tnumber of edges : {0}".format(nb_edges))
print("\tnumber of paths : {0}".format(nb_paths))
print("Done!")
示例13: main
def main(args):
"""
Entry point.
"""
if len(args) != 2:
sys.exit(__doc__ %{"script_name" : args[0].split("/")[-1]})
# Load the simulation parameters.
params = json.load((open(args[1], "r")))
network_params = params["network_params"]
# Setup the network.
G = networkx.read_graphml(network_params["args"]["path"])
G = networkx.convert_node_labels_to_integers(G)
# Load the attack sequences.
fname = network_params["args"]["path"].replace(".graphml", ".pkl")
attack_sequences = pickle.load(open(fname, "rb"))
# Carry out the requested number of trials of the disease dynamics and
# average the results.
Sm, Im, Rm, Rv = 0.0, 0.0, 0.0, 0.0
for t in range(1, params["trials"] + 1):
S, I, R = single_trial(G, params, attack_sequences)
Rm_prev = Rm
Sm += (S - Sm) / t
Im += (I - Im) / t
Rm += (R - Rm) / t
Rv += (R - Rm) * (R - Rm_prev)
# Print the average
print("%.3f\t%.3f\t%.3f\t%.3f" \
%(Sm, Im, Rm, (Rv / params["trials"]) ** 0.5))
示例14: main
def main():
arg_parser = ArgumentParser(description='add edge weights to tree')
arg_parser.add_argument('--input', required=True,
help='inpput file')
arg_parser.add_argument('--output', required=True,
help='outpput file')
arg_parser.add_argument('--seed', type=int, default=None,
help='seed for random number generator')
arg_parser.add_argument('--delim', dest='delimiter', default=' ',
help='delimiter for edge list')
arg_parser.add_argument('--no-data', action='store_true',
dest='no_data', help='show edge data')
arg_parser.add_argument('--edge-list', action='store_true',
help='generate edge list output')
options = arg_parser.parse_args()
random.seed(options.seed)
tree = nx.read_graphml(options.input)
add_edge_weights(tree)
if options.edge_list:
nx.write_edgelist(tree, options.output,
delimiter=options.delimiter,
data=not options.no_data)
else:
nx.write_graphml(tree, options.output)
return 0
示例15: showXY
def showXY(fnm, x="kx", y="ky"):
g = nx.read_graphml(fnm)
x = nx.get_node_attributes(g, x)
y = nx.get_node_attributes(g, y)
coords = zip(x.values(),y.values())
pos = dict(zip(g.nodes(), coords))
nx.draw(g,pos)