fumitoh / modelx

Use Python like a spreadsheet!
https://modelx.io
GNU Lesser General Public License v3.0
89 stars 20 forks source link

Error with Interspatial Dependencies #43

Closed mhumpher closed 2 months ago

mhumpher commented 3 years ago

Background: I have a space that I use for inputs set up as references. I then have other spaces that have interdependent cell references. These spaces also depend on the input space and use its references. The issue that I'm getting is a error when I update the input references I get an error. The error appears to be somewhat random. Once I ran the below script three times before getting the error and then on a fresh console the error appeared on the first run but not the second and then again on a third run.

Example Code:

import modelx as mx

model = mx.new_model("test")

inputs = model.new_space("Inputs")

inputs.a = 2
inputs.b = 3

calc = model.new_space("Pythagoras")

@mx.defcells
def pythagoras():
    return (Inputs.a ** 2 + Add.add2() ** 2) ** .5

calc2 = model.new_space("Add")

@mx.defcells
def add2():
    return Inputs.a + Inputs.b

calc.Inputs = inputs
calc.Add = calc2
calc2.Inputs = inputs

print(model.Pythagoras.pythagoras())
model.Inputs.a = 5
print(model.Pythagoras.pythagoras())

Output:

5.385164807134504
Traceback (most recent call last):

  File "<ipython-input-4-68bc2d796ba1>", line 1, in <module>
    runfile('**[removed]**/test.py', wdir='**[removed]**')

  File "C:\Anaconda3201910\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda3201910\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "**[removed]**/test.py", line 34, in <module>
    model.Inputs.a = 5

  File "C:\Anaconda3201910\lib\site-packages\modelx\core\space.py", line 974, in __setattr__
    self._impl.set_attr(name, value, refmode="auto")

  File "C:\Anaconda3201910\lib\site-packages\modelx\core\space.py", line 1657, in set_attr
    self.spacemgr.change_ref(self, name, value, refmode)

  File "C:\Anaconda3201910\lib\site-packages\modelx\core\model.py", line 1245, in change_ref
    is_relative=is_relative)

  File "C:\Anaconda3201910\lib\site-packages\modelx\core\space.py", line 1798, in on_change_ref
    self.on_del_ref(name)

  File "C:\Anaconda3201910\lib\site-packages\modelx\core\space.py", line 1815, in on_del_ref
    self.self_refs.del_item(name)

  File "C:\Anaconda3201910\lib\site-packages\modelx\core\space.py", line 100, in del_item
    self.owner.model.clear_attr_referrers(self.fresh[name])

  File "C:\Anaconda3201910\lib\site-packages\modelx\core\model.py", line 352, in clear_attr_referrers
    descs = self.tracegraph.remove_with_descs(node)

  File "C:\Anaconda3201910\lib\site-packages\modelx\core\model.py", line 61, in remove_with_descs
    desc = nx.descendants(self, source)

  File "C:\Anaconda3201910\lib\site-packages\networkx\algorithms\dag.py", line 71, in descendants
    raise nx.NetworkXError("The node %s is not in the graph." % source)

TypeError: not all arguments converted during string formatting

Versions: modelx = 0.13.1 networkx = 2.3

fumitoh commented 3 years ago

Thanks for the bug report. The fix will be reflected in the next release.

mhumpher commented 3 years ago

Thanks for the quick response. Tried out the change on my less trivial model and looks like its running smoothly again.

fumitoh commented 3 years ago

Just released v0.14.0 and this issue is fixed.