modelflowib 2.73__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.
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)