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
modeljupytermagic.py
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
'''
|
|
4
|
+
This module defines several magic jupyter functions\:
|
|
5
|
+
|
|
6
|
+
:graphviz: Draw Graphviz graph
|
|
7
|
+
:dataframe: Create Pandas Dataframe
|
|
8
|
+
:latexflow: Create a modelflow modelinstance from latex script
|
|
9
|
+
|
|
10
|
+
he doc strings can only be displayed when the module is used from Jupyter. So they are not shown in the Sphinx documentation To display the doc strings use the functions in jupyter.
|
|
11
|
+
|
|
12
|
+
'''
|
|
13
|
+
|
|
14
|
+
from IPython.display import display, Math, Latex, Markdown , Image, SVG, display_svg,IFrame
|
|
15
|
+
from IPython.lib.latextools import latex_to_png
|
|
16
|
+
from io import StringIO
|
|
17
|
+
import pandas as pd
|
|
18
|
+
from IPython.core.magic import register_line_magic, register_cell_magic
|
|
19
|
+
from subprocess import run
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
from model_latex import latextotxt
|
|
23
|
+
from modelclass import model
|
|
24
|
+
from modelmanipulation import explode
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_options(line,defaultname = 'test'):
|
|
28
|
+
'''
|
|
29
|
+
Retrives options from the first line
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
line (TYPE): DESCRIPTION.
|
|
34
|
+
defaultname (TYPE, optional): DESCRIPTION. Defaults to 'test'.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
name (string): name .
|
|
38
|
+
opt (dict): options.
|
|
39
|
+
|
|
40
|
+
'''
|
|
41
|
+
if line:
|
|
42
|
+
arglistfull = line.split()
|
|
43
|
+
name= arglistfull[0]
|
|
44
|
+
arglist=arglistfull[1:] if len(arglistfull)>= 1 else []
|
|
45
|
+
else:
|
|
46
|
+
name = defaultname
|
|
47
|
+
arglist=[]
|
|
48
|
+
|
|
49
|
+
opt = {o.split('=')[0]: o.split('=')[1] if len(o.split('='))==2 else True for o in arglist}
|
|
50
|
+
|
|
51
|
+
return name,opt
|
|
52
|
+
try:
|
|
53
|
+
# this is to
|
|
54
|
+
|
|
55
|
+
@register_cell_magic
|
|
56
|
+
def graphviz(line, cell):
|
|
57
|
+
'''Creates a ModelFlow model from a Latex document'''
|
|
58
|
+
name,options = get_options(line,'Testgraph')
|
|
59
|
+
gv = cell
|
|
60
|
+
# print(options)
|
|
61
|
+
model.display_graph(gv,name,**options)
|
|
62
|
+
return
|
|
63
|
+
|
|
64
|
+
# In[29]:
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@register_cell_magic
|
|
68
|
+
def latexflow(line, cell):
|
|
69
|
+
'''Creates a ModelFlow model from a Latex document'''
|
|
70
|
+
name,options = get_options(line)
|
|
71
|
+
|
|
72
|
+
lmodel = cell
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
display(Markdown(f'# Now creating the model **{name}**'))
|
|
76
|
+
|
|
77
|
+
fmodel = latextotxt(cell)
|
|
78
|
+
if options.get('debug',False):
|
|
79
|
+
display(Markdown('# Creating this Template model'))
|
|
80
|
+
print(fmodel)
|
|
81
|
+
mmodel = model.from_eq(fmodel)
|
|
82
|
+
mmodel.equations_latex = cell
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
globals()[f'{name}'] = mmodel
|
|
86
|
+
|
|
87
|
+
ia = get_ipython()
|
|
88
|
+
ia.push(f'{name}',interactive=True)
|
|
89
|
+
|
|
90
|
+
display(Markdown('## The model'))
|
|
91
|
+
display(Markdown(cell))
|
|
92
|
+
if options.get('display',False):
|
|
93
|
+
display(Markdown('# Creating this Template model'))
|
|
94
|
+
print(mmodel.equations_original)
|
|
95
|
+
display(Markdown('# And this Business Logic Language model'))
|
|
96
|
+
print(mmodel.equations)
|
|
97
|
+
|
|
98
|
+
return
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def ibmelt(df,prefix='',per=3):
|
|
102
|
+
# breakpoint()
|
|
103
|
+
temp= df.reset_index().rename(columns={'index':'row'}).melt(id_vars='row',var_name='column').assign(var_name=lambda x: prefix+x.row+'_'+x.column) .loc[:,['value','var_name']].set_index('var_name')
|
|
104
|
+
newdf = pd.concat([temp]*per,axis=1).T
|
|
105
|
+
newdf.index = range(per)
|
|
106
|
+
newdf.index.name = 'Year'
|
|
107
|
+
return newdf
|
|
108
|
+
|
|
109
|
+
@register_cell_magic
|
|
110
|
+
def dataframe(line, cell):
|
|
111
|
+
'''Converts this cell to a dataframe. and create a melted dataframe
|
|
112
|
+
|
|
113
|
+
options can be added to the inpput line\:
|
|
114
|
+
|
|
115
|
+
- t transposes the dataframe
|
|
116
|
+
- periods=<number> repeats the melted datframe
|
|
117
|
+
- repeat = <number> repeats the original dataframe - for parameters
|
|
118
|
+
- melt will create a melted dataframe
|
|
119
|
+
- prefix=<a string> prefix columns in the melted dataframe
|
|
120
|
+
- show will show the resulting dataframes'''
|
|
121
|
+
|
|
122
|
+
name,options = get_options(line,'Testgraph')
|
|
123
|
+
|
|
124
|
+
trans = options.get('t',False )
|
|
125
|
+
|
|
126
|
+
prefix = options.get('prefix','')
|
|
127
|
+
periods = int(options.get('periods','1'))
|
|
128
|
+
melt = options.get('melt',False)
|
|
129
|
+
start = int(options.get('start','2021'))
|
|
130
|
+
silent = options.get('silent',True)
|
|
131
|
+
ia = get_ipython()
|
|
132
|
+
|
|
133
|
+
xtrans = (lambda xx:xx.T) if trans else (lambda xx:xx)
|
|
134
|
+
xcell= cell.replace('%','').replace(',','.')
|
|
135
|
+
mul = 0.01 if '%' in cell else 1.
|
|
136
|
+
sio = StringIO(xcell)
|
|
137
|
+
|
|
138
|
+
df = pd.read_csv(sio,sep=r"\s+|\t+|\s+\t+|\t+\s+",engine='python')\
|
|
139
|
+
.pipe(xtrans)\
|
|
140
|
+
.pipe(lambda xx:xx.rename(index = {i:i.upper() if type(i) == str else i for i in xx.index}
|
|
141
|
+
,columns={c:c.upper() for c in xx.columns})) *mul
|
|
142
|
+
# breakpoint()
|
|
143
|
+
if not melt:
|
|
144
|
+
df= pd.concat([df]*periods,axis=0)
|
|
145
|
+
df.index = pd.period_range(start=start,freq = 'Y',periods=len(df))
|
|
146
|
+
df.index.name = 'index'
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
globals()[f'{name}'] = df
|
|
150
|
+
ia.push(f'{name}',interactive=True)
|
|
151
|
+
if melt:
|
|
152
|
+
df_melted = ibmelt(df,prefix=prefix.upper(),per=periods)
|
|
153
|
+
df_melted.index = pd.period_range(start=start,freq = 'Y',periods=len(df_melted))
|
|
154
|
+
df_melted.index.name = 'index'
|
|
155
|
+
|
|
156
|
+
globals()[f'{name}_melted'] = df_melted
|
|
157
|
+
ia.push(f'{name}_melted',interactive=True)
|
|
158
|
+
if not silent:
|
|
159
|
+
melttext = f' and {name}_melted' if melt else ''
|
|
160
|
+
display(Markdown(f'## Created the dataframes: {name}{melttext}'))
|
|
161
|
+
if options.get('show',False):
|
|
162
|
+
display(df)
|
|
163
|
+
if melt: display(df_melted)
|
|
164
|
+
return
|
|
165
|
+
|
|
166
|
+
@register_cell_magic
|
|
167
|
+
def modeleviews(line, cell):
|
|
168
|
+
'''Creates a ModelFlow model from a Latex document
|
|
169
|
+
|
|
170
|
+
In developement
|
|
171
|
+
'''
|
|
172
|
+
|
|
173
|
+
try:
|
|
174
|
+
import pyeviews as evp
|
|
175
|
+
except:
|
|
176
|
+
print('no pyeviews')
|
|
177
|
+
raise Exception('No pyeviews ')
|
|
178
|
+
from pathlib import Path, PureWindowsPath
|
|
179
|
+
|
|
180
|
+
name,options = get_options(line)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
display(Markdown(f'# Now creating the model **{name}**'))
|
|
184
|
+
|
|
185
|
+
commands = cell.split('\n')
|
|
186
|
+
print(commands)
|
|
187
|
+
if options.get('debug',False):
|
|
188
|
+
display(Markdown('# executing these eviews commands'))
|
|
189
|
+
print(cell)
|
|
190
|
+
|
|
191
|
+
eviewsapp = evp.GetEViewsApp(instance='new',showwindow=True)
|
|
192
|
+
for c in commands:
|
|
193
|
+
print(c)
|
|
194
|
+
evp.Run(c,eviewsapp)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
evp.Cleanup()
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
return
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def ibmelt(df,prefix='',per=3):
|
|
204
|
+
# breakpoint()
|
|
205
|
+
temp= df.reset_index().rename(columns={'index':'row'}).melt(id_vars='row',var_name='column').assign(var_name=lambda x: prefix+x.row+'_'+x.column) .loc[:,['value','var_name']].set_index('var_name')
|
|
206
|
+
newdf = pd.concat([temp]*per,axis=1).T
|
|
207
|
+
newdf.index = range(per)
|
|
208
|
+
newdf.index.name = 'Year'
|
|
209
|
+
return newdf
|
|
210
|
+
|
|
211
|
+
@register_cell_magic
|
|
212
|
+
def dataframe(line, cell):
|
|
213
|
+
'''Converts this cell to a dataframe. and create a melted dataframe
|
|
214
|
+
|
|
215
|
+
options can be added to the inpput line
|
|
216
|
+
- t transposes the dataframe
|
|
217
|
+
- periods=<number> repeats the melted datframe
|
|
218
|
+
- repeat = <number> repeats the original dataframe - for parameters
|
|
219
|
+
- melt will create a melted dataframe
|
|
220
|
+
- prefix=<a string> prefix columns in the melted dataframe
|
|
221
|
+
- show will show the resulting dataframes'''
|
|
222
|
+
|
|
223
|
+
name,options = get_options(line,'Testgraph')
|
|
224
|
+
|
|
225
|
+
trans = options.get('t',False )
|
|
226
|
+
|
|
227
|
+
prefix = options.get('prefix','')
|
|
228
|
+
periods = int(options.get('periods','1'))
|
|
229
|
+
melt = options.get('melt',False)
|
|
230
|
+
start = int(options.get('start','2021'))
|
|
231
|
+
silent = options.get('silent',True)
|
|
232
|
+
ia = get_ipython()
|
|
233
|
+
|
|
234
|
+
xtrans = (lambda xx:xx.T) if trans else (lambda xx:xx)
|
|
235
|
+
xcell= cell.replace('%','').replace(',','.')
|
|
236
|
+
mul = 0.01 if '%' in cell else 1.
|
|
237
|
+
sio = StringIO(xcell)
|
|
238
|
+
|
|
239
|
+
df = pd.read_csv(sio,sep=r"\s+|\t+|\s+\t+|\t+\s+",engine='python')\
|
|
240
|
+
.pipe(xtrans)\
|
|
241
|
+
.pipe(lambda xx:xx.rename(index = {i:i.upper() if type(i) == str else i for i in xx.index}
|
|
242
|
+
,columns={c:c.upper() for c in xx.columns})) *mul
|
|
243
|
+
# breakpoint()
|
|
244
|
+
if not melt:
|
|
245
|
+
df= pd.concat([df]*periods,axis=0)
|
|
246
|
+
df.index = pd.period_range(start=start,freq = 'Y',periods=len(df))
|
|
247
|
+
df.index.name = 'index'
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
globals()[f'{name}'] = df
|
|
251
|
+
ia.push(f'{name}',interactive=True)
|
|
252
|
+
if melt:
|
|
253
|
+
df_melted = ibmelt(df,prefix=prefix.upper(),per=periods)
|
|
254
|
+
df_melted.index = pd.period_range(start=start,freq = 'Y',periods=len(df_melted))
|
|
255
|
+
df_melted.index.name = 'index'
|
|
256
|
+
|
|
257
|
+
globals()[f'{name}_melted'] = df_melted
|
|
258
|
+
ia.push(f'{name}_melted',interactive=True)
|
|
259
|
+
if not silent:
|
|
260
|
+
melttext = f' and {name}_melted' if melt else ''
|
|
261
|
+
display(Markdown(f'## Created the dataframes: {name}{melttext}'))
|
|
262
|
+
if options.get('show',False):
|
|
263
|
+
display(df)
|
|
264
|
+
if melt: display(df_melted)
|
|
265
|
+
return
|
|
266
|
+
except:
|
|
267
|
+
print('no magic')
|
modelmacrograb.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Created on Sun Jun 19 14:43:37 2022
|
|
4
|
+
|
|
5
|
+
grab a world bank macromodel using modelnormalize and not modelmanipulation
|
|
6
|
+
|
|
7
|
+
@author: ibhan
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import pandas as pd
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
from modelclass import model
|
|
15
|
+
import modelnormalize as nz
|
|
16
|
+
import modelmanipulation as mp
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class GrabMacroModel():
|
|
20
|
+
'''This class takes a world bank model specification, variable data and variable description
|
|
21
|
+
and transform it to ModelFlow business language'''
|
|
22
|
+
|
|
23
|
+
inputfrml : any = '' #wf1 name
|
|
24
|
+
modelname : str ='macromodel'
|
|
25
|
+
make_fitted : bool = True
|
|
26
|
+
add_add_factor : bool = True
|
|
27
|
+
debug : bool = True
|
|
28
|
+
def __post_init__(self):
|
|
29
|
+
eq = [l.upper() for l in self.inputfrml.split('\n') if len(l.strip())]
|
|
30
|
+
eeq = [('<>' if len(tup := e.split('>')) == 1 else f'{tup[0]}>' ,tup[-1])
|
|
31
|
+
for e in eq ]
|
|
32
|
+
|
|
33
|
+
self.all_frml = [(frmlname,
|
|
34
|
+
nz.normal(
|
|
35
|
+
eq,
|
|
36
|
+
make_fixable = (fixit :='EXO' in frmlname or 'FIXABLE' in frmlname),
|
|
37
|
+
add_add_factor = fixit and self.add_add_factor,
|
|
38
|
+
make_fitted = self.make_fitted and fixit
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
for frmlname,eq in eeq]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if self.debug:
|
|
45
|
+
for frmlname,frml in self.all_frml:
|
|
46
|
+
print(f'\nFrml name:{frmlname}\n{frml}')
|
|
47
|
+
|
|
48
|
+
# create the main model:
|
|
49
|
+
fmainmodel_tomodel = '\n'.join( [f'FRML {frmlname} {frml.normalized}$' for frmlname,frml in self.all_frml])
|
|
50
|
+
# now create the fitted model
|
|
51
|
+
if self.make_fitted:
|
|
52
|
+
ffitmodel_tomodel ='\n'.join( [f'FRML <FIT> {frml.fitted}$'
|
|
53
|
+
for frmlname,frml in self.all_frml if len(frml.fitted)])
|
|
54
|
+
self.mfitmodel = model(ffitmodel_tomodel,modelname = self.modelname + ' calc fittet values')
|
|
55
|
+
else:
|
|
56
|
+
self.mfitmodel = None
|
|
57
|
+
ffitmodel_tomodel = ''
|
|
58
|
+
|
|
59
|
+
# now create the add factor model
|
|
60
|
+
# one for inclusion in the main model, and one for solo use in to create data
|
|
61
|
+
if self.add_add_factor:
|
|
62
|
+
fcalc_add_factor_tomodel_solo = '\n'.join([f'{frml.calc_add_factor}' for frmlname,frml in self.all_frml if frml.calc_add_factor])
|
|
63
|
+
self.mcalc_add_factor = model(fcalc_add_factor_tomodel_solo,modelname = f'Calculation of add factors for {self.modelname}')
|
|
64
|
+
|
|
65
|
+
fcalc_add_factor_tomodel ='\n'.join([f'FRML <CALC_ADD_FACTOR> {frml.calc_add_factor}$' for frmlname,frml in self.all_frml if frml.calc_add_factor])
|
|
66
|
+
else:
|
|
67
|
+
fcalc_add_factor_tomodel =''
|
|
68
|
+
self.mcalc_add_factor = None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
self.fmodel = f'{fmainmodel_tomodel}\n{ffitmodel_tomodel}\n{fcalc_add_factor_tomodel}'
|
|
73
|
+
self.mmodel = model(self.fmodel,modelname =self.modelname)
|
|
74
|
+
# self.mmodel.set_var_description(self.model_all_about['var_description'])
|
|
75
|
+
# self.mmodel.set_var_description(self.var_description)
|
|
76
|
+
# breakpoint()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
if __name__ == '__main__':
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
smallmodel = '''
|
|
85
|
+
<fixable z> dlog(cpv)= -c2_cpv*(log(cpv(-1))-log(y(-1)) - c1_cpv)+c3_cpv*dlog(y)
|
|
86
|
+
|
|
87
|
+
<fixable z> dlog(g) = -c2_g* (log(g(-1))- log(y(-1)) - c1_g) +c3_g*dlog(y)
|
|
88
|
+
|
|
89
|
+
<fixable z> dlog(i) = -c2_i* (log(i(-1))- log(y(-1)-g(-1)) - c1_i) +c3_i*dlog(y)
|
|
90
|
+
|
|
91
|
+
<fixable z> dlog(m) = -c2_m* (log(m(-1))- log(gde(-1)) - c1_m) +c3_m*dlog(gde)
|
|
92
|
+
|
|
93
|
+
<ident> y=cpv+i+g+x-m+ydisc
|
|
94
|
+
|
|
95
|
+
<ident> gde=cpv+i+g
|
|
96
|
+
|
|
97
|
+
'''
|
|
98
|
+
cmodel = GrabMacroModel(smallmodel)
|