-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgraphcommons.py
More file actions
271 lines (209 loc) · 9.87 KB
/
graphcommons.py
File metadata and controls
271 lines (209 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from collections import UserDict
from requests.api import request
from itertools import chain
class Entity(UserDict):
"""Base class for all objects."""
class Signal(Entity):
"""Represents a signal operation in the Graph Commons infrastructure."""
class Path(Entity):
"""Represents a path object in the Graph Commons infrastructure."""
class Edge(Entity):
"""Represents an edge object in the Graph Commons infrastructure."""
def to_signal(self, action, graph):
# signal types: create or update
action = "edge_%s" % action
from_id, to_id = map(self.get, ('from', 'to'))
from_node = graph.get_node(from_id)
to_node = graph.get_node(to_id)
kwargs = dict(action=action,
name=self['edge_type'],
from_name=from_node['name'],
from_type=from_node['type'],
to_name=to_node['name'],
to_type=to_node['type'],
reference=self.get('reference', None),
weight=self.get('weight'),
properties=self['properties'])
return Signal(**kwargs)
class Node(Entity):
"""Represents a node object in the Graph Commons infrastructure."""
def to_signal(self, action, graph):
# signal types: create or update
action = "node_%s" % action
node_type = graph.get_node_type(self['type_id'])
kwargs = dict(action=action,
name=self['name'],
type=self['type'],
reference=self.get('reference', None),
image=self.get('image', None),
color=node_type['color'],
url=self.get('url', None),
description=self['description'],
properties=self['properties'])
return Signal(**kwargs)
class EdgeType(Entity):
"""Represents an edge type object in the Graph Commons infrastructure."""
def to_signal(self, action):
action = "edgetype_%s" % action
kwargs = dict(action=action)
keys = ['name', 'color', 'name_alias', 'weighted', 'properties',
'image_as_icon', 'image']
kwargs.update(dict((k, self.get(k, None)) for k in keys))
return Signal(**kwargs)
class NodeType(Entity):
"""Represents a node type object in the Graph Commons infrastructure."""
def to_signal(self, action):
action = "nodetype_%s" % action
kwargs = dict(action=action)
keys = ['name', 'color', 'name_alias', 'size_limit', 'properties',
'image_as_icon', 'image', 'size']
kwargs.update(dict((k, self.get(k, None)) for k in keys))
return Signal(**kwargs)
class Graph(Entity):
def __init__(self, *args, **kwargs):
super(Graph, self).__init__(*args, **kwargs)
self.edges = list(map(Edge, self['edges'] or []))
self.nodes = list(map(Node, self['nodes'] or []))
self.node_types = list(map(NodeType, self['nodeTypes'] or []))
self.edge_types = list(map(EdgeType, self['edgeTypes'] or []))
# hash for quick search
self._edges = dict((edge['id'], edge) for edge in self.edges)
self._nodes = dict((node['id'], node) for node in self.nodes)
self._node_types = dict((t['id'], t) for t in self.node_types)
self._edge_types = dict((t['id'], t) for t in self.edge_types)
def get_node(self, node_id):
return self._nodes.get(node_id, None)
def get_edge_type(self, edge_type_id):
return self._edge_types.get(edge_type_id, None)
def get_node_type(self, node_type_id):
return self._node_types.get(node_type_id, None)
def edges_for(self, node, direction):
if isinstance(node, str):
node = self.get_node(node)
return [edge for edge in self.edges
if edge[direction] == node['id']]
def edges_from(self, node):
return self.edges_for(node, 'from')
def edges_to(self, node):
return self.edges_for(node, 'to')
def sync(self, graph_commons):
"""Synchronize local and remote representations."""
if self['id'] is None:
return
remote_graph = graph_commons.graphs(self['id'])
# TODO: less forceful, more elegant
self.edges = remote_graph.edges
self.nodes = remote_graph.nodes
self.node_types = remote_graph.node_types
self.edge_types = remote_graph.edge_types
self._edges = dict((edge['id'], edge) for edge in self.edges)
self._nodes = dict((node['id'], node) for node in self.nodes)
self._node_types = dict((t['id'], t) for t in self.node_types)
self._edge_types = dict((t['id'], t) for t in self.edge_types)
class GraphCommonsException(Exception):
def __init__(self, status_code, message):
self.status_code = status_code
# if isinstance(message, unicode):
# message = message.encode("utf-8") # Otherwise, it will not be printed.
self.message = message
def __str__(self):
return self.message
class GraphCommons(object):
BASE_URL = "https://graphcommons.com/api/v1"
ERROR_CODES = [400, 401, 403, 404, 405, 500]
def __init__(self, api_key, base_url=None):
self.api_key = api_key
self.base_url = base_url or self.BASE_URL
def build_url(self, endpoint, id=None):
parts = filter(bool, [self.base_url, endpoint, id])
return '/'.join(parts)
@staticmethod
def get_error_message(response):
try:
bundle = response.json()
except (ValueError, TypeError):
return response.content
return bundle['msg']
def make_request(self, method, endpoint, data=None, id=None):
response = request(method, self.build_url(endpoint, id), json=data,
headers={"Authentication": self.api_key,
"Content-Type": "application/json"
}
)
if response.status_code in self.ERROR_CODES:
raise GraphCommonsException(status_code=response.status_code,
message=GraphCommons.get_error_message(response))
return response
def status(self):
response = self.make_request('get', 'status')
return Entity(**response.json())
def graphs(self, id=None):
response = self.make_request('get', 'graphs', id=id)
return Graph(**response.json()['graph'])
def nodes(self, id=None):
response = self.make_request('get', 'nodes', id=id)
return Node(**response.json()['node'])
def new_graph(self, signals=None, **kwargs):
if signals is not None:
kwargs['signals'] = list(map(dict, signals))
response = self.make_request('post', 'graphs', data=kwargs)
return Graph(**response.json()['graph'])
def update_graph(self, id, signals=None, **kwargs):
if signals is not None:
kwargs['signals'] = list(map(dict, signals))
endpoint = 'graphs/%s/add' % id
response = self.make_request('put', endpoint, data=kwargs)
return Graph(**response.json()['graph'])
def clear_graph(self, graph_id):
graph = self.graphs(graph_id)
# Remove all nodes. (This also removes all edges.)
signals = map(lambda node: dict(action="node_delete", id=node.id), graph.nodes)
endpoint = "graphs/{}/add".format(graph_id)
kwargs = dict(signals=signals)
response = self.make_request("put", endpoint, data=kwargs)
return Graph(**response.json()['graph'])
def create_graph_from_path(self, name, paths, base_graph):
kwargs = dict((k, base_graph.get(k, None)) for k in ['status', 'license', 'users', 'layout'])
kwargs['name'] = name
subtitle = ""
description = ""
edges = []
nodes = []
edge_type_ids = []
node_type_ids = []
for path in paths:
description = u"{}\n{}".format(description, path.path_string)
subtitle = u"{}\n{}".format(subtitle, path.path_string)
# Type ids
edge_type_ids.extend([edge.type_id for edge in path.edges])
node_type_ids.extend([node.type['id'] for node in path.nodes])
edges.extend(path.edges)
nodes.extend(path.nodes)
# Add explanation of original graph.
kwargs['description'] = u"{}\n{}".format(description, base_graph.description)
kwargs['subtitle'] = u"{}\n{}".format(subtitle, base_graph.subtitle)
# Get edge_types by using their ids.
edge_types = map(base_graph.get_edge_type, set(edge_type_ids))
node_types = map(base_graph.get_node_type, set(node_type_ids))
# Add node and edge type Signals first.
signals = map(lambda entity: entity.to_signal('create'), chain(node_types, edge_types))
edge_ids = set()
node_ids = set()
# Remove duplicates for efficiency in graph creation.
edges = [e for e in edges if not (e.id in edge_ids or edge_ids.add(e.id))]
nodes = [n for n in nodes if not (n.id in node_ids or node_ids.add(n.id))]
# Add node and edge Signals.
signals.extend(map(lambda entity: entity.to_signal('create', base_graph), chain(edges, nodes)))
return self.new_graph(signals=signals, **kwargs)
def paths(self, graph_id, kwargs):
end_point = 'graphs/%s/paths' % graph_id
response = self.make_request("get", end_point, data=kwargs).json()
edges = response['edges']
nodes = response['nodes']
paths = []
for path in response['paths']:
p = {'edges': map(Edge, (edges[edge_id] for edge_id in path['edges'])),
'nodes': map(Node, (nodes[node_id] for node_id in path['nodes'])), 'dirs': path['dirs'],
'path_string': path['path_string']}
paths.append(Path(**p))
return paths