Skip to content

Commit

Permalink
Improve backwards compatibility for pickled models (#1332)
Browse files Browse the repository at this point in the history
* fill in _gpr if missing

* make statement more general to capture v0.22.x as well
  • Loading branch information
cdiener committed May 9, 2023
1 parent b535a4f commit c6f0bb4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

## Fixes

COBRApy is now compatible with the latest numpy versions and pin to a lower version
COBRApy is now compatible with the latest numpy versions and pin to a lower version
has been removed.

## Other

Backwards compatibility for pickled models has been improved.

## Deprecated features

## Backwards incompatible changes
2 changes: 2 additions & 0 deletions src/cobra/core/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,8 @@ def __setstate__(self, state: Dict) -> None:
state["_upper_bound"] = state.pop("upper_bound")

# Used for efficient storage in newer cobrapy versions
if "_gpr" not in state:
state["_gpr"] = state["_gene_reaction_rule"]
if type(state["_gpr"]) is str:
state["_gpr"] = GPR.from_string(state["_gpr"])

Expand Down
11 changes: 11 additions & 0 deletions tests/test_core/test_core_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,3 +1037,14 @@ def test_gpr_serialization(model: Model) -> None:
"""Verify that reactions GPRs are serialized compactly as str."""
state = model.reactions[0].__getstate__()
assert type(state["_gpr"]) == str


def test_gpr_serialization_backwards_compatibility(model: Model) -> None:
"""Verify that GPR serialization is backwards compatible."""
state = model.reactions[0].__getstate__()
print(state)
state["gene_reaction_rule"] = state["_gpr"] # old format
del state["_gpr"]
rxn = Reaction("test")
rxn.__setstate__(state)
assert isinstance(rxn.gpr, GPR)

0 comments on commit c6f0bb4

Please sign in to comment.