fumitoh / modelx

Use Python like a spreadsheet!

Home Page:https://modelx.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Circular reference crashing the model

alexeybaran opened this issue · comments

The following circular reference code crashes the model without any warning or explanation:

import modelx as mx
m, s = mx.new_model(), mx.new_space()
@mx.defcells
def a():
    return b()

@mx.defcells
def b():
    return a()

s.a()

@alexeybaran I ran the code above on my PC and got the following error as expected.

modelx.core.errors.FormulaError: Error raised during formula execution
modelx.core.errors.DeepReferenceError: Formula chain exceeded the 50000 limit
Formula traceback:
0: Model1.Space1.a(), line 2
49998: Model1.Space1.a(), line 2
49999: Model1.Space1.b(), line 2
50000: Model1.Space1.a(), line 2
Formula source:
def a():
    return b()
  • How much memory does your PC have?
  • What's your Python version?
  • Try mx.set_recursion(10000) and see if the issue persists.

I get the following error code, if I wait long enough:
Process finished with exit code -2147483647

  • How much memory does your PC have?
    16 GB. I don't see memory consumption going above 60%, when I run this code.
  • What's your Python version?
    3.9
  • Try mx.set_recursion(10000) and see if the issue persists.
    This helps. I'm still curious why 50000 doesn't work.

On a slightly unrelated topic. When I define a() and b() in the way above, I get the following Warning:
UserWarning: Cannot retrieve source code for function 'a'. a.source set to None.
It later crashes Formula traceback functionality. Do you know what the reason of the Warning is?

Are you using Python's default iterpreter? modelx needs IPython to be executed interactively.

Yes, it is default interpreter. What does it mean "Python to be executed interactively"?

To run modelx code interactively in read–eval–print loops, you need IPython. If you run modelx code in a .py file, you don't need IPython, just need Python.

I don't follow. I always ran this kind of code

@mx.defcells
def a():
    return b()

in pyCharm Python console. It seems to do what is called "read–eval–print loops". In the past I had pyCharm linked to Anaconda Python installation. Now it is linked directly to Python installed without Anaconda.

Is it a recent change making modelx more integrated with Jupiter notebooks?

No, it's been always like that. Try get_ipython() in your pyCharm Python console and see if it returns some object.

>>> get_ipython()
Traceback (most recent call last):
  File "C:\Python64\3.9\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
NameError: name 'get_ipython' is not defined

How come your PyCharm not using IPython!? You should set up your PyCharm to use IPython, not the default interpreter.

The prompt would be like In [2]: intead of >>> once you get IPython work in PyCharm console.

This may help: iPython instead of Python for console? – IDEs Support (IntelliJ Platform) | JetBrains

image

This helped. I have iPython now and there is no issue with @defcells.
50000 recursion still doesn't work. 10000 does.

There's a point between 10000 and 50000 where the code starts to crash on your machine. The point seems to differ by hardware & software enviroments.
I want to keep the default max depth as high as possible but may lower it to avoid the crash by anyone.
Anyway, Python 3.11 is coming out later this year and it's expected to solve this max depth problem once and for all.
I was also thinking about placing an init file in user's home directory so that the user can customize modelx parameters like max depth.

Clear. Thank you very much.