Mercurial > hg > drupalISMI
annotate importFromOpenMind/importer/model2neo4j.py @ 31:48bbba800c03
remove unused method.
author | casties |
---|---|
date | Thu, 21 Jan 2016 18:24:51 +0100 |
parents | 1a1877812757 |
children | 7e2e344c3b87 |
rev | line source |
---|---|
23 | 1 import networkx as nx |
29
1a1877812757
include normalized attributes in neo4j with prefix "_n_"
casties
parents:
28
diff
changeset
|
2 from neo4jrestclient.client import GraphDatabase |
23 | 3 import sys |
4 | |
5 ## configure behaviour | |
6 | |
7 # metworkx graph file | |
8 input_fn = 'ismi_graph.gpickle' | |
9 | |
10 # label added to all nodes | |
28 | 11 project_label = '_ismi' |
23 | 12 |
13 # neo4j base URL | |
14 neo4jBaseURL = "http://localhost:7474/db/data/" | |
15 | |
16 | |
17 ## setup | |
18 | |
19 n4j_nodes = {} | |
20 n4j_relations = {} | |
21 | |
22 | |
23 def copyNodes(nx_graph, n4j_graph): | |
24 """copy all nodes from nx_graph to n4j_graph""" | |
25 | |
26 print("Copying nodes to Neo4J") | |
27 cnt = 0 | |
28 for node_id in nx.nodes_iter(nx_graph): | |
29 attrs = nx_graph.node[node_id] | |
29
1a1877812757
include normalized attributes in neo4j with prefix "_n_"
casties
parents:
28
diff
changeset
|
30 ntype = attrs['type'] |
23 | 31 ismi_id = attrs['ismi_id'] |
32 # create node with attributes | |
33 n4j_node = n4j_graph.nodes.create(**attrs) | |
34 # add labels | |
29
1a1877812757
include normalized attributes in neo4j with prefix "_n_"
casties
parents:
28
diff
changeset
|
35 n4j_node.labels.add([project_label, ntype]) |
23 | 36 # save reference |
37 n4j_nodes[ismi_id] = n4j_node | |
38 | |
39 cnt += 1 | |
40 if cnt % 100 == 0: | |
25
5bdcb5805d29
updated openmind-networkx-neo4j conversion with dates, locations and links.
casties
parents:
23
diff
changeset
|
41 print(" %s nodes"%cnt) |
23 | 42 |
43 | |
44 def copyRelations(nx_graph, n4j_graph): | |
45 """copy all relations from nx_graph to n4j_graph""" | |
46 | |
47 print("Copying relations to Neo4J") | |
48 cnt = 0 | |
49 for nx_edge in nx.edges_iter(nx_graph): | |
50 (nx_src, nx_tar) = nx_edge | |
51 # get attributes of edge | |
52 attrs = nx_graph.edge[nx_src][nx_tar][0] | |
29
1a1877812757
include normalized attributes in neo4j with prefix "_n_"
casties
parents:
28
diff
changeset
|
53 ntype = attrs['type'] |
23 | 54 # get ismi_id of source and target nodes |
55 src_id = nx_graph.node[nx_src]['ismi_id'] | |
56 tar_id = nx_graph.node[nx_tar]['ismi_id'] | |
57 # get Neo4J nodes | |
58 src = n4j_nodes.get(src_id, None) | |
59 if src is None: | |
60 print("ERROR: src node %s missing!"%src_id) | |
61 break | |
62 | |
63 tar = n4j_nodes.get(tar_id, None) | |
64 if tar is None: | |
65 print("ERROR: tar node %s missing!"%tar_id) | |
66 break | |
67 | |
68 # create Neo4J relation | |
29
1a1877812757
include normalized attributes in neo4j with prefix "_n_"
casties
parents:
28
diff
changeset
|
69 n4j_rel = n4j_graph.relationships.create(src, ntype, tar) |
23 | 70 # add attributes |
71 n4j_rel.properties = attrs | |
72 | |
73 cnt += 1 | |
74 if cnt % 100 == 0: | |
25
5bdcb5805d29
updated openmind-networkx-neo4j conversion with dates, locations and links.
casties
parents:
23
diff
changeset
|
75 print(" %s relations"%cnt) |
23 | 76 |
77 | |
78 ## main | |
79 | |
80 print("Copy graph from networkx to Neo4J") | |
81 | |
82 # read commandline parameters | |
83 if len(sys.argv) > 1: | |
84 input_fn = sys.argv[1] | |
85 | |
86 # read networkx graph from pickle | |
87 print("Reading graph from %s"%input_fn) | |
88 nx_graph = nx.read_gpickle(input_fn) | |
89 print("Graph info: %s"%nx.info(nx_graph)) | |
90 | |
91 # open neo4j graph db | |
92 print("Opening Neo4J db at %s"%neo4jBaseURL) | |
93 n4j_graph = GraphDatabase(neo4jBaseURL, username="neo4j", password="neo5j") | |
94 | |
95 copyNodes(nx_graph, n4j_graph) | |
96 | |
97 copyRelations(nx_graph, n4j_graph) | |
98 | |
99 print("Done.") |