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 · 2 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

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
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