ecell / ecell4_base

An integrated software environment for multi-algorithm, multi-timescale, multi-spatial-representation simulation of various cellular phenomena
https://ecell4.e-cell.org/
GNU General Public License v3.0
63 stars 23 forks source link

`get_model` causes an error in Jupyter notebook but not in PyCharm. #247

Closed CiaranWelsh closed 5 years ago

CiaranWelsh commented 6 years ago

Hi, I've been playing around with ecell4, I think its a great package and I hope to use it in my thesis. Since I'm new to Ecell this may just be my inexperience rather than a bug but I've been building a mesoscale model in my IDE (pycharm) and the simulation works fine. When I copy the code over to jupyter notebook (for the visualisation), the same code gives an error.

The code I'm running is:

[import numpy, pandas, os, glob
from ecell4 import *
import matplotlib.pyplot as plt

AGE = 20

kRosProd = 0.1 * AGE
kRosDeg = 5
kMMPProd = 0.01
kMMPDeg = 0.001
kColProd = 0.5
kColFrag = 0.001
kFragDeg = 0.1

with species_attributes():
    ROS |{'D': '0.1'}
    # UV |{'D': '1'}
    MMPs | {'D': '1'}
    Collagen | {'D': '0'}
    Frags | {'D': '0'}
    Fibroblast | {'D': '10'}

with reaction_rules():
    ~ROS > ROS | kRosProd
    ROS > ~ROS | kRosDeg
    ~MMPs > MMPs | kMMPProd
    MMPs > ~MMPs | kMMPDeg
    ~Collagen + Fibroblast > Collagen + Fibroblast | kColProd/AGE
    Collagen > Frags | kColFrag
    Frags > ~Frags | kFragDeg

# y = run_simulation(numpy.linspace(0, 100, 100), solver='meso')
m = get_model()
w = meso.MesoscopicWorld(Real3(1, 1, 1), Integer3(4, 4, 4))

w.add_molecules(Species('ROS'), 10)
w.add_molecules(Species('Collagen'), 500)
w.add_molecules(Species('Fibroblast'), 30)
w.add_molecules(Species('MMPs'), 30)
# w.add_molecules(Species('Fibroblast'), 30)
#
w.bind_to(m)

sim = meso.MesoscopicSimulator(w)
col_obs = FixedIntervalNumberObserver(1.0, ['Collagen'])
obs = FixedIntervalNumberObserver(1.0, [i.serial() for i in m.list_species() if i.serial() != 'Collagen'])
sim.run(1000, [col_obs, obs])

viz.plot_number_observer(obs)
viz.plot_number_observer(col_obs)

Again, the code works fine in pycharm, but gives me the following error in jupyter:

AttributeError                            Traceback (most recent call last)
<ipython-input-15-36953b53801b> in <module>()
     41 # y = run_simulation(numpy.linspace(0, 100, 100), solver='meso')
     42 # print(y)
---> 43 m = get_model()
     44 print(m)
     45 #

~/anaconda2/envs/py36/lib/python3.6/site-packages/ecell4/util/decorator.py in get_model(is_netfree, without_reset, seeds, effective)
    421 
    422     for sp in SPECIES_ATTRIBUTES:
--> 423         m.add_species_attribute(sp)
    424     for rr in REACTION_RULES:
    425         m.add_reaction_rule(rr)

AttributeError: 'ecell4.ode.ODENetworkModel' object has no attribute 'add_species_attribute
kaizu commented 6 years ago

Hi, @CiaranWelsh! Thank you for using. We really welcome that. Please ask anything.

About this issue, I tested your code with the latest version (4.1.4) on Jupyter Notebook (Python3, Ubuntu), and couldn't reproduce the error. I just copied, pasted and ran your code. Could you tell me if the environment is different.

Here is my result: https://gist.github.com/kaizu/da15d2123bd627f10e2aff99527c9e21

From the error message, I suspect some causes. Please try the followings.

  1. Restart kernel, and run the code without any other (or try the notebook I uploaded to gist).
  2. Run the following cell before running your code, and you might get another error like RuntimeError: unknown variable [kColFrag] was used.:
import ecell4.util.decorator
ecell4.util.decorator.ENABLE_IMPLICIT_DECLARATION = False
  1. Comment out species_attributes section, and try show(m) just after m = get_model(). Then, you can check if reactions are assigned correctly or not.

Normally, for spatial simulations, NetworkModel is created rather than ODENetworkModel. But, when non-numbers or rate law equations are given at the definition of reaction rules (the value after |), the model is regarded as an ODENetworkModel. Unexpectedly, if some variable shown after | in reaction_rules section is not defined (due to typo or previous executions), the variable is implicitly treated as a new Species, and thus as a rate law. The code above (ENABLE_IMPLICIT_DECLARATION) stops to do that.

However, I don't know why it only happens on Jupyter NB.

This might be not your case, but I think it's worth trying. Thanks.

kaizu commented 5 years ago

Close this once.