topogram / topogram-api-client

Python API client for Topogram
http://topogram.readthedocs.io/
1 stars 2 forks source link

GraphML support #3

Open ungentilgarcon opened 8 years ago

ungentilgarcon commented 8 years ago

We need to add support for GraphML file format.

clemsos commented 8 years ago

Support what, how ? why ?

ungentilgarcon commented 8 years ago

Well GraphML is standard input for gephi like networks, standard output of R based stat tools.

this import function would enable to push large amount of graphml calculated files in topo .

for an example https://github.com/uskudnik/GraphGL/blob/master/examples/graphml-to-json.py

clemsos commented 8 years ago

Yes, GraphML is Gephi standard and already supported by NetworkX and other libs : https://networkx.github.io/documentation/networkx-1.10/reference/readwrite.graphml.html

No need for built-in support, you can just parse a graphML file with nx and push it to Topogram using Python.

Here an (untested) example

import networkx as nx
from topogram_client import TopogramAPIClient

# read graphML file
with open("network.graphml") as f:
    G = nx.read_graphml(f)

# passwords
TOPOGRAM_URL = "https://app.topogram.io" # "http://localhost:3000"
USER = "xxx@xxx.com"
PASSWORD = "xxx"

# connect to the topogram instance 
topogram = TopogramAPIClient(TOPOGRAM_URL)

# topogram.create_user(USER, PASSWORD)
topogram.user_login(USER, PASSWORD)

# create the graph
nodes = []
for n in G.nodes(data=True): 
    node = n[1]
    node["id"] = n[0]
    node["group"] = n[1]["type"]
    nodes.append(node)

print "creating %s nodes ..."%len(nodes)
r = topogram.create_nodes(topogram_ID, nodes)

edges = []
for e in G.edges(data=True): 
    edge = e[2]
    edge["source"] = e[0]
    edge["target"] = e[1]
    edges.append(edge)

print "creating %s edges ..."%len(edges)
r = topogram.create_edges(topogram_ID, edges)

print "done. Topogram is online at %s/topograms/%s"%(TOPOGRAM_URL,topogram_ID)
clemsos commented 8 years ago

Docs : http://topogram.readthedocs.io/en/latest/