Repository navigation
Conversation
Model.copy() carries a package's variables and constraints onto the copy, but the copy gets its own MSPackageManager and therefore a FRESH, empty package object. Rebuilding on the copy then re-adds names the solver already holds: ContainerAlreadyContains: Container ... already contains an object with name 'EX_cpd00027_e0_tf' so a package is unusable on a copied model. This adds BaseFBAPkg.adopt_existing_objects(), which rebinds a package to the matching objects already on model.solver by matching the final underscore-delimited segment of each name against the package's registered types -- the inverse of the `name + "_" + obj_type` scheme the build methods use. An `adopt_existing=False` keyword on __init__ runs it at construction. It is OPT-IN, and the default is off, so nothing changes for existing callers. The reason is that the match is on the name alone, so it adopts any solver object whose name ends in `_<registered type>` whether or not the package created it. Measured: on a model holding an ordinary reaction `rxn00042_tf`, TotalFluxPkg adopts that reaction's own flux variable as the total-flux variable for `rxn00042`; build_package then finds the key taken and never builds the real variable or its coupling constraint, with no error raised, and clear() would delete the reaction's variable from the solver outright. A test pins that hazard rather than leaving it undocumented. Tests: tests/fbapkg/test_basefbapkg_adoption.py, 5 cases -- the default being off, the ContainerAlreadyContains failure that motivates the capability, adoption rebinding a package on a copied model and leaving a rebuild a no-op, the constructor keyword, and the name-collision hazard. 3 of the 5 fail against the unpatched tree; the other two characterise existing behaviour and hold on both sides, which is why they are there. Full suite: 86 passed. pre-commit run --all-files is clean apart from the pre-existing executable-shebang hook on upstream file modes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a capability behind an opt-in flag. Our fork runs the scan unconditionally in
__init__; it is upstreamed as opt-in instead, for the reason in the hazard section below.The failure it exists for
Model.copy()carries a package's variables and constraints onto the copy, but the copy gets its ownMSPackageManagerand therefore a fresh, empty package object. Rebuilding on the copy re-adds names the solver already holds:So a
BaseFBAPkgsubclass is simply unusable on a copied model — which matters, because copying models is routine inmsmodelutl, gapfilling and ATP correction.What this adds
BaseFBAPkg.adopt_existing_objects()rebinds a package to the matching objects already onmodel.solver, by matching the final underscore-delimited segment of each object's name against the package's registered types — the inverse of thename + "_" + obj_typeschemebuild_variableandbuild_constraintuse. Anadopt_existing=Falsekeyword on__init__runs it at construction time.After adoption the package owns the copy's objects, and a subsequent
build_package()is a no-op instead of a collision.Why it is opt-in, and the hazard
The match is on the name alone, so it adopts any solver object whose name ends in
_<registered type>whether or not the package created it. Measured, on a model holding an ordinary reaction calledrxn00042_tf:TotalFluxPkgadopts that reaction's own flux variable as the total-flux variable forrxn00042;build_package()then finds the key taken and never builds the real total-flux variable or its coupling constraint;tfbound of 100, and the model silently optimises something other than what the package describes;clear()would delete that reaction's variable from the solver outright, leaving the reaction inmodel.reactionswithflux_expressionraisingKeyError.Two-character type names (
tf,pk,fu,ru) make that collision cheap to hit by accident. A test pins the hazard rather than leaving it undocumented.So: default off, available where the caller knows the model's naming does not collide. A stricter match — recording the names a package created and adopting only those — would remove the hazard and make a default-on scan safe, but it is a wider change to
build_variableandbuild_constraint, so this PR keeps the name-suffix match behind the flag. Request the stricter form here if you would rather have that and I will build it.Tests
tests/fbapkg/test_basefbapkg_adoption.py, 5 cases:ContainerAlreadyContainsfailure on a copied model that motivates the capability;3 of the 5 fail against the unpatched tree. The other two characterise existing upstream behaviour and hold on both sides — that is deliberate, since their job is to pin the default and the motivating failure, not to prove the change.
Full suite: 86 passed.
pre-commit run --all-filesis clean apart from the pre-existingcheck that executables have shebangshook on upstream file modes.Relationship to the other two
Independent of #170, and independent of #171 though both touch
basefbapkg.pyin different regions (__init__here,clear()and a media accessor there). Whichever of the two merges second may want a trivial rebase — tell me and I will do it.🤖 Generated with Claude Code