本文整理汇总了Python中networkx.read_graphml函数的典型用法代码示例。如果您正苦于以下问题:Python read_graphml函数的具体用法?Python read_graphml怎么用?Python read_graphml使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_graphml函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:dinesharanathunga,项目名称:autonetkit,代码行数:28,代码来源:graphml.py
示例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')
开发者ID:YukiShan,项目名称:amazon-review-spam,代码行数:33,代码来源:driver.py
示例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
开发者ID:ptokponnon,项目名称:autonetkit,代码行数:28,代码来源:graphml.py
示例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()
开发者ID:SergiosLen,项目名称:TwitterMining,代码行数:26,代码来源:joiner.py
示例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
开发者ID:DhanyaRaghu95,项目名称:py-triple-simple,代码行数:31,代码来源:testPyTripleSimple.py
示例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)
开发者ID:vwvolodya,项目名称:project,代码行数:12,代码来源:FileReader.py
示例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
)
开发者ID:ivihernandez,项目名称:facility-location-probability,代码行数:48,代码来源:main.py
示例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
开发者ID:coaj,项目名称:autonetkit,代码行数:48,代码来源:graph_product.py
示例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
开发者ID:kasev,项目名称:WordNets,代码行数:27,代码来源:tools1.py
示例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 ""
开发者ID:asheshwor,项目名称:tribe,代码行数:7,代码来源:tribe-admin.py
示例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))
开发者ID:account2,项目名称:disease,代码行数:33,代码来源:disease.py
示例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!")
开发者ID:k0pernicus,项目名称:PropL,代码行数:33,代码来源:give_me_some_infos_please.py
示例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))
开发者ID:swamiiyer,项目名称:disease,代码行数:33,代码来源:disease.py
示例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
开发者ID:bizatheo,项目名称:training-material,代码行数:25,代码来源:add_random_edge_weights.py
示例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)
开发者ID:epurdy,项目名称:elegans,代码行数:7,代码来源:viz.py
示例16: test_preserve_multi_edge_data
def test_preserve_multi_edge_data(self):
"""
Test that data and keys of edges are preserved on consequent
write and reads
"""
G = nx.MultiGraph()
G.add_node(1)
G.add_node(2)
G.add_edges_from([
# edges with no data, no keys:
(1, 2),
# edges with only data:
(1, 2, dict(key='data_key1')),
(1, 2, dict(id='data_id2')),
(1, 2, dict(key='data_key3', id='data_id3')),
# edges with both data and keys:
(1, 2, 103, dict(key='data_key4')),
(1, 2, 104, dict(id='data_id5')),
(1, 2, 105, dict(key='data_key6', id='data_id7')),
])
fh = io.BytesIO()
nx.write_graphml(G, fh)
fh.seek(0)
H = nx.read_graphml(fh, node_type=int)
assert_edges_equal(
G.edges(data=True, keys=True), H.edges(data=True, keys=True)
)
assert_equal(G._adj, H._adj)
开发者ID:johnyf,项目名称:networkx,代码行数:28,代码来源:test_graphml.py
示例17: UoSM_input
def UoSM_input(fName, w):
# for the name of the graph add .G
# for the name of communities add .C
gFile = os.getcwd() + "/CSV/Graphs/" + fName + "/" + w + ".G"
wFile = os.getcwd() + "/CSV/WalkTrap/" + fName + "/" + w + ".C"
if (not os.path.exists(gFile)) or (not os.path.exists(wFile)):
print "Error: " + gFile + " or " + wFile + " not found"
return
G = nx.read_graphml(gFile)
try:
f = open(wFile, "r")
except IOError:
return
a = sorted(G.nodes())
# ~ b=[str(xx) for xx in range(len(a))]
# ~ myDic=list2dic(b,a)
C = []
for k, line in enumerate(f):
for line in f:
t1 = line.strip(" {}\t\n")
t2 = t1.split(",")
t = [xx.strip() for xx in t2]
# ~ ll=[myDic[xx][0] for xx in t]
ll = [a[int(xx)] for xx in t]
C.append(ll)
return C
开发者ID:kamalshadi,项目名称:NDTdataProcessing,代码行数:26,代码来源:formCom1.py
示例18: test_write_read_attribute_numeric_type_graphml
def test_write_read_attribute_numeric_type_graphml(self):
from xml.etree.ElementTree import parse
G = self.attribute_numeric_type_graph
fh = io.BytesIO()
nx.write_graphml(G, fh, infer_numeric_types=True)
fh.seek(0)
H = nx.read_graphml(fh)
fh.seek(0)
assert_equal(sorted(G.nodes()), sorted(H.nodes()))
assert_equal(sorted(G.edges()), sorted(H.edges()))
assert_equal(sorted(G.edges(data=True)),
sorted(H.edges(data=True)))
self.attribute_numeric_type_fh.seek(0)
xml = parse(fh)
# Children are the key elements, and the graph element
children = xml.getroot().getchildren()
assert_equal(len(children), 3)
keys = [child.items() for child in children[:2]]
assert_equal(len(keys), 2)
assert_in(('attr.type', 'double'), keys[0])
assert_in(('attr.type', 'double'), keys[1])
开发者ID:JaimieMurdock,项目名称:networkx,代码行数:26,代码来源:test_graphml.py
示例19: main
def main( ):
# graph = cp.readGML(savedGraphs["scalefree"])
d = dictionnaryFunctions()
wf = weightFunctions()
met = methodes_complementaires()
# graph = cp.graphGenerators (500, "scalefree")
# graph = cp.generateRandomWeights(graph)
# graph = nx.DiGraph(graph)
graph_json_pause = met.read_json_file(json_graphe)
graph = nx.read_graphml(graphdataset)
graph = gt.createAndSaveBlockModel(graph)
cp.writeJsonFile(graph, json_graph_filename)
print("\n\n")
print(nx.info(graph))
print("\n\n")
d.createDictNodeNumberToId(graph)
d.checkDictionnary()
w = wf.graphWeightsOnArcs(graph)
#miProgram(graph, w, "influencersAdjacencyMatrixWithBlocksAndColouringFunction", json_graph_filename)
for model in ["neighbouringInfluencersWithoutBinaryVariables", "influencersAdjacencyMatrix"]:
miProgram(graph, w, model, json_graph_filename)
开发者ID:stonepierre,项目名称:reseauSocial,代码行数:29,代码来源:influenceurs_das_une_communaute.py
示例20: main
def main():
universe = nx.read_graphml(sys.argv[1])
beings = filter(lambda x: x[1]["type"] == "Being", universe.nodes(data=True))
d ={}
i=0
for b in beings:
ns = nx.neighbors(universe,b[0])
if universe.node[ns[0]]["type"] == "client":
if "names" in universe.node[b[0]]:
n = universe.node[b[0]]["names"]
d[n] = set(map(lambda x: universe.node[x]["name"], ns))
else:
d["UNCLAIMED-{}".format(i)] = set(map(lambda x: universe.node[x]["name"], ns))
i = i+1
for k in sorted(d.keys()):
if len(d[k]) == 1 and list(d[k])[0] == k:
print(list(d[k])[0])
elif len(d[k]) == 1 and list(d[k])[0] != k:
print(list(d[k])[0]+" ==> "+k)
else:
print(k)
print("--------------------")
for n in d[k]:
print(n)
print("\n")
开发者ID:influence-usa,项目名称:lobbying_federal_domestic,代码行数:26,代码来源:experiment.py
注:本文中的networkx.read_graphml函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论