-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
604 lines (468 loc) · 23.9 KB
/
app.py
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# -*- coding: utf-8 -*-
"""
Network Control Theory Tutorial
@author: Johannes.Wiesner
"""
import json
import numpy as np
import pandas as pd
import networkx as nx
import dash
import dash_cytoscape as cyto
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_table
import itertools
import operator
import plotly.express as px
from network_control.utils import matrix_normalization
from network_control.energies import minimum_input,optimal_input
from nct_utils import state_trajectory
###############################################################################
## Set Default Data ###########################################################
###############################################################################
# set seed
np.random.seed(28)
# create a default adjacency matrix
A = np.array([[0, 1, 2, 1, 0, 0, 0, 0, 0],
[1, 0, 0, 3, 0, 0, 0, 0, 0],
[2, 0, 0, 4, 0, 0, 0, 0, 0],
[1, 3, 4, 0, 5, 0, 0, 0, 0],
[0, 0, 0, 5, 0, 6, 0, 0, 0],
[0, 0, 0, 0, 6, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 1, 0, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 1, 0]])
# create default (random) x0 and xf (between 0 and 1)
states_df = pd.DataFrame({'x0':np.round(np.random.rand(len(A)),2),'xf':np.round(np.random.rand(len(A)),2)})
states_df.reset_index(inplace=True)
###############################################################################
## Dash App ###################################################################
###############################################################################
## Topology-Modification ######################################################
# FIXME: transform networkx coordinates into dash/plotly space
# - Positions could however also be irrelevant here, because layout component
# from dash can also decide automatically over node positions
def from_A_to_elements(A):
'''Create a lists of elements from a numpy adjaceny matrix that can be inter-
preted by dash_cytoscape.Cytoscape. The following steps are implemented from
https://community.plotly.com/t/converting-networkx-graph-object-into-cytoscape-format/23224/2
'''
# create graph object
G = nx.Graph(A)
# get node positions
pos = nx.spring_layout(G)
# convert networkx to cytoscape layout
cy = nx.readwrite.json_graph.cytoscape_data(G)
# Add the dictionary key 'label' to the node dict (this is a required attribute for dash)
# Delete the key 'value' from node dict (not needed)
# Delete the key 'name' from node dict (not needed)
# Add the dictionary key 'controller' to the node dict and set to True
for node_dict in cy['elements']['nodes']:
for _,d in node_dict.items():
d['label'] = d.pop('value')
del d['name']
d['controller'] = True
d['constrain'] = True
# NOTE: in cytoscape, all ids of the nodes must be strings, that's why
# we convert the edge ids also to strings (but check if this is really
# necessary)
for edge_dict in cy['elements']['edges']:
for _,d in edge_dict.items():
d['source'] = str(d['source'])
d['target'] = str(d['target'])
# Add the positions you got from as a value for data in the nodes portion of cy
# NOTE: This might be not necessary, as positions can be automatically
# determined in the layout attribute from cyto.Cytoscape (see FIXME above)
for n,p in zip(cy['elements']['nodes'],pos.values()):
n['pos'] = {'x':p[0],'y':p[1]}
# Take the results and write them to a list
elements = cy['elements']['nodes'] + cy['elements']['edges']
return elements
# NOTE: What's that utils module? https://dash.plotly.com/cytoscape/reference
def get_edge_dicts(elements):
'''Extract all edge dictionaries from elements. Edge dicts are
identfied by their 'weight' key'''
edge_dicts = []
for d in elements:
if 'weight' in d['data']:
edge_dicts.append(d)
return edge_dicts
# NOTE: What's that utils module? https://dash.plotly.com/cytoscape/reference
def get_node_dicts(elements):
'''Extract all node dictionaries from elements. Node dicts are
identified by not having a 'weight' key'''
node_dicts = []
for d in elements:
if not 'weight' in d['data']:
node_dicts.append(d)
return node_dicts
def add_edges(selectedNodeData,edge_weight,elements):
'''For each combination of selected nodes, check if this combination is connected
by an edge. If not, create an edge dict for that combination and modify the elements list'''
edge_dicts = get_edge_dicts(elements)
edge_ids = [(d['data']['source'],d['data']['target']) for d in edge_dicts]
# get a list of ids of all nodes that user has currently selected and that
# should be connected by an edge. Sort the list alphanumerically (that ensures
# that we get only get combinations of source and target ids where source id
# is always the lower integer)
node_ids = [d['id'] for d in selectedNodeData]
node_ids.sort()
# create all pair-wise combinations of the selected nodes
source_and_target_ids = list(itertools.combinations(node_ids,2))
# for each source and target tuple, check if this edge already exists. If not,
# create a new edge dict and add it to elements
for (source,target) in source_and_target_ids:
if not (source,target) in edge_ids:
new_edge = {'data':{'weight':edge_weight,'source':source,'target':target}}
elements.append(new_edge)
return elements
def drop_edges(selectedEdgeData,elements):
'''Drop an input list of selected edges from cytoscape elements'''
# get source and target ids for all currently selected edges
source_and_target_ids = [(d['source'],d['target']) for d in selectedEdgeData]
# iterate over all dictionaries in elements, identify edge dicts by their
# 'weight' key and check again if this edge dict belongs to the currently selected
# edges. If yes, add its index to list of to be dropped dictionaires.
drop_these_dicts = []
for idx,d in enumerate(elements):
if 'weight' in d['data']:
if (d['data']['source'],d['data']['target']) in source_and_target_ids:
drop_these_dicts.append(idx)
# drop selected edge dictionaries from elements
elements = [i for j,i in enumerate(elements) if j not in drop_these_dicts]
return elements
def get_edge_min_max(elements):
'''Get minimum and maximum edge weights'''
# get all edges from elements
edge_dicts = get_edge_dicts(elements)
# find minimum and maximum weights
edge_weights = [d['data']['weight'] for d in edge_dicts]
weights_max = max(edge_weights)
weights_min = min(edge_weights)
return weights_min,weights_max
# FIXME: Delete this function if it's not necessary
def set_edge_width(elements,edge_weight):
'''Return the edge width for a single edge'''
weights_min,weights_max = get_edge_min_max(elements)
min_width = 1 # constant (selected by me)
max_width = 10 # constant (selected by me)
edge_width = min_width + ((max_width - min_width) / (weights_max - weights_min)) * (edge_weight - weights_min)
return edge_width
def set_edge_weights(selectedEdgeData,edge_weight,elements):
'''Modify the weights of the selected edges'''
# get source and target ids for all currently selected edges
source_and_target_ids = [(d['source'],d['target']) for d in selectedEdgeData]
# iterate over all dictionaries in elements, identify edge dicts by their
# 'weight' key and check again if this edge dict belongs to the currently selected
# edges. If yes, add its index to list of to be dropped dictionaires.
modify_these_dicts = []
for idx,d in enumerate(elements):
if 'weight' in d['data']:
if (d['data']['source'],d['data']['target']) in source_and_target_ids:
modify_these_dicts.append(idx)
# drop selected edge dictionaries from elements
for i in modify_these_dicts:
elements[i]['data']['weight'] = edge_weight
return elements
## Figure Plotting ###########################################################
def from_elements_to_A(elements):
'''Extract nodes and edges from current elements and convert them to
adjacency matrix
'''
# FIXME: This is inefficient, we iterate over the same list twice (see #8)
edge_dicts = get_edge_dicts(elements)
node_dicts = get_node_dicts((elements))
edges = [(d['data']['source'],d['data']['target'],d['data']['weight']) for d in edge_dicts]
nodes = [d['data']['id'] for d in node_dicts]
n_nodes = len(nodes)
A = np.zeros((n_nodes,n_nodes))
for edge in edges:
i = int(edge[0])
j = int(edge[1])
weight = edge[2]
A[i,j] = weight
A[j,i] = weight
return A
# FIXME: lots of repetitions to from_elements_to_S
def from_elements_to_B(elements):
'''Extract nodes from current elements, check which nodes are selected
as controllers and get a corresponding control matrix B that can be
fed to control_package functions.
'''
# get a list of all nodes from current elements (get their ID and their
# controller attribute)
node_dicts = get_node_dicts(elements)
nodes = [(d['data']['id'],d['data']['controller']) for d in node_dicts]
# sort nodes by their ids and get controller attribute
nodes.sort(key=operator.itemgetter(0))
c_attributes = [n[1] for n in nodes]
# create B matrix
B = np.zeros(shape=(len(nodes),len(nodes)))
for idx,c in enumerate(c_attributes):
if c == True:
B[idx,idx] = 1
return B
# FIXME: lots of repetitions to from_elements_to_B
def from_elements_to_S(elements):
'''Extract nodes from current elements, check which nodes are selected
to be constrained and get a corresponding matrix S that can be
fed to control_package functions.
'''
# get a list of all nodes from current elements (get their ID and their
# controller attribute)
node_dicts = get_node_dicts(elements)
nodes = [(d['data']['id'],d['data']['constrain']) for d in node_dicts]
# sort nodes by their ids and get controller attribute
nodes.sort(key=operator.itemgetter(0))
constrain_attributes = [n[1] for n in nodes]
# create B matrix
S = np.zeros(shape=(len(nodes),len(nodes)))
for idx,constrain in enumerate(constrain_attributes):
if constrain == True:
S[idx,idx] = 1
return S
def get_state_trajectory_fig(A,x0,T,c):
'''Generate a plotly figure that plots a state trajectory using an input
matrix, a source state, a time horizon and a normalization constant'''
# simulate state trajectory
x = state_trajectory(A=A,xi=x0,T=T)
# create figure
x_df = pd.DataFrame(x).reset_index().rename(columns={'index':'Node'})
x_df = x_df.melt(id_vars='Node',var_name='t',value_name='Value')
fig = px.line(x_df,x='t',y='Value',color='Node')
fig.update_layout(title='x(t+1) = Ax(t)',title_x=0.5)
return fig
# FIXME: Plotting speed could probably bee optimized, take a look in niplot module
# TO-DO: n_err should also be visualized somewhere
def get_minimum_energy_figure(A,T,B,x0,xf,c):
# compute minimum energy
x,u,n_err = minimum_input(A,T,B,x0,xf)
# create figure (FIXME: could the following be shorted?)
x_df = pd.DataFrame(x).reset_index().rename(columns={'index':'t'})
x_df = x_df.melt(id_vars='t',var_name='Node',value_name='Value')
x_df['Type'] = 'x'
u_df = pd.DataFrame(u).reset_index().rename(columns={'index':'t'})
u_df = u_df.melt(id_vars='t',var_name='Node',value_name='Value')
u_df['Type'] = 'u'
fig_df = pd.concat([x_df,u_df])
fig = px.line(fig_df,x='t',y='Value',color='Node',facet_row='Type')
fig.update_layout(title='Minimum Control Energy',title_x=0.5)
return fig
# FIXME: Plotting speed could probably bee optimized, take a look in niplot module
# TO-DO: n_err should also be visualized somewhere
def get_optimal_energy_figure(A,T,B,x0,xf,rho,S,c):
# compute optimal energy
x,u,n_err = optimal_input(A,T,B,x0,xf,rho,S)
# create figure (FIXME: could the following be shorted?)
x_df = pd.DataFrame(x).reset_index().rename(columns={'index':'t'})
x_df = x_df.melt(id_vars='t',var_name='Node',value_name='Value')
x_df['Type'] = 'x'
u_df = pd.DataFrame(u).reset_index().rename(columns={'index':'t'})
u_df = u_df.melt(id_vars='t',var_name='Node',value_name='Value')
u_df['Type'] = 'u'
fig_df = pd.concat([x_df,u_df])
fig = px.line(fig_df,x='t',y='Value',color='Node',facet_row='Type')
fig.update_layout(title='Optimal Control Energy',title_x=0.5)
return fig
###############################################################################
## Dash App ###################################################################
###############################################################################
# run dash and take in the list of elements
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
# create custom style sheet
# show node labels
# Style the border of the nodes depending on if they are controllers
# and constrained
stylesheet = [{'selector':'node','style':{'content':'data(label)'}},
{'selector': '[?controller][?constrain]','style':{'border-width':2,'border-color':'black','border-style':'solid'}},
{'selector':'[?controller][!constrain]','style':{'border-width':2,'border-color':'black','border-style':'dashed'}},
{'selector':'[!controller][?constrain]','style':{'border-width':2,'border-color':'black','border-style':'dotted'}},
{'selector':'[!controller][!constrain]','style':{}}
]
app.layout = html.Div([
dbc.Row([
dbc.Col([
# cytoscape graph
cyto.Cytoscape(
id='cytoscape-compound',
layout={'name':'cose'},
elements=from_A_to_elements(A), # initialize elements with a function call
stylesheet=stylesheet,
style={'width':'100%','height':'500px'})
],width=6),
dbc.Col([
# x0/xf data table
dbc.Row([dash_table.DataTable(id='states-table',
columns=[{'id':'index','name':'index','type':'numeric'},
{'id':'x0','name':'x0','type':'numeric','editable':True},
{'id':'xf','name':'xf','type':'numeric','editable':True}],
data=states_df.to_dict('records'),
editable=False)],justify='center'),
dbc.Row([html.Button('Switch columns',id='switch-state-cols',n_clicks=0,style={'width':'100%'})],justify='center')],
width=3),
dbc.Col([
dbc.Row([dbc.Col([dcc.Input(id='c',type="number",debounce=True,placeholder='Normalization Constant (c)',value=1)])]),
dbc.Row([dbc.Col([dcc.Input(id='T',type="number",debounce=True,placeholder='Time Horizon (T)',value=3)]),dbc.Col(html.Button('Plot Trajectories',id='plot-button',n_clicks=0))]),
dbc.Row([dbc.Col([dcc.Input(id='rho',type="number",debounce=True,placeholder='rho',value=1)])]),
dbc.Row([dbc.Col([html.Button('(Dis-)Connect Nodes',id='edge-button',n_clicks=0)])]),
dbc.Row([dbc.Col([html.Button('(Un-)set controll',id='controll-button',n_clicks=0)])]),
dbc.Row([dbc.Col([html.Button('(Un-)set constrain',id='constrain-button',n_clicks=0)])]),
dbc.Row([dbc.Col([dcc.Input(id='edge-weight',type='number',debounce=True,placeholder='Edge Weight',value=1)]),dbc.Col([html.Button('Set Edge Weight',id='edge-weight-button',n_clicks=0)])])],
width=3)
],align='end'),
dbc.Row([
# figures
dbc.Col([dcc.Graph(id='state-trajectory-fig',figure={})],width=4),
dbc.Col([dcc.Graph(id='minimum-energy-fig',figure={})],width=4),
dbc.Col([dcc.Graph(id='optimal-energy-fig',figure={})],width=4)
],className="g-0"),
dbc.Row([
# debugging fields (can be deleted when not necessary anymore)
dbc.Col([html.Label(children='Selected Nodes:'),html.Pre(id='selected-node-data-json-output')],width=3),
dbc.Col([html.Label(children='Selected Edges:'),html.Pre(id='selected-edge-data-json-output')],width=3),
dbc.Col([html.Label(children='Current Elements:'),html.Pre(id='current-elements')],width=3),
dbc.Col([html.Label(children='Current Stylesheet:'),html.Pre(id='current-stylesheet')],width=3)
],className="g-0")
])
## Just for debugging (can be deleted when not necessary anymore ##############
@app.callback(Output('selected-node-data-json-output','children'),
Input('cytoscape-compound','selectedNodeData'),
prevent_initial_call=True)
def displaySelectedNodeData(data):
return json.dumps(data,indent=2)
@app.callback(Output('selected-edge-data-json-output','children'),
Input('cytoscape-compound','selectedEdgeData'),
prevent_initial_call=True)
def displaySelectedEdgeData(data):
return json.dumps(data,indent=2)
@app.callback(Output('current-elements','children'),
Input('cytoscape-compound','elements'),
prevent_initial_call=True)
def displayCurrentElements(elements):
return json.dumps(elements,indent=2)
@app.callback(Output('current-stylesheet','children'),
Input('cytoscape-compound','stylesheet'),
prevent_initial_call=True)
def displayCurrentStylesheet(elements):
return json.dumps(elements,indent=2)
## Callback Functions #########################################################
@app.callback(Output('cytoscape-compound','elements'),
Input('edge-button','n_clicks'),
Input('controll-button','n_clicks'),
Input('constrain-button','n_clicks'),
Input('edge-weight-button','n_clicks'),
State('edge-weight','value'),
State('cytoscape-compound','selectedNodeData'),
State('cytoscape-compound','selectedEdgeData'),
State('cytoscape-compound','elements'),
prevent_initial_call=True)
def updateElements(edge_button,controll_button,constrain_button,edge_weight_button,edge_weight,selectedNodeData,selectedEdgeData,elements):
print('UpdateElements was fired')
# check which button was triggered
ctx = dash.callback_context
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
# add or delete edges
if button_id == 'edge-button':
# FIXME: Is that true? Couldn't we merge add_edges and drop_edges?
# user must exclusively selecte either at least two nodes or one edge
# but not both nodes and edges at the same time
if len(selectedNodeData) > 0 and len(selectedEdgeData) > 0:
return elements
if len(selectedNodeData) >= 2 and len(selectedEdgeData) == 0:
return add_edges(selectedNodeData,edge_weight,elements)
if len(selectedNodeData) == 0 and len(selectedEdgeData) >= 1:
return drop_edges(selectedEdgeData,elements)
else:
return elements
# modify edge weights
elif button_id == 'edge-weight-button':
return set_edge_weights(selectedEdgeData,edge_weight,elements)
# set or unset controll nodes
elif button_id == 'controll-button':
# get a list of ids of all nodes that user has currently selected and for
# which controll state should be switched
node_ids = [d['id'] for d in selectedNodeData]
# identify node dicts in elements and toggle their 'controller' attribute
# if they are part of the selected nodes
node_dicts = get_node_dicts(elements)
for d in node_dicts:
if d['data']['id'] in node_ids:
d['data']['controller'] = not d['data']['controller']
return elements
elif button_id == 'constrain-button':
# get a list of ids of all nodes that user has currently selected and for
# which controll state should be switched
node_ids = [d['id'] for d in selectedNodeData]
# identify node dicts in elements and toggle their 'controller' attribute
# if they are part of the selected nodes
node_dicts = get_node_dicts(elements)
for d in node_dicts:
if d['data']['id'] in node_ids:
d['data']['constrain'] = not d['data']['constrain']
return elements
# TODO: This callback is currently always fired when elements are changed.
# But it actually only needs to be fired when either the minimum or maximum
# edge weights change
@app.callback(Output('cytoscape-compound','stylesheet'),
Input('cytoscape-compound','elements'),
State('cytoscape-compound','stylesheet'))
def updateEdgeStyle(elements,stylesheet):
weights_min,weights_max = get_edge_min_max(elements)
# add (or overwrite) edge style
if not any([d['selector'] == 'edge' for d in stylesheet]):
stylesheet.append({'selector':'edge','style':{'width':f"mapData(weight,{weights_min},{weights_max},1,5)"}})
else:
edge_style_idx = [i for i,d in enumerate(stylesheet) if d['selector'] == 'edge'][0]
stylesheet[edge_style_idx] = {'selector':'edge','style':{'width':f"mapData(weight,{weights_min},{weights_max},1,5)"}}
return stylesheet
@app.callback(Output('states-table','data'),
Input('switch-state-cols','n_clicks'),
State('states-table','derived_virtual_data'),
prevent_initial_call=True)
def switchStateColums(n_clicks,states_data):
print(states_data)
states_data_copy = states_data.copy()
for d in states_data_copy:
d['x0'],d['xf'] = d['xf'],d['x0']
return states_data_copy
@app.callback(Output('state-trajectory-fig','figure'),
Output('minimum-energy-fig','figure'),
Output('optimal-energy-fig','figure'),
Input('plot-button','n_clicks'),
State('cytoscape-compound','elements'),
State('T','value'),
State('c','value'),
State('rho','value'),
State('states-table','derived_virtual_data'),
prevent_initial_call=True)
def updateFigures(n_clicks,elements,T,c,rho,states_data):
# debugging
print('UpdateFigures was fired')
# digest data for network_control package
A = from_elements_to_A(elements)
B = from_elements_to_B(elements)
S = from_elements_to_S(elements)
# debugging
print(f"This is A:\n{A}\n")
print(f"This is B:\n{B}\n")
print(f"This is S:\n{S}\n")
# normalize A
# FIXME: It should also be possible to not normalize the matrix (i.o.
# to get an intuition for what happens when you not apply normalization)
A = matrix_normalization(A,c)
# FIXME: Currently we use reshape (-1,1), but this is a bug in network
# control-package. When this is fixed, we don't need reshape anymore
x0 = np.array([d['x0'] for d in states_data]).reshape(-1,1)
xf = np.array([d['xf'] for d in states_data]).reshape(-1,1)
# debugging
print(f"This is x0:\n{x0}\n")
print(f"This is xf:\n{xf}\n")
fig_1 = get_state_trajectory_fig(A,x0=x0,T=T,c=c)
fig_2 = get_minimum_energy_figure(A=A,T=T,B=B,x0=x0,xf=xf,c=c)
fig_3 = get_optimal_energy_figure(A=A,T=T,B=B,x0=x0,xf=xf,rho=rho,S=S,c=c)
return fig_1,fig_2,fig_3
if __name__ == '__main__':
app.run_server(debug=True,use_reloader=False)