|
| 1 | +from pyenzyme.thinlayers.base import BaseThinLayer |
| 2 | +from pyenzyme.versions.v2 import EnzymeMLDocument, EquationType |
| 3 | + |
| 4 | +# Mock data for creating test species measurements |
| 5 | +MOCK_DATA = { |
| 6 | + "initial": 1.0, |
| 7 | + "time": [1.0, 2.0, 3.0, 4.0], |
| 8 | + "data": [1.0, 2.0, 3.0, 4.0], |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +class TestThinLayer: |
| 13 | + """Test suite for BaseThinLayer functionality.""" |
| 14 | + |
| 15 | + def test_remove_unmodeled_species_reaction(self): |
| 16 | + """ |
| 17 | + Test that unmodeled species are removed when they're not part of any reaction. |
| 18 | +
|
| 19 | + This test verifies that: |
| 20 | + - Species not referenced in reactions are removed from the document |
| 21 | + - Measurements containing only unmodeled species are removed |
| 22 | + - Measurements with mixed modeled/unmodeled species keep only modeled ones |
| 23 | + """ |
| 24 | + enzmldoc = self._create_enzmldoc() |
| 25 | + |
| 26 | + # Add reaction with only Substrate and Product (Unmodeled is not included) |
| 27 | + reaction = enzmldoc.add_to_reactions(id="R1", name="R1") |
| 28 | + reaction.add_to_reactants(species_id="Substrate", stoichiometry=1) |
| 29 | + reaction.add_to_products(species_id="Product", stoichiometry=1) |
| 30 | + |
| 31 | + # Remove unmodeled species |
| 32 | + thinlayer = MockThinLayer(enzmldoc) |
| 33 | + tl_enzmldoc = thinlayer.optimize() |
| 34 | + |
| 35 | + assert len(tl_enzmldoc.small_molecules) == 2, ( |
| 36 | + f"Unmodeled small molecules should be removed, but {len(tl_enzmldoc.small_molecules)} remain." |
| 37 | + ) |
| 38 | + assert len(tl_enzmldoc.measurements) == 2, ( |
| 39 | + f"Unmodeled measurements should be removed, but {len(tl_enzmldoc.measurements)} remain." |
| 40 | + ) |
| 41 | + |
| 42 | + measurement_has_unmodeled: list[str] = [] |
| 43 | + |
| 44 | + for measurement in tl_enzmldoc.measurements: |
| 45 | + for species_data in measurement.species_data: |
| 46 | + if species_data.species_id == "Unmodeled": |
| 47 | + measurement_has_unmodeled.append(measurement.id) |
| 48 | + |
| 49 | + assert len(measurement_has_unmodeled) == 0, ( |
| 50 | + f"Unmodeled species should be removed, but appears in measurements {measurement_has_unmodeled}." |
| 51 | + ) |
| 52 | + |
| 53 | + def test_remove_unmodeled_species_odes(self): |
| 54 | + """ |
| 55 | + Test that unmodeled species are removed when they're not part of any ODE. |
| 56 | +
|
| 57 | + This test verifies that: |
| 58 | + - Species not referenced in ODE equations are removed from the document |
| 59 | + - Measurements containing only unmodeled species are removed |
| 60 | + - Measurements with mixed modeled/unmodeled species keep only modeled ones |
| 61 | + """ |
| 62 | + enzmldoc = self._create_enzmldoc() |
| 63 | + |
| 64 | + # Add ODEs with only Substrate and Product (Unmodeled is not included) |
| 65 | + enzmldoc.add_to_equations( |
| 66 | + species_id="Substrate", |
| 67 | + equation_type=EquationType.ODE, |
| 68 | + equation="-Substrate", |
| 69 | + ) |
| 70 | + |
| 71 | + enzmldoc.add_to_equations( |
| 72 | + species_id="Product", |
| 73 | + equation_type=EquationType.ODE, |
| 74 | + equation="Substrate", |
| 75 | + ) |
| 76 | + |
| 77 | + # Remove unmodeled species |
| 78 | + thinlayer = MockThinLayer(enzmldoc) |
| 79 | + tl_enzmldoc = thinlayer.optimize() |
| 80 | + |
| 81 | + assert len(tl_enzmldoc.small_molecules) == 2, ( |
| 82 | + f"Unmodeled small molecules should be removed, but {len(tl_enzmldoc.small_molecules)} remain." |
| 83 | + ) |
| 84 | + assert len(tl_enzmldoc.measurements) == 2, ( |
| 85 | + f"Unmodeled measurements should be removed, but {len(tl_enzmldoc.measurements)} remain." |
| 86 | + ) |
| 87 | + |
| 88 | + measurement_has_unmodeled: list[str] = [] |
| 89 | + |
| 90 | + for measurement in tl_enzmldoc.measurements: |
| 91 | + for species_data in measurement.species_data: |
| 92 | + if species_data.species_id == "Unmodeled": |
| 93 | + measurement_has_unmodeled.append(measurement.id) |
| 94 | + |
| 95 | + assert len(measurement_has_unmodeled) == 0, ( |
| 96 | + f"Unmodeled species should be removed, but appears in measurements {measurement_has_unmodeled}." |
| 97 | + ) |
| 98 | + |
| 99 | + def _create_enzmldoc(self) -> EnzymeMLDocument: |
| 100 | + """ |
| 101 | + Create a test EnzymeML document with various measurement scenarios. |
| 102 | +
|
| 103 | + Creates a document with: |
| 104 | + - Three species: Substrate, Product, and Unmodeled |
| 105 | + - Four measurements: |
| 106 | + - M1: Contains all three species (mixed modeled/unmodeled) |
| 107 | + - M2: Contains only modeled species (Substrate, Product) |
| 108 | + - M3: Contains only unmodeled species (Unmodeled) |
| 109 | + - M4: Empty measurement (no species data) |
| 110 | +
|
| 111 | + Returns: |
| 112 | + EnzymeMLDocument: A test document for use in unit tests. |
| 113 | + """ |
| 114 | + enzmldoc = EnzymeMLDocument(name="Test") |
| 115 | + |
| 116 | + # Add small molecules |
| 117 | + substrate = enzmldoc.add_to_small_molecules(id="Substrate", name="Substrate") |
| 118 | + product = enzmldoc.add_to_small_molecules(id="Product", name="Product") |
| 119 | + unmodeled = enzmldoc.add_to_small_molecules(id="Unmodeled", name="Unmodeled") |
| 120 | + |
| 121 | + # Add a measurement with unmodeled species |
| 122 | + measurement = enzmldoc.add_to_measurements(id="M1", name="M1") |
| 123 | + measurement.add_to_species_data(species_id=substrate.id, **MOCK_DATA) |
| 124 | + measurement.add_to_species_data(species_id=product.id, **MOCK_DATA) |
| 125 | + measurement.add_to_species_data(species_id=unmodeled.id, **MOCK_DATA) |
| 126 | + |
| 127 | + # Add a Measurement only with modeled species |
| 128 | + measurement = enzmldoc.add_to_measurements(id="M2", name="M2") |
| 129 | + measurement.add_to_species_data(species_id=substrate.id, **MOCK_DATA) |
| 130 | + measurement.add_to_species_data(species_id=product.id, **MOCK_DATA) |
| 131 | + |
| 132 | + # Add a Measurement with only unmodeled species |
| 133 | + measurement = enzmldoc.add_to_measurements(id="M3", name="M3") |
| 134 | + measurement.add_to_species_data(species_id=unmodeled.id, **MOCK_DATA) |
| 135 | + |
| 136 | + # Add an empty measurement |
| 137 | + measurement = enzmldoc.add_to_measurements(id="M4", name="M4") |
| 138 | + |
| 139 | + return enzmldoc |
| 140 | + |
| 141 | + |
| 142 | +class MockThinLayer(BaseThinLayer): |
| 143 | + """ |
| 144 | + Mock implementation of BaseThinLayer for testing purposes. |
| 145 | +
|
| 146 | + This class provides minimal implementations of the abstract methods |
| 147 | + to allow testing of the base class functionality without requiring |
| 148 | + a full thin layer implementation. |
| 149 | + """ |
| 150 | + |
| 151 | + def integrate(self, *args, **kwargs): |
| 152 | + """Mock integration method that does nothing.""" |
| 153 | + pass |
| 154 | + |
| 155 | + def optimize(self, *args, **kwargs): |
| 156 | + """Mock optimization method that does nothing.""" |
| 157 | + return self._remove_unmodeled_species(self.enzmldoc) |
| 158 | + |
| 159 | + def write(self, *args, **kwargs): |
| 160 | + """Mock write method that does nothing.""" |
| 161 | + pass |
0 commit comments