forked from AdriaanRol/AutoDepGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.py
More file actions
509 lines (432 loc) · 18.4 KB
/
graph.py
File metadata and controls
509 lines (432 loc) · 18.4 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import logging
import numpy as np
import types
from typing import Dict, Any, List, Optional
from datetime import datetime
import matplotlib.pyplot as plt
from os.path import join, split
import os
import tempfile
import webbrowser
import warnings
import networkx as nx
import autodepgraph
from autodepgraph.visualization import state_cmap
from autodepgraph import visualization as vis
# Used to find functions in modules
from importlib import import_module
try:
# Only used for finding instrument methods.
from qcodes.instrument.base import Instrument
except ImportError:
Instrument = None
class AutoDepGraph_DAG(nx.DiGraph):
"""
Attributes:
---------------
node_states:
Allowed states for the nodes
matplotlib_edge_properties:
Properties passed to networkx plotting of edges
matplotlib_label_properties:
Properties passed to networkx plotting of labels
"""
node_states: List[str] = ['good', 'needs calibration',
'bad', 'unknown', 'active']
matplotlib_edge_properties: Dict[str, Any] = {'edge_color': 'k', 'alpha': .8}
matplotlib_label_properties: Dict[str, Any] = {'font_color': 'k'}
def __init__(self, name, cfg_plot_mode='svg',
incoming_graph_data=None, **attr):
"""
Directed Acyclic Graph used for calibrations.
Inherits from a networkx DiGraph.
The AutoDepGraph provides several constraints to nodes and edges
as well as add the functionality to walk over the graph in order to
calibrate a node and visualize the graph in real-time.
"""
attr['name'] = name
self.cfg_plot_mode = cfg_plot_mode
self.cfg_plot_mode_args = {'fig': None}
super().__init__(incoming_graph_data, **attr)
# internal attributes
self._DiGraphWindow = None # used for pyqtgraph plotting
self._graph_changed_since_plot = True # used for pyqtgraph plotting
# counters to count how often functions get called for debugging
# purposes
self._exec_cnt = 0
self._calib_cnt = 0
self._check_cnt = 0
@property
def cfg_svg_filename(self):
"""
Default location for storing svg based visualizations of the DAG.
"""
_path_name = split(__file__)[:-1][0]
return join(_path_name, 'svg_viewer', 'adg_graph.svg')
def fresh_copy(self):
return AutoDepGraph_DAG(name=self.name,
cfg_plot_mode=self.cfg_plot_mode)
def add_node(self, node_for_adding, **attr):
"""
Adds a node to the graph, including starting attributes.
attr:
name (type) = default_value
calibrate_function = 'NotImplementedCalibration'
check_functions = 'return_fixed_value'
tolerance (float) = 0
timeout (float) = np.inf
state (str) = 'unknown'
"""
# there are set here to ensure these node attributes exist.
# setting in this way will most likely interfere
# with joining multiple graphs
attr['timeout'] = attr.get('timeout',
np.inf)
attr['calibrate_function'] = attr.get(
'calibrate_function',
'autodepgraph.node_functions.calibration_functions' +
'.NotImplementedCalibration')
attr['calibrate_function_args'] = attr.get(
'calibrate_function_args', None)
attr['check_function'] = attr.get(
'check_function',
'autodepgraph.node_functions.check_functions' +
'.return_fixed_value')
# zero default tolerance -> always recalibrate
attr['tolerance'] = attr.get('tolerance', 0)
super().add_node(node_for_adding, **attr)
self.set_node_state(node_for_adding,
state=attr.get('state', 'unknown'))
# this adds a helper method to the attributes for quick access
self._construct_maintenance_methods(nodes=[node_for_adding])
def _construct_maintenance_methods(self, nodes):
for n in nodes:
self._construct_maintenance_method(node_name=n)
def _construct_maintenance_method(self, node_name):
node_name_no_space = node_name.replace(' ', '_').replace('-', '_')
# This name exists so that text based storing falls back
# on the right placeholder function
def _construct_maintenance_method():
self.maintain_node(node_name)
self.__setattr__('maintain_{}'.format(node_name_no_space),
_construct_maintenance_method)
def add_edge(self, u_of_edge, v_of_edge, **attr):
"""
Adds an edge that denotes a dependency in the calibration graph.
u_of_edge -> v_of_edge denotes that u depends on v.
"""
# Nodes must already exist to ensure they have the right properties
if u_of_edge not in self.nodes():
raise KeyError('{} not in nodes'.format(u_of_edge))
if v_of_edge not in self.nodes():
raise KeyError('{} not in nodes'.format(v_of_edge))
super().add_edge(u_of_edge, v_of_edge, **attr)
def get_node_state(self, node_name):
Delta_T = (datetime.now() -
self.nodes[node_name]['last_update']).total_seconds()
if (Delta_T > self.nodes[node_name]['timeout']):
self.nodes[node_name]['state'] = 'unknown'
return self.nodes[node_name]['state']
def set_node_state(self, node_name, state, update_monitor=True):
if state not in self.node_states:
raise IndexError(f'state {state} not in {self.node_states}')
self.nodes[node_name]['state'] = state
self.nodes[node_name]['last_update'] = datetime.now()
if update_monitor:
self.update_monitor()
def is_manual_node(self, node_name):
if isinstance(self.nodes[node_name]['calibrate_function'], (types.MethodType, types.FunctionType)):
return False
elif 'manual' in self.nodes[node_name]['calibrate_function']:
return True
elif 'NotImplemented' in self.nodes[node_name]['calibrate_function']:
return True
else:
return False
def maintain_node(self, node: str, verbose=False) -> str:
"""
Maintaining a node attempts to go from any state to a good state.
any_state -> good
Maintain a node performs the following steps:
1. get the state of the dependency nodes. If all are OK or unknown,
perform a check on the node itself.
If a node is any other state, execute it to move it to a
good state
2. perform the "check" experiment on the node itself. This quick
check
3. Perform calibration and second round of maintaining dependencies
Returns:
State of the node after maintaining the node
Raises:
Exception if the node could not be calibrated
"""
self._exec_cnt += 1
if verbose:
print('Maintaining node "{}".'.format(node))
# 1. Going over the states of all the required nodes and ensure
# these are all in a 'Good' state.
for req_node_name in self.adj[node]:
req_node_state = self.nodes[req_node_name]['state']
if req_node_state in ['good', 'unknown']:
continue # assume req_node is in a good state
else: # maintaining the node to ensure it is in a good state
req_node_state = self.maintain_node(req_node_name,
verbose=verbose)
if req_node_state == 'bad':
raise ValueError('Could not calibrate "{}"'.format(
req_node_name))
# 2. Once all required nodes are OK, determine action to be taken
state = self.nodes[node]['state']
if state == 'needs calibration':
# action is clear, no check required
pass
else:
# determine latest status of node
state = self.check_node(node, verbose=verbose)
# 3. Take action based on the stae of the node
if state == 'needs calibration':
cal_succes = self.calibrate_node(node, verbose=verbose)
# the calibration can still fail if dependencies that were good
# or unknown were bad. In that case all dependencies will
# explicitly be executed and calibration will be retried
if not cal_succes:
state = 'bad'
if verbose:
print('Initial calibration of "{}" failed, '
'retrying.'.format(node))
if state == 'bad':
# if the state is bad it will execute *all* dependencies. Even
# the ones that were updated before.
if verbose:
print('State of node "{}" is bad, maintaining all required'
' nodes.'.format(node))
for req_node_name in self.adj[node]:
req_node_state = self.maintain_node(req_node_name,
verbose=verbose)
cal_succes = self.calibrate_node(node, verbose=verbose)
if not cal_succes:
raise ValueError(
'Calibration of "{}" failed.'.format(node))
state = self.nodes[node]['state']
return state
def check_node(self, node, verbose=False):
""" Perform check method on specified node
Args:
node: Node to check
verbose: Verbosity level
Returns:
Returns node state after the check
"""
if verbose:
print('\tChecking node {}.'.format(node))
self.set_node_state(node, 'active')
func = _get_function(self.nodes[node]['check_function'])
result = func()
if isinstance(result, float):
if result < self.nodes[node]['tolerance']:
self.set_node_state(node, 'good')
if verbose:
print('\tNode {} is within tolerance.'.format(node))
else:
self.set_node_state(node, 'needs calibration')
if verbose:
print('\tNode {} needs calibration.'.format(node))
elif result == False:
if verbose:
print('\tNode {} is in a "bad" state.'.format(node))
# if the function returns False it means the check is broken
self.set_node_state(node, 'bad')
else:
raise ValueError('Expected float or "False", '
'result is: {}'.format(result))
return self.nodes[node]['state']
def calibrate_node(self, node: str, verbose: bool = False):
""" Calibrate specified node
Args:
node: Node to calibration
verbose: Verbosity level
Returns:
Returns True if the calibration was succesfull, otherwise False
"""
if verbose:
print('\tCalibrating node {}.'.format(node))
self.set_node_state(node, 'active')
func = _get_function(self.nodes[node]['calibrate_function'])
try:
if 'calibrate_function_args' not in self.nodes[node] or self.nodes[node]['calibrate_function_args'] is None:
result = func()
else:
result = func(**self.nodes[node]['calibrate_function_args'])
except Exception as e:
self.set_node_state(node, 'bad')
logging.warning(e)
return False
if result:
self.set_node_state(node, 'good')
if verbose:
print('\tCalibration of node {} successful.'.format(node))
return True
else:
self.set_node_state(node, 'bad')
if verbose:
print('\tCalibration of node {} failed.'.format(node))
return False
def set_all_node_states(self, state):
for node_dat in self.nodes.values():
node_dat['state'] = state
def update_monitor(self):
if self.cfg_plot_mode == 'matplotlib':
self.update_monitor_mpl()
elif self.cfg_plot_mode == 'svg':
self.draw_svg()
elif self.cfg_plot_mode is None or self.cfg_plot_mode == 'None':
return
else:
raise ValueError('cfg_plot_mode should be in ["matplotlib",'
' "svg", "None" ]')
def update_monitor_mpl(self):
"""
Updates a plot using the draw_graph_mpl based on matplotlib.
"""
fig = self.cfg_plot_mode_args.get('fig', None)
if fig is not None:
plt.figure(fig)
plt.clf()
self.draw_mpl(plt.gca())
plt.draw()
plt.pause(.01)
def _generate_node_positions(self, node_positions: Optional[dict] = None):
nodes = self.nodes()
if node_positions is None:
node_positions = {}
def position_generator(N=10, centre=[0, 5]):
""" Generate circle of positions around centre """
idx = 0
while True:
phi = 2*np.pi*(idx/N)+np.pi/2
pos = 2.1*np.array([np.cos(phi), np.sin(phi)]) + centre
yield pos
idx = idx+1
positions = position_generator(len(nodes))
pos = dict([(node, node_positions.get(node, next(positions)))
for node in nodes])
return pos
def draw_mpl(self, ax=None):
if ax is None:
f, ax = plt.subplots()
ax.axis('off')
ax.set_title(self.name)
colors_list = [state_cmap[node_dat['state']] for node_dat in
self.nodes.values()]
node_positions = getattr(self, 'node_positions', None)
if node_positions is None:
pos = nx.nx_agraph.graphviz_layout(self, prog='dot')
else:
pos = self._generate_node_positions(node_positions)
nx.draw_networkx_nodes(self, pos, ax=ax, node_color=colors_list)
nx.draw_networkx_edges(self, pos, ax=ax, arrows=True,
**self.matplotlib_edge_properties)
nx.draw_networkx_labels(
self, pos, ax=ax, **self.matplotlib_label_properties)
self._format_mpl_plot(ax)
@staticmethod
def _format_mpl_plot(ax):
""" Method to format the generated matplotlib figure """
ax.set_xticks([])
ax.set_yticks([])
def draw_svg(self, filename: str = None):
"""
"""
if filename is None:
filename = self.cfg_svg_filename
self._update_drawing_attrs()
vis.draw_graph_svg(self, filename)
def open_html_viewer(self):
""" Open html viewer for the file specified by the svg backend """
template = os.path.join(os.path.split(autodepgraph.__file__)[
0], 'svg_viewer', 'svg_graph_viewer.html')
with open(template, 'rt') as fid:
x = fid.read()
base, file = os.path.split(self.cfg_svg_filename)
x = x.replace('adg_graph.svg', file)
tfile = tempfile.mktemp(prefix='svgviewer-', suffix='.html', dir=base)
with open(tfile, 'wt') as fid:
fid.write(x)
webbrowser.open_new_tab(tfile)
return tfile
def set_node_attribute(self, node, attribute, value):
""" Set the attribute of the specified node
Args:
node (str): name of the node
attribute (str): attribute to set
value (ojbect): value to set
"""
if attribute in ['state']:
raise Exception('please use set_state directly')
nx.set_node_attributes(self, {node: {attribute: value}})
def get_node_attribute(self, node, attribute):
""" Return the attribute of the specified node
Args:
node (str): name of the node
attribute (str): attribute to get
Returns:
value (ojbect): attribute of the object
"""
if attribute in ['state']:
raise Exception('please use get_state directly')
return self.nodes[node][attribute]
def set_node_description(self, node, description):
""" Set the node description field
Args:
node (str): name of the node
description (str): description to set
"""
nx.set_node_attributes(self, {node: {'description': description}})
def calibration_state(self):
""" Return dictionary with current calibration state """
return dict(self.nodes)
def _update_drawing_attrs(self):
for node_name, node_attrs in self.nodes(True):
state = self.get_node_state(node_name)
color = vis.state_cmap[state]
shape = 'hexagon' if self.is_manual_node(node_name) else 'ellipse'
attr_dict = {'shape': shape,
'style': 'filled',
'color': color,
# 'fixedsize':'shape',
# 'fixedsize' : b"true",
'fixedsize': "false",
'fillcolor': color}
node_attrs.update(attr_dict)
def _construct_maintenance_method():
# This placeholder exists to allow reading and writing graphs in a graph
# based format.
print('Placeholder function.')
print('Call DAG._construct_maintenance_methods() to update methods.')
def _get_function(funcStr):
if isinstance(funcStr, (types.MethodType, types.FunctionType)):
warnings.warn('please set function as a str', DeprecationWarning)
f = funcStr
elif '.' in funcStr:
try:
instr_name, method = funcStr.split('.')
instr = Instrument.find_instrument(instr_name)
f = getattr(instr, method)
except Exception as e:
f = get_function_from_module(funcStr)
else:
raise Exception('could not find function %s' % funcStr)
return f
def get_function_from_module(funcStr):
"""
"""
split_idx = funcStr.rfind('.')
module_name = funcStr[:split_idx]
mod = import_module(module_name)
f = getattr(mod, funcStr[(split_idx+1):])
return f
def update_node_state(graph_to_update, graph_to_update_from):
for node_name, attrs in graph_to_update_from.nodes(True):
if node_name in graph_to_update.nodes():
graph_to_update.nodes[node_name]['state'] = attrs['state']
graph_to_update.nodes[node_name]['last_update'] = attrs['last_update']