lecture Networkx(python) – Cytoscape – CSV

  1. cytoscape→networkx
    1. 隣接行列,隣接リストをCSVで書き出して読み込む
    2. graphml書き出して読み込む
    3. (レイアウトも一緒に読み込みたいならば)cyjsで書き出していろいろ呪文を唱えて読み込む
  2. networkx→cytoscape
    1. 隣接行列,隣接リストをCSVで書き出して読み込む
    2. graphml書き出して読み込む
    3. (レイアウトも一緒に読み込みたいならば)cyjsで書き出していろいろ呪文を唱えて読み込む

呪文の一部

def from_networkx(g, layout=None, scale=DEF_SCALE):
 # Dictionary Object to be converted to Cytoscape.js JSON
 cygraph = __build_empty_graph()

 if layout is not None:
 pos = list(map(lambda position:
 {'x': position[0]*scale, 'y': position[1]*scale},
 layout.values()))

 nodes = g.nodes()
 if isinstance(g, nx.MultiDiGraph) or isinstance(g, nx.MultiGraph):
 edges = g.edges(data=True, keys=True)
 edge_builder = __build_multi_edge
 else:
 edges = g.edges(data=True)
 edge_builder = __build_edge

 # Map network table data
 cygraph[DATA] = __map_table_data(g.graph.keys(), g.graph)

 for i, node_id in enumerate(nodes):
 new_node = __create_node(g.node[node_id], node_id)
 if layout is not None:
 new_node['position'] = pos[i]

 cygraph['elements']['nodes'].append(new_node)

 for edge in edges:
 cygraph['elements']['edges'].append(edge_builder(edge, g))

 return cygraph


def to_networkx(cyjs, directed=True):
 """
 Convert Cytoscape.js-style JSON object into NetworkX object.

 By default, data will be handles as a directed graph.
 """

 if directed:
 g = nx.MultiDiGraph()
 else:
 g = nx.MultiGraph()

 network_data = cyjs[DATA]
 if network_data is not None:
 for key in network_data.keys():
 g.graph[key] = network_data[key]

 nodes = cyjs[ELEMENTS][NODES]
 edges = cyjs[ELEMENTS][EDGES]

 for node in nodes:
 data = node[DATA]
 data[POSITION] = [node[POSITION][X], node[POSITION][Y]] #set position as attribute
 g.add_node(data[ID], attr_dict=data)

 for edge in edges:
 data = edge[DATA]
 source = data[SOURCE]
 target = data[TARGET]

 g.add_edge(source, target, attr_dict=data)

 return g

 

 

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA