ModelFlowIb 2.88__tar.gz → 2.90__tar.gz
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-2.88 → modelflowib-2.90}/PKG-INFO +1 -1
- {modelflowib-2.88 → modelflowib-2.90}/model_Excel.py +12 -12
- {modelflowib-2.88 → modelflowib-2.90}/model_parquet_mixin.py +9 -4
- {modelflowib-2.88 → modelflowib-2.90}/modelclass.py +57 -31
- {modelflowib-2.88 → modelflowib-2.90}/modelconstruct_estimation.py +212 -8
- {modelflowib-2.88 → modelflowib-2.90}/modelflowib.egg-info/PKG-INFO +1 -1
- {modelflowib-2.88 → modelflowib-2.90}/modelgrabexcel.py +0 -4
- {modelflowib-2.88 → modelflowib-2.90}/modelgrabgekkobank.py +178 -178
- {modelflowib-2.88 → modelflowib-2.90}/modelmanipulation.py +8 -3
- {modelflowib-2.88 → modelflowib-2.90}/modelpattern.py +0 -24
- {modelflowib-2.88 → modelflowib-2.90}/modelsolver_ng.py +241 -168
- {modelflowib-2.88 → modelflowib-2.90}/pyproject.toml +1 -1
- {modelflowib-2.88 → modelflowib-2.90}/README.md +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/license.md +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelBLfunk.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/model_cvx.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/model_dynare.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/model_financial_stability.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/model_latex.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/model_latex_class.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelcausality.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelconstruct.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modeldash.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modeldashboot.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modeldashsidebar.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modeldekom.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modeldiff.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modeldisplay.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelestimation.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelestimator_new.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelflowib.egg-info/SOURCES.txt +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelflowib.egg-info/dependency_links.txt +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelflowib.egg-info/requires.txt +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelflowib.egg-info/top_level.txt +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelgrab.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelgrabgdx.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelgrabwf2.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelhelp.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelhtml.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelinput_shiny.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelinvert.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modeljupyter.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modeljupytermagic.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelmacrograb.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelmf.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelnet.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelnewton.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelnormalize.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelreport.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modeluserfunk.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelvis.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelwidget.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelwidget_core.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/modelwidget_input.py +0 -0
- {modelflowib-2.88 → modelflowib-2.90}/setup.cfg +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""
|
|
3
|
-
Renamed: use modelgrabexcel instead of model_Excel.
|
|
4
|
-
|
|
5
|
-
This stub only re-exports modelgrabexcel so old code keeps working.
|
|
6
|
-
"""
|
|
7
|
-
import warnings
|
|
8
|
-
|
|
9
|
-
warnings.warn('model_Excel is renamed to modelgrabexcel, use: import modelgrabexcel',
|
|
10
|
-
DeprecationWarning, stacklevel=2)
|
|
11
|
-
|
|
12
|
-
from modelgrabexcel import *
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Renamed: use modelgrabexcel instead of model_Excel.
|
|
4
|
+
|
|
5
|
+
This stub only re-exports modelgrabexcel so old code keeps working.
|
|
6
|
+
"""
|
|
7
|
+
import warnings
|
|
8
|
+
|
|
9
|
+
warnings.warn('model_Excel is renamed to modelgrabexcel, use: import modelgrabexcel',
|
|
10
|
+
DeprecationWarning, stacklevel=2)
|
|
11
|
+
|
|
12
|
+
from modelgrabexcel import *
|
|
@@ -217,6 +217,7 @@ class Parquet_Mixin:
|
|
|
217
217
|
'frml': self.equations,
|
|
218
218
|
'modelname': self.name,
|
|
219
219
|
'oldkwargs': self.oldkwargs,
|
|
220
|
+
'run_on_load': self.__dict__.get('run_on_load', False),
|
|
220
221
|
'var_description': self.var_description,
|
|
221
222
|
'equations_latex': self.equations_latex,
|
|
222
223
|
'wb_MFMSAOPTIONS': (
|
|
@@ -270,7 +271,7 @@ class Parquet_Mixin:
|
|
|
270
271
|
cls,
|
|
271
272
|
infile,
|
|
272
273
|
funks=[],
|
|
273
|
-
run=
|
|
274
|
+
run=None,
|
|
274
275
|
keep_json=False,
|
|
275
276
|
default_url=(
|
|
276
277
|
r'https://raw.githubusercontent.com/IbHansen/'
|
|
@@ -286,8 +287,9 @@ class Parquet_Mixin:
|
|
|
286
287
|
File name or URL.
|
|
287
288
|
funks : list
|
|
288
289
|
User-supplied functions for the model.
|
|
289
|
-
run : bool
|
|
290
|
-
If True, simulate after loading.
|
|
290
|
+
run : bool or None
|
|
291
|
+
If True, simulate after loading. None (default) simulates if the
|
|
292
|
+
model was dumped with ``model.run_on_load = True``.
|
|
291
293
|
keep_json : bool
|
|
292
294
|
If True, stash the raw metadata dict on the model instance.
|
|
293
295
|
**kwargs
|
|
@@ -326,7 +328,7 @@ class Parquet_Mixin:
|
|
|
326
328
|
# ── internal: load from feather/zip ──────────────────────────────────
|
|
327
329
|
|
|
328
330
|
@classmethod
|
|
329
|
-
def _load_feather_zip(cls, pinfile, funks=[], run=
|
|
331
|
+
def _load_feather_zip(cls, pinfile, funks=[], run=None,
|
|
330
332
|
keep_json=False, **kwargs):
|
|
331
333
|
"""Read a .pcimz archive and reconstruct the model.
|
|
332
334
|
|
|
@@ -383,6 +385,9 @@ class Parquet_Mixin:
|
|
|
383
385
|
modelname = meta['modelname']
|
|
384
386
|
mmodel = cls(frml, modelname=modelname, funks=funks, **kwargs)
|
|
385
387
|
mmodel.oldkwargs = meta.get('oldkwargs', {})
|
|
388
|
+
mmodel.run_on_load = meta.get('run_on_load', False)
|
|
389
|
+
if run is None:
|
|
390
|
+
run = mmodel.run_on_load
|
|
386
391
|
mmodel.json_current_per = current_per
|
|
387
392
|
mmodel.set_var_description(meta.get('var_description', {}))
|
|
388
393
|
mmodel.equations_latex = meta.get('equations_latex', '')
|
|
@@ -154,9 +154,14 @@ class BaseModel():
|
|
|
154
154
|
var_description={}, model_description = '',
|
|
155
155
|
var_groups = {}, reports = {}, equations_latex='',
|
|
156
156
|
eviews_dict = {},use_fbmin= True,
|
|
157
|
-
substitution = {},
|
|
157
|
+
substitution = {}, ng = False,
|
|
158
158
|
**kwargs):
|
|
159
|
-
''' initialize a model
|
|
159
|
+
''' initialize a model
|
|
160
|
+
|
|
161
|
+
ng : if True a call without solver= dispatches to the next-generation
|
|
162
|
+
solvers (xgenr_ng, sim_ng, newton_ng, newtonstack_ng) instead of
|
|
163
|
+
the legacy ones; can also be set later with ``mmodel.ng = True``'''
|
|
164
|
+
self.ng = ng
|
|
160
165
|
if i_eq != '':
|
|
161
166
|
self.funks = funks
|
|
162
167
|
self.equations = i_eq if '$' in i_eq else mp.tofrml(i_eq, sep='\n')
|
|
@@ -225,14 +230,16 @@ class BaseModel():
|
|
|
225
230
|
mmodel.equations_original = equations
|
|
226
231
|
return mmodel
|
|
227
232
|
|
|
228
|
-
def get_histmodel(self):
|
|
229
|
-
""" return a model instance with a model which generates historic values for equations
|
|
233
|
+
def get_histmodel(self, tags=('IDENT', 'I')):
|
|
234
|
+
""" return a model instance with a model which generates historic values for equations
|
|
230
235
|
marked by a frml name I or IDENT
|
|
231
|
-
|
|
236
|
+
|
|
237
|
+
tags is a string or a list of strings, the frml names to select (default IDENT and I)
|
|
238
|
+
|
|
232
239
|
Uses :any:`find_hist_model`
|
|
233
|
-
|
|
240
|
+
|
|
234
241
|
"""
|
|
235
|
-
hist_eq = mp.find_hist_model(self.equations)
|
|
242
|
+
hist_eq = mp.find_hist_model(self.equations, tags=tags)
|
|
236
243
|
return type(self)(hist_eq, funks=self.funks)
|
|
237
244
|
|
|
238
245
|
def analyzemodelnew(self, silent):
|
|
@@ -282,7 +289,8 @@ class BaseModel():
|
|
|
282
289
|
mega = [(f,nt) for f,nt in mega_all if not pt.kw_frml_name(f.frmlname, 'CALC_ADD_FACTOR')]
|
|
283
290
|
mega_calc_add_factor = [(f,nt) for f,nt in mega_all if pt.kw_frml_name(f.frmlname, 'CALC_ADD_FACTOR')]
|
|
284
291
|
calc_add_factor_frml = [f'FRML <CALC> {f.expression}' for f,nt in mega_calc_add_factor]
|
|
285
|
-
self.calc_add_factor_model = model(' '.join(calc_add_factor_frml),funks=self.funks,modelname='Calculate add factors'
|
|
292
|
+
self.calc_add_factor_model = model(' '.join(calc_add_factor_frml),funks=self.funks,modelname='Calculate add factors',
|
|
293
|
+
ng=self.ng)
|
|
286
294
|
if not self.calc_add_factor_model.istopo:
|
|
287
295
|
raise Exception('The add factor calculation model should be recursive')
|
|
288
296
|
else:
|
|
@@ -631,7 +639,7 @@ class BaseModel():
|
|
|
631
639
|
def fixed_period_guard(self, what):
|
|
632
640
|
''' Only the ng solvers can evaluate a variable at a fixed period, X(2002).
|
|
633
641
|
Raises an exception naming *what* (the legacy evaluator) if the model has such references '''
|
|
634
|
-
if
|
|
642
|
+
if self.fixed_periods:
|
|
635
643
|
refs = ', '.join(f'{var}({lag})' for var, lag in self.fixed_terms[:6])
|
|
636
644
|
more = ' ...' if len(self.fixed_terms) > 6 else ''
|
|
637
645
|
raise Exception(f'{self.name} refers to variables at fixed periods: {refs}{more}\n'
|
|
@@ -2636,7 +2644,7 @@ class Dekomp_Mixin():
|
|
|
2636
2644
|
end_ = end if end != '' else self.current_per[-1]
|
|
2637
2645
|
|
|
2638
2646
|
mfrml = model(self.allvar[varnavn]['frml'],
|
|
2639
|
-
funks=self.funks) # calculate the formular
|
|
2647
|
+
funks=self.funks, ng=self.ng) # calculate the formular
|
|
2640
2648
|
print_per = mfrml.smpl(start_, end_, altdf_)
|
|
2641
2649
|
|
|
2642
2650
|
old_start, old_end = altdf_.index.slice_locs(
|
|
@@ -2677,8 +2685,9 @@ class Dekomp_Mixin():
|
|
|
2677
2685
|
difdf = {e: smallalt - alldf[e] for e in eksperiments}
|
|
2678
2686
|
# allres = {e : mfrml.xgenr(alldf[e],str(e[1]),str(e[1]),silent= True ) for e in eksperiments} # now evaluate each experiment
|
|
2679
2687
|
# now evaluate each experiment
|
|
2680
|
-
# a variable at a fixed period (X(2002))
|
|
2681
|
-
|
|
2688
|
+
# the ng xgenr with the ng option; a variable at a fixed period (X(2002))
|
|
2689
|
+
# is only evaluated by the ng xgenr
|
|
2690
|
+
xgenr = mfrml.xgenr_ng if mfrml.ng or mfrml.fixed_periods else mfrml.xgenr
|
|
2682
2691
|
allres = {e: xgenr(
|
|
2683
2692
|
alldf[e], e[1], e[1], silent=True) for e in eksperiments}
|
|
2684
2693
|
# dataframes with the effect of each update
|
|
@@ -3743,8 +3752,9 @@ class Graph_Mixin():
|
|
|
3743
3752
|
self.FVS_graph.add_edge(u, v)
|
|
3744
3753
|
|
|
3745
3754
|
h,a = nx.hits(self.FVS_graph)
|
|
3746
|
-
|
|
3747
|
-
self.
|
|
3755
|
+
# rounded and ties broken by name, so the order is the same in every session
|
|
3756
|
+
self.fbhubs = {k:v for k,v in sorted( h.items(), key=lambda x: (-round(x[1], 12), x[0]))}
|
|
3757
|
+
self.fbaut = {k:v for k,v in sorted( a.items(), key=lambda x: (-round(x[1], 12), x[0]))}
|
|
3748
3758
|
|
|
3749
3759
|
self.fblist = [v for v in self.fbhubs.keys()]
|
|
3750
3760
|
except Exception as e:
|
|
@@ -3980,21 +3990,27 @@ class Graph_Mixin():
|
|
|
3980
3990
|
is acyclic.
|
|
3981
3991
|
"""
|
|
3982
3992
|
|
|
3983
|
-
#
|
|
3984
|
-
|
|
3993
|
+
# Build a copy with sorted nodes and edges. The graphs are made from sets of
|
|
3994
|
+
# variable names, whose order changes between Python sessions (hash
|
|
3995
|
+
# randomization), and find_cycle follows the insertion order - so without
|
|
3996
|
+
# this the feedback set, and with it the last digits of a solution, could
|
|
3997
|
+
# differ from session to session.
|
|
3998
|
+
H = nx.DiGraph()
|
|
3999
|
+
H.add_nodes_from(sorted(G.nodes()))
|
|
4000
|
+
H.add_edges_from(sorted(G.edges()))
|
|
3985
4001
|
fvs = set()
|
|
3986
|
-
|
|
4002
|
+
|
|
3987
4003
|
# Function to calculate the degree of a vertex in the cycle
|
|
3988
4004
|
def vertex_degree(vertex):
|
|
3989
4005
|
return H.degree(vertex)
|
|
3990
|
-
|
|
4006
|
+
|
|
3991
4007
|
# Iterate until the graph becomes acyclic
|
|
3992
4008
|
while True:
|
|
3993
4009
|
try:
|
|
3994
4010
|
# Find a cycle in the graph
|
|
3995
4011
|
cycle = nx.find_cycle(H, orientation='original')
|
|
3996
|
-
# Find the vertex in the cycle with the highest degree
|
|
3997
|
-
vertex_to_remove = max(cycle, key=lambda x: vertex_degree(x[0]))[0]
|
|
4012
|
+
# Find the vertex in the cycle with the highest degree, ties broken by name
|
|
4013
|
+
vertex_to_remove = max(cycle, key=lambda x: (vertex_degree(x[0]), x[0]))[0]
|
|
3998
4014
|
# Add the vertex to the feedback vertex set
|
|
3999
4015
|
fvs.add(vertex_to_remove)
|
|
4000
4016
|
# Remove the vertex from the graph
|
|
@@ -4003,10 +4019,10 @@ class Graph_Mixin():
|
|
|
4003
4019
|
# If no cycle is found, the graph is now acyclic, and we break the loop
|
|
4004
4020
|
break
|
|
4005
4021
|
|
|
4006
|
-
# Perform a topological sort on the modified graph
|
|
4007
|
-
topological_sorted_order = list(nx.
|
|
4008
|
-
|
|
4009
|
-
return
|
|
4022
|
+
# Perform a topological sort on the modified graph, ties broken by name
|
|
4023
|
+
topological_sorted_order = list(nx.lexicographical_topological_sort(H))
|
|
4024
|
+
|
|
4025
|
+
return sorted(fvs) , topological_sorted_order
|
|
4010
4026
|
|
|
4011
4027
|
|
|
4012
4028
|
|
|
@@ -6908,6 +6924,7 @@ class Json_Mixin():
|
|
|
6908
6924
|
'current_per': current_per_json ,
|
|
6909
6925
|
'modelname': self.name,
|
|
6910
6926
|
'oldkwargs': self.__dict__.get('oldkwargs',{}),
|
|
6927
|
+
'run_on_load': self.__dict__.get('run_on_load', False),
|
|
6911
6928
|
'var_description': self.var_description,
|
|
6912
6929
|
'equations_latex': self.equations_latex ,
|
|
6913
6930
|
'keep_solutions': {k:v.to_json(double_precision=15) for k,v in self.keep_solutions.items()} if (keep and dump_data) else {},
|
|
@@ -6931,16 +6948,17 @@ class Json_Mixin():
|
|
|
6931
6948
|
return json.dumps(dumpjson)
|
|
6932
6949
|
|
|
6933
6950
|
@classmethod
|
|
6934
|
-
def modelload(cls, infile, funks=[], run=
|
|
6951
|
+
def modelload(cls, infile, funks=[], run=None, keep_json=False,
|
|
6935
6952
|
default_url=r'https://raw.githubusercontent.com/IbHansen/modelflow-manual/main/model_repo/',**kwargs):
|
|
6936
6953
|
'''
|
|
6937
|
-
|
|
6954
|
+
|
|
6938
6955
|
|
|
6939
6956
|
Parameters
|
|
6940
6957
|
----------
|
|
6941
6958
|
infile : A file name or an url with a .pcim file
|
|
6942
6959
|
funks : Functions to use in the resulting model
|
|
6943
|
-
run : simulate the model with the saved time and options
|
|
6960
|
+
run : simulate the model with the saved time and options. None (default)
|
|
6961
|
+
simulates if the model was dumped with model.run_on_load = True
|
|
6944
6962
|
keep_json : save a dict (self.json_keep) in the model instance
|
|
6945
6963
|
default_url : TYPE, optional
|
|
6946
6964
|
Where to look if the file is not avaiable. The default is r'https://raw.githubusercontent.com/IbHansen/modelflow-manual/main/model_repo/'.
|
|
@@ -7045,7 +7063,10 @@ class Json_Mixin():
|
|
|
7045
7063
|
modelname = input['modelname']
|
|
7046
7064
|
# breakpoint()
|
|
7047
7065
|
mmodel = cls(frml, modelname=modelname, funks=funks,**kwargs)
|
|
7048
|
-
mmodel.oldkwargs = input.get('oldkwargs',{})
|
|
7066
|
+
mmodel.oldkwargs = input.get('oldkwargs',{})
|
|
7067
|
+
mmodel.run_on_load = input.get('run_on_load', False)
|
|
7068
|
+
if run is None:
|
|
7069
|
+
run = mmodel.run_on_load and has_data
|
|
7049
7070
|
mmodel.json_current_per = current_per
|
|
7050
7071
|
mmodel.set_var_description(input.get('var_description', {}))
|
|
7051
7072
|
mmodel.equations_latex = input.get('equations_latex', '')
|
|
@@ -7358,8 +7379,10 @@ class Solver_Mixin():
|
|
|
7358
7379
|
else:
|
|
7359
7380
|
solverguess = 'newton_un_normalized'
|
|
7360
7381
|
|
|
7361
|
-
#
|
|
7362
|
-
|
|
7382
|
+
# with ng=True (model option) the ng solvers are the default; a variable
|
|
7383
|
+
# at a fixed period, X(2002), is only evaluated by the ng solvers.
|
|
7384
|
+
# A normalized simultaneous model gets sim_ng - Newton must be asked for.
|
|
7385
|
+
if self.ng or self.fixed_periods:
|
|
7363
7386
|
solverguess = {'xgenr': 'xgenr_ng', 'sim': 'sim_ng',
|
|
7364
7387
|
'newtonstack': 'newtonstack_ng',
|
|
7365
7388
|
'newtonstack_un_normalized': 'newtonstack_ng',
|
|
@@ -7411,7 +7434,8 @@ class Solver_Mixin():
|
|
|
7411
7434
|
if not silent:
|
|
7412
7435
|
print('Running calc_adjust_model ')
|
|
7413
7436
|
print(f'Dummies set {self.find_fix_dummy_fixed(outdf)}')
|
|
7414
|
-
self.calc_add_factor_model.current_per=self.current_per
|
|
7437
|
+
self.calc_add_factor_model.current_per=self.current_per
|
|
7438
|
+
self.calc_add_factor_model.ng = self.ng # follows a later mmodel.ng = ...
|
|
7415
7439
|
out_add_factor = self.calc_add_factor_model(outdf,silent=silent) # calculate the adjustment factors
|
|
7416
7440
|
|
|
7417
7441
|
calc_add_factordf = out_add_factor.loc[self.current_per,self.fix_add_factor] # new adjust values
|
|
@@ -9863,6 +9887,8 @@ class Fix_Mixin():
|
|
|
9863
9887
|
selected = [v for up in pat.split() for v in fnmatch.filter(beh, up.upper())]
|
|
9864
9888
|
exo = [v+'_X' for v in selected ]
|
|
9865
9889
|
dummy = [v+'_D' for v in selected ]
|
|
9890
|
+
# _X/_D columns read as integers (all zero) can not take the float values
|
|
9891
|
+
dataframe[exo + dummy] = dataframe[exo + dummy].astype('float64')
|
|
9866
9892
|
# breakpoint()
|
|
9867
9893
|
with self.set_smpl(start,end,df=dataframe):
|
|
9868
9894
|
dataframe.loc[self.current_per,dummy] = 1.0
|
|
@@ -437,6 +437,184 @@ def extract_model_from_markdown(md_text: str, list_position: str = "front") -> s
|
|
|
437
437
|
return "\n".join(model_lines)
|
|
438
438
|
|
|
439
439
|
|
|
440
|
+
def _as_strings(value) -> list[str]:
|
|
441
|
+
"""None -> [], 'x' -> ['x'], iterable -> list of non-empty strings."""
|
|
442
|
+
if value is None:
|
|
443
|
+
return []
|
|
444
|
+
if isinstance(value, str):
|
|
445
|
+
return [value] if value else []
|
|
446
|
+
return [v for v in value if v]
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def _marker_pattern(markers: list[str]) -> str:
|
|
450
|
+
"""Regex alternation for markers; word-like markers only match whole words,
|
|
451
|
+
so 'FRML' does not match inside 'XFRML' or 'FRMLX'."""
|
|
452
|
+
parts = []
|
|
453
|
+
for m in sorted(markers, key=len, reverse=True):
|
|
454
|
+
pat = re.escape(m)
|
|
455
|
+
if re.match(r'\w', m):
|
|
456
|
+
pat = r'(?<!\w)' + pat
|
|
457
|
+
if re.search(r'\w$', m):
|
|
458
|
+
pat = pat + r'(?!\w)'
|
|
459
|
+
parts.append(pat)
|
|
460
|
+
return '|'.join(parts)
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def extract_equations(text: str, start=None, end=None, comment=None, transform=None) -> str:
|
|
464
|
+
"""Extract equations from a string in a foreign (or plain) format.
|
|
465
|
+
|
|
466
|
+
Parameters
|
|
467
|
+
----------
|
|
468
|
+
text : str
|
|
469
|
+
The source, e.g. the content of a Gekko .frm file or EViews equations.
|
|
470
|
+
start : str | list[str] | None
|
|
471
|
+
Marker(s) which start an equation, e.g. ``'FRML'``. Text outside
|
|
472
|
+
start ... end is ignored. ``None``: an equation starts right after the
|
|
473
|
+
previous one (or at the start of the line when ``end`` is None).
|
|
474
|
+
end : str | list[str] | None
|
|
475
|
+
Marker(s) which end an equation, e.g. ``'$'`` or ``';'``. An equation
|
|
476
|
+
can then span several lines. ``None``: one equation per line.
|
|
477
|
+
comment : str | list[str] | None
|
|
478
|
+
String(s) which start a comment; the rest of the line is dropped,
|
|
479
|
+
e.g. ``'()'`` for Gekko or ``"'"`` for EViews.
|
|
480
|
+
transform : callable | None
|
|
481
|
+
Called with each equation (markers removed, whitespace collapsed to
|
|
482
|
+
single spaces) and returns it in ModelFlow form, ``'lhs = rhs'`` or
|
|
483
|
+
``'<opts> lhs = rhs'``. Return ``None`` or ``''`` to drop the equation.
|
|
484
|
+
See :func:`gekko_to_modelflow` and :func:`eviews_to_modelflow`.
|
|
485
|
+
|
|
486
|
+
Markers are matched case-insensitively, and word-like markers only as
|
|
487
|
+
whole words.
|
|
488
|
+
|
|
489
|
+
Returns
|
|
490
|
+
-------
|
|
491
|
+
str
|
|
492
|
+
One equation per line, ``lhs = rhs`` or ``<opts> lhs = rhs``, without
|
|
493
|
+
FRML and $ -- those are added later by clean_expressions (tofrml).
|
|
494
|
+
"""
|
|
495
|
+
import warnings
|
|
496
|
+
|
|
497
|
+
comments = _as_strings(comment)
|
|
498
|
+
starts = _as_strings(start)
|
|
499
|
+
ends = _as_strings(end)
|
|
500
|
+
|
|
501
|
+
# 1. drop comments, line by line
|
|
502
|
+
if comments:
|
|
503
|
+
comment_re = re.compile('|'.join(re.escape(c) for c in comments))
|
|
504
|
+
lines = [comment_re.split(line, maxsplit=1)[0] for line in text.splitlines()]
|
|
505
|
+
else:
|
|
506
|
+
lines = text.splitlines()
|
|
507
|
+
|
|
508
|
+
# 2. cut into chunks, each holding at most one equation
|
|
509
|
+
if ends:
|
|
510
|
+
chunks = re.split(_marker_pattern(ends), '\n'.join(lines), flags=re.IGNORECASE)
|
|
511
|
+
unterminated = chunks.pop() # text after the last end marker
|
|
512
|
+
if unterminated.strip() and (
|
|
513
|
+
not starts or re.search(_marker_pattern(starts), unterminated, flags=re.IGNORECASE)):
|
|
514
|
+
warnings.warn(f'extract_equations: text after the last end marker ignored: {unterminated.strip()[:80]!r}')
|
|
515
|
+
else:
|
|
516
|
+
chunks = lines
|
|
517
|
+
|
|
518
|
+
# 3. find the equation in each chunk
|
|
519
|
+
start_re = re.compile(_marker_pattern(starts), flags=re.IGNORECASE) if starts else None
|
|
520
|
+
statements = []
|
|
521
|
+
for chunk in chunks:
|
|
522
|
+
if start_re:
|
|
523
|
+
found = list(start_re.finditer(chunk))
|
|
524
|
+
if not found:
|
|
525
|
+
continue
|
|
526
|
+
if len(found) > 1:
|
|
527
|
+
warnings.warn(f'extract_equations: more than one start marker before an end marker, '
|
|
528
|
+
f'only the last equation is used: {" ".join(chunk.split())[:80]!r}')
|
|
529
|
+
chunk = chunk[found[-1].end():]
|
|
530
|
+
stmt = ' '.join(chunk.split())
|
|
531
|
+
if not stmt:
|
|
532
|
+
continue
|
|
533
|
+
if transform is not None:
|
|
534
|
+
stmt = transform(stmt)
|
|
535
|
+
if not stmt or not stmt.strip():
|
|
536
|
+
continue
|
|
537
|
+
# a '$' left by a transform would switch tofrml off for the whole model
|
|
538
|
+
statements.append(stmt.strip().rstrip('$').rstrip())
|
|
539
|
+
|
|
540
|
+
return '\n'.join(statements)
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
def to_line_mode(text: str) -> str:
|
|
544
|
+
"""Bring ModelFlow text to one statement per line without '$'.
|
|
545
|
+
|
|
546
|
+
tofrml (in clean_expressions) only adds FRML and '$' when there is no '$'
|
|
547
|
+
at all in the text; with a single '$' anywhere every statement (FRML, LIST,
|
|
548
|
+
DO, ENDDO, DOABLE) must already be '$'-terminated. Mixing the two -- e.g.
|
|
549
|
+
list_defs ending in '$' plus extracted equations without -- breaks the
|
|
550
|
+
model. '$'-terminated text is therefore split on '$', each statement joined
|
|
551
|
+
to one line and the '$' dropped; '£' comments are removed. Text without
|
|
552
|
+
'$' is returned unchanged.
|
|
553
|
+
"""
|
|
554
|
+
if '$' not in text:
|
|
555
|
+
return text
|
|
556
|
+
return extract_equations(text, end='$', comment=commentchar)
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
def gekko_to_modelflow(stmt: str, code_map=None) -> str:
|
|
560
|
+
"""Translate one Gekko equation (without FRML and $) to ModelFlow form.
|
|
561
|
+
|
|
562
|
+
``'_SJRD log(C) = 0.5*log(Yd) + 1.5'`` -> ``'<STOC> log(C) = 0.5*log(Yd) + 1.5'``
|
|
563
|
+
|
|
564
|
+
The FRML code is mapped to ModelFlow options. Default: the letters after
|
|
565
|
+
the first one are read as J -> add factor, D -> exogenizing dummy, so J and
|
|
566
|
+
D -> ``<STOC>``, J only -> ``<ADD>``, D only -> ``<EXO>``, else ``<>``.
|
|
567
|
+
Gekko's relative J (JR) becomes an ordinary ModelFlow add factor.
|
|
568
|
+
Pass ``code_map`` (a dict code -> options or a callable) to override, for
|
|
569
|
+
instance ``functools.partial(gekko_to_modelflow, code_map={'_SJRD': '<est=ls,stoc>'})``.
|
|
570
|
+
Codes missing from a dict fall back to the default.
|
|
571
|
+
|
|
572
|
+
Also translates ``x[-1]`` -> ``x(-1)``, ``^`` -> ``**``, ``dif(``/``diff(`` -> ``DIFF(``
|
|
573
|
+
and ``pch(`` -> ``PCT_GROWTH(``.
|
|
574
|
+
"""
|
|
575
|
+
import modelnormalize as nz
|
|
576
|
+
|
|
577
|
+
m = re.match(r'\s*(_\w+)\s+(.*)', stmt, flags=re.DOTALL)
|
|
578
|
+
code, body = (m.group(1), m.group(2)) if m else ('', stmt)
|
|
579
|
+
|
|
580
|
+
opts = None
|
|
581
|
+
if callable(code_map):
|
|
582
|
+
opts = code_map(code)
|
|
583
|
+
elif code_map:
|
|
584
|
+
opts = code_map.get(code, code_map.get(code.upper()))
|
|
585
|
+
if opts is None:
|
|
586
|
+
letters = code[2:].upper()
|
|
587
|
+
has_j, has_d = 'J' in letters, 'D' in letters
|
|
588
|
+
opts = '<STOC>' if has_j and has_d else '<ADD>' if has_j else '<EXO>' if has_d else '<>'
|
|
589
|
+
|
|
590
|
+
body = re.sub(r'\[\s*([+-]?\d+)\s*\]', r'(\1)', body)
|
|
591
|
+
body = body.replace('^', '**')
|
|
592
|
+
body = nz.funk_replace_list([('DIF', 'DIFF'), ('PCH', 'PCT_GROWTH')], body)
|
|
593
|
+
return f'{opts} {body}'
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
def eviews_to_modelflow(stmt: str, behavioral_opts: str = '<>') -> str:
|
|
597
|
+
"""Translate one EViews equation to ModelFlow form.
|
|
598
|
+
|
|
599
|
+
``@IDENTITY`` equations get ``<>``, all other equations get
|
|
600
|
+
``behavioral_opts`` (for instance ``'<STOC>'``). EViews functions are
|
|
601
|
+
translated with :meth:`modelgrabwf2.GrabWf2Model.trans_eviews`
|
|
602
|
+
(``D(`` -> ``DIFF(``, ``@PC`` -> ``PCT_GROWTH``, ``^`` -> ``**`` ...).
|
|
603
|
+
"""
|
|
604
|
+
from modelgrabwf2 import GrabWf2Model
|
|
605
|
+
|
|
606
|
+
m = re.match(r'\s*@IDENTITY\s+(.*)', stmt, flags=re.IGNORECASE | re.DOTALL)
|
|
607
|
+
opts, body = ('<>', m.group(1)) if m else (behavioral_opts, stmt)
|
|
608
|
+
return f'{opts} {GrabWf2Model.trans_eviews(body).strip()}'
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
# type_input presets for Makemodel; explicit eq_start/eq_end/comment_start/transform win
|
|
612
|
+
EXTRACT_PRESETS = {
|
|
613
|
+
'gekko': dict(eq_start='FRML', eq_end='$', comment_start='()', transform=gekko_to_modelflow),
|
|
614
|
+
'eviews': dict(eq_start=None, eq_end=None, comment_start="'", transform=eviews_to_modelflow),
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
|
|
440
618
|
|
|
441
619
|
# def findlists_in_latex(input):
|
|
442
620
|
# '''extracte list with sublist from latex'''
|
|
@@ -2056,7 +2234,11 @@ class Makemodel(BaseExplode):
|
|
|
2056
2234
|
|---|---|
|
|
2057
2235
|
| `showoriginal_statements` | Input expressions |
|
|
2058
2236
|
| `showlist_defs` | Lists definitions |
|
|
2059
|
-
| `showtype_input` |
|
|
2237
|
+
| `showtype_input` | Original as type markdown, modelflow, single, gekko or eviews |
|
|
2238
|
+
| `showeq_start` | String(s) which start an equation; giving any extraction argument overrides type_input='markdown' |
|
|
2239
|
+
| `showeq_end` | String(s) which end an equation; None = one per line |
|
|
2240
|
+
| `showcomment_start` | String(s) which start a comment to the end of the line |
|
|
2241
|
+
| `showtransform` | Function translating each extracted equation to ModelFlow |
|
|
2060
2242
|
| `showreplacements` | list of string tupels with string replacements |
|
|
2061
2243
|
| `showfunks` | List of user specified functions to be used in model |
|
|
2062
2244
|
| `showmodelname` | A optional name for this (sub) model |
|
|
@@ -2133,7 +2315,13 @@ class Makemodel(BaseExplode):
|
|
|
2133
2315
|
normal_expressions : List[Any] = field(init=False, metadata={"description": "List of normal expressions"})
|
|
2134
2316
|
list_specification : str = field(init=False, metadata={"description": "All list specifications in string "})
|
|
2135
2317
|
modellist : str = field(init=False, metadata={"description": "The lists defined in string as a dictionary"})
|
|
2136
|
-
type_input : str = field(default= 'markdown' ,metadata={"description": "
|
|
2318
|
+
type_input : str = field(default= 'markdown' ,metadata={"description": "Original as type markdown, modelflow, single, gekko or eviews"})
|
|
2319
|
+
# Equation extraction for non-markdown input, see extract_equations().
|
|
2320
|
+
# type_input='gekko'/'eviews' fills in defaults from EXTRACT_PRESETS.
|
|
2321
|
+
eq_start : Optional[Any] = field(default=None, metadata={"description": "String(s) which start an equation, e.g. 'FRML'"})
|
|
2322
|
+
eq_end : Optional[Any] = field(default=None, metadata={"description": "String(s) which end an equation, e.g. '$'; None = one equation per line"})
|
|
2323
|
+
comment_start : Optional[Any] = field(default=None, metadata={"description": "String(s) which start a comment running to the end of the line"})
|
|
2324
|
+
transform : Optional[Any] = field(default=None, metadata={"description": "Function translating each extracted equation to ModelFlow, e.g. gekko_to_modelflow"})
|
|
2137
2325
|
# Optional estimation. An equation is estimated only if its FRML-name
|
|
2138
2326
|
# contains <estimator=...>/<est=...> or <estimator>/<est>. If the flag has no value,
|
|
2139
2327
|
# the global estimator below is used. Untagged equations remain identities.
|
|
@@ -2158,16 +2346,32 @@ class Makemodel(BaseExplode):
|
|
|
2158
2346
|
Untagged equations are treated exactly as before.
|
|
2159
2347
|
'''
|
|
2160
2348
|
|
|
2161
|
-
|
|
2349
|
+
extract_args_given = any(v is not None for v in
|
|
2350
|
+
(self.eq_start, self.eq_end, self.comment_start, self.transform))
|
|
2351
|
+
|
|
2352
|
+
if self.type_input in EXTRACT_PRESETS or extract_args_given:
|
|
2353
|
+
# extraction arguments win over the default type_input='markdown'
|
|
2354
|
+
preset = EXTRACT_PRESETS.get(self.type_input, {})
|
|
2355
|
+
for name, default in preset.items():
|
|
2356
|
+
if getattr(self, name) is None:
|
|
2357
|
+
setattr(self, name, default)
|
|
2358
|
+
extracted_frml = to_line_mode(self.list_defs) + '\n' + extract_equations(
|
|
2359
|
+
self.original_statements,
|
|
2360
|
+
start=self.eq_start, end=self.eq_end,
|
|
2361
|
+
comment=self.comment_start, transform=self.transform)
|
|
2362
|
+
self.markdown_model = ''
|
|
2363
|
+
|
|
2364
|
+
elif self.type_input == 'markdown':
|
|
2162
2365
|
extracted_frml = extract_model_from_markdown(self.list_defs+self.original_statements)
|
|
2163
2366
|
self.markdown_model = (mfmod_list_to_codeblock(self.original_statements))
|
|
2164
|
-
|
|
2165
|
-
elif self.type_input == 'single':
|
|
2367
|
+
|
|
2368
|
+
elif self.type_input == 'single':
|
|
2166
2369
|
extracted_frml = self.original_statements.replace('\n', '')
|
|
2167
2370
|
self.markdown_model = ''
|
|
2168
|
-
|
|
2169
|
-
else:
|
|
2170
|
-
|
|
2371
|
+
|
|
2372
|
+
else:
|
|
2373
|
+
# one statement per line, also when the source is '$'-terminated
|
|
2374
|
+
extracted_frml = to_line_mode(self.list_defs) + '\n' + to_line_mode(self.original_statements)
|
|
2171
2375
|
self.markdown_model = ''
|
|
2172
2376
|
|
|
2173
2377
|
self.clean_frml_statements = clean_expressions(
|
|
@@ -21,16 +21,12 @@ Some of the docstring are not very informative, to be improved.
|
|
|
21
21
|
|
|
22
22
|
"""
|
|
23
23
|
import pandas as pd
|
|
24
|
-
import networkx as nx
|
|
25
|
-
import openpyxl
|
|
26
24
|
from openpyxl import load_workbook
|
|
27
25
|
from openpyxl.formula import Tokenizer
|
|
28
26
|
from openpyxl.utils import get_column_letter
|
|
29
27
|
from openpyxl.utils import cols_from_range,rows_from_range
|
|
30
28
|
from openpyxl.worksheet.formula import ArrayFormula, DataTableFormula
|
|
31
29
|
|
|
32
|
-
import matplotlib.pylab as plt
|
|
33
|
-
import seaborn as sns
|
|
34
30
|
from pathlib import Path
|
|
35
31
|
|
|
36
32
|
import modelclass as mc
|