Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Raise ContainerAlreadyContains when running optimizations #1421

Open
1 task done
thuwilliamwyx opened this issue Jan 2, 2025 · 6 comments
Open
1 task done
Labels
bug needs information Lacks requires information or a reproducible example to be addressed.

Comments

@thuwilliamwyx
Copy link

thuwilliamwyx commented Jan 2, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Problem description

我在尝试测试我的模型,但我发现当我在一个ipynb中多次优化时,从第二个开始就会提示ContainerAlreadyContains: Container '<optlang.container.Container object at 0x000002286E0D5DC0>' already contains an object with name 'bio1'.只有重启整个ipynb才可以。

Code sample

Code run:

import cobra
import sys
sys.path.append(r'./script/')


ecModel_file="./model/eciLC858_v3.0.json"
model = cobra.io.json.load_json_model(ecModel_file)
cobra.flux_analysis.pfba(model)

Traceback:

---------------------------------------------------------------------------
ContainerAlreadyContains                  Traceback (most recent call last)
Cell In[2], [line 8](vscode-notebook-cell:?execution_count=2&line=8)
      [6](vscode-notebook-cell:?execution_count=2&line=6) ecModel_file="./model/eciLC858_v3.0.json"
      [7](vscode-notebook-cell:?execution_count=2&line=7) model = cobra.io.json.load_json_model(ecModel_file)
----> [8](vscode-notebook-cell:?execution_count=2&line=8) cobra.flux_analysis.pfba(model)

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\cobra\flux_analysis\parsimonious.py:70, in pfba(model, fraction_of_optimum, objective, reactions)
     [68](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/flux_analysis/parsimonious.py:68)     add_pfba(m, objective=objective, fraction_of_optimum=fraction_of_optimum)
     [69](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/flux_analysis/parsimonious.py:69)     m.slim_optimize(error_value=None)
---> [70](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/flux_analysis/parsimonious.py:70)     solution = get_solution(m, reactions=reactions)
     [71](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/flux_analysis/parsimonious.py:71) return solution

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\cobra\core\model.py:1246, in Model.__exit__(self, type, value, traceback)
   [1244](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/core/model.py:1244) """Pop the top context manager and trigger the undo functions"""
   [1245](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/core/model.py:1245) context = self._contexts.pop()
-> [1246](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/core/model.py:1246) context.reset()

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\cobra\util\context.py:42, in HistoryManager.reset(self)
     [40](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/util/context.py:40) while self._history:
     [41](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/util/context.py:41)     entry = self._history.pop()
---> [42](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/util/context.py:42)     entry()

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\cobra\util\solver.py:204, in set_objective.<locals>.reset()
    [203](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/util/solver.py:203) def reset():
--> [204](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/util/solver.py:204)     model.solver.objective = reverse_value
    [205](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/cobra/util/solver.py:205)     model.solver.objective.direction = reverse_value.direction

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\optlang\glpk_interface.py:623, in Model.objective(self, value)
    [621](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/glpk_interface.py:621)         if variable._index is not None:
    [622](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/glpk_interface.py:622)             glp_set_obj_coef(self.problem, variable._index, 0.)
--> [623](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/glpk_interface.py:623) super(Model, self.__class__).objective.fset(self, value)
    [624](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/glpk_interface.py:624) self.update()
    [626](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/glpk_interface.py:626) offset, coef_dict, _ = parse_optimization_expression(value, linear=True)

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\optlang\interface.py:1194, in Model.objective(self, value)
   [1192](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1192)     if isinstance(atom, Variable) and (atom.problem is None or atom.problem != self):
   [1193](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1193)         self._pending_modifications.add_var.append(atom)
-> [1194](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1194) self.update()
   [1195](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1195) if self._objective is not None:
   [1196](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1196)     self._objective.problem = None

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\optlang\interface.py:1422, in Model.update(self, callback)
   [1420](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1420) add_var = self._pending_modifications.add_var
   [1421](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1421) if len(add_var) > 0:
-> [1422](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1422)     self._add_variables(add_var)
   [1423](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1423)     self._pending_modifications.add_var = []
   [1424](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1424) callback()

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\optlang\glpk_interface.py:732, in Model._add_variables(self, variables)
    [730](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/glpk_interface.py:730)     self._glpk_set_col_bounds(variable)
    [731](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/glpk_interface.py:731)     glp_set_col_kind(self.problem, variable._index, _VTYPE_TO_GLPK_VTYPE[variable.type])
--> [732](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/glpk_interface.py:732) super(Model, self)._add_variables(variables)

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\optlang\interface.py:1491, in Model._add_variables(self, variables)
   [1489](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1489) def _add_variables(self, variables):
   [1490](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1490)     for variable in variables:
-> [1491](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1491)         self._variables.append(variable)
   [1492](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1492)         self._variables_to_constraints_mapping[variable.name] = set([])
   [1493](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/interface.py:1493)         variable.problem = self

File c:\ProgramData\Anaconda3\envs\ECMpy2\lib\site-packages\optlang\container.py:172, in Container.append(self, value)
    [170](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/container.py:170) name = value.name
    [171](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/container.py:171) if name in self._dict:
--> [172](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/container.py:172)     raise ContainerAlreadyContains("Container '%s' already contains an object with name '%s'." % (self, value.name))
    [173](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/container.py:173) self._indices[name] = len(self)
    [174](file:///C:/ProgramData/Anaconda3/envs/ECMpy2/lib/site-packages/optlang/container.py:174) self._object_list.append(value)

ContainerAlreadyContains: Container '<optlang.container.Container object at 0x0000021F93DA5CA0>' already contains an object with name 'bio1'.

Environment

### Package Information
Package Version
cobra 0.21.0

Dependency Information

Package Version
appdirs 1.4.4
black missing
bumpversion missing
depinfo 2.2.0
diskcache 5.6.3
future 1.0.0
httpx 0.28.1
importlib-resources 6.4.5
isort missing
numpy 1.22.4
optlang 1.4.4
pandas 1.5.3
pydantic 1.9.2
python-libsbml 5.19.0
rich 6.2.0
ruamel.yaml 0.18.6
scipy 1.10.1
six 1.17.0
swiglpk 5.0.12
tox missing

Build Tools Information

Package Version
pip 24.3.1
setuptools 70.3.0
wheel 0.45.1

Platform Information

Windows 10-AMD64
CPython 3.8.20

Anything else?

No response

@cdiener
Copy link
Member

cdiener commented Jan 7, 2025

Hi, thanks for the report. Can you fill out the rest of the issue template, espcially the environment part so we can check which versions that affects?

Please paste the output of running depinfo --markdown cobra in your environment between the details tags below.

@cdiener
Copy link
Member

cdiener commented Jan 8, 2025

Also do you have an example with a publically available model? I could not find the one from your bug report to reproduce (eciLC858_v3.0).

@cdiener cdiener added the needs information Lacks requires information or a reproducible example to be addressed. label Jan 10, 2025
@thuwilliamwyx
Copy link
Author

Hi, thanks for the report. Can you fill out the rest of the issue template, espcially the environment part so we can check which versions that affects?

Please paste the output of running depinfo --markdown cobra in your environment between the details tags below.

I have already paste the result information of my environment.

@thuwilliamwyx
Copy link
Author

iLC858_v1.1.zip
The attachment is an example of a model used. I tried to read this model and found that using model.optimize() can run normally multiple times, but there are problems with pFBA, and the results of pFBA also seem to be quite incorrect.

import cobra
import sys
sys.path.append(r'./script/')

ecModel_file="./data/iLC858_v1.1.sbml"
model = cobra.io.read_sbml_model(ecModel_file)
print(model.objective.expression)

#######################
model.optimize()
#######################
cobra.flux_analysis.pfba(model) # ContainerAlreadyContains: Container '<optlang.container.Container object at 0x0000013E86E52700>' already contains an object with name 'bio1'.

@cdiener
Copy link
Member

cdiener commented Jan 13, 2025

Hi,

It's working fine for me in cobrapy 0.29.1:

Python 3.12.8 (main, Dec 13 2024, 13:19:48) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cobra
>>> model = cobra.io.read_sbml_model("iLC858_v1.1.sbml")
>>> model.optimize()
<Solution 2.446 at 0x7fc51e6de7e0>
>>> res = cobra.flux_analysis.pfba(model)
>>> res
<Solution 1207.393 at 0x7fc51d873440>
>>> res.fluxes.bio1
np.float64(2.4464999433818218)

Note that the pFBA objective value is from parsimonious objective (sum of absolute fluxes). The individual fluxes are kept in the fluxes attribute. It looks like your cobrapy version is 0.21.0 which is 4 years old. So updating to a more recent version should fix this.

@thuwilliamwyx
Copy link
Author

thuwilliamwyx commented Jan 13, 2025

Thanks cdiener! I'll try upgrading just the cobra and add if I have problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs information Lacks requires information or a reproducible example to be addressed.
Projects
None yet
Development

No branches or pull requests

2 participants