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.
model_latex_class.py ADDED
@@ -0,0 +1,478 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on October 2 2022
4
+ Based on model_latex module
5
+ using dataclases
6
+
7
+ @author: hanseni
8
+
9
+ Mostly to eat latex models and translate to business logic
10
+
11
+ The routines are specific to a style of latex and should be inspected before use
12
+
13
+ """
14
+ from IPython.display import display, Math, Latex, Markdown , Image
15
+ import re
16
+ from IPython.lib.latextools import latex_to_png
17
+ from pathlib import Path
18
+ import os
19
+ import re
20
+ from dataclasses import dataclass,field
21
+
22
+
23
+ import modelmanipulation as mp
24
+ from modelclass import model
25
+ import modelpattern as pt
26
+ def rebank(model):
27
+ ''' All variable names are decorated by a {bank}
28
+ The {bank} is injected as the first dimension '''
29
+
30
+ ypat = re.compile(pt.namepat+pt.lagpat)
31
+ funk = set(pt.funkname) | {'SUM'}
32
+ nobank = set('N S T __{bank} NORM PPF CDF'.split()) # specific variable names not to be decorated
33
+ notouch = funk | nobank # names not to be decorated
34
+ def trans(matchobj):
35
+ ''' The function recieves a matchobj entity. The matching groups can be accesed by matchobj.group()
36
+ it returns a string with a bankname added at the end or at the first __ which marks dimensions '''
37
+ var = matchobj.group(1)
38
+ lag = '(' + matchobj.group(2) + ')'if matchobj.group(2) else ''
39
+ if var.upper() in notouch:
40
+ return var+lag
41
+ else:
42
+ if '__' in var:
43
+ pre,post = var.split('__',1)
44
+ post = ''+post
45
+ else:
46
+ pre = var
47
+ post = ''
48
+
49
+ return (pre+'__{bank}'+post + lag)
50
+
51
+ banked = ypat.sub(trans, model)
52
+ return banked
53
+
54
+
55
+ def defrack(streng):
56
+ '''
57
+ \frac{xxx}{yyy} = ((xxx)/(yyy))
58
+
59
+ '''
60
+ tstreng = streng[:]
61
+ tfunks = [r'\frac{',r'\dfrac{']
62
+ for tfunk in tfunks :
63
+ while tfunk in tstreng:
64
+ start = tstreng.find(tfunk)
65
+ # first find the first matching {}
66
+ match = tstreng[start + len(tfunk):] # the rest of the string in which we have to match }
67
+ open = 1 # we already found the first {
68
+ for index1 in range(len(match)):
69
+ if match[index1] in '{}':
70
+ open = (open + 1) if match[index1] == '{' else (open - 1)
71
+ if not open:
72
+ break
73
+ # now find the second matching {}
74
+ match2 = match[index1 + 1+1:] # the string from the location of second { to the end of string
75
+ open=1
76
+ for index2 in range(len(match2)):
77
+ if match2[index2] in '{}':
78
+ open = (open + 1) if match2[index2] == '{' else (open - 1)
79
+ if not open:
80
+ break
81
+ tstreng = tstreng[:start]+ '(('+ match[:index1] +')/('+ match2[:index2]+'))'+match2[index2+1:]
82
+ return tstreng
83
+
84
+ def depower(streng):
85
+ '''
86
+ ^{(xxx)} = **(xxx)
87
+
88
+ '''
89
+ tstreng = streng[:]
90
+ tfunk = r'^{'
91
+ while tfunk in tstreng:
92
+ start = tstreng.find(tfunk)
93
+ match = tstreng[start + len(tfunk):]
94
+ open=1
95
+ for index in range(len(match)):
96
+ if match[index] in '{}':
97
+ open = (open + 1) if match[index] == '{' else (open - 1)
98
+ if not open:
99
+ break
100
+ tstreng = tstreng[:start]+ '**('+ match[:index] +')' + match[index+1:]
101
+ return tstreng
102
+
103
+
104
+ def debrace(streng):
105
+ '''
106
+ Eliminates underbrace{xxx}_{yyy} in a string
107
+ underbrace{xxx}_{yyy} => (xxx)
108
+ As there can be nested {} we need to match the braces
109
+
110
+ '''
111
+ tstreng = streng[:]
112
+ tfunk = r'\underbrace{'
113
+ while tfunk in tstreng:
114
+ start = tstreng.find(tfunk)
115
+ match = tstreng[start + len(tfunk):]
116
+ open = 1
117
+ for index1 in range(len(match)):
118
+ if match[index1] in '{}':
119
+ open = (open + 1) if match[index1] == '{' else (open - 1)
120
+ if not open:
121
+ break
122
+ goodstuf = tstreng[:start]+match[index1]
123
+ match2 = match[index1 + 1+2:]
124
+ open=1
125
+ for index2 in range(len(match2)):
126
+ if match2[index2] in '{}':
127
+ open = (open + 1) if match2[index2] == '{' else (open - 1)
128
+ if not open:
129
+ break
130
+ tstreng = tstreng[:start]+ ''+ match[:index1] +''+ match2[index2+1:]
131
+ # print(f'{tstreng=}')
132
+ return tstreng
133
+
134
+ def defunk(funk, subs , streng,startp='{',slutp='}'):
135
+ '''
136
+ \funk{xxx} => subs(xxx)
137
+
138
+ in a string
139
+ '''
140
+ tfunk, tstreng = funk[:] , streng[:]
141
+ tfunk = tfunk + startp
142
+ while tfunk in tstreng:
143
+ start = tstreng.find(tfunk)
144
+ match = tstreng[start + len(tfunk):]
145
+ open = 1
146
+ for index in range(len(match)):
147
+ if match[index] in startp+slutp:
148
+ open = (open + 1) if match[index] == startp else (open - 1)
149
+ if not open:
150
+ break
151
+ tstreng = tstreng[:start]+subs+'(' + match[:index] +')' + match[index + 1:]
152
+ return tstreng
153
+ #print(defunk(r'\sqrt',r'sqrt',r'a=\sqrt{b+ \sqrt{f+y}}'))
154
+ #print(defunk(r'\log',r'log',r'a=\log(b(-1)+ \log({f+y}))',startp='(',slutp=')'))
155
+
156
+ def findindex(ind):
157
+ ''' find the index variables on the left hand side. meaning variables braced by {} '''
158
+ lhs=ind.split('=')[0]
159
+ return re.findall('\{([A-Za-z][\w]*)\}',lhs ) # all the index variables
160
+
161
+ def doableold(ind,show=False):
162
+ ''' find all dimensions in the left hand side of = and and decorate with the nessecary do .. enddo '''
163
+
164
+ indexvar = findindex(ind) # all the index variables
165
+ if indexvar :
166
+ pre = ' $ '.join(['Do '+i for level,i in enumerate(indexvar)])+' $ \n '
167
+ post = '\n' + 'enddo $ '*len(indexvar)
168
+ out = pre+ind + post
169
+ if show:
170
+ print('Before doable',ind,sep='\n')
171
+ print('After doable',out,sep='\n')
172
+ print()
173
+ else:
174
+ out=ind
175
+ return out
176
+
177
+
178
+ def findlists(input):
179
+ '''extracte list with sublist from latex'''
180
+ relevant = re.findall(r'\$LIST\s*\\;\s*[^$]*\$',input.upper())
181
+ # print(f'{relevant=}')
182
+ temp1 = [l.replace('$','').replace('\\','')
183
+ .replace(',',' ').replace(';',' ')
184
+ .replace('{','').replace('}','').replace('\n','/ \n')
185
+ for l in relevant]
186
+ # print(f'{temp1=}')
187
+ temp2 = ['LIST ' + l.split('=')[0][4:].strip() +' = '
188
+ + l.split('=')[0][4:]
189
+ +' : '+ l.split('=')[1]+'$' for l in temp1]
190
+
191
+ # print(f'{temp2=}')
192
+ return ('\n'.join(temp2)+'\n')
193
+
194
+
195
+
196
+ def findallindex(ind0):
197
+ '''
198
+ - an equation looks like this
199
+ - <frmlname> [do_condition,...] lhs = rhs
200
+ - indicies are identified as __{} on the left hand side.
201
+
202
+ this function find frmlname and index variables on the left hand side. meaning variables braced by {} '''
203
+ if ind0.startswith('<'):
204
+ frmlname = re.findall('\<.*?\>',ind0)[0]
205
+ ind = ind0[ind0.index('>')+1:].strip()
206
+ else:
207
+ frmlname='<>'
208
+ ind=ind0.strip()
209
+ # print(f'{ind=}')
210
+
211
+ if ind.startswith('['):
212
+ do_conditions = ind[1:ind.index(']')]
213
+ rest = ind[ind.index(']')+1:].strip()
214
+ all_do_condition = {condition.split('.')[0] : condition.replace('.','_') for condition in do_conditions.split(',')}
215
+ # print(f'{do_conditions=}')
216
+ else:
217
+ all_do_condition = dict()
218
+ rest = ind.strip()
219
+
220
+ lhs=ind.split('=')[0]
221
+ do_indicies = re.findall('__\{([A-Za-z][\w]*)\}',lhs ) # all the index variables
222
+
223
+ do_indicies_dict = {ind : all_do_condition.get(ind) for ind in do_indicies}
224
+
225
+ return frmlname,do_indicies_dict,rest
226
+
227
+ def do_list(do_index,do_condition):
228
+ ''' do do_index_list do_condition = 1 $ '''
229
+
230
+
231
+ if do_condition:
232
+ out = f'do {do_index} {do_condition} = 1 $'
233
+ else:
234
+ out = f'do {do_index} $'
235
+ return out
236
+
237
+ def doable(ind,show=False):
238
+ ''' find all dimensions in the left hand side of = and and decorate with the nessecary do .. enddo '''
239
+
240
+ frmlname,do_indicies_dict,rest = findallindex(ind) # all the index variables
241
+ # breakpoint()
242
+
243
+ # print(f'{do_indicies_dict=}*******************')
244
+ if do_indicies_dict :
245
+ pre = ' '.join([do_list(i,c) for i,c in do_indicies_dict.items() ])
246
+ # print(f'{pre=}*******************')
247
+
248
+ post = 'enddo $ '*len(do_indicies_dict)
249
+ out = f'{pre}\n frml {frmlname} {rest} $\n{post}'
250
+ else:
251
+ out=f'frml <> {rest.strip()} $'
252
+
253
+ if show:
254
+ print('\nBefore doable',ind,sep='\n')
255
+ print('After doable',out,sep='\n')
256
+ return out
257
+ #%% class
258
+
259
+ @dataclass
260
+ class a_latex_model:
261
+ '''a model in latex '''
262
+ modeltext : str()
263
+ modelequations : str = field(init=False)
264
+ modellists : str = field(init=False)
265
+
266
+ def __post_init__(self):
267
+ self.modelequations = [equation for name,equation in re.findall(r'\\label\{eq:(.*?)\}\n(.*?)\\end\{',self.modeltext,re.DOTALL)
268
+ if name != 'Exclude'] # select the relevant equations
269
+ # print(f'{self.modelequations=}')
270
+ self.modellists = mp.list_extract(findlists(self.modeltext ))
271
+
272
+ self.eq_list = [a_latex_equation(eq,self.modellists) for eq in self.modelequations]
273
+
274
+ self.model_exploded = '\n'.join(eq.exploded for eq in self.eq_list)
275
+ print(f'{self.model_exploded=}')
276
+ self.mmodel = model( self.model_exploded )
277
+
278
+ @dataclass
279
+ class a_latex_equation():
280
+ original_equation : str = '' # an equation in latex
281
+ modellists : list =''
282
+ original_interior_equation : str = field(init=False)
283
+ original_terminal_equation : str = field(init=False)
284
+
285
+ def __post_init__(self):
286
+ self.original_interior_equation = self.original_terminal_equation = ''
287
+ if self.original_equation.startswith(r'\begin{split}'):
288
+ self.original_interior_equation,self.original_terminal_equation = self.original_equation.split(r'\\')
289
+ else:
290
+ self.original_interior_equation,self.original_terminal_equation = self.original_equation, None
291
+
292
+ self.transformed_interior = self.straighten_eq(self.original_interior_equation)
293
+ self.transformed_terminal = self.straighten_eq(self.original_terminal_equation )
294
+ self.doable_interior = doable(self.transformed_interior)
295
+
296
+ self.exploded_interior = mp.sumunroll(mp.dounloop(self.doable_interior,listin=self.modellists),listin=self.modellists)
297
+
298
+ self.exploded = self.exploded_interior
299
+
300
+ # self.int_frmlname,self.int_do_conditions,self.int_do_indices,self.expression = (
301
+ # findallindex(self.transformed_interior))
302
+
303
+ @property
304
+ def pprint(self):
305
+ print(self.original_interior_equation )
306
+ print(self.transformed_interior)
307
+ print(self.transformed_terminal)
308
+ print(doable(self.transformed_interior))
309
+
310
+ def straighten_eq(self,temp):
311
+ if type(temp) == type(None):
312
+ return None
313
+ trans={r'\left':'',
314
+ r'\right':'',
315
+ r'\min':'min',
316
+ r'\max':'max',
317
+ r'\rho':'rho',
318
+ r'&':'',
319
+ r'\\':'',
320
+ r'\nonumber' : '',
321
+ r'\_' : '_',
322
+ r'_{t}' : '',
323
+ r'_t' :'',
324
+ 'logit^{-1}' : 'logit_inverse',
325
+ r'\{' : '{',
326
+ r'\}' : '}',
327
+ r'\begin{split}' : '',
328
+ '\n' :'',
329
+ r'\forall' :'',
330
+ r'\;' :'',
331
+
332
+
333
+ }
334
+ ftrans = {
335
+ r'\sqrt':'sqrt',
336
+ r'\Delta':'diff',
337
+ r'\Phi':'NORM.CDF',
338
+ r'\Phi^{-1}':'NORM.PDF'
339
+ }
340
+ regtrans = {
341
+ r'\\Delta ([A-Za-z_][\w{},\^]*)':r'diff(\1)', # \Delta xy => diff(xy)
342
+ r'_{t-([1-9]+)}' : r'(-\1)', # _{t-x} => (-x)
343
+ r'_{t\+([1-9]+)}' : r'(+\1)', # _{t+x} => (+x)
344
+
345
+ # r'\^([\w])' : r'_\1', # ^x => _x
346
+ # r'\^\{([\w]+)\}(\w)' : r'_\1_\2', # ^{xx}y => _xx_y
347
+ r'\^{([\w+-]+)}' : r'__{\1}', # ^{xx} => _xx
348
+ r'\^{([\w+-]+),([\w+-]+)\}' : r'__{\1}__{\2}', # ^{xx,yy} => _xx_yy
349
+ r'\s*\\times\s*':'*' ,
350
+ r'\\text{\[([\w+-,.]+)\]}' : r'[\1]',
351
+ r'\\sum_{('+pt.namepat+')}\(' : r'sum(\1,'
352
+ }
353
+ # breakpoint()
354
+ try:
355
+ for before,to in ftrans.items():
356
+ temp = defunk(before,to,temp)
357
+ except:
358
+ print(f'{before=} {to=} {temp=}')
359
+ for before,to in trans.items():
360
+ temp = temp.replace(before,to)
361
+ for before,to in regtrans.items():
362
+ temp = re.sub(before,to,temp)
363
+ temp = debrace(temp)
364
+ temp = defrack(temp)
365
+ temp = depower(temp)
366
+ return temp
367
+
368
+
369
+
370
+ if __name__ == '__main__' :
371
+ test1 =r'''
372
+ $List \; agegroup= \{16, 17, 18, 19, 20, 99, 100 ,101,102\}$
373
+ $List \; agegroupwww= \{16, 17, 18, 19, 20, 99, 100 \}$
374
+
375
+
376
+ \begin{equation}
377
+ \label{eq:mod_another}
378
+ \begin{split}
379
+ \forall [agegroup.noend]\;& \underbrace{QC^{agegroup}_t}_{ddd} & &= \left(\dfrac{PCTOT_t}{PCTOT_{t+1}}\right) * QC^{agegroup+1}_{t+1}
380
+ \\
381
+ & & &=
382
+ \dfrac{VB^{agegroup-1}_{t-1} * \dfrac{NPOP^{agegroup-1}_{t-1}}{NPOP^{agegroup}_t}
383
+ * (1+R) + VY^{agegroup}_t - VB^{agegroup}_{t+1} )}{ PCTOT_{t}}
384
+ \end{split}
385
+ \end{equation}
386
+
387
+ \begin{equation}
388
+ \label{eq:mod_another}
389
+ \begin{split}
390
+ \forall [agegroup.middle]\;& \underbrace{QCx^{agegroup}_t}_{ddd} & &= \left(\dfrac{PCTOT_t}{PCTOT_{t+1}}\right) * QC^{agegroup+1}_{t+1}
391
+ \\
392
+ & & &=
393
+ \dfrac{VB^{agegroup-1}_{t-1} * \dfrac{NPOP^{agegroup-1}_{t-1}}{NPOP^{agegroup}_t}
394
+ * (1+R) + VY^{agegroup}_t - VB^{agegroup}_{t+1} )}{ PCTOT_{t}}
395
+ \end{split}
396
+ \end{equation}
397
+
398
+ \begin{equation}
399
+ \label{eq:mod_another}
400
+ \begin{split}
401
+ \forall [agegroup.end]\;& \underbrace{QCx^{agegroup}_t}_{ddd} & &= \left(\dfrac{PCTOT_t}{PCTOT_{t+1}}\right) * QC^{agegroup+1}_{t+1}
402
+ \\
403
+ & & &=
404
+ \dfrac{VB^{agegroup-1}_{t-1} * \dfrac{NPOP^{agegroup-1}_{t-1}}{NPOP^{agegroup}_t}
405
+ * (1+R) + VY^{agegroup}_t - VB^{agegroup}_{t+1} )}{ PCTOT_{t}}
406
+ \end{split}
407
+ \end{equation}
408
+
409
+ \begin{equation}
410
+ \label{eq:mod_another}
411
+ \begin{split}
412
+ \forall [agegroup.start]\;& \underbrace{QCx^{agegroup}_t}_{ddd} & &= \left(\dfrac{PCTOT_t}{PCTOT_{t+1}}\right) * QC^{agegroup+1}_{t+1}
413
+ \\
414
+ & & &=
415
+ \dfrac{VB^{agegroup-1}_{t-1} * \dfrac{NPOP^{agegroup-1}_{t-1}}{NPOP^{agegroup}_t}
416
+ * (1+R) + VY^{agegroup}_t - VB^{agegroup}_{t+1} )}{ PCTOT_{t}}
417
+ \end{split}
418
+ \end{equation}
419
+
420
+
421
+ '''
422
+ #%% test
423
+
424
+ this = a_latex_model(test1)
425
+ print('\nOutput\n',this.model_exploded)
426
+
427
+ test2 =r'''
428
+ $List \; agegroup= \{16, 17, 18, 19, 20, 99, 100 ,101,102\}$
429
+
430
+ \begin{equation}
431
+ \label{eq:ddd}
432
+ QC_t = \sum_{agegroup}\right(xx^{agegroup}\left) + 33
433
+ \end{equation}
434
+
435
+ '''
436
+ this2 = a_latex_model(test2)
437
+ print('\nOutput\n',this2.model_exploded)
438
+ #%% test2
439
+ test3 =r'''
440
+
441
+ $List \; agegroup= \{0 , 1, 2, 16, 17, 18, 19, 20, 99, 100 \}$
442
+
443
+
444
+ \begin{equation}
445
+ \label{eq:mod_another}
446
+ \forall [agegroup.nostart]\; Population^{agegroup}_t = Population^{agegroup-1}_{t-1} - Dead^{agegroup-1}_{t-1} - Emigration^{agegroup-1}_{t-1}
447
+ \end{equation}
448
+
449
+ \begin{equation}
450
+ \label{eq:mod_another2}
451
+ Born_t = \sum_{agegroup}(Fertility^{agegroup}_t \times Population^{agegroup}_t)
452
+ \end{equation}
453
+
454
+ \begin{equation}
455
+ \label{eq:mod_another24}
456
+ Dead^{agegroup} = Dear\_rate^{agegroup}_t*Population^{agegroup}_t)
457
+ \end{equation}
458
+
459
+ \begin{equation}
460
+ \label{eq:mod_another3}
461
+ \forall [agegroup.start]\;Population^{agegroup}_t =^{agegroup}_t = born_t
462
+ \end{equation}
463
+
464
+
465
+ \begin{equation}
466
+ \label{eq:mod_another25}
467
+ population = \sum_{agegroup}(Population^{agegroup}_t )
468
+ \end{equation}
469
+
470
+ \begin{equation}
471
+ \label{eq:mod_another26}
472
+ Dead = \sum_{agegroup}(Dead^{agegroup}_t )
473
+ \end{equation}
474
+
475
+ '''
476
+ this3 = a_latex_model(test3)
477
+ print('\nOutput\n',this3.model_exploded)
478
+ this3.mmodel.drawmodel(HR=1,sink='POPULATION',svg=1,browser=1)