Skip to content

Commit

Permalink
now almost all of the tests work
Browse files Browse the repository at this point in the history
  • Loading branch information
uri.akavia committed Jun 30, 2022
1 parent 9c42d8c commit 1a6bca6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 52 deletions.
49 changes: 0 additions & 49 deletions src/cobra/io/dict.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
"""Provide functions for cobrapy objects to generic Python objects and vice-versa."""
import itertools
import re
from collections import OrderedDict, defaultdict
from typing import TYPE_CHECKING, Dict, List, Sequence, Set, Tuple, Union

import numpy as np

from ..core import (
ConstraintComponent,
Gene,
Group,
Metabolite,
Model,
Reaction,
UserDefinedConstraint,
)
from ..io.sbml import (
F_GENE,
Expand Down Expand Up @@ -520,52 +517,6 @@ def group_from_dict(
new_group.add_members(cobra_members)
return new_group

def const_comp_to_dict(component: ConstraintComponent) -> Dict:
new_const_comp = OrderedDict()
for key in _REQUIRED_CONSTRAINT_COMP_ATTRIBUTES:
new_const_comp[key] = _fix_type(getattr(component, key))
_update_optional(
component,
new_const_comp,
_OPTIONAL_CONSTRAINT_COMP_ATTRIBUTES,
_ORDERED_OPTIONAL_CONSTRAINT_COMP_KEYS,
)
return new_const_comp


def user_defined_const_to_dict(constraint: UserDefinedConstraint) -> Dict:
new_const = OrderedDict()
for key in _REQUIRED_CONSTRAINT_ATTRIBUTES:
if key != "constraint_comps":
new_const[key] = _fix_type(getattr(constraint, key))
continue
new_const["constraint_comps"] = list(
map(const_comp_to_dict, constraint.constraint_comps)
)
_update_optional(
constraint,
new_const,
_OPTIONAL_CONSTRAINT_ATTRIBUTES,
_ORDERED_OPTIONAL_CONSTRAINT_KEYS,
)
return new_const


def user_defined_const_from_dict(constraint: Dict) -> UserDefinedConstraint:
new_user_defined_const = UserDefinedConstraint()
for k, v in constraint.items():
if k == "constraint_comps":
for comp in v:
new_comp = ConstraintComponent(**comp)
new_user_defined_const.add_constraint_comps([new_comp])
elif k == "annotation":
continue
elif k == "notes":
continue
else:
setattr(new_user_defined_const, k, v)
return new_user_defined_const


def model_to_dict(
model: Model, sort: bool = False, f_replace: dict = F_REPLACE # noqa: W0102
Expand Down
6 changes: 3 additions & 3 deletions tests/test_core/test_udconstraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def test_user_defined_constraints_on_single_variable():


def test_json_reading_writing(model, tmp_path):
cc1 = Variable("FBA")
cc2 = Variable('NH4t')
cc1 = model.reactions.get_by_id('FBA').flux_expression
cc2 = model.reactions.get_by_id('NH4t').flux_expression
cc3 = Variable('difference')
c1 = Constraint(cc1 - cc2 - cc3, lb=0, ub=0, name='c1')

Expand All @@ -109,7 +109,7 @@ def test_json_reading_writing(model, tmp_path):
variable_names = {var.name for var in const_1.variables}
assert 'FBA' in variable_names
solution2 = model.optimize()
assert solution1 == pytest.approx(solution2)
assert solution1.objective_value == pytest.approx(solution2.objective_value)


def test_user_defined_constraints_read_write_json(data_directory, tmp_path):
Expand Down

0 comments on commit 1a6bca6

Please sign in to comment.