|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from topogram_client import TopogramAPIClient |
| 5 | +from random import randint |
| 6 | +from csv import DictReader |
| 7 | + |
| 8 | +# credentials |
| 9 | +TOPOGRAM_URL = "http://localhost:3000" # "https://app.topogram.io" |
| 10 | + |
| 11 | +PASSWORD = "password" |
| 12 | + |
| 13 | +# data |
| 14 | +my_nodes = [] |
| 15 | +my_edges = [] |
| 16 | + |
| 17 | +with open('data/test_data_nodes.csv') as f : |
| 18 | + reader = DictReader(f) |
| 19 | + for n in reader : |
| 20 | + node = { |
| 21 | + "id" : n["id"], |
| 22 | + "name" : n["name"], |
| 23 | + "lat" : float(n["lat"]), |
| 24 | + "lng" : float(n["lng"]), |
| 25 | + "weight" : float(n["weight"]), |
| 26 | + "start" : n["year_start"], |
| 27 | + "end" : n["year_stop"] |
| 28 | + } |
| 29 | + my_nodes.append({ "data" : node }) |
| 30 | + |
| 31 | +print my_nodes |
| 32 | + |
| 33 | +with open('data/test_data_edges.csv') as f : |
| 34 | + reader = DictReader(f) |
| 35 | + for e in reader : |
| 36 | + edge = { |
| 37 | + "source" : e["source"], |
| 38 | + "target" : e["target"], |
| 39 | + "weight" : float(e["weight"]) |
| 40 | + } |
| 41 | + my_edges.append({ "data" : edge }) |
| 42 | + |
| 43 | +# print my_edges |
| 44 | + |
| 45 | +def create_topogram(title, nodes, edges): |
| 46 | + |
| 47 | + print "Creating topogram '%s'"%title |
| 48 | + |
| 49 | + try : |
| 50 | + r = topogram.create_topogram(title) |
| 51 | + except ValueError: |
| 52 | + print '> Topogram already exists' |
| 53 | + r = topogram.get_topogram_by_name(title) |
| 54 | + |
| 55 | + topogram_ID = r["data"]["_id"] |
| 56 | + print "topogram ID : %s"%topogram_ID |
| 57 | + |
| 58 | + # get and backup existing nodes and edges |
| 59 | + existing_nodes = topogram.get_nodes(topogram_ID)["data"] |
| 60 | + existing_edges = topogram.get_edges(topogram_ID)["data"] |
| 61 | + |
| 62 | + # clear existing graph |
| 63 | + if len(existing_nodes): |
| 64 | + topogram.delete_nodes([n["_id"] for n in existing_nodes]) |
| 65 | + print "%s nodes deleted"%len(existing_nodes) |
| 66 | + if len(existing_edges): |
| 67 | + topogram.delete_edges([n["_id"] for n in existing_edges]) |
| 68 | + print "%s edges deleted"%len(existing_edges) |
| 69 | + |
| 70 | + r = topogram.create_nodes(topogram_ID, nodes) |
| 71 | + print r |
| 72 | + print "%s nodes created."%len(r["data"]) |
| 73 | + r = topogram.create_edges(topogram_ID, edges) |
| 74 | + print "%s edges created."%len(r["data"]) |
| 75 | + |
| 76 | + print "done. Topogram has been updated. Check it at %s/topograms/%s"%(TOPOGRAM_URL, topogram_ID) |
| 77 | + |
| 78 | +# connect to the topogram instance (pass debug=True params for more info ) |
| 79 | +topogram = TopogramAPIClient(TOPOGRAM_URL) #, debug=True) |
| 80 | + |
| 81 | +# create a new user |
| 82 | +try : |
| 83 | + topogram.create_user(USER, PASSWORD) |
| 84 | +except ValueError: |
| 85 | + print "> User has already been created." |
| 86 | + |
| 87 | +# login a new user if needed |
| 88 | +resp_user_login = topogram.user_login(USER, PASSWORD) |
| 89 | +print resp_user_login |
| 90 | + |
| 91 | +assert(resp_user_login["status"] == "success") |
| 92 | +assert(resp_user_login["status_code"] == 200) |
| 93 | + |
| 94 | +create_topogram("Geo-time network", my_nodes, my_edges) |
0 commit comments