本文整理汇总了Python中networkx.write_multiline_adjlist函数的典型用法代码示例。如果您正苦于以下问题:Python write_multiline_adjlist函数的具体用法?Python write_multiline_adjlist怎么用?Python write_multiline_adjlist使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_multiline_adjlist函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_results
def save_results(self, image_name, *results):
"""
Create a directory of the following format: current pipeline + fname.
Save and put the results of algorithm processing in the directory.
Args:
| *image_name* (str): image name
| *results* (list): a list of arguments to save
"""
# saving the processed image
try:
cv2.imwrite(os.path.join(self.out_dir, image_name), results[0])
except (IOError, cv2.error):
print('ERROR! Could not write an image file, make sure there is ' +
'enough free space on disk')
sys.exit(1)
if not self.isui:
print('Success!', image_name, 'saved in', self.out_dir)
# exporting graph object
if results[1]:
image_name = os.path.splitext(image_name)[0] + '.txt'
nx.write_multiline_adjlist(results[1], os.path.join(self.out_dir,
image_name),
delimiter='|')
print('Success!', image_name, 'saved in', self.out_dir)
示例2: shortest_path
def shortest_path(src, dst, names, cost):
G = nx.DiGraph()
G.add_nodes_from(names)
indices = {}
for i in range(len(names)):
indices[names[i]] = i
# Add edges from src to stations before it
for i in range(indices[src]):
G.add_edge(src, names[i],
weight=cost(src, names[i]))
# Add edges from stations after dst to dst
for i in range(indices[dst] + 1, len(names)):
G.add_edge(names[i], dst, weight=cost(names[i], dst))
# Add reverse edges from every station after src back
for i in range(indices[src] + 1, len(names)):
for j in range(indices[src] + 1, i):
G.add_edge(names[i], names[j], weight=cost(names[i], names[j]))
# Add edges from every station before dst to
# every station after src
for i in range(len(names)):
for j in range(i + 1, len(names)):
if i >= indices[dst] or j <= indices[src]:
continue
G.add_edge(names[i], names[j],
weight=cost(names[i], names[j]))
nx.write_multiline_adjlist(G, "test.adjlist")
return nx.shortest_path(G, src, dst, weight='weight')
示例3: test_multiline_adjlist_delimiter
def test_multiline_adjlist_delimiter(self):
fh = io.BytesIO()
G = nx.path_graph(3)
nx.write_multiline_adjlist(G, fh, delimiter=':')
fh.seek(0)
H = nx.read_multiline_adjlist(fh, nodetype=int, delimiter=':')
assert_nodes_equal(list(H), list(G))
assert_edges_equal(list(H.edges()), list(G.edges()))
示例4: test_multiline_adjlist_integers
def test_multiline_adjlist_integers(self):
(fd, fname) = tempfile.mkstemp()
G = nx.convert_node_labels_to_integers(self.G)
nx.write_multiline_adjlist(G, fname)
H = nx.read_multiline_adjlist(fname, nodetype=int)
H2 = nx.read_multiline_adjlist(fname, nodetype=int)
assert_nodes_equal(list(H), list(G))
assert_edges_equal(list(H.edges()), list(G.edges()))
os.close(fd)
os.unlink(fname)
示例5: test_multiline_adjlist_digraph
def test_multiline_adjlist_digraph(self):
G = self.DG
(fd, fname) = tempfile.mkstemp()
nx.write_multiline_adjlist(G, fname)
H = nx.read_multiline_adjlist(fname, create_using=nx.DiGraph())
H2 = nx.read_multiline_adjlist(fname, create_using=nx.DiGraph())
assert_not_equal(H, H2) # they should be different graphs
assert_nodes_equal(list(H), list(G))
assert_edges_equal(list(H.edges()), list(G.edges()))
os.close(fd)
os.unlink(fname)
示例6: test_multiline_adjlist_graph
def test_multiline_adjlist_graph(self):
G=self.G
(fd,fname)=tempfile.mkstemp()
nx.write_multiline_adjlist(G,fname)
H=nx.read_multiline_adjlist(fname)
H2=nx.read_multiline_adjlist(fname)
assert_not_equal(H,H2) # they should be different graphs
assert_equal(sorted(H.nodes()),sorted(G.nodes()))
assert_equal(sorted(H.edges()),sorted(G.edges()))
os.close(fd)
os.unlink(fname)
示例7: test_unicode
def test_unicode(self):
G = nx.Graph()
try: # Python 3.x
name1 = chr(2344) + chr(123) + chr(6543)
name2 = chr(5543) + chr(1543) + chr(324)
except ValueError: # Python 2.6+
name1 = unichr(2344) + unichr(123) + unichr(6543)
name2 = unichr(5543) + unichr(1543) + unichr(324)
G.add_edge(name1, 'Radiohead', **{name2: 3})
fd, fname = tempfile.mkstemp()
nx.write_multiline_adjlist(G, fname)
H = nx.read_multiline_adjlist(fname)
assert_graphs_equal(G, H)
os.close(fd)
os.unlink(fname)
示例8: test_latin1
def test_latin1(self):
G = nx.Graph()
try: # Python 3.x
blurb = chr(1245) # just to trigger the exception
name1 = 'Bj' + chr(246) + 'rk'
name2 = chr(220) + 'ber'
except ValueError: # Python 2.6+
name1 = 'Bj' + unichr(246) + 'rk'
name2 = unichr(220) + 'ber'
G.add_edge(name1, 'Radiohead', **{name2: 3})
fd, fname = tempfile.mkstemp()
nx.write_multiline_adjlist(G, fname, encoding='latin-1')
H = nx.read_multiline_adjlist(fname, encoding='latin-1')
assert_graphs_equal(G, H)
os.close(fd)
os.unlink(fname)
示例9: test_latin1
def test_latin1(self):
G = nx.Graph()
try: # Python 3.x
blurb = chr(1245) # just to trigger the exception
name1 = "Bj" + chr(246) + "rk"
name2 = chr(220) + "ber"
except ValueError: # Python 2.6+
name1 = "Bj" + unichr(246) + "rk"
name2 = unichr(220) + "ber"
G.add_edge(name1, "Radiohead", {name2: 3})
fd, fname = tempfile.mkstemp()
nx.write_multiline_adjlist(G, fname, encoding="latin-1")
H = nx.read_multiline_adjlist(fname, encoding="latin-1")
assert_graphs_equal(G, H)
os.close(fd)
os.unlink(fname)
示例10: main
def main(rdfLocation, outputType):
"""Main function which parses the rdf graph then sends it to be queried.
It uses the result from the queryAll function to call the required file parser.
If multiple files are detected it will parse them all.
Returns: Final Network output to file in a variety of common network file formats.
"""
files = []
# Parse The RDF Graph Object by first checking if it is a file location or the file itself.
if os.path.isfile(rdfLocation):
g.parse(location=rdfLocation)
else:
g.parse(data=rdfLocation)
# Specify Output File Type For graph Object
outputFileType = outputType
# Create NetworkX Graph object
G = nx.MultiDiGraph()
# Extract the file types from the rdf as this will be used to call a custom query for a certain filetype
fileInfo = g.query("""SELECT ?name ?filetype WHERE { ?s network:fileType ?filetype .
?s network:fileName ?name . }""")
# Loop through files contained within the dataset and issue relavent queries
for element in fileInfo:
# Extract the FileType and Name from the query result
fileName = str(element[0])
fileType = str(element[1])
# Call XML functions
if fileType == "xml":
print "Found XML File"
xmlData = queryAll(fileName,fileType)
for f in xmlData:
G = FP.parseXML(f,G)
elif fileType == "json":
print "Found JSON file"
jsonData = queryAll(fileName,fileType)
for f in jsonData:
G = FP.parseJSON(f,G)
elif fileType == "csv":
print "Found CSV file"
csvData = queryAll(fileName,fileType)
for f in csvData:
print f
G = FP.parseCSV(f,G)
elif fileType == "excel":
print "Found Excel File"
excelData = queryAll(fileName,fileType)
for f in excelData:
G = FP.parseEXCEL(f,G)
else:
print "Currently Not a Supported File"
print "Finished processing file"
# Write out the graph object to file -------------------------------------------------------------------------------------
if outputFileType == "gml":
nx.write_gml(G,'output.gml')
elif outputFileType == "adj":
nx.write_adjlist(G,"output.adj")
elif outputFileType == "mladj":
nx.write_multiline_adjlist(G,"output.adjlist")
elif outputFileType == "graphml":
nx.write_graphml(G, "output.graphml")
elif outputFileType == "pajek":
nx.write_pajek(G, "output.net")
elif outputFileType == "neo4j":
# Update to address of the Neo4j server
results = neonx.write_to_neo("http://localhost:7474/db/data/", G, 'LINKS_TO')
示例11: write_graph
def write_graph(gr, from_user, to_user):
with get_file(edgelist_fname(**locals()), 'w') as fd:
NX.write_multiline_adjlist(gr, fd, delimiter='\t')
示例12: chr
dt = 'Deatht' + chr(246) + 'ngue'
G = nx.Graph()
G.add_edge(hd, mh)
G.add_edge(mc, st)
G.add_edge(boc, mc)
G.add_edge(boc, dt)
G.add_edge(st, dt)
G.add_edge(q, st)
G.add_edge(dt, mh)
G.add_edge(st, mh)
# write in UTF-8 encoding
fh = open('edgelist.utf-8', 'wb')
fh.write('# -*- coding: utf-8 -*-\n'.encode('utf-8')) # encoding hint for emacs
nx.write_multiline_adjlist(G, fh, delimiter='\t', encoding='utf-8')
# read and store in UTF-8
fh = open('edgelist.utf-8', 'rb')
H = nx.read_multiline_adjlist(fh, delimiter='\t', encoding='utf-8')
for n in G.nodes():
if n not in H:
print(False)
print(list(G.nodes()))
pos = nx.spring_layout(G)
nx.draw(G, pos, font_size=16, with_labels=False)
for p in pos: # raise text positions
pos[p][1] += 0.07