File: conftest.py

package info (click to toggle)
python-cobra 0.29.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,512 kB
  • sloc: python: 14,703; xml: 12,841; makefile: 137; sh: 32
file content (136 lines) | stat: -rw-r--r-- 4,910 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Contains module level fixtures and utility functions."""

from pathlib import Path
from pickle import load
from typing import Callable

import pytest
from optlang.interface import OPTIMAL

from cobra import Model


@pytest.fixture(scope="module")
def mini_model(data_directory: Path) -> Model:
    """Fixture for mini model."""
    with (data_directory / "mini.pickle").open(mode="rb") as infile:
        return load(infile)


@pytest.fixture
def compare_models() -> Callable:
    """Compare models as a fixture.

    Returns
    -------
    _compare_models: Callable
        A function that will assert that the models are identical.

    """

    def _compare_models(model_1: Model, model_2: Model) -> None:
        """Compare two models (only for testing purposes).

        Parameters
        ----------
        model_1: Model
            First model to compare.

        model_2: Model
            Second model to compare.

        This function does not return anything, and does a series of asserts on the
        two models. If the models are not identical, the asserts, and the test that
        called them will fail.

        """
        assert len(model_1.reactions) == len(model_2.reactions)
        assert len(model_1.metabolites) == len(model_2.metabolites)
        assert len(model_1.genes) == len(model_2.genes)
        assert model_1.objective.direction == model_2.objective.direction

        # check Reaction attributes
        for attr in (
            "id",
            "name",
            "lower_bound",
            "upper_bound",
            "objective_coefficient",
            "gene_reaction_rule",
        ):
            assert getattr(model_1.reactions[0], attr) == getattr(
                model_2.reactions[0], attr
            )
            assert getattr(model_1.reactions[5], attr) == getattr(
                model_2.reactions[5], attr
            )
            assert getattr(model_1.reactions[-1], attr) == getattr(
                model_2.reactions[-1], attr
            )

        # check Metabolite attributes
        for attr in ("id", "name", "compartment", "formula", "charge"):
            assert getattr(model_1.metabolites[0], attr) == getattr(
                model_2.metabolites[0], attr
            )
            assert getattr(model_1.metabolites[5], attr) == getattr(
                model_2.metabolites[5], attr
            )
            assert getattr(model_1.metabolites[-1], attr) == getattr(
                model_2.metabolites[-1], attr
            )
            assert len(model_1.reactions[0].metabolites) == len(
                model_2.reactions[0].metabolites
            )
        # TODO: either relax gene attribute checking or fix models for testing.
        # check Gene attributes
        # for attr in ("id", "name"):
        #     assert getattr(model_1.genes[0], attr) == getattr(model_2.genes[0],
        #                                                       attr)
        #     assert getattr(model_1.genes[10], attr) == getattr(model_2.genes[10],
        #                                                        attr)
        #     assert getattr(model_1.genes[-1], attr) == getattr(model_2.genes[-1],
        #                                                        attr)

        assert len(model_1.reactions[8].metabolites) == len(
            model_2.reactions[8].metabolites
        )
        assert len(model_1.reactions[-1].metabolites) == len(
            model_2.reactions[-1].metabolites
        )
        assert len(model_1.genes) == len(model_2.genes)

        # ensure they have the same solution max
        solution_1 = model_1.optimize()
        solution_2 = model_2.optimize()
        if solution_1.status == OPTIMAL and solution_2.status == OPTIMAL:
            assert abs(
                solution_1.objective_value - solution_2.objective_value
            ) == pytest.approx(0.0)
        else:
            assert solution_1.status == solution_2.status

        # ensure the references are correct
        # metabolite -> model reference
        assert model_1.metabolites[0]._model is model_1
        assert model_2.metabolites[0]._model is model_2

        # reaction -> model reference
        assert model_1.reactions[0]._model is model_1
        assert model_2.reactions[0]._model is model_2

        # gene -> model reference
        if model_1.genes:
            assert model_1.genes[0]._model is model_1
            assert model_2.genes[0]._model is model_2

        # extra comparisons
        # assert model_1.compartments == model_2.compartments
        # assert dict(model_1.metabolites[4].annotation) == dict(
        #     model_2.metabolites[4].annotation)
        # assert dict(model_1.reactions[4].annotation) == dict(
        #     model_2.reactions[4].annotation)
        # assert dict(model_1.genes[5].annotation) == dict(
        #     model_2.genes[5].annotation)

    return _compare_models