本文整理汇总了Python中networkx.readwrite.json_graph.node_link_data方法的典型用法代码示例。如果您正苦于以下问题:Python json_graph.node_link_data方法的具体用法?Python json_graph.node_link_data怎么用?Python json_graph.node_link_data使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.readwrite.json_graph
的用法示例。
在下文中一共展示了json_graph.node_link_data方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def __init__(self, G, pos, ax,
gravity=1,
link_distance=20,
charge=-30,
node_size=5,
link_strength=1,
friction=0.9,
draggable=True):
if pos is None:
pass
self.dict_ = {"type": "networkxd3forcelayout",
"graph": node_link_data(G),
"ax_id": mpld3.utils.get_id(ax),
"gravity": gravity,
"charge": charge,
"friction": friction,
"link_distance": link_distance,
"link_strength": link_strength,
"draggable": draggable,
"nominal_radius": node_size}
示例2: compute_label_smoothness
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def compute_label_smoothness(path, rate=0.):
G_org = json_graph.node_link_graph(json.load(open(path+'-G.json')))
# G_org = remove_unlabeled(G_org)
if nx.is_directed(G_org):
G_org = G_org.to_undirected()
class_map = json.load(open(path+'-class_map.json'))
for k, v in class_map.items():
if type(v) != list:
class_map = convert_list(class_map)
break
labels = convert_ndarray(class_map)
labels = np.squeeze(label_to_vector(labels))
# smooth
G_org = label_broadcast(G_org, labels, rate)
with open(path+'-G_'+str(rate)+'.json', 'w') as f:
f.write(json.dumps(json_graph.node_link_data(G_org)))
edge_num = G_org.number_of_edges()
G = pygsp.graphs.Graph(nx.adjacency_matrix(G_org))
smoothness = 0
for src, dst in G_org.edges():
if labels[src] != labels[dst]:
smoothness += 1
print('The smoothness is: ', 2*smoothness/edge_num)
示例3: serialize
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def serialize(g: nx.MultiDiGraph) -> Dict:
"""
Convert networkx.MultiDiGraph as a dictionary.
Parameters
----------
g: networkx.MultiDiGraph
Graph to convert as a dictionary
Returns
-------
dict
A dictionary
"""
data = json_graph.node_link_data(g)
return data
示例4: save_json_tree
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def save_json_tree(filepath, tree):
"""
Transform inferred phylogeny to JSON object and save it to a file
:param filepath: path to the output file
:param tree: reconstructed phylogeny given as networkx DiGraph object
"""
# create simplified JSON tree
out_ids = dict()
out_tree = nx.DiGraph()
for out_id, node in enumerate(tree.nodes()):
out_ids[node] = out_id
out_tree.add_node(out_id, name=tree.node[node]['name'])
for u, v in tree.edges():
out_tree.add_edge(out_ids[u], out_ids[v], value=len(tree.edge[u][v]['muts']))
# create json output from reconstructed phylogeny
json_data = json_graph.node_link_data(out_tree)
# json object to output file
with open(filepath, 'w') as json_file:
json.dump(json_data, json_file, indent=4)
logger.info('Create JSON file from reconstructed phylogeny: {}.'.format(filepath))
示例5: serialize_graph
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def serialize_graph(graph):
"""Make string."""
json_data = json_graph.node_link_data(graph)
serial_data = json.dumps(json_data,
separators=(',', ':'),
indent=4,
cls=SetEncoder)
return serial_data
示例6: node_link_data_to_eden
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def node_link_data_to_eden(input=None, options=dict()):
"""
Takes a string list in the serialised node_link_data JSON format and yields networkx graphs.
Parameters
----------
input : string
A pointer to the data source.
"""
return _node_link_data_to_eden(util.read(input))
示例7: _node_link_data_to_eden
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def _node_link_data_to_eden(serialized_list):
"""Takes a string list in the serialised node_link_data JSON format and yields networkx graphs."""
for serial_data in serialized_list:
py_obj = json.loads(serial_data)
graph = json_graph.node_link_graph(py_obj)
yield graph
示例8: eden_to_node_link_data
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def eden_to_node_link_data(graph_list):
"""Takes a list of networkx graphs and yields serialised node_link_data JSON strings."""
for G in graph_list:
json_data = json_graph.node_link_data(G)
serial_data = json.dumps(json_data)
yield serial_data
示例9: eden_to_node_link_file
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def eden_to_node_link_file(graph_list, fname):
"""Takes a list of networkx graphs and writes a serialised node_link_data JSON file."""
with open(fname, 'w') as f:
serials = eden_to_node_link_data(graph_list)
for serial in serials:
f.write('%s\n' % serial)
示例10: get_bundle
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def get_bundle(self, user):
graph = self.build_graph(user)
return json_graph.node_link_data(graph)
示例11: tryout_web_graphs
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def tryout_web_graphs(self, infr):
"""
https://plot.ly/python/
http://bokeh.pydata.org/en/latest/
pip install bokeh
Notes:
http://www.coppelia.io/2014/07/an-a-to-z-of-extra-features-for-the-d3-force-layout/
http://andrewmellor.co.uk/blog/articles/2014/12/14/d3-networks/
pip install plotly # eww need to sign up and get a key
http://igraph.org/
import mpld3
mpld3.save_html(fig, open('fig.html', 'w'))
mpld3.save_json(fig, open('fig.json', 'w'))
fig = pt.gcf()
"""
#import plottool_ibeis as pt
# http://andrewmellor.co.uk/blog/articles/2014/12/14/d3-networks/
from networkx.readwrite import json_graph
G = infr.graph
data = json_graph.node_link_data(G)
json_text = ut.to_json(data, pretty=True)
ut.writeto('graph.json', json_text)
ut.editfile('graph.json')
ut.startfile('d3_example.html')
# d3_location = ut.grab_zipped_url('https://github.com/d3/d3/releases/download/v3.5.17/d3.zip')
# python -m SimpleHTTPServer 8000
示例12: save_js_arc
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def save_js_arc(reduced_CC_graph, channels_hash, output_directory, output_file_name):
"""
Saves the nx_graph as a js file with variable name communities, it is used in index.html to generate the arc graph
Args:
nx_graph: a networkx graph, here it is the reduced community community graph
channels_hash(dict): list of channel names
output_drectory(str): location where to save the file
output_file_name(str): name of the file to be saved
Returns:
null
"""
check_if_dir_exists(output_directory) #create output directory if doesn't exist
copy2("./lib/protovis/" + "arc_graph.html", output_directory) #copy required files to output_directory
copy2("./lib/protovis/" + "ex.css", output_directory)
copy2("./lib/protovis/" + "protovis-r3.2.js", output_directory)
jsondict = json_graph.node_link_data(reduced_CC_graph)
max_weight_val = max(item['weight'] for item in jsondict['links'])
# the key names in the jsondict_top_channels are kept as the following so that index.html can render it
jsondict_top_channels = {}
jsondict_top_channels['nodes'] = [{'nodeName':channels_hash[int(node['id']) - config.STARTING_HASH_CHANNEL]} for node in jsondict['nodes']]
jsondict_top_channels['links'] = [{'source': link['source'], 'target': link['target'],\
'value': int(link['weight'] * config.EXPANSION_PARAMETER / float(max_weight_val))} for link in jsondict['links']]
with open(output_directory + output_file_name, 'w') as f:
f.write("var communities =")
json.dump(jsondict_top_channels, f)
示例13: json_G
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def json_G(self, G):
return json_graph.node_link_data(G)
示例14: to_dict
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def to_dict(self):
return json_graph.node_link_data(self.graph)
示例15: json_output_graph
# 需要导入模块: from networkx.readwrite import json_graph [as 别名]
# 或者: from networkx.readwrite.json_graph import node_link_data [as 别名]
def json_output_graph(self, **kwargs):
"""supports both 1.10<=networkx<2.0 and networx>=2.0 by returning the
same json output regardless networx version
:return: graph in json format
"""
# TODO(annarez): when we decide to support networkx 2.0 with
# versioning of Vitrage, we can move part of the logic to vitrageclient
node_link_data = json_graph.node_link_data(self._g)
node_link_data.update(kwargs)
vitrage_id_to_index = dict()
for index, node in enumerate(node_link_data['nodes']):
vitrage_id_to_index[node[VProps.VITRAGE_ID]] = index
if VProps.ID in self._g.nodes[node[VProps.ID]]:
node[VProps.ID] = self._g.nodes[node[VProps.ID]][VProps.ID]
node[VProps.GRAPH_INDEX] = index
vers = nx.__version__
if vers >= '2.0':
for i in range(len(node_link_data['links'])):
node_link_data['links'][i]['source'] = vitrage_id_to_index[
node_link_data['links'][i]['source']]
node_link_data['links'][i]['target'] = vitrage_id_to_index[
node_link_data['links'][i]['target']]
if kwargs.get('raw', False):
return node_link_data
else:
return json.dumps(node_link_data)