ModelFlowIb 1.46__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
modeldashsidebar.py ADDED
@@ -0,0 +1,442 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Fri May 14 22:46:21 2021
4
+
5
+ @author: bruger
6
+ """
7
+
8
+ import dash
9
+ from jupyter_dash import JupyterDash
10
+ from jupyter_dash.comms import _send_jupyter_config_comm_request
11
+ try:
12
+ from dash import Dash, callback, html, dcc, dash_table, Input, Output, State, MATCH, ALL
13
+ import dash_bootstrap_components as dbc
14
+ from dash_interactive_graphviz import DashInteractiveGraphviz
15
+ # from dash.exceptions import PreventUpdate
16
+
17
+ # try:
18
+ # from dash import dcc
19
+ # except:
20
+ # import dash_core_components as dcc
21
+
22
+ # import dash_bootstrap_components as dbc
23
+ # try:
24
+ # from dash import html
25
+ # except:
26
+ # import dash_html_components as html
27
+
28
+ # from dash.dependencies import Input, Output, State
29
+
30
+ # from dash_interactive_graphviz import DashInteractiveGraphviz
31
+ except:
32
+ print('No Dash')
33
+
34
+ import plotly.graph_objs as go
35
+ from plotly.graph_objs.scatter.marker import Line
36
+
37
+ import webbrowser
38
+ from threading import Timer
39
+
40
+ import time
41
+
42
+ import json
43
+ import networkx as nx
44
+
45
+ from dataclasses import dataclass,field
46
+
47
+
48
+ from pathlib import Path
49
+ import pandas as pd
50
+
51
+ from modelhelp import cutout
52
+
53
+ import logging
54
+ log = logging.getLogger('werkzeug')
55
+ log.setLevel(logging.ERROR)
56
+ # log.setLevel(logging.INFO)
57
+ # log.setLevel(logging.DEBUG)
58
+
59
+
60
+
61
+ initial_dot_source = """
62
+ digraph {
63
+ node[style="filled"]
64
+ a ->b->d
65
+ a->c->d
66
+ }
67
+ """
68
+
69
+ sidebar_width, rest_width = "15%", "82%"
70
+ # the style arguments for the sidebar. We use position:fixed and a fixed width
71
+ SIDEBAR_STYLE = {
72
+ "position": "fixed",
73
+ "top": 0,
74
+ "left": 0,
75
+ "bottom": 0,
76
+ "width": sidebar_width,
77
+ "padding": "2rem 1rem",
78
+ "background-color": "#f8f9fa",
79
+ "overflow": "scroll",
80
+ }
81
+
82
+ # the styles for the main content position it to the right of the sidebar and
83
+ # add some padding.
84
+ CONTENT_STYLE_TOP = {
85
+ "background-color": "f8f9fa",
86
+ "width": "100%",
87
+
88
+ "margin-left": "0%",
89
+ }
90
+ CONTENT_STYLE_GRAPH = {
91
+ "background-color": "f8f9fa",
92
+ "width":"82%",
93
+
94
+ "margin-left": '0%',
95
+ }
96
+ CONTENT_STYLE_TAB = {
97
+ "background-color": "f8f9fa",
98
+ "margin-left": sidebar_width,
99
+ "width": "85%",
100
+ "height": "auto",
101
+ }
102
+
103
+
104
+ def app_setup(jupyter=False):
105
+ print('apprun')
106
+ if jupyter:
107
+ # Timer(1,_send_jupyter_config_comm_request).start()
108
+ # JupyterDash.infer_jupyter_proxy_config()
109
+ app = JupyterDash(__name__,external_stylesheets=[dbc.themes.BOOTSTRAP])
110
+ else:
111
+ app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
112
+ return app
113
+
114
+ def app_run(app,jupyter=False,debug=False,port=5000,inline=True):
115
+ def open_browser(port=port):
116
+ webbrowser.open_new(f"http://localhost:{port}")
117
+
118
+
119
+ if jupyter:
120
+ if inline:
121
+ app.run_server(debug=debug,port=port,mode='inline')
122
+ else:
123
+ Timer(1, open_browser).start()
124
+ app.run_server(debug=debug,port=port,mode='external')
125
+
126
+ else:
127
+ Timer(1, open_browser).start()
128
+
129
+ app.run_server(debug=debug,port=port)
130
+
131
+
132
+ def get_stack(df,v='Guess it',heading='Yes',pct=True,threshold=0.5,desdict = {}):
133
+ pv = cutout(df,threshold)
134
+ template = '%{y:.1f}% explained by:<br><b>%{meta}</b>:' if pct else '%{y:.2f} explained by:<br><b>%{meta}</b>:'
135
+ trace = [go.Bar(x=pv.columns.astype('str'), y=pv.loc[rowname,:], name=rowname,hovertemplate = template,
136
+ meta = desdict.get(rowname,'hest')) for rowname in pv.index]
137
+ out = { 'data': trace,
138
+ 'layout':
139
+ go.Layout(title=f'{heading}', barmode='relative',legend_itemclick='toggleothers',
140
+ yaxis = {'tickformat': ',7.0','ticksuffix':'%' if pct else ''})
141
+ }
142
+ return out
143
+
144
+ def get_no_stack(df,v='No attribution for exogenous variables ',desdict = {}):
145
+ out = {'layout':
146
+ go.Layout(title=f'{v}')
147
+ }
148
+ return out
149
+
150
+ def get_line_old(pv,v='Guess it',heading='Yes'):
151
+ trace = [go.Line(x=pv.columns.astype('str'), y=pv.loc[rowname,:], name=rowname) for rowname in pv.index]
152
+ out = { 'data': trace,
153
+ 'layout':
154
+ go.Layout(title=f'{heading}')
155
+ }
156
+ return out
157
+ def get_line(pv,v='Guess it',heading='Yes',pct=True):
158
+ trace = [go.Scatter(x=pv.columns.astype('str'),
159
+ y=pv.loc[rowname,:],
160
+ name=rowname
161
+ ) for rowname in pv.index]
162
+ out = { 'data': trace,
163
+ 'layout':
164
+ go.Layout(title=f'{heading}')
165
+ }
166
+ return out
167
+
168
+
169
+ def generate_table(dataframe, max_rows=10):
170
+ return html.Table([
171
+ html.Thead(
172
+ html.Tr([html.Th('var')]+[html.Th(col) for col in dataframe.columns])
173
+ ),
174
+ html.Tbody([
175
+ html.Tr([html.Td(dataframe.index[i])]+[html.Td(f'{dataframe.iloc[i][col]:25.2f}') for col in dataframe.columns])
176
+ for i in range(min(len(dataframe), max_rows))
177
+ ])
178
+ ])
179
+
180
+
181
+ @dataclass
182
+ class Dash_graph():
183
+
184
+ mmodel : any = None
185
+ pre_var : str =''
186
+ filter: float = 0
187
+ up : int = 1
188
+ down : int = 0
189
+ time_att : bool = False
190
+ attshow :bool = False
191
+ all : bool = False
192
+ dashport : int = 5001
193
+ debug : bool = False
194
+ jupyter : bool = False
195
+ show_trigger : bool = False
196
+ inline : bool =False
197
+ lag : bool = False
198
+ threshold : float =0.5
199
+ growthshow : bool = False
200
+
201
+ def __post_init__(self):
202
+ self.fokusvar = set()
203
+ # print('Still worlking on the layout of this')
204
+ self.firstsession = True
205
+ selected_var = self.pre_var if self.pre_var else sorted(self.mmodel.allvar.keys())[0]
206
+ self.outvar_state = selected_var
207
+ sidebar = html.Div(
208
+ [
209
+ html.H3("Current Variable"),
210
+ dcc.Markdown(
211
+ id='outvar_state',
212
+ children=f'{selected_var}'),
213
+
214
+ html.H3("Select a variable"),
215
+ dcc.Dropdown(id='var',value=selected_var,options=[
216
+ dict(label=v, value=v)
217
+ for v in sorted(self.mmodel.allvar.keys())],persistence = True,persistence_type='session'),
218
+ html.H3("Tree walking"),
219
+ dbc.Row([
220
+ html.H5("Up"),
221
+ dcc.Dropdown(id="up",value=self.up,options=[
222
+ dict(label=engine, value=engine)
223
+ for engine in list(range(10))],persistence = True,persistence_type='session'),
224
+
225
+ html.H5("Down"),
226
+ dcc.Dropdown(id="down",value=self.down,options=[
227
+ dict(label=engine, value=engine)
228
+ for engine in list(range(10))],persistence = True,persistence_type='session'),
229
+ ]),
230
+ dbc.Tooltip('Looking at preceding variabels up to this level',
231
+ target = 'up'),
232
+ dbc.Tooltip('Looking at dependent variabels down to this level',
233
+ target = 'down'),
234
+
235
+ html.H3("Graph filter%"),
236
+ dcc.Dropdown(id="filter",value=self.filter,options=[
237
+ dict(label=t, value=t)
238
+ for t in list(range(0,100,10))],persistence = True,persistence_type='session'),
239
+ dbc.Tooltip('All branches contribution less are pruned from the graph',
240
+ target = 'filter'),
241
+
242
+ html.H3("Graph orientation"),
243
+ dcc.RadioItems(id='orient',
244
+ options=[
245
+ {'label': 'Vertical', 'value':'v'},
246
+
247
+ {'label': 'Horisontal', 'value': 'h'},
248
+ ],
249
+ value='v',labelStyle={'display': 'block'},persistence = True,persistence_type='session' ),
250
+
251
+ html.H3("Node Display"),
252
+ dcc.RadioItems(id='node',
253
+ options=[
254
+ {'label': 'Name', 'value':'name'},
255
+ {'label': '+values', 'value':'all'},
256
+
257
+ {'label': '+Attribution', 'value': 'attshow'},
258
+ ],persistence = True, persistence_type='session',
259
+ value='all' if self.all else ('attshow' if self.attshow else 'name') ,labelStyle={'display': 'block'}),
260
+
261
+ html.H3("Behavior when clicking on node"),
262
+ dcc.RadioItems(id='onclick',
263
+ options=[
264
+ {'label': 'Center', 'value':'c'},
265
+ {'label': 'Display', 'value': 'd'},
266
+ ],
267
+ value='c',persistence = True,persistence_type='session'),
268
+
269
+
270
+ ],
271
+ style=SIDEBAR_STYLE
272
+ )
273
+ # breakpoint()
274
+ outvar= selected_var
275
+ tab0 = html.Div([
276
+ dbc.Tabs(id="tabs", children=[
277
+ dbc.Tab(id='Graph',label='Graph',children= [DashInteractiveGraphviz(id="gv" , style=CONTENT_STYLE_GRAPH,
278
+
279
+ dot_source = self.mmodel.draw(selected_var,up=self.up,down=self.down,showatt=False,lag=self.lag,
280
+ debug=0,dot=True,HR=False,filter = self.filter,
281
+ all=False,attshow=False))],
282
+ style=CONTENT_STYLE_TOP , ),
283
+
284
+ dbc.Tab(id='Chart',label='Chart',
285
+ children = [dcc.Graph(id='chart',
286
+ figure=get_line(self.mmodel.value_dic[selected_var].iloc[:2,:],selected_var,f'The values for {selected_var}'))
287
+ , dcc.Graph(id='chart_dif',
288
+ figure=get_line(self.mmodel.value_dic[selected_var].iloc[[2],:],selected_var,f'The impact for {selected_var}'))
289
+ ],
290
+ style=CONTENT_STYLE_TOP),
291
+
292
+ dbc.Tab(id='Attribution',label='Attribution',
293
+ children = [dcc.Graph(id='att_pct',
294
+ figure = (get_stack(self.mmodel.att_dic[outvar],outvar,f'Attribution of the impact - pct. for {outvar}',
295
+ threshold=self.threshold,desdict=self.mmodel.var_description)
296
+ if outvar in self.mmodel.endogene else html.H3("Graph orientation")))
297
+ , dcc.Graph(id='att_level',
298
+ figure = (get_stack(self.mmodel.att_dic_level[outvar],outvar,f'Attribution of the impact - level for {outvar}',
299
+ pct=False,threshold=self.threshold,desdict=self.mmodel.var_description) if outvar in self.mmodel.endogene else html.H3("Graph orientation")))
300
+
301
+ ],
302
+ style=CONTENT_STYLE_TOP)
303
+
304
+
305
+ ],persistence=True, persistence_type = 'session'),
306
+ ],style = CONTENT_STYLE_TAB)
307
+ # tabbed = tab0
308
+ # tabbed = dbc.Container(tab0,id='tabbed',style={"height": "100vh"},fluid=True)
309
+ tabbed = html.Div(tab0,id='tabbed',style={"height": "100vh"})
310
+ self.app = app_setup(jupyter=self.jupyter)
311
+
312
+
313
+
314
+ # app.layout = html.Div([sidebar,body2])
315
+ self.app.layout = dbc.Container([sidebar,tabbed],style={"height": "100vh","width":"100%"},fluid=True)
316
+
317
+ @self.app.callback(
318
+ [Output("gv", "dot_source"),
319
+ Output('chart','figure'), Output('chart_dif','figure'),
320
+ Output('att_pct','figure'), Output('att_level','figure'),
321
+ Output('outvar_state','children'),
322
+ Output('tabs','active_tab')],
323
+
324
+ [Input('var', "value"),
325
+ Input('gv', "selected_node"), Input('gv', "selected_edge"),
326
+ Input('up', "value"),Input('down', "value"),Input('filter', "value"),
327
+ Input('orient', "value"),
328
+ Input('onclick','value'),
329
+ Input('node','value')
330
+
331
+ ]
332
+ , State('outvar_state','children')
333
+ )
334
+ def display_output( var,
335
+ selected_node,selected_edge,
336
+ up,down,filter,
337
+ orient,
338
+ onclick,
339
+ node,
340
+ outvar_state
341
+ ):
342
+ # time.sleep(3)
343
+ ctx = dash.callback_context
344
+ # if ctx.triggered[0]['prop_id']== 'gv.selected_node' and ctx.triggered[0]['value']== None:
345
+ # raise PreventUpdate
346
+
347
+ # print(f'new trigger :{ctx.triggered=}')
348
+ # breakpoint()
349
+ if ctx.triggered:
350
+
351
+ trigger = ctx.triggered[0]['prop_id'].split('.')[0]
352
+ if self.show_trigger:
353
+ print('\nNew Trigger:')
354
+ ctx_msg = json.dumps({
355
+ 'states': ctx.states,
356
+ 'triggered': ctx.triggered,
357
+ 'inputs': ctx.inputs
358
+ }, indent=2)
359
+ print(ctx_msg)
360
+ # print(f'{outvar=},{data_show=}')
361
+ print(f'{outvar_state=}')
362
+ print(f'{self.fokusvar=} When triggerd')
363
+
364
+ update_var = True
365
+
366
+ if trigger == 'var':
367
+ outvar=var
368
+
369
+
370
+
371
+ # elif trigger == 'gv' :
372
+
373
+ # print(f'{selected_node=}')
374
+ elif selected_node:
375
+ xvar= selected_node.split('(')[0]
376
+ if onclick == 'c':
377
+ outvar = xvar if trigger == 'gv' else self.outvar_state
378
+
379
+ elif onclick == 'd' :
380
+ if trigger == 'gv':
381
+ if xvar in self.fokusvar:
382
+ self.fokusvar = self.fokusvar-{xvar}
383
+ else:
384
+ self.fokusvar = self.fokusvar | {xvar}
385
+ outvar=xvar
386
+
387
+ else:
388
+
389
+ outvar=self.outvar_state
390
+
391
+ self.fokusvar = set() if trigger in {'onclick','var'} else self.fokusvar
392
+
393
+ if onclick == 'c' or trigger == 'var' : #or outvar not in self.mmodel.value_dic.keys() : # or trigger in ['up','down','orient','filter','node'] :
394
+ dot_out = self.mmodel.draw(outvar,up=up,down=down,filter=filter,showatt=False,debug=0,
395
+ lag=self.lag,dot=True,HR=orient=='h',last=0,all = False,
396
+ attshow=False)
397
+
398
+ elif onclick == 'd' :
399
+ # print(f'{outvar=}')
400
+ if self.show_trigger:
401
+ print(f'{self.fokusvar=} on display ')
402
+
403
+ dot_out = self.mmodel.draw(self.outvar_state,up=up,down=down,filter=filter,showatt=False,debug=0,
404
+ lag=self.lag,dot=True,HR=orient=='h',fokus2=self.fokusvar,all= node == 'all',
405
+ attshow= node =='attshow',growthshow= node=='all' or node =='attshow',)
406
+
407
+ chart_out = get_line(self.mmodel.get_values(outvar).iloc[:2,:],outvar,f'The values for {outvar}:{self.mmodel.var_description.get(outvar,"")}')
408
+ chart_dif_out = get_line(self.mmodel.get_values(outvar).iloc[[2],:],outvar,f'The impact for {outvar}:{self.mmodel.var_description.get(outvar,"")}')
409
+
410
+ att_pct_out = (get_stack(self.mmodel.get_att_pct(
411
+ outvar.split('(')[0], lag=False, start='', end=''),outvar,f'Attribution of the impact - pct. for {outvar}:{self.mmodel.var_description.get(outvar,"")}'
412
+ ,threshold=self.threshold,desdict=self.mmodel.var_description)
413
+ if outvar in self.mmodel.endogene else html.H3(""))
414
+
415
+ att_level_out = (get_stack(self.mmodel.get_att_level(
416
+ outvar.split('(')[0], lag=False, start='', end=''),outvar,f'Attribution of the impact - level for {outvar}',
417
+ pct=False,threshold=self.threshold,desdict=self.mmodel.var_description) if outvar in self.mmodel.endogene else html.H3("") )
418
+
419
+ else:
420
+ return [dash.no_update, dash.no_update,dash.no_update,dash.no_update,dash.no_update, dash.no_update, dash.no_update]
421
+ if self.show_trigger:
422
+ print(f'{self.fokusvar=} before render')
423
+ self.outvar_state = outvar if onclick == 'c' or trigger == 'var' else self.outvar_state
424
+ outvar_state = self.outvar_state
425
+ return [dot_out, chart_out, chart_dif_out, att_pct_out, att_level_out, outvar_state ,dash.no_update,]
426
+
427
+ app_run(self.app,jupyter=self.jupyter,debug=self.debug,port=self.dashport,inline=self.inline)
428
+ #%test
429
+ if __name__ == "__main__":
430
+
431
+ #%% testing
432
+
433
+ if not 'baseline' in locals() or 0 :
434
+ from modelclass import model
435
+ madam,baseline = model.modelload('../Examples/ADAM/baseline.pcim',run=1,silent=0 )
436
+ # make a simpel experimet VAT
437
+ scenarie = baseline.copy()
438
+ scenarie.TG = scenarie.TG + 0.05
439
+ _ = madam(scenarie)
440
+
441
+
442
+ _ = Dash_graph(madam,'FE',debug = 0,all=1,filter=30,show_trigger=True,jupyter=False,up=1,dashport=5005)