本文整理汇总了Python中networkx.Graph类的典型用法代码示例。如果您正苦于以下问题:Python Graph类的具体用法?Python Graph怎么用?Python Graph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Graph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _update_unfit_groups_with_crossgroup_dist
def _update_unfit_groups_with_crossgroup_dist(dist_metrics, fit_group, fit_pvals, unfit_group, buffer_group,
user_ids, user_profiles, user_connections, ks_alpha=0.05):
""" update members in unfit_group with cross-group distance. unfit members are kept in buffer_group
"""
# to keep API consistant
# restore user_profiles to DataFrame including
user_graph = Graph()
user_graph.add_edges_from(user_connections)
unfit_group_copy = unfit_group.copy()
for gg, gg_user_ids in unfit_group_copy.items():
# extract cross-group distance metrics dictionary to avoid duplicate
# tests with distance metrics associated with user's group
other_group_keys = [group_key for group_key in dist_metrics.keys() if not group_key == gg]
cross_group_dist_metrics = {key: dist_metrics[key] for key in other_group_keys}
for ii, ii_user_id in enumerate(gg_user_ids):
ii_new_group, ii_new_pval = find_fit_group(ii_user_id, cross_group_dist_metrics,
user_ids, user_profiles, user_graph, ks_alpha,
current_group=None, fit_rayleigh=False)
# redistribute the user based on fit-tests
if not ii_new_group is None:
# remove member with fit from buffer_group
if ii_new_group in fit_group:
fit_group[ii_new_group].append(ii_user_id)
fit_pvals[ii_new_group].append(ii_new_pval)
else:
fit_group[ii_new_group] = [ii_user_id]
fit_pvals[ii_new_group] = [ii_new_pval]
else:
buffer_group.append(ii_user_id)
return fit_group, fit_pvals, buffer_group
示例2: _update_buffer_group
def _update_buffer_group(dist_metrics, fit_group, fit_pvals, buffer_group,
user_ids, user_profiles, user_connections, ks_alpha=0.05):
""" return fit_group, fit_pvals, buffer_group
redistribute member in buffer group into fit_group if fit had been found
"""
# to keep API consistant
# restore user_profiles to DataFrame including
user_graph = Graph()
user_graph.add_edges_from(user_connections)
buffer_group_copy = buffer_group.copy()
if len(buffer_group_copy) > 0:
for ii, ii_user_id in enumerate(buffer_group_copy):
ii_new_group, ii_new_pval = find_fit_group(ii_user_id, dist_metrics,
user_ids, user_profiles, user_graph, ks_alpha,
current_group=None, fit_rayleigh=False)
if not ii_new_group is None:
# remove member with fit from buffer_group
buffer_group.remove(ii_user_id)
if ii_new_group in fit_group:
fit_group[ii_new_group].append(ii_user_id)
fit_pvals[ii_new_group].append(ii_new_pval)
else:
fit_group[ii_new_group] = [ii_user_id]
fit_pvals[ii_new_group] = [ii_new_pval]
return fit_group, fit_pvals, buffer_group
示例3: __init__
def __init__(self, data=None, name='', file=None, **attr):
Graph.__init__(self, data=data,name=name,**attr)
if file is None:
import sys
self.fh=sys.stdout
else:
self.fh=open(file,'w')
示例4: __init__
def __init__(self, environment=None, channelType=None, algorithms=(),
networkRouting=True, propagation_type=2, **kwargs):
Graph.__init__(self)
self._environment = environment or Environment()
# assert(isinstance(self.environment, Environment))
self.channelType = channelType or ChannelType(self._environment)
if isinstance(self.channelType, Doi):
doi = kwargs.pop('doi', 0)
print "In DOI %s" %doi
self.channelType.set_params(doi=doi)
self.channelType.environment = self._environment
self.propagation = propagation.PropagationModel(propagation_type=propagation_type)
self.pos = {}
self.ori = {}
self.labels = {}
#self.star = star_graph
self.name = "WSN"
self._algorithms = ()
self.algorithms = algorithms or settings.ALGORITHMS
self.algorithmState = {'index': 0, 'step': 1, 'finished': False}
self.outbox = []
self.networkRouting = networkRouting
self.comm_range = kwargs.pop('commRange', None) or settings.COMM_RANGE
logger.info("Instance of Network has been initialized with %s (%s)" % (self.propagation, self.comm_range))
示例5: add_node
def add_node(self, node=None, pos=None, ori=None, commRange=None):
"""
Add node to network.
Attributes:
`node` -- node to add, default: new node is created
`pos` -- position (x,y), default: random free position in environment
`ori` -- orientation from 0 to 2*pi, default: random orientation
"""
if (not node):
node = Node(commRange=commRange)
assert(isinstance(node, Node))
if not node.network:
node.network = self
else:
logger.warning('Node is already in another network, can\'t add.')
return None
pos = pos if pos is not None else self.find_random_pos(n=100)
ori = ori if ori is not None else rand() * 2 * pi
ori = ori % (2 * pi)
if (self._environment.is_space(pos)):
Graph.add_node(self, node)
self.pos[node] = array(pos)
self.ori[node] = ori
self.labels[node] = str(node.id)
logger.debug('Node %d is placed on position %s.' % (node.id, pos))
self.recalculate_edges([node])
else:
logger.error('Given position is not free space.')
return node
示例6: extract_colored_faces
def extract_colored_faces(fname, colors):
output = {color:[] for color in colors}
vertices, faces = load_ply(fname)
for color in colors:
colored_vertices_indices = np.nonzero((vertices['color'] == color).all(axis=1))[0]
colored_faces = np.nonzero(np.all((np.in1d(faces["indices"][:,0], colored_vertices_indices),
np.in1d(faces["indices"][:,1], colored_vertices_indices),
np.in1d(faces["indices"][:,2], colored_vertices_indices)), axis=0))[0]
colored_faces_graph = Graph()
colored_faces_graph.add_edges_from(faces['indices'][colored_faces][:,:2])
colored_faces_graph.add_edges_from(faces['indices'][colored_faces][:,1:])
colored_faces_graph.add_edges_from(faces['indices'][colored_faces][:,(0,2)])
planes_vertices_indices = list(connected_components(colored_faces_graph))
print len(planes_vertices_indices)
for plane_vertices_indices in planes_vertices_indices:
colored_vertices = vertices["position"][list(plane_vertices_indices)]
dipdir, dip = calc_sphere(*general_axis(colored_vertices, -1))
X, Y, Z = colored_vertices.mean(axis=0)
highest_vertex = colored_vertices[np.argmax(colored_vertices[:,2]),:]
lowest_vertex = colored_vertices[np.argmin(colored_vertices[:,2]),:]
trace = np.linalg.norm(highest_vertex - lowest_vertex)
output[color].append((dipdir, dip, X, Y, Z, trace))
return output
示例7: convert_local_tree_topology_to_graph
def convert_local_tree_topology_to_graph(loc_tree_topo, tree_node_labeling):
""" Creates a directed, acyclic NetworkX graph from a local tree topology
Parameters
----------
loc_tree_topo: array-like
The local tree toplogy, where the root node element is -1
tree_node_labeling: array-like
The integer ids for each tree node
Returns
-------
G : NetworkX graph
"""
assert( loc_tree_topo[0] == -1 )
G = Graph()
G.add_nodes_from( tree_node_labeling )
# build up graph connectivity
con = vstack( (loc_tree_topo, range(len(loc_tree_topo))) )
# prune root node connectivity
con = con[:,1:]
# update with correct labels
con = tree_node_labeling[con]
G.add_edges_from( zip(con[0,:], con[1,:]) )
return G
示例8: multiple_edges
def multiple_edges(self, new):
"""
Get/set whether or not self allows multiple edges.
INPUT:
new: boolean or None
DOCTEST:
sage: G = sage.graphs.base.graph_backends.NetworkXGraphBackend()
sage: G.multiple_edges(True)
sage: G.multiple_edges(None)
True
"""
try:
assert(not isinstance(self._nxg, (NetworkXGraphDeprecated, NetworkXDiGraphDeprecated)))
except AssertionError:
self._nxg = self._nxg.mutate()
from networkx import Graph,MultiGraph,DiGraph,MultiDiGraph
if new is None:
return self._nxg.is_multigraph()
if new == self._nxg.is_multigraph():
return
if new:
if self._nxg.is_directed():
self._nxg = MultiDiGraph(self._nxg)
else:
self._nxg = MultiGraph(self._nxg)
else:
if self._nxg.is_directed():
self._nxg = DiGraph(self._nxg)
else:
self._nxg = Graph(self._nxg)
示例9: tuples_to_graph
def tuples_to_graph(tuples):
G = Graph()
for node, attribute in tuples:
print 'adding', node, attribute
G.add_nodes_from(node, freq=attribute)
G.add_edges_from(to_edges(node))
return G
示例10: edmondskarp
def edmondskarp(G: nx.Graph, s, t):
RG = G.copy()
for u,v in G.edges_iter():
G.edge[u][v]['flow'] = 0
path = isthereapath(RG,s,t)
while len(path) > 0:
path_cp = min([RG.edge[u][v]['capacity'] for u,v in path])
for u,v in path:
if G.has_edge(u,v):
G.edge[u][v]['flow'] += path_cp
RG.edge[u][v]['capacity'] -= path_cp
if RG.edge[u][v]['capacity'] == 0:
RG.remove_edge(u,v)
if RG.has_edge(v,u):
RG.edge[v][u]['capacity'] += path_cp
else:
RG.add_edge(v,u,capacity=path_cp)
else:
# then this edge is a "cancelling" flow
# residue should go up and cancelling "capacity" should go down
G.edge[v][u]['flow'] -= path_cp
RG.edge[v][u]['capacity'] += path_cp
RG.edge[u][v]['capacity'] -= path_cp
if RG.edge[u][v]['capacity'] == 0:
RG.remove_edge(u,v)
path = isthereapath(RG,s,t)
return RG
示例11: network_UKGDS
def network_UKGDS(filename,header=28):
"""
Load Excel file with UKGDS data format and build dict array of bus coordinates
and graph structure suitable for plotting with the networkx module.
"""
from numpy import array,where
from pandas import ExcelFile
from networkx import Graph
data = ExcelFile(filename)
bus = data.parse("Buses",header=header)
branch = data.parse("Branches",header=header)
pos = {}
for node in range(len(bus["BNU"])):
pos.update({node:array([bus["BXC"][node],bus["BYC"][node]])})
net = []
for k in range(len(branch["CFB"])):
von = where(bus["BNU"]==branch["CFB"][k])[0][0]
zu = where(bus["BNU"]==branch["CTB"][k])[0][0]
net.append([von,zu])
nodes = set([n1 for n1,n2 in net] + [n2 for n1,n2 in net])
G = Graph()
for node in nodes:
G.add_node(node)
for edge in net:
G.add_edge(edge[0],edge[1])
return G,pos
示例12: compute_molecule
def compute_molecule(universe):
'''
Cluster atoms into molecules.
The algorithm is to create a network graph containing every atom (in every
frame as nodes and bonds as edges). Using this connectivity information,
one can perform a (breadth first) traversal of the network graph to cluster
all nodes (whose indices correspond to physical atoms).
Args:
universe (:class:`~exatomic.universe.Universe`): Atomic universe
Returns:
objs (tuple): Molecule indices (for atom dataframe(s)) and molecule dataframe
Warning:
This function will modify (in place) a few tables of the universe!
'''
if 'bond_count' not in universe.atom: # The bond count is used to find single atoms;
universe.compute_bond_count() # single atoms are treated as molecules.
b0 = None
b1 = None
bonded = universe.two[universe.two['bond'] == True]
if universe.is_periodic:
mapper = universe.projected_atom['atom']
b0 = bonded['prjd_atom0'].map(mapper)
b1 = bonded['prjd_atom1'].map(mapper)
else:
b0 = bonded['atom0']
b1 = bonded['atom1']
graph = Graph()
graph.add_edges_from(zip(b0.values, b1.values))
mapper = {}
for i, molecule in enumerate(connected_components(graph)):
for atom in molecule:
mapper[atom] = i
n = 1
if len(mapper.values()) > 0:
n += max(mapper.values())
else:
n -= 1
idxs = universe.atom[universe.atom['bond_count'] == 0].index
for i, index in enumerate(idxs):
mapper[index] = i + n
# Set the molecule indices
universe.atom['molecule'] = universe.atom.index.map(lambda idx: mapper[idx])
# Now compute molecule table
universe.atom['mass'] = universe.atom['symbol'].map(symbol_to_element_mass)
# The coordinates of visual_atom represent grouped molecules for
# periodic calculations and absolute coordinates for free boundary conditions.
molecules = universe.atom.groupby('molecule')
molecule = molecules['symbol'].value_counts().unstack().fillna(0).astype(np.int64)
molecule.columns.name = None
molecule['frame'] = universe.atom.drop_duplicates('molecule').set_index('molecule')['frame']
molecule['mass'] = molecules['mass'].sum()
del universe.atom['mass']
frame = universe.atom[['molecule', 'frame']].drop_duplicates('molecule')
frame = frame.set_index('molecule')['frame'].astype(np.int64)
molecule['frame'] = frame.astype('category')
return Molecule(molecule)
示例13: summarize
def summarize(text, sentence_count=5, language='english'):
processor = LanguageProcessor(language)
sentence_list = processor.split_sentences(text)
wordset_list = map(processor.extract_significant_words, sentence_list)
stemsets = [
{processor.stem(word) for word in wordset}
for wordset in wordset_list
]
graph = Graph()
pairs = combinations(enumerate(stemsets), 2)
for (index_a, stems_a), (index_b, stems_b) in pairs:
if stems_a and stems_b:
similarity = 1 - jaccard(stems_a, stems_b)
if similarity > 0:
graph.add_edge(index_a, index_b, weight=similarity)
ranked_sentence_indexes = list(pagerank(graph).items())
if ranked_sentence_indexes:
sentences_by_rank = sorted(
ranked_sentence_indexes, key=itemgetter(1), reverse=True)
best_sentences = map(itemgetter(0), sentences_by_rank[:sentence_count])
best_sentences_in_order = sorted(best_sentences)
else:
best_sentences_in_order = range(min(sentence_count, len(sentence_list)))
return ' '.join(sentence_list[index] for index in best_sentences_in_order)
示例14: draw_network
def draw_network(bus,branch,bus_names=None,coordinates=None,ax=None):
"""Generate networkx Graph object and draw network diagram
It is assumed that bus and branch ordering corresponds to PyPower format
"""
from networkx import Graph,draw
from matplotlib.pyplot import show
if isinstance(coordinates,np.ndarray):
pos = {}
if coordinates.shape[0]==2:
coordinates = coordinates.T
for node in range(len(bus)):
pos.update({node:coordinates[node,:]})
else:
pos = None
net = []
for k in range(len(branch)):
von = np.where(bus[:,BUS_I]==branch[k,F_BUS])[0][0]
zu = np.where(bus[:,BUS_I]==branch[k,T_BUS])[0][0]
net.append([von,zu])
nodes = set([n1 for n1,n2 in net] + [n2 for n1,n2 in net])
G = Graph()
for node in nodes:
G.add_node(node)
for edge in net:
G.add_edge(edge[0],edge[1])
draw(G,pos,ax=ax)
show()
示例15: multiple_edges
def multiple_edges(self, new=None):
"""
Get/set whether or not self allows multiple edges.
INPUT:
- ``new`` -- can be a boolean (in which case it sets the value) or
``None``, in which case the current value is returned. It is set to
``None`` by default.
TESTS::
sage: G = sage.graphs.base.graph_backends.NetworkXGraphBackend()
sage: G.multiple_edges(True)
sage: G.multiple_edges(None)
True
"""
if isinstance(self._nxg, (NetworkXGraphDeprecated, NetworkXDiGraphDeprecated)):
self._nxg = self._nxg.mutate()
from networkx import Graph,MultiGraph,DiGraph,MultiDiGraph
if new is None:
return self._nxg.is_multigraph()
if new == self._nxg.is_multigraph():
return
if new:
if self._nxg.is_directed():
self._nxg = MultiDiGraph(self._nxg)
else:
self._nxg = MultiGraph(self._nxg)
else:
if self._nxg.is_directed():
self._nxg = DiGraph(self._nxg)
else:
self._nxg = Graph(self._nxg)