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.
- ModelFlowIb-1.46.dist-info/METADATA +151 -0
- ModelFlowIb-1.46.dist-info/RECORD +48 -0
- ModelFlowIb-1.46.dist-info/WHEEL +5 -0
- ModelFlowIb-1.46.dist-info/top_level.txt +44 -0
- data/ken_base.pcim +1 -0
- modelBLfunk.py +70 -0
- model_Excel.py +334 -0
- model_cvx.py +132 -0
- model_doable.py +40 -0
- model_dynare.py +164 -0
- model_financial_stability.py +88 -0
- model_ifrs9.py +88 -0
- model_latex.py +505 -0
- model_latex_class.py +478 -0
- model_latex_classpy.py +401 -0
- model_run_numba.py +15 -0
- modelclass.py +7057 -0
- modelclass2.py +463 -0
- modeldash.py +265 -0
- modeldashboot.py +202 -0
- modeldashsidebar.py +442 -0
- modeldekom.py +518 -0
- modeldiff.py +561 -0
- modelgrab.py +318 -0
- modelgrabwf2.py +554 -0
- modelhelp.py +253 -0
- modelinvert.py +211 -0
- modeljupyter.py +821 -0
- modeljupytermagic.py +267 -0
- modelmacrograb.py +98 -0
- modelmanipulation.py +1273 -0
- modelmf.py +265 -0
- modelnet.py +114 -0
- modelnewton.py +819 -0
- modelnormalize.py +348 -0
- modelpattern.py +253 -0
- modelpatternrev.py +72 -0
- modelsandbox.py +76 -0
- modelsandbox_Mixin.py +1565 -0
- modeltodo.py +31 -0
- modeluserfunk.py +97 -0
- modeluserfunk_old.py +90 -0
- modelvis.py +677 -0
- modelwidget.py +610 -0
- modelwidget_input.py +961 -0
- modelwidget_new.py +1005 -0
- modelwidget_new_new.py +1238 -0
- modelwidget_shinypy.py +935 -0
modelwidget_new.py
ADDED
|
@@ -0,0 +1,1005 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Created on Mon Aug 9 14:46:11 2021
|
|
4
|
+
|
|
5
|
+
To define Jupyter widgets to update and show variables.
|
|
6
|
+
|
|
7
|
+
this version is made to make is easy to define input widgets as dictikonaries.
|
|
8
|
+
|
|
9
|
+
@author: Ib
|
|
10
|
+
"""
|
|
11
|
+
# from shiny import App, event, html_dependencies, http_staticfiles, input_handler, module, reactive, render, render_image, render_plot, render_text, render_ui, req, run_app, session, types, ui
|
|
12
|
+
# from shinywidgets import output_widget, reactive_read, register_widget, render_widget
|
|
13
|
+
import ipywidgets as ipy
|
|
14
|
+
|
|
15
|
+
import pandas as pd
|
|
16
|
+
|
|
17
|
+
from ipywidgets import interact, Dropdown, Checkbox, IntRangeSlider, SelectMultiple, Layout
|
|
18
|
+
from ipywidgets import Select
|
|
19
|
+
from ipywidgets import interactive, ToggleButtons, SelectionRangeSlider, RadioButtons
|
|
20
|
+
from ipywidgets import interactive_output, HBox, VBox, link, Dropdown,Output
|
|
21
|
+
from copy import copy
|
|
22
|
+
|
|
23
|
+
import pandas as pd
|
|
24
|
+
import ipywidgets as widgets
|
|
25
|
+
|
|
26
|
+
from ipysheet import sheet, cell, current
|
|
27
|
+
from ipysheet.pandas_loader import from_dataframe, to_dataframe
|
|
28
|
+
|
|
29
|
+
from IPython.display import display, clear_output,Latex, Markdown
|
|
30
|
+
from dataclasses import dataclass,field
|
|
31
|
+
import matplotlib.pylab as plt
|
|
32
|
+
import yaml
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass
|
|
38
|
+
class basewidget:
|
|
39
|
+
''' basis for widget updating in jupyter'''
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
widgetdef : dict
|
|
43
|
+
expname : str = "Carbon tax rate, US$ per tonn "
|
|
44
|
+
tab: bool = False
|
|
45
|
+
|
|
46
|
+
def __post_init__(self):
|
|
47
|
+
self.datachildren = [globals()[widgettype+'widget'](widgetdict['content'],expname=widgetdict.get('heading','No heading'))
|
|
48
|
+
for widgettype,widgetdict in self.widgetdef ]
|
|
49
|
+
breakpoint()
|
|
50
|
+
self.datawidget = VBox([child.datawidget for child in self.datachildren])
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def update_df(self,df,current_per):
|
|
54
|
+
''' will update container widgets'''
|
|
55
|
+
for w in self.datachildren:
|
|
56
|
+
w.update_df(df,current_per)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def reset(self,g):
|
|
61
|
+
''' will reset container widgets'''
|
|
62
|
+
for w in self.datachildren:
|
|
63
|
+
w.reset(g)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass
|
|
67
|
+
class tabwidget:
|
|
68
|
+
'''A widget to create tab or acordium contaners'''
|
|
69
|
+
|
|
70
|
+
widgetdef : dict
|
|
71
|
+
expname : str = "Carbon tax rate, US$ per tonn "
|
|
72
|
+
tab: bool =True
|
|
73
|
+
|
|
74
|
+
def __post_init__(self):
|
|
75
|
+
# self.tabdefdict = self.widgetdef['content']
|
|
76
|
+
self.tabdefheadings = [subtabdef[0] for subtabdef in self.widgetdef]
|
|
77
|
+
self.selected_index = 0
|
|
78
|
+
|
|
79
|
+
thiswidget = widgets.Tab if self.tab else widgets.Accordion
|
|
80
|
+
# breakpoint()
|
|
81
|
+
|
|
82
|
+
self.datachildren = [globals()[subwidgettype+'widget'](subwidgetdict['content'],expname=subwidgetdict.get('heading','No heading'))
|
|
83
|
+
for tabheading,(subwidgettype,subwidgetdict) in self.widgetdef ]
|
|
84
|
+
|
|
85
|
+
self.datawidget = thiswidget([child.datawidget for child in self.datachildren],
|
|
86
|
+
selected_index = self.selected_index,
|
|
87
|
+
layout={'height': 'max-content'})
|
|
88
|
+
|
|
89
|
+
for i,key in enumerate(self.tabdefheadings):
|
|
90
|
+
self.datawidget.set_title(i,key)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def update_df(self,df,current_per):
|
|
94
|
+
''' will update container widgets'''
|
|
95
|
+
for w in self.datachildren:
|
|
96
|
+
w.update_df(df,current_per)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def reset(self,g):
|
|
100
|
+
''' will reset container widgets'''
|
|
101
|
+
for w in self.datachildren:
|
|
102
|
+
w.reset(g)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass
|
|
107
|
+
class sheetwidget:
|
|
108
|
+
''' class defining a widget which updates from a sheet '''
|
|
109
|
+
df_var : any = pd.DataFrame() # definition
|
|
110
|
+
trans : any = lambda x : x # renaming of variables
|
|
111
|
+
transpose : bool = False # orientation of dataframe
|
|
112
|
+
expname : str = "Carbon tax rate, US$ per tonn "
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def __post_init__(self):
|
|
116
|
+
...
|
|
117
|
+
|
|
118
|
+
self.wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
newnamedf = self.df_var.copy().rename(columns=self.trans)
|
|
122
|
+
self.org_df_var = self.newnamedf.T if self.transpose else newnamedf
|
|
123
|
+
|
|
124
|
+
self.wsheet = sheet(from_dataframe(self.org_df_var),column_resizing=True)
|
|
125
|
+
self.org_values = [c.value for c in self.wsheet.cells]
|
|
126
|
+
self.datawidget=widgets.VBox([self.wexp,self.wsheet]) if len(self.expname) else self.wsheet
|
|
127
|
+
|
|
128
|
+
def update_df(self,df,current_per=None):
|
|
129
|
+
# breakpoint()
|
|
130
|
+
self.df_new_var = to_dataframe(self.wsheet).T if self.transpose else to_dataframe(self.wsheet)
|
|
131
|
+
self.df_new_var.columns = self.df_var.columns
|
|
132
|
+
self.df_new_var.index = self.df_var.index
|
|
133
|
+
|
|
134
|
+
df.loc[self.df_new_var.index,self.df_new_var.columns] = df.loc[ self.df_new_var.index,self.df_new_var.columns] + self.df_new_var
|
|
135
|
+
pass
|
|
136
|
+
return df
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def reset(self,g):
|
|
140
|
+
for c,v in zip(self.wsheet.cells,self.org_values):
|
|
141
|
+
c.value = v
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@dataclass
|
|
145
|
+
class slidewidget:
|
|
146
|
+
''' class defefining a widget with lines of slides '''
|
|
147
|
+
slidedef : dict # definition
|
|
148
|
+
altname : str = 'Alternative'
|
|
149
|
+
basename : str = 'Baseline'
|
|
150
|
+
expname : str = "Carbon tax rate, US$ per tonn "
|
|
151
|
+
def __post_init__(self):
|
|
152
|
+
...
|
|
153
|
+
|
|
154
|
+
wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
155
|
+
walt = widgets.Label(value = self.altname,layout={'width':'8%', 'border':"hide"})
|
|
156
|
+
wbas = widgets.Label(value = self.basename,layout={'width':'10%', 'border':"hide"})
|
|
157
|
+
whead = widgets.HBox([wexp,walt,wbas])
|
|
158
|
+
#
|
|
159
|
+
breakpoint()
|
|
160
|
+
self.wset = [widgets.FloatSlider(description=des,
|
|
161
|
+
min=cont['min'],
|
|
162
|
+
max=cont['max'],
|
|
163
|
+
value=cont['value'],
|
|
164
|
+
step=cont.get('step',0.01),
|
|
165
|
+
layout={'width':'60%'},style={'description_width':'40%'},
|
|
166
|
+
readout_format = f":>,.{cont.get('dec',2)}f",
|
|
167
|
+
continuous_update=False
|
|
168
|
+
)
|
|
169
|
+
for des,cont in self.slidedef.items()]
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
for w in self.wset:
|
|
173
|
+
w.observe(self.set_slide_value,names='value',type='change')
|
|
174
|
+
|
|
175
|
+
waltval= [widgets.Label(
|
|
176
|
+
value=f"{cont['value']:>,.{cont.get('dec',2)}f}",
|
|
177
|
+
layout=widgets.Layout(display="flex", justify_content="center", width="10%", border="hide"))
|
|
178
|
+
for des,cont in self.slidedef.items()
|
|
179
|
+
|
|
180
|
+
]
|
|
181
|
+
self.wslide = [widgets.HBox([s,v]) for s,v in zip(self.wset,waltval)]
|
|
182
|
+
self.slidewidget = widgets.VBox([whead] + self.wslide)
|
|
183
|
+
self.datawidget = widgets.VBox([whead] + self.wslide)
|
|
184
|
+
|
|
185
|
+
# define the result object
|
|
186
|
+
self.current_values = {des:
|
|
187
|
+
{key : v.split() if key=='var' else v for key,v in cont.items() if key in {'value','var','op'}}
|
|
188
|
+
for des,cont in self.slidedef.items()}
|
|
189
|
+
|
|
190
|
+
# register_widget(self.widget_id, self.datawidget)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def reset(self,g):
|
|
194
|
+
for i,(des,cont) in enumerate(self.slidedef.items()):
|
|
195
|
+
self.wset[i].value = cont['value']
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def update_df(self,df,current_per):
|
|
200
|
+
''' updates a dataframe with the values from the widget'''
|
|
201
|
+
for i,(des,cont) in enumerate(self.current_values.items()):
|
|
202
|
+
op = cont.get('op','=')
|
|
203
|
+
value = cont['value']
|
|
204
|
+
for var in cont['var']:
|
|
205
|
+
if op == '+':
|
|
206
|
+
df.loc[current_per,var] = df.loc[current_per,var] + value
|
|
207
|
+
elif op == '+impulse':
|
|
208
|
+
df.loc[current_per[0],var] = df.loc[current_per[0],var] + value
|
|
209
|
+
elif op == '=start-':
|
|
210
|
+
startindex = df.index.get_loc(current_per[0])
|
|
211
|
+
varloc = df.columns.get_loc(var)
|
|
212
|
+
df.iloc[:startindex,varloc] = value
|
|
213
|
+
elif op == '=':
|
|
214
|
+
df.loc[current_per,var] = value
|
|
215
|
+
elif op == '=impulse':
|
|
216
|
+
df.loc[current_per[0],var] = value
|
|
217
|
+
elif op == '%':
|
|
218
|
+
df.loc[current_per,var] = df.loc[current_per,var] * (1-value/100)
|
|
219
|
+
else:
|
|
220
|
+
print(f'Wrong operator in {cont}.\nNot updated')
|
|
221
|
+
assert 1==3,'wRONG OPERATOR'
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def set_slide_value(self,g):
|
|
225
|
+
''' updates the new values to the self.current_vlues
|
|
226
|
+
will be used in update_df
|
|
227
|
+
'''
|
|
228
|
+
line_des = g['owner'].description
|
|
229
|
+
if 0:
|
|
230
|
+
print()
|
|
231
|
+
for k,v in g.items():
|
|
232
|
+
print(f'{k}:{v}')
|
|
233
|
+
|
|
234
|
+
self.current_values[line_des]['value'] = g['new']
|
|
235
|
+
|
|
236
|
+
@dataclass
|
|
237
|
+
class sumslidewidget:
|
|
238
|
+
''' class defefining a widget with lines of slides '''
|
|
239
|
+
slidedef : dict # definition
|
|
240
|
+
maxsum : any = None
|
|
241
|
+
altname : str = 'Alternative'
|
|
242
|
+
basename : str = 'Baseline'
|
|
243
|
+
expname : str = "Carbon tax rate, US$ per tonn "
|
|
244
|
+
def __post_init__(self):
|
|
245
|
+
...
|
|
246
|
+
|
|
247
|
+
self.first = list(self.slidedef.keys())[:-1]
|
|
248
|
+
self.lastdes = list(self.slidedef.keys())[-1]
|
|
249
|
+
|
|
250
|
+
wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
251
|
+
walt = widgets.Label(value = self.altname,layout={'width':'8%', 'border':"hide"})
|
|
252
|
+
wbas = widgets.Label(value = self.basename,layout={'width':'10%', 'border':"hide"})
|
|
253
|
+
whead = widgets.HBox([wexp,walt,wbas])
|
|
254
|
+
#
|
|
255
|
+
self.wset = [widgets.FloatSlider(description=des,
|
|
256
|
+
min=cont['min'],
|
|
257
|
+
max=cont['max'],
|
|
258
|
+
value=cont['value'],
|
|
259
|
+
step=cont.get('step',0.01),
|
|
260
|
+
layout={'width':'60%'},style={'description_width':'40%'},
|
|
261
|
+
readout_format = f":>,.{cont.get('dec',2)}f",
|
|
262
|
+
continuous_update=False,
|
|
263
|
+
disabled= False
|
|
264
|
+
)
|
|
265
|
+
for des,cont in self.slidedef.items()]
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
for w in self.wset:
|
|
270
|
+
w.observe(self.set_slide_value,names='value',type='change')
|
|
271
|
+
|
|
272
|
+
waltval= [widgets.Label(
|
|
273
|
+
value=f"{cont['value']:>,.{cont.get('dec',2)}f}",
|
|
274
|
+
layout=widgets.Layout(display="flex", justify_content="center", width="10%", border="hide"))
|
|
275
|
+
for des,cont in self.slidedef.items()
|
|
276
|
+
|
|
277
|
+
]
|
|
278
|
+
self.wslide = [widgets.HBox([s,v]) for s,v in zip(self.wset,waltval)]
|
|
279
|
+
self.slidewidget = widgets.VBox([whead] + self.wslide)
|
|
280
|
+
self.datawidget = widgets.VBox([whead] + self.wslide)
|
|
281
|
+
|
|
282
|
+
# define the result object
|
|
283
|
+
self.current_values = {des:
|
|
284
|
+
{key : v.split() if key=='var' else v for key,v in cont.items() if key in {'value','var','op','min','max'}}
|
|
285
|
+
for des,cont in self.slidedef.items()}
|
|
286
|
+
|
|
287
|
+
def reset(self,g):
|
|
288
|
+
for i,(des,cont) in enumerate(self.slidedef.items()):
|
|
289
|
+
self.wset[i].value = cont['value']
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def update_df(self,df,current_per):
|
|
294
|
+
''' updates a dataframe with the values from the widget'''
|
|
295
|
+
for i,(des,cont) in enumerate(self.current_values.items()):
|
|
296
|
+
op = cont.get('op','=')
|
|
297
|
+
value = cont['value']
|
|
298
|
+
for var in cont['var']:
|
|
299
|
+
if op == '+':
|
|
300
|
+
df.loc[current_per,var] = df.loc[current_per,var] + value
|
|
301
|
+
elif op == '+impulse':
|
|
302
|
+
df.loc[current_per[0],var] = df.loc[current_per[0],var] + value
|
|
303
|
+
elif op == '=start-':
|
|
304
|
+
startindex = df.index.get_loc(current_per[0])
|
|
305
|
+
varloc = df.columns.get_loc(var)
|
|
306
|
+
df.iloc[:startindex,varloc] = value
|
|
307
|
+
elif op == '=':
|
|
308
|
+
df.loc[current_per,var] = value
|
|
309
|
+
elif op == '=impulse':
|
|
310
|
+
df.loc[current_per[0],var] = value
|
|
311
|
+
else:
|
|
312
|
+
print(f'Wrong operator in {cont}.\nNot updated')
|
|
313
|
+
assert 1==3,'wRONG OPERATOR'
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def set_slide_value(self,g):
|
|
317
|
+
''' updates the new values to the self.current_vlues
|
|
318
|
+
will be used in update_df
|
|
319
|
+
'''
|
|
320
|
+
line_des = g['owner'].description
|
|
321
|
+
line_index = list(self.current_values.keys()).index(line_des)
|
|
322
|
+
if 0:
|
|
323
|
+
print()
|
|
324
|
+
for k,v in g.items():
|
|
325
|
+
print(f'{k}:{v}')
|
|
326
|
+
|
|
327
|
+
self.current_values[line_des]['value'] = g['new']
|
|
328
|
+
# print(self.current_values)
|
|
329
|
+
if type(self.maxsum) == float:
|
|
330
|
+
allvalues = [v['value'] for v in self.current_values.values()]
|
|
331
|
+
thissum = sum(allvalues)
|
|
332
|
+
if thissum > self.maxsum:
|
|
333
|
+
# print(f'{allvalues=}')
|
|
334
|
+
# print(f"{self.current_values[self.lastdes]['min']=}")
|
|
335
|
+
newlast = self.maxsum-sum(allvalues[:-1])
|
|
336
|
+
newlast = max(newlast,self.current_values[self.lastdes]['min'])
|
|
337
|
+
# print(f'{newlast=}')
|
|
338
|
+
self.current_values[self.lastdes]['value']= newlast
|
|
339
|
+
|
|
340
|
+
newsum= sum([v['value'] for v in self.current_values.values()])
|
|
341
|
+
# print(f'{newsum=}')
|
|
342
|
+
# print(f'{line_index=}')
|
|
343
|
+
if newsum > self.maxsum:
|
|
344
|
+
self.current_values[line_des]['value']=self.wset[line_index].value-newsum +self.maxsum
|
|
345
|
+
self.wset[line_index].value = self.current_values[line_des]['value']
|
|
346
|
+
|
|
347
|
+
self.wset[-1].value = newlast
|
|
348
|
+
|
|
349
|
+
@dataclass
|
|
350
|
+
class radiowidget:
|
|
351
|
+
''' class defefining a widget with a number of radiobutton groups
|
|
352
|
+
|
|
353
|
+
When df_update is executed the cheked variable will be set to 1 the other to 0
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
{'<heading 1> [['text1','variable1'],
|
|
358
|
+
['text2','variable2']
|
|
359
|
+
.....]
|
|
360
|
+
'heading 2' [['text1','variable1'],
|
|
361
|
+
['text2','variable2']
|
|
362
|
+
.....]
|
|
363
|
+
|
|
364
|
+
....
|
|
365
|
+
....
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
'''
|
|
371
|
+
radiodef : dict # definition
|
|
372
|
+
expname : str = "Carbon tax rate, US$ per tonn "
|
|
373
|
+
def __post_init__(self):
|
|
374
|
+
...
|
|
375
|
+
|
|
376
|
+
wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
377
|
+
whead = widgets.HBox([wexp])
|
|
378
|
+
#
|
|
379
|
+
self.wradiolist = [widgets.RadioButtons(options=[i for i,j in cont],description=des,layout={'width':'70%'},
|
|
380
|
+
style={'description_width':'37%'}) for des,cont in self.radiodef.items()]
|
|
381
|
+
if len(self.wradiolist) <=2:
|
|
382
|
+
self.wradio = widgets.HBox(self.wradiolist)
|
|
383
|
+
else:
|
|
384
|
+
self.wradio = widgets.VBox(self.wradiolist)
|
|
385
|
+
|
|
386
|
+
self.datawidget = widgets.VBox([whead] + [self.wradio])
|
|
387
|
+
|
|
388
|
+
def reset(self,g):
|
|
389
|
+
for wradio in self.wradiolist:
|
|
390
|
+
wradio.index = 0
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def update_df(self,df,current_per):
|
|
394
|
+
''' updates a dataframe with the values from the widget'''
|
|
395
|
+
for wradio,(des,cont) in zip(self.wradiolist,self.radiodef.items()):
|
|
396
|
+
# print(f'{des=} {wradio.value=},{wradio.index=},{cont[wradio.index]=}')
|
|
397
|
+
# print(f'{cont=}')
|
|
398
|
+
for varlabel,variable in cont:
|
|
399
|
+
df.loc[current_per,variable] = 0
|
|
400
|
+
selected_variable = cont[wradio.index][1]
|
|
401
|
+
df.loc[current_per,selected_variable] = 1
|
|
402
|
+
# print(df.loc[current_per,:])
|
|
403
|
+
|
|
404
|
+
@dataclass
|
|
405
|
+
class checkwidget:
|
|
406
|
+
''' class defefining a widget with a number of radiobutton groups
|
|
407
|
+
|
|
408
|
+
When df_update is executed the cheked variable will be set to 1 the other to 0
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
{
|
|
413
|
+
'<heading 1>:['variable1 variable2 ---',value ],
|
|
414
|
+
'<heading 2>:['variable10 variable20 ---',value ]
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
'''
|
|
420
|
+
checkdef : any # definition
|
|
421
|
+
expname : str = ""
|
|
422
|
+
def __post_init__(self):
|
|
423
|
+
...
|
|
424
|
+
|
|
425
|
+
wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
426
|
+
whead = widgets.HBox([wexp])
|
|
427
|
+
#
|
|
428
|
+
self.wchecklist = [widgets.Checkbox(description=des,value=val) for des,(var,val)
|
|
429
|
+
in self.checkdef.items()]
|
|
430
|
+
self.wcheck = widgets.VBox(self.wchecklist)
|
|
431
|
+
|
|
432
|
+
self.datawidget = widgets.VBox([whead] + [self.wcheck])
|
|
433
|
+
|
|
434
|
+
def reset(self,g):
|
|
435
|
+
for wcheck,(des,(variable,val)) in zip(self.wchecklist,self.checkdef.items()):
|
|
436
|
+
wcheck.value = val
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
def update_df(self,df,current_per):
|
|
440
|
+
''' updates a dataframe with the values from the widget'''
|
|
441
|
+
for wcheck,(des,(variable,val)) in zip(self.wchecklist,self.checkdef.items()):
|
|
442
|
+
for selected_variable in variable.split():
|
|
443
|
+
df.loc[current_per,selected_variable] = 1.0 if wcheck.value else 0.0
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
@dataclass
|
|
448
|
+
class updatewidget:
|
|
449
|
+
''' class to input and run a model
|
|
450
|
+
|
|
451
|
+
- display(wtotal) to activate the widget
|
|
452
|
+
|
|
453
|
+
'''
|
|
454
|
+
|
|
455
|
+
mmodel : any # a model
|
|
456
|
+
datawidget : any # a widget to update from
|
|
457
|
+
basename : str ='Business as usual'
|
|
458
|
+
keeppat : str = '*'
|
|
459
|
+
varpat : str ='*'
|
|
460
|
+
showvarpat : bool = True # Show varpaths to
|
|
461
|
+
exodif : any = pd.DataFrame() # definition
|
|
462
|
+
lwrun : bool = True
|
|
463
|
+
lwupdate : bool = False
|
|
464
|
+
lwreset : bool = True
|
|
465
|
+
lwsetbas : bool = True
|
|
466
|
+
outputwidget : str = 'jupviz'
|
|
467
|
+
display_first :any = None
|
|
468
|
+
# to the plot widget
|
|
469
|
+
prefix_dict : dict = field(default_factory=dict)
|
|
470
|
+
vline : list = field(default_factory=list)
|
|
471
|
+
relativ_start : int = 0
|
|
472
|
+
short :bool = False
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
def __post_init__(self):
|
|
477
|
+
self.baseline = self.mmodel.basedf.copy()
|
|
478
|
+
wrun = widgets.Button(description="Run scenario")
|
|
479
|
+
wrun.on_click(self.run)
|
|
480
|
+
|
|
481
|
+
wupdate = widgets.Button(description="Update the dataset ")
|
|
482
|
+
wupdate.on_click(self.update)
|
|
483
|
+
|
|
484
|
+
wreset = widgets.Button(description="Reset to start")
|
|
485
|
+
wreset.on_click(self.reset)
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
wsetbas = widgets.Button(description="Use as baseline")
|
|
489
|
+
wsetbas.on_click(self.setbasis)
|
|
490
|
+
self.experiment = 0
|
|
491
|
+
|
|
492
|
+
lbut = []
|
|
493
|
+
|
|
494
|
+
if self.lwrun: lbut.append(wrun)
|
|
495
|
+
if self.lwupdate: lbut.append(wupdate)
|
|
496
|
+
if self.lwreset: lbut.append(wreset)
|
|
497
|
+
if self.lwsetbas : lbut.append(wsetbas)
|
|
498
|
+
|
|
499
|
+
wbut = widgets.HBox(lbut)
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
self.wname = widgets.Text(value=self.basename,placeholder='Type something',description='Scenario name:',
|
|
503
|
+
layout={'width':'30%'},style={'description_width':'50%'})
|
|
504
|
+
self.wpat = widgets.Text(value= self.varpat,placeholder='Type something',description='Display variables:',
|
|
505
|
+
layout={'width':'65%'},style={'description_width':'30%'})
|
|
506
|
+
|
|
507
|
+
self.wpat.layout.visibility = 'visible' if self.showvarpat else 'hidden'
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
winputstring = widgets.HBox([self.wname,self.wpat])
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
self.mmodel.keep_solutions = {}
|
|
514
|
+
self.mmodel.keep_solutions = {self.wname.value : self.baseline}
|
|
515
|
+
self.mmodel.keep_exodif = {}
|
|
516
|
+
|
|
517
|
+
self.experiment += 1
|
|
518
|
+
self.wname.value = f'Experiment {self.experiment}'
|
|
519
|
+
|
|
520
|
+
self.keep_ui = keep_plot_shiny(mmodel = self.mmodel,
|
|
521
|
+
selectfrom = self.varpat, prefix_dict=self.prefix_dict,
|
|
522
|
+
vline=self.vline,relativ_start=self.relativ_start,
|
|
523
|
+
short = self.short)
|
|
524
|
+
|
|
525
|
+
self.wtotal = widgets.VBox([self.datawidget.datawidget,winputstring,wbut,
|
|
526
|
+
self.keep_ui.datawidget])
|
|
527
|
+
|
|
528
|
+
self.start = copy(self.mmodel.current_per[0])
|
|
529
|
+
self.slut = copy(self.mmodel.current_per[-1])
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
def update(self,g):
|
|
534
|
+
self.thisexperiment = self.baseline.copy()
|
|
535
|
+
# print(f'update smpl {self.mmodel.current_per=}')
|
|
536
|
+
|
|
537
|
+
self.datawidget.update_df(self.thisexperiment,self.mmodel.current_per)
|
|
538
|
+
self.exodif = self.mmodel.exodif(self.baseline,self.thisexperiment)
|
|
539
|
+
print(f'update 2 smpl {self.mmodel.current_per[0]=}')
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
def run(self,g):
|
|
545
|
+
self.update(g)
|
|
546
|
+
# print(f'run smpl {self.mmodel.current_per[0]=}')
|
|
547
|
+
# self.start = self.mmodel.current_per[0]
|
|
548
|
+
# self.slut = self.mmodel.current_per[-1]
|
|
549
|
+
print(f'{self.start=} {self.slut=}')
|
|
550
|
+
self.mmodel(self.thisexperiment,start=self.start,slut=self.slut,progressbar=1,keep = self.wname.value,
|
|
551
|
+
keep_variables = self.keeppat)
|
|
552
|
+
# print(f'run efter smpl {self.mmodel.current_per[0]=}')
|
|
553
|
+
self.mmodel.keep_exodif[self.wname.value] = self.exodif
|
|
554
|
+
self.mmodel.inputwidget_alternativerun = True
|
|
555
|
+
self.current_experiment = self.wname.value
|
|
556
|
+
self.experiment += 1
|
|
557
|
+
self.wname.value = f'Experiment {self.experiment}'
|
|
558
|
+
self.keep_ui.trigger(None)
|
|
559
|
+
|
|
560
|
+
def setbasis(self,g):
|
|
561
|
+
self.mmodel.keep_solutions={self.current_experiment:self.mmodel.keep_solutions[self.current_experiment]}
|
|
562
|
+
|
|
563
|
+
self.mmodel.keep_exodif[self.current_experiment] = self.exodif
|
|
564
|
+
self.mmodel.inputwidget_alternativerun = True
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
def reset(self,g):
|
|
570
|
+
self.datawidget.reset(g)
|
|
571
|
+
|
|
572
|
+
def fig_to_image(figs,format='svg'):
|
|
573
|
+
from io import StringIO
|
|
574
|
+
f = StringIO()
|
|
575
|
+
figs.savefig(f,format=format,bbox_inches="tight")
|
|
576
|
+
f.seek(0)
|
|
577
|
+
image= f.read()
|
|
578
|
+
return image
|
|
579
|
+
|
|
580
|
+
@dataclass
|
|
581
|
+
class htmlwidget_df:
|
|
582
|
+
''' class displays a dataframe in a html widget '''
|
|
583
|
+
|
|
584
|
+
mmodel : any # a model
|
|
585
|
+
df_var : any = pd.DataFrame() # definition
|
|
586
|
+
trans : any = lambda x : x # renaming of variables
|
|
587
|
+
transpose : bool = False # orientation of dataframe
|
|
588
|
+
expname : str = "Test"
|
|
589
|
+
percent : bool = False
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
def __post_init__(self):
|
|
593
|
+
...
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
newnamedf = self.df_var.copy().rename(columns=self.trans)
|
|
598
|
+
self.org_df_var = newnamedf.T if self.transpose else newnamedf
|
|
599
|
+
self.wexp = widgets.Label(value = f'{self.expname} {self.org_df_var.shape}',layout={'width':'54%'})
|
|
600
|
+
self.datawidget=self.wexp
|
|
601
|
+
# return
|
|
602
|
+
image = self.mmodel.ibsstyle(self.org_df_var,percent = self.percent).to_html()
|
|
603
|
+
# image = self.org_df_var.to_html()
|
|
604
|
+
self.whtml = widgets.HTML(image)
|
|
605
|
+
# self.datawidget=widgets.VBox([self.wexp,self.whtml]) if len(self.expname) else self.whtml
|
|
606
|
+
self.datawidget=widgets.VBox([self.wexp,self.whtml]) if len(self.expname) else self.whtml
|
|
607
|
+
|
|
608
|
+
@property
|
|
609
|
+
def show(self):
|
|
610
|
+
display(self.datawidget)
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
@dataclass
|
|
614
|
+
class htmlwidget_fig:
|
|
615
|
+
''' class displays a dataframe in a html widget '''
|
|
616
|
+
|
|
617
|
+
figs : any # a model
|
|
618
|
+
expname : str = ""
|
|
619
|
+
format : str = "svg"
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
def __post_init__(self):
|
|
623
|
+
...
|
|
624
|
+
|
|
625
|
+
self.wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
image = fig_to_image(self.figs,format=self.format)
|
|
629
|
+
# self.whtml = widgets.HTML(image,layout={'width':'54%'})
|
|
630
|
+
self.whtml = widgets.Image(image,width=300,format = 'svg')
|
|
631
|
+
self.datawidget=widgets.VBox([self.wexp,self.whtml]) if len(self.expname) else self.whtml
|
|
632
|
+
|
|
633
|
+
@dataclass
|
|
634
|
+
class htmlwidget_label:
|
|
635
|
+
''' class displays a dataframe in a html widget '''
|
|
636
|
+
|
|
637
|
+
expname : str = ""
|
|
638
|
+
format : str = "svg"
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
def __post_init__(self):
|
|
642
|
+
...
|
|
643
|
+
|
|
644
|
+
self.wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
self.datawidget=self.wexp
|
|
648
|
+
|
|
649
|
+
@dataclass
|
|
650
|
+
class visshow:
|
|
651
|
+
mmodel : any # a model
|
|
652
|
+
varpat : str ='*'
|
|
653
|
+
showvarpat : bool = True
|
|
654
|
+
show_on : bool = True # Display when called
|
|
655
|
+
|
|
656
|
+
def __post_init__(self):
|
|
657
|
+
...
|
|
658
|
+
from IPython import get_ipython
|
|
659
|
+
try:
|
|
660
|
+
get_ipython().magic('matplotlib notebook')
|
|
661
|
+
except:
|
|
662
|
+
...
|
|
663
|
+
# print(plt.get_backend())
|
|
664
|
+
this_vis = self.mmodel[self.varpat]
|
|
665
|
+
self.out_dict = {}
|
|
666
|
+
self.out_dict['Baseline'] ={'df':this_vis.base}
|
|
667
|
+
self.out_dict['Alternative'] ={'df':this_vis}
|
|
668
|
+
self.out_dict['Difference'] ={'df':this_vis.dif}
|
|
669
|
+
self.out_dict['Diff. pct. level'] ={'df':this_vis.difpctlevel.mul100,'percent':True}
|
|
670
|
+
self.out_dict['Base growth'] ={'df':this_vis.base.pct.mul100,'percent':True}
|
|
671
|
+
self.out_dict['Alt. growth'] ={'df':this_vis.pct.mul100,'percent':True}
|
|
672
|
+
self.out_dict['Diff. in growth'] ={'df':this_vis.difpct.mul100,'percent':True}
|
|
673
|
+
|
|
674
|
+
out = widgets.Output()
|
|
675
|
+
self.out_to_data = {key:
|
|
676
|
+
htmlwidget_df(self.mmodel,value['df'].df.T,expname=key,
|
|
677
|
+
|
|
678
|
+
percent=value.get('percent',False))
|
|
679
|
+
for key,value in self.out_dict.items()}
|
|
680
|
+
|
|
681
|
+
with out: # to suppress the display of matplotlib creation
|
|
682
|
+
self.out_to_figs ={key:
|
|
683
|
+
htmlwidget_fig(value['df'].rename().plot(top=0.9,title=''),expname='')
|
|
684
|
+
for key,value in self.out_dict.items() }
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
tabnew = {key: tabwidget({'Charts':self.out_to_figs[key],'Data':self.out_to_data[key]},selected_index=0) for key in self.out_to_figs.keys()}
|
|
688
|
+
|
|
689
|
+
exodif = self.mmodel.exodif()
|
|
690
|
+
exonames = exodif.columns
|
|
691
|
+
exoindex = exodif.index
|
|
692
|
+
if len(exonames):
|
|
693
|
+
exobase = self.mmodel.basedf.loc[exoindex,exonames]
|
|
694
|
+
exolast = self.mmodel.lastdf.loc[exoindex,exonames]
|
|
695
|
+
|
|
696
|
+
out_exodif = htmlwidget_df(self.mmodel,exodif.T)
|
|
697
|
+
out_exobase = htmlwidget_df(self.mmodel,exobase.T)
|
|
698
|
+
out_exolast = htmlwidget_df(self.mmodel,exolast.T)
|
|
699
|
+
else:
|
|
700
|
+
out_exodif = htmlwidget_label(expname = 'No difference in exogenous')
|
|
701
|
+
out_exobase = htmlwidget_label(expname = 'No difference in exogenous')
|
|
702
|
+
out_exolast = htmlwidget_label(expname = 'No difference in exogenous')
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
out_tab_info = tabwidget({'Delta exogenous':out_exodif,
|
|
706
|
+
'Baseline exogenous':out_exobase,
|
|
707
|
+
'Alt. exogenous':out_exolast},selected_index=0)
|
|
708
|
+
|
|
709
|
+
tabnew['Exo info'] = out_tab_info
|
|
710
|
+
|
|
711
|
+
this = tabwidget(tabnew,selected_index=0)
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
self.datawidget = this.datawidget
|
|
716
|
+
if self.show_on:
|
|
717
|
+
...
|
|
718
|
+
display(self.datawidget)
|
|
719
|
+
try:
|
|
720
|
+
get_ipython().magic('matplotlib inline')
|
|
721
|
+
except:
|
|
722
|
+
...
|
|
723
|
+
|
|
724
|
+
def __repr__(self):
|
|
725
|
+
return ''
|
|
726
|
+
|
|
727
|
+
def _html_repr_(self):
|
|
728
|
+
return self.datawidget
|
|
729
|
+
|
|
730
|
+
@property
|
|
731
|
+
def show(self):
|
|
732
|
+
display(self.datawidget)
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
@dataclass
|
|
736
|
+
class shinywidget:
|
|
737
|
+
'''A to translate a widget to shiny widget '''
|
|
738
|
+
|
|
739
|
+
a_widget : any # The datawidget to wrap
|
|
740
|
+
widget_id : str
|
|
741
|
+
|
|
742
|
+
def __post_init__(self):
|
|
743
|
+
from shinywidgets import register_widget
|
|
744
|
+
...
|
|
745
|
+
register_widget(self.widget_id, self.a_widget.datawidget)
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
def update_df(self,df,current_per):
|
|
749
|
+
''' will update container widgets'''
|
|
750
|
+
self.a_widget.update_df(df,current_per)
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
def reset(self,g):
|
|
754
|
+
''' will reset container widgets'''
|
|
755
|
+
self.a_widget.reset(g)
|
|
756
|
+
|
|
757
|
+
@dataclass
|
|
758
|
+
class keep_plot_shiny:
|
|
759
|
+
mmodel : any # a model
|
|
760
|
+
pat : str ='*'
|
|
761
|
+
smpl=('', '')
|
|
762
|
+
relativ_start : int = 0
|
|
763
|
+
selectfrom : str ='*'
|
|
764
|
+
legend=0
|
|
765
|
+
dec=''
|
|
766
|
+
use_descriptions=True
|
|
767
|
+
select_width=''
|
|
768
|
+
select_height='200px'
|
|
769
|
+
vline : list = field(default_factory=list)
|
|
770
|
+
prefix_dict : dict = field(default_factory=dict)
|
|
771
|
+
add_var_name=False
|
|
772
|
+
short :any = 0
|
|
773
|
+
multi :any = False
|
|
774
|
+
|
|
775
|
+
"""
|
|
776
|
+
Plots the keept dataframes
|
|
777
|
+
|
|
778
|
+
Args:
|
|
779
|
+
pat (str, optional): a string of variables to select pr default. Defaults to '*'.
|
|
780
|
+
smpl (tuple with 2 elements, optional): the selected smpl, has to match the dataframe index used. Defaults to ('','').
|
|
781
|
+
selectfrom (list, optional): the variables to select from, Defaults to [] -> all keept variables .
|
|
782
|
+
legend (bool, optional)c: DESCRIPTION. legends or to the right of the curve. Defaults to 1.
|
|
783
|
+
dec (string, optional): decimals on the y-axis. Defaults to '0'.
|
|
784
|
+
use_descriptions : Use the variable descriptions from the model
|
|
785
|
+
|
|
786
|
+
Returns:
|
|
787
|
+
None.
|
|
788
|
+
|
|
789
|
+
self.keep_wiz_figs is set to a dictionary containing the figures. Can be used to produce publication
|
|
790
|
+
quality files.
|
|
791
|
+
|
|
792
|
+
"""
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
def __post_init__(self):
|
|
796
|
+
from copy import copy
|
|
797
|
+
minper = self.mmodel.lastdf.index[0]
|
|
798
|
+
maxper = self.mmodel.lastdf.index[-1]
|
|
799
|
+
options = [(ind, nr) for nr, ind in enumerate(self.mmodel.lastdf.index)]
|
|
800
|
+
self.old_current_per = copy(self.mmodel.current_per)
|
|
801
|
+
# print(f'Før {self.mmodel.current_per=}')
|
|
802
|
+
with self.mmodel.set_smpl(*self.smpl):
|
|
803
|
+
# print(f'efter set smpl {self.mmodel.current_per=}')
|
|
804
|
+
with self.mmodel.set_smpl_relative(self.relativ_start,0):
|
|
805
|
+
# print(f'efter set smpl relativ {self.mmodel.current_per=}')
|
|
806
|
+
...
|
|
807
|
+
show_per = copy(list(self.mmodel.current_per)[:])
|
|
808
|
+
self.mmodel.current_per = copy(self.old_current_per)
|
|
809
|
+
# print(f'efter context set smpl {self.mmodel.current_per=}')
|
|
810
|
+
# breakpoint()
|
|
811
|
+
init_start = self.mmodel.lastdf.index.get_loc(show_per[0])
|
|
812
|
+
init_end = self.mmodel.lastdf.index.get_loc(show_per[-1])
|
|
813
|
+
keepvar = sorted (list(self.mmodel.keep_solutions.values())[0].columns)
|
|
814
|
+
defaultvar = ([v for v in self.mmodel.vlist(self.pat) if v in keepvar][0],)
|
|
815
|
+
# print('******* selectfrom')
|
|
816
|
+
_selectfrom = [s.upper() for s in self.mmodel.vlist(self.selectfrom)] if self.selectfrom else keepvar
|
|
817
|
+
# _selectfrom = keepvar
|
|
818
|
+
# print(f'{_selectfrom=}')
|
|
819
|
+
gross_selectfrom = [(f'{(v+" ") if self.add_var_name else ""}{self.mmodel.var_description[v] if self.use_descriptions else v}',v) for v in _selectfrom]
|
|
820
|
+
# print(f'{gross_selectfrom=}')
|
|
821
|
+
width = self.select_width if self.select_width else '50%' if self.use_descriptions else '50%'
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
description_width = 'initial'
|
|
825
|
+
description_width_long = 'initial'
|
|
826
|
+
keep_keys = list(self.mmodel.keep_solutions.keys())
|
|
827
|
+
keep_first = keep_keys[0]
|
|
828
|
+
select_prefix = [(c,iso) for iso,c in self.prefix_dict.items()]
|
|
829
|
+
i_smpl = SelectionRangeSlider(value=[init_start, init_end], continuous_update=False, options=options, min=minper,
|
|
830
|
+
max=maxper, layout=Layout(width='75%'), description='Show interval')
|
|
831
|
+
selected_vars = SelectMultiple( options=gross_selectfrom, layout=Layout(width=width, height=self.select_height, font="monospace"),
|
|
832
|
+
description='Select one or more', style={'description_width': description_width})
|
|
833
|
+
|
|
834
|
+
diff = RadioButtons(options=[('No', False), ('Yes', True), ('In percent', 'pct')], description=fr'Difference to: "{keep_first}"',
|
|
835
|
+
value=False, style={'description_width': 'auto'}, layout=Layout(width='auto'))
|
|
836
|
+
showtype = RadioButtons(options=[('Level', 'level'), ('Growth', 'growth')],
|
|
837
|
+
description='Data type', value='level', style={'description_width': description_width})
|
|
838
|
+
scale = RadioButtons(options=[('Linear', 'linear'), ('Log', 'log')], description='Y-scale',
|
|
839
|
+
value='linear', style={'description_width': description_width})
|
|
840
|
+
#
|
|
841
|
+
legend = RadioButtons(options=[('Yes', 1), ('No', 0)], description='Legends', value=self.legend, style={
|
|
842
|
+
'description_width': description_width})
|
|
843
|
+
|
|
844
|
+
self.widget_dict = {'i_smpl': i_smpl, 'selected_vars': selected_vars, 'diff': diff, 'showtype': showtype,
|
|
845
|
+
'scale': scale, 'legend': legend}
|
|
846
|
+
for wid in self.widget_dict.values():
|
|
847
|
+
wid.observe(self.trigger,names='value',type='change')
|
|
848
|
+
|
|
849
|
+
# breakpoint()
|
|
850
|
+
self.out_widget = widgets.HTML(value="Hello <b>World</b>",
|
|
851
|
+
placeholder='',
|
|
852
|
+
description='',)
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
# selected_vars.observe(get_value_selected_vars,names='value',type='change')
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
def get_prefix(g):
|
|
862
|
+
|
|
863
|
+
try:
|
|
864
|
+
current_suffix = {v[len(g['old'][0]):] for v in selected_vars.value}
|
|
865
|
+
except:
|
|
866
|
+
current_suffix = ''
|
|
867
|
+
new_prefix = g['new']
|
|
868
|
+
selected_prefix_var = tuple((des,variable) for des,variable in gross_selectfrom
|
|
869
|
+
if any([variable.startswith(n) for n in new_prefix]))
|
|
870
|
+
# An exception is trigered but has no consequences
|
|
871
|
+
try:
|
|
872
|
+
selected_vars.options = selected_prefix_var
|
|
873
|
+
except:
|
|
874
|
+
...
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
# print('here')
|
|
878
|
+
|
|
879
|
+
if current_suffix:
|
|
880
|
+
new_selection = [f'{n}{c}' for c in current_suffix for n in new_prefix
|
|
881
|
+
if f'{n}{c}' in {s for p,s in selected_prefix_var}]
|
|
882
|
+
selected_vars.value = new_selection
|
|
883
|
+
# print(f"{new_selection=}{current_suffix=}{g['old']=}")
|
|
884
|
+
else:
|
|
885
|
+
selected_vars.value = [selected_prefix_var[0][1]]
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
if len(self.prefix_dict):
|
|
889
|
+
selected_prefix = SelectMultiple(value=[select_prefix[0][1]], options=select_prefix,
|
|
890
|
+
layout=Layout(width='25%', height=self.select_height, font="monospace"),
|
|
891
|
+
description='')
|
|
892
|
+
|
|
893
|
+
selected_prefix.observe(get_prefix,names='value',type='change')
|
|
894
|
+
select = HBox([selected_vars,selected_prefix])
|
|
895
|
+
# print(f'{select_prefix=}')
|
|
896
|
+
get_prefix({'new':select_prefix[0]})
|
|
897
|
+
else:
|
|
898
|
+
select = VBox([selected_vars])
|
|
899
|
+
|
|
900
|
+
options1 = HBox([diff]) if self.short >=2 else HBox([diff,legend])
|
|
901
|
+
options2 = HBox([scale, showtype])
|
|
902
|
+
if self.short:
|
|
903
|
+
vui = [select, options1, i_smpl]
|
|
904
|
+
else:
|
|
905
|
+
vui = [select, options1, options2, i_smpl]
|
|
906
|
+
vui = vui[:-1] if self.short >= 2 else vui
|
|
907
|
+
|
|
908
|
+
self.datawidget= VBox(vui+[self.out_widget])
|
|
909
|
+
# show = interactive_output(explain, {'i_smpl': i_smpl, 'selected_vars': selected_vars, 'diff': diff, 'showtype': showtype,
|
|
910
|
+
# 'scale': scale, 'legend': legend})
|
|
911
|
+
# print('klar til register')
|
|
912
|
+
|
|
913
|
+
def explain(self, i_smpl=None, selected_vars=None, diff=None, showtype=None, scale=None, legend= None):
|
|
914
|
+
# print(f'Start explain {self.mmodel.current_per[0]=}')
|
|
915
|
+
variabler = ' '.join(v for v in selected_vars)
|
|
916
|
+
smpl = (self.mmodel.lastdf.index[i_smpl[0]], self.mmodel.lastdf.index[i_smpl[1]])
|
|
917
|
+
if type(diff) == str:
|
|
918
|
+
diffpct = True
|
|
919
|
+
ldiff = False
|
|
920
|
+
else:
|
|
921
|
+
ldiff = diff
|
|
922
|
+
diffpct = False
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
with self.mmodel.set_smpl(*smpl):
|
|
926
|
+
|
|
927
|
+
if self.multi:
|
|
928
|
+
self.keep_wiz_fig = self.mmodel.keep_plot_multi(variabler, diff=ldiff, diffpct = diffpct, scale=scale, showtype=showtype,
|
|
929
|
+
legend=legend, dec=self.dec, vline=self.vline)
|
|
930
|
+
else:
|
|
931
|
+
self.keep_wiz_figs = self.mmodel.keep_plot(variabler, diff=ldiff, diffpct = diffpct,
|
|
932
|
+
scale=scale, showtype=showtype,
|
|
933
|
+
legend=legend, dec=self.dec, vline=self.vline)
|
|
934
|
+
self.keep_wiz_fig = self.keep_wiz_figs[selected_vars[0]]
|
|
935
|
+
# print(f'Efter plot {self.mmodel.current_per=}')
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
def trigger(self,g):
|
|
939
|
+
self.mmodel.current_per = copy(self.old_current_per)
|
|
940
|
+
|
|
941
|
+
values = {widname : wid.value for widname,wid in self.widget_dict.items() }
|
|
942
|
+
# print(f'Triggerd:\n{values} \n Start trigger {self.mmodel.current_per=}')
|
|
943
|
+
self.explain(**values)
|
|
944
|
+
xxx = fig_to_image(self.keep_wiz_fig,format='svg')
|
|
945
|
+
self.out_widget.value = xxx
|
|
946
|
+
# print(f'end trigger {self.mmodel.current_per[0]=}')
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
def make_widget(widgetdef):
|
|
952
|
+
'''
|
|
953
|
+
'''
|
|
954
|
+
|
|
955
|
+
# breakpoint()
|
|
956
|
+
widgettype,widgetdict = widgetdef
|
|
957
|
+
# ...
|
|
958
|
+
# print(widgettype,widgetdict)
|
|
959
|
+
# reswidget = [globals()[widgettype+'widget'](widgetdict['content'],expname=widgetdict.get('heading','No heading'))
|
|
960
|
+
# for widgettype,widgetdict in widgetdefs ]
|
|
961
|
+
# return basewidget({'content':reswidget})
|
|
962
|
+
# reswidget = [globals()[widgettype+'widget'](widgetdict['content'],expname=widgetdict.get('heading','No heading'))
|
|
963
|
+
# for widgettype,widgetdict in widgetdefs ]
|
|
964
|
+
reswidget = globals()[widgettype+'widget'](widgetdict['content'],
|
|
965
|
+
tab=widgetdict.get('tab',True ),
|
|
966
|
+
expname=widgetdict.get('heading','No heading')
|
|
967
|
+
)
|
|
968
|
+
return reswidget
|
|
969
|
+
|
|
970
|
+
slidedef = ['slide', {'heading':'Dette er en prøve',
|
|
971
|
+
'content' :{'Productivity' : {'var':'ALFA', 'value': 0.5 ,'min':0.0, 'max':1.0},
|
|
972
|
+
'DEPRECIATES_RATE' : {'var':'DEPRECIATES_RATE', 'value': 0.05,'min':0.0, 'max':1.0},
|
|
973
|
+
'LABOR_GROWTH' : {'var':'LABOR_GROWTH', 'value': 0.01,'min':0.0, 'max':1.0},
|
|
974
|
+
'SAVING_RATIO' : {'var': 'SAVING_RATIO', 'value': 0.05,'min':0.0, 'max':1.0}
|
|
975
|
+
}} ]
|
|
976
|
+
|
|
977
|
+
radiodef = ['radio', {'heading':'Dette er en anden prøve',
|
|
978
|
+
'content' : {'Ib': [['Arbejdskraft','LABOR_GROWTH'],
|
|
979
|
+
['Opsparing','SAVING_RATIO'] ],
|
|
980
|
+
'Søren': [['alfa','ALFA'],
|
|
981
|
+
['a','A'] ],
|
|
982
|
+
'Marie': [['Hest','HEST'],
|
|
983
|
+
['Ko','KO'] ],
|
|
984
|
+
|
|
985
|
+
}} ]
|
|
986
|
+
|
|
987
|
+
checkdef = ['check', {'heading':'Dette er en treidje prøve',
|
|
988
|
+
'content' :{'Arbejdskraft':['LABOR_GROWTH KO',1],
|
|
989
|
+
'Opsparing' :['SAVING_RATIO',0] }}]
|
|
990
|
+
|
|
991
|
+
alldef = [slidedef] + [radiodef] + [checkdef]
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
tabdef = ['tab',{'content':{'Den første':{'content':alldef},'Den anden':{'content':alldef}}}]
|
|
995
|
+
tabdef = ['tab',{'content':[['Den første',['base',{'content':alldef}]],
|
|
996
|
+
['Den anden' ,['base',{'content':alldef}]]]
|
|
997
|
+
}]
|
|
998
|
+
|
|
999
|
+
basedef = ['base',{'content':alldef}]
|
|
1000
|
+
|
|
1001
|
+
make_widget(tabdef)
|
|
1002
|
+
|
|
1003
|
+
# print(yaml.dump(tabdef,indent=4))
|
|
1004
|
+
|
|
1005
|
+
|