本文整理汇总了Python中networkx.algorithms.bipartite.weighted_projected_graph函数的典型用法代码示例。如果您正苦于以下问题:Python weighted_projected_graph函数的具体用法?Python weighted_projected_graph怎么用?Python weighted_projected_graph使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了weighted_projected_graph函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_directed_projection
def test_directed_projection(self):
G=nx.DiGraph()
G.add_edge('A',1)
G.add_edge(1,'B')
G.add_edge('A',2)
G.add_edge('B',2)
P=bipartite.projected_graph(G,'AB')
assert_edges_equal(list(P.edges()),[('A','B')])
P=bipartite.weighted_projected_graph(G,'AB')
assert_edges_equal(list(P.edges()),[('A','B')])
assert_equal(P['A']['B']['weight'],1)
P=bipartite.projected_graph(G,'AB',multigraph=True)
assert_edges_equal(list(P.edges()),[('A','B')])
G=nx.DiGraph()
G.add_edge('A',1)
G.add_edge(1,'B')
G.add_edge('A',2)
G.add_edge(2,'B')
P=bipartite.projected_graph(G,'AB')
assert_edges_equal(list(P.edges()),[('A','B')])
P=bipartite.weighted_projected_graph(G,'AB')
assert_edges_equal(list(P.edges()),[('A','B')])
assert_equal(P['A']['B']['weight'],2)
P=bipartite.projected_graph(G,'AB',multigraph=True)
assert_edges_equal(list(P.edges()),[('A','B'),('A','B')])
示例2: test_directed_projection
def test_directed_projection(self):
G = nx.DiGraph()
G.add_edge("A", 1)
G.add_edge(1, "B")
G.add_edge("A", 2)
G.add_edge("B", 2)
P = bipartite.projected_graph(G, "AB")
assert_equal(sorted(P.edges()), [("A", "B")])
P = bipartite.weighted_projected_graph(G, "AB")
assert_equal(sorted(P.edges()), [("A", "B")])
assert_equal(P["A"]["B"]["weight"], 1)
P = bipartite.projected_graph(G, "AB", multigraph=True)
assert_equal(sorted(P.edges()), [("A", "B")])
G = nx.DiGraph()
G.add_edge("A", 1)
G.add_edge(1, "B")
G.add_edge("A", 2)
G.add_edge(2, "B")
P = bipartite.projected_graph(G, "AB")
assert_equal(sorted(P.edges()), [("A", "B")])
P = bipartite.weighted_projected_graph(G, "AB")
assert_equal(sorted(P.edges()), [("A", "B")])
assert_equal(P["A"]["B"]["weight"], 2)
P = bipartite.projected_graph(G, "AB", multigraph=True)
assert_equal(sorted(P.edges()), [("A", "B"), ("A", "B")])
示例3: test_project_weighted_ratio
def test_project_weighted_ratio(self):
edges = [
("A", "B", 2 / 6.0),
("A", "C", 1 / 6.0),
("B", "C", 1 / 6.0),
("B", "D", 1 / 6.0),
("B", "E", 2 / 6.0),
("E", "F", 1 / 6.0),
]
Panswer = nx.Graph()
Panswer.add_weighted_edges_from(edges)
P = bipartite.weighted_projected_graph(self.G, "ABCDEF", ratio=True)
assert_equal(P.edges(), Panswer.edges())
for u, v in P.edges():
assert_equal(P[u][v]["weight"], Panswer[u][v]["weight"])
edges = [
("A", "B", 3 / 3.0),
("A", "E", 1 / 3.0),
("A", "C", 1 / 3.0),
("A", "D", 1 / 3.0),
("B", "E", 1 / 3.0),
("B", "C", 1 / 3.0),
("B", "D", 1 / 3.0),
("C", "D", 1 / 3.0),
]
Panswer = nx.Graph()
Panswer.add_weighted_edges_from(edges)
P = bipartite.weighted_projected_graph(self.N, "ABCDE", ratio=True)
assert_equal(P.edges(), Panswer.edges())
for u, v in P.edges():
assert_equal(P[u][v]["weight"], Panswer[u][v]["weight"])
示例4: test_project_weighted_shared
def test_project_weighted_shared(self):
edges = [("A", "B", 2), ("A", "C", 1), ("B", "C", 1), ("B", "D", 1), ("B", "E", 2), ("E", "F", 1)]
Panswer = nx.Graph()
Panswer.add_weighted_edges_from(edges)
P = bipartite.weighted_projected_graph(self.G, "ABCDEF")
assert_edges_equal(P.edges(), Panswer.edges())
for u, v in P.edges():
assert_equal(P[u][v]["weight"], Panswer[u][v]["weight"])
edges = [
("A", "B", 3),
("A", "E", 1),
("A", "C", 1),
("A", "D", 1),
("B", "E", 1),
("B", "C", 1),
("B", "D", 1),
("C", "D", 1),
]
Panswer = nx.Graph()
Panswer.add_weighted_edges_from(edges)
P = bipartite.weighted_projected_graph(self.N, "ABCDE")
assert_edges_equal(P.edges(), Panswer.edges())
for u, v in P.edges():
assert_equal(P[u][v]["weight"], Panswer[u][v]["weight"])
示例5: test_project_weighted_shared
def test_project_weighted_shared(self):
edges=[('A','B',2),
('A','C',1),
('B','C',1),
('B','D',1),
('B','E',2),
('E','F',1)]
Panswer=nx.Graph()
Panswer.add_weighted_edges_from(edges)
P=bipartite.weighted_projected_graph(self.G,'ABCDEF')
assert_equal(P.edges(),Panswer.edges())
for u,v in P.edges():
assert_equal(P[u][v]['weight'],Panswer[u][v]['weight'])
edges=[('A','B',3),
('A','E',1),
('A','C',1),
('A','D',1),
('B','E',1),
('B','C',1),
('B','D',1),
('C','D',1)]
Panswer=nx.Graph()
Panswer.add_weighted_edges_from(edges)
P=bipartite.weighted_projected_graph(self.N,'ABCDE')
assert_equal(P.edges(),Panswer.edges())
for u,v in P.edges():
assert_equal(P[u][v]['weight'],Panswer[u][v]['weight'])
示例6: test_project_weighted_ratio
def test_project_weighted_ratio(self):
edges=[('A','B',2/6.0),
('A','C',1/6.0),
('B','C',1/6.0),
('B','D',1/6.0),
('B','E',2/6.0),
('E','F',1/6.0)]
Panswer=nx.Graph()
Panswer.add_weighted_edges_from(edges)
P=bipartite.weighted_projected_graph(self.G, 'ABCDEF', ratio=True)
assert_edges_equal(list(P.edges()),Panswer.edges())
for u,v in list(P.edges()):
assert_equal(P[u][v]['weight'],Panswer[u][v]['weight'])
edges=[('A','B',3/3.0),
('A','E',1/3.0),
('A','C',1/3.0),
('A','D',1/3.0),
('B','E',1/3.0),
('B','C',1/3.0),
('B','D',1/3.0),
('C','D',1/3.0)]
Panswer=nx.Graph()
Panswer.add_weighted_edges_from(edges)
P=bipartite.weighted_projected_graph(self.N, 'ABCDE', ratio=True)
assert_edges_equal(list(P.edges()),Panswer.edges())
for u,v in list(P.edges()):
assert_equal(P[u][v]['weight'],Panswer[u][v]['weight'])
示例7: test_path_weighted_projected_graph
def test_path_weighted_projected_graph(self):
G=nx.path_graph(4)
P=bipartite.weighted_projected_graph(G,[1,3])
assert_nodes_equal(list(P),[1,3])
assert_edges_equal(list(P.edges()),[(1,3)])
P[1][3]['weight']=1
P=bipartite.weighted_projected_graph(G,[0,2])
assert_nodes_equal(list(P),[0,2])
assert_edges_equal(list(P.edges()),[(0,2)])
P[0][2]['weight']=1
示例8: test_path_weighted_projected_graph
def test_path_weighted_projected_graph(self):
G = nx.path_graph(4)
P = bipartite.weighted_projected_graph(G, [1, 3])
assert_equal(sorted(P.nodes()), [1, 3])
assert_equal(sorted(P.edges()), [(1, 3)])
P[1][3]["weight"] = 1
P = bipartite.weighted_projected_graph(G, [0, 2])
assert_equal(sorted(P.nodes()), [0, 2])
assert_equal(sorted(P.edges()), [(0, 2)])
P[0][2]["weight"] = 1
示例9: create_complete_graph
def create_complete_graph(CProject):
"""
Creates a multipartite graph consisting of papers on the one hand,
and all facts of available plugin-results on the other hand.
Args: CProject
Returns: (bipartite_graph, monopartite_fact_graph, monopartite_paper_graph, paper_nodes, fact_nodes)
"""
partition_mapping = {"papers":0,
"binomial":1, "genus":2, "genussp":3,
"carb3":4, "prot3":5, "dna":6, "prot":7,
"human":8}
gene = ["human"]
species = ["binomial"]
sequence = ["dna", "prot"]
plugins = {"gene":gene, "species": species, "sequence":sequence}
M = nx.Graph()
labels = {}
for ctree in CProject.get_ctrees():
for plugin, types in plugins.items():
for ptype in types:
try:
results = ctree.show_results(plugin).get(ptype, [])
except AttributeError:
continue
if len(results) > 0:
source = " ".join(ctree.get_title().split())
if not source in M.nodes():
# add paper node to one side of the bipartite network
M.add_node(source, bipartite=0)
labels[str(source)] = str(source)
for result in results:
target = result.get("exact")
# add fact node to other side of the bipartite network
if not target in M.nodes():
M.add_node(target, bipartite=1, ptype=ptype)
labels[target] = target.encode("utf-8").decode("utf-8")
# add a link between a paper and author
M.add_edge(source, target)
paper_nodes = set(n for n,d in M.nodes(data=True) if d.get('bipartite')==0)
fact_nodes = set(M) - paper_nodes
fact_graph = bipartite.weighted_projected_graph(M, fact_nodes)
paper_graph = bipartite.weighted_projected_graph(M, paper_nodes)
return M, fact_graph, paper_graph, fact_nodes, paper_nodes
示例10: test_path_weighted_projected_directed_graph
def test_path_weighted_projected_directed_graph(self):
G = nx.DiGraph()
G.add_path(list(range(4)))
P = bipartite.weighted_projected_graph(G, [1, 3])
assert_equal(sorted(P.nodes()), [1, 3])
assert_equal(sorted(P.edges()), [(1, 3)])
P[1][3]["weight"] = 1
P = bipartite.weighted_projected_graph(G, [0, 2])
assert_equal(sorted(P.nodes()), [0, 2])
assert_equal(sorted(P.edges()), [(0, 2)])
P[0][2]["weight"] = 1
示例11: create_network
def create_network(CProject, plugin, query):
"""
Creates the network between papers and plugin results.
Plugin may be any of ["regex", "gene", "sequence", "species"]
Query corresponds to the fact types found by a plugin,
for "species" it is one of ["binomial", "genus", "genussp"]
for "sequences" it is one of ["carb3", "prot3", "dna", "prot"]
for "gene" it is "human"
for "regex" it is "regexname".
Args: CProject object
plugin = "string"
query = "string"
Returns: (bipartite_graph, monopartite_graph, fact_nodes, paper_nodes)
>>> bipartiteGraph, factGraph, paperGraph, fact_nodes, paper_nodes = create_network(CProject, "species", "binomial")
"""
B = nx.Graph()
labels = {}
for ctree in CProject.get_ctrees():
try:
results = ctree.show_results(plugin).get(query, [])
except AttributeError:
continue
if len(results) > 0:
# add paper node to one side of the bipartite network
source = " ".join(ctree.get_title().split())
B.add_node(source, bipartite=0)
labels[str(source)] = str(source)
for result in results:
exact = result.get("exact")
# add fact node to other side of the bipartite network
B.add_node(exact, bipartite=1)
labels[exact] = exact.encode("utf-8").decode("utf-8")
# add a link between a paper and author
B.add_edge(source, exact)
paper_nodes = set(n for n,d in B.nodes(data=True) if d['bipartite']==0)
fact_nodes = set(B) - paper_nodes
fact_graph = bipartite.weighted_projected_graph(B, fact_nodes)
paper_graph = bipartite.weighted_projected_graph(B, paper_nodes)
return B, fact_graph, paper_graph, fact_nodes, paper_nodes
示例12: incremental_reconstruction
def incremental_reconstruction(data):
data.invent_reference_lla()
graph = data.load_tracks_graph()
tracks, images = tracks_and_images(graph)
remaining_images = set(images)
print 'images', len(images)
print 'nonfisheye images', len(remaining_images)
image_graph = bipartite.weighted_projected_graph(graph, images)
reconstructions = []
pairs = compute_image_pairs(graph, image_graph, data.config)
for im1, im2 in pairs:
if im1 in remaining_images and im2 in remaining_images:
reconstruction = bootstrap_reconstruction(data, graph, im1, im2)
if reconstruction:
remaining_images.remove(im1)
remaining_images.remove(im2)
reconstruction = grow_reconstruction(data, graph, reconstruction, remaining_images)
reconstructions.append(reconstruction)
reconstructions = sorted(reconstructions, key=lambda x: -len(x.shots))
data.save_reconstruction(reconstructions)
for k, r in enumerate(reconstructions):
print 'Reconstruction', k, ':', len(r.shots), 'images', ',', len(r.points),'points'
print len(reconstructions), 'partial reconstructions in total.'
示例13: make_network
def make_network(raw_file_list):
# handles the individual nodes
collect_nodes_by_source = []
list_of_source_names = []
node_list = []
GB = nx.Graph()
# for all the nodes...
for i in range(len(raw_file_list)):
# check whether they are name, source or else (not returned). "i" works as an index to identify the respective node when it comes back
checker, a = my_containsAny(raw_file_list[i], i)
# raw data starts with a source, all following non-source lines refer to names or tags. So all returned nodes should be linked to each other
if checker == "source":
GB.add_node(raw_file_list[a], bipartite = 0)
source = raw_file_list[a]
while source == raw_file_list[a]:
if checker == "node":
GB.add_node(raw_file_list[a], bipartite = 1)
GB.add_edge(raw_file_list[a], raw_file_list[a+1])
G = bnx.weighted_projected_graph(GB, GB.nodes(["bipartite"]==1))
#nx.write_graphml(GB, "abolitionists_bipartite.graphml")
nx.write_pajek(G, "abolitionists.net")
print "done!"
示例14: incremental_reconstruction
def incremental_reconstruction(data):
"""Run the entire incremental reconstruction pipeline."""
data.invent_reference_lla()
graph = data.load_tracks_graph()
tracks, images = tracks_and_images(graph)
remaining_images = set(images)
gcp = None
if data.ground_control_points_exist():
gcp = data.load_ground_control_points()
image_graph = bipartite.weighted_projected_graph(graph, images)
reconstructions = []
pairs = compute_image_pairs(graph, image_graph, data.config)
for im1, im2 in pairs:
if im1 in remaining_images and im2 in remaining_images:
reconstruction = bootstrap_reconstruction(data, graph, im1, im2)
if reconstruction:
remaining_images.remove(im1)
remaining_images.remove(im2)
reconstruction = grow_reconstruction(
data, graph, reconstruction, remaining_images, gcp)
reconstructions.append(reconstruction)
reconstructions = sorted(reconstructions,
key=lambda x: -len(x.shots))
data.save_reconstruction(reconstructions)
for k, r in enumerate(reconstructions):
logger.info("Reconstruction {}: {} images, {} points".format(
k, len(r.shots), len(r.points)))
logger.info("{} partial reconstructions in total.".format(
len(reconstructions)))
示例15: test_project_multigraph
def test_project_multigraph(self):
G = nx.Graph()
G.add_edge("a", 1)
G.add_edge("b", 1)
G.add_edge("a", 2)
G.add_edge("b", 2)
P = bipartite.projected_graph(G, "ab")
assert_edges_equal(P.edges(), [("a", "b")])
P = bipartite.weighted_projected_graph(G, "ab")
assert_edges_equal(P.edges(), [("a", "b")])
P = bipartite.projected_graph(G, "ab", multigraph=True)
assert_edges_equal(P.edges(), [("a", "b"), ("a", "b")])