ModelFlowIb 2.69__tar.gz → 2.70__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.69 → modelflowib-2.70}/ModelFlowIb.egg-info/PKG-INFO +1 -1
- {modelflowib-2.69 → modelflowib-2.70}/PKG-INFO +1 -1
- {modelflowib-2.69 → modelflowib-2.70}/modelclass.py +163 -31
- {modelflowib-2.69 → modelflowib-2.70}/modelconstruct.py +1 -2
- {modelflowib-2.69 → modelflowib-2.70}/modeljupytermagic.py +14 -1
- {modelflowib-2.69 → modelflowib-2.70}/modelmanipulation.py +52 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelmf.py +5 -1
- {modelflowib-2.69 → modelflowib-2.70}/modelpattern.py +13 -5
- {modelflowib-2.69 → modelflowib-2.70}/modelreport.py +5 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelwidget_input.py +22 -2
- {modelflowib-2.69 → modelflowib-2.70}/pyproject.toml +1 -1
- {modelflowib-2.69 → modelflowib-2.70}/ModelFlowIb.egg-info/SOURCES.txt +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/ModelFlowIb.egg-info/dependency_links.txt +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/ModelFlowIb.egg-info/requires.txt +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/ModelFlowIb.egg-info/top_level.txt +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelBLfunk.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/model_Excel.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/model_cvx.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/model_doable.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/model_dynare.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/model_financial_stability.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/model_ifrs9.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/model_latex.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/model_latex_class.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/model_run_numba.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelclass2.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeldash.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeldashboot.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeldashsidebar.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeldekom.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeldiff.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeldisplay.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelestimation.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelfeedback.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelgrab.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelgrabwf2.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelhelp.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelinvert.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeljupyter.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelmacrograb.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelnet.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelnewton.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelnormalize.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelpatternrev.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelsandbox.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelsandbox_Mixin.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeltodo.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeluserfunk.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modeluserfunk_old.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelvis.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelwidget.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelwidget_input_old.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/modelwidget_shinypy.py +0 -0
- {modelflowib-2.69 → modelflowib-2.70}/setup.cfg +0 -0
|
@@ -615,7 +615,7 @@ class BaseModel():
|
|
|
615
615
|
return 'values[row'+t.lag+','+str(columnsnr[t.var])+']'
|
|
616
616
|
|
|
617
617
|
columnsnr = self.get_columnsnr(databank)
|
|
618
|
-
fib1 = ['def make_los(funks=[]):\n']
|
|
618
|
+
fib1 = ['def make_los(funks=[],errorfunk=None):\n']
|
|
619
619
|
fib1.append(short + 'from modeluserfunk import ' +
|
|
620
620
|
(', '.join(pt.userfunk)).lower()+'\n')
|
|
621
621
|
fib1.append(short + 'from modelBLfunk import ' +
|
|
@@ -631,8 +631,11 @@ class BaseModel():
|
|
|
631
631
|
for v in self.solveorder)
|
|
632
632
|
|
|
633
633
|
fib2 = [long + 'except :\n']
|
|
634
|
+
# fib2.append(
|
|
635
|
+
# longer + 'print("Error in",allvar[solveorder[sys.exc_info()[2].tb_lineno-'+str(startline)+']]["frml"])\n')
|
|
634
636
|
fib2.append(
|
|
635
|
-
longer + '
|
|
637
|
+
longer + f'errorfunk(values,sys.exc_info()[2].tb_lineno,overhead={startline-1},overeq={0})\n')
|
|
638
|
+
|
|
636
639
|
fib2.append(longer + 'raise\n')
|
|
637
640
|
fib2.append(long + 'return \n')
|
|
638
641
|
fib2.append(short + 'return los\n')
|
|
@@ -691,12 +694,15 @@ class BaseModel():
|
|
|
691
694
|
databank[i] = databank[i].astype(object)
|
|
692
695
|
# databank.loc[:, i] = databank.loc[:, i].astype('O')
|
|
693
696
|
self.genrcolumns = databank.columns.copy()
|
|
697
|
+
self.genrindex = databank.index.copy()
|
|
698
|
+
|
|
694
699
|
make_los_text = self.outeval(databank)
|
|
695
700
|
self.make_los_text = make_los_text
|
|
696
701
|
exec(make_los_text, globals()) # creates the los function
|
|
697
|
-
self.solve_dag = make_los(self.funks)
|
|
702
|
+
self.solve_dag = make_los(self.funks,self.errfunk)
|
|
698
703
|
values = databank.values.copy() #
|
|
699
704
|
for periode in sol_periode:
|
|
705
|
+
self.periode = periode
|
|
700
706
|
row = databank.index.get_loc(periode)
|
|
701
707
|
self.solve_dag(values, row, self.solveorder, self.allvar)
|
|
702
708
|
if not silent:
|
|
@@ -1818,36 +1824,38 @@ class Model_help_Mixin():
|
|
|
1818
1824
|
return dataframe
|
|
1819
1825
|
|
|
1820
1826
|
@staticmethod
|
|
1821
|
-
def in_notebook():
|
|
1827
|
+
def in_notebook() -> bool:
|
|
1822
1828
|
try:
|
|
1823
1829
|
from IPython import get_ipython
|
|
1824
|
-
|
|
1830
|
+
shell = get_ipython()
|
|
1831
|
+
if shell is None: # pragma: no cover
|
|
1825
1832
|
return False
|
|
1833
|
+
return hasattr(shell, "kernel")
|
|
1826
1834
|
except ImportError:
|
|
1827
|
-
return False
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
@staticmethod
|
|
1831
|
-
def is_running_in_jupyter():
|
|
1835
|
+
return False
|
|
1836
|
+
|
|
1837
|
+
|
|
1838
|
+
@staticmethod
|
|
1839
|
+
def is_running_in_jupyter() -> bool:
|
|
1832
1840
|
try:
|
|
1833
|
-
|
|
1834
|
-
shell = get_ipython()
|
|
1835
|
-
if shell
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
except
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
@staticmethod
|
|
1845
|
-
def is_running_in_colab():
|
|
1841
|
+
from IPython import get_ipython
|
|
1842
|
+
shell = get_ipython()
|
|
1843
|
+
if shell is None:
|
|
1844
|
+
return False
|
|
1845
|
+
return shell.__class__.__name__ == "ZMQInteractiveShell"
|
|
1846
|
+
except ImportError:
|
|
1847
|
+
return False
|
|
1848
|
+
|
|
1849
|
+
|
|
1850
|
+
@staticmethod
|
|
1851
|
+
def is_running_in_colab() -> bool:
|
|
1846
1852
|
try:
|
|
1847
|
-
|
|
1848
|
-
|
|
1853
|
+
import google.colab # noqa: F401
|
|
1854
|
+
return True
|
|
1855
|
+
except ImportError:
|
|
1849
1856
|
return False
|
|
1850
|
-
|
|
1857
|
+
|
|
1858
|
+
|
|
1851
1859
|
# @staticmethod
|
|
1852
1860
|
class defsub(dict):
|
|
1853
1861
|
'''A subclass of dict.
|
|
@@ -8606,7 +8614,7 @@ class Solver_Mixin():
|
|
|
8606
8614
|
self.saveeval3(self.values_, self.row_, a)
|
|
8607
8615
|
self.errfunk(self.values_, linenr, overhead, overeq)
|
|
8608
8616
|
|
|
8609
|
-
def
|
|
8617
|
+
def errfunk_old(self, values, linenr, overhead=4, overeq=0):
|
|
8610
8618
|
''' developement function
|
|
8611
8619
|
|
|
8612
8620
|
to handle run time errors in model calculations'''
|
|
@@ -8614,7 +8622,7 @@ class Solver_Mixin():
|
|
|
8614
8622
|
# winsound.Beep(500,1000)
|
|
8615
8623
|
self.errdump = pd.DataFrame(
|
|
8616
8624
|
values, columns=self.genrcolumns, index=self.genrindex)
|
|
8617
|
-
self.lastdf = self.errdump
|
|
8625
|
+
self.lastdf = self.errdump.copy()
|
|
8618
8626
|
|
|
8619
8627
|
print('>> Error in :', self.name)
|
|
8620
8628
|
print('>> In :', self.periode)
|
|
@@ -8628,16 +8636,140 @@ class Solver_Mixin():
|
|
|
8628
8636
|
errvar = self.solveorder[varposition]
|
|
8629
8637
|
outeq = self.allvar[errvar]['frml']
|
|
8630
8638
|
print('>> Equation :', outeq)
|
|
8639
|
+
self.print_eq_values(errvar, self.errdump, per=[self.periode])
|
|
8640
|
+
|
|
8631
8641
|
print('A snapshot of the data at the error point is at .errdump ')
|
|
8632
8642
|
print('Also the .lastdf contains .errdump, for inspecting ')
|
|
8633
|
-
|
|
8643
|
+
|
|
8634
8644
|
if hasattr(self, 'dumplist'):
|
|
8635
8645
|
self.dumpdf = pd.DataFrame(self.dumplist)
|
|
8636
8646
|
del self.dumplist
|
|
8637
8647
|
self.dumpdf.columns = ['fair', 'per', 'iteration']+self.dump
|
|
8638
8648
|
|
|
8639
|
-
|
|
8640
|
-
|
|
8649
|
+
def errfunk(self, values, linenr, overhead=4, overeq=0):
|
|
8650
|
+
'''Development function to handle run time errors in model calculations'''
|
|
8651
|
+
|
|
8652
|
+
import pandas as pd
|
|
8653
|
+
|
|
8654
|
+
def find_log_arguments(text):
|
|
8655
|
+
"""Return all arguments inside balanced LOG(...) calls."""
|
|
8656
|
+
out = []
|
|
8657
|
+
upper = text.upper()
|
|
8658
|
+
target = 'LOG('
|
|
8659
|
+
i = 0
|
|
8660
|
+
while True:
|
|
8661
|
+
start = upper.find(target, i)
|
|
8662
|
+
if start == -1:
|
|
8663
|
+
break
|
|
8664
|
+
j = start + len(target)
|
|
8665
|
+
depth = 1
|
|
8666
|
+
argstart = j
|
|
8667
|
+
while j < len(text) and depth:
|
|
8668
|
+
if text[j] == '(':
|
|
8669
|
+
depth += 1
|
|
8670
|
+
elif text[j] == ')':
|
|
8671
|
+
depth -= 1
|
|
8672
|
+
j += 1
|
|
8673
|
+
if depth == 0:
|
|
8674
|
+
out.append(text[argstart:j-1].strip())
|
|
8675
|
+
i = j
|
|
8676
|
+
else:
|
|
8677
|
+
break
|
|
8678
|
+
return out
|
|
8679
|
+
|
|
8680
|
+
self.errdump = pd.DataFrame(values, columns=self.genrcolumns, index=self.genrindex)
|
|
8681
|
+
self.lastdf = self.errdump.copy()
|
|
8682
|
+
|
|
8683
|
+
print('Houston, we have a problem!\nThis equation raises an error:\n')
|
|
8684
|
+
# print('>> Error in :', self.name)
|
|
8685
|
+
|
|
8686
|
+
varposition = linenr - overhead - 1 + overeq
|
|
8687
|
+
# print('>> varposition :', varposition)
|
|
8688
|
+
|
|
8689
|
+
if not (0 <= varposition < len(self.solveorder)):
|
|
8690
|
+
print('>> Could not map traceback line to solveorder')
|
|
8691
|
+
print('A snapshot of the data at the error point is at .errdump')
|
|
8692
|
+
print('Also .lastdf contains a copy of .errdump for inspecting')
|
|
8693
|
+
return
|
|
8694
|
+
|
|
8695
|
+
errvar = self.solveorder[varposition]
|
|
8696
|
+
# print('>> Err variable :', errvar)
|
|
8697
|
+
outeq = self.allvar[errvar]['frml']
|
|
8698
|
+
|
|
8699
|
+
print('>>:', outeq,)
|
|
8700
|
+
print('In:', self.periode,'\n')
|
|
8701
|
+
print('Below you will find information which might help you identify the problem')
|
|
8702
|
+
terms = self.allvar[errvar]['terms']
|
|
8703
|
+
|
|
8704
|
+
# allowed_funks= sorted(pt.funkname + [f.__name__.upper() for f in self.funks])
|
|
8705
|
+
for i,t in enumerate(terms[:-1]):
|
|
8706
|
+
if t.var:
|
|
8707
|
+
if terms[i+1].op =='(' :
|
|
8708
|
+
print(f'UPS {t.var} looks like a function call, but it is not an allowed function name')
|
|
8709
|
+
return
|
|
8710
|
+
|
|
8711
|
+
|
|
8712
|
+
try:
|
|
8713
|
+
basepos = self.errdump.index.get_loc(self.periode)
|
|
8714
|
+
rows = []
|
|
8715
|
+
seen = set()
|
|
8716
|
+
|
|
8717
|
+
for t in terms:
|
|
8718
|
+
var = t.var
|
|
8719
|
+
if not var:
|
|
8720
|
+
continue
|
|
8721
|
+
|
|
8722
|
+
lagtxt = t.lag
|
|
8723
|
+
lag = int(lagtxt) if str(lagtxt).strip() else 0
|
|
8724
|
+
key = (var, lag)
|
|
8725
|
+
if key in seen:
|
|
8726
|
+
continue
|
|
8727
|
+
seen.add(key)
|
|
8728
|
+
|
|
8729
|
+
pos = basepos + lag
|
|
8730
|
+
value = self.errdump.iloc[pos][var] if 0 <= pos < len(self.errdump.index) else float('nan')
|
|
8731
|
+
term = f'{var}({lag:+})' if lag else var
|
|
8732
|
+
rows.append({'term': term, 'value': value})
|
|
8733
|
+
|
|
8734
|
+
if rows:
|
|
8735
|
+
print('\n>> Values of terms in used:')
|
|
8736
|
+
print(pd.DataFrame(rows).to_string(index=False))
|
|
8737
|
+
except Exception as e:
|
|
8738
|
+
print('>> Could not print compact values:', e)
|
|
8739
|
+
|
|
8740
|
+
try:
|
|
8741
|
+
log_args = find_log_arguments(outeq)
|
|
8742
|
+
if log_args:
|
|
8743
|
+
log_rows = []
|
|
8744
|
+
tmpvar = 'ZZZ__ERR_LOG_ARG'
|
|
8745
|
+
|
|
8746
|
+
for arg in log_args:
|
|
8747
|
+
try:
|
|
8748
|
+
val = self.errdump.mfcalc(
|
|
8749
|
+
f'<{self.periode}> {tmpvar} = {arg}'
|
|
8750
|
+
).loc[self.periode, tmpvar]
|
|
8751
|
+
status = 'Bad log argument' if pd.isna(val) or val <= 0 else 'ok'
|
|
8752
|
+
except Exception:
|
|
8753
|
+
val = ''
|
|
8754
|
+
status = 'Could not evaluate'
|
|
8755
|
+
|
|
8756
|
+
log_rows.append({'log(arg)': arg, 'value': val, 'status': status})
|
|
8757
|
+
|
|
8758
|
+
print('\n>> This shows the arguments to LOG, they should be numbers > 0:')
|
|
8759
|
+
print(pd.DataFrame(log_rows).to_string(index=False))
|
|
8760
|
+
|
|
8761
|
+
except Exception as e:
|
|
8762
|
+
print('\n>> Could not check LOG arguments:', e)
|
|
8763
|
+
|
|
8764
|
+
print('\nA snapshot of the data at the error point is at .errdump')
|
|
8765
|
+
print('Also .lastdf contains a copy of .errdump for inspecting')
|
|
8766
|
+
|
|
8767
|
+
if hasattr(self, 'dumplist'):
|
|
8768
|
+
self.dumpdf = pd.DataFrame(self.dumplist)
|
|
8769
|
+
del self.dumplist
|
|
8770
|
+
self.dumpdf.columns = ['fair', 'per', 'iteration'] + self.dump
|
|
8771
|
+
|
|
8772
|
+
|
|
8641
8773
|
def show_iterations(self, pat='*', per='', last=0, change=False,top=.9):
|
|
8642
8774
|
'''
|
|
8643
8775
|
shows the last iterations for the most recent simulation.
|
|
@@ -1102,7 +1102,7 @@ class BaseExplode:
|
|
|
1102
1102
|
original_statements : str = field(default="", metadata={"description": "Input expressions"})
|
|
1103
1103
|
normal_frml : str = field(default="",init=False, metadata={"description": "Output normalized expressions"})
|
|
1104
1104
|
markdown_model : str = field(default="",init=False, metadata={"description": "As markdown"})
|
|
1105
|
-
funks: List[Any] = field(default_factory=list)
|
|
1105
|
+
funks : List[Any] = field(default_factory=list, metadata={"description": "List of user specified functions to be used in model"})
|
|
1106
1106
|
|
|
1107
1107
|
|
|
1108
1108
|
|
|
@@ -1149,7 +1149,6 @@ class Mexplode(BaseExplode):
|
|
|
1149
1149
|
normal_expressions : List[Any] = field(init=False, metadata={"description": "List of normal expressions"})
|
|
1150
1150
|
list_specification : str = field(init=False, metadata={"description": "All list specifications in string "})
|
|
1151
1151
|
modellist : str = field(init=False, metadata={"description": "The lists defined in string as a dictionary"})
|
|
1152
|
-
funks : List[Any] = field(default_factory=list, metadata={"description": "List of user specified functions to be used in model"})
|
|
1153
1152
|
type_input : str = field(default= 'markdown' ,metadata={"description": "Originaal as type modelflow or markdown"})
|
|
1154
1153
|
|
|
1155
1154
|
def __post_init__(self):
|
|
@@ -438,6 +438,19 @@ try:
|
|
|
438
438
|
print(f"⚠️ Warning: replacements '{repl_arg}' not found or invalid.")
|
|
439
439
|
replacements = None
|
|
440
440
|
|
|
441
|
+
funks=[]
|
|
442
|
+
if 'funks' in options:
|
|
443
|
+
funks_arg = options['funks']
|
|
444
|
+
try:
|
|
445
|
+
funks = ast.literal_eval(funks_arg)
|
|
446
|
+
except (ValueError, SyntaxError):
|
|
447
|
+
if funks_arg in user_ns:
|
|
448
|
+
funks = user_ns[funks_arg]
|
|
449
|
+
else:
|
|
450
|
+
print(f"⚠️ Warning: funks '{funks_arg}' not found or invalid.")
|
|
451
|
+
funks = []
|
|
452
|
+
|
|
453
|
+
|
|
441
454
|
# ------------------------------------------------------------
|
|
442
455
|
# Handle segmented models
|
|
443
456
|
# ------------------------------------------------------------
|
|
@@ -502,7 +515,7 @@ try:
|
|
|
502
515
|
# ------------------------------------------------------------
|
|
503
516
|
# Create Mexplode model
|
|
504
517
|
# ------------------------------------------------------------
|
|
505
|
-
emodel = Mexplode(model_text, replacements=replacements)
|
|
518
|
+
emodel = Mexplode(model_text, replacements=replacements,funks=funks)
|
|
506
519
|
|
|
507
520
|
|
|
508
521
|
user_ns[exploded_name] = emodel
|
|
@@ -666,6 +666,7 @@ def check_syntax_frml(frml):
|
|
|
666
666
|
return True
|
|
667
667
|
except:
|
|
668
668
|
return False
|
|
669
|
+
|
|
669
670
|
def check_syntax_udtryk(udtryk):
|
|
670
671
|
''' check syntax of frml '''
|
|
671
672
|
try:
|
|
@@ -674,6 +675,57 @@ def check_syntax_udtryk(udtryk):
|
|
|
674
675
|
except:
|
|
675
676
|
return False
|
|
676
677
|
|
|
678
|
+
def check_syntax(expression_list, noprint=False, raise_error=True):
|
|
679
|
+
"""
|
|
680
|
+
Check the Python syntax of one or more expressions.
|
|
681
|
+
|
|
682
|
+
Parameters
|
|
683
|
+
----------
|
|
684
|
+
expression_list : str or iterable of str
|
|
685
|
+
A single expression or a collection of expressions to check.
|
|
686
|
+
Each expression is passed to `check_syntax_udtryk_new`.
|
|
687
|
+
|
|
688
|
+
noprint : bool, default False
|
|
689
|
+
If False, syntax errors are printed when found.
|
|
690
|
+
If True, printing is suppressed.
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
raise_error : bool, default True
|
|
694
|
+
If True, a SyntaxError is raised when one or more syntax errors
|
|
695
|
+
are detected.
|
|
696
|
+
If False, errors are not raised.
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
Returns
|
|
700
|
+
-------
|
|
701
|
+
None
|
|
702
|
+
This function does not return a value.
|
|
703
|
+
|
|
704
|
+
Notes
|
|
705
|
+
-----
|
|
706
|
+
If `expression_list` is a string, it is treated as a single expression.
|
|
707
|
+
If syntax errors are found, they can be printed and/or raised depending
|
|
708
|
+
on the values of `noprint` and `raise_error`.
|
|
709
|
+
"""
|
|
710
|
+
expressions = [expression_list] if isinstance(expression_list, str) else expression_list
|
|
711
|
+
|
|
712
|
+
errlist = []
|
|
713
|
+
for udtryk in expressions:
|
|
714
|
+
ok, text = check_syntax_udtryk_new(udtryk)
|
|
715
|
+
if not ok:
|
|
716
|
+
errlist.append(text)
|
|
717
|
+
|
|
718
|
+
if errlist:
|
|
719
|
+
message = "\n\n".join(errlist)
|
|
720
|
+
if not noprint:
|
|
721
|
+
print(message)
|
|
722
|
+
if raise_error:
|
|
723
|
+
if noprint:
|
|
724
|
+
raise SyntaxError(message)
|
|
725
|
+
else:
|
|
726
|
+
raise SyntaxError() from None
|
|
727
|
+
|
|
728
|
+
|
|
677
729
|
def check_syntax_udtryk_new(udtryk):
|
|
678
730
|
'''Check syntax of an expression, pinpoint errors, and show error location.'''
|
|
679
731
|
try:
|
|
@@ -17,6 +17,7 @@ import fnmatch
|
|
|
17
17
|
|
|
18
18
|
from modelclass import model
|
|
19
19
|
import modelvis as mv
|
|
20
|
+
from modelmanipulation import check_syntax
|
|
20
21
|
|
|
21
22
|
|
|
22
23
|
if not hasattr(pd.DataFrame,'mf'):
|
|
@@ -66,8 +67,11 @@ if not hasattr(pd.DataFrame,'mf'):
|
|
|
66
67
|
|
|
67
68
|
def makemodel(self,eq,**kwargs):
|
|
68
69
|
'''Makes a model from equations'''
|
|
70
|
+
# self.eq = eq
|
|
71
|
+
self.eq = "\n".join(line.lstrip() for line in eq.splitlines())
|
|
72
|
+
# check_syntax(self.eq)
|
|
69
73
|
self.modelopt = {**self.modelopt,**kwargs}
|
|
70
|
-
|
|
74
|
+
|
|
71
75
|
self.model = model.from_eq(self.eq,**kwargs)
|
|
72
76
|
return self.copy()
|
|
73
77
|
|
|
@@ -17,7 +17,7 @@ from collections import namedtuple
|
|
|
17
17
|
from collections import defaultdict
|
|
18
18
|
from typing import NamedTuple, Optional
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
import modelmanipulation as mp
|
|
21
21
|
import modelBLfunk
|
|
22
22
|
|
|
23
23
|
class FrmlParts(NamedTuple):
|
|
@@ -275,17 +275,25 @@ def list_extract(equations,silent=True):
|
|
|
275
275
|
def check_syntax_model(equations,test=True):
|
|
276
276
|
''' cheks if equations have syntax errors by calling the python compile.parse '''
|
|
277
277
|
import ast
|
|
278
|
-
|
|
278
|
+
ok =True
|
|
279
279
|
try:
|
|
280
280
|
for frml in find_frml(equations):
|
|
281
281
|
a, fr, n, udtryk = split_frml(frml)
|
|
282
282
|
ast.parse(re.sub(r'\n','',re.sub(' ','',udtryk[:-1])))
|
|
283
283
|
except SyntaxError:
|
|
284
284
|
print('Syntax error in:',frml)
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
return error
|
|
285
|
+
ok =False
|
|
286
|
+
return ok
|
|
288
287
|
|
|
288
|
+
def get_expressions(equations):
|
|
289
|
+
''' returns a generator of expressions in a model with frml <> expression $ '''
|
|
290
|
+
expressions = (split_frml(f)[3][:-1].replace(r'\n','').replace(r' ','') for f in find_frml(equations))
|
|
291
|
+
return expressions
|
|
292
|
+
|
|
293
|
+
def check_syntax_model(equations):
|
|
294
|
+
mp.check_syntax(get_expressions(equations))
|
|
295
|
+
return True
|
|
296
|
+
|
|
289
297
|
|
|
290
298
|
def udtryk_parse(udtryk,funks=[]):
|
|
291
299
|
'''returns a list of terms from an expression ie: lhs=rhs $
|
|
@@ -816,7 +816,8 @@ class slidewidget(SingleWidgetBase):
|
|
|
816
816
|
|
|
817
817
|
# Normalize current_values (notably split var strings)
|
|
818
818
|
self.current_values = {
|
|
819
|
-
des: {k: (v.split() if k == "var" else v) for k, v in cont.items() if k in {"value", "var", "op"}}
|
|
819
|
+
# des: {k: (v.split() if k == "var" else v) for k, v in cont.items() if k in {"value", "var", "op"}}
|
|
820
|
+
des: {k: (v.split() if k == "var" else v) for k, v in cont.items()}
|
|
820
821
|
for des, cont in self.widgetdef["content"].items()
|
|
821
822
|
}
|
|
822
823
|
|
|
@@ -836,6 +837,7 @@ class slidewidget(SingleWidgetBase):
|
|
|
836
837
|
for _, cont in self.current_values.items():
|
|
837
838
|
op = cont.get("op", "=")
|
|
838
839
|
value = cont["value"]
|
|
840
|
+
# debug_var(value,current_per)
|
|
839
841
|
for var in cont["var"]:
|
|
840
842
|
if op == "+":
|
|
841
843
|
df.loc[current_per, var] = df.loc[current_per, var] + value
|
|
@@ -850,7 +852,25 @@ class slidewidget(SingleWidgetBase):
|
|
|
850
852
|
elif op == "=impulse":
|
|
851
853
|
df.loc[current_per[0], var] = value
|
|
852
854
|
elif op == "%":
|
|
853
|
-
df.loc[current_per, var] = df.loc[current_per, var] * (1
|
|
855
|
+
df.loc[current_per, var] = df.loc[current_per, var] * (1 + value / 100)
|
|
856
|
+
elif op == "%growth":
|
|
857
|
+
startindex = df.index.get_loc(current_per[0])
|
|
858
|
+
varloc = df.columns.get_loc(var)
|
|
859
|
+
start_value = cont.get('start_value','')
|
|
860
|
+
|
|
861
|
+
if startindex < 1:
|
|
862
|
+
if start_value :
|
|
863
|
+
df.iloc[startindex,varloc]= float(start_value)
|
|
864
|
+
else:
|
|
865
|
+
raise ValueError('For %growth we need a start value=')
|
|
866
|
+
else:
|
|
867
|
+
|
|
868
|
+
df.iloc[startindex,varloc]= df.iloc[startindex-1,varloc] * (1 + value / 100)
|
|
869
|
+
|
|
870
|
+
for i,per in enumerate(current_per) :
|
|
871
|
+
if i == 0:
|
|
872
|
+
continue
|
|
873
|
+
df.iloc[i+startindex,varloc]= df.iloc[i+startindex-1,varloc] * (1 + value / 100)
|
|
854
874
|
else:
|
|
855
875
|
raise ValueError(f"Unsupported operator {op!r} in slider mapping for {var!r}.")
|
|
856
876
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|