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.py
ADDED
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Created on Mon Aug 9 14:46:11 2021
|
|
4
|
+
|
|
5
|
+
To define Jupyter widgets show variables.
|
|
6
|
+
@author: Ib
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import pandas as pd
|
|
10
|
+
import ipywidgets as widgets
|
|
11
|
+
|
|
12
|
+
from ipysheet import sheet, cell, current
|
|
13
|
+
from ipysheet.pandas_loader import from_dataframe, to_dataframe
|
|
14
|
+
|
|
15
|
+
from IPython.display import display, clear_output,Latex, Markdown
|
|
16
|
+
from dataclasses import dataclass,field
|
|
17
|
+
import matplotlib.pylab as plt
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
from modelclass import insertModelVar
|
|
22
|
+
from modelclass import model
|
|
23
|
+
from modeljupyter import jupviz
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class basewidget:
|
|
28
|
+
''' basis for widget updating in jupyter'''
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
datachildren : list = field(default_factory=list) # list of children widgets
|
|
32
|
+
|
|
33
|
+
def update_df(self,df,current_per):
|
|
34
|
+
''' will update container widgets'''
|
|
35
|
+
for w in self.datachildren:
|
|
36
|
+
w.update_df(df,current_per)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass
|
|
40
|
+
class tabwidget:
|
|
41
|
+
'''A widget to create tab or acordium contaners'''
|
|
42
|
+
|
|
43
|
+
tabdefdict : dict # = field(default_factory = lambda: ({}))
|
|
44
|
+
tab : bool = True
|
|
45
|
+
selected_index :any = None
|
|
46
|
+
|
|
47
|
+
def __post_init__(self):
|
|
48
|
+
|
|
49
|
+
thiswidget = widgets.Tab if self.tab else widgets.Accordion
|
|
50
|
+
self.datachildren = [tabcontent
|
|
51
|
+
for tabcontent in self.tabdefdict.values()]
|
|
52
|
+
self.datawidget = thiswidget([child.datawidget for child in self.datachildren],
|
|
53
|
+
selected_index = self.selected_index,
|
|
54
|
+
layout={'height': 'max-content'})
|
|
55
|
+
|
|
56
|
+
for i,key in enumerate(self.tabdefdict.keys()):
|
|
57
|
+
self.datawidget.set_title(i,key)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def update_df(self,df,current_per):
|
|
61
|
+
''' will update container widgets'''
|
|
62
|
+
for w in self.datachildren:
|
|
63
|
+
w.update_df(df,current_per)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def reset(self,g):
|
|
67
|
+
''' will reset container widgets'''
|
|
68
|
+
for w in self.datachildren:
|
|
69
|
+
w.reset(g)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass
|
|
73
|
+
class sheetwidget:
|
|
74
|
+
''' class defining a widget which updates from a sheet '''
|
|
75
|
+
df_var : any = pd.DataFrame() # definition
|
|
76
|
+
trans : any = lambda x : x # renaming of variables
|
|
77
|
+
transpose : bool = False # orientation of dataframe
|
|
78
|
+
expname : str = "Carbon tax rate, US$ per tonn "
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def __post_init__(self):
|
|
82
|
+
...
|
|
83
|
+
|
|
84
|
+
self.wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
newnamedf = self.df_var.copy().rename(columns=self.trans)
|
|
88
|
+
self.org_df_var = self.newnamedf.T if self.transpose else newnamedf
|
|
89
|
+
|
|
90
|
+
self.wsheet = sheet(from_dataframe(self.org_df_var),column_resizing=True)
|
|
91
|
+
self.org_values = [c.value for c in self.wsheet.cells]
|
|
92
|
+
self.datawidget=widgets.VBox([self.wexp,self.wsheet]) if len(self.expname) else self.wsheet
|
|
93
|
+
|
|
94
|
+
def update_df(self,df,current_per=None):
|
|
95
|
+
# breakpoint()
|
|
96
|
+
self.df_new_var = to_dataframe(self.wsheet).T if self.transpose else to_dataframe(self.wsheet)
|
|
97
|
+
self.df_new_var.columns = self.df_var.columns
|
|
98
|
+
self.df_new_var.index = self.df_var.index
|
|
99
|
+
|
|
100
|
+
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
|
|
101
|
+
pass
|
|
102
|
+
return df
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def reset(self,g):
|
|
106
|
+
for c,v in zip(self.wsheet.cells,self.org_values):
|
|
107
|
+
c.value = v
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@dataclass
|
|
112
|
+
class slidewidget:
|
|
113
|
+
''' class defefining a widget with lines of slides '''
|
|
114
|
+
slidedef : dict # definition
|
|
115
|
+
altname : str = 'Alternative'
|
|
116
|
+
basename : str = 'Baseline'
|
|
117
|
+
expname : str = "Carbon tax rate, US$ per tonn "
|
|
118
|
+
def __post_init__(self):
|
|
119
|
+
...
|
|
120
|
+
|
|
121
|
+
wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
122
|
+
walt = widgets.Label(value = self.altname,layout={'width':'8%', 'border':"hide"})
|
|
123
|
+
wbas = widgets.Label(value = self.basename,layout={'width':'10%', 'border':"hide"})
|
|
124
|
+
whead = widgets.HBox([wexp,walt,wbas])
|
|
125
|
+
#
|
|
126
|
+
self.wset = [widgets.FloatSlider(description=des,
|
|
127
|
+
min=cont['min'],
|
|
128
|
+
max=cont['max'],
|
|
129
|
+
value=cont['value'],
|
|
130
|
+
step=cont.get('step',0.01),
|
|
131
|
+
layout={'width':'60%'},style={'description_width':'40%'},
|
|
132
|
+
readout_format = f":>,.{cont.get('dec',2)}f",
|
|
133
|
+
continuous_update=False
|
|
134
|
+
)
|
|
135
|
+
for des,cont in self.slidedef.items()]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
for w in self.wset:
|
|
139
|
+
w.observe(self.set_slide_value,names='value',type='change')
|
|
140
|
+
|
|
141
|
+
waltval= [widgets.Label(
|
|
142
|
+
value=f"{cont['value']:>,.{cont.get('dec',2)}f}",
|
|
143
|
+
layout=widgets.Layout(display="flex", justify_content="center", width="10%", border="hide"))
|
|
144
|
+
for des,cont in self.slidedef.items()
|
|
145
|
+
|
|
146
|
+
]
|
|
147
|
+
self.wslide = [widgets.HBox([s,v]) for s,v in zip(self.wset,waltval)]
|
|
148
|
+
self.slidewidget = widgets.VBox([whead] + self.wslide)
|
|
149
|
+
self.datawidget = widgets.VBox([whead] + self.wslide)
|
|
150
|
+
|
|
151
|
+
# define the result object
|
|
152
|
+
self.current_values = {des:
|
|
153
|
+
{key : v.split() if key=='var' else v for key,v in cont.items() if key in {'value','var','op'}}
|
|
154
|
+
for des,cont in self.slidedef.items()}
|
|
155
|
+
|
|
156
|
+
def reset(self,g):
|
|
157
|
+
for i,(des,cont) in enumerate(self.slidedef.items()):
|
|
158
|
+
self.wset[i].value = cont['value']
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def update_df(self,df,current_per):
|
|
163
|
+
''' updates a dataframe with the values from the widget'''
|
|
164
|
+
for i,(des,cont) in enumerate(self.current_values.items()):
|
|
165
|
+
op = cont.get('op','=')
|
|
166
|
+
value = cont['value']
|
|
167
|
+
for var in cont['var']:
|
|
168
|
+
if op == '+':
|
|
169
|
+
df.loc[current_per,var] = df.loc[current_per,var] + value
|
|
170
|
+
elif op == '+impulse':
|
|
171
|
+
df.loc[current_per[0],var] = df.loc[current_per[0],var] + value
|
|
172
|
+
elif op == '=start-':
|
|
173
|
+
startindex = df.index.get_loc(current_per[0])
|
|
174
|
+
varloc = df.columns.get_loc(var)
|
|
175
|
+
df.iloc[:startindex,varloc] = value
|
|
176
|
+
elif op == '=':
|
|
177
|
+
df.loc[current_per,var] = value
|
|
178
|
+
elif op == '=impulse':
|
|
179
|
+
df.loc[current_per[0],var] = value
|
|
180
|
+
elif op == '%':
|
|
181
|
+
df.loc[current_per,var] = df.loc[current_per,var] * (1-value/100)
|
|
182
|
+
else:
|
|
183
|
+
print(f'Wrong operator in {cont}.\nNot updated')
|
|
184
|
+
assert 1==3,'wRONG OPERATOR'
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def set_slide_value(self,g):
|
|
188
|
+
''' updates the new values to the self.current_vlues
|
|
189
|
+
will be used in update_df
|
|
190
|
+
'''
|
|
191
|
+
line_des = g['owner'].description
|
|
192
|
+
if 0:
|
|
193
|
+
print()
|
|
194
|
+
for k,v in g.items():
|
|
195
|
+
print(f'{k}:{v}')
|
|
196
|
+
|
|
197
|
+
self.current_values[line_des]['value'] = g['new']
|
|
198
|
+
|
|
199
|
+
@dataclass
|
|
200
|
+
class sumslidewidget:
|
|
201
|
+
''' class defefining a widget with lines of slides '''
|
|
202
|
+
slidedef : dict # definition
|
|
203
|
+
maxsum : any = None
|
|
204
|
+
altname : str = 'Alternative'
|
|
205
|
+
basename : str = 'Baseline'
|
|
206
|
+
expname : str = "Carbon tax rate, US$ per tonn "
|
|
207
|
+
def __post_init__(self):
|
|
208
|
+
...
|
|
209
|
+
|
|
210
|
+
self.first = list(self.slidedef.keys())[:-1]
|
|
211
|
+
self.lastdes = list(self.slidedef.keys())[-1]
|
|
212
|
+
|
|
213
|
+
wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
214
|
+
walt = widgets.Label(value = self.altname,layout={'width':'8%', 'border':"hide"})
|
|
215
|
+
wbas = widgets.Label(value = self.basename,layout={'width':'10%', 'border':"hide"})
|
|
216
|
+
whead = widgets.HBox([wexp,walt,wbas])
|
|
217
|
+
#
|
|
218
|
+
self.wset = [widgets.FloatSlider(description=des,
|
|
219
|
+
min=cont['min'],
|
|
220
|
+
max=cont['max'],
|
|
221
|
+
value=cont['value'],
|
|
222
|
+
step=cont.get('step',0.01),
|
|
223
|
+
layout={'width':'60%'},style={'description_width':'40%'},
|
|
224
|
+
readout_format = f":>,.{cont.get('dec',2)}f",
|
|
225
|
+
continuous_update=False,
|
|
226
|
+
disabled= False
|
|
227
|
+
)
|
|
228
|
+
for des,cont in self.slidedef.items()]
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
for w in self.wset:
|
|
233
|
+
w.observe(self.set_slide_value,names='value',type='change')
|
|
234
|
+
|
|
235
|
+
waltval= [widgets.Label(
|
|
236
|
+
value=f"{cont['value']:>,.{cont.get('dec',2)}f}",
|
|
237
|
+
layout=widgets.Layout(display="flex", justify_content="center", width="10%", border="hide"))
|
|
238
|
+
for des,cont in self.slidedef.items()
|
|
239
|
+
|
|
240
|
+
]
|
|
241
|
+
self.wslide = [widgets.HBox([s,v]) for s,v in zip(self.wset,waltval)]
|
|
242
|
+
self.slidewidget = widgets.VBox([whead] + self.wslide)
|
|
243
|
+
self.datawidget = widgets.VBox([whead] + self.wslide)
|
|
244
|
+
|
|
245
|
+
# define the result object
|
|
246
|
+
self.current_values = {des:
|
|
247
|
+
{key : v.split() if key=='var' else v for key,v in cont.items() if key in {'value','var','op','min','max'}}
|
|
248
|
+
for des,cont in self.slidedef.items()}
|
|
249
|
+
|
|
250
|
+
def reset(self,g):
|
|
251
|
+
for i,(des,cont) in enumerate(self.slidedef.items()):
|
|
252
|
+
self.wset[i].value = cont['value']
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def update_df(self,df,current_per):
|
|
257
|
+
''' updates a dataframe with the values from the widget'''
|
|
258
|
+
for i,(des,cont) in enumerate(self.current_values.items()):
|
|
259
|
+
op = cont.get('op','=')
|
|
260
|
+
value = cont['value']
|
|
261
|
+
for var in cont['var']:
|
|
262
|
+
if op == '+':
|
|
263
|
+
df.loc[current_per,var] = df.loc[current_per,var] + value
|
|
264
|
+
elif op == '+impulse':
|
|
265
|
+
df.loc[current_per[0],var] = df.loc[current_per[0],var] + value
|
|
266
|
+
elif op == '=start-':
|
|
267
|
+
startindex = df.index.get_loc(current_per[0])
|
|
268
|
+
varloc = df.columns.get_loc(var)
|
|
269
|
+
df.iloc[:startindex,varloc] = value
|
|
270
|
+
elif op == '=':
|
|
271
|
+
df.loc[current_per,var] = value
|
|
272
|
+
elif op == '=impulse':
|
|
273
|
+
df.loc[current_per[0],var] = value
|
|
274
|
+
else:
|
|
275
|
+
print(f'Wrong operator in {cont}.\nNot updated')
|
|
276
|
+
assert 1==3,'wRONG OPERATOR'
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def set_slide_value(self,g):
|
|
280
|
+
''' updates the new values to the self.current_vlues
|
|
281
|
+
will be used in update_df
|
|
282
|
+
'''
|
|
283
|
+
line_des = g['owner'].description
|
|
284
|
+
line_index = list(self.current_values.keys()).index(line_des)
|
|
285
|
+
if 0:
|
|
286
|
+
print()
|
|
287
|
+
for k,v in g.items():
|
|
288
|
+
print(f'{k}:{v}')
|
|
289
|
+
|
|
290
|
+
self.current_values[line_des]['value'] = g['new']
|
|
291
|
+
# print(self.current_values)
|
|
292
|
+
if type(self.maxsum) == float:
|
|
293
|
+
allvalues = [v['value'] for v in self.current_values.values()]
|
|
294
|
+
thissum = sum(allvalues)
|
|
295
|
+
if thissum > self.maxsum:
|
|
296
|
+
# print(f'{allvalues=}')
|
|
297
|
+
# print(f"{self.current_values[self.lastdes]['min']=}")
|
|
298
|
+
newlast = self.maxsum-sum(allvalues[:-1])
|
|
299
|
+
newlast = max(newlast,self.current_values[self.lastdes]['min'])
|
|
300
|
+
# print(f'{newlast=}')
|
|
301
|
+
self.current_values[self.lastdes]['value']= newlast
|
|
302
|
+
|
|
303
|
+
newsum= sum([v['value'] for v in self.current_values.values()])
|
|
304
|
+
# print(f'{newsum=}')
|
|
305
|
+
# print(f'{line_index=}')
|
|
306
|
+
if newsum > self.maxsum:
|
|
307
|
+
self.current_values[line_des]['value']=self.wset[line_index].value-newsum +self.maxsum
|
|
308
|
+
self.wset[line_index].value = self.current_values[line_des]['value']
|
|
309
|
+
|
|
310
|
+
self.wset[-1].value = newlast
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
@dataclass
|
|
315
|
+
class updatewidget:
|
|
316
|
+
''' class to input and run a model'''
|
|
317
|
+
|
|
318
|
+
mmodel : any # a model
|
|
319
|
+
a_datawidget : any # a tab to update from
|
|
320
|
+
basename : str ='Business as usual'
|
|
321
|
+
keeppat : str = '*'
|
|
322
|
+
varpat : str ='*'
|
|
323
|
+
showvarpat : bool = True
|
|
324
|
+
exodif : any = pd.DataFrame() # definition
|
|
325
|
+
lwrun : bool = True
|
|
326
|
+
lwupdate : bool = False
|
|
327
|
+
lwreset : bool = True
|
|
328
|
+
lwsetbas : bool = True
|
|
329
|
+
lwshow :bool = True
|
|
330
|
+
outputwidget : str = 'jupviz'
|
|
331
|
+
prefix_dict : dict = field(default_factory=dict)
|
|
332
|
+
display_first :any = None
|
|
333
|
+
vline : list = field(default_factory=list)
|
|
334
|
+
relativ_start : int = 0
|
|
335
|
+
short :bool = False
|
|
336
|
+
legend :bool = False
|
|
337
|
+
|
|
338
|
+
def __post_init__(self):
|
|
339
|
+
self.baseline = self.mmodel.basedf.copy()
|
|
340
|
+
wrun = widgets.Button(description="Run scenario")
|
|
341
|
+
wrun.on_click(self.run)
|
|
342
|
+
|
|
343
|
+
wupdate = widgets.Button(description="Update the dataset ")
|
|
344
|
+
wupdate.on_click(self.update)
|
|
345
|
+
|
|
346
|
+
wreset = widgets.Button(description="Reset to start")
|
|
347
|
+
wreset.on_click(self.reset)
|
|
348
|
+
|
|
349
|
+
wshow = widgets.Button(description="Show results")
|
|
350
|
+
wshow.on_click(self.show)
|
|
351
|
+
|
|
352
|
+
wsetbas = widgets.Button(description="Use as baseline")
|
|
353
|
+
wsetbas.on_click(self.setbasis)
|
|
354
|
+
self.experiment = 0
|
|
355
|
+
|
|
356
|
+
lbut = []
|
|
357
|
+
|
|
358
|
+
if self.lwrun: lbut.append(wrun)
|
|
359
|
+
if self.lwshow: lbut.append(wshow)
|
|
360
|
+
if self.lwupdate: lbut.append(wupdate)
|
|
361
|
+
if self.lwreset: lbut.append(wreset)
|
|
362
|
+
if self.lwsetbas : lbut.append(wsetbas)
|
|
363
|
+
|
|
364
|
+
wbut = widgets.HBox(lbut)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
self.wname = widgets.Text(value=self.basename,placeholder='Type something',description='Scenario name:',
|
|
368
|
+
layout={'width':'30%'},style={'description_width':'50%'})
|
|
369
|
+
self.wpat = widgets.Text(value= self.varpat,placeholder='Type something',description='Display variables:',
|
|
370
|
+
layout={'width':'65%'},style={'description_width':'30%'})
|
|
371
|
+
|
|
372
|
+
self.wpat.layout.visibility = 'visible' if self.showvarpat else 'hidden'
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
winputstring = widgets.HBox([self.wname,self.wpat])
|
|
376
|
+
|
|
377
|
+
self.wtotal = widgets.VBox([self.a_datawidget.datawidget,winputstring,wbut])
|
|
378
|
+
|
|
379
|
+
self.mmodel.keep_solutions = {}
|
|
380
|
+
self.mmodel.keep_solutions = {self.wname.value : self.baseline}
|
|
381
|
+
self.mmodel.keep_exodif = {}
|
|
382
|
+
|
|
383
|
+
self.experiment += 1
|
|
384
|
+
self.wname.value = f'Experiment {self.experiment}'
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def update(self,g):
|
|
388
|
+
self.thisexperiment = self.baseline.copy()
|
|
389
|
+
self.a_datawidget.update_df(self.thisexperiment,self.mmodel.current_per)
|
|
390
|
+
self.exodif = self.mmodel.exodif(self.baseline,self.thisexperiment)
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def show(self,g=None):
|
|
394
|
+
if self.outputwidget == 'jupviz':
|
|
395
|
+
clear_output()
|
|
396
|
+
display(self.wtotal)
|
|
397
|
+
|
|
398
|
+
displaydict = {k :v.loc[self.mmodel.current_per,self.wpat.value.split()]
|
|
399
|
+
for k,v in self.mmodel.keep_solutions.items()}
|
|
400
|
+
jupviz(displaydict,legend=0)()
|
|
401
|
+
|
|
402
|
+
elif self.outputwidget == 'keep_viz':
|
|
403
|
+
|
|
404
|
+
selectfrom = [v for v in self.mmodel.vlist(self.wpat.value) if v in
|
|
405
|
+
set(list(self.mmodel.keep_solutions.values())[0].columns)]
|
|
406
|
+
clear_output()
|
|
407
|
+
display(self.wtotal)
|
|
408
|
+
plt.close('all')
|
|
409
|
+
_ = self.mmodel.keep_viz(pat=selectfrom[0],selectfrom=selectfrom,vline=self.vline)
|
|
410
|
+
|
|
411
|
+
elif self.outputwidget == 'keep_viz_prefix':
|
|
412
|
+
|
|
413
|
+
selectfrom = [v for v in self.mmodel.vlist(self.wpat.value) if v in
|
|
414
|
+
set(list(self.mmodel.keep_solutions.values())[0].columns)]
|
|
415
|
+
clear_output()
|
|
416
|
+
if self.display_first:
|
|
417
|
+
display(self.display_first)
|
|
418
|
+
display(self.wtotal)
|
|
419
|
+
plt.close('all')
|
|
420
|
+
with self.mmodel.set_smpl_relative(self.relativ_start,0):
|
|
421
|
+
_ = self.mmodel.keep_viz_prefix(pat=selectfrom[0],
|
|
422
|
+
selectfrom=selectfrom,prefix_dict=self.prefix_dict,vline=self.vline,short=self.short,legend=self.legend)
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def run(self,g):
|
|
426
|
+
clear_output(True)
|
|
427
|
+
display(self.wtotal)
|
|
428
|
+
self.update(g)
|
|
429
|
+
self.mmodel(self.thisexperiment,progressbar=1,keep = self.wname.value,
|
|
430
|
+
keep_variables = self.keeppat)
|
|
431
|
+
self.mmodel.keep_exodif[self.wname.value] = self.exodif
|
|
432
|
+
self.mmodel.inputwidget_alternativerun = True
|
|
433
|
+
self.current_experiment = self.wname.value
|
|
434
|
+
self.experiment += 1
|
|
435
|
+
self.wname.value = f'Experiment {self.experiment}'
|
|
436
|
+
self.show(g)
|
|
437
|
+
|
|
438
|
+
def setbasis(self,g):
|
|
439
|
+
clear_output(True)
|
|
440
|
+
display(self.wtotal)
|
|
441
|
+
self.mmodel.keep_solutions={self.current_experiment:self.mmodel.keep_solutions[self.current_experiment]}
|
|
442
|
+
|
|
443
|
+
self.mmodel.keep_exodif[self.current_experiment] = self.exodif
|
|
444
|
+
self.mmodel.inputwidget_alternativerun = True
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def reset(self,g):
|
|
450
|
+
self.a_datawidget.reset(g)
|
|
451
|
+
|
|
452
|
+
def fig_to_image(figs,format='svg'):
|
|
453
|
+
from io import StringIO
|
|
454
|
+
f = StringIO()
|
|
455
|
+
figs.savefig(f,format=format)
|
|
456
|
+
f.seek(0)
|
|
457
|
+
image= f.read()
|
|
458
|
+
return image
|
|
459
|
+
|
|
460
|
+
@dataclass
|
|
461
|
+
class htmlwidget_df:
|
|
462
|
+
''' class displays a dataframe in a html widget '''
|
|
463
|
+
|
|
464
|
+
mmodel : any # a model
|
|
465
|
+
df_var : any = pd.DataFrame() # definition
|
|
466
|
+
trans : any = lambda x : x # renaming of variables
|
|
467
|
+
transpose : bool = False # orientation of dataframe
|
|
468
|
+
expname : str = ""
|
|
469
|
+
percent : bool = False
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def __post_init__(self):
|
|
473
|
+
...
|
|
474
|
+
|
|
475
|
+
self.wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
newnamedf = self.df_var.copy().rename(columns=self.trans)
|
|
479
|
+
self.org_df_var = newnamedf.T if self.transpose else newnamedf
|
|
480
|
+
image = self.mmodel.ibsstyle(self.org_df_var,percent = self.percent).to_html()
|
|
481
|
+
self.whtml = widgets.HTML(image)
|
|
482
|
+
self.datawidget=widgets.VBox([self.wexp,self.whtml]) if len(self.expname) else self.whtml
|
|
483
|
+
|
|
484
|
+
@property
|
|
485
|
+
def show(self):
|
|
486
|
+
display(self.datawidget)
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
@dataclass
|
|
490
|
+
class htmlwidget_fig:
|
|
491
|
+
''' class displays a dataframe in a html widget '''
|
|
492
|
+
|
|
493
|
+
figs : any # a model
|
|
494
|
+
expname : str = ""
|
|
495
|
+
format : str = "svg"
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
def __post_init__(self):
|
|
499
|
+
...
|
|
500
|
+
|
|
501
|
+
self.wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
image = fig_to_image(self.figs,format=self.format)
|
|
505
|
+
self.whtml = widgets.HTML(image)
|
|
506
|
+
self.datawidget=widgets.VBox([self.wexp,self.whtml]) if len(self.expname) else self.whtml
|
|
507
|
+
|
|
508
|
+
@dataclass
|
|
509
|
+
class htmlwidget_label:
|
|
510
|
+
''' class displays a dataframe in a html widget '''
|
|
511
|
+
|
|
512
|
+
expname : str = ""
|
|
513
|
+
format : str = "svg"
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
def __post_init__(self):
|
|
517
|
+
...
|
|
518
|
+
|
|
519
|
+
self.wexp = widgets.Label(value = self.expname,layout={'width':'54%'})
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
self.datawidget=self.wexp
|
|
523
|
+
|
|
524
|
+
@dataclass
|
|
525
|
+
class visshow:
|
|
526
|
+
mmodel : any # a model
|
|
527
|
+
varpat : str ='*'
|
|
528
|
+
showvarpat : bool = True
|
|
529
|
+
show_on : bool = True # Display when called
|
|
530
|
+
|
|
531
|
+
def __post_init__(self):
|
|
532
|
+
...
|
|
533
|
+
from IPython import get_ipython
|
|
534
|
+
get_ipython().magic('matplotlib notebook')
|
|
535
|
+
# print(plt.get_backend())
|
|
536
|
+
this_vis = self.mmodel[self.varpat]
|
|
537
|
+
self.out_dict = {}
|
|
538
|
+
self.out_dict['Baseline'] ={'df':this_vis.base}
|
|
539
|
+
self.out_dict['Alternative'] ={'df':this_vis}
|
|
540
|
+
self.out_dict['Difference'] ={'df':this_vis.dif}
|
|
541
|
+
self.out_dict['Diff. pct. level'] ={'df':this_vis.difpctlevel.mul100,'percent':True}
|
|
542
|
+
self.out_dict['Base growth'] ={'df':this_vis.base.pct.mul100,'percent':True}
|
|
543
|
+
self.out_dict['Alt. growth'] ={'df':this_vis.pct.mul100,'percent':True}
|
|
544
|
+
self.out_dict['Diff. in growth'] ={'df':this_vis.difpct.mul100,'percent':True}
|
|
545
|
+
|
|
546
|
+
out = widgets.Output()
|
|
547
|
+
self.out_to_data = {key:
|
|
548
|
+
htmlwidget_df(self.mmodel,value['df'].df.T,expname=key,
|
|
549
|
+
|
|
550
|
+
percent=value.get('percent',False))
|
|
551
|
+
for key,value in self.out_dict.items()}
|
|
552
|
+
|
|
553
|
+
with out: # to suppress the display of matplotlib creation
|
|
554
|
+
self.out_to_figs ={key:
|
|
555
|
+
htmlwidget_fig(value['df'].rename().plot(top=0.9,title=''),expname='')
|
|
556
|
+
for key,value in self.out_dict.items() }
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
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()}
|
|
560
|
+
|
|
561
|
+
exodif = self.mmodel.exodif()
|
|
562
|
+
exonames = exodif.columns
|
|
563
|
+
exoindex = exodif.index
|
|
564
|
+
if len(exonames):
|
|
565
|
+
exobase = self.mmodel.basedf.loc[exoindex,exonames]
|
|
566
|
+
exolast = self.mmodel.lastdf.loc[exoindex,exonames]
|
|
567
|
+
|
|
568
|
+
out_exodif = htmlwidget_df(self.mmodel,exodif.T)
|
|
569
|
+
out_exobase = htmlwidget_df(self.mmodel,exobase.T)
|
|
570
|
+
out_exolast = htmlwidget_df(self.mmodel,exolast.T)
|
|
571
|
+
else:
|
|
572
|
+
out_exodif = htmlwidget_label(expname = 'No difference in exogenous')
|
|
573
|
+
out_exobase = htmlwidget_label(expname = 'No difference in exogenous')
|
|
574
|
+
out_exolast = htmlwidget_label(expname = 'No difference in exogenous')
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
out_tab_info = tabwidget({'Delta exogenous':out_exodif,
|
|
578
|
+
'Baseline exogenous':out_exobase,
|
|
579
|
+
'Alt. exogenous':out_exolast},selected_index=0)
|
|
580
|
+
|
|
581
|
+
tabnew['Exo info'] = out_tab_info
|
|
582
|
+
|
|
583
|
+
this = tabwidget(tabnew,selected_index=0)
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
self.datawidget = this.datawidget
|
|
588
|
+
if self.show_on:
|
|
589
|
+
...
|
|
590
|
+
display(self.datawidget)
|
|
591
|
+
get_ipython().magic('matplotlib inline')
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
def __repr__(self):
|
|
595
|
+
return ''
|
|
596
|
+
|
|
597
|
+
def _html_repr_(self):
|
|
598
|
+
return self.datawidget
|
|
599
|
+
|
|
600
|
+
@property
|
|
601
|
+
def show(self):
|
|
602
|
+
display(self.datawidget)
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
if __name__ == '__main__':
|
|
606
|
+
if 'masia' not in locals():
|
|
607
|
+
print('loading model')
|
|
608
|
+
masia,baseline = model.modelload('C:/wb Dawn/UNESCAP-/Asia/Asia.pcim',run=0,silent=1)
|
|
609
|
+
test = slidewidget(masia,{})
|
|
610
|
+
|